cache.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  20. // src/primitives/cache.js
  21. var cache_exports = {};
  22. __export(cache_exports, {
  23. Cache: () => Cache,
  24. CacheStorage: () => CacheStorage,
  25. caches: () => caches,
  26. createCaches: () => createCaches
  27. });
  28. module.exports = __toCommonJS(cache_exports);
  29. var import_fetch = require("./fetch");
  30. function createCaches() {
  31. const getKey = /* @__PURE__ */ __name((request) => new URL(request.url).toString(), "getKey");
  32. const normalizeRequest = /* @__PURE__ */ __name((input, { invokeName }) => {
  33. if (typeof proxy === "object" && proxy.__normalized__)
  34. return input;
  35. const request = input instanceof import_fetch.Request ? input : new import_fetch.Request(input);
  36. if (request.method !== "GET") {
  37. throw new TypeError(
  38. `Failed to execute '${invokeName}' on 'Cache': Request method '${request.method}' is unsupported`
  39. );
  40. }
  41. if (!request.url.startsWith("http")) {
  42. throw new TypeError(
  43. `Failed to execute '${invokeName}' on 'Cache': Request scheme '${request.url.split(":")[0]}' is unsupported`
  44. );
  45. }
  46. Object.defineProperty(request, "__normalized__", {
  47. enumerable: false,
  48. writable: false,
  49. value: true
  50. });
  51. return request;
  52. }, "normalizeRequest");
  53. class Cache2 {
  54. constructor(Storage = Map) {
  55. Object.defineProperty(this, "store", {
  56. enumerable: false,
  57. writable: false,
  58. value: new Storage()
  59. });
  60. }
  61. async add(request) {
  62. const response = await (0, import_fetch.fetch)(
  63. normalizeRequest(request, { invokeName: "add" })
  64. );
  65. if (!response.ok) {
  66. throw new TypeError(
  67. "Failed to execute 'add' on 'Cache': Request failed"
  68. );
  69. }
  70. return this.put(request, response);
  71. }
  72. async addAll(requests) {
  73. await Promise.all(requests.map((request) => this.add(request)));
  74. }
  75. async match(request) {
  76. const key = getKey(normalizeRequest(request, { invokeName: "match" }));
  77. const cached = this.store.get(key);
  78. return cached ? new import_fetch.Response(cached.body, cached.init) : void 0;
  79. }
  80. async delete(request) {
  81. const key = getKey(normalizeRequest(request, { invokeName: "delete" }));
  82. return this.store.delete(key);
  83. }
  84. async put(request, response) {
  85. if (response.status === 206) {
  86. throw new TypeError(
  87. "Failed to execute 'put' on 'Cache': Partial response (status code 206) is unsupported"
  88. );
  89. }
  90. const vary = response.headers.get("vary");
  91. if (vary !== null && vary.includes("*")) {
  92. throw new TypeError(
  93. "Failed to execute 'put' on 'Cache': Vary header contains *"
  94. );
  95. }
  96. request = normalizeRequest(request, { invokeName: "put" });
  97. try {
  98. this.store.set(getKey(request), {
  99. body: new Uint8Array(await response.arrayBuffer()),
  100. init: {
  101. status: response.status,
  102. headers: [...response.headers]
  103. }
  104. });
  105. } catch (error) {
  106. if (error.message === "disturbed") {
  107. throw new TypeError(
  108. "Failed to execute 'put' on 'Cache': Response body is already used"
  109. );
  110. }
  111. throw error;
  112. }
  113. }
  114. }
  115. __name(Cache2, "Cache");
  116. const cacheStorage = /* @__PURE__ */ __name((Storage = Map) => {
  117. const caches2 = new Storage();
  118. const open = /* @__PURE__ */ __name(async (cacheName) => {
  119. let cache = caches2.get(cacheName);
  120. if (cache === void 0) {
  121. cache = new Cache2(Storage);
  122. caches2.set(cacheName, cache);
  123. }
  124. return cache;
  125. }, "open");
  126. const has = /* @__PURE__ */ __name((cacheName) => Promise.resolve(caches2.has(cacheName)), "has");
  127. const keys = /* @__PURE__ */ __name(() => Promise.resolve(caches2.keys()), "keys");
  128. const _delete = /* @__PURE__ */ __name((cacheName) => Promise.resolve(caches2.delete(cacheName)), "_delete");
  129. const match = /* @__PURE__ */ __name(async (request, options) => {
  130. for (const cache of caches2.values()) {
  131. const cached = await cache.match(request, options);
  132. if (cached !== void 0)
  133. return cached;
  134. }
  135. }, "match");
  136. return {
  137. open,
  138. has,
  139. keys,
  140. delete: _delete,
  141. match
  142. };
  143. }, "cacheStorage");
  144. return { Cache: Cache2, cacheStorage };
  145. }
  146. __name(createCaches, "createCaches");
  147. function Cache() {
  148. if (!(this instanceof Cache))
  149. return new Cache();
  150. throw TypeError("Illegal constructor");
  151. }
  152. __name(Cache, "Cache");
  153. function CacheStorage() {
  154. if (!(this instanceof CacheStorage))
  155. return new CacheStorage();
  156. throw TypeError("Illegal constructor");
  157. }
  158. __name(CacheStorage, "CacheStorage");
  159. var caches = (() => {
  160. const { cacheStorage } = createCaches();
  161. const caches2 = cacheStorage();
  162. caches2.open("default");
  163. return caches2;
  164. })();
  165. // Annotate the CommonJS export names for ESM import in node:
  166. 0 && (module.exports = {
  167. Cache,
  168. CacheStorage,
  169. caches,
  170. createCaches
  171. });