response.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. // src/response.ts
  20. var response_exports = {};
  21. __export(response_exports, {
  22. GlobalResponse: () => GlobalResponse,
  23. Response: () => Response,
  24. cacheKey: () => cacheKey
  25. });
  26. module.exports = __toCommonJS(response_exports);
  27. var responseCache = Symbol("responseCache");
  28. var getResponseCache = Symbol("getResponseCache");
  29. var cacheKey = Symbol("cache");
  30. var GlobalResponse = global.Response;
  31. var Response = class _Response {
  32. #body;
  33. #init;
  34. [getResponseCache]() {
  35. delete this[cacheKey];
  36. return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
  37. }
  38. constructor(body, init) {
  39. let headers;
  40. this.#body = body;
  41. if (init instanceof _Response) {
  42. const cachedGlobalResponse = init[responseCache];
  43. if (cachedGlobalResponse) {
  44. this.#init = cachedGlobalResponse;
  45. this[getResponseCache]();
  46. return;
  47. } else {
  48. this.#init = init.#init;
  49. headers = new Headers(init.#init.headers);
  50. }
  51. } else {
  52. this.#init = init;
  53. }
  54. if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
  55. ;
  56. this[cacheKey] = [init?.status || 200, body, headers || init?.headers];
  57. }
  58. }
  59. get headers() {
  60. const cache = this[cacheKey];
  61. if (cache) {
  62. if (!(cache[2] instanceof Headers)) {
  63. cache[2] = new Headers(
  64. cache[2] || { "content-type": "text/plain; charset=UTF-8" }
  65. );
  66. }
  67. return cache[2];
  68. }
  69. return this[getResponseCache]().headers;
  70. }
  71. get status() {
  72. return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
  73. }
  74. get ok() {
  75. const status = this.status;
  76. return status >= 200 && status < 300;
  77. }
  78. };
  79. ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
  80. Object.defineProperty(Response.prototype, k, {
  81. get() {
  82. return this[getResponseCache]()[k];
  83. }
  84. });
  85. });
  86. ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
  87. Object.defineProperty(Response.prototype, k, {
  88. value: function() {
  89. return this[getResponseCache]()[k]();
  90. }
  91. });
  92. });
  93. Object.setPrototypeOf(Response, GlobalResponse);
  94. Object.setPrototypeOf(Response.prototype, GlobalResponse.prototype);
  95. // Annotate the CommonJS export names for ESM import in node:
  96. 0 && (module.exports = {
  97. GlobalResponse,
  98. Response,
  99. cacheKey
  100. });