@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,12 @@
|
|
|
1
|
+
import type { Identity } from '../protocol/route.js';
|
|
2
|
+
/** how a request becomes a principal; the secret keys the partition id */
|
|
3
|
+
export type IdentityOptions = {
|
|
4
|
+
resolve(request: Request): Identity | null | Promise<Identity | null>;
|
|
5
|
+
secret: string | Uint8Array;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* The `Spool-Partition` value for a principal: an HMAC-SHA-256 under the server secret, so the
|
|
9
|
+
* browser can tell partitions apart without learning the principal, and one principal's id cannot
|
|
10
|
+
* be computed from another's.
|
|
11
|
+
*/
|
|
12
|
+
export declare function partitionId(secret: string | Uint8Array, principal: string): Promise<string>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function base64url(bytes) {
|
|
2
|
+
let binary = '';
|
|
3
|
+
for (const byte of bytes)
|
|
4
|
+
binary += String.fromCharCode(byte);
|
|
5
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The `Spool-Partition` value for a principal: an HMAC-SHA-256 under the server secret, so the
|
|
9
|
+
* browser can tell partitions apart without learning the principal, and one principal's id cannot
|
|
10
|
+
* be computed from another's.
|
|
11
|
+
*/
|
|
12
|
+
export async function partitionId(secret, principal) {
|
|
13
|
+
const raw = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret;
|
|
14
|
+
const key = await crypto.subtle.importKey('raw', raw, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
15
|
+
const mac = await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(`spool-partition:1\u0000${principal}`));
|
|
16
|
+
return base64url(new Uint8Array(mac));
|
|
17
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { BeaconEvent } from '../protocol/beacon.js';
|
|
2
|
+
import { type TimedView } from '../protocol/view.js';
|
|
3
|
+
/** where flushed beacon events go: Analytics Engine, a log, a queue */
|
|
4
|
+
export interface Sink {
|
|
5
|
+
write(events: readonly BeaconEvent[]): void | Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/** a sink that keeps events in `events`, for tests */
|
|
8
|
+
export declare function memorySink(): Sink & {
|
|
9
|
+
events: BeaconEvent[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The `spool.view` events among `events`, validated, with cross-origin counts joined from the
|
|
13
|
+
* loads reports among them; anything else is skipped. A page reports when it is hidden, which can
|
|
14
|
+
* be a later flush than its view, so pass the whole stream and not one batch.
|
|
15
|
+
*/
|
|
16
|
+
export declare function viewsOf(events: readonly BeaconEvent[]): TimedView[];
|
|
17
|
+
/**
|
|
18
|
+
* A sink that writes one JSON line per event through `write`, such as `console.log` or a stream's
|
|
19
|
+
* `write`. A view becomes a flat object (`event`, `at` and the view's fields) and a loads report one
|
|
20
|
+
* (`event`, `view`, `count`, `at`) so a log query can read them; any other request is written as it
|
|
21
|
+
* was held.
|
|
22
|
+
*/
|
|
23
|
+
export declare function jsonLinesSink(write: (line: string) => void): Sink;
|
|
24
|
+
/** the part of an Analytics Engine binding the sink calls */
|
|
25
|
+
export interface AnalyticsEngineLike {
|
|
26
|
+
writeDataPoint(point: {
|
|
27
|
+
indexes?: string[];
|
|
28
|
+
blobs?: Array<string | null>;
|
|
29
|
+
doubles?: number[];
|
|
30
|
+
}): void;
|
|
31
|
+
}
|
|
32
|
+
/** longest slice of a body one data point carries, in UTF-8 bytes; a longer body is split across points */
|
|
33
|
+
export declare const MAX_BEACON_BLOB = 4096;
|
|
34
|
+
/**
|
|
35
|
+
* Cuts `text` into slices of at most `limit` UTF-8 bytes, never inside a code point. Text with no
|
|
36
|
+
* bytes still gives one empty slice, so every event leaves one data point at least.
|
|
37
|
+
*/
|
|
38
|
+
export declare function splitBytes(text: string, limit?: number): string[];
|
|
39
|
+
/**
|
|
40
|
+
* A sink for a Workers Analytics Engine dataset, one data point per event.
|
|
41
|
+
*
|
|
42
|
+
* | | views | other requests |
|
|
43
|
+
* | --- | --- | --- |
|
|
44
|
+
* | index1 | route pattern | path |
|
|
45
|
+
* | blob1 | `view` | `beacon` |
|
|
46
|
+
* | blob2 to blob9 | tier, reason, engine, scope, strategy, route, id, from | content type, body slice, vendor |
|
|
47
|
+
* | double1 to double6 | at, staleSec, swAgeMs, gapMs, mismatch, crossOriginLoads | at, slice number from 0, slice count |
|
|
48
|
+
*
|
|
49
|
+
* `staleSec`, `gapMs` and `crossOriginLoads` are -1 when unknown; `mismatch` is -1 when nothing was
|
|
50
|
+
* compared, 0 for a match and 1 for a mismatch. A page's loads report is a row with `blob1` of
|
|
51
|
+
* `loads`, the view id as `blob2` and `at` and the count as `double1` and `double2`. Analytics
|
|
52
|
+
* Engine caps an index at 96 bytes and a row at 16 KB, so a body longer than {@link MAX_BEACON_BLOB}
|
|
53
|
+
* bytes is written as several points, one per slice, with the same index, content type, vendor and
|
|
54
|
+
* `at`; join the slices by those and `double2` (the slice number) to rebuild it. Nothing is cut off.
|
|
55
|
+
*/
|
|
56
|
+
export declare function analyticsEngineSink(dataset: AnalyticsEngineLike): Sink;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { loadsFromBeacon, viewFromBeacon, viewsWithLoads } from '../protocol/view.js';
|
|
2
|
+
/** a sink that keeps events in `events`, for tests */
|
|
3
|
+
export function memorySink() {
|
|
4
|
+
const events = [];
|
|
5
|
+
return {
|
|
6
|
+
events,
|
|
7
|
+
write(batch) {
|
|
8
|
+
events.push(...batch);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The `spool.view` events among `events`, validated, with cross-origin counts joined from the
|
|
14
|
+
* loads reports among them; anything else is skipped. A page reports when it is hidden, which can
|
|
15
|
+
* be a later flush than its view, so pass the whole stream and not one batch.
|
|
16
|
+
*/
|
|
17
|
+
export function viewsOf(events) {
|
|
18
|
+
return viewsWithLoads(events);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A sink that writes one JSON line per event through `write`, such as `console.log` or a stream's
|
|
22
|
+
* `write`. A view becomes a flat object (`event`, `at` and the view's fields) and a loads report one
|
|
23
|
+
* (`event`, `view`, `count`, `at`) so a log query can read them; any other request is written as it
|
|
24
|
+
* was held.
|
|
25
|
+
*/
|
|
26
|
+
export function jsonLinesSink(write) {
|
|
27
|
+
return {
|
|
28
|
+
write(batch) {
|
|
29
|
+
for (const event of batch) {
|
|
30
|
+
const view = viewFromBeacon(event);
|
|
31
|
+
const loads = loadsFromBeacon(event);
|
|
32
|
+
if (view)
|
|
33
|
+
write(JSON.stringify({ event: 'spool.view', ...view }));
|
|
34
|
+
else if (loads)
|
|
35
|
+
write(JSON.stringify({ event: 'spool.loads', ...loads }));
|
|
36
|
+
else
|
|
37
|
+
write(JSON.stringify(event));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** longest slice of a body one data point carries, in UTF-8 bytes; a longer body is split across points */
|
|
43
|
+
export const MAX_BEACON_BLOB = 4096;
|
|
44
|
+
const encoder = new TextEncoder();
|
|
45
|
+
/**
|
|
46
|
+
* Cuts `text` into slices of at most `limit` UTF-8 bytes, never inside a code point. Text with no
|
|
47
|
+
* bytes still gives one empty slice, so every event leaves one data point at least.
|
|
48
|
+
*/
|
|
49
|
+
export function splitBytes(text, limit = MAX_BEACON_BLOB) {
|
|
50
|
+
const parts = [];
|
|
51
|
+
let current = '';
|
|
52
|
+
let size = 0;
|
|
53
|
+
for (const char of text) {
|
|
54
|
+
const n = encoder.encode(char).length;
|
|
55
|
+
if (size + n > limit) {
|
|
56
|
+
parts.push(current);
|
|
57
|
+
current = '';
|
|
58
|
+
size = 0;
|
|
59
|
+
}
|
|
60
|
+
current += char;
|
|
61
|
+
size += n;
|
|
62
|
+
}
|
|
63
|
+
parts.push(current);
|
|
64
|
+
return parts;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A sink for a Workers Analytics Engine dataset, one data point per event.
|
|
68
|
+
*
|
|
69
|
+
* | | views | other requests |
|
|
70
|
+
* | --- | --- | --- |
|
|
71
|
+
* | index1 | route pattern | path |
|
|
72
|
+
* | blob1 | `view` | `beacon` |
|
|
73
|
+
* | blob2 to blob9 | tier, reason, engine, scope, strategy, route, id, from | content type, body slice, vendor |
|
|
74
|
+
* | double1 to double6 | at, staleSec, swAgeMs, gapMs, mismatch, crossOriginLoads | at, slice number from 0, slice count |
|
|
75
|
+
*
|
|
76
|
+
* `staleSec`, `gapMs` and `crossOriginLoads` are -1 when unknown; `mismatch` is -1 when nothing was
|
|
77
|
+
* compared, 0 for a match and 1 for a mismatch. A page's loads report is a row with `blob1` of
|
|
78
|
+
* `loads`, the view id as `blob2` and `at` and the count as `double1` and `double2`. Analytics
|
|
79
|
+
* Engine caps an index at 96 bytes and a row at 16 KB, so a body longer than {@link MAX_BEACON_BLOB}
|
|
80
|
+
* bytes is written as several points, one per slice, with the same index, content type, vendor and
|
|
81
|
+
* `at`; join the slices by those and `double2` (the slice number) to rebuild it. Nothing is cut off.
|
|
82
|
+
*/
|
|
83
|
+
export function analyticsEngineSink(dataset) {
|
|
84
|
+
return {
|
|
85
|
+
write(batch) {
|
|
86
|
+
for (const event of batch) {
|
|
87
|
+
const view = viewFromBeacon(event);
|
|
88
|
+
const loads = loadsFromBeacon(event);
|
|
89
|
+
if (loads) {
|
|
90
|
+
dataset.writeDataPoint({
|
|
91
|
+
indexes: [event.path],
|
|
92
|
+
blobs: ['loads', loads.view],
|
|
93
|
+
doubles: [loads.at, loads.count]
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else if (view) {
|
|
97
|
+
dataset.writeDataPoint({
|
|
98
|
+
indexes: [view.route.slice(0, 96)],
|
|
99
|
+
blobs: [
|
|
100
|
+
'view',
|
|
101
|
+
view.tier,
|
|
102
|
+
view.reason,
|
|
103
|
+
view.engine,
|
|
104
|
+
view.scope,
|
|
105
|
+
view.strategy,
|
|
106
|
+
view.route,
|
|
107
|
+
view.id,
|
|
108
|
+
view.from ?? null
|
|
109
|
+
],
|
|
110
|
+
doubles: [
|
|
111
|
+
view.at,
|
|
112
|
+
view.staleSec ?? -1,
|
|
113
|
+
view.swAgeMs,
|
|
114
|
+
view.gapMs ?? -1,
|
|
115
|
+
view.mismatch === null ? -1 : Number(view.mismatch),
|
|
116
|
+
view.crossOriginLoads ?? -1
|
|
117
|
+
]
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const slices = splitBytes(event.body);
|
|
122
|
+
slices.forEach((slice, n) => {
|
|
123
|
+
dataset.writeDataPoint({
|
|
124
|
+
indexes: [event.path.slice(0, 96)],
|
|
125
|
+
blobs: ['beacon', event.contentType, slice, event.vendor ?? null],
|
|
126
|
+
doubles: [event.at, n, slices.length]
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** measures the compressed size of some bytes, in bytes */
|
|
2
|
+
export type Sizer = (bytes: Uint8Array) => number | Promise<number>;
|
|
3
|
+
/**
|
|
4
|
+
* Brotli size through `CompressionStream`, which Node, Bun and Deno have. A runtime that refuses
|
|
5
|
+
* `brotli` gets gzip, which is larger, so it ships fewer entries and never more.
|
|
6
|
+
*/
|
|
7
|
+
export declare const compressedSize: Sizer;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
let format;
|
|
2
|
+
/**
|
|
3
|
+
* Brotli size through `CompressionStream`, which Node, Bun and Deno have. A runtime that refuses
|
|
4
|
+
* `brotli` gets gzip, which is larger, so it ships fewer entries and never more.
|
|
5
|
+
*/
|
|
6
|
+
export const compressedSize = async (bytes) => {
|
|
7
|
+
if (format === undefined) {
|
|
8
|
+
try {
|
|
9
|
+
new CompressionStream('brotli');
|
|
10
|
+
format = 'brotli';
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
format = 'gzip';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
const stream = new Response(bytes).body?.pipeThrough(new CompressionStream(format));
|
|
17
|
+
return (await new Response(stream).arrayBuffer()).byteLength;
|
|
18
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type CollectorConfig } from '../protocol/collector.js';
|
|
2
|
+
import { type AssetMode, type ProvisionConfig, type SwConfig, type ThirdPartyConfig } from '../protocol/config.js';
|
|
3
|
+
import { type Route } from '../protocol/route.js';
|
|
4
|
+
import type { Mode } from '../protocol/view.js';
|
|
5
|
+
import type { VersionStore } from '../stores/version-store.js';
|
|
6
|
+
import { type IdentityOptions } from './identity.js';
|
|
7
|
+
import type { Sink } from './sink.js';
|
|
8
|
+
import { type Sizer } from './size.js';
|
|
9
|
+
import type { WorkSeries } from './work.js';
|
|
10
|
+
/** most changed tags a response carries inline before the worker is left to ask `/.spool/sync` */
|
|
11
|
+
export declare const MAX_PIGGYBACK = 32;
|
|
12
|
+
/** most pages one catalogue request makes the server build, whatever its route suggests */
|
|
13
|
+
export declare const MAX_CATALOGUE_BUILD = 32;
|
|
14
|
+
/** what `createSpool` takes */
|
|
15
|
+
export type SpoolOptions = {
|
|
16
|
+
routes: readonly Route[];
|
|
17
|
+
store: VersionStore;
|
|
18
|
+
identity?: IdentityOptions;
|
|
19
|
+
sink?: Sink;
|
|
20
|
+
/**
|
|
21
|
+
* serve the page runtime from a CDN with SRI; omitted, it is served from
|
|
22
|
+
* `/.spool/<version>/client.js`. `url` defaults to this version's `cdn.drupflare.com` build,
|
|
23
|
+
* whose `integrity` is in the release notes and in `dist/cdn/manifest.json`.
|
|
24
|
+
*/
|
|
25
|
+
cdn?: {
|
|
26
|
+
url?: string;
|
|
27
|
+
integrity: string;
|
|
28
|
+
};
|
|
29
|
+
/** same-origin POST paths the worker queues and flushes in one request */
|
|
30
|
+
beacons?: readonly string[];
|
|
31
|
+
/** the mode for routes that do not name one; default `serve`. `shadow` never serves locally */
|
|
32
|
+
mode?: Mode;
|
|
33
|
+
/** the share of views, 0 to 1, that run in shadow while the mode is `serve`; default 0 */
|
|
34
|
+
shadowSample?: number;
|
|
35
|
+
/** whether the worker queues a `spool.view` per page view; default true */
|
|
36
|
+
views?: boolean;
|
|
37
|
+
/** records the time each request took, by route; read it with `work.samples()` */
|
|
38
|
+
work?: WorkSeries;
|
|
39
|
+
/** reads a millisecond clock for `work`; default `performance.now`, which is elapsed time and not CPU */
|
|
40
|
+
meter?: () => number;
|
|
41
|
+
/**
|
|
42
|
+
* priors the worker uses before its own telemetry exists, and the server uses to leave out an
|
|
43
|
+
* artifact whose transfer its expected reuse does not repay
|
|
44
|
+
*/
|
|
45
|
+
provision?: ProvisionConfig;
|
|
46
|
+
/** the most compressed bytes of entries one catalogue bundle may carry; default 32 KiB */
|
|
47
|
+
catalogueBytes?: number;
|
|
48
|
+
/** measures an entry's compressed bytes for the catalogue; default Brotli, gzip where the runtime has no Brotli stream */
|
|
49
|
+
catalogueSize?: Sizer;
|
|
50
|
+
/** how the worker treats same-origin subresources; default `cache` */
|
|
51
|
+
assets?: AssetMode;
|
|
52
|
+
/** same-origin paths whose request ends the session; the worker wipes the principal store first */
|
|
53
|
+
logout?: readonly string[];
|
|
54
|
+
/** cross-origin analytics collectors the worker holds and delivers later */
|
|
55
|
+
collectors?: readonly CollectorConfig[];
|
|
56
|
+
/** the opt-in worker cache for versioned cross-origin files */
|
|
57
|
+
thirdParty?: ThirdPartyConfig;
|
|
58
|
+
/**
|
|
59
|
+
* a bundled worker to serve in place of the embedded one, such as a bundle of `serve(config,
|
|
60
|
+
* renderers)` from `@drupflare/spool/sw`; it finds the config in `self.__SPOOL_CONFIG__`
|
|
61
|
+
*/
|
|
62
|
+
worker?: string;
|
|
63
|
+
};
|
|
64
|
+
/** per-call overrides for {@link Spool.decorate} */
|
|
65
|
+
export type DecorateOptions = {
|
|
66
|
+
/** the cursor read before the response was built; read now when omitted */
|
|
67
|
+
vector?: number;
|
|
68
|
+
/** `Spool-Clear` tokens to send, such as `['*']` on logout */
|
|
69
|
+
clear?: readonly string[];
|
|
70
|
+
/** also send `Clear-Site-Data: "storage"`, the browser's own wipe of everything the origin holds */
|
|
71
|
+
hardReset?: boolean;
|
|
72
|
+
};
|
|
73
|
+
/** what `createSpool` returns */
|
|
74
|
+
export type Spool = {
|
|
75
|
+
/** answers `/.spool/*` and the worker script, otherwise calls `next` and decorates */
|
|
76
|
+
handle(request: Request, next: (request: Request) => Response | Promise<Response>): Promise<Response>;
|
|
77
|
+
/** a `Response` for `/.spool/*`, `undefined` for anything else */
|
|
78
|
+
respond(request: Request): Promise<Response | undefined>;
|
|
79
|
+
/** adds the envelope headers and the client tag to a response the app produced */
|
|
80
|
+
decorate(request: Request, response: Response, options?: DecorateOptions): Promise<Response>;
|
|
81
|
+
/** bumps the vector after a write; `drop` also removes artifacts by id */
|
|
82
|
+
invalidate(tags: readonly string[], options?: {
|
|
83
|
+
drop?: readonly string[];
|
|
84
|
+
}): Promise<number>;
|
|
85
|
+
/**
|
|
86
|
+
* The `Spool-Partition` value for a principal. A login or account-switch handler sets it on its
|
|
87
|
+
* own response, since the request it answers still carries the old identity: the worker then
|
|
88
|
+
* swaps its store before any local view can read the old one.
|
|
89
|
+
*/
|
|
90
|
+
partition(principal: string): Promise<string>;
|
|
91
|
+
/** the `/spool-sw.js` response */
|
|
92
|
+
stub(): Response;
|
|
93
|
+
/** the config embedded in the worker script */
|
|
94
|
+
readonly config: SwConfig;
|
|
95
|
+
};
|
|
96
|
+
/** wires routes, a version store and the endpoints into one handler set */
|
|
97
|
+
export declare function createSpool(options: SpoolOptions): Spool;
|