geturl.ts 4.5 KB

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