@drupflare/spool 0.0.0-stage → 0.2.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 +1749 -2
- package/dist/cdn/client.js +1 -0
- package/dist/cdn/location.d.ts +11 -0
- package/dist/cdn/location.js +15 -0
- package/dist/cdn/manifest.json +16 -0
- package/dist/cdn/spool-sw.js +1 -0
- package/dist/client/entry.d.ts +1 -0
- package/dist/client/entry.js +25 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +2 -0
- package/dist/client/lifecycle.d.ts +10 -0
- package/dist/client/lifecycle.js +10 -0
- package/dist/client/loads.d.ts +16 -0
- package/dist/client/loads.js +41 -0
- package/dist/client/register.d.ts +34 -0
- package/dist/client/register.js +58 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/media/embeds.d.ts +59 -0
- package/dist/media/embeds.js +120 -0
- package/dist/media/headers.d.ts +13 -0
- package/dist/media/headers.js +44 -0
- package/dist/media/html.d.ts +27 -0
- package/dist/media/html.js +93 -0
- package/dist/media/image.d.ts +222 -0
- package/dist/media/image.js +413 -0
- package/dist/media/index.d.ts +6 -0
- package/dist/media/index.js +6 -0
- package/dist/media/lazy.d.ts +26 -0
- package/dist/media/lazy.js +74 -0
- package/dist/media/responsive.d.ts +45 -0
- package/dist/media/responsive.js +98 -0
- package/dist/media/video.d.ts +30 -0
- package/dist/media/video.js +75 -0
- package/dist/node/listener.d.ts +19 -0
- package/dist/node/listener.js +76 -0
- package/dist/protocol/artifact.d.ts +54 -0
- package/dist/protocol/artifact.js +79 -0
- package/dist/protocol/assembly.d.ts +34 -0
- package/dist/protocol/assembly.js +72 -0
- package/dist/protocol/beacon.d.ts +59 -0
- package/dist/protocol/beacon.js +86 -0
- package/dist/protocol/collector.d.ts +41 -0
- package/dist/protocol/collector.js +84 -0
- package/dist/protocol/config.d.ts +89 -0
- package/dist/protocol/config.js +13 -0
- package/dist/protocol/envelope.d.ts +47 -0
- package/dist/protocol/envelope.js +162 -0
- package/dist/protocol/errors.d.ts +7 -0
- package/dist/protocol/errors.js +9 -0
- package/dist/protocol/graph.d.ts +81 -0
- package/dist/protocol/graph.js +201 -0
- package/dist/protocol/index.d.ts +19 -0
- package/dist/protocol/index.js +19 -0
- package/dist/protocol/manifest.d.ts +29 -0
- package/dist/protocol/manifest.js +94 -0
- package/dist/protocol/normalise.d.ts +20 -0
- package/dist/protocol/normalise.js +29 -0
- package/dist/protocol/pattern.d.ts +12 -0
- package/dist/protocol/pattern.js +82 -0
- package/dist/protocol/provision.d.ts +100 -0
- package/dist/protocol/provision.js +105 -0
- package/dist/protocol/route.d.ts +179 -0
- package/dist/protocol/route.js +135 -0
- package/dist/protocol/sync.d.ts +31 -0
- package/dist/protocol/sync.js +87 -0
- package/dist/protocol/tag.d.ts +12 -0
- package/dist/protocol/tag.js +26 -0
- package/dist/protocol/template/compile.d.ts +35 -0
- package/dist/protocol/template/compile.js +82 -0
- package/dist/protocol/template/interpret.d.ts +9 -0
- package/dist/protocol/template/interpret.js +82 -0
- package/dist/protocol/vector.d.ts +32 -0
- package/dist/protocol/vector.js +56 -0
- package/dist/protocol/view.d.ts +110 -0
- package/dist/protocol/view.js +201 -0
- package/dist/proxy/dictionary.d.ts +58 -0
- package/dist/proxy/dictionary.js +197 -0
- package/dist/proxy/eligible.d.ts +45 -0
- package/dist/proxy/eligible.js +96 -0
- package/dist/proxy/index.d.ts +5 -0
- package/dist/proxy/index.js +5 -0
- package/dist/proxy/proxy.d.ts +133 -0
- package/dist/proxy/proxy.js +507 -0
- package/dist/proxy/purge.d.ts +10 -0
- package/dist/proxy/purge.js +66 -0
- package/dist/proxy/tags.d.ts +8 -0
- package/dist/proxy/tags.js +36 -0
- package/dist/report/derive.d.ts +223 -0
- package/dist/report/derive.js +494 -0
- package/dist/report/economics.d.ts +14 -0
- package/dist/report/economics.js +24 -0
- package/dist/report/index.d.ts +9 -0
- package/dist/report/index.js +9 -0
- package/dist/report/inputs.d.ts +141 -0
- package/dist/report/inputs.js +216 -0
- package/dist/report/ledger.d.ts +169 -0
- package/dist/report/ledger.js +278 -0
- package/dist/report/measurement.d.ts +48 -0
- package/dist/report/measurement.js +117 -0
- package/dist/report/model.d.ts +42 -0
- package/dist/report/model.js +74 -0
- package/dist/report/thirdparty.d.ts +62 -0
- package/dist/report/thirdparty.js +107 -0
- package/dist/report/traffic.d.ts +140 -0
- package/dist/report/traffic.js +228 -0
- package/dist/report/trim.d.ts +81 -0
- package/dist/report/trim.js +105 -0
- package/dist/server/beacon.d.ts +20 -0
- package/dist/server/beacon.js +47 -0
- package/dist/server/embedded.d.ts +6 -0
- package/dist/server/embedded.js +6 -0
- package/dist/server/ga4.d.ts +70 -0
- package/dist/server/ga4.js +165 -0
- package/dist/server/identity.d.ts +12 -0
- package/dist/server/identity.js +17 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +7 -0
- package/dist/server/sink.d.ts +56 -0
- package/dist/server/sink.js +133 -0
- package/dist/server/size.d.ts +7 -0
- package/dist/server/size.js +18 -0
- package/dist/server/spool.d.ts +97 -0
- package/dist/server/spool.js +463 -0
- package/dist/server/stub.d.ts +9 -0
- package/dist/server/stub.js +19 -0
- package/dist/server/sync.d.ts +12 -0
- package/dist/server/sync.js +48 -0
- package/dist/server/work.d.ts +22 -0
- package/dist/server/work.js +30 -0
- package/dist/stores/durable-object.d.ts +19 -0
- package/dist/stores/durable-object.js +53 -0
- package/dist/stores/index.d.ts +2 -0
- package/dist/stores/index.js +2 -0
- package/dist/stores/memory.d.ts +4 -0
- package/dist/stores/memory.js +10 -0
- package/dist/stores/version-store.d.ts +11 -0
- package/dist/stores/version-store.js +1 -0
- package/dist/sw/analytics.d.ts +81 -0
- package/dist/sw/analytics.js +275 -0
- package/dist/sw/collect.d.ts +8 -0
- package/dist/sw/collect.js +33 -0
- package/dist/sw/core.d.ts +1 -0
- package/dist/sw/core.js +3 -0
- package/dist/sw/freshness.d.ts +15 -0
- package/dist/sw/freshness.js +20 -0
- package/dist/sw/graph/index.d.ts +67 -0
- package/dist/sw/graph/index.js +170 -0
- package/dist/sw/graph/invalidate.d.ts +23 -0
- package/dist/sw/graph/invalidate.js +109 -0
- package/dist/sw/graph/projections.d.ts +7 -0
- package/dist/sw/graph/projections.js +9 -0
- package/dist/sw/graph/records.d.ts +7 -0
- package/dist/sw/graph/records.js +9 -0
- package/dist/sw/graph/store.d.ts +32 -0
- package/dist/sw/graph/store.js +52 -0
- package/dist/sw/idb.d.ts +8 -0
- package/dist/sw/idb.js +117 -0
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/partition.d.ts +21 -0
- package/dist/sw/partition.js +137 -0
- package/dist/sw/policy.d.ts +8 -0
- package/dist/sw/policy.js +24 -0
- package/dist/sw/provision.d.ts +39 -0
- package/dist/sw/provision.js +61 -0
- package/dist/sw/render.d.ts +21 -0
- package/dist/sw/render.js +53 -0
- package/dist/sw/replica.d.ts +77 -0
- package/dist/sw/replica.js +299 -0
- package/dist/sw/router.d.ts +42 -0
- package/dist/sw/router.js +48 -0
- package/dist/sw/routing.d.ts +23 -0
- package/dist/sw/routing.js +48 -0
- package/dist/sw/runtime.d.ts +112 -0
- package/dist/sw/runtime.js +1070 -0
- package/dist/sw/scheduler.d.ts +83 -0
- package/dist/sw/scheduler.js +149 -0
- package/dist/sw/seal.d.ts +37 -0
- package/dist/sw/seal.js +183 -0
- package/dist/sw/serve.d.ts +15 -0
- package/dist/sw/serve.js +63 -0
- package/dist/sw/shadow.d.ts +16 -0
- package/dist/sw/shadow.js +27 -0
- package/dist/sw/static.d.ts +17 -0
- package/dist/sw/static.js +81 -0
- package/dist/sw/storage.d.ts +64 -0
- package/dist/sw/storage.js +34 -0
- package/dist/sw/thirdparty.d.ts +17 -0
- package/dist/sw/thirdparty.js +36 -0
- package/dist/trim/css.d.ts +38 -0
- package/dist/trim/css.js +328 -0
- package/dist/trim/index.d.ts +3 -0
- package/dist/trim/index.js +3 -0
- package/dist/trim/manifest.d.ts +67 -0
- package/dist/trim/manifest.js +134 -0
- package/dist/trim/tokens.d.ts +47 -0
- package/dist/trim/tokens.js +142 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +3 -0
- package/package.json +150 -5
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { mergeList } from '../media/headers.js';
|
|
2
|
+
import { ProtocolError } from '../protocol/errors.js';
|
|
3
|
+
import { decline } from './eligible.js';
|
|
4
|
+
const MAGIC = {
|
|
5
|
+
dcb: [0xff, 0x44, 0x43, 0x42],
|
|
6
|
+
dcz: [0x5e, 0x2a, 0x4d, 0x18, 0x20, 0x00, 0x00, 0x00]
|
|
7
|
+
};
|
|
8
|
+
const DEFAULT_MATCH = /\.(?:css|m?js)$/i;
|
|
9
|
+
const DEFAULT_MAX_BYTES = 2 * 1024 * 1024;
|
|
10
|
+
const DEFAULT_CACHE_BYTES = 16 * 1024 * 1024;
|
|
11
|
+
const SAFE_DIRECTORY = /^[A-Za-z0-9/_.~%-]*$/;
|
|
12
|
+
const AVAILABLE = /^:([A-Za-z0-9+/]{43}=):$/;
|
|
13
|
+
/** the body of a `dcb` or `dcz` response: the coding's magic bytes, the dictionary's SHA-256, then the stream */
|
|
14
|
+
export function frameDictionaryBody(format, hash, stream) {
|
|
15
|
+
const magic = MAGIC[format];
|
|
16
|
+
const out = new Uint8Array(magic.length + hash.length + stream.length);
|
|
17
|
+
out.set(magic, 0);
|
|
18
|
+
out.set(hash, magic.length);
|
|
19
|
+
out.set(stream, magic.length + hash.length);
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
const toBase64 = (bytes) => btoa(String.fromCharCode(...bytes));
|
|
23
|
+
async function sha256(bytes) {
|
|
24
|
+
return new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
|
|
25
|
+
}
|
|
26
|
+
/** the codings `Accept-Encoding` lists with a quality above zero, lowercase */
|
|
27
|
+
function accepted(header) {
|
|
28
|
+
const out = new Set();
|
|
29
|
+
for (const part of (header ?? '').split(',')) {
|
|
30
|
+
const [token, ...params] = part.trim().toLowerCase().split(';');
|
|
31
|
+
const q = params.map((p) => p.trim()).find((p) => p.startsWith('q='));
|
|
32
|
+
if (token && !(q !== undefined && Number(q.slice(2)) === 0))
|
|
33
|
+
out.add(token.trim());
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
function kindOf(response) {
|
|
38
|
+
const type = (response.headers.get('content-type') ?? '').split(';')[0]?.trim().toLowerCase();
|
|
39
|
+
if (type === 'text/css')
|
|
40
|
+
return 'style';
|
|
41
|
+
if (type === 'text/javascript' || type === 'application/javascript')
|
|
42
|
+
return 'script';
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
/** applies the defaults and checks the values; `ProtocolError('bad-route')` for one out of range */
|
|
46
|
+
export function createDictionaries(options, gate) {
|
|
47
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
48
|
+
const cacheBytes = options.cacheBytes ?? DEFAULT_CACHE_BYTES;
|
|
49
|
+
const formats = options.formats ?? ['dcz'];
|
|
50
|
+
const match = options.match ?? DEFAULT_MATCH;
|
|
51
|
+
const { base } = options;
|
|
52
|
+
const isBase = base === undefined
|
|
53
|
+
? () => true
|
|
54
|
+
: typeof base === 'function'
|
|
55
|
+
? base
|
|
56
|
+
: (pathname) => [].concat(base).some((pattern) => {
|
|
57
|
+
pattern.lastIndex = 0;
|
|
58
|
+
return pattern.test(pathname);
|
|
59
|
+
});
|
|
60
|
+
if (!Number.isInteger(maxBytes) || maxBytes < 1) {
|
|
61
|
+
throw new ProtocolError('bad-route', 'dictionary maxBytes must be a positive integer');
|
|
62
|
+
}
|
|
63
|
+
if (!Number.isInteger(cacheBytes) || cacheBytes < 1) {
|
|
64
|
+
throw new ProtocolError('bad-route', 'dictionary cacheBytes must be a positive integer');
|
|
65
|
+
}
|
|
66
|
+
if (formats.length === 0 || formats.some((f) => f !== 'dcb' && f !== 'dcz')) {
|
|
67
|
+
throw new ProtocolError('bad-route', `dictionary formats must list 'dcb' or 'dcz'`);
|
|
68
|
+
}
|
|
69
|
+
const stats = {
|
|
70
|
+
registered: 0,
|
|
71
|
+
encoded: 0,
|
|
72
|
+
bytesIn: 0,
|
|
73
|
+
bytesOut: 0,
|
|
74
|
+
skipped: {}
|
|
75
|
+
};
|
|
76
|
+
const held = new Map();
|
|
77
|
+
let size = 0;
|
|
78
|
+
const skip = (why) => void (stats.skipped[why] = (stats.skipped[why] ?? 0) + 1);
|
|
79
|
+
function touch(key, body) {
|
|
80
|
+
held.delete(key);
|
|
81
|
+
held.set(key, body);
|
|
82
|
+
}
|
|
83
|
+
function remember(key, bytes) {
|
|
84
|
+
const body = held.get(key);
|
|
85
|
+
if (body)
|
|
86
|
+
return touch(key, body);
|
|
87
|
+
held.set(key, bytes);
|
|
88
|
+
size += bytes.length;
|
|
89
|
+
stats.registered++;
|
|
90
|
+
for (const [oldest, body] of held) {
|
|
91
|
+
if (size <= cacheBytes || held.size === 1)
|
|
92
|
+
break;
|
|
93
|
+
held.delete(oldest);
|
|
94
|
+
size -= body.length;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function applies(request, response) {
|
|
98
|
+
if (request.method !== 'GET' || response.status !== 200)
|
|
99
|
+
return false;
|
|
100
|
+
match.lastIndex = 0;
|
|
101
|
+
if (!match.test(new URL(request.url).pathname))
|
|
102
|
+
return false;
|
|
103
|
+
return kindOf(response) !== null;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
stats,
|
|
107
|
+
applies,
|
|
108
|
+
async apply(request, response) {
|
|
109
|
+
const url = new URL(request.url);
|
|
110
|
+
const auth = new Headers();
|
|
111
|
+
if (request.headers.has('authorization'))
|
|
112
|
+
auth.set('authorization', 'x');
|
|
113
|
+
const why = decline({
|
|
114
|
+
method: 'GET',
|
|
115
|
+
search: '',
|
|
116
|
+
requestHeaders: auth,
|
|
117
|
+
response,
|
|
118
|
+
tags: [],
|
|
119
|
+
data: true,
|
|
120
|
+
principal: false
|
|
121
|
+
}, gate);
|
|
122
|
+
if (why !== null)
|
|
123
|
+
return response;
|
|
124
|
+
const declared = Number(response.headers.get('content-length'));
|
|
125
|
+
if (request.headers.has('range')) {
|
|
126
|
+
skip('range');
|
|
127
|
+
return response;
|
|
128
|
+
}
|
|
129
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
130
|
+
skip('size');
|
|
131
|
+
return response;
|
|
132
|
+
}
|
|
133
|
+
const directory = url.pathname.slice(0, url.pathname.lastIndexOf('/') + 1);
|
|
134
|
+
if (!SAFE_DIRECTORY.test(directory)) {
|
|
135
|
+
skip('pattern');
|
|
136
|
+
return response;
|
|
137
|
+
}
|
|
138
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
139
|
+
const headers = new Headers(response.headers);
|
|
140
|
+
const plain = () => new Response(bytes, {
|
|
141
|
+
status: 200,
|
|
142
|
+
statusText: response.statusText,
|
|
143
|
+
headers
|
|
144
|
+
});
|
|
145
|
+
if (bytes.length === 0 || bytes.length > maxBytes) {
|
|
146
|
+
skip('size');
|
|
147
|
+
return plain();
|
|
148
|
+
}
|
|
149
|
+
const hash = await sha256(bytes);
|
|
150
|
+
const key = AVAILABLE.exec(request.headers.get('available-dictionary') ?? '')?.[1];
|
|
151
|
+
const dictionary = key === undefined ? undefined : held.get(key);
|
|
152
|
+
if (key !== undefined && dictionary)
|
|
153
|
+
touch(key, dictionary);
|
|
154
|
+
mergeList(headers, 'vary', ['Accept-Encoding', 'Available-Dictionary']);
|
|
155
|
+
if (isBase(url.pathname)) {
|
|
156
|
+
headers.set('use-as-dictionary', `match="${directory}*", match-dest=("${kindOf(response)}")`);
|
|
157
|
+
remember(toBase64(hash), bytes);
|
|
158
|
+
}
|
|
159
|
+
if (!dictionary) {
|
|
160
|
+
skip('unavailable');
|
|
161
|
+
return plain();
|
|
162
|
+
}
|
|
163
|
+
const wanted = accepted(request.headers.get('accept-encoding'));
|
|
164
|
+
const format = formats.find((f) => wanted.has(f));
|
|
165
|
+
if (!format) {
|
|
166
|
+
skip('unavailable');
|
|
167
|
+
return plain();
|
|
168
|
+
}
|
|
169
|
+
let framed;
|
|
170
|
+
try {
|
|
171
|
+
const stream = await options.encoder(dictionary, bytes, format);
|
|
172
|
+
framed = frameDictionaryBody(format, await sha256(dictionary), stream);
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
skip('failed');
|
|
176
|
+
return plain();
|
|
177
|
+
}
|
|
178
|
+
if (framed.length >= bytes.length) {
|
|
179
|
+
skip('larger');
|
|
180
|
+
return plain();
|
|
181
|
+
}
|
|
182
|
+
headers.set('content-encoding', format);
|
|
183
|
+
headers.delete('content-length');
|
|
184
|
+
const etag = headers.get('etag');
|
|
185
|
+
if (etag && !etag.startsWith('W/'))
|
|
186
|
+
headers.set('etag', `W/${etag}`);
|
|
187
|
+
stats.encoded++;
|
|
188
|
+
stats.bytesIn += bytes.length;
|
|
189
|
+
stats.bytesOut += framed.length;
|
|
190
|
+
return new Response(framed, {
|
|
191
|
+
status: 200,
|
|
192
|
+
statusText: response.statusText,
|
|
193
|
+
headers
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** why the proxy will not keep a response on the device */
|
|
2
|
+
export type Decline = 'method' | 'query' | 'authorization' | 'cookie' | 'status' | 'type' | 'set-cookie' | 'private' | 'no-store' | 'vary' | 'tags' | 'size' | 'charset';
|
|
3
|
+
/** what the verdict reads; `principal` is a route the caller declared for a signed-in visitor */
|
|
4
|
+
export type Candidate = {
|
|
5
|
+
method: string;
|
|
6
|
+
/** the search part of the page URL, empty when there is none */
|
|
7
|
+
search: string;
|
|
8
|
+
/** the headers of the request the visitor made */
|
|
9
|
+
requestHeaders: Headers;
|
|
10
|
+
response: Response;
|
|
11
|
+
/** the tags the response named, before any default */
|
|
12
|
+
tags: readonly string[];
|
|
13
|
+
/** a `data` route answers any content type, a page must be HTML */
|
|
14
|
+
data: boolean;
|
|
15
|
+
principal: boolean;
|
|
16
|
+
};
|
|
17
|
+
/** the limits the verdict applies */
|
|
18
|
+
export type Gate = {
|
|
19
|
+
publicCookies: ReadonlySet<string>;
|
|
20
|
+
maxTags: number;
|
|
21
|
+
maxBytes: number;
|
|
22
|
+
};
|
|
23
|
+
/** the cookie names a `Cookie` header carries */
|
|
24
|
+
export declare function cookieNames(header: string | null): string[];
|
|
25
|
+
/** whether a response sets a cookie whose name is not on the allowlist */
|
|
26
|
+
export declare function setsPrivateCookie(headers: Headers, publicCookies: ReadonlySet<string>): boolean;
|
|
27
|
+
/** the lowercase `charset` a `Content-Type` value names, or null */
|
|
28
|
+
export declare function headerCharset(contentType: string | null): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* The lowercase charset an HTML response declares, from its `Content-Type` or else a `<meta>` in
|
|
31
|
+
* its first 1024 bytes, or null when it declares none.
|
|
32
|
+
*/
|
|
33
|
+
export declare function htmlCharset(headers: Headers, bytes: Uint8Array): string | null;
|
|
34
|
+
/** whether `charset` is UTF-8; a page that declares none is read as UTF-8 */
|
|
35
|
+
export declare function isUtf8(charset: string | null): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* The first reason the response may not be answered locally, or `null` when it may. Everything
|
|
38
|
+
* here declines: a request with a cookie outside the allowlist or an `Authorization` header, a
|
|
39
|
+
* query string, a non-200, a body that is not HTML, a page whose `Content-Type` names a charset
|
|
40
|
+
* other than UTF-8, a `Set-Cookie` off the allowlist, `private`, `no-store`, a `Vary`
|
|
41
|
+
* that is not about encoding or cookies, and more tags than the gate allows. A `principal` route
|
|
42
|
+
* stands for a visitor the caller's `identity` resolves, so the cookie, `Authorization` and
|
|
43
|
+
* `private` rules do not apply to it.
|
|
44
|
+
*/
|
|
45
|
+
export declare function decline(input: Candidate, gate: Gate): Decline | null;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** `Vary` members that do not change the body once cookies are checked by name */
|
|
2
|
+
const STABLE_VARY = new Set(['accept-encoding', 'cookie']);
|
|
3
|
+
/** the cookie names a `Cookie` header carries */
|
|
4
|
+
export function cookieNames(header) {
|
|
5
|
+
if (!header)
|
|
6
|
+
return [];
|
|
7
|
+
return header
|
|
8
|
+
.split(';')
|
|
9
|
+
.map((part) => part.split('=')[0]?.trim() ?? '')
|
|
10
|
+
.filter((name) => name !== '');
|
|
11
|
+
}
|
|
12
|
+
/** whether a response sets a cookie whose name is not on the allowlist */
|
|
13
|
+
export function setsPrivateCookie(headers, publicCookies) {
|
|
14
|
+
return headers
|
|
15
|
+
.getSetCookie()
|
|
16
|
+
.some((line) => !publicCookies.has(line.split('=')[0]?.trim() ?? ''));
|
|
17
|
+
}
|
|
18
|
+
const UTF8_LABELS = new Set(['utf-8', 'utf8', 'unicode-1-1-utf-8']);
|
|
19
|
+
/** the lowercase `charset` a `Content-Type` value names, or null */
|
|
20
|
+
export function headerCharset(contentType) {
|
|
21
|
+
const match = /;\s*charset\s*=\s*"?([^";\s]+)/i.exec(contentType ?? '');
|
|
22
|
+
return match ? match[1].toLowerCase() : null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The lowercase charset an HTML response declares, from its `Content-Type` or else a `<meta>` in
|
|
26
|
+
* its first 1024 bytes, or null when it declares none.
|
|
27
|
+
*/
|
|
28
|
+
export function htmlCharset(headers, bytes) {
|
|
29
|
+
const header = headerCharset(headers.get('content-type'));
|
|
30
|
+
if (header)
|
|
31
|
+
return header;
|
|
32
|
+
const head = String.fromCharCode(...bytes.subarray(0, 1024));
|
|
33
|
+
const meta = /<meta[^>]+charset\s*=\s*["']?([^"'\s/>;]+)/i.exec(head);
|
|
34
|
+
return meta ? meta[1].toLowerCase() : null;
|
|
35
|
+
}
|
|
36
|
+
/** whether `charset` is UTF-8; a page that declares none is read as UTF-8 */
|
|
37
|
+
export function isUtf8(charset) {
|
|
38
|
+
return charset === null || UTF8_LABELS.has(charset);
|
|
39
|
+
}
|
|
40
|
+
/** the lowercase directive names of a `Cache-Control` value; a `private="x"` counts as `private` */
|
|
41
|
+
function directives(value) {
|
|
42
|
+
return new Set((value ?? '')
|
|
43
|
+
.split(',')
|
|
44
|
+
.map((part) => (part.split('=')[0] ?? '').trim().toLowerCase())
|
|
45
|
+
.filter((name) => name !== ''));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The first reason the response may not be answered locally, or `null` when it may. Everything
|
|
49
|
+
* here declines: a request with a cookie outside the allowlist or an `Authorization` header, a
|
|
50
|
+
* query string, a non-200, a body that is not HTML, a page whose `Content-Type` names a charset
|
|
51
|
+
* other than UTF-8, a `Set-Cookie` off the allowlist, `private`, `no-store`, a `Vary`
|
|
52
|
+
* that is not about encoding or cookies, and more tags than the gate allows. A `principal` route
|
|
53
|
+
* stands for a visitor the caller's `identity` resolves, so the cookie, `Authorization` and
|
|
54
|
+
* `private` rules do not apply to it.
|
|
55
|
+
*/
|
|
56
|
+
export function decline(input, gate) {
|
|
57
|
+
const { response, principal } = input;
|
|
58
|
+
if (input.method !== 'GET' && input.method !== 'HEAD')
|
|
59
|
+
return 'method';
|
|
60
|
+
if (input.search !== '')
|
|
61
|
+
return 'query';
|
|
62
|
+
if (!principal) {
|
|
63
|
+
if (input.requestHeaders.has('authorization'))
|
|
64
|
+
return 'authorization';
|
|
65
|
+
const cookies = cookieNames(input.requestHeaders.get('cookie'));
|
|
66
|
+
if (cookies.some((name) => !gate.publicCookies.has(name)))
|
|
67
|
+
return 'cookie';
|
|
68
|
+
}
|
|
69
|
+
if (response.status !== 200)
|
|
70
|
+
return 'status';
|
|
71
|
+
const type = (response.headers.get('content-type') ?? '').toLowerCase();
|
|
72
|
+
if (!input.data && !type.includes('text/html'))
|
|
73
|
+
return 'type';
|
|
74
|
+
// the stored copy is UTF-8 text, so a page in another charset would come back garbled
|
|
75
|
+
if (!input.data && !isUtf8(headerCharset(type)))
|
|
76
|
+
return 'charset';
|
|
77
|
+
if (setsPrivateCookie(response.headers, gate.publicCookies))
|
|
78
|
+
return 'set-cookie';
|
|
79
|
+
const control = directives(response.headers.get('cache-control'));
|
|
80
|
+
if (!principal && control.has('private'))
|
|
81
|
+
return 'private';
|
|
82
|
+
if (control.has('no-store'))
|
|
83
|
+
return 'no-store';
|
|
84
|
+
const vary = (response.headers.get('vary') ?? '')
|
|
85
|
+
.split(',')
|
|
86
|
+
.map((name) => name.trim().toLowerCase())
|
|
87
|
+
.filter((name) => name !== '');
|
|
88
|
+
if (vary.some((name) => !STABLE_VARY.has(name)))
|
|
89
|
+
return 'vary';
|
|
90
|
+
if (input.tags.length > gate.maxTags)
|
|
91
|
+
return 'tags';
|
|
92
|
+
const length = Number(response.headers.get('content-length'));
|
|
93
|
+
if (Number.isFinite(length) && length > gate.maxBytes)
|
|
94
|
+
return 'size';
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { type EmbedOptions, type EmbedStats } from '../media/embeds.js';
|
|
2
|
+
import { type ImageOptions, type ImageStats } from '../media/image.js';
|
|
3
|
+
import { type VideoOptions } from '../media/video.js';
|
|
4
|
+
import { type AssemblyRoute, type DataRoute } from '../protocol/route.js';
|
|
5
|
+
import { type Spool, type SpoolOptions } from '../server/spool.js';
|
|
6
|
+
import { type TrimManifest } from '../trim/index.js';
|
|
7
|
+
import { type DictionaryOptions, type DictionaryStats } from './dictionary.js';
|
|
8
|
+
/**
|
|
9
|
+
* A route the proxy answers locally, declared like a `createSpool` route minus `load`: the proxy
|
|
10
|
+
* loads the page from the origin. A `data` route parses the origin's JSON. `depends` adds tags to
|
|
11
|
+
* the ones the origin sent.
|
|
12
|
+
*/
|
|
13
|
+
export type ProxyRoute = ({
|
|
14
|
+
pattern: string;
|
|
15
|
+
strategy?: 'assembly';
|
|
16
|
+
} & Omit<AssemblyRoute, 'strategy' | 'load'>) | ({
|
|
17
|
+
pattern: string;
|
|
18
|
+
strategy: 'data';
|
|
19
|
+
} & Omit<DataRoute, 'strategy' | 'load'>);
|
|
20
|
+
/** what `spoolProxy` takes */
|
|
21
|
+
export type ProxyOptions = {
|
|
22
|
+
/** the origin to forward to, such as `https://origin.internal`; no path */
|
|
23
|
+
origin: string | URL;
|
|
24
|
+
/**
|
|
25
|
+
* the options `createSpool` takes, minus `routes`: the worker's route list is fixed when the
|
|
26
|
+
* spool is created, and the proxy puts its own catch-all route there
|
|
27
|
+
*/
|
|
28
|
+
spool: Omit<SpoolOptions, 'routes'>;
|
|
29
|
+
/** routes that override the default of one public `assembly` route per path; they match first */
|
|
30
|
+
routes?: readonly ProxyRoute[];
|
|
31
|
+
/**
|
|
32
|
+
* cookie names that carry no identity, such as a bot-check or load-balancer cookie: they do not
|
|
33
|
+
* make a request ineligible, and setting them does not clear the device; default none
|
|
34
|
+
*/
|
|
35
|
+
publicCookies?: readonly string[];
|
|
36
|
+
/** keep the tag headers on what the browser receives; default false */
|
|
37
|
+
exposeTags?: boolean;
|
|
38
|
+
/** most tags one response may name before it is not kept; default 512 */
|
|
39
|
+
maxTags?: number;
|
|
40
|
+
/** largest page body kept, in bytes; default 2 MiB */
|
|
41
|
+
maxBytes?: number;
|
|
42
|
+
/** patterns blanked before a page is hashed, such as a per-render form token */
|
|
43
|
+
ignore?: readonly RegExp[];
|
|
44
|
+
/** most pages whose last body hash is remembered, the oldest going first; default 10,000 */
|
|
45
|
+
maxRemembered?: number;
|
|
46
|
+
/** the shared secret `POST /.spool/purge` requires; without one every purge is refused */
|
|
47
|
+
purgeSecret?: string;
|
|
48
|
+
/** how requests reach the origin; default global `fetch`. A Workers service binding passes its `fetch` */
|
|
49
|
+
fetch?: (request: Request) => Promise<Response>;
|
|
50
|
+
/**
|
|
51
|
+
* trimmed stylesheets from `buildTrimManifest`. A same-origin stylesheet in the manifest is
|
|
52
|
+
* answered with its trimmed text when the origin's bytes still hash to the manifest's `source`,
|
|
53
|
+
* and every HTML page is checked against the guard first. Without it nothing is trimmed.
|
|
54
|
+
*/
|
|
55
|
+
trim?: TrimManifest;
|
|
56
|
+
/**
|
|
57
|
+
* Converts JPEG and PNG responses to WebP for a browser that accepts it, at the quality and
|
|
58
|
+
* width the request's headers call for, up to a width ceiling of 2560 pixels when the request
|
|
59
|
+
* names none. Identical simultaneous requests share one encoder call, and an encoder that runs
|
|
60
|
+
* past 10 seconds is abandoned for that request. See `ImageOptions`. Without it no image is
|
|
61
|
+
* touched.
|
|
62
|
+
*/
|
|
63
|
+
images?: ImageOptions;
|
|
64
|
+
/**
|
|
65
|
+
* Rewrites `<video>` tags in HTML responses, such as `preload="none"`; see `VideoOptions`.
|
|
66
|
+
* Without it no page is touched for video.
|
|
67
|
+
*/
|
|
68
|
+
video?: VideoOptions;
|
|
69
|
+
/**
|
|
70
|
+
* Replaces server-rendered YouTube and Vimeo `<iframe>` embeds with a link that loads the
|
|
71
|
+
* player on click; see `EmbedOptions`. A page that sends a `Content-Security-Policy` is left
|
|
72
|
+
* alone. Without it no embed is touched.
|
|
73
|
+
*/
|
|
74
|
+
embeds?: EmbedOptions;
|
|
75
|
+
/**
|
|
76
|
+
* Serves a stylesheet or script as `dcz` or `dcb` against an earlier public file of the same
|
|
77
|
+
* directory the visitor already holds (RFC 9842, Chromium 130 and later); see
|
|
78
|
+
* `DictionaryOptions`. Without it no response is marked or encoded.
|
|
79
|
+
*/
|
|
80
|
+
dictionary?: DictionaryOptions;
|
|
81
|
+
};
|
|
82
|
+
/** what the proxy counted since it started: trimmed answers, hash mismatches and guard firings */
|
|
83
|
+
export type TrimStats = {
|
|
84
|
+
served: number;
|
|
85
|
+
mismatched: number;
|
|
86
|
+
guarded: number;
|
|
87
|
+
};
|
|
88
|
+
/** what the proxy counted for `video`: pages with at least one rewritten tag, and the tags */
|
|
89
|
+
export type VideoStats = {
|
|
90
|
+
pages: number;
|
|
91
|
+
rewritten: number;
|
|
92
|
+
};
|
|
93
|
+
/** where the proxy writes a guard firing or a hash mismatch to the sink, as JSON `{ kind, ... }` */
|
|
94
|
+
export declare const TRIM_PATH = "/.spool/trim";
|
|
95
|
+
/** the Fetch handler `spoolProxy` returns, with the spool it built */
|
|
96
|
+
export type SpoolProxy = {
|
|
97
|
+
(request: Request): Promise<Response>;
|
|
98
|
+
readonly spool: Spool;
|
|
99
|
+
/** live counters; all zero when `trim` is not set */
|
|
100
|
+
readonly trim: Readonly<TrimStats>;
|
|
101
|
+
/** live counters; all zero when `images` is not set */
|
|
102
|
+
readonly images: Readonly<ImageStats>;
|
|
103
|
+
/** live counters; all zero when `video` is not set */
|
|
104
|
+
readonly video: Readonly<VideoStats>;
|
|
105
|
+
/** live counters; all zero when `embeds` is not set */
|
|
106
|
+
readonly embeds: Readonly<EmbedStats>;
|
|
107
|
+
/** live counters; all zero when `dictionary` is not set */
|
|
108
|
+
readonly dictionary: Readonly<DictionaryStats>;
|
|
109
|
+
};
|
|
110
|
+
/**
|
|
111
|
+
* A Fetch handler that sits in front of an origin that cannot load the library, such as PHP or a
|
|
112
|
+
* Drupal site with no module, and gives it local page views.
|
|
113
|
+
*
|
|
114
|
+
* `/.spool/*` and `/spool-sw.js` are answered here. Everything else goes to the origin with its
|
|
115
|
+
* method, headers and body, and redirects are passed through. A GET or HEAD without `Authorization`
|
|
116
|
+
* or a cookie outside `publicCookies`, answered 200 as `text/html` with no `Set-Cookie`, `private`
|
|
117
|
+
* or `no-store`, is decorated so the worker can keep it; anything else is forwarded untouched.
|
|
118
|
+
* The tags the origin sends in `Cache-Tag`, `Surrogate-Key`, `X-Drupal-Cache-Tags` or `xkey`
|
|
119
|
+
* become the page's dependencies, and a page with none depends on its own path.
|
|
120
|
+
*
|
|
121
|
+
* With `images` the proxy answers a JPEG or PNG request from a browser that accepts WebP with the
|
|
122
|
+
* converted file, and adds `Accept-CH` to HTML so later image requests carry their width. With
|
|
123
|
+
* `video` it rewrites `<video>` tags in HTML. Both run on the origin's response before the page is
|
|
124
|
+
* kept, so a held page carries the rewritten tags.
|
|
125
|
+
*
|
|
126
|
+
* Limits: the proxy sees finished HTML, so an artifact is the whole page, the replica holds no
|
|
127
|
+
* records, and two pages never share a fragment. A page is invalidated by `POST /.spool/purge`, or
|
|
128
|
+
* when the proxy sees its body change, and it stays on a device until its route's `maxStale` runs
|
|
129
|
+
* out and the next sync reports the change. Freshness is therefore bounded by `maxStale` plus the
|
|
130
|
+
* time a purge takes to arrive. The memory of last-seen bodies lives in the instance: a restart
|
|
131
|
+
* forgets it, and each instance of a scaled-out proxy keeps its own.
|
|
132
|
+
*/
|
|
133
|
+
export declare function spoolProxy(options: ProxyOptions): SpoolProxy;
|