@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,134 @@
|
|
|
1
|
+
import { trimCss } from './css.js';
|
|
2
|
+
import { decodeEntities, isStylesheetLink, pageAssets, pageTokens, scriptTokens, tagsOf } from './tokens.js';
|
|
3
|
+
/** the query parameter that asks the proxy for the full stylesheet */
|
|
4
|
+
export const TRIM_PARAM = 'spool-trim';
|
|
5
|
+
const BASE = 'http://spool-trim.invalid';
|
|
6
|
+
/**
|
|
7
|
+
* A query string with the {@link TRIM_PARAM} parameter removed and every other parameter left
|
|
8
|
+
* byte for byte, so the origin gets the URL the page named.
|
|
9
|
+
*/
|
|
10
|
+
export function stripTrimParam(search) {
|
|
11
|
+
if (!search.includes(TRIM_PARAM))
|
|
12
|
+
return search;
|
|
13
|
+
const kept = search
|
|
14
|
+
.slice(1)
|
|
15
|
+
.split('&')
|
|
16
|
+
.filter((piece) => piece.split('=')[0] !== TRIM_PARAM);
|
|
17
|
+
return kept.length > 0 ? `?${kept.join('&')}` : '';
|
|
18
|
+
}
|
|
19
|
+
/** `pathname + search` of a URL with the {@link TRIM_PARAM} parameter dropped, the manifest key */
|
|
20
|
+
export function trimKey(url, base = BASE) {
|
|
21
|
+
const parsed = new URL(url, base);
|
|
22
|
+
return parsed.pathname + stripTrimParam(parsed.search);
|
|
23
|
+
}
|
|
24
|
+
/** `sha256-<base64>` of `bytes` */
|
|
25
|
+
export async function sourceHash(bytes) {
|
|
26
|
+
const data = typeof bytes === 'string' ? new TextEncoder().encode(bytes) : bytes;
|
|
27
|
+
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
|
|
28
|
+
return `sha256-${btoa(String.fromCharCode(...hash))}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Trims every stylesheet against the union of the tokens of all `pages` and all `scripts`, so one
|
|
32
|
+
* file keeps one URL for the whole site. A stylesheet that loses no rule gets no entry. `pages` is
|
|
33
|
+
* HTML, `scripts` the same-origin JavaScript the pages load, `styles` the same-origin CSS.
|
|
34
|
+
*
|
|
35
|
+
* `foreignScripts` are scripts from other origins that the pages load, such as a widget that
|
|
36
|
+
* builds markup. Their words widen the tokens, because a class such a script adds is in the DOM
|
|
37
|
+
* whatever origin served it. The guard never looks at their URLs, so a vendor that starts adding a
|
|
38
|
+
* new class after the crawl is not caught.
|
|
39
|
+
*/
|
|
40
|
+
export async function buildTrimManifest(input) {
|
|
41
|
+
const tokens = new Set();
|
|
42
|
+
for (const html of input.pages)
|
|
43
|
+
for (const token of pageTokens(html))
|
|
44
|
+
tokens.add(token);
|
|
45
|
+
for (const { text } of [...input.scripts, ...(input.foreignScripts ?? [])]) {
|
|
46
|
+
for (const token of scriptTokens(text))
|
|
47
|
+
tokens.add(token);
|
|
48
|
+
}
|
|
49
|
+
const scripts = [...new Set(input.scripts.map((s) => trimKey(s.url)))].sort();
|
|
50
|
+
const styles = {};
|
|
51
|
+
for (const style of input.styles) {
|
|
52
|
+
const key = trimKey(style.url);
|
|
53
|
+
if (key in styles)
|
|
54
|
+
continue;
|
|
55
|
+
const { css, removedRules, missing } = trimCss(style.text, tokens);
|
|
56
|
+
if (removedRules === 0)
|
|
57
|
+
continue;
|
|
58
|
+
styles[key] = { source: await sourceHash(style.text), css, missing, scripts };
|
|
59
|
+
}
|
|
60
|
+
return { schema: 1, styles };
|
|
61
|
+
}
|
|
62
|
+
const known = new WeakMap();
|
|
63
|
+
function scriptsOf(entry) {
|
|
64
|
+
let set = known.get(entry);
|
|
65
|
+
if (!set)
|
|
66
|
+
known.set(entry, (set = new Set(entry.scripts)));
|
|
67
|
+
return set;
|
|
68
|
+
}
|
|
69
|
+
function sameOriginKey(href, page) {
|
|
70
|
+
let target;
|
|
71
|
+
try {
|
|
72
|
+
target = new URL(href, page);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return target.origin === page.origin ? trimKey(target.href) : null;
|
|
78
|
+
}
|
|
79
|
+
function withParam(raw) {
|
|
80
|
+
const hash = raw.indexOf('#');
|
|
81
|
+
const head = hash < 0 ? raw : raw.slice(0, hash);
|
|
82
|
+
const tail = hash < 0 ? '' : raw.slice(hash);
|
|
83
|
+
const url = `${head}${head.includes('?') ? '&' : '?'}${TRIM_PARAM}=0${tail}`;
|
|
84
|
+
// the raw value is already in entity form, and may hold a quote from a single-quoted or bare attribute
|
|
85
|
+
return url.replaceAll('"', '"');
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Decides whether `html` may use the trimmed stylesheets it links. It may not when one of its
|
|
89
|
+
* tokens is in a linked entry's `missing` set, or when it loads a same-origin script the manifest
|
|
90
|
+
* was not built with. Then each link to a trimmed file gets `spool-trim=0`, which the proxy
|
|
91
|
+
* answers with the full file. It cannot see a class a third-party script adds, or markup injected
|
|
92
|
+
* after load from a page the manifest never covered.
|
|
93
|
+
*/
|
|
94
|
+
export function guardPage(html, manifest, pageUrl) {
|
|
95
|
+
const page = new URL(pageUrl);
|
|
96
|
+
const assets = pageAssets(html);
|
|
97
|
+
const files = [
|
|
98
|
+
...new Set(assets.styles
|
|
99
|
+
.map((href) => sameOriginKey(href, page))
|
|
100
|
+
.filter((key) => key !== null && key in manifest.styles))
|
|
101
|
+
];
|
|
102
|
+
if (files.length === 0)
|
|
103
|
+
return { html, fired: false, files };
|
|
104
|
+
const entries = files.map((key) => manifest.styles[key]);
|
|
105
|
+
// any case counts, as in trimCss, for a quirks-mode page
|
|
106
|
+
const tokens = new Set([...pageTokens(html)].map((token) => token.toLowerCase()));
|
|
107
|
+
let reason;
|
|
108
|
+
if (entries.some((entry) => entry.missing.some((token) => tokens.has(token.toLowerCase())))) {
|
|
109
|
+
reason = 'token';
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const scripts = assets.scripts
|
|
113
|
+
.map((src) => sameOriginKey(src, page))
|
|
114
|
+
.filter((key) => key !== null);
|
|
115
|
+
if (entries.some((entry) => scripts.some((key) => !scriptsOf(entry).has(key)))) {
|
|
116
|
+
reason = 'script';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (!reason)
|
|
120
|
+
return { html, fired: false, files };
|
|
121
|
+
let rewritten = '';
|
|
122
|
+
let from = 0;
|
|
123
|
+
for (const tag of tagsOf(html)) {
|
|
124
|
+
if (!isStylesheetLink(tag))
|
|
125
|
+
continue;
|
|
126
|
+
const href = tag.attributes.find((a) => a.name === 'href');
|
|
127
|
+
const key = sameOriginKey(decodeEntities(href.value), page);
|
|
128
|
+
if (key === null || !(key in manifest.styles))
|
|
129
|
+
continue;
|
|
130
|
+
rewritten += `${html.slice(from, href.start)}href="${withParam(href.value)}"`;
|
|
131
|
+
from = href.end;
|
|
132
|
+
}
|
|
133
|
+
return { html: rewritten + html.slice(from), fired: true, reason, files };
|
|
134
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** `text` with the five named entities and numeric character references decoded */
|
|
2
|
+
export declare function decodeEntities(text: string): string;
|
|
3
|
+
/** one attribute of a start tag: its lowercase name, its raw value and where it sits in the HTML */
|
|
4
|
+
export type Attribute = {
|
|
5
|
+
name: string;
|
|
6
|
+
value: string;
|
|
7
|
+
start: number;
|
|
8
|
+
end: number;
|
|
9
|
+
};
|
|
10
|
+
/** a start tag, with the text between a `<script>` or `<style>` and its end tag as `body` */
|
|
11
|
+
export type Tag = {
|
|
12
|
+
/** lowercase */
|
|
13
|
+
name: string;
|
|
14
|
+
attributes: Attribute[];
|
|
15
|
+
body?: string;
|
|
16
|
+
};
|
|
17
|
+
/** the value of the first attribute called `name` on `tag` */
|
|
18
|
+
export declare const attr: (tag: Tag, name: string) => string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* The start tags of `html`, read the way a browser reads them: an attribute name is anything up to
|
|
21
|
+
* whitespace, `/`, `>` or `=`, so a stray quote in a name (`<a href="x" 13715"="">`) is part of
|
|
22
|
+
* that name and does not open a value. A quote opens a value only after `=`. The text of a
|
|
23
|
+
* `<script>` or `<style>` is not scanned for tags. Comments are not skipped: a tag inside one adds
|
|
24
|
+
* tokens, which only keeps more rules.
|
|
25
|
+
*/
|
|
26
|
+
export declare function tagsOf(html: string): Tag[];
|
|
27
|
+
/**
|
|
28
|
+
* The words a page can match a selector with: tag names (lowercase), attribute names, class
|
|
29
|
+
* values and ids from the markup, and every word of an inline script, which covers
|
|
30
|
+
* `drupal-settings-json` and other JSON blocks as well as code that adds classes. The set is
|
|
31
|
+
* deliberately wide. A token that is present only keeps a rule, so extra words cost bytes and
|
|
32
|
+
* never correctness.
|
|
33
|
+
*/
|
|
34
|
+
export declare function pageTokens(html: string): Set<string>;
|
|
35
|
+
/** every word in a script's source, with a lowercase copy of each, which covers classes JS adds */
|
|
36
|
+
export declare function scriptTokens(js: string): Set<string>;
|
|
37
|
+
/** whether `tag` is a `<link rel=stylesheet>` with an `href` */
|
|
38
|
+
export declare function isStylesheetLink(tag: Tag): boolean;
|
|
39
|
+
/** the decoded `href` of every `<link rel=stylesheet>` and `src` of every external script */
|
|
40
|
+
export declare function pageAssets(html: string): {
|
|
41
|
+
styles: string[];
|
|
42
|
+
scripts: string[];
|
|
43
|
+
};
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const WORD = /[A-Za-z0-9_-]+/g;
|
|
2
|
+
const NAME = /[a-zA-Z][^\s/>]*/y;
|
|
3
|
+
const NAMED = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" };
|
|
4
|
+
/** `text` with the five named entities and numeric character references decoded */
|
|
5
|
+
export function decodeEntities(text) {
|
|
6
|
+
return text.replace(/&(?:#(\d{1,7})|#x([0-9a-f]{1,6})|(amp|lt|gt|quot|apos));/gi, (whole, dec, hex, name) => {
|
|
7
|
+
if (name)
|
|
8
|
+
return NAMED[name.toLowerCase()];
|
|
9
|
+
const code = dec ? Number.parseInt(dec, 10) : Number.parseInt(hex, 16);
|
|
10
|
+
return code > 0 && code <= 0x10ffff ? String.fromCodePoint(code) : whole;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function addWords(into, text, fold) {
|
|
14
|
+
for (const [word] of text.matchAll(WORD)) {
|
|
15
|
+
into.add(word);
|
|
16
|
+
if (fold)
|
|
17
|
+
into.add(word.toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** the value of the first attribute called `name` on `tag` */
|
|
21
|
+
export const attr = (tag, name) => tag.attributes.find((a) => a.name === name)?.value;
|
|
22
|
+
/**
|
|
23
|
+
* The start tags of `html`, read the way a browser reads them: an attribute name is anything up to
|
|
24
|
+
* whitespace, `/`, `>` or `=`, so a stray quote in a name (`<a href="x" 13715"="">`) is part of
|
|
25
|
+
* that name and does not open a value. A quote opens a value only after `=`. The text of a
|
|
26
|
+
* `<script>` or `<style>` is not scanned for tags. Comments are not skipped: a tag inside one adds
|
|
27
|
+
* tokens, which only keeps more rules.
|
|
28
|
+
*/
|
|
29
|
+
export function tagsOf(html) {
|
|
30
|
+
const tags = [];
|
|
31
|
+
let at = 0;
|
|
32
|
+
while (at < html.length) {
|
|
33
|
+
const open = html.indexOf('<', at);
|
|
34
|
+
if (open < 0)
|
|
35
|
+
break;
|
|
36
|
+
NAME.lastIndex = open + 1;
|
|
37
|
+
const found = NAME.exec(html);
|
|
38
|
+
if (!found) {
|
|
39
|
+
at = open + 1;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const attributes = [];
|
|
43
|
+
let i = open + 1 + found[0].length;
|
|
44
|
+
while (i < html.length && html[i] !== '>') {
|
|
45
|
+
if (/[\s/]/.test(html[i])) {
|
|
46
|
+
i++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const start = i;
|
|
50
|
+
i++;
|
|
51
|
+
while (i < html.length && !/[\s/>=]/.test(html[i]))
|
|
52
|
+
i++;
|
|
53
|
+
const name = html.slice(start, i).toLowerCase();
|
|
54
|
+
let after = i;
|
|
55
|
+
while (after < html.length && /\s/.test(html[after]))
|
|
56
|
+
after++;
|
|
57
|
+
let value = '';
|
|
58
|
+
if (html[after] === '=') {
|
|
59
|
+
after++;
|
|
60
|
+
while (after < html.length && /\s/.test(html[after]))
|
|
61
|
+
after++;
|
|
62
|
+
const quote = html[after];
|
|
63
|
+
if (quote === '"' || quote === "'") {
|
|
64
|
+
const close = html.indexOf(quote, after + 1);
|
|
65
|
+
const stop = close < 0 ? html.length : close;
|
|
66
|
+
value = html.slice(after + 1, stop);
|
|
67
|
+
i = close < 0 ? html.length : close + 1;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
let stop = after;
|
|
71
|
+
while (stop < html.length && !/[\s>]/.test(html[stop]))
|
|
72
|
+
stop++;
|
|
73
|
+
value = html.slice(after, stop);
|
|
74
|
+
i = stop;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
attributes.push({ name, value, start, end: i });
|
|
78
|
+
}
|
|
79
|
+
const tag = { name: found[0].toLowerCase(), attributes };
|
|
80
|
+
at = Math.min(html.length, i + 1);
|
|
81
|
+
if (tag.name === 'script' || tag.name === 'style') {
|
|
82
|
+
const end = new RegExp(`</${tag.name}\\s*>`, 'gi');
|
|
83
|
+
end.lastIndex = at;
|
|
84
|
+
const close = end.exec(html);
|
|
85
|
+
tag.body = html.slice(at, close ? close.index : html.length);
|
|
86
|
+
at = close ? close.index + close[0].length : html.length;
|
|
87
|
+
}
|
|
88
|
+
tags.push(tag);
|
|
89
|
+
}
|
|
90
|
+
return tags;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The words a page can match a selector with: tag names (lowercase), attribute names, class
|
|
94
|
+
* values and ids from the markup, and every word of an inline script, which covers
|
|
95
|
+
* `drupal-settings-json` and other JSON blocks as well as code that adds classes. The set is
|
|
96
|
+
* deliberately wide. A token that is present only keeps a rule, so extra words cost bytes and
|
|
97
|
+
* never correctness.
|
|
98
|
+
*/
|
|
99
|
+
export function pageTokens(html) {
|
|
100
|
+
const tokens = new Set();
|
|
101
|
+
for (const tag of tagsOf(html)) {
|
|
102
|
+
tokens.add(tag.name);
|
|
103
|
+
if (tag.name === 'script' && tag.body)
|
|
104
|
+
addWords(tokens, tag.body, true);
|
|
105
|
+
for (const { name, value } of tag.attributes) {
|
|
106
|
+
tokens.add(name);
|
|
107
|
+
if (name === 'class' || name === 'id') {
|
|
108
|
+
if (name === 'id' && value !== '')
|
|
109
|
+
tokens.add(value);
|
|
110
|
+
for (const piece of decodeEntities(value).split(/\s+/))
|
|
111
|
+
if (piece)
|
|
112
|
+
tokens.add(piece);
|
|
113
|
+
addWords(tokens, value, false);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return tokens;
|
|
118
|
+
}
|
|
119
|
+
/** every word in a script's source, with a lowercase copy of each, which covers classes JS adds */
|
|
120
|
+
export function scriptTokens(js) {
|
|
121
|
+
const tokens = new Set();
|
|
122
|
+
addWords(tokens, js, true);
|
|
123
|
+
return tokens;
|
|
124
|
+
}
|
|
125
|
+
/** whether `tag` is a `<link rel=stylesheet>` with an `href` */
|
|
126
|
+
export function isStylesheetLink(tag) {
|
|
127
|
+
const rel = (attr(tag, 'rel') ?? '').toLowerCase().split(/\s+/);
|
|
128
|
+
return tag.name === 'link' && rel.includes('stylesheet') && (attr(tag, 'href') ?? '') !== '';
|
|
129
|
+
}
|
|
130
|
+
/** the decoded `href` of every `<link rel=stylesheet>` and `src` of every external script */
|
|
131
|
+
export function pageAssets(html) {
|
|
132
|
+
const styles = [];
|
|
133
|
+
const scripts = [];
|
|
134
|
+
for (const tag of tagsOf(html)) {
|
|
135
|
+
if (isStylesheetLink(tag))
|
|
136
|
+
styles.push(decodeEntities(attr(tag, 'href')));
|
|
137
|
+
const src = attr(tag, 'src');
|
|
138
|
+
if (tag.name === 'script' && src)
|
|
139
|
+
scripts.push(decodeEntities(src));
|
|
140
|
+
}
|
|
141
|
+
return { styles, scripts };
|
|
142
|
+
}
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,147 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@drupflare/spool",
|
|
3
|
+
"description": "Client-Side Page Rendering and Execution Framework",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./protocol": {
|
|
14
|
+
"types": "./dist/protocol/index.d.ts",
|
|
15
|
+
"default": "./dist/protocol/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./server": {
|
|
18
|
+
"types": "./dist/server/index.d.ts",
|
|
19
|
+
"default": "./dist/server/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./node": {
|
|
22
|
+
"types": "./dist/node/listener.d.ts",
|
|
23
|
+
"default": "./dist/node/listener.js"
|
|
24
|
+
},
|
|
25
|
+
"./proxy": {
|
|
26
|
+
"types": "./dist/proxy/index.d.ts",
|
|
27
|
+
"default": "./dist/proxy/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./stores": {
|
|
30
|
+
"types": "./dist/stores/index.d.ts",
|
|
31
|
+
"default": "./dist/stores/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./stores/durable-object": {
|
|
34
|
+
"types": "./dist/stores/durable-object.d.ts",
|
|
35
|
+
"default": "./dist/stores/durable-object.js"
|
|
36
|
+
},
|
|
37
|
+
"./sw": {
|
|
38
|
+
"types": "./dist/sw/index.d.ts",
|
|
39
|
+
"default": "./dist/sw/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./client": {
|
|
42
|
+
"types": "./dist/client/index.d.ts",
|
|
43
|
+
"default": "./dist/client/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./report": {
|
|
46
|
+
"types": "./dist/report/index.d.ts",
|
|
47
|
+
"default": "./dist/report/index.js"
|
|
48
|
+
},
|
|
49
|
+
"./trim": {
|
|
50
|
+
"types": "./dist/trim/index.d.ts",
|
|
51
|
+
"default": "./dist/trim/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./package.json": "./package.json"
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"dist",
|
|
57
|
+
"LICENSE",
|
|
58
|
+
"README.md"
|
|
59
|
+
],
|
|
60
|
+
"scripts": {
|
|
61
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.dom.json --noEmit && tsc -p tests/node/tsconfig.json --noEmit && tsc -p tests/browser/tsconfig.json --noEmit && tsc -p scripts/tsconfig.json --noEmit",
|
|
62
|
+
"build": "tsc -p tsconfig.build.json && tsc -p tsconfig.build.dom.json",
|
|
63
|
+
"prepublishOnly": "bun run build && bun run build:cdn",
|
|
64
|
+
"build:sw": "bun scripts/build-sw.ts",
|
|
65
|
+
"build:cdn": "bun scripts/build-cdn.ts",
|
|
66
|
+
"publish:cdn": "bun scripts/publish-cdn.ts",
|
|
67
|
+
"test": "vitest run --project=unit --project=node",
|
|
68
|
+
"test:watch": "vitest --project=unit --project=node",
|
|
69
|
+
"test:coverage": "vitest run --project=unit --project=node --coverage",
|
|
70
|
+
"test:browser": "playwright test",
|
|
71
|
+
"prettier": "prettier --write .",
|
|
72
|
+
"prettier:check": "prettier --check .",
|
|
73
|
+
"prepare": "husky",
|
|
74
|
+
"docs:build": "typedoc",
|
|
75
|
+
"docs:serve": "typedoc --watch",
|
|
76
|
+
"impact": "bun scripts/impact.ts",
|
|
77
|
+
"impact:check": "bun scripts/impact.ts --check",
|
|
78
|
+
"measure:repeat-nav": "bun scripts/measure/repeat-nav.ts",
|
|
79
|
+
"measure:zero-network": "bun scripts/measure/zero-network.ts",
|
|
80
|
+
"measure:device-cpu": "bun scripts/measure/device-cpu.ts",
|
|
81
|
+
"measure:on-wire": "bun scripts/measure/on-wire.ts",
|
|
82
|
+
"measure:rapl": "node scripts/measure/rapl.ts",
|
|
83
|
+
"measure:iphone": "bun scripts/measure/iphone.ts",
|
|
84
|
+
"measure:traces": "bun scripts/measure/traces.ts",
|
|
85
|
+
"measure:payload": "bun scripts/measure/payload.ts",
|
|
86
|
+
"measure:trim": "bun scripts/measure/trim.ts"
|
|
87
|
+
},
|
|
88
|
+
"devDependencies": {
|
|
89
|
+
"@azure/functions": "^4.16.5",
|
|
90
|
+
"@cloudflare/vitest-pool-workers": "^0.22.0",
|
|
91
|
+
"@cloudflare/workers-types": "^5.20260811.1",
|
|
92
|
+
"@google-cloud/functions-framework": "^5.0.5",
|
|
93
|
+
"@hapi/hapi": "^21.4.10",
|
|
94
|
+
"@nestjs/common": "^12.1.3",
|
|
95
|
+
"@nestjs/core": "^12.1.3",
|
|
96
|
+
"@nestjs/platform-express": "^12.1.3",
|
|
97
|
+
"@netlify/edge-functions": "^4.0.2",
|
|
98
|
+
"@netlify/functions": "^6.0.2",
|
|
99
|
+
"@playwright/test": "^1.62.1",
|
|
100
|
+
"@types/bun": "^1.4.3",
|
|
101
|
+
"@types/express": "^5.0.6",
|
|
102
|
+
"@types/koa": "^3.0.3",
|
|
103
|
+
"@types/node": "^24",
|
|
104
|
+
"@vitest/coverage-istanbul": "^4.1.0",
|
|
105
|
+
"elysia": "^1.4.30",
|
|
106
|
+
"express": "^5.3.0",
|
|
107
|
+
"fake-indexeddb": "^6.2.6",
|
|
108
|
+
"fastify": "^5.12.7",
|
|
109
|
+
"h3": "^1",
|
|
110
|
+
"hono": "^4.13.13",
|
|
111
|
+
"husky": "^9.1.7",
|
|
112
|
+
"itty-router": "^5.0.24",
|
|
113
|
+
"koa": "^3.2.1",
|
|
114
|
+
"lint-staged": "^17.3.0",
|
|
115
|
+
"prettier": "^3.9.10",
|
|
116
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
117
|
+
"prettier-plugin-sh": "^0.20.2",
|
|
118
|
+
"reflect-metadata": "^0.2.2",
|
|
119
|
+
"rxjs": "^7.8.2",
|
|
120
|
+
"typedoc": "^0.28.20",
|
|
121
|
+
"typescript": "^5.9.3",
|
|
122
|
+
"vitest": "^4.1.0",
|
|
123
|
+
"wrangler": "^4.20.0"
|
|
124
|
+
},
|
|
125
|
+
"license": "MIT",
|
|
126
|
+
"author": "Gregory Mitchell <me@gmitch215.xyz>",
|
|
127
|
+
"homepage": "https://github.com/drupflare/spool",
|
|
128
|
+
"repository": {
|
|
129
|
+
"type": "git",
|
|
130
|
+
"url": "git+https://github.com/drupflare/spool.git"
|
|
131
|
+
},
|
|
132
|
+
"bugs": {
|
|
133
|
+
"url": "https://github.com/drupflare/spool/issues"
|
|
134
|
+
},
|
|
135
|
+
"keywords": [
|
|
136
|
+
"cloudflare",
|
|
137
|
+
"workers",
|
|
138
|
+
"service-worker",
|
|
139
|
+
"indexeddb",
|
|
140
|
+
"offline",
|
|
141
|
+
"local-first",
|
|
142
|
+
"energy"
|
|
143
|
+
],
|
|
144
|
+
"lint-staged": {
|
|
145
|
+
"*.{ts,js,mjs,css,md,json,jsonc}": "prettier --write"
|
|
146
|
+
}
|
|
147
|
+
}
|