index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var proxy_exports = {};
  19. __export(proxy_exports, {
  20. proxy: () => proxy
  21. });
  22. module.exports = __toCommonJS(proxy_exports);
  23. var import_http_exception = require("../../http-exception");
  24. const hopByHopHeaders = [
  25. "connection",
  26. "keep-alive",
  27. "proxy-authenticate",
  28. "proxy-authorization",
  29. "te",
  30. "trailer",
  31. "transfer-encoding",
  32. "upgrade"
  33. ];
  34. const ALLOWED_TOKEN_PATTERN = /^[!#$%&'*+\-.0-9A-Z^_`a-z|~]+$/;
  35. const buildRequestInitFromRequest = (request, strictConnectionProcessing) => {
  36. if (!request) {
  37. return {};
  38. }
  39. const headers = new Headers(request.headers);
  40. if (strictConnectionProcessing) {
  41. const connectionValue = headers.get("connection");
  42. if (connectionValue) {
  43. const headerNames = connectionValue.split(",").map((h) => h.trim());
  44. const invalidHeaders = headerNames.filter((h) => !ALLOWED_TOKEN_PATTERN.test(h));
  45. if (invalidHeaders.length > 0) {
  46. throw new import_http_exception.HTTPException(400, {
  47. message: `Invalid Connection header value: ${invalidHeaders.join(", ")}`
  48. });
  49. }
  50. headerNames.forEach((headerName) => {
  51. headers.delete(headerName);
  52. });
  53. }
  54. }
  55. hopByHopHeaders.forEach((header) => {
  56. headers.delete(header);
  57. });
  58. return {
  59. method: request.method,
  60. body: request.body,
  61. duplex: request.body ? "half" : void 0,
  62. headers,
  63. signal: request.signal
  64. };
  65. };
  66. const preprocessRequestInit = (requestInit) => {
  67. if (!requestInit.headers || Array.isArray(requestInit.headers) || requestInit.headers instanceof Headers) {
  68. return requestInit;
  69. }
  70. const headers = new Headers();
  71. for (const [key, value] of Object.entries(requestInit.headers)) {
  72. if (value == null) {
  73. headers.delete(key);
  74. } else {
  75. headers.set(key, value);
  76. }
  77. }
  78. requestInit.headers = headers;
  79. return requestInit;
  80. };
  81. const proxy = async (input, proxyInit) => {
  82. const {
  83. raw,
  84. customFetch,
  85. strictConnectionProcessing = false,
  86. ...requestInit
  87. } = proxyInit instanceof Request ? { raw: proxyInit } : proxyInit ?? {};
  88. const req = new Request(input, {
  89. ...buildRequestInitFromRequest(raw, strictConnectionProcessing),
  90. ...preprocessRequestInit(requestInit)
  91. });
  92. req.headers.delete("accept-encoding");
  93. const res = await (customFetch || fetch)(req);
  94. const resHeaders = new Headers(res.headers);
  95. hopByHopHeaders.forEach((header) => {
  96. resHeaders.delete(header);
  97. });
  98. if (resHeaders.has("content-encoding")) {
  99. resHeaders.delete("content-encoding");
  100. resHeaders.delete("content-length");
  101. }
  102. return new Response(res.body, {
  103. status: res.status,
  104. statusText: res.statusText,
  105. headers: resHeaders
  106. });
  107. };
  108. // Annotate the CommonJS export names for ESM import in node:
  109. 0 && (module.exports = {
  110. proxy
  111. });