@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,137 @@
|
|
|
1
|
+
import { idbStorage } from './idb.js';
|
|
2
|
+
import { sealedStorage } from './seal.js';
|
|
3
|
+
const KEYS_DB = 'spool-keys';
|
|
4
|
+
const KEYS = 'keys';
|
|
5
|
+
const META = 'meta';
|
|
6
|
+
const CURRENT = 'partition';
|
|
7
|
+
function wrap(request) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
request.onsuccess = () => resolve(request.result);
|
|
10
|
+
request.onerror = () => reject(request.error);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function openKeysDb(factory) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const request = factory.open(KEYS_DB, 1);
|
|
16
|
+
request.onupgradeneeded = () => {
|
|
17
|
+
request.result.createObjectStore(KEYS);
|
|
18
|
+
request.result.createObjectStore(META);
|
|
19
|
+
};
|
|
20
|
+
request.onsuccess = () => {
|
|
21
|
+
const db = request.result;
|
|
22
|
+
db.onversionchange = () => db.close();
|
|
23
|
+
resolve(db);
|
|
24
|
+
};
|
|
25
|
+
request.onerror = () => reject(request.error);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Keys in two places. A `persistent` key is a non-extractable `CryptoKey` in IndexedDB, so it
|
|
30
|
+
* survives a worker restart and a browser restart and is deleted on logout or a change of
|
|
31
|
+
* partition. A `session` key lives in this worker's memory and is lost when the browser stops the
|
|
32
|
+
* worker; what it sealed is unreadable from then on.
|
|
33
|
+
*/
|
|
34
|
+
export function idbVault(factory = indexedDB) {
|
|
35
|
+
const session = new Map();
|
|
36
|
+
let tail = Promise.resolve();
|
|
37
|
+
const serial = (fn) => {
|
|
38
|
+
const run = tail.then(fn, fn);
|
|
39
|
+
tail = run.catch(() => undefined);
|
|
40
|
+
return run;
|
|
41
|
+
};
|
|
42
|
+
const generate = () => crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
|
|
43
|
+
async function inDb(mode, run) {
|
|
44
|
+
const db = await openKeysDb(factory);
|
|
45
|
+
try {
|
|
46
|
+
const tx = db.transaction(KEYS, mode);
|
|
47
|
+
const finished = new Promise((resolve, reject) => {
|
|
48
|
+
tx.oncomplete = () => resolve();
|
|
49
|
+
tx.onerror = () => reject(tx.error);
|
|
50
|
+
tx.onabort = () => reject(tx.error);
|
|
51
|
+
});
|
|
52
|
+
const result = await run(tx.objectStore(KEYS));
|
|
53
|
+
await finished;
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
db.close();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
get: (partition, retention, create) => serial(async () => {
|
|
62
|
+
if (retention === 'session') {
|
|
63
|
+
let key = session.get(partition);
|
|
64
|
+
if (!key && create) {
|
|
65
|
+
key = await generate();
|
|
66
|
+
session.set(partition, key);
|
|
67
|
+
}
|
|
68
|
+
return key;
|
|
69
|
+
}
|
|
70
|
+
const found = await inDb('readonly', (s) => wrap(s.get(partition)));
|
|
71
|
+
if (found || !create)
|
|
72
|
+
return found;
|
|
73
|
+
const key = await generate();
|
|
74
|
+
await inDb('readwrite', async (s) => void (await wrap(s.put(key, partition))));
|
|
75
|
+
return key;
|
|
76
|
+
}),
|
|
77
|
+
shred: (partition) => serial(async () => {
|
|
78
|
+
session.delete(partition);
|
|
79
|
+
await inDb('readwrite', async (s) => void (await wrap(s.delete(partition))));
|
|
80
|
+
})
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/** partitions as IndexedDB databases named for the partition, sealed under `vault` */
|
|
84
|
+
export function idbPartitions(factory = indexedDB, vault = idbVault(factory)) {
|
|
85
|
+
const opened = new Map();
|
|
86
|
+
const dbName = (id) => `spool-p-${id}`;
|
|
87
|
+
async function inMeta(mode, run) {
|
|
88
|
+
const db = await openKeysDb(factory);
|
|
89
|
+
try {
|
|
90
|
+
const tx = db.transaction(META, mode);
|
|
91
|
+
const finished = new Promise((resolve, reject) => {
|
|
92
|
+
tx.oncomplete = () => resolve();
|
|
93
|
+
tx.onerror = () => reject(tx.error);
|
|
94
|
+
tx.onabort = () => reject(tx.error);
|
|
95
|
+
});
|
|
96
|
+
const result = await run(tx.objectStore(META));
|
|
97
|
+
await finished;
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
db.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
current: async () => (await inMeta('readonly', (s) => wrap(s.get(CURRENT)))) ?? null,
|
|
106
|
+
setCurrent: (id) => inMeta('readwrite', async (s) => {
|
|
107
|
+
if (id === null)
|
|
108
|
+
await wrap(s.delete(CURRENT));
|
|
109
|
+
else
|
|
110
|
+
await wrap(s.put(id, CURRENT));
|
|
111
|
+
}),
|
|
112
|
+
open(id, retentionOf, shared) {
|
|
113
|
+
const found = opened.get(id);
|
|
114
|
+
if (found)
|
|
115
|
+
return found.sealed;
|
|
116
|
+
const base = idbStorage(factory, dbName(id));
|
|
117
|
+
const sealed = sealedStorage(base, { partition: id, vault, retentionOf, shared });
|
|
118
|
+
opened.set(id, { sealed, destroy: base.destroy });
|
|
119
|
+
return sealed;
|
|
120
|
+
},
|
|
121
|
+
async destroy(id) {
|
|
122
|
+
const held = opened.get(id);
|
|
123
|
+
held?.sealed.kill();
|
|
124
|
+
opened.delete(id);
|
|
125
|
+
await vault.shred(id);
|
|
126
|
+
if (held)
|
|
127
|
+
await held.destroy();
|
|
128
|
+
else {
|
|
129
|
+
await new Promise((resolve) => {
|
|
130
|
+
const request = factory.deleteDatabase(dbName(id));
|
|
131
|
+
request.onsuccess = () => resolve();
|
|
132
|
+
request.onerror = () => resolve();
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** the response headers that carry a site's security policy; a local render replays them */
|
|
2
|
+
export declare const POLICY_HEADERS: readonly ["content-security-policy", "content-security-policy-report-only", "x-frame-options", "x-content-type-options", "referrer-policy", "permissions-policy", "cross-origin-opener-policy", "cross-origin-embedder-policy", "cross-origin-resource-policy"];
|
|
3
|
+
/** most paths whose own header set is kept, oldest dropped first */
|
|
4
|
+
export declare const MAX_POLICY_PATHS = 128;
|
|
5
|
+
/** header names and values, as stored in the replica meta */
|
|
6
|
+
export type HeaderSet = Array<[string, string]>;
|
|
7
|
+
/** the {@link POLICY_HEADERS} a response carries, possibly none */
|
|
8
|
+
export declare function policyHeaders(headers: Headers): HeaderSet;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** the response headers that carry a site's security policy; a local render replays them */
|
|
2
|
+
export const POLICY_HEADERS = [
|
|
3
|
+
'content-security-policy',
|
|
4
|
+
'content-security-policy-report-only',
|
|
5
|
+
'x-frame-options',
|
|
6
|
+
'x-content-type-options',
|
|
7
|
+
'referrer-policy',
|
|
8
|
+
'permissions-policy',
|
|
9
|
+
'cross-origin-opener-policy',
|
|
10
|
+
'cross-origin-embedder-policy',
|
|
11
|
+
'cross-origin-resource-policy'
|
|
12
|
+
];
|
|
13
|
+
/** most paths whose own header set is kept, oldest dropped first */
|
|
14
|
+
export const MAX_POLICY_PATHS = 128;
|
|
15
|
+
/** the {@link POLICY_HEADERS} a response carries, possibly none */
|
|
16
|
+
export function policyHeaders(headers) {
|
|
17
|
+
const out = [];
|
|
18
|
+
for (const name of POLICY_HEADERS) {
|
|
19
|
+
const value = headers.get(name);
|
|
20
|
+
if (value !== null)
|
|
21
|
+
out.push([name, value]);
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Cost } from '../protocol/provision.js';
|
|
2
|
+
export * from '../protocol/provision.js';
|
|
3
|
+
/**
|
|
4
|
+
* What one artifact cost and what it served. `catalogueBytes` is the part of `bytes` that arrived
|
|
5
|
+
* in a catalogue bundle (the JSON text, uncompressed) and `catalogueViews` the part of `views`
|
|
6
|
+
* that was the first open of a catalogue entry.
|
|
7
|
+
*/
|
|
8
|
+
export type LedgerEntry = {
|
|
9
|
+
id: string;
|
|
10
|
+
bytes: number;
|
|
11
|
+
requests: number;
|
|
12
|
+
views: number;
|
|
13
|
+
catalogueBytes: number;
|
|
14
|
+
catalogueViews: number;
|
|
15
|
+
};
|
|
16
|
+
/** the whole ledger summed; `bytesPerView` is null until a view has been served */
|
|
17
|
+
export type LedgerTotals = {
|
|
18
|
+
bytes: number;
|
|
19
|
+
requests: number;
|
|
20
|
+
views: number;
|
|
21
|
+
bytesPerView: number | null;
|
|
22
|
+
catalogueBytes: number;
|
|
23
|
+
catalogueViews: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Bytes and requests spent provisioning each artifact, against the local views served from it.
|
|
27
|
+
* It lives in memory, so a worker restart starts it over.
|
|
28
|
+
*/
|
|
29
|
+
export declare class Ledger {
|
|
30
|
+
#private;
|
|
31
|
+
/** charges a fetch of `id`; `catalogue` marks bytes that came in a catalogue bundle */
|
|
32
|
+
spend(id: string, cost: Cost, catalogue?: boolean): void;
|
|
33
|
+
/** counts a local view served from `id`; `catalogue` marks the first open of a catalogue entry */
|
|
34
|
+
serve(id: string, catalogue?: boolean): void;
|
|
35
|
+
entries(): LedgerEntry[];
|
|
36
|
+
totals(): LedgerTotals;
|
|
37
|
+
/** the mean cost of one fetch so far, catalogue entries left out, or null before the first */
|
|
38
|
+
meanFetchBytes(): number | null;
|
|
39
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export * from '../protocol/provision.js';
|
|
2
|
+
/**
|
|
3
|
+
* Bytes and requests spent provisioning each artifact, against the local views served from it.
|
|
4
|
+
* It lives in memory, so a worker restart starts it over.
|
|
5
|
+
*/
|
|
6
|
+
export class Ledger {
|
|
7
|
+
#rows = new Map();
|
|
8
|
+
#row(id) {
|
|
9
|
+
const row = this.#rows.get(id) ?? {
|
|
10
|
+
id,
|
|
11
|
+
bytes: 0,
|
|
12
|
+
requests: 0,
|
|
13
|
+
views: 0,
|
|
14
|
+
catalogueBytes: 0,
|
|
15
|
+
catalogueViews: 0
|
|
16
|
+
};
|
|
17
|
+
this.#rows.set(id, row);
|
|
18
|
+
return row;
|
|
19
|
+
}
|
|
20
|
+
/** charges a fetch of `id`; `catalogue` marks bytes that came in a catalogue bundle */
|
|
21
|
+
spend(id, cost, catalogue = false) {
|
|
22
|
+
const row = this.#row(id);
|
|
23
|
+
row.bytes += cost.bytes;
|
|
24
|
+
row.requests += cost.requests;
|
|
25
|
+
if (catalogue)
|
|
26
|
+
row.catalogueBytes += cost.bytes;
|
|
27
|
+
}
|
|
28
|
+
/** counts a local view served from `id`; `catalogue` marks the first open of a catalogue entry */
|
|
29
|
+
serve(id, catalogue = false) {
|
|
30
|
+
const row = this.#row(id);
|
|
31
|
+
row.views += 1;
|
|
32
|
+
if (catalogue)
|
|
33
|
+
row.catalogueViews += 1;
|
|
34
|
+
}
|
|
35
|
+
entries() {
|
|
36
|
+
return [...this.#rows.values()]
|
|
37
|
+
.map((r) => ({ ...r }))
|
|
38
|
+
.sort((a, b) => (a.id < b.id ? -1 : 1));
|
|
39
|
+
}
|
|
40
|
+
totals() {
|
|
41
|
+
const rows = [...this.#rows.values()];
|
|
42
|
+
const sum = (pick) => rows.reduce((t, r) => t + pick(r), 0);
|
|
43
|
+
const views = sum((r) => r.views);
|
|
44
|
+
const bytes = sum((r) => r.bytes);
|
|
45
|
+
return {
|
|
46
|
+
bytes,
|
|
47
|
+
requests: sum((r) => r.requests),
|
|
48
|
+
views,
|
|
49
|
+
bytesPerView: views === 0 ? null : bytes / views,
|
|
50
|
+
catalogueBytes: sum((r) => r.catalogueBytes),
|
|
51
|
+
catalogueViews: sum((r) => r.catalogueViews)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** the mean cost of one fetch so far, catalogue entries left out, or null before the first */
|
|
55
|
+
meanFetchBytes() {
|
|
56
|
+
const rows = [...this.#rows.values()].filter((r) => r.bytes > r.catalogueBytes);
|
|
57
|
+
return rows.length === 0
|
|
58
|
+
? null
|
|
59
|
+
: rows.reduce((t, r) => t + r.bytes - r.catalogueBytes, 0) / rows.length;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Artifact } from '../protocol/artifact.js';
|
|
2
|
+
import type { SwConfig } from '../protocol/config.js';
|
|
3
|
+
import type { SerializedRoute } from '../protocol/route.js';
|
|
4
|
+
/** a custom renderer for one route pattern; it replaces the built-in one for that route */
|
|
5
|
+
export type Renderer = (artifact: Artifact) => string | Promise<string>;
|
|
6
|
+
/** renderers keyed by route pattern */
|
|
7
|
+
export type Renderers = Record<string, Renderer>;
|
|
8
|
+
/** the page for an artifact, or the reason it cannot be built here and the network must answer */
|
|
9
|
+
export type Rendered = {
|
|
10
|
+
ok: true;
|
|
11
|
+
html: string;
|
|
12
|
+
} | {
|
|
13
|
+
ok: false;
|
|
14
|
+
reason: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Builds the HTML for a stored artifact, or the JSON text of a `data` one. The result carries the same client tag the server
|
|
18
|
+
* injects, so it equals the network render after normalisation. Any failure (a missing template,
|
|
19
|
+
* an unfilled placeholder, a renderer that throws) is a refusal, never a partial page.
|
|
20
|
+
*/
|
|
21
|
+
export declare function renderLocal(config: SwConfig, route: SerializedRoute, artifact: Artifact, renderers?: Renderers): Promise<Rendered>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { renderAssembly } from '../protocol/assembly.js';
|
|
2
|
+
import { injectClientTag } from '../protocol/tag.js';
|
|
3
|
+
import { interpretTemplate } from '../protocol/template/interpret.js';
|
|
4
|
+
/**
|
|
5
|
+
* Builds the HTML for a stored artifact, or the JSON text of a `data` one. The result carries the same client tag the server
|
|
6
|
+
* injects, so it equals the network render after normalisation. Any failure (a missing template,
|
|
7
|
+
* an unfilled placeholder, a renderer that throws) is a refusal, never a partial page.
|
|
8
|
+
*/
|
|
9
|
+
export async function renderLocal(config, route, artifact, renderers = {}) {
|
|
10
|
+
try {
|
|
11
|
+
const custom = Object.hasOwn(renderers, route.pattern)
|
|
12
|
+
? renderers[route.pattern]
|
|
13
|
+
: undefined;
|
|
14
|
+
let html;
|
|
15
|
+
if (custom)
|
|
16
|
+
html = await custom(artifact);
|
|
17
|
+
else if (artifact.strategy === 'data' && route.strategy === 'data') {
|
|
18
|
+
return { ok: true, html: JSON.stringify(artifact.body.data) };
|
|
19
|
+
}
|
|
20
|
+
else if (artifact.strategy === 'projection' && route.strategy === 'projection') {
|
|
21
|
+
return { ok: false, reason: `no renderer for ${route.pattern}` };
|
|
22
|
+
}
|
|
23
|
+
else if (artifact.strategy === 'static' || artifact.strategy !== route.strategy) {
|
|
24
|
+
return {
|
|
25
|
+
ok: false,
|
|
26
|
+
reason: `artifact is ${artifact.strategy}, route is ${route.strategy}`
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
else if (artifact.strategy === 'assembly') {
|
|
30
|
+
const result = renderAssembly(artifact.body);
|
|
31
|
+
if (!result.ok)
|
|
32
|
+
return result;
|
|
33
|
+
html = result.html;
|
|
34
|
+
}
|
|
35
|
+
else if (artifact.strategy !== 'template') {
|
|
36
|
+
return { ok: false, reason: `${artifact.strategy} has no built-in render` };
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const template = Object.hasOwn(config.templates, route.pattern)
|
|
40
|
+
? config.templates[route.pattern]
|
|
41
|
+
: undefined;
|
|
42
|
+
if (!template)
|
|
43
|
+
return { ok: false, reason: `no template for ${route.pattern}` };
|
|
44
|
+
html = interpretTemplate(template, artifact.body.data);
|
|
45
|
+
}
|
|
46
|
+
if (route.strategy === 'data')
|
|
47
|
+
return { ok: true, html };
|
|
48
|
+
return { ok: true, html: injectClientTag(html, config.clientTag) };
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
return { ok: false, reason: error instanceof Error ? error.message : 'render failed' };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Artifact } from '../protocol/artifact.js';
|
|
2
|
+
import { type Envelope } from '../protocol/envelope.js';
|
|
3
|
+
import { type SyncResponse } from '../protocol/sync.js';
|
|
4
|
+
import { Graph, type Materialized } from './graph/index.js';
|
|
5
|
+
import { type HeaderSet } from './policy.js';
|
|
6
|
+
import type { ReplicaStorage, StoredArtifact } from './storage.js';
|
|
7
|
+
/**
|
|
8
|
+
* The device-side copy: artifacts, the tag versions learned from syncs, and the cursor they are
|
|
9
|
+
* current to. An artifact is valid while no dependency tag has a version above the cursor the
|
|
10
|
+
* artifact was built at; every sync and piggybacked diff only ever raises tag versions, so
|
|
11
|
+
* a stale copy is never revived.
|
|
12
|
+
*/
|
|
13
|
+
export declare class Replica {
|
|
14
|
+
#private;
|
|
15
|
+
/** records and projections; untouched until a page that references them is stored or shown */
|
|
16
|
+
readonly graph: Graph;
|
|
17
|
+
constructor(storage: ReplicaStorage, version: string, now?: () => number);
|
|
18
|
+
/** reads the persisted meta; a different runtime version wipes what the old one stored */
|
|
19
|
+
load(): Promise<void>;
|
|
20
|
+
/** the vector cursor the replica is current to, or null before the first artifact */
|
|
21
|
+
get cursor(): number | null;
|
|
22
|
+
/** when the replica last completed a sync or a piggybacked diff */
|
|
23
|
+
get syncedAt(): number;
|
|
24
|
+
/** the security headers of the latest network navigation, or undefined before one was seen */
|
|
25
|
+
get sitePolicy(): HeaderSet | undefined;
|
|
26
|
+
/** the security headers `path` itself was last answered with, or undefined */
|
|
27
|
+
policyOf(path: string): HeaderSet | undefined;
|
|
28
|
+
/** keeps the headers of a network view: for `path`, or for the whole site when `path` is null */
|
|
29
|
+
keepPolicy(path: string | null, headers: HeaderSet): Promise<void>;
|
|
30
|
+
/** the stored artifact for `id`, memory-only routes included */
|
|
31
|
+
get(id: string): Promise<StoredArtifact | undefined>;
|
|
32
|
+
/** every stored artifact id */
|
|
33
|
+
ids(): Promise<string[]>;
|
|
34
|
+
/** whether no dependency of `artifact` has changed since it was built */
|
|
35
|
+
isCurrent(artifact: Artifact): boolean;
|
|
36
|
+
/** when `stored` was last known good */
|
|
37
|
+
validatedAt(stored: StoredArtifact): number;
|
|
38
|
+
/** records that a sync just confirmed `id`, if it is still stored */
|
|
39
|
+
validate(id: string): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* What to ask `/.spool/sync?need=` about before showing `artifact`: each tag it depends on at
|
|
42
|
+
* the version it was built, plus its own drop tag. Null for an artifact with no references,
|
|
43
|
+
* which a full sync validates unless `always` is set, and for one that depends on more than the
|
|
44
|
+
* server accepts.
|
|
45
|
+
*/
|
|
46
|
+
needFor(artifact: Artifact, always?: boolean): Record<string, number> | null;
|
|
47
|
+
/** the records and projections held, for an artifact request's `have`; loads the graph */
|
|
48
|
+
held(): Promise<Record<string, number>>;
|
|
49
|
+
/** the artifact with its references resolved from the graph; loads the graph when it has any */
|
|
50
|
+
materialize(artifact: Artifact): Promise<Materialized>;
|
|
51
|
+
/**
|
|
52
|
+
* Stores an artifact unless a change it missed is already known. The first one sets the cursor
|
|
53
|
+
* to its vector, since nothing earlier can affect it. Returns whether it was kept.
|
|
54
|
+
*/
|
|
55
|
+
put(artifact: Artifact, persist: boolean, catalogue?: boolean): Promise<boolean>;
|
|
56
|
+
/** marks the catalogue entry `id` as opened, so it is an ordinary artifact from now on */
|
|
57
|
+
open(id: string): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* `need` with the dependency tags and drop tag of every unopened catalogue entry added, each at
|
|
60
|
+
* the vector its entry was built at. A revoked entry that no page opens is named by no page's
|
|
61
|
+
* sync, so a required sync of a principal page carries this. Null when the union is over
|
|
62
|
+
* `MAX_NEED`, which the caller answers with a full sync.
|
|
63
|
+
*/
|
|
64
|
+
sweep(need: Record<string, number>): Promise<Record<string, number> | null>;
|
|
65
|
+
/** removes artifacts by id */
|
|
66
|
+
drop(ids: readonly string[]): Promise<void>;
|
|
67
|
+
/** empties the replica; `cursor` is where it restarts */
|
|
68
|
+
wipe(cursor?: number | null): Promise<void>;
|
|
69
|
+
/** applies a `/.spool/sync` answer and returns the ids of the artifacts it invalidated */
|
|
70
|
+
applySync(response: SyncResponse): Promise<string[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Applies the spool headers of a network response. Invalidations always apply. The cursor and
|
|
73
|
+
* the sync clock move only when the response answers the cursor this worker sent and carries
|
|
74
|
+
* no `Age`, so a copy replayed from a shared cache cannot extend freshness.
|
|
75
|
+
*/
|
|
76
|
+
applyEnvelope(envelope: Envelope, sent: number | null, aged: boolean): Promise<string[]>;
|
|
77
|
+
}
|