swimple 0.5.0 → 0.6.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/headers.d.ts ADDED
@@ -0,0 +1,42 @@
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 ADDED
@@ -0,0 +1,134 @@
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/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference no-default-lib="true"/>
2
+ /**
3
+ * Creates a request handler function for service worker fetch events.
4
+ * The handler implements HTTP caching with configurable strategies (cache-first, network-first, stale-while-revalidate),
5
+ * automatic cache invalidation for mutations, and periodic cache cleanup. Only handles same-origin GET requests
6
+ * that match the configured scope.
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
10
+ */
11
+ export function createHandleRequest(config: HandleRequestConfig): (event: FetchEvent) => Promise<Response> | null;
12
+ export { cleanupOldCacheEntries } from "./helpers.js";
13
+ import type { HandleRequestConfig } from "./types";
package/package.json CHANGED
@@ -1,12 +1,18 @@
1
1
  {
2
2
  "name": "swimple",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "A simple service worker library for request caching",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "exports": {
8
- ".": "./index.js",
9
- "./headers": "./headers.js"
8
+ ".": {
9
+ "types": "./index.d.ts",
10
+ "default": "./index.js"
11
+ },
12
+ "./headers": {
13
+ "types": "./headers.d.ts",
14
+ "default": "./headers.js"
15
+ }
10
16
  },
11
17
  "scripts": {
12
18
  "test": "npm run test:unit && npm run test:e2e && npm run test:ui",
@@ -14,7 +20,8 @@
14
20
  "test:e2e": "node --test tests/**/*.e2e.test.js",
15
21
  "test:ui": "playwright test",
16
22
  "format:check": "prettier --check .",
17
- "format:fix": "prettier --write ."
23
+ "format:fix": "prettier --write .",
24
+ "prepublishOnly": "tsc --allowJs --declaration --emitDeclarationOnly --outDir ./ --skipLibCheck --target es2022 --module es2022 --moduleResolution node index.js headers.js helpers.js"
18
25
  },
19
26
  "keywords": [
20
27
  "service-worker",
@@ -28,7 +35,8 @@
28
35
  "license": "MIT",
29
36
  "devDependencies": {
30
37
  "@playwright/test": "^1.57.0",
31
- "prettier": "^3.7.4"
38
+ "prettier": "^3.7.4",
39
+ "typescript": "^5.9.3"
32
40
  },
33
41
  "engines": {
34
42
  "node": ">=24"
package/types.d.ts CHANGED
@@ -1,3 +1,5 @@
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
+
1
3
  /**
2
4
  * Caching strategy for handling requests.
3
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.