createRequest.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 'x-oss-user-agent': this.userAgent
  34. };
  35. if (this.userAgent.includes('nodejs')) {
  36. headers['User-Agent'] = this.userAgent;
  37. }
  38. if (this.options.isRequestPay) {
  39. Object.assign(headers, { 'x-oss-request-payer': 'requester' });
  40. }
  41. if (this.options.stsToken) {
  42. headers['x-oss-security-token'] = this.options.stsToken;
  43. }
  44. copy(params.headers).to(headers);
  45. if (!getHeader(headers, 'Content-Type')) {
  46. if (params.mime && params.mime.indexOf('/') > 0) {
  47. headers['Content-Type'] = params.mime;
  48. } else {
  49. headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
  50. }
  51. }
  52. if (!getHeader(headers, 'Content-Type')) {
  53. delHeader(headers, 'Content-Type');
  54. }
  55. if (params.content) {
  56. headers['Content-Md5'] = crypto
  57. .createHash('md5')
  58. .update(Buffer.from(params.content, 'utf8'))
  59. .digest('base64');
  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: 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. }