@serwist/expiration 9.0.0-preview.16 → 9.0.0-preview.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- import { CacheExpiration } from "./CacheExpiration.js";
2
- import type { ExpirationPluginOptions } from "./ExpirationPlugin.js";
3
- import { ExpirationPlugin } from "./ExpirationPlugin.js";
4
- export { CacheExpiration, ExpirationPlugin };
5
- export type { ExpirationPluginOptions };
1
+ export { CacheExpiration, ExpirationPlugin } from "@serwist/sw/plugins";
2
+ export type { ExpirationPluginOptions } from "@serwist/sw/plugins";
6
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,CAAC;AAE7C,YAAY,EAAE,uBAAuB,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACxE,YAAY,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/index.js CHANGED
@@ -1,332 +1 @@
1
- import { assert, SerwistError, logger, privateCacheNames, getFriendlyURL } from '@serwist/core/internal';
2
- import { deleteDB, openDB } from 'idb';
3
- import { registerQuotaErrorCallback } from '@serwist/core';
4
-
5
- const DB_NAME = "serwist-expiration";
6
- const CACHE_OBJECT_STORE = "cache-entries";
7
- const normalizeURL = (unNormalizedUrl)=>{
8
- const url = new URL(unNormalizedUrl, location.href);
9
- url.hash = "";
10
- return url.href;
11
- };
12
- class CacheTimestampsModel {
13
- _cacheName;
14
- _db = null;
15
- constructor(cacheName){
16
- this._cacheName = cacheName;
17
- }
18
- _upgradeDb(db) {
19
- const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
20
- keyPath: [
21
- "url",
22
- "cacheName"
23
- ]
24
- });
25
- objStore.createIndex("cacheName", "cacheName", {
26
- unique: false
27
- });
28
- objStore.createIndex("timestamp", "timestamp", {
29
- unique: false
30
- });
31
- }
32
- _upgradeDbAndDeleteOldDbs(db) {
33
- this._upgradeDb(db);
34
- if (this._cacheName) {
35
- void deleteDB(this._cacheName);
36
- }
37
- }
38
- async setTimestamp(url, timestamp) {
39
- url = normalizeURL(url);
40
- const entry = {
41
- cacheName: this._cacheName,
42
- url,
43
- timestamp
44
- };
45
- const db = await this.getDb();
46
- const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
47
- durability: "relaxed"
48
- });
49
- await tx.store.put(entry);
50
- await tx.done;
51
- }
52
- async getTimestamp(url) {
53
- const db = await this.getDb();
54
- const entry = await db.get(CACHE_OBJECT_STORE, [
55
- this._cacheName,
56
- normalizeURL(url)
57
- ]);
58
- return entry?.timestamp;
59
- }
60
- async expireEntries(minTimestamp, maxCount) {
61
- const db = await this.getDb();
62
- let cursor = await db.transaction(CACHE_OBJECT_STORE, "readwrite").store.index("timestamp").openCursor(null, "prev");
63
- const urlsDeleted = [];
64
- let entriesNotDeletedCount = 0;
65
- while(cursor){
66
- const result = cursor.value;
67
- if (result.cacheName === this._cacheName) {
68
- if (minTimestamp && result.timestamp < minTimestamp || maxCount && entriesNotDeletedCount >= maxCount) {
69
- cursor.delete();
70
- urlsDeleted.push(result.url);
71
- } else {
72
- entriesNotDeletedCount++;
73
- }
74
- }
75
- cursor = await cursor.continue();
76
- }
77
- return urlsDeleted;
78
- }
79
- async getDb() {
80
- if (!this._db) {
81
- this._db = await openDB(DB_NAME, 1, {
82
- upgrade: this._upgradeDbAndDeleteOldDbs.bind(this)
83
- });
84
- }
85
- return this._db;
86
- }
87
- }
88
-
89
- class CacheExpiration {
90
- _isRunning = false;
91
- _rerunRequested = false;
92
- _maxEntries;
93
- _maxAgeSeconds;
94
- _matchOptions;
95
- _cacheName;
96
- _timestampModel;
97
- constructor(cacheName, config = {}){
98
- if (process.env.NODE_ENV !== "production") {
99
- assert.isType(cacheName, "string", {
100
- moduleName: "@serwist/expiration",
101
- className: "CacheExpiration",
102
- funcName: "constructor",
103
- paramName: "cacheName"
104
- });
105
- if (!(config.maxEntries || config.maxAgeSeconds)) {
106
- throw new SerwistError("max-entries-or-age-required", {
107
- moduleName: "@serwist/expiration",
108
- className: "CacheExpiration",
109
- funcName: "constructor"
110
- });
111
- }
112
- if (config.maxEntries) {
113
- assert.isType(config.maxEntries, "number", {
114
- moduleName: "@serwist/expiration",
115
- className: "CacheExpiration",
116
- funcName: "constructor",
117
- paramName: "config.maxEntries"
118
- });
119
- }
120
- if (config.maxAgeSeconds) {
121
- assert.isType(config.maxAgeSeconds, "number", {
122
- moduleName: "@serwist/expiration",
123
- className: "CacheExpiration",
124
- funcName: "constructor",
125
- paramName: "config.maxAgeSeconds"
126
- });
127
- }
128
- }
129
- this._maxEntries = config.maxEntries;
130
- this._maxAgeSeconds = config.maxAgeSeconds;
131
- this._matchOptions = config.matchOptions;
132
- this._cacheName = cacheName;
133
- this._timestampModel = new CacheTimestampsModel(cacheName);
134
- }
135
- async expireEntries() {
136
- if (this._isRunning) {
137
- this._rerunRequested = true;
138
- return;
139
- }
140
- this._isRunning = true;
141
- const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
142
- const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
143
- const cache = await self.caches.open(this._cacheName);
144
- for (const url of urlsExpired){
145
- await cache.delete(url, this._matchOptions);
146
- }
147
- if (process.env.NODE_ENV !== "production") {
148
- if (urlsExpired.length > 0) {
149
- logger.groupCollapsed(`Expired ${urlsExpired.length} ` + `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` + `${urlsExpired.length === 1 ? "it" : "them"} from the ` + `'${this._cacheName}' cache.`);
150
- logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
151
- for (const url of urlsExpired){
152
- logger.log(` ${url}`);
153
- }
154
- logger.groupEnd();
155
- } else {
156
- logger.debug("Cache expiration ran and found no entries to remove.");
157
- }
158
- }
159
- this._isRunning = false;
160
- if (this._rerunRequested) {
161
- this._rerunRequested = false;
162
- void this.expireEntries();
163
- }
164
- }
165
- async updateTimestamp(url) {
166
- if (process.env.NODE_ENV !== "production") {
167
- assert.isType(url, "string", {
168
- moduleName: "@serwist/expiration",
169
- className: "CacheExpiration",
170
- funcName: "updateTimestamp",
171
- paramName: "url"
172
- });
173
- }
174
- await this._timestampModel.setTimestamp(url, Date.now());
175
- }
176
- async isURLExpired(url) {
177
- if (!this._maxAgeSeconds) {
178
- if (process.env.NODE_ENV !== "production") {
179
- throw new SerwistError("expired-test-without-max-age", {
180
- methodName: "isURLExpired",
181
- paramName: "maxAgeSeconds"
182
- });
183
- }
184
- return false;
185
- }
186
- const timestamp = await this._timestampModel.getTimestamp(url);
187
- const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
188
- return timestamp !== undefined ? timestamp < expireOlderThan : true;
189
- }
190
- async delete() {
191
- this._rerunRequested = false;
192
- await this._timestampModel.expireEntries(Number.POSITIVE_INFINITY);
193
- }
194
- }
195
-
196
- class ExpirationPlugin {
197
- _config;
198
- _cacheExpirations;
199
- constructor(config = {}){
200
- if (process.env.NODE_ENV !== "production") {
201
- if (!(config.maxEntries || config.maxAgeSeconds)) {
202
- throw new SerwistError("max-entries-or-age-required", {
203
- moduleName: "@serwist/expiration",
204
- className: "ExpirationPlugin",
205
- funcName: "constructor"
206
- });
207
- }
208
- if (config.maxEntries) {
209
- assert.isType(config.maxEntries, "number", {
210
- moduleName: "@serwist/expiration",
211
- className: "ExpirationPlugin",
212
- funcName: "constructor",
213
- paramName: "config.maxEntries"
214
- });
215
- }
216
- if (config.maxAgeSeconds) {
217
- assert.isType(config.maxAgeSeconds, "number", {
218
- moduleName: "@serwist/expiration",
219
- className: "ExpirationPlugin",
220
- funcName: "constructor",
221
- paramName: "config.maxAgeSeconds"
222
- });
223
- }
224
- if (config.maxAgeFrom) {
225
- assert.isType(config.maxAgeFrom, "string", {
226
- moduleName: "@serwist/expiration",
227
- className: "ExpirationPlugin",
228
- funcName: "constructor",
229
- paramName: "config.maxAgeFrom"
230
- });
231
- }
232
- }
233
- this._config = config;
234
- this._cacheExpirations = new Map();
235
- if (!this._config.maxAgeFrom) {
236
- this._config.maxAgeFrom = "last-fetched";
237
- }
238
- if (this._config.purgeOnQuotaError) {
239
- registerQuotaErrorCallback(()=>this.deleteCacheAndMetadata());
240
- }
241
- }
242
- _getCacheExpiration(cacheName) {
243
- if (cacheName === privateCacheNames.getRuntimeName()) {
244
- throw new SerwistError("expire-custom-caches-only");
245
- }
246
- let cacheExpiration = this._cacheExpirations.get(cacheName);
247
- if (!cacheExpiration) {
248
- cacheExpiration = new CacheExpiration(cacheName, this._config);
249
- this._cacheExpirations.set(cacheName, cacheExpiration);
250
- }
251
- return cacheExpiration;
252
- }
253
- cachedResponseWillBeUsed({ event, cacheName, request, cachedResponse }) {
254
- if (!cachedResponse) {
255
- return null;
256
- }
257
- const isFresh = this._isResponseDateFresh(cachedResponse);
258
- const cacheExpiration = this._getCacheExpiration(cacheName);
259
- const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
260
- const done = (async ()=>{
261
- if (isMaxAgeFromLastUsed) {
262
- await cacheExpiration.updateTimestamp(request.url);
263
- }
264
- await cacheExpiration.expireEntries();
265
- })();
266
- try {
267
- event.waitUntil(done);
268
- } catch (error) {
269
- if (process.env.NODE_ENV !== "production") {
270
- if (event instanceof FetchEvent) {
271
- logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
272
- }
273
- }
274
- }
275
- return isFresh ? cachedResponse : null;
276
- }
277
- _isResponseDateFresh(cachedResponse) {
278
- const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
279
- if (isMaxAgeFromLastUsed) {
280
- return true;
281
- }
282
- const now = Date.now();
283
- if (!this._config.maxAgeSeconds) {
284
- return true;
285
- }
286
- const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
287
- if (dateHeaderTimestamp === null) {
288
- return true;
289
- }
290
- return dateHeaderTimestamp >= now - this._config.maxAgeSeconds * 1000;
291
- }
292
- _getDateHeaderTimestamp(cachedResponse) {
293
- if (!cachedResponse.headers.has("date")) {
294
- return null;
295
- }
296
- const dateHeader = cachedResponse.headers.get("date");
297
- const parsedDate = new Date(dateHeader);
298
- const headerTime = parsedDate.getTime();
299
- if (Number.isNaN(headerTime)) {
300
- return null;
301
- }
302
- return headerTime;
303
- }
304
- async cacheDidUpdate({ cacheName, request }) {
305
- if (process.env.NODE_ENV !== "production") {
306
- assert.isType(cacheName, "string", {
307
- moduleName: "@serwist/expiration",
308
- className: "Plugin",
309
- funcName: "cacheDidUpdate",
310
- paramName: "cacheName"
311
- });
312
- assert.isInstance(request, Request, {
313
- moduleName: "@serwist/expiration",
314
- className: "Plugin",
315
- funcName: "cacheDidUpdate",
316
- paramName: "request"
317
- });
318
- }
319
- const cacheExpiration = this._getCacheExpiration(cacheName);
320
- await cacheExpiration.updateTimestamp(request.url);
321
- await cacheExpiration.expireEntries();
322
- }
323
- async deleteCacheAndMetadata() {
324
- for (const [cacheName, cacheExpiration] of this._cacheExpirations){
325
- await self.caches.delete(cacheName);
326
- await cacheExpiration.delete();
327
- }
328
- this._cacheExpirations = new Map();
329
- }
330
- }
331
-
332
- export { CacheExpiration, ExpirationPlugin };
1
+ export { CacheExpiration, ExpirationPlugin } from '@serwist/sw/plugins';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/expiration",
3
- "version": "9.0.0-preview.16",
3
+ "version": "9.0.0-preview.18",
4
4
  "type": "module",
5
5
  "description": "A module that expires cached responses based on age or maximum number of entries.",
6
6
  "files": [
@@ -29,13 +29,12 @@
29
29
  "./package.json": "./package.json"
30
30
  },
31
31
  "dependencies": {
32
- "idb": "8.0.0",
33
- "@serwist/core": "9.0.0-preview.16"
32
+ "@serwist/sw": "9.0.0-preview.18"
34
33
  },
35
34
  "devDependencies": {
36
35
  "rollup": "4.13.0",
37
36
  "typescript": "5.5.0-dev.20240323",
38
- "@serwist/constants": "9.0.0-preview.16"
37
+ "@serwist/constants": "9.0.0-preview.18"
39
38
  },
40
39
  "peerDependencies": {
41
40
  "typescript": ">=5.0.0"
package/src/index.ts CHANGED
@@ -1,15 +1,2 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { CacheExpiration } from "./CacheExpiration.js";
10
- import type { ExpirationPluginOptions } from "./ExpirationPlugin.js";
11
- import { ExpirationPlugin } from "./ExpirationPlugin.js";
12
-
13
- export { CacheExpiration, ExpirationPlugin };
14
-
15
- export type { ExpirationPluginOptions };
1
+ export { CacheExpiration, ExpirationPlugin } from "@serwist/sw/plugins";
2
+ export type { ExpirationPluginOptions } from "@serwist/sw/plugins";
@@ -1,66 +0,0 @@
1
- interface CacheExpirationConfig {
2
- /**
3
- * The maximum number of entries to cache. Entries used the least will
4
- * be removed as the maximum is reached.
5
- */
6
- maxEntries?: number;
7
- /**
8
- * The maximum age of an entry before it's treated as stale and removed.
9
- */
10
- maxAgeSeconds?: number;
11
- /**
12
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
13
- * that will be used when calling `delete()` on the cache.
14
- */
15
- matchOptions?: CacheQueryOptions;
16
- }
17
- /**
18
- * Allows you to expires cached responses based on age or maximum number of entries.
19
- * @see https://serwist.pages.dev/docs/expiration/cache-expiration
20
- */
21
- export declare class CacheExpiration {
22
- private _isRunning;
23
- private _rerunRequested;
24
- private readonly _maxEntries?;
25
- private readonly _maxAgeSeconds?;
26
- private readonly _matchOptions?;
27
- private readonly _cacheName;
28
- private readonly _timestampModel;
29
- /**
30
- * To construct a new CacheExpiration instance you must provide at least
31
- * one of the `config` properties.
32
- *
33
- * @param cacheName Name of the cache to apply restrictions to.
34
- * @param config
35
- */
36
- constructor(cacheName: string, config?: CacheExpirationConfig);
37
- /**
38
- * Expires entries for the given cache and given criteria.
39
- */
40
- expireEntries(): Promise<void>;
41
- /**
42
- * Updates the timestamp for the given URL, allowing it to be correctly
43
- * tracked by the class.
44
- *
45
- * @param url
46
- */
47
- updateTimestamp(url: string): Promise<void>;
48
- /**
49
- * Checks if a URL has expired or not before it's used.
50
- *
51
- * This looks the timestamp up in IndexedDB and can be slow.
52
- *
53
- * Note: This method does not remove an expired entry, call
54
- * `expireEntries()` to remove such entries instead.
55
- *
56
- * @param url
57
- * @returns
58
- */
59
- isURLExpired(url: string): Promise<boolean>;
60
- /**
61
- * Removes the IndexedDB used to keep track of cache expiration metadata.
62
- */
63
- delete(): Promise<void>;
64
- }
65
- export {};
66
- //# sourceMappingURL=CacheExpiration.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CacheExpiration.d.ts","sourceRoot":"","sources":["../src/CacheExpiration.ts"],"names":[],"mappings":"AAYA,UAAU,qBAAqB;IAC7B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,YAAY,CAAC,EAAE,iBAAiB,CAAC;CAClC;AAED;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,eAAe,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAS;IACtC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAoB;IACnD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAuB;IAEvD;;;;;;OAMG;gBACS,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,qBAA0B;IA2CjE;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IA0CpC;;;;;OAKG;IACG,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAajD;;;;;;;;;;OAUG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAejD;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAM9B"}
@@ -1,116 +0,0 @@
1
- import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, SerwistPlugin } from "@serwist/core";
2
- export interface ExpirationPluginOptions {
3
- /**
4
- * The maximum number of entries to cache. Entries used the least will be removed
5
- * as the maximum is reached.
6
- */
7
- maxEntries?: number;
8
- /**
9
- * The maximum number of seconds before an entry is treated as stale and removed.
10
- */
11
- maxAgeSeconds?: number;
12
- /**
13
- * Determines whether `maxAgeSeconds` should be calculated from when an
14
- * entry was last fetched or when it was last used.
15
- *
16
- * @default "last-fetched"
17
- */
18
- maxAgeFrom?: "last-fetched" | "last-used";
19
- /**
20
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
21
- * that will be used when calling `delete()` on the cache.
22
- */
23
- matchOptions?: CacheQueryOptions;
24
- /**
25
- * Whether to opt this cache into automatic deletion if the available storage quota has been exceeded.
26
- */
27
- purgeOnQuotaError?: boolean;
28
- }
29
- /**
30
- * This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
31
- * limit on the age and/or the number of cached requests.
32
- *
33
- * It can only be used with Strategy instances that have a custom `cacheName` property set.
34
- * In other words, it can't be used to expire entries in strategies that use the default runtime
35
- * cache name.
36
- *
37
- * Whenever a cached response is used or updated, this plugin will look
38
- * at the associated cache and remove any old or extra responses.
39
- *
40
- * When using `maxAgeSeconds`, responses may be used *once* after expiring
41
- * because the expiration clean up will not have occurred until *after* the
42
- * cached response has been used. If the response has a "Date" header, then a lightweight expiration
43
- * check is performed, and the response will not be used immediately.
44
- *
45
- * When using `maxEntries`, the least recently requested entry will be removed
46
- * from the cache.
47
- *
48
- * @see https://serwist.pages.dev/docs/expiration/expiration-plugin
49
- */
50
- export declare class ExpirationPlugin implements SerwistPlugin {
51
- private readonly _config;
52
- private _cacheExpirations;
53
- /**
54
- * @param config
55
- */
56
- constructor(config?: ExpirationPluginOptions);
57
- /**
58
- * A simple helper method to return a CacheExpiration instance for a given
59
- * cache name.
60
- *
61
- * @param cacheName
62
- * @returns
63
- * @private
64
- */
65
- private _getCacheExpiration;
66
- /**
67
- * A "lifecycle" callback that will be triggered automatically by the
68
- * `@serwist/strategies` handlers when a `Response` is about to be returned
69
- * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
70
- * the handler. It allows the `Response` to be inspected for freshness and
71
- * prevents it from being used if the `Response`'s `Date` header value is
72
- * older than the configured `maxAgeSeconds`.
73
- *
74
- * @param options
75
- * @returns `cachedResponse` if it is fresh and `null` if it is stale or
76
- * not available.
77
- * @private
78
- */
79
- cachedResponseWillBeUsed({ event, cacheName, request, cachedResponse }: CachedResponseWillBeUsedCallbackParam): Response | null;
80
- /**
81
- * @param cachedResponse
82
- * @returns
83
- * @private
84
- */
85
- private _isResponseDateFresh;
86
- /**
87
- * Extracts the `Date` header and parse it into an useful value.
88
- *
89
- * @param cachedResponse
90
- * @returns
91
- * @private
92
- */
93
- private _getDateHeaderTimestamp;
94
- /**
95
- * A "lifecycle" callback that will be triggered automatically by the
96
- * `@serwist/strategies` handlers when an entry is added to a cache.
97
- *
98
- * @param options
99
- * @private
100
- */
101
- cacheDidUpdate({ cacheName, request }: CacheDidUpdateCallbackParam): Promise<void>;
102
- /**
103
- * Deletes the underlying `Cache` instance associated with this instance and the metadata
104
- * from IndexedDB used to keep track of expiration details for each `Cache` instance.
105
- *
106
- * When using cache expiration, calling this method is preferable to calling
107
- * `caches.delete()` directly, since this will ensure that the IndexedDB
108
- * metadata is also cleanly removed and that open IndexedDB instances are deleted.
109
- *
110
- * Note that if you're *not* using cache expiration for a given cache, calling
111
- * `caches.delete()` and passing in the cache's name should be sufficient.
112
- * There is no Serwist-specific method needed for cleanup in that case.
113
- */
114
- deleteCacheAndMetadata(): Promise<void>;
115
- }
116
- //# sourceMappingURL=ExpirationPlugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ExpirationPlugin.d.ts","sourceRoot":"","sources":["../src/ExpirationPlugin.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,2BAA2B,EAAE,qCAAqC,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAMvH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,cAAc,GAAG,WAAW,CAAC;IAC1C;;;OAGG;IACH,YAAY,CAAC,EAAE,iBAAiB,CAAC;IACjC;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,gBAAiB,YAAW,aAAa;IACpD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0B;IAClD,OAAO,CAAC,iBAAiB,CAA+B;IAExD;;OAEG;gBACS,MAAM,GAAE,uBAA4B;IAkDhD;;;;;;;OAOG;IACH,OAAO,CAAC,mBAAmB;IAa3B;;;;;;;;;;;;OAYG;IACH,wBAAwB,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,cAAc,EAAE,EAAE,qCAAqC;IAqC7G;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAwB5B;;;;;;OAMG;IACH,OAAO,CAAC,uBAAuB;IAkB/B;;;;;;OAMG;IACG,cAAc,CAAC,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,2BAA2B;IAqBxE;;;;;;;;;;;OAWG;IACG,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC;CAW9C"}
@@ -1,65 +0,0 @@
1
- /**
2
- * Returns the timestamp model.
3
- *
4
- * @private
5
- */
6
- export declare class CacheTimestampsModel {
7
- private readonly _cacheName;
8
- private _db;
9
- /**
10
- *
11
- * @param cacheName
12
- *
13
- * @private
14
- */
15
- constructor(cacheName: string);
16
- /**
17
- * Performs an upgrade of indexedDB.
18
- *
19
- * @param db
20
- *
21
- * @private
22
- */
23
- private _upgradeDb;
24
- /**
25
- * Performs an upgrade of indexedDB and deletes deprecated DBs.
26
- *
27
- * @param db
28
- *
29
- * @private
30
- */
31
- private _upgradeDbAndDeleteOldDbs;
32
- /**
33
- * @param url
34
- * @param timestamp
35
- *
36
- * @private
37
- */
38
- setTimestamp(url: string, timestamp: number): Promise<void>;
39
- /**
40
- * Returns the timestamp stored for a given URL.
41
- *
42
- * @param url
43
- * @returns
44
- * @private
45
- */
46
- getTimestamp(url: string): Promise<number | undefined>;
47
- /**
48
- * Iterates through all the entries in the object store (from newest to
49
- * oldest) and removes entries once either `maxCount` is reached or the
50
- * entry's timestamp is less than `minTimestamp`.
51
- *
52
- * @param minTimestamp
53
- * @param maxCount
54
- * @returns
55
- * @private
56
- */
57
- expireEntries(minTimestamp: number, maxCount?: number): Promise<string[]>;
58
- /**
59
- * Returns an open connection to the database.
60
- *
61
- * @private
62
- */
63
- private getDb;
64
- }
65
- //# sourceMappingURL=CacheTimestampsModel.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CacheTimestampsModel.d.ts","sourceRoot":"","sources":["../../src/models/CacheTimestampsModel.ts"],"names":[],"mappings":"AAmCA;;;;GAIG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,GAAG,CAA4C;IAEvD;;;;;OAKG;gBACS,SAAS,EAAE,MAAM;IAI7B;;;;;;OAMG;IACH,OAAO,CAAC,UAAU;IAYlB;;;;;;OAMG;IACH,OAAO,CAAC,yBAAyB;IAOjC;;;;;OAKG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBjE;;;;;;OAMG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAM5D;;;;;;;;;OASG;IACG,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAyB/E;;;;OAIG;YACW,KAAK;CAQpB"}
@@ -1,192 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import { assert, SerwistError, logger } from "@serwist/core/internal";
10
-
11
- import { CacheTimestampsModel } from "./models/CacheTimestampsModel.js";
12
-
13
- interface CacheExpirationConfig {
14
- /**
15
- * The maximum number of entries to cache. Entries used the least will
16
- * be removed as the maximum is reached.
17
- */
18
- maxEntries?: number;
19
- /**
20
- * The maximum age of an entry before it's treated as stale and removed.
21
- */
22
- maxAgeSeconds?: number;
23
- /**
24
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
25
- * that will be used when calling `delete()` on the cache.
26
- */
27
- matchOptions?: CacheQueryOptions;
28
- }
29
-
30
- /**
31
- * Allows you to expires cached responses based on age or maximum number of entries.
32
- * @see https://serwist.pages.dev/docs/expiration/cache-expiration
33
- */
34
- export class CacheExpiration {
35
- private _isRunning = false;
36
- private _rerunRequested = false;
37
- private readonly _maxEntries?: number;
38
- private readonly _maxAgeSeconds?: number;
39
- private readonly _matchOptions?: CacheQueryOptions;
40
- private readonly _cacheName: string;
41
- private readonly _timestampModel: CacheTimestampsModel;
42
-
43
- /**
44
- * To construct a new CacheExpiration instance you must provide at least
45
- * one of the `config` properties.
46
- *
47
- * @param cacheName Name of the cache to apply restrictions to.
48
- * @param config
49
- */
50
- constructor(cacheName: string, config: CacheExpirationConfig = {}) {
51
- if (process.env.NODE_ENV !== "production") {
52
- assert!.isType(cacheName, "string", {
53
- moduleName: "@serwist/expiration",
54
- className: "CacheExpiration",
55
- funcName: "constructor",
56
- paramName: "cacheName",
57
- });
58
-
59
- if (!(config.maxEntries || config.maxAgeSeconds)) {
60
- throw new SerwistError("max-entries-or-age-required", {
61
- moduleName: "@serwist/expiration",
62
- className: "CacheExpiration",
63
- funcName: "constructor",
64
- });
65
- }
66
-
67
- if (config.maxEntries) {
68
- assert!.isType(config.maxEntries, "number", {
69
- moduleName: "@serwist/expiration",
70
- className: "CacheExpiration",
71
- funcName: "constructor",
72
- paramName: "config.maxEntries",
73
- });
74
- }
75
-
76
- if (config.maxAgeSeconds) {
77
- assert!.isType(config.maxAgeSeconds, "number", {
78
- moduleName: "@serwist/expiration",
79
- className: "CacheExpiration",
80
- funcName: "constructor",
81
- paramName: "config.maxAgeSeconds",
82
- });
83
- }
84
- }
85
-
86
- this._maxEntries = config.maxEntries;
87
- this._maxAgeSeconds = config.maxAgeSeconds;
88
- this._matchOptions = config.matchOptions;
89
- this._cacheName = cacheName;
90
- this._timestampModel = new CacheTimestampsModel(cacheName);
91
- }
92
-
93
- /**
94
- * Expires entries for the given cache and given criteria.
95
- */
96
- async expireEntries(): Promise<void> {
97
- if (this._isRunning) {
98
- this._rerunRequested = true;
99
- return;
100
- }
101
- this._isRunning = true;
102
-
103
- const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
104
-
105
- const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
106
-
107
- // Delete URLs from the cache
108
- const cache = await self.caches.open(this._cacheName);
109
- for (const url of urlsExpired) {
110
- await cache.delete(url, this._matchOptions);
111
- }
112
-
113
- if (process.env.NODE_ENV !== "production") {
114
- if (urlsExpired.length > 0) {
115
- logger.groupCollapsed(
116
- `Expired ${urlsExpired.length} ` +
117
- `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` +
118
- `${urlsExpired.length === 1 ? "it" : "them"} from the ` +
119
- `'${this._cacheName}' cache.`,
120
- );
121
- logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
122
- for (const url of urlsExpired) {
123
- logger.log(` ${url}`);
124
- }
125
- logger.groupEnd();
126
- } else {
127
- logger.debug("Cache expiration ran and found no entries to remove.");
128
- }
129
- }
130
-
131
- this._isRunning = false;
132
- if (this._rerunRequested) {
133
- this._rerunRequested = false;
134
- void this.expireEntries();
135
- }
136
- }
137
-
138
- /**
139
- * Updates the timestamp for the given URL, allowing it to be correctly
140
- * tracked by the class.
141
- *
142
- * @param url
143
- */
144
- async updateTimestamp(url: string): Promise<void> {
145
- if (process.env.NODE_ENV !== "production") {
146
- assert!.isType(url, "string", {
147
- moduleName: "@serwist/expiration",
148
- className: "CacheExpiration",
149
- funcName: "updateTimestamp",
150
- paramName: "url",
151
- });
152
- }
153
-
154
- await this._timestampModel.setTimestamp(url, Date.now());
155
- }
156
-
157
- /**
158
- * Checks if a URL has expired or not before it's used.
159
- *
160
- * This looks the timestamp up in IndexedDB and can be slow.
161
- *
162
- * Note: This method does not remove an expired entry, call
163
- * `expireEntries()` to remove such entries instead.
164
- *
165
- * @param url
166
- * @returns
167
- */
168
- async isURLExpired(url: string): Promise<boolean> {
169
- if (!this._maxAgeSeconds) {
170
- if (process.env.NODE_ENV !== "production") {
171
- throw new SerwistError("expired-test-without-max-age", {
172
- methodName: "isURLExpired",
173
- paramName: "maxAgeSeconds",
174
- });
175
- }
176
- return false;
177
- }
178
- const timestamp = await this._timestampModel.getTimestamp(url);
179
- const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
180
- return timestamp !== undefined ? timestamp < expireOlderThan : true;
181
- }
182
-
183
- /**
184
- * Removes the IndexedDB used to keep track of cache expiration metadata.
185
- */
186
- async delete(): Promise<void> {
187
- // Make sure we don't attempt another rerun if we're called in the middle of
188
- // a cache expiration.
189
- this._rerunRequested = false;
190
- await this._timestampModel.expireEntries(Number.POSITIVE_INFINITY); // Expires all.
191
- }
192
- }
@@ -1,297 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { CacheDidUpdateCallbackParam, CachedResponseWillBeUsedCallbackParam, SerwistPlugin } from "@serwist/core";
10
- import { registerQuotaErrorCallback } from "@serwist/core";
11
- import { assert, SerwistError, getFriendlyURL, logger, privateCacheNames } from "@serwist/core/internal";
12
-
13
- import { CacheExpiration } from "./CacheExpiration.js";
14
-
15
- export interface ExpirationPluginOptions {
16
- /**
17
- * The maximum number of entries to cache. Entries used the least will be removed
18
- * as the maximum is reached.
19
- */
20
- maxEntries?: number;
21
- /**
22
- * The maximum number of seconds before an entry is treated as stale and removed.
23
- */
24
- maxAgeSeconds?: number;
25
- /**
26
- * Determines whether `maxAgeSeconds` should be calculated from when an
27
- * entry was last fetched or when it was last used.
28
- *
29
- * @default "last-fetched"
30
- */
31
- maxAgeFrom?: "last-fetched" | "last-used";
32
- /**
33
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
34
- * that will be used when calling `delete()` on the cache.
35
- */
36
- matchOptions?: CacheQueryOptions;
37
- /**
38
- * Whether to opt this cache into automatic deletion if the available storage quota has been exceeded.
39
- */
40
- purgeOnQuotaError?: boolean;
41
- }
42
-
43
- /**
44
- * This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
45
- * limit on the age and/or the number of cached requests.
46
- *
47
- * It can only be used with Strategy instances that have a custom `cacheName` property set.
48
- * In other words, it can't be used to expire entries in strategies that use the default runtime
49
- * cache name.
50
- *
51
- * Whenever a cached response is used or updated, this plugin will look
52
- * at the associated cache and remove any old or extra responses.
53
- *
54
- * When using `maxAgeSeconds`, responses may be used *once* after expiring
55
- * because the expiration clean up will not have occurred until *after* the
56
- * cached response has been used. If the response has a "Date" header, then a lightweight expiration
57
- * check is performed, and the response will not be used immediately.
58
- *
59
- * When using `maxEntries`, the least recently requested entry will be removed
60
- * from the cache.
61
- *
62
- * @see https://serwist.pages.dev/docs/expiration/expiration-plugin
63
- */
64
- export class ExpirationPlugin implements SerwistPlugin {
65
- private readonly _config: ExpirationPluginOptions;
66
- private _cacheExpirations: Map<string, CacheExpiration>;
67
-
68
- /**
69
- * @param config
70
- */
71
- constructor(config: ExpirationPluginOptions = {}) {
72
- if (process.env.NODE_ENV !== "production") {
73
- if (!(config.maxEntries || config.maxAgeSeconds)) {
74
- throw new SerwistError("max-entries-or-age-required", {
75
- moduleName: "@serwist/expiration",
76
- className: "ExpirationPlugin",
77
- funcName: "constructor",
78
- });
79
- }
80
-
81
- if (config.maxEntries) {
82
- assert!.isType(config.maxEntries, "number", {
83
- moduleName: "@serwist/expiration",
84
- className: "ExpirationPlugin",
85
- funcName: "constructor",
86
- paramName: "config.maxEntries",
87
- });
88
- }
89
-
90
- if (config.maxAgeSeconds) {
91
- assert!.isType(config.maxAgeSeconds, "number", {
92
- moduleName: "@serwist/expiration",
93
- className: "ExpirationPlugin",
94
- funcName: "constructor",
95
- paramName: "config.maxAgeSeconds",
96
- });
97
- }
98
-
99
- if (config.maxAgeFrom) {
100
- assert!.isType(config.maxAgeFrom, "string", {
101
- moduleName: "@serwist/expiration",
102
- className: "ExpirationPlugin",
103
- funcName: "constructor",
104
- paramName: "config.maxAgeFrom",
105
- });
106
- }
107
- }
108
-
109
- this._config = config;
110
- this._cacheExpirations = new Map();
111
-
112
- if (!this._config.maxAgeFrom) {
113
- this._config.maxAgeFrom = "last-fetched";
114
- }
115
-
116
- if (this._config.purgeOnQuotaError) {
117
- registerQuotaErrorCallback(() => this.deleteCacheAndMetadata());
118
- }
119
- }
120
-
121
- /**
122
- * A simple helper method to return a CacheExpiration instance for a given
123
- * cache name.
124
- *
125
- * @param cacheName
126
- * @returns
127
- * @private
128
- */
129
- private _getCacheExpiration(cacheName: string): CacheExpiration {
130
- if (cacheName === privateCacheNames.getRuntimeName()) {
131
- throw new SerwistError("expire-custom-caches-only");
132
- }
133
-
134
- let cacheExpiration = this._cacheExpirations.get(cacheName);
135
- if (!cacheExpiration) {
136
- cacheExpiration = new CacheExpiration(cacheName, this._config);
137
- this._cacheExpirations.set(cacheName, cacheExpiration);
138
- }
139
- return cacheExpiration;
140
- }
141
-
142
- /**
143
- * A "lifecycle" callback that will be triggered automatically by the
144
- * `@serwist/strategies` handlers when a `Response` is about to be returned
145
- * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
146
- * the handler. It allows the `Response` to be inspected for freshness and
147
- * prevents it from being used if the `Response`'s `Date` header value is
148
- * older than the configured `maxAgeSeconds`.
149
- *
150
- * @param options
151
- * @returns `cachedResponse` if it is fresh and `null` if it is stale or
152
- * not available.
153
- * @private
154
- */
155
- cachedResponseWillBeUsed({ event, cacheName, request, cachedResponse }: CachedResponseWillBeUsedCallbackParam) {
156
- if (!cachedResponse) {
157
- return null;
158
- }
159
-
160
- const isFresh = this._isResponseDateFresh(cachedResponse);
161
-
162
- // Expire entries to ensure that even if the expiration date has
163
- // expired, it'll only be used once.
164
- const cacheExpiration = this._getCacheExpiration(cacheName);
165
-
166
- const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
167
-
168
- const done = (async () => {
169
- // Update the metadata for the request URL to the current timestamp.
170
- // Only applies if `maxAgeFrom` is `"last-used"`, since the current
171
- // lifecycle callback is `cachedResponseWillBeUsed`.
172
- // This needs to be called before `expireEntries()` so as to avoid
173
- // this URL being marked as expired.
174
- if (isMaxAgeFromLastUsed) {
175
- await cacheExpiration.updateTimestamp(request.url);
176
- }
177
- await cacheExpiration.expireEntries();
178
- })();
179
- try {
180
- event.waitUntil(done);
181
- } catch (error) {
182
- if (process.env.NODE_ENV !== "production") {
183
- if (event instanceof FetchEvent) {
184
- logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
185
- }
186
- }
187
- }
188
-
189
- return isFresh ? cachedResponse : null;
190
- }
191
-
192
- /**
193
- * @param cachedResponse
194
- * @returns
195
- * @private
196
- */
197
- private _isResponseDateFresh(cachedResponse: Response): boolean {
198
- const isMaxAgeFromLastUsed = this._config.maxAgeFrom === "last-used";
199
- // If `maxAgeFrom` is `"last-used"`, the `Date` header doesn't really
200
- // matter since it is about when the response was created.
201
- if (isMaxAgeFromLastUsed) {
202
- return true;
203
- }
204
- const now = Date.now();
205
- if (!this._config.maxAgeSeconds) {
206
- return true;
207
- }
208
- // Check if the `Date` header will suffice a quick expiration check.
209
- // See https://github.com/GoogleChromeLabs/sw-toolbox/issues/164 for
210
- // discussion.
211
- const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
212
- if (dateHeaderTimestamp === null) {
213
- // Unable to parse date, so assume it's fresh.
214
- return true;
215
- }
216
- // If we have a valid headerTime, then our response is fresh if the
217
- // headerTime plus maxAgeSeconds is greater than the current time.
218
- return dateHeaderTimestamp >= now - this._config.maxAgeSeconds * 1000;
219
- }
220
-
221
- /**
222
- * Extracts the `Date` header and parse it into an useful value.
223
- *
224
- * @param cachedResponse
225
- * @returns
226
- * @private
227
- */
228
- private _getDateHeaderTimestamp(cachedResponse: Response): number | null {
229
- if (!cachedResponse.headers.has("date")) {
230
- return null;
231
- }
232
-
233
- const dateHeader = cachedResponse.headers.get("date")!;
234
- const parsedDate = new Date(dateHeader);
235
- const headerTime = parsedDate.getTime();
236
-
237
- // If the `Date` header is invalid for some reason, `parsedDate.getTime()`
238
- // will return NaN.
239
- if (Number.isNaN(headerTime)) {
240
- return null;
241
- }
242
-
243
- return headerTime;
244
- }
245
-
246
- /**
247
- * A "lifecycle" callback that will be triggered automatically by the
248
- * `@serwist/strategies` handlers when an entry is added to a cache.
249
- *
250
- * @param options
251
- * @private
252
- */
253
- async cacheDidUpdate({ cacheName, request }: CacheDidUpdateCallbackParam) {
254
- if (process.env.NODE_ENV !== "production") {
255
- assert!.isType(cacheName, "string", {
256
- moduleName: "@serwist/expiration",
257
- className: "Plugin",
258
- funcName: "cacheDidUpdate",
259
- paramName: "cacheName",
260
- });
261
- assert!.isInstance(request, Request, {
262
- moduleName: "@serwist/expiration",
263
- className: "Plugin",
264
- funcName: "cacheDidUpdate",
265
- paramName: "request",
266
- });
267
- }
268
-
269
- const cacheExpiration = this._getCacheExpiration(cacheName);
270
- await cacheExpiration.updateTimestamp(request.url);
271
- await cacheExpiration.expireEntries();
272
- }
273
-
274
- /**
275
- * Deletes the underlying `Cache` instance associated with this instance and the metadata
276
- * from IndexedDB used to keep track of expiration details for each `Cache` instance.
277
- *
278
- * When using cache expiration, calling this method is preferable to calling
279
- * `caches.delete()` directly, since this will ensure that the IndexedDB
280
- * metadata is also cleanly removed and that open IndexedDB instances are deleted.
281
- *
282
- * Note that if you're *not* using cache expiration for a given cache, calling
283
- * `caches.delete()` and passing in the cache's name should be sufficient.
284
- * There is no Serwist-specific method needed for cleanup in that case.
285
- */
286
- async deleteCacheAndMetadata(): Promise<void> {
287
- // Do this one at a time instead of all at once via `Promise.all()` to
288
- // reduce the chance of inconsistency if a promise rejects.
289
- for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
290
- await self.caches.delete(cacheName);
291
- await cacheExpiration.delete();
292
- }
293
-
294
- // Reset this._cacheExpirations to its initial state.
295
- this._cacheExpirations = new Map();
296
- }
297
- }
@@ -1,171 +0,0 @@
1
- /*
2
- Copyright 2018 Google LLC
3
-
4
- Use of this source code is governed by an MIT-style
5
- license that can be found in the LICENSE file or at
6
- https://opensource.org/licenses/MIT.
7
- */
8
-
9
- import type { DBSchema, IDBPDatabase } from "idb";
10
- import { deleteDB, openDB } from "idb";
11
-
12
- const DB_NAME = "serwist-expiration";
13
- const CACHE_OBJECT_STORE = "cache-entries";
14
-
15
- const normalizeURL = (unNormalizedUrl: string) => {
16
- const url = new URL(unNormalizedUrl, location.href);
17
- url.hash = "";
18
-
19
- return url.href;
20
- };
21
-
22
- interface CacheTimestampsModelEntry {
23
- cacheName: string;
24
- url: string;
25
- timestamp: number;
26
- }
27
-
28
- interface CacheDbSchema extends DBSchema {
29
- "cache-entries": {
30
- key: [string, string];
31
- value: CacheTimestampsModelEntry;
32
- indexes: { cacheName: string; timestamp: number };
33
- };
34
- }
35
-
36
- /**
37
- * Returns the timestamp model.
38
- *
39
- * @private
40
- */
41
- export class CacheTimestampsModel {
42
- private readonly _cacheName: string;
43
- private _db: IDBPDatabase<CacheDbSchema> | null = null;
44
-
45
- /**
46
- *
47
- * @param cacheName
48
- *
49
- * @private
50
- */
51
- constructor(cacheName: string) {
52
- this._cacheName = cacheName;
53
- }
54
-
55
- /**
56
- * Performs an upgrade of indexedDB.
57
- *
58
- * @param db
59
- *
60
- * @private
61
- */
62
- private _upgradeDb(db: IDBPDatabase<CacheDbSchema>) {
63
- const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
64
- keyPath: ["url", "cacheName"],
65
- });
66
-
67
- // TODO(philipwalton): once we don't have to support EdgeHTML, we can
68
- // create a single index with the keyPath `['cacheName', 'timestamp']`
69
- // instead of doing both these indexes.
70
- objStore.createIndex("cacheName", "cacheName", { unique: false });
71
- objStore.createIndex("timestamp", "timestamp", { unique: false });
72
- }
73
-
74
- /**
75
- * Performs an upgrade of indexedDB and deletes deprecated DBs.
76
- *
77
- * @param db
78
- *
79
- * @private
80
- */
81
- private _upgradeDbAndDeleteOldDbs(db: IDBPDatabase<CacheDbSchema>) {
82
- this._upgradeDb(db);
83
- if (this._cacheName) {
84
- void deleteDB(this._cacheName);
85
- }
86
- }
87
-
88
- /**
89
- * @param url
90
- * @param timestamp
91
- *
92
- * @private
93
- */
94
- async setTimestamp(url: string, timestamp: number): Promise<void> {
95
- url = normalizeURL(url);
96
-
97
- const entry = {
98
- cacheName: this._cacheName,
99
- url,
100
- timestamp,
101
- } satisfies CacheTimestampsModelEntry;
102
- const db = await this.getDb();
103
- const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
104
- durability: "relaxed",
105
- });
106
- await tx.store.put(entry);
107
- await tx.done;
108
- }
109
-
110
- /**
111
- * Returns the timestamp stored for a given URL.
112
- *
113
- * @param url
114
- * @returns
115
- * @private
116
- */
117
- async getTimestamp(url: string): Promise<number | undefined> {
118
- const db = await this.getDb();
119
- const entry = await db.get(CACHE_OBJECT_STORE, [this._cacheName, normalizeURL(url)]);
120
- return entry?.timestamp;
121
- }
122
-
123
- /**
124
- * Iterates through all the entries in the object store (from newest to
125
- * oldest) and removes entries once either `maxCount` is reached or the
126
- * entry's timestamp is less than `minTimestamp`.
127
- *
128
- * @param minTimestamp
129
- * @param maxCount
130
- * @returns
131
- * @private
132
- */
133
- async expireEntries(minTimestamp: number, maxCount?: number): Promise<string[]> {
134
- const db = await this.getDb();
135
- let cursor = await db.transaction(CACHE_OBJECT_STORE, "readwrite").store.index("timestamp").openCursor(null, "prev");
136
- const urlsDeleted: string[] = [];
137
- let entriesNotDeletedCount = 0;
138
- while (cursor) {
139
- const result = cursor.value;
140
- // TODO(philipwalton): once we can use a multi-key index, we
141
- // won't have to check `cacheName` here.
142
- if (result.cacheName === this._cacheName) {
143
- // Delete an entry if it's older than the max age or
144
- // if we already have the max number allowed.
145
- if ((minTimestamp && result.timestamp < minTimestamp) || (maxCount && entriesNotDeletedCount >= maxCount)) {
146
- cursor.delete();
147
- urlsDeleted.push(result.url);
148
- } else {
149
- entriesNotDeletedCount++;
150
- }
151
- }
152
- cursor = await cursor.continue();
153
- }
154
-
155
- return urlsDeleted;
156
- }
157
-
158
- /**
159
- * Returns an open connection to the database.
160
- *
161
- * @private
162
- */
163
- private async getDb() {
164
- if (!this._db) {
165
- this._db = await openDB(DB_NAME, 1, {
166
- upgrade: this._upgradeDbAndDeleteOldDbs.bind(this),
167
- });
168
- }
169
- return this._db;
170
- }
171
- }