createRequest.js 4.0 KB

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