|
| 1 | +// deno-lint-ignore-file no-explicit-any |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +/** |
| 7 | + * Implementation of the Cache and CacheStorage APIs for Andromeda |
| 8 | + * Based on: https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage |
| 9 | + * Spec: https://w3c.github.io/ServiceWorker/#cache-interface |
| 10 | + * |
| 11 | + * Note: This is a simplified implementation that wraps the native sync functions |
| 12 | + * in promises to maintain compatibility with the Web API specification. |
| 13 | + */ |
| 14 | + |
| 15 | +type RequestInfo = Request | string | URL; |
| 16 | + |
| 17 | +interface CacheQueryOptions { |
| 18 | + ignoreSearch?: boolean; |
| 19 | + ignoreMethod?: boolean; |
| 20 | + ignoreVary?: boolean; |
| 21 | +} |
| 22 | + |
| 23 | +class Cache { |
| 24 | + #cacheName: string; |
| 25 | + |
| 26 | + constructor(cacheName: string) { |
| 27 | + this.#cacheName = cacheName; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Returns a Promise that resolves to the response associated with the first matching request in the Cache object. |
| 32 | + */ |
| 33 | + match( |
| 34 | + request: RequestInfo, |
| 35 | + options?: CacheQueryOptions, |
| 36 | + ): Promise<Response | undefined> { |
| 37 | + return Promise.resolve( |
| 38 | + cache_match(this.#cacheName, request as any, options), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns a Promise that resolves to an array of all matching responses in the Cache object. |
| 44 | + */ |
| 45 | + matchAll( |
| 46 | + request?: RequestInfo, |
| 47 | + options?: CacheQueryOptions, |
| 48 | + ): Promise<Response[]> { |
| 49 | + return Promise.resolve( |
| 50 | + cache_matchAll(this.#cacheName, request as any, options), |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Takes a URL, retrieves it and adds the resulting response object to the given cache. |
| 56 | + */ |
| 57 | + add(request: RequestInfo): Promise<void> { |
| 58 | + return Promise.resolve(cache_add(this.#cacheName, request as any)); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache. |
| 63 | + */ |
| 64 | + addAll(requests: RequestInfo[]): Promise<void> { |
| 65 | + return Promise.resolve(cache_addAll(this.#cacheName, requests as any)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Takes both a request and its response and adds it to the given cache. |
| 70 | + */ |
| 71 | + put(request: RequestInfo, response: Response): Promise<void> { |
| 72 | + // Clone the response to ensure it can be consumed |
| 73 | + const responseClone = response.clone(); |
| 74 | + return Promise.resolve( |
| 75 | + cache_put(this.#cacheName, request as any, responseClone), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Finds the Cache entry whose key is the request, and if found, deletes the Cache entry and returns a Promise that resolves to true. |
| 81 | + */ |
| 82 | + delete( |
| 83 | + request: RequestInfo, |
| 84 | + options?: CacheQueryOptions, |
| 85 | + ): Promise<boolean> { |
| 86 | + return Promise.resolve( |
| 87 | + cache_delete(this.#cacheName, request as any, options), |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns a Promise that resolves to an array of Cache keys. |
| 93 | + */ |
| 94 | + keys( |
| 95 | + request?: RequestInfo, |
| 96 | + options?: CacheQueryOptions, |
| 97 | + ): Promise<Request[]> { |
| 98 | + return Promise.resolve( |
| 99 | + cache_keys(this.#cacheName, request as any, options), |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +class CacheStorage { |
| 105 | + /** |
| 106 | + * Returns a Promise that resolves to the Cache object matching the cacheName. |
| 107 | + */ |
| 108 | + open(cacheName: string): Promise<Cache> { |
| 109 | + // Call the sync function |
| 110 | + cacheStorage_open(cacheName); |
| 111 | + return Promise.resolve(new Cache(cacheName)); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns a Promise that resolves to true if a Cache object matching the cacheName exists. |
| 116 | + */ |
| 117 | + has(cacheName: string): Promise<boolean> { |
| 118 | + return Promise.resolve(cacheStorage_has(cacheName)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Finds the Cache object matching the cacheName, and if found, deletes the Cache object and returns a Promise that resolves to true. |
| 123 | + */ |
| 124 | + delete(cacheName: string): Promise<boolean> { |
| 125 | + return Promise.resolve(cacheStorage_delete(cacheName)); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns a Promise that will resolve with an array containing strings corresponding to all of the named Cache objects. |
| 130 | + */ |
| 131 | + keys(): Promise<string[]> { |
| 132 | + return Promise.resolve(cacheStorage_keys()); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Checks if a given Request is a key in any of the Cache objects that the CacheStorage object tracks. |
| 137 | + */ |
| 138 | + match( |
| 139 | + request: RequestInfo, |
| 140 | + options?: CacheQueryOptions, |
| 141 | + ): Promise<Response | undefined> { |
| 142 | + return Promise.resolve(cacheStorage_match(request as any, options)); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// Create global CacheStorage instance |
| 147 | +let cacheStorageInstance: CacheStorage | undefined; |
| 148 | + |
| 149 | +function getCacheStorage(): CacheStorage { |
| 150 | + if (!cacheStorageInstance) { |
| 151 | + cacheStorageInstance = new CacheStorage(); |
| 152 | + } |
| 153 | + return cacheStorageInstance; |
| 154 | +} |
| 155 | + |
| 156 | +// Define the global 'caches' property |
| 157 | +Object.defineProperty(globalThis, "caches", { |
| 158 | + get: () => getCacheStorage(), |
| 159 | + configurable: true, |
| 160 | + enumerable: true, |
| 161 | +}); |
0 commit comments