swimple 0.6.0 → 0.8.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/helpers.js +1 -2
- package/index.d.ts +239 -10
- package/index.js +1 -2
- package/package.json +4 -3
- package/types.js +30 -0
- package/headers.d.ts +0 -42
- package/helpers.d.ts +0 -134
- package/types.d.ts +0 -41
package/helpers.js
CHANGED
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
/// <reference no-default-lib="true"/>
|
|
6
6
|
/// <reference lib="esnext" />
|
|
7
7
|
/// <reference lib="webworker" />
|
|
8
|
-
|
|
9
|
-
/** @import { CacheStrategy, HandleRequestConfig, LoggingLevel } from "./types" */
|
|
8
|
+
/// <reference path="./types.js" />
|
|
10
9
|
|
|
11
10
|
import {
|
|
12
11
|
CACHE_TIMESTAMP_HEADER,
|
package/index.d.ts
CHANGED
|
@@ -1,13 +1,242 @@
|
|
|
1
1
|
/// <reference no-default-lib="true"/>
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @param {HandleRequestConfig} config - Configuration options including cache name, scope, default strategy, and TTL settings
|
|
9
|
-
* @returns {(event: FetchEvent) => Promise<Response> | null} Request handler function that can be used in service worker fetch event listeners
|
|
3
|
+
* Caching strategy for handling requests.
|
|
4
|
+
* - `cache-first`: Return from cache if fresh (within TTL), otherwise fetch from network immediately. Stale cache is only used when offline (network request fails). No background updates.
|
|
5
|
+
* - `network-first`: Try network first, fall back to stale cache if offline (within stale TTL). Cache updated when network succeeds.
|
|
6
|
+
* - `stale-while-revalidate`: Return from cache immediately (fresh = no update, stale = update in background). Fetch from network if too stale or missing.
|
|
10
7
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
type CacheStrategy = "cache-first" | "network-first" | "stale-while-revalidate";
|
|
9
|
+
/**
|
|
10
|
+
* Logging level for cache operations.
|
|
11
|
+
* - `none`: No logging
|
|
12
|
+
* - `minimal`: Logs cache hits and cache invalidation only
|
|
13
|
+
* - `verbose`: Logs all events including cache misses, cleanup, and header usage
|
|
14
|
+
*/
|
|
15
|
+
type LoggingLevel = "none" | "minimal" | "verbose";
|
|
16
|
+
type HandleRequestConfig = {
|
|
17
|
+
/**
|
|
18
|
+
* - Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries.
|
|
19
|
+
*/
|
|
20
|
+
cacheName: string;
|
|
21
|
+
/**
|
|
22
|
+
* - URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. Note: Cross-origin requests are never cached, regardless of scope or TTL headers.
|
|
23
|
+
*/
|
|
24
|
+
scope?: string[];
|
|
25
|
+
/**
|
|
26
|
+
* - Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`.
|
|
27
|
+
*/
|
|
28
|
+
defaultStrategy?: CacheStrategy;
|
|
29
|
+
/**
|
|
30
|
+
* - Maximum age for fresh content. Fresh content will be returned from cache for cache-first and stale-while-revalidate strategies, and also from network-first when offline. Fresh content does not get updated from the network. Since this defaults to `300`, caching is automatic by default for GET requests matching the scope. Set to `0` or `undefined` to disable automatic caching (individual requests can still enable caching with `X-SW-Cache-TTL-Seconds` header).
|
|
31
|
+
*/
|
|
32
|
+
defaultTTLSeconds?: number;
|
|
33
|
+
/**
|
|
34
|
+
* - Maximum age for stale content. Stale content will be returned from cache for cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies. That means responses past the fresh TTL but within stale TTL can still be returned from cache. Stale content does get updated from the network.
|
|
35
|
+
*/
|
|
36
|
+
defaultStaleTTLSeconds?: number;
|
|
37
|
+
/**
|
|
38
|
+
* - Automatically invalidate cache on POST/PATCH/PUT/DELETE requests.
|
|
39
|
+
*/
|
|
40
|
+
inferInvalidation?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* - Custom fetch function to use for network requests. Receives a `Request` object and must return a `Promise<Response>`. Useful for handling authentication errors (401/403) or adding custom headers to all requests.
|
|
43
|
+
*/
|
|
44
|
+
customFetch?: typeof fetch;
|
|
45
|
+
/**
|
|
46
|
+
* - Maximum age (in seconds) before cache entries are automatically cleaned up. Entries older than this age are deleted. Defaults to 7200 seconds (2 hours, which is 2x the default stale TTL). Cache entries are cleaned up reactively (when accessed) and periodically (every 100 fetches).
|
|
47
|
+
*/
|
|
48
|
+
maxCacheAgeSeconds?: number;
|
|
49
|
+
/**
|
|
50
|
+
* - Logging level: "none" (no logging), "minimal" (cache hits and invalidation only), or "verbose" (all logging including misses, cleanup, and headers). Defaults to "none".
|
|
51
|
+
*/
|
|
52
|
+
loggingLevel?: LoggingLevel;
|
|
53
|
+
};
|
|
54
|
+
declare module "headers" {
|
|
55
|
+
/**
|
|
56
|
+
* Header constants used by swimple for cache control.
|
|
57
|
+
* These constants can be imported in non-service-worker code to set headers on requests.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* // Node.js / npm import
|
|
61
|
+
* import { CACHE_STRATEGY_HEADER, CACHE_TTL_HEADER } from "swimple/headers";
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // CDN import
|
|
65
|
+
* import { CACHE_STRATEGY_HEADER, CACHE_TTL_HEADER } from "https://cdn.jsdelivr.net/npm/swimple@1.0.0/headers.js";
|
|
66
|
+
*/
|
|
67
|
+
/**
|
|
68
|
+
* Header name for overriding the caching strategy for a specific request.
|
|
69
|
+
* Values: "cache-first", "network-first", or "stale-while-revalidate"
|
|
70
|
+
*/
|
|
71
|
+
export const CACHE_STRATEGY_HEADER: "X-SW-Cache-Strategy";
|
|
72
|
+
/**
|
|
73
|
+
* Header name for setting the time-to-live (in seconds) for a cached response.
|
|
74
|
+
* Set to "0" to completely opt out of caching for a specific request.
|
|
75
|
+
*/
|
|
76
|
+
export const CACHE_TTL_HEADER: "X-SW-Cache-TTL-Seconds";
|
|
77
|
+
/**
|
|
78
|
+
* Header name for setting the stale time-to-live (in seconds) for a cached response.
|
|
79
|
+
* Used by cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies.
|
|
80
|
+
*/
|
|
81
|
+
export const CACHE_STALE_TTL_HEADER: "X-SW-Cache-Stale-TTL-Seconds";
|
|
82
|
+
/**
|
|
83
|
+
* Header name for explicitly invalidating specific cache entries.
|
|
84
|
+
* Can be set multiple times to invalidate multiple paths.
|
|
85
|
+
*/
|
|
86
|
+
export const CACHE_INVALIDATE_HEADER: "X-SW-Cache-Invalidate";
|
|
87
|
+
/**
|
|
88
|
+
* Header name for clearing the entire cache.
|
|
89
|
+
* Any value works - the header's presence triggers cache clearing.
|
|
90
|
+
*/
|
|
91
|
+
export const CACHE_CLEAR_HEADER: "X-SW-Cache-Clear";
|
|
92
|
+
/**
|
|
93
|
+
* Internal header name used to store the cache timestamp in cached responses.
|
|
94
|
+
* This header is set automatically by the library and should not be set manually.
|
|
95
|
+
*/
|
|
96
|
+
export const CACHE_TIMESTAMP_HEADER: "x-sw-cache-timestamp";
|
|
97
|
+
}
|
|
98
|
+
declare module "helpers" {
|
|
99
|
+
/**
|
|
100
|
+
* Set the logging level
|
|
101
|
+
* @param {LoggingLevel} level - Logging level: "none", "minimal", or "verbose"
|
|
102
|
+
*/
|
|
103
|
+
export function setLoggingLevel(level: LoggingLevel): void;
|
|
104
|
+
/**
|
|
105
|
+
* Get header value (case-insensitive)
|
|
106
|
+
* @param {Headers} headers
|
|
107
|
+
* @param {string} name
|
|
108
|
+
* @returns {string | null}
|
|
109
|
+
*/
|
|
110
|
+
export function getHeader(headers: Headers, name: string): string | null;
|
|
111
|
+
/**
|
|
112
|
+
* Get all header values for a given header name (case-insensitive)
|
|
113
|
+
* Useful when a header has been set multiple times (e.g., multiple X-SW-Cache-Invalidate headers)
|
|
114
|
+
* When headers are set multiple times with append(), they are concatenated with ", " (comma+space)
|
|
115
|
+
* This function splits them back into individual values
|
|
116
|
+
* @param {Headers} headers
|
|
117
|
+
* @param {string} name - Header name to look up
|
|
118
|
+
* @returns {string[]} Array of all header values for the given name
|
|
119
|
+
*/
|
|
120
|
+
export function getAllHeaders(headers: Headers, name: string): string[];
|
|
121
|
+
/**
|
|
122
|
+
* Get cache timestamp from response
|
|
123
|
+
* @param {Response} response
|
|
124
|
+
* @returns {number | null} Timestamp in milliseconds since epoch, or null if not found
|
|
125
|
+
*/
|
|
126
|
+
export function getCacheTimestamp(response: Response): number | null;
|
|
127
|
+
/**
|
|
128
|
+
* Check if response is fresh
|
|
129
|
+
* @param {Response} response
|
|
130
|
+
* @param {number} ttl - Time-to-live in seconds
|
|
131
|
+
* @returns {boolean}
|
|
132
|
+
*/
|
|
133
|
+
export function isFresh(response: Response, ttl: number): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Check if response is stale (but usable)
|
|
136
|
+
* @param {Response} response
|
|
137
|
+
* @param {number} ttl - Time-to-live in seconds
|
|
138
|
+
* @param {number | null} staleTTL - Stale time-to-live in seconds
|
|
139
|
+
* @returns {boolean}
|
|
140
|
+
*/
|
|
141
|
+
export function isStale(response: Response, ttl: number, staleTTL: number | null): boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Add timestamp to response
|
|
144
|
+
* @param {Response} response
|
|
145
|
+
* @returns {Response}
|
|
146
|
+
*/
|
|
147
|
+
export function addTimestamp(response: Response): Response;
|
|
148
|
+
/**
|
|
149
|
+
* Get inferred invalidation paths
|
|
150
|
+
* @param {string} url
|
|
151
|
+
* @returns {string[]}
|
|
152
|
+
*/
|
|
153
|
+
export function getInferredInvalidationPaths(url: string): string[];
|
|
154
|
+
/**
|
|
155
|
+
* Get strategy from request headers or use default
|
|
156
|
+
* @param {Headers} headers
|
|
157
|
+
* @param {CacheStrategy} defaultStrategy
|
|
158
|
+
* @param {string} url - Request URL for logging
|
|
159
|
+
* @returns {CacheStrategy}
|
|
160
|
+
*/
|
|
161
|
+
export function getStrategy(headers: Headers, defaultStrategy: CacheStrategy, url?: string): CacheStrategy;
|
|
162
|
+
/**
|
|
163
|
+
* Get TTL from request headers or use default
|
|
164
|
+
* @param {Headers} headers
|
|
165
|
+
* @param {number} defaultTTL - Default TTL in seconds
|
|
166
|
+
* @param {string} url - Request URL for logging
|
|
167
|
+
* @returns {number | null} TTL in seconds, or null if caching is disabled
|
|
168
|
+
*/
|
|
169
|
+
export function getTTL(headers: Headers, defaultTTL: number, url?: string): number | null;
|
|
170
|
+
/**
|
|
171
|
+
* Get stale TTL from request headers or use default
|
|
172
|
+
* @param {Headers} headers
|
|
173
|
+
* @param {number} defaultStaleTTL - Default stale TTL in seconds
|
|
174
|
+
* @param {string} url - Request URL for logging
|
|
175
|
+
* @returns {number | null} Stale TTL in seconds, or null if stale caching is disabled
|
|
176
|
+
*/
|
|
177
|
+
export function getStaleTTL(headers: Headers, defaultStaleTTL: number, url?: string): number | null;
|
|
178
|
+
/**
|
|
179
|
+
* Check if URL matches scope. Returns true if scope array is empty or if the URL pathname starts with any of the scope prefixes.
|
|
180
|
+
* @param {string} url
|
|
181
|
+
* @param {string[]} scope
|
|
182
|
+
* @param {number} defaultTTLSeconds
|
|
183
|
+
* @returns {boolean}
|
|
184
|
+
*/
|
|
185
|
+
export function matchesScope(url: string, scope: string[], defaultTTLSeconds: number): boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Invalidate cache entries
|
|
188
|
+
* @param {string} cacheName
|
|
189
|
+
* @param {string[]} urls
|
|
190
|
+
* @returns {Promise<void>}
|
|
191
|
+
*/
|
|
192
|
+
export function invalidateCache(cacheName: string, urls: string[]): Promise<void>;
|
|
193
|
+
/**
|
|
194
|
+
* Clear entire cache
|
|
195
|
+
* @param {string} cacheName
|
|
196
|
+
* @returns {Promise<void>}
|
|
197
|
+
*/
|
|
198
|
+
export function clearCache(cacheName: string): Promise<void>;
|
|
199
|
+
/**
|
|
200
|
+
* Check if a cached response is older than the maximum age
|
|
201
|
+
* @param {Response} response
|
|
202
|
+
* @param {number} maxAgeSeconds - Maximum age in seconds
|
|
203
|
+
* @returns {boolean}
|
|
204
|
+
*/
|
|
205
|
+
export function isOlderThanMaxAge(response: Response, maxAgeSeconds: number): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Clean up cache entries older than maxAgeSeconds
|
|
208
|
+
* @param {string} cacheName
|
|
209
|
+
* @param {number} maxAgeSeconds - Maximum age in seconds
|
|
210
|
+
* @returns {Promise<void>}
|
|
211
|
+
*/
|
|
212
|
+
export function cleanupOldCacheEntries(cacheName: string, maxAgeSeconds: number): Promise<void>;
|
|
213
|
+
/**
|
|
214
|
+
* Log an informational message (minimal and verbose levels)
|
|
215
|
+
* @param {string} message - Message to log
|
|
216
|
+
*/
|
|
217
|
+
export function logInfo(message: string): void;
|
|
218
|
+
/**
|
|
219
|
+
* Log a verbose message (verbose level only)
|
|
220
|
+
* @param {string} message - Message to log
|
|
221
|
+
*/
|
|
222
|
+
export function logVerbose(message: string): void;
|
|
223
|
+
/**
|
|
224
|
+
* Validate configuration object
|
|
225
|
+
* @param {HandleRequestConfig} config - Configuration object to validate
|
|
226
|
+
* @throws {Error} If config is invalid
|
|
227
|
+
*/
|
|
228
|
+
export function validateConfig(config: HandleRequestConfig): void;
|
|
229
|
+
}
|
|
230
|
+
declare module "index" {
|
|
231
|
+
/**
|
|
232
|
+
* Creates a request handler function for service worker fetch events.
|
|
233
|
+
* The handler implements HTTP caching with configurable strategies (cache-first, network-first, stale-while-revalidate),
|
|
234
|
+
* automatic cache invalidation for mutations, and periodic cache cleanup. Only handles same-origin GET requests
|
|
235
|
+
* that match the configured scope.
|
|
236
|
+
*
|
|
237
|
+
* @param {HandleRequestConfig} config - Configuration options including cache name, scope, default strategy, and TTL settings
|
|
238
|
+
* @returns {(event: FetchEvent) => Promise<Response> | null} Request handler function that can be used in service worker fetch event listeners
|
|
239
|
+
*/
|
|
240
|
+
export function createHandleRequest(config: HandleRequestConfig): (event: FetchEvent) => Promise<Response> | null;
|
|
241
|
+
export { cleanupOldCacheEntries } from "./helpers.js";
|
|
242
|
+
}
|
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swimple",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A simple service worker library for request caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
9
|
".": {
|
|
9
10
|
"types": "./index.d.ts",
|
|
10
11
|
"default": "./index.js"
|
|
11
12
|
},
|
|
12
13
|
"./headers": {
|
|
13
|
-
"types": "./
|
|
14
|
+
"types": "./index.d.ts",
|
|
14
15
|
"default": "./headers.js"
|
|
15
16
|
}
|
|
16
17
|
},
|
|
@@ -21,7 +22,7 @@
|
|
|
21
22
|
"test:ui": "playwright test",
|
|
22
23
|
"format:check": "prettier --check .",
|
|
23
24
|
"format:fix": "prettier --write .",
|
|
24
|
-
"prepublishOnly": "tsc
|
|
25
|
+
"prepublishOnly": "tsc index.js headers.js helpers.js --declaration --allowJs --emitDeclarationOnly --outfile index.d.ts --target esnext"
|
|
25
26
|
},
|
|
26
27
|
"keywords": [
|
|
27
28
|
"service-worker",
|
package/types.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Caching strategy for handling requests.
|
|
5
|
+
* - `cache-first`: Return from cache if fresh (within TTL), otherwise fetch from network immediately. Stale cache is only used when offline (network request fails). No background updates.
|
|
6
|
+
* - `network-first`: Try network first, fall back to stale cache if offline (within stale TTL). Cache updated when network succeeds.
|
|
7
|
+
* - `stale-while-revalidate`: Return from cache immediately (fresh = no update, stale = update in background). Fetch from network if too stale or missing.
|
|
8
|
+
* @typedef {"cache-first" | "network-first" | "stale-while-revalidate"} CacheStrategy
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Logging level for cache operations.
|
|
13
|
+
* - `none`: No logging
|
|
14
|
+
* - `minimal`: Logs cache hits and cache invalidation only
|
|
15
|
+
* - `verbose`: Logs all events including cache misses, cleanup, and header usage
|
|
16
|
+
* @typedef {"none" | "minimal" | "verbose"} LoggingLevel
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @typedef HandleRequestConfig
|
|
21
|
+
* @property {string} cacheName - Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries.
|
|
22
|
+
* @property {string[]} [scope] - URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. Note: Cross-origin requests are never cached, regardless of scope or TTL headers.
|
|
23
|
+
* @property {CacheStrategy} [defaultStrategy] - Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`.
|
|
24
|
+
* @property {number} [defaultTTLSeconds] - Maximum age for fresh content. Fresh content will be returned from cache for cache-first and stale-while-revalidate strategies, and also from network-first when offline. Fresh content does not get updated from the network. Since this defaults to `300`, caching is automatic by default for GET requests matching the scope. Set to `0` or `undefined` to disable automatic caching (individual requests can still enable caching with `X-SW-Cache-TTL-Seconds` header).
|
|
25
|
+
* @property {number} [defaultStaleTTLSeconds] - Maximum age for stale content. Stale content will be returned from cache for cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies. That means responses past the fresh TTL but within stale TTL can still be returned from cache. Stale content does get updated from the network.
|
|
26
|
+
* @property {boolean} [inferInvalidation] - Automatically invalidate cache on POST/PATCH/PUT/DELETE requests.
|
|
27
|
+
* @property {typeof fetch} [customFetch] - Custom fetch function to use for network requests. Receives a `Request` object and must return a `Promise<Response>`. Useful for handling authentication errors (401/403) or adding custom headers to all requests.
|
|
28
|
+
* @property {number} [maxCacheAgeSeconds] - Maximum age (in seconds) before cache entries are automatically cleaned up. Entries older than this age are deleted. Defaults to 7200 seconds (2 hours, which is 2x the default stale TTL). Cache entries are cleaned up reactively (when accessed) and periodically (every 100 fetches).
|
|
29
|
+
* @property {LoggingLevel} [loggingLevel] - Logging level: "none" (no logging), "minimal" (cache hits and invalidation only), or "verbose" (all logging including misses, cleanup, and headers). Defaults to "none".
|
|
30
|
+
*/
|
package/headers.d.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Header constants used by swimple for cache control.
|
|
3
|
-
* These constants can be imported in non-service-worker code to set headers on requests.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* // Node.js / npm import
|
|
7
|
-
* import { CACHE_STRATEGY_HEADER, CACHE_TTL_HEADER } from "swimple/headers";
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* // CDN import
|
|
11
|
-
* import { CACHE_STRATEGY_HEADER, CACHE_TTL_HEADER } from "https://cdn.jsdelivr.net/npm/swimple@1.0.0/headers.js";
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* Header name for overriding the caching strategy for a specific request.
|
|
15
|
-
* Values: "cache-first", "network-first", or "stale-while-revalidate"
|
|
16
|
-
*/
|
|
17
|
-
export const CACHE_STRATEGY_HEADER: "X-SW-Cache-Strategy";
|
|
18
|
-
/**
|
|
19
|
-
* Header name for setting the time-to-live (in seconds) for a cached response.
|
|
20
|
-
* Set to "0" to completely opt out of caching for a specific request.
|
|
21
|
-
*/
|
|
22
|
-
export const CACHE_TTL_HEADER: "X-SW-Cache-TTL-Seconds";
|
|
23
|
-
/**
|
|
24
|
-
* Header name for setting the stale time-to-live (in seconds) for a cached response.
|
|
25
|
-
* Used by cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies.
|
|
26
|
-
*/
|
|
27
|
-
export const CACHE_STALE_TTL_HEADER: "X-SW-Cache-Stale-TTL-Seconds";
|
|
28
|
-
/**
|
|
29
|
-
* Header name for explicitly invalidating specific cache entries.
|
|
30
|
-
* Can be set multiple times to invalidate multiple paths.
|
|
31
|
-
*/
|
|
32
|
-
export const CACHE_INVALIDATE_HEADER: "X-SW-Cache-Invalidate";
|
|
33
|
-
/**
|
|
34
|
-
* Header name for clearing the entire cache.
|
|
35
|
-
* Any value works - the header's presence triggers cache clearing.
|
|
36
|
-
*/
|
|
37
|
-
export const CACHE_CLEAR_HEADER: "X-SW-Cache-Clear";
|
|
38
|
-
/**
|
|
39
|
-
* Internal header name used to store the cache timestamp in cached responses.
|
|
40
|
-
* This header is set automatically by the library and should not be set manually.
|
|
41
|
-
*/
|
|
42
|
-
export const CACHE_TIMESTAMP_HEADER: "x-sw-cache-timestamp";
|
package/helpers.d.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/// <reference no-default-lib="true"/>
|
|
2
|
-
/**
|
|
3
|
-
* Set the logging level
|
|
4
|
-
* @param {LoggingLevel} level - Logging level: "none", "minimal", or "verbose"
|
|
5
|
-
*/
|
|
6
|
-
export function setLoggingLevel(level: LoggingLevel): void;
|
|
7
|
-
/**
|
|
8
|
-
* Get header value (case-insensitive)
|
|
9
|
-
* @param {Headers} headers
|
|
10
|
-
* @param {string} name
|
|
11
|
-
* @returns {string | null}
|
|
12
|
-
*/
|
|
13
|
-
export function getHeader(headers: Headers, name: string): string | null;
|
|
14
|
-
/**
|
|
15
|
-
* Get all header values for a given header name (case-insensitive)
|
|
16
|
-
* Useful when a header has been set multiple times (e.g., multiple X-SW-Cache-Invalidate headers)
|
|
17
|
-
* When headers are set multiple times with append(), they are concatenated with ", " (comma+space)
|
|
18
|
-
* This function splits them back into individual values
|
|
19
|
-
* @param {Headers} headers
|
|
20
|
-
* @param {string} name - Header name to look up
|
|
21
|
-
* @returns {string[]} Array of all header values for the given name
|
|
22
|
-
*/
|
|
23
|
-
export function getAllHeaders(headers: Headers, name: string): string[];
|
|
24
|
-
/**
|
|
25
|
-
* Get cache timestamp from response
|
|
26
|
-
* @param {Response} response
|
|
27
|
-
* @returns {number | null} Timestamp in milliseconds since epoch, or null if not found
|
|
28
|
-
*/
|
|
29
|
-
export function getCacheTimestamp(response: Response): number | null;
|
|
30
|
-
/**
|
|
31
|
-
* Check if response is fresh
|
|
32
|
-
* @param {Response} response
|
|
33
|
-
* @param {number} ttl - Time-to-live in seconds
|
|
34
|
-
* @returns {boolean}
|
|
35
|
-
*/
|
|
36
|
-
export function isFresh(response: Response, ttl: number): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Check if response is stale (but usable)
|
|
39
|
-
* @param {Response} response
|
|
40
|
-
* @param {number} ttl - Time-to-live in seconds
|
|
41
|
-
* @param {number | null} staleTTL - Stale time-to-live in seconds
|
|
42
|
-
* @returns {boolean}
|
|
43
|
-
*/
|
|
44
|
-
export function isStale(response: Response, ttl: number, staleTTL: number | null): boolean;
|
|
45
|
-
/**
|
|
46
|
-
* Add timestamp to response
|
|
47
|
-
* @param {Response} response
|
|
48
|
-
* @returns {Response}
|
|
49
|
-
*/
|
|
50
|
-
export function addTimestamp(response: Response): Response;
|
|
51
|
-
/**
|
|
52
|
-
* Get inferred invalidation paths
|
|
53
|
-
* @param {string} url
|
|
54
|
-
* @returns {string[]}
|
|
55
|
-
*/
|
|
56
|
-
export function getInferredInvalidationPaths(url: string): string[];
|
|
57
|
-
/**
|
|
58
|
-
* Get strategy from request headers or use default
|
|
59
|
-
* @param {Headers} headers
|
|
60
|
-
* @param {CacheStrategy} defaultStrategy
|
|
61
|
-
* @param {string} url - Request URL for logging
|
|
62
|
-
* @returns {CacheStrategy}
|
|
63
|
-
*/
|
|
64
|
-
export function getStrategy(headers: Headers, defaultStrategy: CacheStrategy, url?: string): CacheStrategy;
|
|
65
|
-
/**
|
|
66
|
-
* Get TTL from request headers or use default
|
|
67
|
-
* @param {Headers} headers
|
|
68
|
-
* @param {number} defaultTTL - Default TTL in seconds
|
|
69
|
-
* @param {string} url - Request URL for logging
|
|
70
|
-
* @returns {number | null} TTL in seconds, or null if caching is disabled
|
|
71
|
-
*/
|
|
72
|
-
export function getTTL(headers: Headers, defaultTTL: number, url?: string): number | null;
|
|
73
|
-
/**
|
|
74
|
-
* Get stale TTL from request headers or use default
|
|
75
|
-
* @param {Headers} headers
|
|
76
|
-
* @param {number} defaultStaleTTL - Default stale TTL in seconds
|
|
77
|
-
* @param {string} url - Request URL for logging
|
|
78
|
-
* @returns {number | null} Stale TTL in seconds, or null if stale caching is disabled
|
|
79
|
-
*/
|
|
80
|
-
export function getStaleTTL(headers: Headers, defaultStaleTTL: number, url?: string): number | null;
|
|
81
|
-
/**
|
|
82
|
-
* Check if URL matches scope. Returns true if scope array is empty or if the URL pathname starts with any of the scope prefixes.
|
|
83
|
-
* @param {string} url
|
|
84
|
-
* @param {string[]} scope
|
|
85
|
-
* @param {number} defaultTTLSeconds
|
|
86
|
-
* @returns {boolean}
|
|
87
|
-
*/
|
|
88
|
-
export function matchesScope(url: string, scope: string[], defaultTTLSeconds: number): boolean;
|
|
89
|
-
/**
|
|
90
|
-
* Invalidate cache entries
|
|
91
|
-
* @param {string} cacheName
|
|
92
|
-
* @param {string[]} urls
|
|
93
|
-
* @returns {Promise<void>}
|
|
94
|
-
*/
|
|
95
|
-
export function invalidateCache(cacheName: string, urls: string[]): Promise<void>;
|
|
96
|
-
/**
|
|
97
|
-
* Clear entire cache
|
|
98
|
-
* @param {string} cacheName
|
|
99
|
-
* @returns {Promise<void>}
|
|
100
|
-
*/
|
|
101
|
-
export function clearCache(cacheName: string): Promise<void>;
|
|
102
|
-
/**
|
|
103
|
-
* Check if a cached response is older than the maximum age
|
|
104
|
-
* @param {Response} response
|
|
105
|
-
* @param {number} maxAgeSeconds - Maximum age in seconds
|
|
106
|
-
* @returns {boolean}
|
|
107
|
-
*/
|
|
108
|
-
export function isOlderThanMaxAge(response: Response, maxAgeSeconds: number): boolean;
|
|
109
|
-
/**
|
|
110
|
-
* Clean up cache entries older than maxAgeSeconds
|
|
111
|
-
* @param {string} cacheName
|
|
112
|
-
* @param {number} maxAgeSeconds - Maximum age in seconds
|
|
113
|
-
* @returns {Promise<void>}
|
|
114
|
-
*/
|
|
115
|
-
export function cleanupOldCacheEntries(cacheName: string, maxAgeSeconds: number): Promise<void>;
|
|
116
|
-
/**
|
|
117
|
-
* Log an informational message (minimal and verbose levels)
|
|
118
|
-
* @param {string} message - Message to log
|
|
119
|
-
*/
|
|
120
|
-
export function logInfo(message: string): void;
|
|
121
|
-
/**
|
|
122
|
-
* Log a verbose message (verbose level only)
|
|
123
|
-
* @param {string} message - Message to log
|
|
124
|
-
*/
|
|
125
|
-
export function logVerbose(message: string): void;
|
|
126
|
-
/**
|
|
127
|
-
* Validate configuration object
|
|
128
|
-
* @param {HandleRequestConfig} config - Configuration object to validate
|
|
129
|
-
* @throws {Error} If config is invalid
|
|
130
|
-
*/
|
|
131
|
-
export function validateConfig(config: HandleRequestConfig): void;
|
|
132
|
-
import type { LoggingLevel } from "./types";
|
|
133
|
-
import type { CacheStrategy } from "./types";
|
|
134
|
-
import type { HandleRequestConfig } from "./types";
|
package/types.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// NOTE: this is the only .d.ts file that is checked in, other declaration files are generated as part of the prepublishOnly script.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Caching strategy for handling requests.
|
|
5
|
-
* - `cache-first`: Return from cache if fresh (within TTL), otherwise fetch from network immediately. Stale cache is only used when offline (network request fails). No background updates.
|
|
6
|
-
* - `network-first`: Try network first, fall back to stale cache if offline (within stale TTL). Cache updated when network succeeds.
|
|
7
|
-
* - `stale-while-revalidate`: Return from cache immediately (fresh = no update, stale = update in background). Fetch from network if too stale or missing.
|
|
8
|
-
*/
|
|
9
|
-
export type CacheStrategy =
|
|
10
|
-
| "cache-first"
|
|
11
|
-
| "network-first"
|
|
12
|
-
| "stale-while-revalidate";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Logging level for cache operations.
|
|
16
|
-
* - `none`: No logging
|
|
17
|
-
* - `minimal`: Logs cache hits and cache invalidation only
|
|
18
|
-
* - `verbose`: Logs all events including cache misses, cleanup, and header usage
|
|
19
|
-
*/
|
|
20
|
-
export type LoggingLevel = "none" | "minimal" | "verbose";
|
|
21
|
-
|
|
22
|
-
export interface HandleRequestConfig {
|
|
23
|
-
/** Name of the cache, used when calling `Cache.open(cacheName)` internally. Changing this name effectively clears the previous cache entries. */
|
|
24
|
-
cacheName: string;
|
|
25
|
-
/** URL prefixes to cache by default (e.g., `['/api/']`). If not set and `defaultTTLSeconds` is set, all same-origin GET requests are cached automatically. If not set and `defaultTTLSeconds` is not set (or 0), no requests are cached by default. Individual requests outside the scope can still enable caching with `X-SW-Cache-TTL-Seconds` header. Note: Cross-origin requests are never cached, regardless of scope or TTL headers. */
|
|
26
|
-
scope?: string[];
|
|
27
|
-
/** Default caching strategy: `'cache-first'`, `'network-first'`, or `'stale-while-revalidate'`. */
|
|
28
|
-
defaultStrategy?: CacheStrategy;
|
|
29
|
-
/** Maximum age for fresh content. Fresh content will be returned from cache for cache-first and stale-while-revalidate strategies, and also from network-first when offline. Fresh content does not get updated from the network. Since this defaults to `300`, caching is automatic by default for GET requests matching the scope. Set to `0` or `undefined` to disable automatic caching (individual requests can still enable caching with `X-SW-Cache-TTL-Seconds` header). */
|
|
30
|
-
defaultTTLSeconds?: number;
|
|
31
|
-
/** Maximum age for stale content. Stale content will be returned from cache for cache-first (when offline), network-first (when offline), and stale-while-revalidate strategies. That means responses past the fresh TTL but within stale TTL can still be returned from cache. Stale content does get updated from the network. */
|
|
32
|
-
defaultStaleTTLSeconds?: number;
|
|
33
|
-
/** Automatically invalidate cache on POST/PATCH/PUT/DELETE requests. */
|
|
34
|
-
inferInvalidation?: boolean;
|
|
35
|
-
/** Custom fetch function to use for network requests. Receives a `Request` object and must return a `Promise<Response>`. Useful for handling authentication errors (401/403) or adding custom headers to all requests. */
|
|
36
|
-
customFetch?: typeof fetch;
|
|
37
|
-
/** Maximum age (in seconds) before cache entries are automatically cleaned up. Entries older than this age are deleted. Defaults to 7200 seconds (2 hours, which is 2x the default stale TTL). Cache entries are cleaned up reactively (when accessed) and periodically (every 100 fetches). */
|
|
38
|
-
maxCacheAgeSeconds?: number;
|
|
39
|
-
/** Logging level: "none" (no logging), "minimal" (cache hits and invalidation only), or "verbose" (all logging including misses, cleanup, and headers). Defaults to "none". */
|
|
40
|
-
loggingLevel?: LoggingLevel;
|
|
41
|
-
}
|