@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.
Files changed (182) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1557 -2
  3. package/dist/cdn/client.js +1 -0
  4. package/dist/cdn/location.d.ts +11 -0
  5. package/dist/cdn/location.js +15 -0
  6. package/dist/cdn/manifest.json +16 -0
  7. package/dist/cdn/spool-sw.js +1 -0
  8. package/dist/client/entry.d.ts +1 -0
  9. package/dist/client/entry.js +25 -0
  10. package/dist/client/index.d.ts +3 -0
  11. package/dist/client/index.js +2 -0
  12. package/dist/client/lifecycle.d.ts +10 -0
  13. package/dist/client/lifecycle.js +10 -0
  14. package/dist/client/loads.d.ts +16 -0
  15. package/dist/client/loads.js +41 -0
  16. package/dist/client/register.d.ts +34 -0
  17. package/dist/client/register.js +58 -0
  18. package/dist/index.d.ts +2 -0
  19. package/dist/index.js +2 -0
  20. package/dist/node/listener.d.ts +19 -0
  21. package/dist/node/listener.js +76 -0
  22. package/dist/protocol/artifact.d.ts +54 -0
  23. package/dist/protocol/artifact.js +79 -0
  24. package/dist/protocol/assembly.d.ts +34 -0
  25. package/dist/protocol/assembly.js +72 -0
  26. package/dist/protocol/beacon.d.ts +59 -0
  27. package/dist/protocol/beacon.js +86 -0
  28. package/dist/protocol/collector.d.ts +41 -0
  29. package/dist/protocol/collector.js +84 -0
  30. package/dist/protocol/config.d.ts +89 -0
  31. package/dist/protocol/config.js +13 -0
  32. package/dist/protocol/envelope.d.ts +47 -0
  33. package/dist/protocol/envelope.js +162 -0
  34. package/dist/protocol/errors.d.ts +7 -0
  35. package/dist/protocol/errors.js +9 -0
  36. package/dist/protocol/graph.d.ts +81 -0
  37. package/dist/protocol/graph.js +201 -0
  38. package/dist/protocol/index.d.ts +19 -0
  39. package/dist/protocol/index.js +19 -0
  40. package/dist/protocol/manifest.d.ts +29 -0
  41. package/dist/protocol/manifest.js +94 -0
  42. package/dist/protocol/normalise.d.ts +20 -0
  43. package/dist/protocol/normalise.js +29 -0
  44. package/dist/protocol/pattern.d.ts +12 -0
  45. package/dist/protocol/pattern.js +82 -0
  46. package/dist/protocol/provision.d.ts +100 -0
  47. package/dist/protocol/provision.js +105 -0
  48. package/dist/protocol/route.d.ts +179 -0
  49. package/dist/protocol/route.js +135 -0
  50. package/dist/protocol/sync.d.ts +31 -0
  51. package/dist/protocol/sync.js +87 -0
  52. package/dist/protocol/tag.d.ts +12 -0
  53. package/dist/protocol/tag.js +26 -0
  54. package/dist/protocol/template/compile.d.ts +35 -0
  55. package/dist/protocol/template/compile.js +82 -0
  56. package/dist/protocol/template/interpret.d.ts +9 -0
  57. package/dist/protocol/template/interpret.js +82 -0
  58. package/dist/protocol/vector.d.ts +32 -0
  59. package/dist/protocol/vector.js +56 -0
  60. package/dist/protocol/view.d.ts +110 -0
  61. package/dist/protocol/view.js +201 -0
  62. package/dist/proxy/eligible.d.ts +45 -0
  63. package/dist/proxy/eligible.js +96 -0
  64. package/dist/proxy/index.d.ts +4 -0
  65. package/dist/proxy/index.js +4 -0
  66. package/dist/proxy/proxy.d.ts +86 -0
  67. package/dist/proxy/proxy.js +388 -0
  68. package/dist/proxy/purge.d.ts +10 -0
  69. package/dist/proxy/purge.js +66 -0
  70. package/dist/proxy/tags.d.ts +8 -0
  71. package/dist/proxy/tags.js +36 -0
  72. package/dist/report/derive.d.ts +221 -0
  73. package/dist/report/derive.js +490 -0
  74. package/dist/report/economics.d.ts +14 -0
  75. package/dist/report/economics.js +24 -0
  76. package/dist/report/index.d.ts +8 -0
  77. package/dist/report/index.js +8 -0
  78. package/dist/report/inputs.d.ts +141 -0
  79. package/dist/report/inputs.js +216 -0
  80. package/dist/report/measurement.d.ts +48 -0
  81. package/dist/report/measurement.js +114 -0
  82. package/dist/report/model.d.ts +42 -0
  83. package/dist/report/model.js +74 -0
  84. package/dist/report/thirdparty.d.ts +62 -0
  85. package/dist/report/thirdparty.js +107 -0
  86. package/dist/report/traffic.d.ts +140 -0
  87. package/dist/report/traffic.js +228 -0
  88. package/dist/report/trim.d.ts +81 -0
  89. package/dist/report/trim.js +105 -0
  90. package/dist/server/beacon.d.ts +20 -0
  91. package/dist/server/beacon.js +47 -0
  92. package/dist/server/embedded.d.ts +6 -0
  93. package/dist/server/embedded.js +6 -0
  94. package/dist/server/ga4.d.ts +70 -0
  95. package/dist/server/ga4.js +165 -0
  96. package/dist/server/identity.d.ts +12 -0
  97. package/dist/server/identity.js +17 -0
  98. package/dist/server/index.d.ts +7 -0
  99. package/dist/server/index.js +7 -0
  100. package/dist/server/sink.d.ts +56 -0
  101. package/dist/server/sink.js +133 -0
  102. package/dist/server/size.d.ts +7 -0
  103. package/dist/server/size.js +18 -0
  104. package/dist/server/spool.d.ts +97 -0
  105. package/dist/server/spool.js +463 -0
  106. package/dist/server/stub.d.ts +9 -0
  107. package/dist/server/stub.js +19 -0
  108. package/dist/server/sync.d.ts +12 -0
  109. package/dist/server/sync.js +48 -0
  110. package/dist/server/work.d.ts +22 -0
  111. package/dist/server/work.js +30 -0
  112. package/dist/stores/durable-object.d.ts +19 -0
  113. package/dist/stores/durable-object.js +53 -0
  114. package/dist/stores/index.d.ts +2 -0
  115. package/dist/stores/index.js +2 -0
  116. package/dist/stores/memory.d.ts +4 -0
  117. package/dist/stores/memory.js +10 -0
  118. package/dist/stores/version-store.d.ts +11 -0
  119. package/dist/stores/version-store.js +1 -0
  120. package/dist/sw/analytics.d.ts +81 -0
  121. package/dist/sw/analytics.js +275 -0
  122. package/dist/sw/collect.d.ts +8 -0
  123. package/dist/sw/collect.js +33 -0
  124. package/dist/sw/core.d.ts +1 -0
  125. package/dist/sw/core.js +3 -0
  126. package/dist/sw/freshness.d.ts +15 -0
  127. package/dist/sw/freshness.js +20 -0
  128. package/dist/sw/graph/index.d.ts +67 -0
  129. package/dist/sw/graph/index.js +170 -0
  130. package/dist/sw/graph/invalidate.d.ts +23 -0
  131. package/dist/sw/graph/invalidate.js +109 -0
  132. package/dist/sw/graph/projections.d.ts +7 -0
  133. package/dist/sw/graph/projections.js +9 -0
  134. package/dist/sw/graph/records.d.ts +7 -0
  135. package/dist/sw/graph/records.js +9 -0
  136. package/dist/sw/graph/store.d.ts +32 -0
  137. package/dist/sw/graph/store.js +52 -0
  138. package/dist/sw/idb.d.ts +8 -0
  139. package/dist/sw/idb.js +117 -0
  140. package/dist/sw/index.d.ts +2 -0
  141. package/dist/sw/index.js +1 -0
  142. package/dist/sw/partition.d.ts +21 -0
  143. package/dist/sw/partition.js +137 -0
  144. package/dist/sw/policy.d.ts +8 -0
  145. package/dist/sw/policy.js +24 -0
  146. package/dist/sw/provision.d.ts +39 -0
  147. package/dist/sw/provision.js +61 -0
  148. package/dist/sw/render.d.ts +21 -0
  149. package/dist/sw/render.js +53 -0
  150. package/dist/sw/replica.d.ts +77 -0
  151. package/dist/sw/replica.js +299 -0
  152. package/dist/sw/router.d.ts +42 -0
  153. package/dist/sw/router.js +48 -0
  154. package/dist/sw/routing.d.ts +23 -0
  155. package/dist/sw/routing.js +48 -0
  156. package/dist/sw/runtime.d.ts +112 -0
  157. package/dist/sw/runtime.js +1070 -0
  158. package/dist/sw/scheduler.d.ts +83 -0
  159. package/dist/sw/scheduler.js +149 -0
  160. package/dist/sw/seal.d.ts +37 -0
  161. package/dist/sw/seal.js +183 -0
  162. package/dist/sw/serve.d.ts +15 -0
  163. package/dist/sw/serve.js +63 -0
  164. package/dist/sw/shadow.d.ts +16 -0
  165. package/dist/sw/shadow.js +27 -0
  166. package/dist/sw/static.d.ts +17 -0
  167. package/dist/sw/static.js +81 -0
  168. package/dist/sw/storage.d.ts +64 -0
  169. package/dist/sw/storage.js +34 -0
  170. package/dist/sw/thirdparty.d.ts +17 -0
  171. package/dist/sw/thirdparty.js +36 -0
  172. package/dist/trim/css.d.ts +38 -0
  173. package/dist/trim/css.js +328 -0
  174. package/dist/trim/index.d.ts +3 -0
  175. package/dist/trim/index.js +3 -0
  176. package/dist/trim/manifest.d.ts +67 -0
  177. package/dist/trim/manifest.js +134 -0
  178. package/dist/trim/tokens.d.ts +43 -0
  179. package/dist/trim/tokens.js +142 -0
  180. package/dist/version.d.ts +2 -0
  181. package/dist/version.js +3 -0
  182. package/package.json +146 -5
@@ -0,0 +1,170 @@
1
+ import { collectRefs, resolveRefs } from '../../protocol/graph.js';
2
+ import { DependencyIndex } from './invalidate.js';
3
+ import { projectionStore } from './projections.js';
4
+ import { recordStore } from './records.js';
5
+ export { DependencyIndex } from './invalidate.js';
6
+ export { projectionStore } from './projections.js';
7
+ export { recordStore } from './records.js';
8
+ export { EntryStore } from './store.js';
9
+ /** whether an artifact references anything in the graph */
10
+ export function usesGraph(artifact) {
11
+ return (artifact.records !== undefined ||
12
+ artifact.projections !== undefined ||
13
+ hasRefs(collectRefs(artifact.body)));
14
+ }
15
+ const hasRefs = (refs) => refs.records.length + refs.projections.length > 0;
16
+ function childProjections(entry) {
17
+ return collectRefs(entry.data).projections;
18
+ }
19
+ /**
20
+ * Records and projections stored once, the index over them, and the rules that tie them to
21
+ * artifacts. Nothing is read from storage until {@link Graph.ensure}: a worker that wakes for a
22
+ * page with no references never touches it.
23
+ */
24
+ export class Graph {
25
+ records;
26
+ projections;
27
+ index = new DependencyIndex();
28
+ #deps;
29
+ #loaded;
30
+ constructor(deps) {
31
+ this.#deps = deps;
32
+ this.records = recordStore(deps.storage);
33
+ this.projections = projectionStore(deps.storage);
34
+ }
35
+ /** whether `ensure` has been called */
36
+ get loaded() {
37
+ return this.#loaded !== undefined;
38
+ }
39
+ /**
40
+ * Loads records and projections, builds the index from the stored artifacts, and drops what a
41
+ * change learned while the graph was asleep outdated. Resolves to the artifacts that lost a
42
+ * dependency on the first call and to nothing after.
43
+ */
44
+ ensure() {
45
+ const first = this.#loaded === undefined;
46
+ this.#loaded ??= this.#load();
47
+ return first ? this.#loaded : this.#loaded.then(() => []);
48
+ }
49
+ async #load() {
50
+ await Promise.all([this.records.load(), this.projections.load()]);
51
+ for (const entry of this.projections.all()) {
52
+ this.index.addProjection(entry.id, entry.deps, childProjections(entry));
53
+ }
54
+ for (const { artifact } of await this.#deps.artifacts()) {
55
+ const uses = collectRefs(artifact.body);
56
+ if (hasRefs(uses))
57
+ this.index.addArtifact(artifact.id, uses);
58
+ }
59
+ const changed = {};
60
+ for (const record of this.records.all()) {
61
+ const version = this.#deps.tagVersion(record.id);
62
+ if (version > record.version)
63
+ changed[record.id] = version;
64
+ }
65
+ for (const projection of this.projections.all()) {
66
+ for (const dep of projection.deps) {
67
+ const version = this.#deps.tagVersion(dep);
68
+ if (version > projection.version)
69
+ changed[dep] = version;
70
+ }
71
+ }
72
+ return (await this.invalidate(changed)).artifacts;
73
+ }
74
+ /** stores what an artifact brought and indexes the artifact's references */
75
+ async put(artifact, persist) {
76
+ await this.records.put(artifact.records ?? [], persist);
77
+ const added = await this.projections.put(artifact.projections ?? [], persist);
78
+ for (const id of added) {
79
+ const entry = this.projections.get(id);
80
+ this.index.addProjection(id, entry.deps, childProjections(entry));
81
+ }
82
+ const uses = collectRefs(artifact.body);
83
+ if (hasRefs(uses))
84
+ this.index.addArtifact(artifact.id, uses);
85
+ }
86
+ /** forgets artifacts in the index; their records stay, so a refetch can reuse them */
87
+ forgetArtifacts(ids) {
88
+ this.index.removeArtifacts(ids);
89
+ }
90
+ /** records and projections held, with the version each is at, for an artifact request's `have` */
91
+ held(limit) {
92
+ const out = {};
93
+ for (const entry of [...this.records.all(), ...this.projections.all()].slice(0, limit)) {
94
+ out[entry.id] = entry.version;
95
+ }
96
+ return out;
97
+ }
98
+ /**
99
+ * Applies tag versions learned from a sync. A record is dropped when its tag moved past its
100
+ * version. A projection is dropped when a tag it reads did, or when a projection it references
101
+ * was. Artifacts that reference anything dropped are reported, not removed: the replica owns them.
102
+ */
103
+ async invalidate(changed) {
104
+ const tags = Object.keys(changed);
105
+ const stale = (id, version) => (changed[id] ?? 0) > version;
106
+ const records = tags.filter((tag) => {
107
+ const held = this.records.get(tag);
108
+ return held !== undefined && stale(tag, held.version);
109
+ });
110
+ const direct = this.index.projectionsReading(tags).filter((id) => {
111
+ const held = this.projections.get(id);
112
+ return held !== undefined && held.deps.some((dep) => stale(dep, held.version));
113
+ });
114
+ const projections = this.index.withParents(direct).filter((id) => this.projections.get(id));
115
+ const artifacts = this.index.artifactsUsing(records, projections);
116
+ await this.records.drop(records);
117
+ await this.projections.drop(projections);
118
+ this.index.removeProjections(projections);
119
+ return { records: records.sort(), projections, artifacts };
120
+ }
121
+ /** forgets everything in memory and in storage */
122
+ async clear() {
123
+ this.records.forget();
124
+ this.projections.forget();
125
+ this.index.clear();
126
+ await this.#deps.storage.clearGraph();
127
+ }
128
+ /** the artifact with every reference replaced by what the graph holds */
129
+ async materialize(artifact) {
130
+ if (artifact.strategy === 'static' || !hasRefs(collectRefs(artifact.body))) {
131
+ return { ok: true, artifact };
132
+ }
133
+ await this.ensure();
134
+ const lookup = {
135
+ record: (id) => this.records.get(id),
136
+ projection: (id) => this.projections.get(id)
137
+ };
138
+ if (artifact.strategy !== 'assembly') {
139
+ const data = resolveRefs(artifact.body.data, lookup);
140
+ if (!data.ok)
141
+ return data;
142
+ return { ok: true, artifact: { ...artifact, body: { data: data.value } } };
143
+ }
144
+ const text = (values) => {
145
+ const out = {};
146
+ for (const [name, value] of Object.entries(values ?? {})) {
147
+ const resolved = resolveRefs(value, lookup);
148
+ if (!resolved.ok)
149
+ return resolved;
150
+ if (typeof resolved.value !== 'string') {
151
+ return { ok: false, reason: `${name} did not resolve to text` };
152
+ }
153
+ out[name] = resolved.value;
154
+ }
155
+ return { ok: true, value: out };
156
+ };
157
+ const fragments = text(artifact.body.fragments);
158
+ if (!fragments.ok)
159
+ return fragments;
160
+ const slots = text(artifact.body.slots);
161
+ if (!slots.ok)
162
+ return slots;
163
+ const body = { shell: artifact.body.shell };
164
+ if (artifact.body.fragments)
165
+ body.fragments = fragments.value;
166
+ if (artifact.body.slots)
167
+ body.slots = slots.value;
168
+ return { ok: true, artifact: { ...artifact, body } };
169
+ }
170
+ }
@@ -0,0 +1,23 @@
1
+ import type { RefSet } from '../../protocol/graph.js';
2
+ /**
3
+ * The dependency index: which projections read a tag, which projections read another projection,
4
+ * and which artifacts use a record or a projection. It answers "what does this change reach"
5
+ * without reading any data.
6
+ */
7
+ export declare class DependencyIndex {
8
+ #private;
9
+ /** records that `projection` reads the tags in `deps` and references the projections in `children` */
10
+ addProjection(projection: string, deps: readonly string[], children: readonly string[]): void;
11
+ removeProjections(ids: readonly string[]): void;
12
+ /** records that `artifact` references these records and projections */
13
+ addArtifact(artifact: string, uses: RefSet): void;
14
+ removeArtifacts(ids: readonly string[]): void;
15
+ /** forgets everything */
16
+ clear(): void;
17
+ /** the projections that read any of `tags` directly */
18
+ projectionsReading(tags: Iterable<string>): string[];
19
+ /** `projections` and every projection that references one of them, directly or not */
20
+ withParents(projections: Iterable<string>): string[];
21
+ /** the artifacts that reference any of these records or projections */
22
+ artifactsUsing(records: Iterable<string>, projections: Iterable<string>): string[];
23
+ }
@@ -0,0 +1,109 @@
1
+ function add(map, key, value) {
2
+ const set = map.get(key) ?? new Set();
3
+ set.add(value);
4
+ map.set(key, set);
5
+ }
6
+ function remove(map, key, value) {
7
+ const set = map.get(key);
8
+ if (!set)
9
+ return;
10
+ set.delete(value);
11
+ if (set.size === 0)
12
+ map.delete(key);
13
+ }
14
+ /**
15
+ * The dependency index: which projections read a tag, which projections read another projection,
16
+ * and which artifacts use a record or a projection. It answers "what does this change reach"
17
+ * without reading any data.
18
+ */
19
+ export class DependencyIndex {
20
+ #projectionsByTag = new Map();
21
+ #parentsByProjection = new Map();
22
+ #artifactsByRecord = new Map();
23
+ #artifactsByProjection = new Map();
24
+ #usesByArtifact = new Map();
25
+ #readsByProjection = new Map();
26
+ /** records that `projection` reads the tags in `deps` and references the projections in `children` */
27
+ addProjection(projection, deps, children) {
28
+ this.removeProjections([projection]);
29
+ this.#readsByProjection.set(projection, { deps: [...deps], children: [...children] });
30
+ for (const tag of deps)
31
+ add(this.#projectionsByTag, tag, projection);
32
+ for (const child of children)
33
+ add(this.#parentsByProjection, child, projection);
34
+ }
35
+ removeProjections(ids) {
36
+ for (const id of ids) {
37
+ const reads = this.#readsByProjection.get(id);
38
+ if (!reads)
39
+ continue;
40
+ for (const tag of reads.deps)
41
+ remove(this.#projectionsByTag, tag, id);
42
+ for (const child of reads.children)
43
+ remove(this.#parentsByProjection, child, id);
44
+ this.#readsByProjection.delete(id);
45
+ }
46
+ }
47
+ /** records that `artifact` references these records and projections */
48
+ addArtifact(artifact, uses) {
49
+ this.removeArtifacts([artifact]);
50
+ this.#usesByArtifact.set(artifact, uses);
51
+ for (const id of uses.records)
52
+ add(this.#artifactsByRecord, id, artifact);
53
+ for (const id of uses.projections)
54
+ add(this.#artifactsByProjection, id, artifact);
55
+ }
56
+ removeArtifacts(ids) {
57
+ for (const id of ids) {
58
+ const uses = this.#usesByArtifact.get(id);
59
+ if (!uses)
60
+ continue;
61
+ for (const record of uses.records)
62
+ remove(this.#artifactsByRecord, record, id);
63
+ for (const projection of uses.projections)
64
+ remove(this.#artifactsByProjection, projection, id);
65
+ this.#usesByArtifact.delete(id);
66
+ }
67
+ }
68
+ /** forgets everything */
69
+ clear() {
70
+ this.#projectionsByTag.clear();
71
+ this.#parentsByProjection.clear();
72
+ this.#artifactsByRecord.clear();
73
+ this.#artifactsByProjection.clear();
74
+ this.#usesByArtifact.clear();
75
+ this.#readsByProjection.clear();
76
+ }
77
+ /** the projections that read any of `tags` directly */
78
+ projectionsReading(tags) {
79
+ const out = new Set();
80
+ for (const tag of tags)
81
+ for (const id of this.#projectionsByTag.get(tag) ?? [])
82
+ out.add(id);
83
+ return [...out].sort();
84
+ }
85
+ /** `projections` and every projection that references one of them, directly or not */
86
+ withParents(projections) {
87
+ const out = new Set();
88
+ const queue = [...projections];
89
+ for (let id = queue.pop(); id !== undefined; id = queue.pop()) {
90
+ if (out.has(id))
91
+ continue;
92
+ out.add(id);
93
+ queue.push(...(this.#parentsByProjection.get(id) ?? []));
94
+ }
95
+ return [...out].sort();
96
+ }
97
+ /** the artifacts that reference any of these records or projections */
98
+ artifactsUsing(records, projections) {
99
+ const out = new Set();
100
+ for (const id of records)
101
+ for (const a of this.#artifactsByRecord.get(id) ?? [])
102
+ out.add(a);
103
+ for (const id of projections) {
104
+ for (const a of this.#artifactsByProjection.get(id) ?? [])
105
+ out.add(a);
106
+ }
107
+ return [...out].sort();
108
+ }
109
+ }
@@ -0,0 +1,7 @@
1
+ import type { ProjectionEntry } from '../../protocol/graph.js';
2
+ import type { ReplicaStorage } from '../storage.js';
3
+ import { EntryStore } from './store.js';
4
+ /** projections derived from records, stored once and shared by every route that references them */
5
+ export type ProjectionStore = EntryStore<ProjectionEntry>;
6
+ /** a projection store over the replica's storage */
7
+ export declare function projectionStore(storage: ReplicaStorage): ProjectionStore;
@@ -0,0 +1,9 @@
1
+ import { EntryStore } from './store.js';
2
+ /** a projection store over the replica's storage */
3
+ export function projectionStore(storage) {
4
+ return new EntryStore({
5
+ all: () => storage.allProjections(),
6
+ put: (entries) => storage.putProjections(entries),
7
+ delete: (ids) => storage.deleteProjections(ids)
8
+ });
9
+ }
@@ -0,0 +1,7 @@
1
+ import type { RecordEntry } from '../../protocol/graph.js';
2
+ import type { ReplicaStorage } from '../storage.js';
3
+ import { EntryStore } from './store.js';
4
+ /** records stored once by id at their newest version */
5
+ export type RecordStore = EntryStore<RecordEntry>;
6
+ /** a record store over the replica's storage */
7
+ export declare function recordStore(storage: ReplicaStorage): RecordStore;
@@ -0,0 +1,9 @@
1
+ import { EntryStore } from './store.js';
2
+ /** a record store over the replica's storage */
3
+ export function recordStore(storage) {
4
+ return new EntryStore({
5
+ all: () => storage.allRecords(),
6
+ put: (entries) => storage.putRecords(entries),
7
+ delete: (ids) => storage.deleteRecords(ids)
8
+ });
9
+ }
@@ -0,0 +1,32 @@
1
+ /** how an {@link EntryStore} reaches durable storage */
2
+ export type EntryBacking<T> = {
3
+ all(): Promise<T[]>;
4
+ put(entries: readonly T[]): Promise<void>;
5
+ delete(ids: readonly string[]): Promise<void>;
6
+ };
7
+ /**
8
+ * Versioned entries by id, each held once at its newest version. An entry that arrives at a
9
+ * version the store already has is not written again.
10
+ */
11
+ export declare class EntryStore<T extends {
12
+ id: string;
13
+ version: number;
14
+ }> {
15
+ #private;
16
+ constructor(backing: EntryBacking<T>);
17
+ /** reads what is persisted */
18
+ load(): Promise<void>;
19
+ get(id: string): T | undefined;
20
+ /** every held entry */
21
+ all(): T[];
22
+ /** ids in sorted order */
23
+ ids(): string[];
24
+ /**
25
+ * Stores the entries newer than what is held and returns their ids. `persist: false` keeps
26
+ * them in memory only and removes any persisted copy, so a restart cannot bring back an older one.
27
+ */
28
+ put(entries: readonly T[], persist: boolean): Promise<string[]>;
29
+ drop(ids: readonly string[]): Promise<void>;
30
+ /** forgets every entry in memory; the caller clears the backing store */
31
+ forget(): void;
32
+ }
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Versioned entries by id, each held once at its newest version. An entry that arrives at a
3
+ * version the store already has is not written again.
4
+ */
5
+ export class EntryStore {
6
+ #backing;
7
+ #entries = new Map();
8
+ constructor(backing) {
9
+ this.#backing = backing;
10
+ }
11
+ /** reads what is persisted */
12
+ async load() {
13
+ for (const entry of await this.#backing.all())
14
+ this.#entries.set(entry.id, entry);
15
+ }
16
+ get(id) {
17
+ return this.#entries.get(id);
18
+ }
19
+ /** every held entry */
20
+ all() {
21
+ return [...this.#entries.values()];
22
+ }
23
+ /** ids in sorted order */
24
+ ids() {
25
+ return [...this.#entries.keys()].sort();
26
+ }
27
+ /**
28
+ * Stores the entries newer than what is held and returns their ids. `persist: false` keeps
29
+ * them in memory only and removes any persisted copy, so a restart cannot bring back an older one.
30
+ */
31
+ async put(entries, persist) {
32
+ const fresh = entries.filter((e) => (this.#entries.get(e.id)?.version ?? -1) < e.version);
33
+ for (const entry of fresh)
34
+ this.#entries.set(entry.id, entry);
35
+ if (fresh.length > 0) {
36
+ if (persist)
37
+ await this.#backing.put(fresh);
38
+ else
39
+ await this.#backing.delete(fresh.map((e) => e.id));
40
+ }
41
+ return fresh.map((e) => e.id);
42
+ }
43
+ async drop(ids) {
44
+ const held = ids.filter((id) => this.#entries.delete(id));
45
+ if (held.length > 0)
46
+ await this.#backing.delete(held);
47
+ }
48
+ /** forgets every entry in memory; the caller clears the backing store */
49
+ forget() {
50
+ this.#entries.clear();
51
+ }
52
+ }
@@ -0,0 +1,8 @@
1
+ import type { ReplicaStorage } from './storage.js';
2
+ /**
3
+ * The replica in IndexedDB: artifacts by id, one meta record and an auto-numbered beacon queue.
4
+ * `factory` is the `indexedDB` global unless a test passes `fake-indexeddb`.
5
+ */
6
+ export declare function idbStorage(factory?: IDBFactory, name?: string): ReplicaStorage & {
7
+ destroy(): Promise<void>;
8
+ };
package/dist/sw/idb.js ADDED
@@ -0,0 +1,117 @@
1
+ const NAME = 'spool';
2
+ const ARTIFACTS = 'artifacts';
3
+ const META = 'meta';
4
+ const BEACONS = 'beacons';
5
+ const RECORDS = 'records';
6
+ const PROJECTIONS = 'projections';
7
+ const META_KEY = 'replica';
8
+ const FLUSH_KEY = 'flush';
9
+ function wrap(request) {
10
+ return new Promise((resolve, reject) => {
11
+ request.onsuccess = () => resolve(request.result);
12
+ request.onerror = () => reject(request.error);
13
+ });
14
+ }
15
+ function done(tx) {
16
+ return new Promise((resolve, reject) => {
17
+ tx.oncomplete = () => resolve();
18
+ tx.onerror = () => reject(tx.error);
19
+ tx.onabort = () => reject(tx.error);
20
+ });
21
+ }
22
+ /**
23
+ * The replica in IndexedDB: artifacts by id, one meta record and an auto-numbered beacon queue.
24
+ * `factory` is the `indexedDB` global unless a test passes `fake-indexeddb`.
25
+ */
26
+ export function idbStorage(factory = indexedDB, name = NAME) {
27
+ let opened;
28
+ const open = () => (opened ??= new Promise((resolve, reject) => {
29
+ const request = factory.open(name, 2);
30
+ request.onupgradeneeded = () => {
31
+ const db = request.result;
32
+ const missing = (name) => !db.objectStoreNames.contains(name);
33
+ if (missing(ARTIFACTS))
34
+ db.createObjectStore(ARTIFACTS, { keyPath: 'artifact.id' });
35
+ if (missing(META))
36
+ db.createObjectStore(META);
37
+ if (missing(BEACONS))
38
+ db.createObjectStore(BEACONS, { autoIncrement: true });
39
+ if (missing(RECORDS))
40
+ db.createObjectStore(RECORDS, { keyPath: 'id' });
41
+ if (missing(PROJECTIONS))
42
+ db.createObjectStore(PROJECTIONS, { keyPath: 'id' });
43
+ };
44
+ request.onsuccess = () => {
45
+ const db = request.result;
46
+ db.onversionchange = () => {
47
+ db.close();
48
+ opened = undefined;
49
+ };
50
+ resolve(db);
51
+ };
52
+ request.onerror = () => reject(request.error);
53
+ }));
54
+ async function tx(store, mode, run) {
55
+ const db = await open();
56
+ const transaction = db.transaction(store, mode);
57
+ const finished = done(transaction);
58
+ const result = await run(transaction.objectStore(store));
59
+ await finished;
60
+ return result;
61
+ }
62
+ return {
63
+ destroy: async () => {
64
+ const db = await opened?.catch(() => undefined);
65
+ db?.close();
66
+ opened = undefined;
67
+ await new Promise((resolve) => {
68
+ const request = factory.deleteDatabase(name);
69
+ request.onsuccess = () => resolve();
70
+ request.onerror = () => resolve();
71
+ });
72
+ },
73
+ getArtifact: (id) => tx(ARTIFACTS, 'readonly', (s) => wrap(s.get(id))),
74
+ putArtifact: (stored) => tx(ARTIFACTS, 'readwrite', async (s) => void (await wrap(s.put(stored)))),
75
+ deleteArtifacts: (ids) => tx(ARTIFACTS, 'readwrite', async (s) => {
76
+ await Promise.all(ids.map((id) => wrap(s.delete(id))));
77
+ }),
78
+ allArtifacts: () => tx(ARTIFACTS, 'readonly', (s) => wrap(s.getAll())),
79
+ clearArtifacts: () => tx(ARTIFACTS, 'readwrite', async (s) => void (await wrap(s.clear()))),
80
+ getMeta: () => tx(META, 'readonly', (s) => wrap(s.get(META_KEY))),
81
+ setMeta: (meta) => tx(META, 'readwrite', async (s) => void (await wrap(s.put(meta, META_KEY)))),
82
+ addBeacon: (event) => tx(BEACONS, 'readwrite', async (s) => void (await wrap(s.add(event)))),
83
+ readBeacons: () => tx(BEACONS, 'readonly', async (s) => {
84
+ const [keys, events] = await Promise.all([
85
+ wrap(s.getAllKeys()),
86
+ wrap(s.getAll())
87
+ ]);
88
+ return keys.map((key, i) => ({
89
+ key: key,
90
+ event: events[i]
91
+ }));
92
+ }),
93
+ removeBeacons: (keys) => tx(BEACONS, 'readwrite', async (s) => {
94
+ await Promise.all(keys.map((key) => wrap(s.delete(key))));
95
+ }),
96
+ getFlush: () => tx(META, 'readonly', (s) => wrap(s.get(FLUSH_KEY))),
97
+ setFlush: (state) => tx(META, 'readwrite', async (s) => void (await wrap(s.put(state, FLUSH_KEY)))),
98
+ putRecords: (entries) => tx(RECORDS, 'readwrite', async (s) => {
99
+ await Promise.all(entries.map((entry) => wrap(s.put(entry))));
100
+ }),
101
+ allRecords: () => tx(RECORDS, 'readonly', (s) => wrap(s.getAll())),
102
+ deleteRecords: (ids) => tx(RECORDS, 'readwrite', async (s) => {
103
+ await Promise.all(ids.map((id) => wrap(s.delete(id))));
104
+ }),
105
+ putProjections: (entries) => tx(PROJECTIONS, 'readwrite', async (s) => {
106
+ await Promise.all(entries.map((entry) => wrap(s.put(entry))));
107
+ }),
108
+ allProjections: () => tx(PROJECTIONS, 'readonly', (s) => wrap(s.getAll())),
109
+ deleteProjections: (ids) => tx(PROJECTIONS, 'readwrite', async (s) => {
110
+ await Promise.all(ids.map((id) => wrap(s.delete(id))));
111
+ }),
112
+ clearGraph: async () => {
113
+ await tx(RECORDS, 'readwrite', async (s) => void (await wrap(s.clear())));
114
+ await tx(PROJECTIONS, 'readwrite', async (s) => void (await wrap(s.clear())));
115
+ }
116
+ };
117
+ }
@@ -0,0 +1,2 @@
1
+ export type { Renderer, Renderers } from './render.js';
2
+ export { MANIFEST_FILE, manifestConfig, serve } from './serve.js';
@@ -0,0 +1 @@
1
+ export { MANIFEST_FILE, manifestConfig, serve } from './serve.js';
@@ -0,0 +1,21 @@
1
+ import type { Retention } from '../protocol/route.js';
2
+ import { type KeyVault, type SealedStorage } from './seal.js';
3
+ /**
4
+ * Keys in two places. A `persistent` key is a non-extractable `CryptoKey` in IndexedDB, so it
5
+ * survives a worker restart and a browser restart and is deleted on logout or a change of
6
+ * partition. A `session` key lives in this worker's memory and is lost when the browser stops the
7
+ * worker; what it sealed is unreadable from then on.
8
+ */
9
+ export declare function idbVault(factory?: IDBFactory): KeyVault;
10
+ /** how the worker finds, opens and destroys the stores of the partition in use */
11
+ export interface PartitionHost {
12
+ /** the partition the stores belong to now, or null */
13
+ current(): Promise<string | null>;
14
+ setCurrent(id: string | null): Promise<void>;
15
+ /** the sealed store of a partition; `retentionOf` names the key of each artifact's route */
16
+ open(id: string, retentionOf: (artifactId: string) => Retention, shared: Retention): SealedStorage;
17
+ /** shreds the keys first, then deletes the store; the store is shut before either */
18
+ destroy(id: string): Promise<void>;
19
+ }
20
+ /** partitions as IndexedDB databases named for the partition, sealed under `vault` */
21
+ export declare function idbPartitions(factory?: IDBFactory, vault?: KeyVault): PartitionHost;