ts-service-worker 1.0.31
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/README.md +229 -0
- package/dist/cli.mjs +6376 -0
- package/dist/index.js +6400 -0
- package/dist/index.js.map +7 -0
- package/package.json +55 -0
- package/types/cli.d.mts +1 -0
- package/types/fileSpecsToPaths.d.mts +9 -0
- package/types/generate.d.mts +27 -0
- package/types/index.d.mts +3 -0
- package/types/requestHandler.d.mts +3 -0
- package/types/serviceWorkerTemplate.d.mts +1 -0
- package/types/strategies.d.mts +124 -0
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ts-service-worker",
|
|
3
|
+
"version": "1.0.31",
|
|
4
|
+
"description": "Generate caching service-worker.js using declarative Typescript.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Andrew Peterson, NDP Software",
|
|
7
|
+
"email": "andy@ndpsoftware.com"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"build",
|
|
11
|
+
"cache",
|
|
12
|
+
"declarative configuration",
|
|
13
|
+
"offline",
|
|
14
|
+
"progressive web apps",
|
|
15
|
+
"pwa",
|
|
16
|
+
"service worker",
|
|
17
|
+
"sw",
|
|
18
|
+
"ts",
|
|
19
|
+
"typescript"
|
|
20
|
+
],
|
|
21
|
+
"license": "Copyright (c) 2023 Andrew J. Peterson, NDP Software",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
|
|
24
|
+
"type": "commonjs",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./types/index.d.mts",
|
|
27
|
+
"bin": {
|
|
28
|
+
"ts-service-worker": "./dist/cli.mjs"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"README.md",
|
|
32
|
+
"dist",
|
|
33
|
+
"types"
|
|
34
|
+
],
|
|
35
|
+
|
|
36
|
+
"scripts": {
|
|
37
|
+
"example1": "node --loader tsx example/amp-what-sw.mts",
|
|
38
|
+
"example2": "./dist/cli.mjs example/offline-backup.json",
|
|
39
|
+
"test": "node --test --loader tsx src/specs/*.mts",
|
|
40
|
+
"build": "yarn clean && yarn build-dist && yarn build-bin && yarn build-types",
|
|
41
|
+
"clean": "rm -fr ./dist/* ./types/*",
|
|
42
|
+
"build-dist": "./node_modules/.bin/esbuild --target=esnext --outfile=./dist/index.js --bundle --platform=node --sourcemap --format=cjs src/index.mts",
|
|
43
|
+
"build-bin": "esbuild --banner:js=\"#!/usr/bin/env node\" --bundle --platform=node --format=esm --target=es2022 src/cli.mts --outfile=dist/cli.mjs && chmod u+x dist/cli.mjs",
|
|
44
|
+
"build-types": "tsc -p src/tsconfig.json",
|
|
45
|
+
"prepublishOnly": "yarn build"
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=16"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"esbuild": "*",
|
|
53
|
+
"tsx": "*"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/types/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type FilesSpec = string | Array<string> | {
|
|
2
|
+
glob: string | Array<string>;
|
|
3
|
+
dir?: string;
|
|
4
|
+
prefix?: string;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Convert a messy file spec into an array of paths.
|
|
8
|
+
*/
|
|
9
|
+
export declare function fileSpecsToPaths(files: FilesSpec): Array<string>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { InputCacheStrategy, InputPaths } from './strategies.mjs';
|
|
2
|
+
export type Plan = Array<InputCacheStrategy>;
|
|
3
|
+
type Version = string;
|
|
4
|
+
export type Options = {
|
|
5
|
+
version?: Version;
|
|
6
|
+
debug?: boolean;
|
|
7
|
+
skipWaiting?: boolean;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Produce a string that contains Javascript code to implement
|
|
11
|
+
* the given caching `Plan`, taking into account options.
|
|
12
|
+
*
|
|
13
|
+
* Options:
|
|
14
|
+
* - `version`: a semvar version for the cache in the user's browser. This
|
|
15
|
+
* is largely ignored, but importantly a major version bump
|
|
16
|
+
* will cause the cache to be rebuilt.
|
|
17
|
+
* - `debug`: true will generate `console.log` notices for
|
|
18
|
+
* all activity in the service worker. It should be turned off in production.
|
|
19
|
+
* - `skipWaiting`: setting to `true` has the same effect as
|
|
20
|
+
* calling the method as described in the service worker documention (but simpler!).
|
|
21
|
+
*/
|
|
22
|
+
export declare function generateServiceWorker(inputSpec: Plan, optionsIn?: Options): string;
|
|
23
|
+
export declare function generateVersion(version: Version): string;
|
|
24
|
+
export declare function generateFlag(name: string, bool: boolean): string;
|
|
25
|
+
export declare function pathsToJS(paths: InputPaths): string;
|
|
26
|
+
export declare function pathToJS(path: string | RegExp | symbol): string;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const TEMPLATE: string;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { FilesSpec } from "./fileSpecsToPaths.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Preload and cache any number of files enumerated from disk using globs.
|
|
4
|
+
* Implies 'cacheFirst'.
|
|
5
|
+
*/
|
|
6
|
+
export type CacheFileOnInstall = {
|
|
7
|
+
strategy: 'cache-on-install';
|
|
8
|
+
files: FilesSpec;
|
|
9
|
+
};
|
|
10
|
+
export declare function isCacheFileOnInstall(s: {
|
|
11
|
+
strategy: string;
|
|
12
|
+
}): s is CacheFileOnInstall;
|
|
13
|
+
/**
|
|
14
|
+
* Preload and cache enumerated paths (not files)
|
|
15
|
+
* Implies 'cacheFirst'.
|
|
16
|
+
*/
|
|
17
|
+
export type CachePathOnInstall = {
|
|
18
|
+
strategy: 'cache-on-install';
|
|
19
|
+
paths: string | Array<string>;
|
|
20
|
+
};
|
|
21
|
+
export declare function isCachePathOnInstall(s: {
|
|
22
|
+
strategy: string;
|
|
23
|
+
}): s is CachePathOnInstall;
|
|
24
|
+
/**
|
|
25
|
+
Serve from the cache if available, otherwise go to the network.
|
|
26
|
+
Ideal for: CSS, images, fonts, JS, templates… basically anything you'd consider static to that "version" of your site.
|
|
27
|
+
@see https://web.dev/learn/pwa/serving/#cache-first
|
|
28
|
+
On its own implies no update to the cache file, so only useful
|
|
29
|
+
for immutable resources.
|
|
30
|
+
https://web.dev/offline-cookbook/#on-network-response
|
|
31
|
+
if retrieved via network, gets cached
|
|
32
|
+
*/
|
|
33
|
+
export type CacheFirstStrategy<Paths> = {
|
|
34
|
+
readonly strategy: 'cacheFirst';
|
|
35
|
+
readonly paths: Paths;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* If the network connection is not available, use
|
|
39
|
+
* the cached version.
|
|
40
|
+
* Files are cached in the background as they are fetched.
|
|
41
|
+
* @see https://web.dev/learn/pwa/serving/#network-first
|
|
42
|
+
*/
|
|
43
|
+
export type NetworkFirstStrategy<Paths> = {
|
|
44
|
+
strategy: 'networkFirst';
|
|
45
|
+
paths: Paths;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Serve files from the cache, and then in the background update
|
|
49
|
+
* with a fresh version.
|
|
50
|
+
* Useful for potentially slow and non-time-sensitive resources.
|
|
51
|
+
* @see https://web.dev/learn/pwa/serving/#stale-while-revalidate
|
|
52
|
+
*/
|
|
53
|
+
export type StaleWhileRevalidateStrategy<Paths> = {
|
|
54
|
+
strategy: 'staleWhileRevalidate';
|
|
55
|
+
paths: Paths;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Serve normally (from network) unless not available. If
|
|
59
|
+
* network is down, use the response from the backup path.
|
|
60
|
+
* The backup path is cached on service worker installation.
|
|
61
|
+
*/
|
|
62
|
+
export type StaticOfflineBackupStrategy<Paths> = {
|
|
63
|
+
strategy: 'staticOfflineBackup';
|
|
64
|
+
paths: Paths;
|
|
65
|
+
backup: string;
|
|
66
|
+
};
|
|
67
|
+
export declare function isStaticOfflineBackup(s: {
|
|
68
|
+
strategy: string;
|
|
69
|
+
}): s is StaticOfflineBackupStrategy<unknown>;
|
|
70
|
+
/**
|
|
71
|
+
* Serve from the network only. No caching.
|
|
72
|
+
* https://web.dev/learn/pwa/serving/#network-only
|
|
73
|
+
*/
|
|
74
|
+
export type NetworkOnlyStrategy<Paths> = {
|
|
75
|
+
strategy: 'networkOnly';
|
|
76
|
+
paths: Paths;
|
|
77
|
+
};
|
|
78
|
+
export type RoutableStrategy<Paths> = CacheFirstStrategy<Paths> | NetworkFirstStrategy<Paths> | StaleWhileRevalidateStrategy<Paths> | NetworkOnlyStrategy<Paths> | StaticOfflineBackupStrategy<Paths>;
|
|
79
|
+
export type CacheOnInstallStrategy = CachePathOnInstall | CacheFileOnInstall;
|
|
80
|
+
export type InputCacheStrategy = RoutableStrategy<InputPaths> | CacheOnInstallStrategy;
|
|
81
|
+
export type InputCacheStrategyAsPaths = RoutableStrategy<InputPaths> | CachePathOnInstall;
|
|
82
|
+
/**
|
|
83
|
+
* Indicator for all file paths routed from the same origin
|
|
84
|
+
* as the service worker is served from.
|
|
85
|
+
*/
|
|
86
|
+
export declare const Origin: unique symbol;
|
|
87
|
+
/**
|
|
88
|
+
* Indicator for all files served within the service worker's
|
|
89
|
+
* scope. Usually you will want `Origin`, but this is included
|
|
90
|
+
* for completeness.
|
|
91
|
+
*/
|
|
92
|
+
export declare const Scope: unique symbol;
|
|
93
|
+
export type InputPaths = RegExp | string | OutputPaths | typeof Origin | typeof Scope;
|
|
94
|
+
export type OutputPaths = Array<string | RegExp>;
|
|
95
|
+
export declare function isRoutableStrategy<Paths>(x: {
|
|
96
|
+
strategy: string;
|
|
97
|
+
}): x is RoutableStrategy<Paths>;
|
|
98
|
+
export declare function convertPreloadFilesToPaths(strategy: CacheFileOnInstall): CachePathOnInstall;
|
|
99
|
+
/**
|
|
100
|
+
* Converts paths specified by scanning the disk to the paths
|
|
101
|
+
* that are the result of that scan.
|
|
102
|
+
* Users may specific some paths as "files", which are
|
|
103
|
+
* then determined by scanning the disk (with glob patterns.)
|
|
104
|
+
* Obviously this is useful for certain static resources,
|
|
105
|
+
* where it might be tedious to keep the service worker spec
|
|
106
|
+
* up-to-date with resources in a folder.
|
|
107
|
+
* The downside of this are that it can pick up extra files
|
|
108
|
+
* without the developer being aware, so it's important to
|
|
109
|
+
* review either the generated service worker code, or the
|
|
110
|
+
* cache created in the browser.
|
|
111
|
+
*/
|
|
112
|
+
export declare function convertPreloadFiles(strategy: InputCacheStrategy): InputCacheStrategyAsPaths;
|
|
113
|
+
/**
|
|
114
|
+
* Run through all the strategies in a plan and extract all
|
|
115
|
+
* the paths that should be "preloaded" on service worker
|
|
116
|
+
* installation. This includes explicit preload paths,
|
|
117
|
+
* preload-files (which have been converted to paths before
|
|
118
|
+
* this call), and offline backup paths.
|
|
119
|
+
*/
|
|
120
|
+
export declare function extractAllPreloadPaths(plan: Array<{
|
|
121
|
+
strategy: string;
|
|
122
|
+
}>): OutputPaths;
|
|
123
|
+
export declare function convertPreloadPathsToCacheFirst(spec: Array<InputCacheStrategyAsPaths>): Array<RoutableStrategy<InputPaths>>;
|
|
124
|
+
export declare function convertAllFilesToPaths(spec: Array<InputCacheStrategy>): Array<InputCacheStrategyAsPaths>;
|