geturl.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getUrl = exports.createGetUrl = void 0;
  4. const tslib_1 = require("tslib");
  5. const http_1 = tslib_1.__importDefault(require("http"));
  6. const https_1 = tslib_1.__importDefault(require("https"));
  7. const zlib_1 = require("zlib");
  8. const errors_js_1 = require("./errors.js");
  9. const data_js_1 = require("./data.js");
  10. /**
  11. * @_ignore:
  12. */
  13. function createGetUrl(options) {
  14. async function getUrl(req, signal) {
  15. // Make sure we weren't cancelled before sending
  16. (0, errors_js_1.assert)(signal == null || !signal.cancelled, "request cancelled before sending", "CANCELLED");
  17. const protocol = req.url.split(":")[0].toLowerCase();
  18. (0, errors_js_1.assert)(protocol === "http" || protocol === "https", `unsupported protocol ${protocol}`, "UNSUPPORTED_OPERATION", {
  19. info: { protocol },
  20. operation: "request"
  21. });
  22. (0, errors_js_1.assert)(protocol === "https" || !req.credentials || req.allowInsecureAuthentication, "insecure authorized connections unsupported", "UNSUPPORTED_OPERATION", {
  23. operation: "request"
  24. });
  25. const method = req.method;
  26. const headers = Object.assign({}, req.headers);
  27. const reqOptions = { method, headers };
  28. if (options) {
  29. if (options.agent) {
  30. reqOptions.agent = options.agent;
  31. }
  32. }
  33. // Create a Node-specific AbortController, if available
  34. let abort = null;
  35. try {
  36. abort = new AbortController();
  37. reqOptions.abort = abort.signal;
  38. }
  39. catch (e) {
  40. console.log(e);
  41. }
  42. const request = ((protocol === "http") ? http_1.default : https_1.default).request(req.url, reqOptions);
  43. request.setTimeout(req.timeout);
  44. const body = req.body;
  45. if (body) {
  46. request.write(Buffer.from(body));
  47. }
  48. request.end();
  49. return new Promise((resolve, reject) => {
  50. if (signal) {
  51. signal.addListener(() => {
  52. if (abort) {
  53. abort.abort();
  54. }
  55. reject((0, errors_js_1.makeError)("request cancelled", "CANCELLED"));
  56. });
  57. }
  58. request.on("timeout", () => {
  59. reject((0, errors_js_1.makeError)("request timeout", "TIMEOUT"));
  60. });
  61. request.once("response", (resp) => {
  62. const statusCode = resp.statusCode || 0;
  63. const statusMessage = resp.statusMessage || "";
  64. const headers = Object.keys(resp.headers || {}).reduce((accum, name) => {
  65. let value = resp.headers[name] || "";
  66. if (Array.isArray(value)) {
  67. value = value.join(", ");
  68. }
  69. accum[name] = value;
  70. return accum;
  71. }, {});
  72. let body = null;
  73. //resp.setEncoding("utf8");
  74. resp.on("data", (chunk) => {
  75. if (signal) {
  76. try {
  77. signal.checkSignal();
  78. }
  79. catch (error) {
  80. return reject(error);
  81. }
  82. }
  83. if (body == null) {
  84. body = chunk;
  85. }
  86. else {
  87. const newBody = new Uint8Array(body.length + chunk.length);
  88. newBody.set(body, 0);
  89. newBody.set(chunk, body.length);
  90. body = newBody;
  91. }
  92. });
  93. resp.on("end", () => {
  94. if (headers["content-encoding"] === "gzip" && body) {
  95. body = (0, data_js_1.getBytes)((0, zlib_1.gunzipSync)(body));
  96. }
  97. resolve({ statusCode, statusMessage, headers, body });
  98. });
  99. resp.on("error", (error) => {
  100. //@TODO: Should this just return nornal response with a server error?
  101. error.response = { statusCode, statusMessage, headers, body };
  102. reject(error);
  103. });
  104. });
  105. request.on("error", (error) => { reject(error); });
  106. });
  107. }
  108. return getUrl;
  109. }
  110. exports.createGetUrl = createGetUrl;
  111. // @TODO: remove in v7; provided for backwards compat
  112. const defaultGetUrl = createGetUrl({});
  113. /**
  114. * @_ignore:
  115. */
  116. async function getUrl(req, signal) {
  117. return defaultGetUrl(req, signal);
  118. }
  119. exports.getUrl = getUrl;
  120. //# sourceMappingURL=geturl.js.map