@drupflare/spool 0.0.0-stage → 0.1.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 +1557 -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/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/eligible.d.ts +45 -0
- package/dist/proxy/eligible.js +96 -0
- package/dist/proxy/index.d.ts +4 -0
- package/dist/proxy/index.js +4 -0
- package/dist/proxy/proxy.d.ts +86 -0
- package/dist/proxy/proxy.js +388 -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 +221 -0
- package/dist/report/derive.js +490 -0
- package/dist/report/economics.d.ts +14 -0
- package/dist/report/economics.js +24 -0
- package/dist/report/index.d.ts +8 -0
- package/dist/report/index.js +8 -0
- package/dist/report/inputs.d.ts +141 -0
- package/dist/report/inputs.js +216 -0
- package/dist/report/measurement.d.ts +48 -0
- package/dist/report/measurement.js +114 -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 +43 -0
- package/dist/trim/tokens.js +142 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +3 -0
- package/package.json +146 -5
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { PATHS } from '../protocol/config.js';
|
|
2
|
+
import { writeEnvelope } from '../protocol/envelope.js';
|
|
3
|
+
import { ProtocolError } from '../protocol/errors.js';
|
|
4
|
+
import { normalise } from '../protocol/normalise.js';
|
|
5
|
+
import { defineRoute, matchRoute } from '../protocol/route.js';
|
|
6
|
+
import { artifactTag } from '../protocol/sync.js';
|
|
7
|
+
import { createSpool } from '../server/spool.js';
|
|
8
|
+
import { guardPage, sourceHash, stripTrimParam, TRIM_PARAM, trimKey } from '../trim/index.js';
|
|
9
|
+
import { decline, htmlCharset, isUtf8, setsPrivateCookie } from './eligible.js';
|
|
10
|
+
import { PURGE_PATH, respondPurge } from './purge.js';
|
|
11
|
+
import { pathTag, readTags, stripTags } from './tags.js';
|
|
12
|
+
const HOP_BY_HOP = [
|
|
13
|
+
'connection',
|
|
14
|
+
'keep-alive',
|
|
15
|
+
'proxy-authenticate',
|
|
16
|
+
'proxy-authorization',
|
|
17
|
+
'te',
|
|
18
|
+
'trailer',
|
|
19
|
+
'transfer-encoding',
|
|
20
|
+
'upgrade',
|
|
21
|
+
'host'
|
|
22
|
+
];
|
|
23
|
+
/** where the proxy writes a guard firing or a hash mismatch to the sink, as JSON `{ kind, ... }` */
|
|
24
|
+
export const TRIM_PATH = '/.spool/trim';
|
|
25
|
+
const isHtml = (response) => (response.headers.get('content-type') ?? '').toLowerCase().includes('text/html');
|
|
26
|
+
/** one character per byte, so an ASCII edit leaves every other byte as it was */
|
|
27
|
+
function fromBytes(bytes) {
|
|
28
|
+
let text = '';
|
|
29
|
+
for (let at = 0; at < bytes.length; at += 8192) {
|
|
30
|
+
text += String.fromCharCode(...bytes.subarray(at, at + 8192));
|
|
31
|
+
}
|
|
32
|
+
return text;
|
|
33
|
+
}
|
|
34
|
+
const toBytes = (text) => Uint8Array.from(text, (char) => char.charCodeAt(0));
|
|
35
|
+
/**
|
|
36
|
+
* A Fetch handler that sits in front of an origin that cannot load the library, such as PHP or a
|
|
37
|
+
* Drupal site with no module, and gives it local page views.
|
|
38
|
+
*
|
|
39
|
+
* `/.spool/*` and `/spool-sw.js` are answered here. Everything else goes to the origin with its
|
|
40
|
+
* method, headers and body, and redirects are passed through. A GET or HEAD without `Authorization`
|
|
41
|
+
* or a cookie outside `publicCookies`, answered 200 as `text/html` with no `Set-Cookie`, `private`
|
|
42
|
+
* or `no-store`, is decorated so the worker can keep it; anything else is forwarded untouched.
|
|
43
|
+
* The tags the origin sends in `Cache-Tag`, `Surrogate-Key`, `X-Drupal-Cache-Tags` or `xkey`
|
|
44
|
+
* become the page's dependencies, and a page with none depends on its own path.
|
|
45
|
+
*
|
|
46
|
+
* Limits: the proxy sees finished HTML, so an artifact is the whole page, the replica holds no
|
|
47
|
+
* records, and two pages never share a fragment. A page is invalidated by `POST /.spool/purge`, or
|
|
48
|
+
* when the proxy sees its body change, and it stays on a device until its route's `maxStale` runs
|
|
49
|
+
* out and the next sync reports the change. Freshness is therefore bounded by `maxStale` plus the
|
|
50
|
+
* time a purge takes to arrive. The memory of last-seen bodies lives in the instance: a restart
|
|
51
|
+
* forgets it, and each instance of a scaled-out proxy keeps its own.
|
|
52
|
+
*/
|
|
53
|
+
export function spoolProxy(options) {
|
|
54
|
+
const base = new URL(options.origin);
|
|
55
|
+
if (base.pathname !== '/' || base.search !== '' || base.hash !== '') {
|
|
56
|
+
throw new ProtocolError('bad-route', `origin ${base.href} must have no path`);
|
|
57
|
+
}
|
|
58
|
+
const gate = {
|
|
59
|
+
publicCookies: new Set(options.publicCookies ?? []),
|
|
60
|
+
maxTags: options.maxTags ?? 512,
|
|
61
|
+
maxBytes: options.maxBytes ?? 2 * 1024 * 1024
|
|
62
|
+
};
|
|
63
|
+
if (!Number.isInteger(gate.maxTags) || gate.maxTags < 1) {
|
|
64
|
+
throw new ProtocolError('bad-route', 'maxTags must be a positive integer');
|
|
65
|
+
}
|
|
66
|
+
if (!Number.isInteger(gate.maxBytes) || gate.maxBytes < 1) {
|
|
67
|
+
throw new ProtocolError('bad-route', 'maxBytes must be a positive integer');
|
|
68
|
+
}
|
|
69
|
+
const remembered = options.maxRemembered ?? 10_000;
|
|
70
|
+
if (!Number.isInteger(remembered) || remembered < 1) {
|
|
71
|
+
throw new ProtocolError('bad-route', 'maxRemembered must be a positive integer');
|
|
72
|
+
}
|
|
73
|
+
const call = options.fetch ?? ((request) => fetch(request));
|
|
74
|
+
const { store } = options.spool;
|
|
75
|
+
const { trim } = options;
|
|
76
|
+
const stats = { served: 0, mismatched: 0, guarded: 0 };
|
|
77
|
+
function note(kind, detail) {
|
|
78
|
+
if (!options.spool.sink)
|
|
79
|
+
return;
|
|
80
|
+
try {
|
|
81
|
+
const written = options.spool.sink.write([
|
|
82
|
+
{
|
|
83
|
+
path: TRIM_PATH,
|
|
84
|
+
body: JSON.stringify({ kind, ...detail }),
|
|
85
|
+
contentType: 'application/json',
|
|
86
|
+
at: Date.now()
|
|
87
|
+
}
|
|
88
|
+
]);
|
|
89
|
+
Promise.resolve(written).catch(() => undefined);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
// a failing sink never breaks a page
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** the page with its trimmed stylesheets pointed at the full files when the guard fires */
|
|
96
|
+
function guarded(text, pageUrl) {
|
|
97
|
+
if (!trim)
|
|
98
|
+
return text;
|
|
99
|
+
const verdict = guardPage(text, trim, pageUrl);
|
|
100
|
+
if (!verdict.fired)
|
|
101
|
+
return text;
|
|
102
|
+
stats.guarded++;
|
|
103
|
+
note('guard', {
|
|
104
|
+
page: new URL(pageUrl).pathname,
|
|
105
|
+
reason: verdict.reason,
|
|
106
|
+
files: verdict.files
|
|
107
|
+
});
|
|
108
|
+
return verdict.html;
|
|
109
|
+
}
|
|
110
|
+
function weaken(headers) {
|
|
111
|
+
const etag = headers.get('etag');
|
|
112
|
+
if (etag && !etag.startsWith('W/'))
|
|
113
|
+
headers.set('etag', `W/${etag}`);
|
|
114
|
+
}
|
|
115
|
+
// the page a request resolved to, so `depends` and `load` do not fetch it again
|
|
116
|
+
const pages = new WeakMap();
|
|
117
|
+
const seen = new Map();
|
|
118
|
+
function originHeaders(source, publicUrl) {
|
|
119
|
+
const headers = new Headers(source);
|
|
120
|
+
for (const name of HOP_BY_HOP)
|
|
121
|
+
headers.delete(name);
|
|
122
|
+
for (const name of [...headers.keys()])
|
|
123
|
+
if (name.startsWith('spool-'))
|
|
124
|
+
headers.delete(name);
|
|
125
|
+
headers.set('x-forwarded-host', publicUrl.host);
|
|
126
|
+
headers.set('x-forwarded-proto', publicUrl.protocol.slice(0, -1));
|
|
127
|
+
return headers;
|
|
128
|
+
}
|
|
129
|
+
function forward(request, url) {
|
|
130
|
+
const read = request.method === 'GET' || request.method === 'HEAD';
|
|
131
|
+
return new Request(new URL(url.pathname + stripTrimParam(url.search), base), {
|
|
132
|
+
method: request.method,
|
|
133
|
+
headers: originHeaders(request.headers, url),
|
|
134
|
+
redirect: 'manual',
|
|
135
|
+
signal: request.signal,
|
|
136
|
+
...(read || !request.body ? {} : { body: request.body, duplex: 'half' })
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/** the headers the browser gets: bodies arrive decoded, so the encoding and its length go */
|
|
140
|
+
function tidy(source, url) {
|
|
141
|
+
const headers = new Headers(source);
|
|
142
|
+
if (headers.has('content-encoding')) {
|
|
143
|
+
headers.delete('content-encoding');
|
|
144
|
+
headers.delete('content-length');
|
|
145
|
+
}
|
|
146
|
+
const location = headers.get('location');
|
|
147
|
+
if (location && /^[a-z][a-z0-9+.-]*:/i.test(location)) {
|
|
148
|
+
const target = new URL(location);
|
|
149
|
+
if (target.origin === base.origin) {
|
|
150
|
+
headers.set('location', new URL(target.pathname + target.search + target.hash, url).href);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (!options.exposeTags)
|
|
154
|
+
stripTags(headers);
|
|
155
|
+
return headers;
|
|
156
|
+
}
|
|
157
|
+
async function digest(text) {
|
|
158
|
+
const bytes = new TextEncoder().encode(normalise(text, options.ignore ? { ignore: options.ignore } : {}));
|
|
159
|
+
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', bytes));
|
|
160
|
+
return btoa(String.fromCharCode(...hash));
|
|
161
|
+
}
|
|
162
|
+
/** records a public page's body and bumps its tags when it differs from the last one seen */
|
|
163
|
+
async function settle(path, text, tags) {
|
|
164
|
+
const hash = await digest(text);
|
|
165
|
+
const prior = seen.get(path);
|
|
166
|
+
seen.delete(path);
|
|
167
|
+
seen.set(path, { hash, tags });
|
|
168
|
+
if (seen.size > remembered)
|
|
169
|
+
seen.delete(seen.keys().next().value);
|
|
170
|
+
if (prior && prior.hash !== hash) {
|
|
171
|
+
await spool.invalidate([...new Set([...prior.tags, ...tags])]);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
async function fetchPage(ctx, data, principal) {
|
|
175
|
+
const path = ctx.url.pathname;
|
|
176
|
+
const headers = originHeaders(ctx.request.headers, new URL(ctx.request.url));
|
|
177
|
+
for (const name of [
|
|
178
|
+
'range',
|
|
179
|
+
'if-match',
|
|
180
|
+
'if-none-match',
|
|
181
|
+
'if-modified-since',
|
|
182
|
+
'if-unmodified-since',
|
|
183
|
+
'if-range',
|
|
184
|
+
'content-length',
|
|
185
|
+
'content-type'
|
|
186
|
+
]) {
|
|
187
|
+
headers.delete(name);
|
|
188
|
+
}
|
|
189
|
+
headers.set('accept', data ? 'application/json' : 'text/html');
|
|
190
|
+
const response = await call(new Request(new URL(path, base), { method: 'GET', headers, redirect: 'manual' }));
|
|
191
|
+
const tags = readTags(response.headers);
|
|
192
|
+
const why = decline({
|
|
193
|
+
method: 'GET',
|
|
194
|
+
search: '',
|
|
195
|
+
requestHeaders: ctx.request.headers,
|
|
196
|
+
response,
|
|
197
|
+
tags,
|
|
198
|
+
data,
|
|
199
|
+
principal
|
|
200
|
+
}, gate);
|
|
201
|
+
if (why)
|
|
202
|
+
throw new ProtocolError('bad-artifact', `${path} is not kept (${why})`);
|
|
203
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
204
|
+
if (!data && !isUtf8(htmlCharset(response.headers, bytes))) {
|
|
205
|
+
throw new ProtocolError('bad-artifact', `${path} is not kept (charset)`);
|
|
206
|
+
}
|
|
207
|
+
const text = guarded(new TextDecoder().decode(bytes), ctx.url.href);
|
|
208
|
+
if (text.length > gate.maxBytes) {
|
|
209
|
+
throw new ProtocolError('bad-artifact', `${path} is not kept (size)`);
|
|
210
|
+
}
|
|
211
|
+
const effective = tags.length > 0 ? tags : [pathTag(path)];
|
|
212
|
+
if (!principal)
|
|
213
|
+
await settle(path, text, effective);
|
|
214
|
+
return { text, tags: effective };
|
|
215
|
+
}
|
|
216
|
+
function pageFor(ctx, data, principal) {
|
|
217
|
+
const known = pages.get(ctx.request);
|
|
218
|
+
if (known && known.path === ctx.url.pathname)
|
|
219
|
+
return known.page;
|
|
220
|
+
const page = fetchPage(ctx, data, principal);
|
|
221
|
+
pages.set(ctx.request, { path: ctx.url.pathname, page });
|
|
222
|
+
return page;
|
|
223
|
+
}
|
|
224
|
+
function proxied(pattern, declaration) {
|
|
225
|
+
const data = declaration?.strategy === 'data';
|
|
226
|
+
const principal = declaration?.scope === 'principal';
|
|
227
|
+
if (principal && !options.spool.identity) {
|
|
228
|
+
throw new ProtocolError('bad-route', `${pattern}: a principal route needs spool.identity`);
|
|
229
|
+
}
|
|
230
|
+
const extra = declaration?.depends;
|
|
231
|
+
const depends = async (ctx) => [
|
|
232
|
+
...(await pageFor(ctx, data, principal)).tags,
|
|
233
|
+
...(extra ? await extra(ctx) : [])
|
|
234
|
+
];
|
|
235
|
+
if (declaration?.strategy === 'data') {
|
|
236
|
+
return defineRoute(pattern, {
|
|
237
|
+
...declaration,
|
|
238
|
+
strategy: 'data',
|
|
239
|
+
depends,
|
|
240
|
+
load: async (ctx) => {
|
|
241
|
+
const { text } = await pageFor(ctx, true, principal);
|
|
242
|
+
try {
|
|
243
|
+
return JSON.parse(text);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
throw new ProtocolError('bad-artifact', `${pattern}: the origin sent no JSON`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
return defineRoute(pattern, {
|
|
252
|
+
...declaration,
|
|
253
|
+
strategy: 'assembly',
|
|
254
|
+
depends,
|
|
255
|
+
load: async (ctx) => ({ shell: (await pageFor(ctx, false, principal)).text })
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
const routes = [
|
|
259
|
+
...(options.routes ?? []).map((declaration) => proxied(declaration.pattern, declaration)),
|
|
260
|
+
proxied('/**')
|
|
261
|
+
];
|
|
262
|
+
const spool = createSpool({ ...options.spool, routes });
|
|
263
|
+
/** a page the proxy declines tells the worker to drop its copy of that path, in the same response */
|
|
264
|
+
async function unfit(response, path, why, page) {
|
|
265
|
+
if (!page || why === 'query')
|
|
266
|
+
return response;
|
|
267
|
+
const headers = new Headers(response.headers);
|
|
268
|
+
writeEnvelope(headers, { invalidate: { [artifactTag(path)]: (await store.cursor()) + 1 } });
|
|
269
|
+
return new Response(response.body, {
|
|
270
|
+
status: response.status,
|
|
271
|
+
statusText: response.statusText,
|
|
272
|
+
headers
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
async function purge(tags) {
|
|
276
|
+
const named = new Set(tags);
|
|
277
|
+
for (const [path, entry] of seen) {
|
|
278
|
+
if (entry.tags.some((tag) => named.has(tag)))
|
|
279
|
+
seen.delete(path);
|
|
280
|
+
}
|
|
281
|
+
return spool.invalidate(tags);
|
|
282
|
+
}
|
|
283
|
+
async function handle(request) {
|
|
284
|
+
const url = new URL(request.url);
|
|
285
|
+
if (url.pathname === PURGE_PATH)
|
|
286
|
+
return respondPurge(request, options.purgeSecret, purge);
|
|
287
|
+
const answered = await spool.respond(request);
|
|
288
|
+
if (answered)
|
|
289
|
+
return answered;
|
|
290
|
+
if (url.pathname === PATHS.worker && request.method === 'GET')
|
|
291
|
+
return spool.stub();
|
|
292
|
+
let upstream;
|
|
293
|
+
try {
|
|
294
|
+
upstream = await call(forward(request, url));
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
return new Response('bad gateway', {
|
|
298
|
+
status: 502,
|
|
299
|
+
headers: { 'content-type': 'text/plain', 'cache-control': 'no-store' }
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
if (trim && request.method === 'GET') {
|
|
303
|
+
const key = trimKey(url.href);
|
|
304
|
+
const entry = trim.styles[key];
|
|
305
|
+
if (entry &&
|
|
306
|
+
url.searchParams.get(TRIM_PARAM) !== '0' &&
|
|
307
|
+
upstream.status === 200 &&
|
|
308
|
+
(upstream.headers.get('content-type') ?? '').toLowerCase().includes('text/css')) {
|
|
309
|
+
const bytes = new Uint8Array(await upstream.arrayBuffer());
|
|
310
|
+
const headers = new Headers(upstream.headers);
|
|
311
|
+
headers.delete('content-length');
|
|
312
|
+
const match = (await sourceHash(bytes)) === entry.source;
|
|
313
|
+
if (match) {
|
|
314
|
+
weaken(headers);
|
|
315
|
+
stats.served++;
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
stats.mismatched++;
|
|
319
|
+
note('mismatch', { file: key });
|
|
320
|
+
}
|
|
321
|
+
upstream = new Response(match ? entry.css : bytes, {
|
|
322
|
+
status: 200,
|
|
323
|
+
statusText: upstream.statusText,
|
|
324
|
+
headers
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
else if (isHtml(upstream) && ![204, 205, 304].includes(upstream.status)) {
|
|
328
|
+
const bytes = new Uint8Array(await upstream.arrayBuffer());
|
|
329
|
+
// another charset is rewritten byte for byte; the guard inserts ASCII only
|
|
330
|
+
const utf8 = isUtf8(htmlCharset(upstream.headers, bytes));
|
|
331
|
+
const text = utf8 ? new TextDecoder().decode(bytes) : fromBytes(bytes);
|
|
332
|
+
const verdict = guarded(text, url.href);
|
|
333
|
+
const headers = new Headers(upstream.headers);
|
|
334
|
+
headers.delete('content-length');
|
|
335
|
+
if (verdict !== text)
|
|
336
|
+
weaken(headers);
|
|
337
|
+
const body = verdict === text ? bytes : utf8 ? verdict : toBytes(verdict);
|
|
338
|
+
upstream = new Response(body, {
|
|
339
|
+
status: upstream.status,
|
|
340
|
+
statusText: upstream.statusText,
|
|
341
|
+
headers
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const tags = readTags(upstream.headers);
|
|
346
|
+
const headers = tidy(upstream.headers, url);
|
|
347
|
+
const init = { status: upstream.status, statusText: upstream.statusText, headers };
|
|
348
|
+
// a new cookie may be a login or a logout (the held copies are another visitor's view)
|
|
349
|
+
if (setsPrivateCookie(upstream.headers, gate.publicCookies))
|
|
350
|
+
writeEnvelope(headers, { clear: ['*'] });
|
|
351
|
+
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
352
|
+
return new Response(upstream.body, init);
|
|
353
|
+
}
|
|
354
|
+
const hit = matchRoute(routes, url.pathname);
|
|
355
|
+
const data = hit.route.strategy === 'data';
|
|
356
|
+
const principal = hit.route.scope === 'principal';
|
|
357
|
+
const page = upstream.status === 200 && (data || isHtml(upstream));
|
|
358
|
+
const why = decline({
|
|
359
|
+
method: request.method,
|
|
360
|
+
search: url.search,
|
|
361
|
+
requestHeaders: request.headers,
|
|
362
|
+
response: upstream,
|
|
363
|
+
tags,
|
|
364
|
+
data,
|
|
365
|
+
principal
|
|
366
|
+
}, gate);
|
|
367
|
+
if (why)
|
|
368
|
+
return unfit(new Response(upstream.body, init), url.pathname, why, page);
|
|
369
|
+
const bytes = new Uint8Array(await upstream.arrayBuffer());
|
|
370
|
+
if (!data && !isUtf8(htmlCharset(upstream.headers, bytes))) {
|
|
371
|
+
return unfit(new Response(bytes, init), url.pathname, 'charset', page);
|
|
372
|
+
}
|
|
373
|
+
const text = new TextDecoder().decode(bytes);
|
|
374
|
+
headers.delete('content-length');
|
|
375
|
+
if (text.length > gate.maxBytes) {
|
|
376
|
+
return unfit(new Response(text, init), url.pathname, 'size', page);
|
|
377
|
+
}
|
|
378
|
+
const effective = tags.length > 0 ? tags : [pathTag(url.pathname)];
|
|
379
|
+
if (request.method === 'GET' && !principal)
|
|
380
|
+
await settle(url.pathname, text, effective);
|
|
381
|
+
pages.set(request, {
|
|
382
|
+
path: url.pathname,
|
|
383
|
+
page: Promise.resolve({ text, tags: effective })
|
|
384
|
+
});
|
|
385
|
+
return spool.decorate(request, new Response(text, init));
|
|
386
|
+
}
|
|
387
|
+
return Object.assign(handle, { spool, trim: stats });
|
|
388
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** where a CMS or a hook tells the proxy that tags changed */
|
|
2
|
+
export declare const PURGE_PATH = "/.spool/purge";
|
|
3
|
+
/** compares two secrets through HMAC digests of equal length, so neither the value nor its length shows in the time taken */
|
|
4
|
+
export declare function equalSecrets(a: string, b: string): Promise<boolean>;
|
|
5
|
+
/**
|
|
6
|
+
* Answers `POST /.spool/purge`. The caller sends `Authorization: Bearer <secret>` and a JSON body
|
|
7
|
+
* `{"tags": ["node:1", ...]}`. With no secret configured every call is refused, so a proxy that
|
|
8
|
+
* never asked for purges cannot be made to bump its vector.
|
|
9
|
+
*/
|
|
10
|
+
export declare function respondPurge(request: Request, secret: string | undefined, apply: (tags: string[]) => Promise<number>): Promise<Response>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { json } from '../server/sync.js';
|
|
2
|
+
/** where a CMS or a hook tells the proxy that tags changed */
|
|
3
|
+
export const PURGE_PATH = '/.spool/purge';
|
|
4
|
+
const MAX_BODY = 65_536;
|
|
5
|
+
const MAX_TAGS = 1000;
|
|
6
|
+
const MAX_TAG_LENGTH = 1024;
|
|
7
|
+
const encoder = new TextEncoder();
|
|
8
|
+
let macKey;
|
|
9
|
+
/** compares two secrets through HMAC digests of equal length, so neither the value nor its length shows in the time taken */
|
|
10
|
+
export async function equalSecrets(a, b) {
|
|
11
|
+
macKey ??= crypto.subtle.importKey('raw', crypto.getRandomValues(new Uint8Array(32)), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
12
|
+
const key = await macKey;
|
|
13
|
+
const digest = async (value) => new Uint8Array(await crypto.subtle.sign('HMAC', key, encoder.encode(value)));
|
|
14
|
+
const left = await digest(a);
|
|
15
|
+
const right = await digest(b);
|
|
16
|
+
let diff = 0;
|
|
17
|
+
for (let i = 0; i < left.length; i++)
|
|
18
|
+
diff |= (left[i] ?? 0) ^ (right[i] ?? 1);
|
|
19
|
+
return diff === 0;
|
|
20
|
+
}
|
|
21
|
+
function bearer(request) {
|
|
22
|
+
const match = /^Bearer\s+(.+)$/i.exec(request.headers.get('authorization') ?? '');
|
|
23
|
+
return match ? match[1].trim() : null;
|
|
24
|
+
}
|
|
25
|
+
function tagsOf(text) {
|
|
26
|
+
let body;
|
|
27
|
+
try {
|
|
28
|
+
body = JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const tags = body?.tags;
|
|
34
|
+
if (!Array.isArray(tags) || tags.length === 0 || tags.length > MAX_TAGS)
|
|
35
|
+
return null;
|
|
36
|
+
for (const tag of tags) {
|
|
37
|
+
if (typeof tag !== 'string' || tag === '' || tag.length > MAX_TAG_LENGTH)
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
return [...new Set(tags)];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Answers `POST /.spool/purge`. The caller sends `Authorization: Bearer <secret>` and a JSON body
|
|
44
|
+
* `{"tags": ["node:1", ...]}`. With no secret configured every call is refused, so a proxy that
|
|
45
|
+
* never asked for purges cannot be made to bump its vector.
|
|
46
|
+
*/
|
|
47
|
+
export async function respondPurge(request, secret, apply) {
|
|
48
|
+
if (request.method !== 'POST')
|
|
49
|
+
return json({ error: 'use POST' }, 405, { allow: 'POST' });
|
|
50
|
+
if (!secret)
|
|
51
|
+
return json({ error: 'purge is not configured' }, 403);
|
|
52
|
+
const given = bearer(request);
|
|
53
|
+
if (given === null || !(await equalSecrets(given, secret))) {
|
|
54
|
+
return json({ error: 'bad credentials' }, 401, { 'www-authenticate': 'Bearer' });
|
|
55
|
+
}
|
|
56
|
+
const length = Number(request.headers.get('content-length'));
|
|
57
|
+
if (Number.isFinite(length) && length > MAX_BODY)
|
|
58
|
+
return json({ error: 'body too large' }, 413);
|
|
59
|
+
const text = await request.text();
|
|
60
|
+
if (text.length > MAX_BODY)
|
|
61
|
+
return json({ error: 'body too large' }, 413);
|
|
62
|
+
const tags = tagsOf(text);
|
|
63
|
+
if (!tags)
|
|
64
|
+
return json({ error: 'send {"tags": [...]} with 1 to 1000 tags' }, 400);
|
|
65
|
+
return json({ vector: await apply(tags), tags: tags.length });
|
|
66
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** the tag headers {@link readTags} understands, lowercase */
|
|
2
|
+
export declare const TAG_HEADERS: readonly string[];
|
|
3
|
+
/** every tag the response names across all four headers, in first-seen order, without repeats */
|
|
4
|
+
export declare function readTags(headers: Headers): string[];
|
|
5
|
+
/** removes every tag header, so the browser never sees the site's cache vocabulary */
|
|
6
|
+
export declare function stripTags(headers: Headers): void;
|
|
7
|
+
/** the tag a page with no tag header depends on */
|
|
8
|
+
export declare function pathTag(path: string): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response headers that carry cache tags, with the separator each vendor documents:
|
|
3
|
+
* Cloudflare `Cache-Tag` takes commas, Fastly `Surrogate-Key` and Drupal `X-Drupal-Cache-Tags` take
|
|
4
|
+
* spaces, Varnish `xkey` takes spaces or commas. A header sent twice reaches `Headers.get` joined
|
|
5
|
+
* with a comma and a space, so the space-separated forms also split on that join.
|
|
6
|
+
*/
|
|
7
|
+
const SEPARATORS = {
|
|
8
|
+
'cache-tag': /\s*,\s*/,
|
|
9
|
+
'surrogate-key': /,?\s+/,
|
|
10
|
+
'x-drupal-cache-tags': /,?\s+/,
|
|
11
|
+
xkey: /[\s,]+/
|
|
12
|
+
};
|
|
13
|
+
/** the tag headers {@link readTags} understands, lowercase */
|
|
14
|
+
export const TAG_HEADERS = Object.keys(SEPARATORS);
|
|
15
|
+
/** every tag the response names across all four headers, in first-seen order, without repeats */
|
|
16
|
+
export function readTags(headers) {
|
|
17
|
+
const tags = new Set();
|
|
18
|
+
for (const [name, separator] of Object.entries(SEPARATORS)) {
|
|
19
|
+
const value = headers.get(name);
|
|
20
|
+
if (value === null)
|
|
21
|
+
continue;
|
|
22
|
+
for (const tag of value.trim().split(separator))
|
|
23
|
+
if (tag !== '')
|
|
24
|
+
tags.add(tag);
|
|
25
|
+
}
|
|
26
|
+
return [...tags];
|
|
27
|
+
}
|
|
28
|
+
/** removes every tag header, so the browser never sees the site's cache vocabulary */
|
|
29
|
+
export function stripTags(headers) {
|
|
30
|
+
for (const name of TAG_HEADERS)
|
|
31
|
+
headers.delete(name);
|
|
32
|
+
}
|
|
33
|
+
/** the tag a page with no tag header depends on */
|
|
34
|
+
export function pathTag(path) {
|
|
35
|
+
return `proxy:path:${path}`;
|
|
36
|
+
}
|