createRequest.js 3.7 KB

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