@serwist/expiration 8.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/CacheExpiration.d.ts +69 -0
- package/dist/CacheExpiration.d.ts.map +1 -0
- package/dist/ExpirationPlugin.d.ts +116 -0
- package/dist/ExpirationPlugin.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +521 -0
- package/dist/index.old.cjs +524 -0
- package/dist/models/CacheTimestampsModel.d.ts +74 -0
- package/dist/models/CacheTimestampsModel.d.ts.map +1 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-expiration
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;IAwCpC;;;;;;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;IAgBjD;;;OAGG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;CAM9B;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,CAiCjE;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"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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 };
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|