@drupflare/spool 0.1.0 → 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.
@@ -0,0 +1,74 @@
1
+ import { ProtocolError } from '../protocol/errors.js';
2
+ import { tagsOf } from '../trim/tokens.js';
3
+ import { appendAttributes, applyEdits, commentRanges, elementRanges, find, headRange, noscriptRanges, within } from './html.js';
4
+ /** applies the defaults to `options` and refuses a `skip` that is not a whole number of 0 or more */
5
+ export function resolveLazyOptions(options = {}) {
6
+ const skip = options.skip ?? 2;
7
+ if (!Number.isInteger(skip) || skip < 0) {
8
+ throw new ProtocolError('bad-route', 'images lazy.skip must be a whole number of 0 or more');
9
+ }
10
+ return { skip, iframes: options.iframes === true };
11
+ }
12
+ /** a tag that says it loads some other way: its own `loading` or `fetchpriority`, or a lazy-loading library's data attribute */
13
+ function opted(tag) {
14
+ if (find(tag, 'loading') || find(tag, 'fetchpriority'))
15
+ return true;
16
+ return tag.attributes.some((a) => a.name === 'data-src' || a.name === 'data-srcset' || a.name.startsWith('data-lazy'));
17
+ }
18
+ /** the `href` of every `<link rel="preload" as="image">` in `tags` */
19
+ function preloaded(tags) {
20
+ const hrefs = new Set();
21
+ for (const tag of tags) {
22
+ if (tag.name !== 'link')
23
+ continue;
24
+ const rel = (find(tag, 'rel')?.value ?? '').toLowerCase().split(/\s+/);
25
+ const as = (find(tag, 'as')?.value ?? '').trim().toLowerCase();
26
+ const href = find(tag, 'href')?.value.trim();
27
+ if (rel.includes('preload') && as === 'image' && href)
28
+ hrefs.add(href);
29
+ }
30
+ return hrefs;
31
+ }
32
+ /**
33
+ * Adds `loading="lazy"` and `decoding="async"` to the `<img>` tags of `html` after the first
34
+ * `skip`, and `loading="lazy"` to `<iframe>` tags with `iframes`, and says how many tags changed.
35
+ * Tags inside `<head>`, `<noscript>` or an HTML comment are neither counted nor edited. A tag that
36
+ * has `loading`, `fetchpriority`, `data-src`, `data-srcset` or a `data-lazy*` attribute, that sits
37
+ * inside a `<header>`, or whose `src` a `<link rel="preload" as="image">` names, is counted toward
38
+ * `skip` and left alone. A tag that already has `decoding` keeps it. Everything outside the edited
39
+ * start tags stays byte for byte, and the edits are ASCII.
40
+ */
41
+ export function rewriteLazy(html, options = {}) {
42
+ const { skip, iframes } = resolveLazyOptions(options);
43
+ const tags = tagsOf(html);
44
+ const comments = commentRanges(html);
45
+ const noscripts = noscriptRanges(html, tags);
46
+ const head = headRange(html, tags);
47
+ const headers = elementRanges(html, tags, 'header');
48
+ const preloads = preloaded(tags);
49
+ const edits = [];
50
+ let seen = 0;
51
+ let rewritten = 0;
52
+ for (const tag of tags) {
53
+ if (tag.name !== 'img' && !(iframes && tag.name === 'iframe'))
54
+ continue;
55
+ if (html[tag.end] !== '>')
56
+ continue;
57
+ if (within(comments, tag.start) || within(noscripts, tag.start))
58
+ continue;
59
+ if (head && tag.start >= head[0] && tag.start < head[1])
60
+ continue;
61
+ if (tag.name === 'img' && seen++ < skip)
62
+ continue;
63
+ if (opted(tag) || within(headers, tag.start))
64
+ continue;
65
+ if (tag.name === 'img' && preloads.has((find(tag, 'src')?.value ?? '').trim()))
66
+ continue;
67
+ const added = ['loading="lazy"'];
68
+ if (tag.name === 'img' && !find(tag, 'decoding'))
69
+ added.push('decoding="async"');
70
+ edits.push(appendAttributes(html, tag, added));
71
+ rewritten++;
72
+ }
73
+ return { html: applyEdits(html, edits), rewritten };
74
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * What `images.responsive` adds to an `<img>`: one `srcset` candidate per width, each the image's
3
+ * own URL with `spool-w=<width>`, and `sizes` when the tag has none. `widths` is a fixed list, since
4
+ * the proxy only answers a width from it; default 320, 480, 640, 960, 1280 and 1920. `sizes` defaults
5
+ * to `100vw`, which asks for the widest candidate the screen can use, so name the real layout width
6
+ * here for a site whose images fill less of the page.
7
+ */
8
+ export type ResponsiveOptions = {
9
+ widths?: readonly number[];
10
+ sizes?: string;
11
+ };
12
+ /** `ResponsiveOptions` with every default applied, the widths sorted and unique */
13
+ export type ResolvedResponsiveOptions = {
14
+ widths: readonly number[];
15
+ sizes: string;
16
+ };
17
+ /** the query parameter that names a width; the proxy removes it before the request reaches the origin */
18
+ export declare const RESPONSIVE_PARAM = "spool-w";
19
+ /** applies the defaults to `options` and refuses a width or a `sizes` it cannot use with a `ProtocolError` */
20
+ export declare function resolveResponsiveOptions(options?: ResponsiveOptions): ResolvedResponsiveOptions;
21
+ /**
22
+ * The query string with every `spool-w` parameter removed, and the width the last one named when it
23
+ * is one of `widths`. A value that is not a listed width is dropped without a width, so the request
24
+ * is answered as if it had none.
25
+ */
26
+ export declare function takeWidth(search: string, widths: readonly number[]): {
27
+ search: string;
28
+ width?: number;
29
+ };
30
+ /**
31
+ * Adds `srcset` (and `sizes`, when absent) to the `<img>` tags of `html` whose `src` is a
32
+ * same-origin URL that `paths` matches, and says how many tags changed. A tag that has `srcset`,
33
+ * `data-src` or `data-srcset`, sits inside `<picture>`, `<noscript>`, `<head>` or an HTML comment,
34
+ * names no usable `src` (empty, a data URL, another origin, a path `paths` does not match, one that
35
+ * already carries `spool-w`, or one with a space, comma, quote or fragment in it) is left alone. The
36
+ * original `src` stays as the fallback. `pageUrl` resolves a relative `src`. Everything outside the
37
+ * edited start tags stays byte for byte, and the edits are ASCII.
38
+ */
39
+ export declare function rewriteResponsive(html: string, options: ResponsiveOptions, context: {
40
+ pageUrl: string;
41
+ paths: RegExp;
42
+ }): {
43
+ html: string;
44
+ rewritten: number;
45
+ };
@@ -0,0 +1,98 @@
1
+ import { ProtocolError } from '../protocol/errors.js';
2
+ import { tagsOf } from '../trim/tokens.js';
3
+ import { appendAttributes, applyEdits, commentRanges, elementRanges, find, headRange, noscriptRanges, within } from './html.js';
4
+ /** the query parameter that names a width; the proxy removes it before the request reaches the origin */
5
+ export const RESPONSIVE_PARAM = 'spool-w';
6
+ const DEFAULT_WIDTHS = [320, 480, 640, 960, 1280, 1920];
7
+ const MAX_WIDTH = 16384;
8
+ /** applies the defaults to `options` and refuses a width or a `sizes` it cannot use with a `ProtocolError` */
9
+ export function resolveResponsiveOptions(options = {}) {
10
+ const widths = options.widths ?? DEFAULT_WIDTHS;
11
+ if (widths.length === 0 ||
12
+ !widths.every((w) => Number.isInteger(w) && w >= 1 && w <= MAX_WIDTH)) {
13
+ throw new ProtocolError('bad-route', `images responsive.widths must be a list of integers from 1 to ${MAX_WIDTH}`);
14
+ }
15
+ const sizes = (options.sizes ?? '100vw').trim();
16
+ if (sizes === '' || /[\u0000-\u001f]/.test(sizes)) {
17
+ throw new ProtocolError('bad-route', 'images responsive.sizes must be a non-empty single line');
18
+ }
19
+ return { widths: [...new Set(widths)].sort((a, b) => a - b), sizes };
20
+ }
21
+ /**
22
+ * The query string with every `spool-w` parameter removed, and the width the last one named when it
23
+ * is one of `widths`. A value that is not a listed width is dropped without a width, so the request
24
+ * is answered as if it had none.
25
+ */
26
+ export function takeWidth(search, widths) {
27
+ if (!search.includes(RESPONSIVE_PARAM))
28
+ return { search };
29
+ const kept = [];
30
+ let width;
31
+ for (const piece of search.slice(1).split('&')) {
32
+ const [name, value] = piece.split('=');
33
+ if (name !== RESPONSIVE_PARAM) {
34
+ kept.push(piece);
35
+ }
36
+ else if (value !== undefined &&
37
+ /^\d{1,5}$/.test(value) &&
38
+ widths.includes(Number(value))) {
39
+ width = Number(value);
40
+ }
41
+ }
42
+ const rest = kept.length > 0 ? `?${kept.join('&')}` : '';
43
+ return width === undefined ? { search: rest } : { search: rest, width };
44
+ }
45
+ const escape = (text) => text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;');
46
+ /**
47
+ * Adds `srcset` (and `sizes`, when absent) to the `<img>` tags of `html` whose `src` is a
48
+ * same-origin URL that `paths` matches, and says how many tags changed. A tag that has `srcset`,
49
+ * `data-src` or `data-srcset`, sits inside `<picture>`, `<noscript>`, `<head>` or an HTML comment,
50
+ * names no usable `src` (empty, a data URL, another origin, a path `paths` does not match, one that
51
+ * already carries `spool-w`, or one with a space, comma, quote or fragment in it) is left alone. The
52
+ * original `src` stays as the fallback. `pageUrl` resolves a relative `src`. Everything outside the
53
+ * edited start tags stays byte for byte, and the edits are ASCII.
54
+ */
55
+ export function rewriteResponsive(html, options, context) {
56
+ const { widths, sizes } = resolveResponsiveOptions(options);
57
+ const page = new URL(context.pageUrl);
58
+ const tags = tagsOf(html);
59
+ const comments = commentRanges(html);
60
+ const noscripts = noscriptRanges(html, tags);
61
+ const pictures = elementRanges(html, tags, 'picture');
62
+ const head = headRange(html, tags);
63
+ const edits = [];
64
+ for (const tag of tags) {
65
+ if (tag.name !== 'img' || html[tag.end] !== '>')
66
+ continue;
67
+ if (within(comments, tag.start) || within(noscripts, tag.start))
68
+ continue;
69
+ if (within(pictures, tag.start))
70
+ continue;
71
+ if (head && tag.start >= head[0] && tag.start < head[1])
72
+ continue;
73
+ if (find(tag, 'srcset') || find(tag, 'data-src') || find(tag, 'data-srcset'))
74
+ continue;
75
+ const src = (find(tag, 'src')?.value ?? '').trim();
76
+ if (src === '' || /[\s,"'<>#]/.test(src))
77
+ continue;
78
+ let target;
79
+ try {
80
+ target = new URL(src.replace(/&amp;/gi, '&'), page);
81
+ }
82
+ catch {
83
+ continue;
84
+ }
85
+ if (target.origin !== page.origin || target.searchParams.has(RESPONSIVE_PARAM))
86
+ continue;
87
+ context.paths.lastIndex = 0;
88
+ if (!context.paths.test(target.pathname))
89
+ continue;
90
+ const joint = src.includes('?') ? '&amp;' : '?';
91
+ const candidates = widths.map((w) => `${src}${joint}${RESPONSIVE_PARAM}=${w} ${w}w`);
92
+ const added = [`srcset="${candidates.join(', ')}"`];
93
+ if (!find(tag, 'sizes'))
94
+ added.push(`sizes="${escape(sizes)}"`);
95
+ edits.push(appendAttributes(html, tag, added));
96
+ }
97
+ return { html: applyEdits(html, edits), rewritten: edits.length };
98
+ }
@@ -0,0 +1,30 @@
1
+ import type { MediaSignals } from './headers.js';
2
+ /**
3
+ * What `spoolProxy({ video })` does to `<video>` start tags. `preload` is the value to set, or
4
+ * `false` to leave the attribute alone. `autoplay: 'defer'` removes the `autoplay` attribute, marks
5
+ * the tag `data-spool-autoplay`, adds `controls` so a visitor can start the video, and sets
6
+ * `preload` to `none` unless the option is `metadata`. With `'keep'` the `autoplay` attribute stays,
7
+ * and a video that autoplays loads its file whatever `preload` says. `when` limits the rewrite to a
8
+ * visitor who sent `Save-Data: on` (`save-data`) or who also has an `ECT` of `slow-2g`, `2g` or
9
+ * `3g` (`slow`). Defaults: `preload: 'none'`, `autoplay: 'keep'`, `when: 'always'`.
10
+ */
11
+ export type VideoOptions = {
12
+ preload?: 'none' | 'metadata' | false;
13
+ autoplay?: 'keep' | 'defer';
14
+ when?: 'always' | 'save-data' | 'slow';
15
+ };
16
+ /** `VideoOptions` with every default applied */
17
+ export type ResolvedVideoOptions = Required<VideoOptions>;
18
+ /** applies the defaults to `options` and refuses an unknown value with `ProtocolError('bad-route')` */
19
+ export declare function resolveVideoOptions(options?: VideoOptions): ResolvedVideoOptions;
20
+ /**
21
+ * Rewrites the `<video>` start tags of `html` as `options` ask and says how many tags changed.
22
+ * It reads tags with `tagsOf` and skips a tag inside an HTML comment, a tag that never closes, and
23
+ * every element that is not `<video>`. A tag that already says what the options ask for is not
24
+ * counted. Everything outside the edited start tags stays byte for byte, and the edits are ASCII,
25
+ * so a page in another charset read one byte per character comes back the same way.
26
+ */
27
+ export declare function rewriteVideo(html: string, options: VideoOptions, signals: MediaSignals): {
28
+ html: string;
29
+ rewritten: number;
30
+ };
@@ -0,0 +1,75 @@
1
+ import { ProtocolError } from '../protocol/errors.js';
2
+ import { appendAttributes, applyEdits, find, setValue, tagsNamed } from './html.js';
3
+ /** applies the defaults to `options` and refuses an unknown value with `ProtocolError('bad-route')` */
4
+ export function resolveVideoOptions(options = {}) {
5
+ const resolved = {
6
+ preload: options.preload ?? 'none',
7
+ autoplay: options.autoplay ?? 'keep',
8
+ when: options.when ?? 'always'
9
+ };
10
+ if (![false, 'none', 'metadata'].includes(resolved.preload)) {
11
+ throw new ProtocolError('bad-route', `video preload must be 'none', 'metadata' or false`);
12
+ }
13
+ if (!['keep', 'defer'].includes(resolved.autoplay)) {
14
+ throw new ProtocolError('bad-route', `video autoplay must be 'keep' or 'defer'`);
15
+ }
16
+ if (!['always', 'save-data', 'slow'].includes(resolved.when)) {
17
+ throw new ProtocolError('bad-route', `video when must be 'always', 'save-data' or 'slow'`);
18
+ }
19
+ return resolved;
20
+ }
21
+ function editsFor(html, tag, options) {
22
+ const edits = [];
23
+ const autoplay = find(tag, 'autoplay');
24
+ const defer = options.autoplay === 'defer' && autoplay !== undefined;
25
+ const target = defer ? (options.preload === 'metadata' ? 'metadata' : 'none') : options.preload;
26
+ const added = [];
27
+ if (defer) {
28
+ if (find(tag, 'data-spool-autoplay')) {
29
+ let from = autoplay.start;
30
+ while (from > tag.start && /\s/.test(html[from - 1]))
31
+ from--;
32
+ edits.push({ at: from, to: autoplay.end, text: '' });
33
+ }
34
+ else {
35
+ edits.push({ at: autoplay.start, to: autoplay.end, text: 'data-spool-autoplay' });
36
+ }
37
+ if (!find(tag, 'controls'))
38
+ added.push('controls');
39
+ }
40
+ if (target !== false) {
41
+ const preload = find(tag, 'preload');
42
+ if (!preload)
43
+ added.push(`preload="${target}"`);
44
+ else if (preload.value.trim().toLowerCase() !== target) {
45
+ edits.push(setValue(html, preload, target));
46
+ }
47
+ }
48
+ if (added.length > 0)
49
+ edits.push(appendAttributes(html, tag, added));
50
+ return edits;
51
+ }
52
+ /**
53
+ * Rewrites the `<video>` start tags of `html` as `options` ask and says how many tags changed.
54
+ * It reads tags with `tagsOf` and skips a tag inside an HTML comment, a tag that never closes, and
55
+ * every element that is not `<video>`. A tag that already says what the options ask for is not
56
+ * counted. Everything outside the edited start tags stays byte for byte, and the edits are ASCII,
57
+ * so a page in another charset read one byte per character comes back the same way.
58
+ */
59
+ export function rewriteVideo(html, options, signals) {
60
+ const resolved = resolveVideoOptions(options);
61
+ const applies = resolved.when === 'always' ||
62
+ (resolved.when === 'save-data' ? signals.saveData : signals.slow || signals.saveData);
63
+ if (!applies)
64
+ return { html, rewritten: 0 };
65
+ const edits = [];
66
+ let rewritten = 0;
67
+ for (const tag of tagsNamed(html, 'video')) {
68
+ const tagEdits = editsFor(html, tag, resolved);
69
+ if (tagEdits.length === 0)
70
+ continue;
71
+ rewritten++;
72
+ edits.push(...tagEdits);
73
+ }
74
+ return { html: applyEdits(html, edits), rewritten };
75
+ }
@@ -0,0 +1,58 @@
1
+ import { type Gate } from './eligible.js';
2
+ /** the content codings of RFC 9842: Brotli (`dcb`) and Zstandard (`dcz`) against a dictionary */
3
+ export type DictionaryFormat = 'dcb' | 'dcz';
4
+ /**
5
+ * Compresses `input` against `dictionary` and returns the compressed stream only. spool adds the
6
+ * coding's header (the magic bytes and the SHA-256 of the dictionary). For `dcz` the dictionary is a
7
+ * raw content dictionary and the window must stay at or under 8 MiB or 1.25 times the dictionary,
8
+ * whichever is larger. For `dcb` it is a Brotli prefix dictionary. It throws for a format it cannot
9
+ * produce, and the proxy then answers with the plain body.
10
+ */
11
+ export type DictionaryEncoder = (dictionary: Uint8Array, input: Uint8Array, format: DictionaryFormat) => Promise<Uint8Array>;
12
+ /** what `spoolProxy({ dictionary })` takes */
13
+ export type DictionaryOptions = {
14
+ /** produces the compressed stream; see {@link DictionaryEncoder} */
15
+ encoder: DictionaryEncoder;
16
+ /** the paths that become and use dictionaries, tested against the pathname; default `/\.(?:css|m?js)$/i` */
17
+ match?: RegExp;
18
+ /**
19
+ * Which of those responses register as dictionaries, as a function of the pathname, a regular
20
+ * expression or a list of them; default every one `match` accepts. The others get no
21
+ * `Use-As-Dictionary`, so the browser holds only the bases you name, and they are still encoded
22
+ * against a base the request names.
23
+ */
24
+ base?: RegExp | readonly RegExp[] | ((pathname: string) => boolean);
25
+ /** largest body kept as a dictionary or encoded, in bytes; default 2 MiB */
26
+ maxBytes?: number;
27
+ /** the codings offered, in order of preference, when the request accepts them; default `['dcz']` */
28
+ formats?: readonly DictionaryFormat[];
29
+ /** bytes of dictionaries kept in memory, the least recently used going first; default 16 MiB */
30
+ cacheBytes?: number;
31
+ };
32
+ /**
33
+ * What the proxy counted since it started: `registered` is the dictionaries stored (new content
34
+ * hashes), `encoded` the responses sent as `dcb` or `dcz`, `bytesIn` and `bytesOut` the plain and
35
+ * encoded sizes of those responses, and `skipped` the eligible responses left plain by reason:
36
+ * `unavailable` (the request named no dictionary the proxy holds, or accepted no coding offered),
37
+ * `size`, `larger` (the encoded body was not smaller), `failed` (the encoder threw), `range` and
38
+ * `pattern` (the path cannot be written as a `match` pattern).
39
+ */
40
+ export type DictionaryStats = {
41
+ registered: number;
42
+ encoded: number;
43
+ bytesIn: number;
44
+ bytesOut: number;
45
+ skipped: Record<string, number>;
46
+ };
47
+ /** the body of a `dcb` or `dcz` response: the coding's magic bytes, the dictionary's SHA-256, then the stream */
48
+ export declare function frameDictionaryBody(format: DictionaryFormat, hash: Uint8Array, stream: Uint8Array): Uint8Array;
49
+ /** the state `spoolProxy` keeps for `dictionary`: checked options, the dictionaries and the counters */
50
+ export type Dictionaries = {
51
+ readonly stats: DictionaryStats;
52
+ /** whether a response could take part, by its content type and the request's method and path */
53
+ applies(request: Request, response: Response): boolean;
54
+ /** answers one origin response for `request`, registering it and encoding it when the request names a stored dictionary */
55
+ apply(request: Request, response: Response): Promise<Response>;
56
+ };
57
+ /** applies the defaults and checks the values; `ProtocolError('bad-route')` for one out of range */
58
+ export declare function createDictionaries(options: DictionaryOptions, gate: Gate): Dictionaries;
@@ -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
+ }
@@ -1,3 +1,4 @@
1
+ export * from './dictionary.js';
1
2
  export * from './eligible.js';
2
3
  export * from './proxy.js';
3
4
  export * from './purge.js';
@@ -1,3 +1,4 @@
1
+ export * from './dictionary.js';
1
2
  export * from './eligible.js';
2
3
  export * from './proxy.js';
3
4
  export * from './purge.js';