租房小程序前端代码
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.

139 lines
4.9 KiB

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