getReqUrl.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getReqUrl = void 0;
  7. const copy_to_1 = __importDefault(require("copy-to"));
  8. const url_1 = __importDefault(require("url"));
  9. const merge_descriptors_1 = __importDefault(require("merge-descriptors"));
  10. const is_type_of_1 = __importDefault(require("is-type-of"));
  11. const isIP_1 = require("../utils/isIP");
  12. const checkConfigValid_1 = require("../utils/checkConfigValid");
  13. function getReqUrl(params) {
  14. const ep = {};
  15. const isCname = this.options.cname;
  16. checkConfigValid_1.checkConfigValid(this.options.endpoint, 'endpoint');
  17. copy_to_1.default(this.options.endpoint, false).to(ep);
  18. if (params.bucket && !isCname && !isIP_1.isIP(ep.hostname) && !this.options.sldEnable) {
  19. ep.host = `${params.bucket}.${ep.host}`;
  20. }
  21. let resourcePath = '/';
  22. if (params.bucket && this.options.sldEnable) {
  23. resourcePath += `${params.bucket}/`;
  24. }
  25. if (params.object) {
  26. // Preserve '/' in result url
  27. resourcePath += this._escape(params.object).replace(/\+/g, '%2B');
  28. }
  29. ep.pathname = resourcePath;
  30. const query = {};
  31. if (params.query) {
  32. merge_descriptors_1.default(query, params.query);
  33. }
  34. if (params.subres) {
  35. let subresAsQuery = {};
  36. if (is_type_of_1.default.string(params.subres)) {
  37. subresAsQuery[params.subres] = '';
  38. }
  39. else if (is_type_of_1.default.array(params.subres)) {
  40. params.subres.forEach(k => {
  41. subresAsQuery[k] = '';
  42. });
  43. }
  44. else {
  45. subresAsQuery = params.subres;
  46. }
  47. merge_descriptors_1.default(query, subresAsQuery);
  48. }
  49. ep.query = query;
  50. return url_1.default.format(ep);
  51. }
  52. exports.getReqUrl = getReqUrl;