租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.7 KiB

3 months ago
  1. const crypto = require('crypto');
  2. const debug = require('debug')('ali-oss');
  3. const _isString = require('lodash/isString');
  4. const _isArray = require('lodash/isArray');
  5. const _isObject = require('lodash/isObject');
  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. interface Headers {
  16. [propName: string]: any;
  17. 'x-oss-date': string;
  18. 'x-oss-user-agent'?: string;
  19. }
  20. interface ReqParams {
  21. [propName: string]: any;
  22. }
  23. function getHeader(headers: Headers, name: string) {
  24. return headers[name] || headers[name.toLowerCase()];
  25. }
  26. function delHeader(headers: Headers, name: string) {
  27. delete headers[name];
  28. delete headers[name.toLowerCase()];
  29. }
  30. export function createRequest(this: any, params) {
  31. let date = new Date();
  32. if (this.options.amendTimeSkewed) {
  33. date = +new Date() + this.options.amendTimeSkewed;
  34. }
  35. const headers: Headers = {
  36. 'x-oss-date': dateFormat(
  37. date,
  38. this.options.authorizationV4 ? "UTC:yyyymmdd'T'HHMMss'Z'" : "UTC:ddd, dd mmm yyyy HH:MM:ss 'GMT'"
  39. )
  40. };
  41. if (this.options.authorizationV4) {
  42. headers['x-oss-content-sha256'] = 'UNSIGNED-PAYLOAD';
  43. }
  44. if (typeof window !== 'undefined') {
  45. headers['x-oss-user-agent'] = this.userAgent;
  46. }
  47. if (this.userAgent.includes('nodejs')) {
  48. headers['User-Agent'] = this.userAgent;
  49. }
  50. if (this.options.isRequestPay) {
  51. Object.assign(headers, { 'x-oss-request-payer': 'requester' });
  52. }
  53. if (this.options.stsToken) {
  54. headers['x-oss-security-token'] = this.options.stsToken;
  55. }
  56. copy(params.headers).to(headers);
  57. if (!getHeader(headers, 'Content-Type')) {
  58. if (params.mime && params.mime.indexOf('/') > 0) {
  59. headers['Content-Type'] = params.mime;
  60. } else if (isDingTalk()) {
  61. headers['Content-Type'] = 'application/octet-stream';
  62. } else {
  63. headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || ''));
  64. }
  65. }
  66. if (!getHeader(headers, 'Content-Type')) {
  67. delHeader(headers, 'Content-Type');
  68. }
  69. if (params.content) {
  70. if (!params.disabledMD5) {
  71. if (!params.headers || !params.headers['Content-MD5']) {
  72. headers['Content-MD5'] = crypto.createHash('md5').update(Buffer.from(params.content, 'utf8')).digest('base64');
  73. } else {
  74. headers['Content-MD5'] = params.headers['Content-MD5'];
  75. }
  76. }
  77. if (!headers['Content-Length']) {
  78. headers['Content-Length'] = params.content.length;
  79. }
  80. }
  81. const { hasOwnProperty } = Object.prototype;
  82. for (const k in headers) {
  83. if (headers[k] && hasOwnProperty.call(headers, k)) {
  84. headers[k] = encoder(String(headers[k]), this.options.headerEncoding);
  85. }
  86. }
  87. const queries = {};
  88. if (_isString(params.subres)) {
  89. queries[params.subres] = null;
  90. } else if (_isArray(params.subres)) {
  91. params.subres.forEach(v => {
  92. queries[v] = null;
  93. });
  94. } else if (_isObject(params.subres)) {
  95. Object.entries(params.subres).forEach(v => {
  96. queries[v[0]] = v[1] === '' ? null : v[1];
  97. });
  98. }
  99. if (_isObject(params.query)) {
  100. Object.entries(params.query).forEach(v => {
  101. queries[v[0]] = v[1];
  102. });
  103. }
  104. headers.authorization = this.options.authorizationV4
  105. ? this.authorizationV4(
  106. params.method,
  107. {
  108. headers,
  109. queries
  110. },
  111. params.bucket,
  112. params.object,
  113. params.additionalHeaders
  114. )
  115. : this.authorization(params.method, this._getResource(params), params.subres, headers, this.options.headerEncoding);
  116. // const url = this._getReqUrl(params);
  117. if (isIP(this.options.endpoint.hostname)) {
  118. const { region, internal, secure } = this.options;
  119. const hostInfo = setRegion(region, internal, secure);
  120. headers.host = `${params.bucket}.${hostInfo.host}`;
  121. }
  122. const url = getReqUrl.bind(this)(params);
  123. debug('request %s %s, with headers %j, !!stream: %s', params.method, url, headers, !!params.stream);
  124. const timeout = params.timeout || this.options.timeout;
  125. const reqParams: ReqParams = {
  126. method: params.method,
  127. content: params.content,
  128. stream: params.stream,
  129. headers,
  130. timeout,
  131. writeStream: params.writeStream,
  132. customResponse: params.customResponse,
  133. ctx: params.ctx || this.ctx
  134. };
  135. if (this.agent) {
  136. reqParams.agent = this.agent;
  137. }
  138. if (this.httpsAgent) {
  139. reqParams.httpsAgent = this.httpsAgent;
  140. }
  141. reqParams.enableProxy = !!this.options.enableProxy;
  142. reqParams.proxy = this.options.proxy ? this.options.proxy : null;
  143. return {
  144. url,
  145. params: reqParams
  146. };
  147. }