createRequest.ts 3.8 KB

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