@serwist/expiration 8.4.4 → 9.0.0-preview.1
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/CacheExpiration.d.ts +1 -0
- package/dist/CacheExpiration.d.ts.map +1 -0
- package/dist/ExpirationPlugin.d.ts +1 -0
- package/dist/ExpirationPlugin.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -228
- package/dist/models/CacheTimestampsModel.d.ts +1 -0
- package/dist/models/CacheTimestampsModel.d.ts.map +1 -0
- package/package.json +18 -16
- package/src/CacheExpiration.ts +197 -0
- package/src/ExpirationPlugin.ts +279 -0
- package/{dist/index.d.cts → src/index.ts} +10 -0
- package/src/models/CacheTimestampsModel.ts +214 -0
- package/dist/index.cjs +0 -525
package/dist/index.cjs
DELETED
|
@@ -1,525 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var internal = require('@serwist/core/internal');
|
|
4
|
-
var idb = require('idb');
|
|
5
|
-
var core = require('@serwist/core');
|
|
6
|
-
|
|
7
|
-
const DB_NAME = "serwist-expiration";
|
|
8
|
-
const CACHE_OBJECT_STORE = "cache-entries";
|
|
9
|
-
const normalizeURL = (unNormalizedUrl)=>{
|
|
10
|
-
const url = new URL(unNormalizedUrl, location.href);
|
|
11
|
-
url.hash = "";
|
|
12
|
-
return url.href;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Returns the timestamp model.
|
|
16
|
-
*
|
|
17
|
-
* @private
|
|
18
|
-
*/ class CacheTimestampsModel {
|
|
19
|
-
_cacheName;
|
|
20
|
-
_db = null;
|
|
21
|
-
/**
|
|
22
|
-
*
|
|
23
|
-
* @param cacheName
|
|
24
|
-
*
|
|
25
|
-
* @private
|
|
26
|
-
*/ constructor(cacheName){
|
|
27
|
-
this._cacheName = cacheName;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Performs an upgrade of indexedDB.
|
|
31
|
-
*
|
|
32
|
-
* @param db
|
|
33
|
-
*
|
|
34
|
-
* @private
|
|
35
|
-
*/ _upgradeDb(db) {
|
|
36
|
-
// TODO(philipwalton): EdgeHTML doesn't support arrays as a keyPath, so we
|
|
37
|
-
// have to use the `id` keyPath here and create our own values (a
|
|
38
|
-
// concatenation of `url + cacheName`) instead of simply using
|
|
39
|
-
// `keyPath: ['url', 'cacheName']`, which is supported in other browsers.
|
|
40
|
-
const objStore = db.createObjectStore(CACHE_OBJECT_STORE, {
|
|
41
|
-
keyPath: "id"
|
|
42
|
-
});
|
|
43
|
-
// TODO(philipwalton): once we don't have to support EdgeHTML, we can
|
|
44
|
-
// create a single index with the keyPath `['cacheName', 'timestamp']`
|
|
45
|
-
// instead of doing both these indexes.
|
|
46
|
-
objStore.createIndex("cacheName", "cacheName", {
|
|
47
|
-
unique: false
|
|
48
|
-
});
|
|
49
|
-
objStore.createIndex("timestamp", "timestamp", {
|
|
50
|
-
unique: false
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Performs an upgrade of indexedDB and deletes deprecated DBs.
|
|
55
|
-
*
|
|
56
|
-
* @param db
|
|
57
|
-
*
|
|
58
|
-
* @private
|
|
59
|
-
*/ _upgradeDbAndDeleteOldDbs(db) {
|
|
60
|
-
this._upgradeDb(db);
|
|
61
|
-
if (this._cacheName) {
|
|
62
|
-
void idb.deleteDB(this._cacheName);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* @param url
|
|
67
|
-
* @param timestamp
|
|
68
|
-
*
|
|
69
|
-
* @private
|
|
70
|
-
*/ async setTimestamp(url, timestamp) {
|
|
71
|
-
url = normalizeURL(url);
|
|
72
|
-
const entry = {
|
|
73
|
-
url,
|
|
74
|
-
timestamp,
|
|
75
|
-
cacheName: this._cacheName,
|
|
76
|
-
// Creating an ID from the URL and cache name won't be necessary once
|
|
77
|
-
// Edge switches to Chromium and all browsers we support work with
|
|
78
|
-
// array keyPaths.
|
|
79
|
-
id: this._getId(url)
|
|
80
|
-
};
|
|
81
|
-
const db = await this.getDb();
|
|
82
|
-
const tx = db.transaction(CACHE_OBJECT_STORE, "readwrite", {
|
|
83
|
-
durability: "relaxed"
|
|
84
|
-
});
|
|
85
|
-
await tx.store.put(entry);
|
|
86
|
-
await tx.done;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Returns the timestamp stored for a given URL.
|
|
90
|
-
*
|
|
91
|
-
* @param url
|
|
92
|
-
* @returns
|
|
93
|
-
* @private
|
|
94
|
-
*/ async getTimestamp(url) {
|
|
95
|
-
const db = await this.getDb();
|
|
96
|
-
const entry = await db.get(CACHE_OBJECT_STORE, this._getId(url));
|
|
97
|
-
return entry?.timestamp;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Iterates through all the entries in the object store (from newest to
|
|
101
|
-
* oldest) and removes entries once either `maxCount` is reached or the
|
|
102
|
-
* entry's timestamp is less than `minTimestamp`.
|
|
103
|
-
*
|
|
104
|
-
* @param minTimestamp
|
|
105
|
-
* @param maxCount
|
|
106
|
-
* @returns
|
|
107
|
-
* @private
|
|
108
|
-
*/ async expireEntries(minTimestamp, maxCount) {
|
|
109
|
-
const db = await this.getDb();
|
|
110
|
-
let cursor = await db.transaction(CACHE_OBJECT_STORE).store.index("timestamp").openCursor(null, "prev");
|
|
111
|
-
const entriesToDelete = [];
|
|
112
|
-
let entriesNotDeletedCount = 0;
|
|
113
|
-
while(cursor){
|
|
114
|
-
const result = cursor.value;
|
|
115
|
-
// TODO(philipwalton): once we can use a multi-key index, we
|
|
116
|
-
// won't have to check `cacheName` here.
|
|
117
|
-
if (result.cacheName === this._cacheName) {
|
|
118
|
-
// Delete an entry if it's older than the max age or
|
|
119
|
-
// if we already have the max number allowed.
|
|
120
|
-
if (minTimestamp && result.timestamp < minTimestamp || maxCount && entriesNotDeletedCount >= maxCount) {
|
|
121
|
-
// TODO(philipwalton): we should be able to delete the
|
|
122
|
-
// entry right here, but doing so causes an iteration
|
|
123
|
-
// bug in Safari stable (fixed in TP). Instead we can
|
|
124
|
-
// store the keys of the entries to delete, and then
|
|
125
|
-
// delete the separate transactions.
|
|
126
|
-
// https://github.com/GoogleChrome/workbox/issues/1978
|
|
127
|
-
// cursor.delete();
|
|
128
|
-
// We only need to return the URL, not the whole entry.
|
|
129
|
-
entriesToDelete.push(cursor.value);
|
|
130
|
-
} else {
|
|
131
|
-
entriesNotDeletedCount++;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
cursor = await cursor.continue();
|
|
135
|
-
}
|
|
136
|
-
// TODO(philipwalton): once the Safari bug in the following issue is fixed,
|
|
137
|
-
// we should be able to remove this loop and do the entry deletion in the
|
|
138
|
-
// cursor loop above:
|
|
139
|
-
// https://github.com/GoogleChrome/workbox/issues/1978
|
|
140
|
-
const urlsDeleted = [];
|
|
141
|
-
for (const entry of entriesToDelete){
|
|
142
|
-
await db.delete(CACHE_OBJECT_STORE, entry.id);
|
|
143
|
-
urlsDeleted.push(entry.url);
|
|
144
|
-
}
|
|
145
|
-
return urlsDeleted;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Takes a URL and returns an ID that will be unique in the object store.
|
|
149
|
-
*
|
|
150
|
-
* @param url
|
|
151
|
-
* @returns
|
|
152
|
-
* @private
|
|
153
|
-
*/ _getId(url) {
|
|
154
|
-
// Creating an ID from the URL and cache name won't be necessary once
|
|
155
|
-
// Edge switches to Chromium and all browsers we support work with
|
|
156
|
-
// array keyPaths.
|
|
157
|
-
return `${this._cacheName}|${normalizeURL(url)}`;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Returns an open connection to the database.
|
|
161
|
-
*
|
|
162
|
-
* @private
|
|
163
|
-
*/ async getDb() {
|
|
164
|
-
if (!this._db) {
|
|
165
|
-
this._db = await idb.openDB(DB_NAME, 1, {
|
|
166
|
-
upgrade: this._upgradeDbAndDeleteOldDbs.bind(this)
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
return this._db;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* The `CacheExpiration` class allows you define an expiration and / or
|
|
175
|
-
* limit on the number of responses stored in a
|
|
176
|
-
* [`Cache`](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
|
|
177
|
-
*/ class CacheExpiration {
|
|
178
|
-
_isRunning = false;
|
|
179
|
-
_rerunRequested = false;
|
|
180
|
-
_maxEntries;
|
|
181
|
-
_maxAgeSeconds;
|
|
182
|
-
_matchOptions;
|
|
183
|
-
_cacheName;
|
|
184
|
-
_timestampModel;
|
|
185
|
-
/**
|
|
186
|
-
* To construct a new CacheExpiration instance you must provide at least
|
|
187
|
-
* one of the `config` properties.
|
|
188
|
-
*
|
|
189
|
-
* @param cacheName Name of the cache to apply restrictions to.
|
|
190
|
-
* @param config
|
|
191
|
-
*/ constructor(cacheName, config = {}){
|
|
192
|
-
if (process.env.NODE_ENV !== "production") {
|
|
193
|
-
internal.assert.isType(cacheName, "string", {
|
|
194
|
-
moduleName: "@serwist/expiration",
|
|
195
|
-
className: "CacheExpiration",
|
|
196
|
-
funcName: "constructor",
|
|
197
|
-
paramName: "cacheName"
|
|
198
|
-
});
|
|
199
|
-
if (!(config.maxEntries || config.maxAgeSeconds)) {
|
|
200
|
-
throw new internal.SerwistError("max-entries-or-age-required", {
|
|
201
|
-
moduleName: "@serwist/expiration",
|
|
202
|
-
className: "CacheExpiration",
|
|
203
|
-
funcName: "constructor"
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
if (config.maxEntries) {
|
|
207
|
-
internal.assert.isType(config.maxEntries, "number", {
|
|
208
|
-
moduleName: "@serwist/expiration",
|
|
209
|
-
className: "CacheExpiration",
|
|
210
|
-
funcName: "constructor",
|
|
211
|
-
paramName: "config.maxEntries"
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
if (config.maxAgeSeconds) {
|
|
215
|
-
internal.assert.isType(config.maxAgeSeconds, "number", {
|
|
216
|
-
moduleName: "@serwist/expiration",
|
|
217
|
-
className: "CacheExpiration",
|
|
218
|
-
funcName: "constructor",
|
|
219
|
-
paramName: "config.maxAgeSeconds"
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
this._maxEntries = config.maxEntries;
|
|
224
|
-
this._maxAgeSeconds = config.maxAgeSeconds;
|
|
225
|
-
this._matchOptions = config.matchOptions;
|
|
226
|
-
this._cacheName = cacheName;
|
|
227
|
-
this._timestampModel = new CacheTimestampsModel(cacheName);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Expires entries for the given cache and given criteria.
|
|
231
|
-
*/ async expireEntries() {
|
|
232
|
-
if (this._isRunning) {
|
|
233
|
-
this._rerunRequested = true;
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
this._isRunning = true;
|
|
237
|
-
const minTimestamp = this._maxAgeSeconds ? Date.now() - this._maxAgeSeconds * 1000 : 0;
|
|
238
|
-
const urlsExpired = await this._timestampModel.expireEntries(minTimestamp, this._maxEntries);
|
|
239
|
-
// Delete URLs from the cache
|
|
240
|
-
const cache = await self.caches.open(this._cacheName);
|
|
241
|
-
for (const url of urlsExpired){
|
|
242
|
-
await cache.delete(url, this._matchOptions);
|
|
243
|
-
}
|
|
244
|
-
if (process.env.NODE_ENV !== "production") {
|
|
245
|
-
if (urlsExpired.length > 0) {
|
|
246
|
-
internal.logger.groupCollapsed(`Expired ${urlsExpired.length} ` + `${urlsExpired.length === 1 ? "entry" : "entries"} and removed ` + `${urlsExpired.length === 1 ? "it" : "them"} from the ` + `'${this._cacheName}' cache.`);
|
|
247
|
-
internal.logger.log(`Expired the following ${urlsExpired.length === 1 ? "URL" : "URLs"}:`);
|
|
248
|
-
for (const url of urlsExpired){
|
|
249
|
-
internal.logger.log(` ${url}`);
|
|
250
|
-
}
|
|
251
|
-
internal.logger.groupEnd();
|
|
252
|
-
} else {
|
|
253
|
-
internal.logger.debug("Cache expiration ran and found no entries to remove.");
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
this._isRunning = false;
|
|
257
|
-
if (this._rerunRequested) {
|
|
258
|
-
this._rerunRequested = false;
|
|
259
|
-
internal.dontWaitFor(this.expireEntries());
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Update the timestamp for the given URL. This ensures the when
|
|
264
|
-
* removing entries based on maximum entries, most recently used
|
|
265
|
-
* is accurate or when expiring, the timestamp is up-to-date.
|
|
266
|
-
*
|
|
267
|
-
* @param url
|
|
268
|
-
*/ async updateTimestamp(url) {
|
|
269
|
-
if (process.env.NODE_ENV !== "production") {
|
|
270
|
-
internal.assert.isType(url, "string", {
|
|
271
|
-
moduleName: "@serwist/expiration",
|
|
272
|
-
className: "CacheExpiration",
|
|
273
|
-
funcName: "updateTimestamp",
|
|
274
|
-
paramName: "url"
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
await this._timestampModel.setTimestamp(url, Date.now());
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Can be used to check if a URL has expired or not before it's used.
|
|
281
|
-
*
|
|
282
|
-
* This requires a look up from IndexedDB, so can be slow.
|
|
283
|
-
*
|
|
284
|
-
* Note: This method will not remove the cached entry, call
|
|
285
|
-
* `expireEntries()` to remove indexedDB and Cache entries.
|
|
286
|
-
*
|
|
287
|
-
* @param url
|
|
288
|
-
* @returns
|
|
289
|
-
*/ async isURLExpired(url) {
|
|
290
|
-
if (!this._maxAgeSeconds) {
|
|
291
|
-
if (process.env.NODE_ENV !== "production") {
|
|
292
|
-
throw new internal.SerwistError("expired-test-without-max-age", {
|
|
293
|
-
methodName: "isURLExpired",
|
|
294
|
-
paramName: "maxAgeSeconds"
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
const timestamp = await this._timestampModel.getTimestamp(url);
|
|
300
|
-
const expireOlderThan = Date.now() - this._maxAgeSeconds * 1000;
|
|
301
|
-
return timestamp !== undefined ? timestamp < expireOlderThan : true;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Removes the IndexedDB object store used to keep track of cache expiration
|
|
305
|
-
* metadata.
|
|
306
|
-
*/ async delete() {
|
|
307
|
-
// Make sure we don't attempt another rerun if we're called in the middle of
|
|
308
|
-
// a cache expiration.
|
|
309
|
-
this._rerunRequested = false;
|
|
310
|
-
await this._timestampModel.expireEntries(Infinity); // Expires all.
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* This plugin can be used in a `@serwist/strategies` Strategy to regularly enforce a
|
|
316
|
-
* limit on the age and / or the number of cached requests.
|
|
317
|
-
*
|
|
318
|
-
* It can only be used with Strategy instances that have a
|
|
319
|
-
* [custom `cacheName` property set](/web/tools/workbox/guides/configure-workbox#custom_cache_names_in_strategies).
|
|
320
|
-
* In other words, it can't be used to expire entries in strategy that uses the
|
|
321
|
-
* default runtime cache name.
|
|
322
|
-
*
|
|
323
|
-
* Whenever a cached response is used or updated, this plugin will look
|
|
324
|
-
* at the associated cache and remove any old or extra responses.
|
|
325
|
-
*
|
|
326
|
-
* When using `maxAgeSeconds`, responses may be used *once* after expiring
|
|
327
|
-
* because the expiration clean up will not have occurred until *after* the
|
|
328
|
-
* cached response has been used. If the response has a "Date" header, then
|
|
329
|
-
* a light weight expiration check is performed and the response will not be
|
|
330
|
-
* used immediately.
|
|
331
|
-
*
|
|
332
|
-
* When using `maxEntries`, the entry least-recently requested will be removed
|
|
333
|
-
* from the cache first.
|
|
334
|
-
*/ class ExpirationPlugin {
|
|
335
|
-
_config;
|
|
336
|
-
_maxAgeSeconds;
|
|
337
|
-
_cacheExpirations;
|
|
338
|
-
/**
|
|
339
|
-
* @param config
|
|
340
|
-
*/ constructor(config = {}){
|
|
341
|
-
if (process.env.NODE_ENV !== "production") {
|
|
342
|
-
if (!(config.maxEntries || config.maxAgeSeconds)) {
|
|
343
|
-
throw new internal.SerwistError("max-entries-or-age-required", {
|
|
344
|
-
moduleName: "@serwist/expiration",
|
|
345
|
-
className: "Plugin",
|
|
346
|
-
funcName: "constructor"
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
if (config.maxEntries) {
|
|
350
|
-
internal.assert.isType(config.maxEntries, "number", {
|
|
351
|
-
moduleName: "@serwist/expiration",
|
|
352
|
-
className: "Plugin",
|
|
353
|
-
funcName: "constructor",
|
|
354
|
-
paramName: "config.maxEntries"
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
if (config.maxAgeSeconds) {
|
|
358
|
-
internal.assert.isType(config.maxAgeSeconds, "number", {
|
|
359
|
-
moduleName: "@serwist/expiration",
|
|
360
|
-
className: "Plugin",
|
|
361
|
-
funcName: "constructor",
|
|
362
|
-
paramName: "config.maxAgeSeconds"
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
this._config = config;
|
|
367
|
-
this._maxAgeSeconds = config.maxAgeSeconds;
|
|
368
|
-
this._cacheExpirations = new Map();
|
|
369
|
-
if (config.purgeOnQuotaError) {
|
|
370
|
-
core.registerQuotaErrorCallback(()=>this.deleteCacheAndMetadata());
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* A simple helper method to return a CacheExpiration instance for a given
|
|
375
|
-
* cache name.
|
|
376
|
-
*
|
|
377
|
-
* @param cacheName
|
|
378
|
-
* @returns
|
|
379
|
-
* @private
|
|
380
|
-
*/ _getCacheExpiration(cacheName) {
|
|
381
|
-
if (cacheName === internal.privateCacheNames.getRuntimeName()) {
|
|
382
|
-
throw new internal.SerwistError("expire-custom-caches-only");
|
|
383
|
-
}
|
|
384
|
-
let cacheExpiration = this._cacheExpirations.get(cacheName);
|
|
385
|
-
if (!cacheExpiration) {
|
|
386
|
-
cacheExpiration = new CacheExpiration(cacheName, this._config);
|
|
387
|
-
this._cacheExpirations.set(cacheName, cacheExpiration);
|
|
388
|
-
}
|
|
389
|
-
return cacheExpiration;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* A "lifecycle" callback that will be triggered automatically by the
|
|
393
|
-
* `@serwist/strategies` handlers when a `Response` is about to be returned
|
|
394
|
-
* from a [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to
|
|
395
|
-
* the handler. It allows the `Response` to be inspected for freshness and
|
|
396
|
-
* prevents it from being used if the `Response`'s `Date` header value is
|
|
397
|
-
* older than the configured `maxAgeSeconds`.
|
|
398
|
-
*
|
|
399
|
-
* @param options
|
|
400
|
-
* @returns Either the `cachedResponse`, if it's fresh, or `null` if the `Response`
|
|
401
|
-
* is older than `maxAgeSeconds`.
|
|
402
|
-
* @private
|
|
403
|
-
*/ cachedResponseWillBeUsed = async ({ event, request, cacheName, cachedResponse })=>{
|
|
404
|
-
if (!cachedResponse) {
|
|
405
|
-
return null;
|
|
406
|
-
}
|
|
407
|
-
const isFresh = this._isResponseDateFresh(cachedResponse);
|
|
408
|
-
// Expire entries to ensure that even if the expiration date has
|
|
409
|
-
// expired, it'll only be used once.
|
|
410
|
-
const cacheExpiration = this._getCacheExpiration(cacheName);
|
|
411
|
-
internal.dontWaitFor(cacheExpiration.expireEntries());
|
|
412
|
-
// Update the metadata for the request URL to the current timestamp,
|
|
413
|
-
// but don't `await` it as we don't want to block the response.
|
|
414
|
-
const updateTimestampDone = cacheExpiration.updateTimestamp(request.url);
|
|
415
|
-
if (event) {
|
|
416
|
-
try {
|
|
417
|
-
event.waitUntil(updateTimestampDone);
|
|
418
|
-
} catch (error) {
|
|
419
|
-
if (process.env.NODE_ENV !== "production") {
|
|
420
|
-
// The event may not be a fetch event; only log the URL if it is.
|
|
421
|
-
if ("request" in event) {
|
|
422
|
-
internal.logger.warn(`Unable to ensure service worker stays alive when updating cache entry for '${internal.getFriendlyURL(event.request.url)}'.`);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
return isFresh ? cachedResponse : null;
|
|
428
|
-
};
|
|
429
|
-
/**
|
|
430
|
-
* @param cachedResponse
|
|
431
|
-
* @returns
|
|
432
|
-
* @private
|
|
433
|
-
*/ _isResponseDateFresh(cachedResponse) {
|
|
434
|
-
if (!this._maxAgeSeconds) {
|
|
435
|
-
// We aren't expiring by age, so return true, it's fresh
|
|
436
|
-
return true;
|
|
437
|
-
}
|
|
438
|
-
// Check if the 'date' header will suffice a quick expiration check.
|
|
439
|
-
// See https://github.com/GoogleChromeLabs/sw-toolbox/issues/164 for
|
|
440
|
-
// discussion.
|
|
441
|
-
const dateHeaderTimestamp = this._getDateHeaderTimestamp(cachedResponse);
|
|
442
|
-
if (dateHeaderTimestamp === null) {
|
|
443
|
-
// Unable to parse date, so assume it's fresh.
|
|
444
|
-
return true;
|
|
445
|
-
}
|
|
446
|
-
// If we have a valid headerTime, then our response is fresh iff the
|
|
447
|
-
// headerTime plus maxAgeSeconds is greater than the current time.
|
|
448
|
-
const now = Date.now();
|
|
449
|
-
return dateHeaderTimestamp >= now - this._maxAgeSeconds * 1000;
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* This method will extract the data header and parse it into a useful
|
|
453
|
-
* value.
|
|
454
|
-
*
|
|
455
|
-
* @param cachedResponse
|
|
456
|
-
* @returns
|
|
457
|
-
* @private
|
|
458
|
-
*/ _getDateHeaderTimestamp(cachedResponse) {
|
|
459
|
-
if (!cachedResponse.headers.has("date")) {
|
|
460
|
-
return null;
|
|
461
|
-
}
|
|
462
|
-
const dateHeader = cachedResponse.headers.get("date");
|
|
463
|
-
const parsedDate = new Date(dateHeader);
|
|
464
|
-
const headerTime = parsedDate.getTime();
|
|
465
|
-
// If the Date header was invalid for some reason, parsedDate.getTime()
|
|
466
|
-
// will return NaN.
|
|
467
|
-
if (Number.isNaN(headerTime)) {
|
|
468
|
-
return null;
|
|
469
|
-
}
|
|
470
|
-
return headerTime;
|
|
471
|
-
}
|
|
472
|
-
/**
|
|
473
|
-
* A "lifecycle" callback that will be triggered automatically by the
|
|
474
|
-
* `@serwist/strategies` handlers when an entry is added to a cache.
|
|
475
|
-
*
|
|
476
|
-
* @param options
|
|
477
|
-
* @private
|
|
478
|
-
*/ cacheDidUpdate = async ({ cacheName, request })=>{
|
|
479
|
-
if (process.env.NODE_ENV !== "production") {
|
|
480
|
-
internal.assert.isType(cacheName, "string", {
|
|
481
|
-
moduleName: "@serwist/expiration",
|
|
482
|
-
className: "Plugin",
|
|
483
|
-
funcName: "cacheDidUpdate",
|
|
484
|
-
paramName: "cacheName"
|
|
485
|
-
});
|
|
486
|
-
internal.assert.isInstance(request, Request, {
|
|
487
|
-
moduleName: "@serwist/expiration",
|
|
488
|
-
className: "Plugin",
|
|
489
|
-
funcName: "cacheDidUpdate",
|
|
490
|
-
paramName: "request"
|
|
491
|
-
});
|
|
492
|
-
}
|
|
493
|
-
const cacheExpiration = this._getCacheExpiration(cacheName);
|
|
494
|
-
await cacheExpiration.updateTimestamp(request.url);
|
|
495
|
-
await cacheExpiration.expireEntries();
|
|
496
|
-
};
|
|
497
|
-
/**
|
|
498
|
-
* This is a helper method that performs two operations:
|
|
499
|
-
*
|
|
500
|
-
* - Deletes *all* the underlying Cache instances associated with this plugin
|
|
501
|
-
* instance, by calling caches.delete() on your behalf.
|
|
502
|
-
* - Deletes the metadata from IndexedDB used to keep track of expiration
|
|
503
|
-
* details for each Cache instance.
|
|
504
|
-
*
|
|
505
|
-
* When using cache expiration, calling this method is preferable to calling
|
|
506
|
-
* `caches.delete()` directly, since this will ensure that the IndexedDB
|
|
507
|
-
* metadata is also cleanly removed and open IndexedDB instances are deleted.
|
|
508
|
-
*
|
|
509
|
-
* Note that if you're *not* using cache expiration for a given cache, calling
|
|
510
|
-
* `caches.delete()` and passing in the cache's name should be sufficient.
|
|
511
|
-
* There is no Serwist-specific method needed for cleanup in that case.
|
|
512
|
-
*/ async deleteCacheAndMetadata() {
|
|
513
|
-
// Do this one at a time instead of all at once via `Promise.all()` to
|
|
514
|
-
// reduce the chance of inconsistency if a promise rejects.
|
|
515
|
-
for (const [cacheName, cacheExpiration] of this._cacheExpirations){
|
|
516
|
-
await self.caches.delete(cacheName);
|
|
517
|
-
await cacheExpiration.delete();
|
|
518
|
-
}
|
|
519
|
-
// Reset this._cacheExpirations to its initial state.
|
|
520
|
-
this._cacheExpirations = new Map();
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
exports.CacheExpiration = CacheExpiration;
|
|
525
|
-
exports.ExpirationPlugin = ExpirationPlugin;
|