@serwist/expiration 9.0.0-preview.9 → 9.0.0

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";
2
+ export type { ExpirationPluginOptions } from "serwist";
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,SAAS,CAAC;AAC5D,YAAY,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -1,318 +1 @@
1
- import { assert, SerwistError, logger, dontWaitFor, 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: "id"
21
- });
22
- objStore.createIndex("cacheName", "cacheName", {
23
- unique: false
24
- });
25
- objStore.createIndex("timestamp", "timestamp", {
26
- unique: false
27
- });
28
- }
29
- _upgradeDbAndDeleteOldDbs(db) {
30
- this._upgradeDb(db);
31
- if (this._cacheName) {
32
- void deleteDB(this._cacheName);
33
- }
34
- }
35
- async setTimestamp(url, timestamp) {
36
- url = normalizeURL(url);
37
- const entry = {
38
- url,
39
- timestamp,
40
- cacheName: this._cacheName,
41
- id: this._getId(url)
42
- };
43
- const db = await this.getDb();
44
- const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
45
- durability: "relaxed"
46
- });
47
- await tx.store.put(entry);
48
- await tx.done;
49
- }
50
- async getTimestamp(url) {
51
- const db = await this.getDb();
52
- const entry = await db.get(CACHE_OBJECT_STORE, this._getId(url));
53
- return entry?.timestamp;
54
- }
55
- async expireEntries(minTimestamp, maxCount) {
56
- const db = await this.getDb();
57
- let cursor = await db.transaction(CACHE_OBJECT_STORE).store.index("timestamp").openCursor(null, "prev");
58
- const entriesToDelete = [];
59
- let entriesNotDeletedCount = 0;
60
- while(cursor){
61
- const result = cursor.value;
62
- if (result.cacheName === this._cacheName) {
63
- if (minTimestamp && result.timestamp < minTimestamp || maxCount && entriesNotDeletedCount >= maxCount) {
64
- entriesToDelete.push(cursor.value);
65
- } else {
66
- entriesNotDeletedCount++;
67
- }
68
- }
69
- cursor = await cursor.continue();
70
- }
71
- const urlsDeleted = [];
72
- for (const entry of entriesToDelete){
73
- await db.delete(CACHE_OBJECT_STORE, entry.id);
74
- urlsDeleted.push(entry.url);
75
- }
76
- return urlsDeleted;
77
- }
78
- _getId(url) {
79
- return `${this._cacheName}|${normalizeURL(url)}`;
80
- }
81
- async getDb() {
82
- if (!this._db) {
83
- this._db = await openDB(DB_NAME, 1, {
84
- upgrade: this._upgradeDbAndDeleteOldDbs.bind(this)
85
- });
86
- }
87
- return this._db;
88
- }
89
- }
90
-
91
- class CacheExpiration {
92
- _isRunning = false;
93
- _rerunRequested = false;
94
- _maxEntries;
95
- _maxAgeSeconds;
96
- _matchOptions;
97
- _cacheName;
98
- _timestampModel;
99
- constructor(cacheName, config = {}){
100
- if (process.env.NODE_ENV !== "production") {
101
- assert.isType(cacheName, "string", {
102
- moduleName: "@serwist/expiration",
103
- className: "CacheExpiration",
104
- funcName: "constructor",
105
- paramName: "cacheName"
106
- });
107
- if (!(config.maxEntries || config.maxAgeSeconds)) {
108
- throw new SerwistError("max-entries-or-age-required", {
109
- moduleName: "@serwist/expiration",
110
- className: "CacheExpiration",
111
- funcName: "constructor"
112
- });
113
- }
114
- if (config.maxEntries) {
115
- assert.isType(config.maxEntries, "number", {
116
- moduleName: "@serwist/expiration",
117
- className: "CacheExpiration",
118
- funcName: "constructor",
119
- paramName: "config.maxEntries"
120
- });
121
- }
122
- if (config.maxAgeSeconds) {
123
- assert.isType(config.maxAgeSeconds, "number", {
124
- moduleName: "@serwist/expiration",
125
- className: "CacheExpiration",
126
- funcName: "constructor",
127
- paramName: "config.maxAgeSeconds"
128
- });
129
- }
130
- }
131
- this._maxEntries = config.maxEntries;
132
- this._maxAgeSeconds = config.maxAgeSeconds;
133
- this._matchOptions = config.matchOptions;
134
- this._cacheName = cacheName;
135
- this._timestampModel = new CacheTimestampsModel(cacheName);
136
- }
137
- async expireEntries() {
138
- if (this._isRunning) {
139
- this._rerunRequested = true;
140
- return;
141
- }
142
- this._isRunning = true;
143
- const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
144
- const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
145
- const cache = await self.caches.open(this._cacheName);
146
- for (const url of urlsExpired){
147
- await cache.delete(url, this._matchOptions);
148
- }
149
- if (process.env.NODE_ENV !== "production") {
150
- if (urlsExpired.length > 0) {
151
- logger.groupCollapsed(`Expired ${urlsExpired.length} ` + `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` + `${urlsExpired.length === 1 ? "it" : "them"} from the ` + `'${this._cacheName}' cache.`);
152
- logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
153
- for (const url of urlsExpired){
154
- logger.log(` ${url}`);
155
- }
156
- logger.groupEnd();
157
- } else {
158
- logger.debug("Cache expiration ran and found no entries to remove.");
159
- }
160
- }
161
- this._isRunning = false;
162
- if (this._rerunRequested) {
163
- this._rerunRequested = false;
164
- dontWaitFor(this.expireEntries());
165
- }
166
- }
167
- async updateTimestamp(url) {
168
- if (process.env.NODE_ENV !== "production") {
169
- assert.isType(url, "string", {
170
- moduleName: "@serwist/expiration",
171
- className: "CacheExpiration",
172
- funcName: "updateTimestamp",
173
- paramName: "url"
174
- });
175
- }
176
- await this._timestampModel.setTimestamp(url, Date.now());
177
- }
178
- async isURLExpired(url) {
179
- if (!this._maxAgeSeconds) {
180
- if (process.env.NODE_ENV !== "production") {
181
- throw new SerwistError("expired-test-without-max-age", {
182
- methodName: "isURLExpired",
183
- paramName: "maxAgeSeconds"
184
- });
185
- }
186
- return false;
187
- }
188
- const timestamp = await this._timestampModel.getTimestamp(url);
189
- const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
190
- return timestamp !== undefined ? timestamp < expireOlderThan : true;
191
- }
192
- async delete() {
193
- this._rerunRequested = false;
194
- await this._timestampModel.expireEntries(Infinity);
195
- }
196
- }
197
-
198
- class ExpirationPlugin {
199
- _config;
200
- _maxAgeSeconds;
201
- _cacheExpirations;
202
- constructor(config = {}){
203
- if (process.env.NODE_ENV !== "production") {
204
- if (!(config.maxEntries || config.maxAgeSeconds)) {
205
- throw new SerwistError("max-entries-or-age-required", {
206
- moduleName: "@serwist/expiration",
207
- className: "Plugin",
208
- funcName: "constructor"
209
- });
210
- }
211
- if (config.maxEntries) {
212
- assert.isType(config.maxEntries, "number", {
213
- moduleName: "@serwist/expiration",
214
- className: "Plugin",
215
- funcName: "constructor",
216
- paramName: "config.maxEntries"
217
- });
218
- }
219
- if (config.maxAgeSeconds) {
220
- assert.isType(config.maxAgeSeconds, "number", {
221
- moduleName: "@serwist/expiration",
222
- className: "Plugin",
223
- funcName: "constructor",
224
- paramName: "config.maxAgeSeconds"
225
- });
226
- }
227
- }
228
- this._config = config;
229
- this._maxAgeSeconds = config.maxAgeSeconds;
230
- this._cacheExpirations = new Map();
231
- if (config.purgeOnQuotaError) {
232
- registerQuotaErrorCallback(()=>this.deleteCacheAndMetadata());
233
- }
234
- }
235
- _getCacheExpiration(cacheName) {
236
- if (cacheName === privateCacheNames.getRuntimeName()) {
237
- throw new SerwistError("expire-custom-caches-only");
238
- }
239
- let cacheExpiration = this._cacheExpirations.get(cacheName);
240
- if (!cacheExpiration) {
241
- cacheExpiration = new CacheExpiration(cacheName, this._config);
242
- this._cacheExpirations.set(cacheName, cacheExpiration);
243
- }
244
- return cacheExpiration;
245
- }
246
- cachedResponseWillBeUsed = async ({ event, request, cacheName, cachedResponse })=>{
247
- if (!cachedResponse) {
248
- return null;
249
- }
250
- const isFresh = this._isResponseDateFresh(cachedResponse);
251
- const cacheExpiration = this._getCacheExpiration(cacheName);
252
- dontWaitFor(cacheExpiration.expireEntries());
253
- const updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
254
- if (event) {
255
- try {
256
- event.waitUntil(updateTimestampDone);
257
- } catch (error) {
258
- if (process.env.NODE_ENV !== "production") {
259
- if ("request" in event) {
260
- logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL(event.request.url)}'.`);
261
- }
262
- }
263
- }
264
- }
265
- return isFresh ? cachedResponse : null;
266
- };
267
- _isResponseDateFresh(cachedResponse) {
268
- if (!this._maxAgeSeconds) {
269
- return true;
270
- }
271
- const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
272
- if (dateHeaderTimestamp === null) {
273
- return true;
274
- }
275
- const now = Date.now();
276
- return dateHeaderTimestamp >= now - this._maxAgeSeconds * 1000;
277
- }
278
- _getDateHeaderTimestamp(cachedResponse) {
279
- if (!cachedResponse.headers.has("date")) {
280
- return null;
281
- }
282
- const dateHeader = cachedResponse.headers.get("date");
283
- const parsedDate = new Date(dateHeader);
284
- const headerTime = parsedDate.getTime();
285
- if (Number.isNaN(headerTime)) {
286
- return null;
287
- }
288
- return headerTime;
289
- }
290
- cacheDidUpdate = async ({ cacheName, request })=>{
291
- if (process.env.NODE_ENV !== "production") {
292
- assert.isType(cacheName, "string", {
293
- moduleName: "@serwist/expiration",
294
- className: "Plugin",
295
- funcName: "cacheDidUpdate",
296
- paramName: "cacheName"
297
- });
298
- assert.isInstance(request, Request, {
299
- moduleName: "@serwist/expiration",
300
- className: "Plugin",
301
- funcName: "cacheDidUpdate",
302
- paramName: "request"
303
- });
304
- }
305
- const cacheExpiration = this._getCacheExpiration(cacheName);
306
- await cacheExpiration.updateTimestamp(request.url);
307
- await cacheExpiration.expireEntries();
308
- };
309
- async deleteCacheAndMetadata() {
310
- for (const [cacheName, cacheExpiration] of this._cacheExpirations){
311
- await self.caches.delete(cacheName);
312
- await cacheExpiration.delete();
313
- }
314
- this._cacheExpirations = new Map();
315
- }
316
- }
317
-
318
- export { CacheExpiration, ExpirationPlugin };
1
+ export { CacheExpiration, ExpirationPlugin } from 'serwist';
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@serwist/expiration",
3
- "version": "9.0.0-preview.9",
3
+ "version": "9.0.0",
4
4
  "type": "module",
5
- "description": "A service worker helper library that expires cached responses based on age or maximum number of entries.",
5
+ "description": "A module that expires cached responses based on age or maximum number of entries.",
6
6
  "files": [
7
7
  "src",
8
8
  "dist"
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "author": "Google's Web DevRel Team, Serwist's Team",
18
18
  "license": "MIT",
19
- "repository": "serwist/serwist",
19
+ "repository": "https://github.com/serwist/serwist",
20
20
  "bugs": "https://github.com/serwist/serwist/issues",
21
21
  "homepage": "https://serwist.pages.dev",
22
22
  "main": "./dist/index.js",
@@ -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.9"
32
+ "serwist": "9.0.0"
34
33
  },
35
34
  "devDependencies": {
36
- "rollup": "4.9.6",
37
- "typescript": "5.4.0-dev.20240206",
38
- "@serwist/constants": "9.0.0-preview.9"
35
+ "rollup": "4.14.3",
36
+ "typescript": "5.5.0-dev.20240415",
37
+ "@serwist/configs": "9.0.0"
39
38
  },
40
39
  "peerDependencies": {
41
40
  "typescript": ">=5.0.0"
@@ -47,7 +46,6 @@
47
46
  },
48
47
  "scripts": {
49
48
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
50
- "dev": "rollup --config rollup.config.js --watch",
51
49
  "lint": "biome lint ./src",
52
50
  "typecheck": "tsc"
53
51
  }
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";
2
+ export type { ExpirationPluginOptions } from "serwist";
@@ -1,69 +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
- * The `CacheExpiration` class allows you define an expiration and / or
19
- * limit on the number of responses stored in a
20
- * [`Cache`](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
21
- */
22
- declare class CacheExpiration {
23
- private _isRunning;
24
- private _rerunRequested;
25
- private readonly _maxEntries?;
26
- private readonly _maxAgeSeconds?;
27
- private readonly _matchOptions?;
28
- private readonly _cacheName;
29
- private readonly _timestampModel;
30
- /**
31
- * To construct a new CacheExpiration instance you must provide at least
32
- * one of the `config` properties.
33
- *
34
- * @param cacheName Name of the cache to apply restrictions to.
35
- * @param config
36
- */
37
- constructor(cacheName: string, config?: CacheExpirationConfig);
38
- /**
39
- * Expires entries for the given cache and given criteria.
40
- */
41
- expireEntries(): Promise<void>;
42
- /**
43
- * Update the timestamp for the given URL. This ensures the when
44
- * removing entries based on maximum entries, most recently used
45
- * is accurate or when expiring, the timestamp is up-to-date.
46
- *
47
- * @param url
48
- */
49
- updateTimestamp(url: string): Promise<void>;
50
- /**
51
- * Can be used to check if a URL has expired or not before it's used.
52
- *
53
- * This requires a look up from IndexedDB, so can be slow.
54
- *
55
- * Note: This method will not remove the cached entry, call
56
- * `expireEntries()` to remove indexedDB and Cache entries.
57
- *
58
- * @param url
59
- * @returns
60
- */
61
- isURLExpired(url: string): Promise<boolean>;
62
- /**
63
- * Removes the IndexedDB object store used to keep track of cache expiration
64
- * metadata.
65
- */
66
- delete(): Promise<void>;
67
- }
68
- export { CacheExpiration };
69
- //# 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;;;;GAIG;AACH,cAAM,eAAe;IACnB,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;;;;;;OAMG;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;;;OAGG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAM9B;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -1,116 +0,0 @@
1
- import type { 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 age of an entry before it's treated as stale and removed.
10
- */
11
- maxAgeSeconds?: number;
12
- /**
13
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
14
- * that will be used when calling `delete()` on the cache.
15
- */
16
- matchOptions?: CacheQueryOptions;
17
- /**
18
- * Whether to opt this cache in to automatic deletion if the available storage quota has been exceeded.
19
- */
20
- purgeOnQuotaError?: boolean;
21
- }
22
- /**
23
- * This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
24
- * limit on the age and / or the number of cached requests.
25
- *
26
- * It can only be used with Strategy instances that have a
27
- * [custom `cacheName` property set](/web/tools/workbox/guides/configure-workbox#custom_cache_names_in_strategies).
28
- * In other words, it can't be used to expire entries in strategy that uses the
29
- * default runtime cache name.
30
- *
31
- * Whenever a cached response is used or updated, this plugin will look
32
- * at the associated cache and remove any old or extra responses.
33
- *
34
- * When using `maxAgeSeconds`, responses may be used *once* after expiring
35
- * because the expiration clean up will not have occurred until *after* the
36
- * cached response has been used. If the response has a "Date" header, then
37
- * a light weight expiration check is performed and the response will not be
38
- * used immediately.
39
- *
40
- * When using `maxEntries`, the entry least-recently requested will be removed
41
- * from the cache first.
42
- */
43
- declare class ExpirationPlugin implements SerwistPlugin {
44
- private readonly _config;
45
- private readonly _maxAgeSeconds?;
46
- private _cacheExpirations;
47
- /**
48
- * @param config
49
- */
50
- constructor(config?: ExpirationPluginOptions);
51
- /**
52
- * A simple helper method to return a CacheExpiration instance for a given
53
- * cache name.
54
- *
55
- * @param cacheName
56
- * @returns
57
- * @private
58
- */
59
- private _getCacheExpiration;
60
- /**
61
- * A "lifecycle" callback that will be triggered automatically by the
62
- * `@serwist/strategies` handlers when a `Response` is about to be returned
63
- * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
64
- * the handler. It allows the `Response` to be inspected for freshness and
65
- * prevents it from being used if the `Response`'s `Date` header value is
66
- * older than the configured `maxAgeSeconds`.
67
- *
68
- * @param options
69
- * @returns Either the `cachedResponse`, if it's fresh, or `null` if the `Response`
70
- * is older than `maxAgeSeconds`.
71
- * @private
72
- */
73
- cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"];
74
- /**
75
- * @param cachedResponse
76
- * @returns
77
- * @private
78
- */
79
- private _isResponseDateFresh;
80
- /**
81
- * This method will extract the data header and parse it into a useful
82
- * value.
83
- *
84
- * @param cachedResponse
85
- * @returns
86
- * @private
87
- */
88
- private _getDateHeaderTimestamp;
89
- /**
90
- * A "lifecycle" callback that will be triggered automatically by the
91
- * `@serwist/strategies` handlers when an entry is added to a cache.
92
- *
93
- * @param options
94
- * @private
95
- */
96
- cacheDidUpdate: SerwistPlugin["cacheDidUpdate"];
97
- /**
98
- * This is a helper method that performs two operations:
99
- *
100
- * - Deletes *all* the underlying Cache instances associated with this plugin
101
- * instance, by calling caches.delete() on your behalf.
102
- * - Deletes the metadata from IndexedDB used to keep track of expiration
103
- * details for each Cache instance.
104
- *
105
- * When using cache expiration, calling this method is preferable to calling
106
- * `caches.delete()` directly, since this will ensure that the IndexedDB
107
- * metadata is also cleanly removed and open IndexedDB instances are deleted.
108
- *
109
- * Note that if you're *not* using cache expiration for a given cache, calling
110
- * `caches.delete()` and passing in the cache's name should be sufficient.
111
- * There is no Serwist-specific method needed for cleanup in that case.
112
- */
113
- deleteCacheAndMetadata(): Promise<void>;
114
- }
115
- export { ExpirationPlugin };
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,aAAa,EAAE,MAAM,eAAe,CAAC;AAMnD,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,YAAY,CAAC,EAAE,iBAAiB,CAAC;IACjC;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,cAAM,gBAAiB,YAAW,aAAa;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0B;IAClD,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAS;IACzC,OAAO,CAAC,iBAAiB,CAA+B;IAExD;;OAEG;gBACS,MAAM,GAAE,uBAA4B;IAsChD;;;;;;;OAOG;IACH,OAAO,CAAC,mBAAmB;IAa3B;;;;;;;;;;;;OAYG;IACH,wBAAwB,EAAE,aAAa,CAAC,0BAA0B,CAAC,CA+BjE;IAEF;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAqB5B;;;;;;;OAOG;IACH,OAAO,CAAC,uBAAuB;IAkB/B;;;;;;OAMG;IACH,cAAc,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAmB7C;IAEF;;;;;;;;;;;;;;;OAeG;IACG,sBAAsB,IAAI,OAAO,CAAC,IAAI,CAAC;CAW9C;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAC"}
@@ -1,74 +0,0 @@
1
- /**
2
- * Returns the timestamp model.
3
- *
4
- * @private
5
- */
6
- 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
- * Takes a URL and returns an ID that will be unique in the object store.
60
- *
61
- * @param url
62
- * @returns
63
- * @private
64
- */
65
- private _getId;
66
- /**
67
- * Returns an open connection to the database.
68
- *
69
- * @private
70
- */
71
- private getDb;
72
- }
73
- export { CacheTimestampsModel };
74
- //# sourceMappingURL=CacheTimestampsModel.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CacheTimestampsModel.d.ts","sourceRoot":"","sources":["../../src/models/CacheTimestampsModel.ts"],"names":[],"mappings":"AAoCA;;;;GAIG;AACH,cAAM,oBAAoB;IACxB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,GAAG,CAA4C;IAEvD;;;;;OAKG;gBACS,SAAS,EAAE,MAAM;IAI7B;;;;;;OAMG;IACH,OAAO,CAAC,UAAU;IAgBlB;;;;;;OAMG;IACH,OAAO,CAAC,yBAAyB;IAOjC;;;;;OAKG;IACG,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBjE;;;;;;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;IA2C/E;;;;;;OAMG;IACH,OAAO,CAAC,MAAM;IAOd;;;;OAIG;YACW,KAAK;CAQpB;AAED,OAAO,EAAE,oBAAoB,EAAE,CAAC"}
@@ -1,197 +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, dontWaitFor, 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
- * The `CacheExpiration` class allows you define an expiration and / or
32
- * limit on the number of responses stored in a
33
- * [`Cache`](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
34
- */
35
- class CacheExpiration {
36
- private _isRunning = false;
37
- private _rerunRequested = false;
38
- private readonly _maxEntries?: number;
39
- private readonly _maxAgeSeconds?: number;
40
- private readonly _matchOptions?: CacheQueryOptions;
41
- private readonly _cacheName: string;
42
- private readonly _timestampModel: CacheTimestampsModel;
43
-
44
- /**
45
- * To construct a new CacheExpiration instance you must provide at least
46
- * one of the `config` properties.
47
- *
48
- * @param cacheName Name of the cache to apply restrictions to.
49
- * @param config
50
- */
51
- constructor(cacheName: string, config: CacheExpirationConfig = {}) {
52
- if (process.env.NODE_ENV !== "production") {
53
- assert!.isType(cacheName, "string", {
54
- moduleName: "@serwist/expiration",
55
- className: "CacheExpiration",
56
- funcName: "constructor",
57
- paramName: "cacheName",
58
- });
59
-
60
- if (!(config.maxEntries || config.maxAgeSeconds)) {
61
- throw new SerwistError("max-entries-or-age-required", {
62
- moduleName: "@serwist/expiration",
63
- className: "CacheExpiration",
64
- funcName: "constructor",
65
- });
66
- }
67
-
68
- if (config.maxEntries) {
69
- assert!.isType(config.maxEntries, "number", {
70
- moduleName: "@serwist/expiration",
71
- className: "CacheExpiration",
72
- funcName: "constructor",
73
- paramName: "config.maxEntries",
74
- });
75
- }
76
-
77
- if (config.maxAgeSeconds) {
78
- assert!.isType(config.maxAgeSeconds, "number", {
79
- moduleName: "@serwist/expiration",
80
- className: "CacheExpiration",
81
- funcName: "constructor",
82
- paramName: "config.maxAgeSeconds",
83
- });
84
- }
85
- }
86
-
87
- this._maxEntries = config.maxEntries;
88
- this._maxAgeSeconds = config.maxAgeSeconds;
89
- this._matchOptions = config.matchOptions;
90
- this._cacheName = cacheName;
91
- this._timestampModel = new CacheTimestampsModel(cacheName);
92
- }
93
-
94
- /**
95
- * Expires entries for the given cache and given criteria.
96
- */
97
- async expireEntries(): Promise<void> {
98
- if (this._isRunning) {
99
- this._rerunRequested = true;
100
- return;
101
- }
102
- this._isRunning = true;
103
-
104
- const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
105
-
106
- const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
107
-
108
- // Delete URLs from the cache
109
- const cache = await self.caches.open(this._cacheName);
110
- for (const url of urlsExpired) {
111
- await cache.delete(url, this._matchOptions);
112
- }
113
-
114
- if (process.env.NODE_ENV !== "production") {
115
- if (urlsExpired.length > 0) {
116
- logger.groupCollapsed(
117
- `Expired ${urlsExpired.length} ` +
118
- `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` +
119
- `${urlsExpired.length === 1 ? "it" : "them"} from the ` +
120
- `'${this._cacheName}' cache.`,
121
- );
122
- logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
123
- for (const url of urlsExpired) {
124
- logger.log(` ${url}`);
125
- }
126
- logger.groupEnd();
127
- } else {
128
- logger.debug("Cache expiration ran and found no entries to remove.");
129
- }
130
- }
131
-
132
- this._isRunning = false;
133
- if (this._rerunRequested) {
134
- this._rerunRequested = false;
135
- dontWaitFor(this.expireEntries());
136
- }
137
- }
138
-
139
- /**
140
- * Update the timestamp for the given URL. This ensures the when
141
- * removing entries based on maximum entries, most recently used
142
- * is accurate or when expiring, the timestamp is up-to-date.
143
- *
144
- * @param url
145
- */
146
- async updateTimestamp(url: string): Promise<void> {
147
- if (process.env.NODE_ENV !== "production") {
148
- assert!.isType(url, "string", {
149
- moduleName: "@serwist/expiration",
150
- className: "CacheExpiration",
151
- funcName: "updateTimestamp",
152
- paramName: "url",
153
- });
154
- }
155
-
156
- await this._timestampModel.setTimestamp(url, Date.now());
157
- }
158
-
159
- /**
160
- * Can be used to check if a URL has expired or not before it's used.
161
- *
162
- * This requires a look up from IndexedDB, so can be slow.
163
- *
164
- * Note: This method will not remove the cached entry, call
165
- * `expireEntries()` to remove indexedDB and Cache entries.
166
- *
167
- * @param url
168
- * @returns
169
- */
170
- async isURLExpired(url: string): Promise<boolean> {
171
- if (!this._maxAgeSeconds) {
172
- if (process.env.NODE_ENV !== "production") {
173
- throw new SerwistError("expired-test-without-max-age", {
174
- methodName: "isURLExpired",
175
- paramName: "maxAgeSeconds",
176
- });
177
- }
178
- return false;
179
- }
180
- const timestamp = await this._timestampModel.getTimestamp(url);
181
- const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
182
- return timestamp !== undefined ? timestamp < expireOlderThan : true;
183
- }
184
-
185
- /**
186
- * Removes the IndexedDB object store used to keep track of cache expiration
187
- * metadata.
188
- */
189
- async delete(): Promise<void> {
190
- // Make sure we don't attempt another rerun if we're called in the middle of
191
- // a cache expiration.
192
- this._rerunRequested = false;
193
- await this._timestampModel.expireEntries(Infinity); // Expires all.
194
- }
195
- }
196
-
197
- export { CacheExpiration };
@@ -1,279 +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 { SerwistPlugin } from "@serwist/core";
10
- import { registerQuotaErrorCallback } from "@serwist/core";
11
- import { assert, SerwistError, dontWaitFor, 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 age of an entry before it's treated as stale and removed.
23
- */
24
- maxAgeSeconds?: number;
25
- /**
26
- * The [`CacheQueryOptions`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/delete#Parameters)
27
- * that will be used when calling `delete()` on the cache.
28
- */
29
- matchOptions?: CacheQueryOptions;
30
- /**
31
- * Whether to opt this cache in to automatic deletion if the available storage quota has been exceeded.
32
- */
33
- purgeOnQuotaError?: boolean;
34
- }
35
-
36
- /**
37
- * This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
38
- * limit on the age and / or the number of cached requests.
39
- *
40
- * It can only be used with Strategy instances that have a
41
- * [custom `cacheName` property set](/web/tools/workbox/guides/configure-workbox#custom_cache_names_in_strategies).
42
- * In other words, it can't be used to expire entries in strategy that uses the
43
- * default runtime cache name.
44
- *
45
- * Whenever a cached response is used or updated, this plugin will look
46
- * at the associated cache and remove any old or extra responses.
47
- *
48
- * When using `maxAgeSeconds`, responses may be used *once* after expiring
49
- * because the expiration clean up will not have occurred until *after* the
50
- * cached response has been used. If the response has a "Date" header, then
51
- * a light weight expiration check is performed and the response will not be
52
- * used immediately.
53
- *
54
- * When using `maxEntries`, the entry least-recently requested will be removed
55
- * from the cache first.
56
- */
57
- class ExpirationPlugin implements SerwistPlugin {
58
- private readonly _config: ExpirationPluginOptions;
59
- private readonly _maxAgeSeconds?: number;
60
- private _cacheExpirations: Map<string, CacheExpiration>;
61
-
62
- /**
63
- * @param config
64
- */
65
- constructor(config: ExpirationPluginOptions = {}) {
66
- if (process.env.NODE_ENV !== "production") {
67
- if (!(config.maxEntries || config.maxAgeSeconds)) {
68
- throw new SerwistError("max-entries-or-age-required", {
69
- moduleName: "@serwist/expiration",
70
- className: "Plugin",
71
- funcName: "constructor",
72
- });
73
- }
74
-
75
- if (config.maxEntries) {
76
- assert!.isType(config.maxEntries, "number", {
77
- moduleName: "@serwist/expiration",
78
- className: "Plugin",
79
- funcName: "constructor",
80
- paramName: "config.maxEntries",
81
- });
82
- }
83
-
84
- if (config.maxAgeSeconds) {
85
- assert!.isType(config.maxAgeSeconds, "number", {
86
- moduleName: "@serwist/expiration",
87
- className: "Plugin",
88
- funcName: "constructor",
89
- paramName: "config.maxAgeSeconds",
90
- });
91
- }
92
- }
93
-
94
- this._config = config;
95
- this._maxAgeSeconds = config.maxAgeSeconds;
96
- this._cacheExpirations = new Map();
97
-
98
- if (config.purgeOnQuotaError) {
99
- registerQuotaErrorCallback(() => this.deleteCacheAndMetadata());
100
- }
101
- }
102
-
103
- /**
104
- * A simple helper method to return a CacheExpiration instance for a given
105
- * cache name.
106
- *
107
- * @param cacheName
108
- * @returns
109
- * @private
110
- */
111
- private _getCacheExpiration(cacheName: string): CacheExpiration {
112
- if (cacheName === privateCacheNames.getRuntimeName()) {
113
- throw new SerwistError("expire-custom-caches-only");
114
- }
115
-
116
- let cacheExpiration = this._cacheExpirations.get(cacheName);
117
- if (!cacheExpiration) {
118
- cacheExpiration = new CacheExpiration(cacheName, this._config);
119
- this._cacheExpirations.set(cacheName, cacheExpiration);
120
- }
121
- return cacheExpiration;
122
- }
123
-
124
- /**
125
- * A "lifecycle" callback that will be triggered automatically by the
126
- * `@serwist/strategies` handlers when a `Response` is about to be returned
127
- * from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
128
- * the handler. It allows the `Response` to be inspected for freshness and
129
- * prevents it from being used if the `Response`'s `Date` header value is
130
- * older than the configured `maxAgeSeconds`.
131
- *
132
- * @param options
133
- * @returns Either the `cachedResponse`, if it's fresh, or `null` if the `Response`
134
- * is older than `maxAgeSeconds`.
135
- * @private
136
- */
137
- cachedResponseWillBeUsed: SerwistPlugin["cachedResponseWillBeUsed"] = async ({ event, request, cacheName, cachedResponse }) => {
138
- if (!cachedResponse) {
139
- return null;
140
- }
141
-
142
- const isFresh = this._isResponseDateFresh(cachedResponse);
143
-
144
- // Expire entries to ensure that even if the expiration date has
145
- // expired, it'll only be used once.
146
- const cacheExpiration = this._getCacheExpiration(cacheName);
147
- dontWaitFor(cacheExpiration.expireEntries());
148
-
149
- // Update the metadata for the request URL to the current timestamp,
150
- // but don't `await` it as we don't want to block the response.
151
- const updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
152
- if (event) {
153
- try {
154
- event.waitUntil(updateTimestampDone);
155
- } catch (error) {
156
- if (process.env.NODE_ENV !== "production") {
157
- // The event may not be a fetch event; only log the URL if it is.
158
- if ("request" in event) {
159
- logger.warn(
160
- `Unable to ensure service worker stays alive when updating cache entry for '${getFriendlyURL((event as FetchEvent).request.url)}'.`,
161
- );
162
- }
163
- }
164
- }
165
- }
166
-
167
- return isFresh ? cachedResponse : null;
168
- };
169
-
170
- /**
171
- * @param cachedResponse
172
- * @returns
173
- * @private
174
- */
175
- private _isResponseDateFresh(cachedResponse: Response): boolean {
176
- if (!this._maxAgeSeconds) {
177
- // We aren't expiring by age, so return true, it's fresh
178
- return true;
179
- }
180
-
181
- // Check if the 'date' header will suffice a quick expiration check.
182
- // See https://github.com/GoogleChromeLabs/sw-toolbox/issues/164 for
183
- // discussion.
184
- const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
185
- if (dateHeaderTimestamp === null) {
186
- // Unable to parse date, so assume it's fresh.
187
- return true;
188
- }
189
-
190
- // If we have a valid headerTime, then our response is fresh iff the
191
- // headerTime plus maxAgeSeconds is greater than the current time.
192
- const now = Date.now();
193
- return dateHeaderTimestamp >= now - this._maxAgeSeconds * 1000;
194
- }
195
-
196
- /**
197
- * This method will extract the data header and parse it into a useful
198
- * value.
199
- *
200
- * @param cachedResponse
201
- * @returns
202
- * @private
203
- */
204
- private _getDateHeaderTimestamp(cachedResponse: Response): number | null {
205
- if (!cachedResponse.headers.has("date")) {
206
- return null;
207
- }
208
-
209
- const dateHeader = cachedResponse.headers.get("date");
210
- const parsedDate = new Date(dateHeader!);
211
- const headerTime = parsedDate.getTime();
212
-
213
- // If the Date header was invalid for some reason, parsedDate.getTime()
214
- // will return NaN.
215
- if (Number.isNaN(headerTime)) {
216
- return null;
217
- }
218
-
219
- return headerTime;
220
- }
221
-
222
- /**
223
- * A "lifecycle" callback that will be triggered automatically by the
224
- * `@serwist/strategies` handlers when an entry is added to a cache.
225
- *
226
- * @param options
227
- * @private
228
- */
229
- cacheDidUpdate: SerwistPlugin["cacheDidUpdate"] = async ({ cacheName, request }) => {
230
- if (process.env.NODE_ENV !== "production") {
231
- assert!.isType(cacheName, "string", {
232
- moduleName: "@serwist/expiration",
233
- className: "Plugin",
234
- funcName: "cacheDidUpdate",
235
- paramName: "cacheName",
236
- });
237
- assert!.isInstance(request, Request, {
238
- moduleName: "@serwist/expiration",
239
- className: "Plugin",
240
- funcName: "cacheDidUpdate",
241
- paramName: "request",
242
- });
243
- }
244
-
245
- const cacheExpiration = this._getCacheExpiration(cacheName);
246
- await cacheExpiration.updateTimestamp(request.url);
247
- await cacheExpiration.expireEntries();
248
- };
249
-
250
- /**
251
- * This is a helper method that performs two operations:
252
- *
253
- * - Deletes *all* the underlying Cache instances associated with this plugin
254
- * instance, by calling caches.delete() on your behalf.
255
- * - Deletes the metadata from IndexedDB used to keep track of expiration
256
- * details for each Cache instance.
257
- *
258
- * When using cache expiration, calling this method is preferable to calling
259
- * `caches.delete()` directly, since this will ensure that the IndexedDB
260
- * metadata is also cleanly removed and open IndexedDB instances are deleted.
261
- *
262
- * Note that if you're *not* using cache expiration for a given cache, calling
263
- * `caches.delete()` and passing in the cache's name should be sufficient.
264
- * There is no Serwist-specific method needed for cleanup in that case.
265
- */
266
- async deleteCacheAndMetadata(): Promise<void> {
267
- // Do this one at a time instead of all at once via `Promise.all()` to
268
- // reduce the chance of inconsistency if a promise rejects.
269
- for (const [cacheName, cacheExpiration] of this._cacheExpirations) {
270
- await self.caches.delete(cacheName);
271
- await cacheExpiration.delete();
272
- }
273
-
274
- // Reset this._cacheExpirations to its initial state.
275
- this._cacheExpirations = new Map();
276
- }
277
- }
278
-
279
- export { ExpirationPlugin };
@@ -1,214 +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
- id: string;
24
- cacheName: string;
25
- url: string;
26
- timestamp: number;
27
- }
28
-
29
- interface CacheDbSchema extends DBSchema {
30
- "cache-entries": {
31
- key: string;
32
- value: CacheTimestampsModelEntry;
33
- indexes: { cacheName: string; timestamp: number };
34
- };
35
- }
36
-
37
- /**
38
- * Returns the timestamp model.
39
- *
40
- * @private
41
- */
42
- class CacheTimestampsModel {
43
- private readonly _cacheName: string;
44
- private _db: IDBPDatabase<CacheDbSchema> | null = null;
45
-
46
- /**
47
- *
48
- * @param cacheName
49
- *
50
- * @private
51
- */
52
- constructor(cacheName: string) {
53
- this._cacheName = cacheName;
54
- }
55
-
56
- /**
57
- * Performs an upgrade of indexedDB.
58
- *
59
- * @param db
60
- *
61
- * @private
62
- */
63
- private _upgradeDb(db: IDBPDatabase<CacheDbSchema>) {
64
- // TODO(philipwalton): EdgeHTML doesn't support arrays as a keyPath, so we
65
- // have to use the `id` keyPath here and create our own values (a
66
- // concatenation of `url + cacheName`) instead of simply using
67
- // `keyPath: ['url', 'cacheName']`, which is supported in other browsers.
68
- const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
69
- keyPath: "id",
70
- });
71
-
72
- // TODO(philipwalton): once we don't have to support EdgeHTML, we can
73
- // create a single index with the keyPath `['cacheName', 'timestamp']`
74
- // instead of doing both these indexes.
75
- objStore.createIndex("cacheName", "cacheName", { unique: false });
76
- objStore.createIndex("timestamp", "timestamp", { unique: false });
77
- }
78
-
79
- /**
80
- * Performs an upgrade of indexedDB and deletes deprecated DBs.
81
- *
82
- * @param db
83
- *
84
- * @private
85
- */
86
- private _upgradeDbAndDeleteOldDbs(db: IDBPDatabase<CacheDbSchema>) {
87
- this._upgradeDb(db);
88
- if (this._cacheName) {
89
- void deleteDB(this._cacheName);
90
- }
91
- }
92
-
93
- /**
94
- * @param url
95
- * @param timestamp
96
- *
97
- * @private
98
- */
99
- async setTimestamp(url: string, timestamp: number): Promise<void> {
100
- url = normalizeURL(url);
101
-
102
- const entry: CacheTimestampsModelEntry = {
103
- url,
104
- timestamp,
105
- cacheName: this._cacheName,
106
- // Creating an ID from the URL and cache name won't be necessary once
107
- // Edge switches to Chromium and all browsers we support work with
108
- // array keyPaths.
109
- id: this._getId(url),
110
- };
111
- const db = await this.getDb();
112
- const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
113
- durability: "relaxed",
114
- });
115
- await tx.store.put(entry);
116
- await tx.done;
117
- }
118
-
119
- /**
120
- * Returns the timestamp stored for a given URL.
121
- *
122
- * @param url
123
- * @returns
124
- * @private
125
- */
126
- async getTimestamp(url: string): Promise<number | undefined> {
127
- const db = await this.getDb();
128
- const entry = await db.get(CACHE_OBJECT_STORE, this._getId(url));
129
- return entry?.timestamp;
130
- }
131
-
132
- /**
133
- * Iterates through all the entries in the object store (from newest to
134
- * oldest) and removes entries once either `maxCount` is reached or the
135
- * entry's timestamp is less than `minTimestamp`.
136
- *
137
- * @param minTimestamp
138
- * @param maxCount
139
- * @returns
140
- * @private
141
- */
142
- async expireEntries(minTimestamp: number, maxCount?: number): Promise<string[]> {
143
- const db = await this.getDb();
144
- let cursor = await db.transaction(CACHE_OBJECT_STORE).store.index("timestamp").openCursor(null, "prev");
145
- const entriesToDelete: CacheTimestampsModelEntry[] = [];
146
- let entriesNotDeletedCount = 0;
147
- while (cursor) {
148
- const result = cursor.value;
149
- // TODO(philipwalton): once we can use a multi-key index, we
150
- // won't have to check `cacheName` here.
151
- if (result.cacheName === this._cacheName) {
152
- // Delete an entry if it's older than the max age or
153
- // if we already have the max number allowed.
154
- if ((minTimestamp && result.timestamp < minTimestamp) || (maxCount && entriesNotDeletedCount >= maxCount)) {
155
- // TODO(philipwalton): we should be able to delete the
156
- // entry right here, but doing so causes an iteration
157
- // bug in Safari stable (fixed in TP). Instead we can
158
- // store the keys of the entries to delete, and then
159
- // delete the separate transactions.
160
- // https://github.com/GoogleChrome/workbox/issues/1978
161
- // cursor.delete();
162
-
163
- // We only need to return the URL, not the whole entry.
164
- entriesToDelete.push(cursor.value);
165
- } else {
166
- entriesNotDeletedCount++;
167
- }
168
- }
169
- cursor = await cursor.continue();
170
- }
171
-
172
- // TODO(philipwalton): once the Safari bug in the following issue is fixed,
173
- // we should be able to remove this loop and do the entry deletion in the
174
- // cursor loop above:
175
- // https://github.com/GoogleChrome/workbox/issues/1978
176
- const urlsDeleted: string[] = [];
177
- for (const entry of entriesToDelete) {
178
- await db.delete(CACHE_OBJECT_STORE, entry.id);
179
- urlsDeleted.push(entry.url);
180
- }
181
-
182
- return urlsDeleted;
183
- }
184
-
185
- /**
186
- * Takes a URL and returns an ID that will be unique in the object store.
187
- *
188
- * @param url
189
- * @returns
190
- * @private
191
- */
192
- private _getId(url: string): string {
193
- // Creating an ID from the URL and cache name won't be necessary once
194
- // Edge switches to Chromium and all browsers we support work with
195
- // array keyPaths.
196
- return `${this._cacheName}|${normalizeURL(url)}`;
197
- }
198
-
199
- /**
200
- * Returns an open connection to the database.
201
- *
202
- * @private
203
- */
204
- private async getDb() {
205
- if (!this._db) {
206
- this._db = await openDB(DB_NAME, 1, {
207
- upgrade: this._upgradeDbAndDeleteOldDbs.bind(this),
208
- });
209
- }
210
- return this._db;
211
- }
212
- }
213
-
214
- export { CacheTimestampsModel };