createRequest.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const crypto = require('crypto');
  2. const debug = require('debug')('ali-oss');
  3. const mime = require('mime');
  4. const dateFormat = require('dateformat');
  5. const copy = require('copy-to');
  6. const path = require('path');
  7. const { encoder } = require('./encoder');
  8. const { isIP } = require('./isIP');
  9. const { setRegion } = require('./setRegion');
  10. const { getReqUrl } = require('../client/getReqUrl');
  11. interface Headers {
  12. [propName: string]: any
  13. 'x-oss-date': string,
  14. 'x-oss-user-agent'?: string,
  15. }
  16. interface ReqParams {
  17. [propName: string]: any
  18. }
  19. function getHeader(headers: Headers, name: string) {
  20. return headers[name] || headers[name.toLowerCase()];
  21. }
  22. function delHeader(headers: Headers, name: string) {
  23. delete headers[name];
  24. delete headers[name.toLowerCase()];
  25. }
  26. export function createRequest(this: any, params) {
  27. let date = new Date();
  28. if (this.options.amendTimeSkewed) {
  29. date = +new Date() + this.options.amendTimeSkewed;
  30. }
  31. const headers: Headers = {
  32. 'x-oss-date': dateFormat(date, 'UTC:ddd, dd mmm yyyy HH:MM:ss \'GMT\''),
  33. };
  34. if (typeof window !== 'undefined') {
  35. headers['x-oss-user-agent'] = this.userAgent;
  36. }
  37. if (this.userAgent.includes('nodejs')) {
  38. headers['User-Agent'] = this.userAgent;
  39. }
  40. if (this.options.isRequestPay) {
  41. Object.assign(headers, { 'x-oss-request-payer': 'requester' });
  42. }
  43. if (this.options.stsToken) {
  44. headers['x-oss-security-token'] = this.options.stsToken;
  45. }
  46. copy(params.headers).to(headers);
  47. if (!getHeader(headers, 'Content-Type')) {
  48. if (params.mime && params.mime.indexOf('/') > 0) {
  49. headers['Content-Type'] = params.mime;
  50. } else {
  51. headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
  52. }
  53. }
  54. if (!getHeader(headers, 'Content-Type')) {
  55. delHeader(headers, 'Content-Type');
  56. }
  57. if (params.content) {
  58. if (!params.disabledMD5) {
  59. headers['Content-MD5'] = crypto
  60. .createHash('md5')
  61. .update(Buffer.from(params.content, 'utf8'))
  62. .digest('base64');
  63. }
  64. if (!headers['Content-Length']) {
  65. headers['Content-Length'] = params.content.length;
  66. }
  67. }
  68. const { hasOwnProperty } = Object.prototype;
  69. for (const k in headers) {
  70. if (headers[k] && hasOwnProperty.call(headers, k)) {
  71. headers[k] = encoder(String(headers[k]), this.options.headerEncoding);
  72. }
  73. }
  74. const authResource = this._getResource(params);
  75. headers.authorization = this.authorization(params.method, authResource, params.subres, headers, this.options.headerEncoding);
  76. // const url = this._getReqUrl(params);
  77. if (isIP(this.options.endpoint.hostname)) {
  78. const { region, internal, secure } = this.options;
  79. const hostInfo = setRegion(region, internal, secure);
  80. headers.host = `${params.bucket}.${hostInfo.host}`;
  81. }
  82. const url = getReqUrl.bind(this)(params);
  83. debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream);
  84. const timeout = params.timeout || this.options.timeout;
  85. const reqParams: ReqParams = {
  86. method: params.method,
  87. content: params.content,
  88. stream: params.stream,
  89. headers,
  90. timeout,
  91. writeStream: params.writeStream,
  92. customResponse: params.customResponse,
  93. ctx: params.ctx || this.ctx
  94. };
  95. if (this.agent) {
  96. reqParams.agent = this.agent;
  97. }
  98. if (this.httpsAgent) {
  99. reqParams.httpsAgent = this.httpsAgent;
  100. }
  101. reqParams.enableProxy = !!this.options.enableProxy;
  102. reqParams.proxy = this.options.proxy ? this.options.proxy : null;
  103. return {
  104. url,
  105. params: reqParams
  106. };
  107. }