@drupflare/spool 0.3.0 → 1.0.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 (206) hide show
  1. package/README.md +279 -1607
  2. package/dist/angular/index.d.ts +13 -0
  3. package/dist/angular/index.js +25 -0
  4. package/dist/angular/pages.d.ts +28 -0
  5. package/dist/angular/pages.js +85 -0
  6. package/dist/cdn/client.js +1 -1
  7. package/dist/cdn/location.d.ts +6 -0
  8. package/dist/cdn/location.js +8 -0
  9. package/dist/cdn/manifest.json +7 -7
  10. package/dist/cdn/spool-sw.js +1 -1
  11. package/dist/client/bootstrap.d.ts +57 -0
  12. package/dist/client/bootstrap.js +307 -0
  13. package/dist/client/contact.d.ts +24 -0
  14. package/dist/client/contact.js +66 -0
  15. package/dist/client/declared.d.ts +23 -0
  16. package/dist/client/declared.js +68 -0
  17. package/dist/client/entry.js +123 -2
  18. package/dist/client/gate.d.ts +1 -0
  19. package/dist/client/gate.js +148 -0
  20. package/dist/client/index.d.ts +25 -0
  21. package/dist/client/index.js +1 -0
  22. package/dist/client/live.d.ts +66 -0
  23. package/dist/client/live.js +225 -0
  24. package/dist/client/navigate-entry.d.ts +1 -0
  25. package/dist/client/navigate-entry.js +4 -0
  26. package/dist/client/navigate.d.ts +54 -0
  27. package/dist/client/navigate.js +272 -0
  28. package/dist/client/persist.d.ts +2 -0
  29. package/dist/client/persist.js +9 -0
  30. package/dist/client/register.d.ts +34 -2
  31. package/dist/client/register.js +64 -7
  32. package/dist/express/index.d.ts +11 -0
  33. package/dist/express/index.js +209 -0
  34. package/dist/hono/index.d.ts +19 -0
  35. package/dist/hono/index.js +49 -0
  36. package/dist/next/index.d.ts +33 -0
  37. package/dist/next/index.js +41 -0
  38. package/dist/next/pages.d.ts +22 -0
  39. package/dist/next/pages.js +39 -0
  40. package/dist/node/listener.d.ts +3 -0
  41. package/dist/node/listener.js +57 -2
  42. package/dist/nuxt/index.d.ts +22 -0
  43. package/dist/nuxt/index.js +104 -0
  44. package/dist/nuxt/pages.d.ts +26 -0
  45. package/dist/nuxt/pages.js +48 -0
  46. package/dist/nuxt/runtime/index.d.ts +24 -0
  47. package/dist/nuxt/runtime/index.js +58 -0
  48. package/dist/plugins/coverage.d.ts +14 -0
  49. package/dist/plugins/coverage.js +27 -0
  50. package/dist/plugins/mode.d.ts +19 -0
  51. package/dist/plugins/mode.js +20 -0
  52. package/dist/plugins/path.d.ts +16 -0
  53. package/dist/plugins/path.js +139 -0
  54. package/dist/plugins/report.d.ts +22 -0
  55. package/dist/plugins/report.js +18 -0
  56. package/dist/plugins/routes.d.ts +38 -0
  57. package/dist/plugins/routes.js +52 -0
  58. package/dist/plugins/static.d.ts +42 -0
  59. package/dist/plugins/static.js +97 -0
  60. package/dist/protocol/bundle.d.ts +111 -0
  61. package/dist/protocol/bundle.js +428 -0
  62. package/dist/protocol/config.d.ts +80 -1
  63. package/dist/protocol/config.js +31 -1
  64. package/dist/protocol/dataset.d.ts +189 -0
  65. package/dist/protocol/dataset.js +212 -0
  66. package/dist/protocol/envelope.d.ts +34 -1
  67. package/dist/protocol/envelope.js +16 -1
  68. package/dist/protocol/errors.d.ts +1 -1
  69. package/dist/protocol/graph.d.ts +23 -4
  70. package/dist/protocol/graph.js +34 -3
  71. package/dist/protocol/index-rows.d.ts +22 -0
  72. package/dist/protocol/index-rows.js +53 -0
  73. package/dist/protocol/index.d.ts +5 -0
  74. package/dist/protocol/index.js +5 -0
  75. package/dist/protocol/normalise.d.ts +4 -4
  76. package/dist/protocol/normalise.js +9 -5
  77. package/dist/protocol/plan.d.ts +100 -0
  78. package/dist/protocol/plan.js +661 -0
  79. package/dist/protocol/provision.d.ts +46 -0
  80. package/dist/protocol/provision.js +38 -0
  81. package/dist/protocol/query.d.ts +66 -5
  82. package/dist/protocol/query.js +103 -4
  83. package/dist/protocol/ring.d.ts +58 -0
  84. package/dist/protocol/ring.js +6 -0
  85. package/dist/protocol/route.d.ts +63 -13
  86. package/dist/protocol/route.js +126 -6
  87. package/dist/protocol/tag.d.ts +22 -2
  88. package/dist/protocol/tag.js +25 -4
  89. package/dist/protocol/view.d.ts +2 -0
  90. package/dist/protocol/view.js +8 -2
  91. package/dist/protocol/views.d.ts +6 -0
  92. package/dist/protocol/views.js +15 -0
  93. package/dist/protocol/write.d.ts +234 -0
  94. package/dist/protocol/write.js +343 -0
  95. package/dist/proxy/eligible.d.ts +22 -1
  96. package/dist/proxy/eligible.js +62 -15
  97. package/dist/proxy/proxy.js +28 -18
  98. package/dist/react-router/index.d.ts +20 -0
  99. package/dist/react-router/index.js +60 -0
  100. package/dist/react-router/pages.d.ts +34 -0
  101. package/dist/react-router/pages.js +83 -0
  102. package/dist/report/dataset.d.ts +48 -0
  103. package/dist/report/dataset.js +93 -0
  104. package/dist/report/demand.d.ts +49 -0
  105. package/dist/report/demand.js +60 -0
  106. package/dist/report/derive.d.ts +285 -0
  107. package/dist/report/derive.js +676 -1
  108. package/dist/report/economics.d.ts +66 -1
  109. package/dist/report/economics.js +208 -1
  110. package/dist/report/headline.d.ts +158 -0
  111. package/dist/report/headline.js +473 -0
  112. package/dist/report/index.d.ts +4 -0
  113. package/dist/report/index.js +4 -0
  114. package/dist/report/inputs.d.ts +176 -0
  115. package/dist/report/inputs.js +248 -0
  116. package/dist/report/institution.d.ts +188 -1
  117. package/dist/report/institution.js +146 -0
  118. package/dist/report/loops.d.ts +73 -0
  119. package/dist/report/loops.js +101 -0
  120. package/dist/report/measurement.d.ts +1 -1
  121. package/dist/report/measurement.js +7 -1
  122. package/dist/report/model.d.ts +2 -0
  123. package/dist/report/model.js +4 -0
  124. package/dist/report/thirdparty.d.ts +57 -0
  125. package/dist/report/thirdparty.js +111 -0
  126. package/dist/report/traffic.d.ts +8 -0
  127. package/dist/report/traffic.js +7 -0
  128. package/dist/report/trim.d.ts +56 -0
  129. package/dist/report/trim.js +83 -1
  130. package/dist/server/bundle.d.ts +55 -0
  131. package/dist/server/bundle.js +172 -0
  132. package/dist/server/dataset.d.ts +54 -0
  133. package/dist/server/dataset.js +355 -0
  134. package/dist/server/embedded.d.ts +7 -3
  135. package/dist/server/embedded.js +7 -3
  136. package/dist/server/index.d.ts +6 -0
  137. package/dist/server/index.js +6 -0
  138. package/dist/server/live-sockets.d.ts +54 -0
  139. package/dist/server/live-sockets.js +131 -0
  140. package/dist/server/live.d.ts +146 -0
  141. package/dist/server/live.js +228 -0
  142. package/dist/server/ring.d.ts +77 -0
  143. package/dist/server/ring.js +311 -0
  144. package/dist/server/spool.d.ts +158 -3
  145. package/dist/server/spool.js +578 -41
  146. package/dist/server/static.d.ts +42 -0
  147. package/dist/server/static.js +29 -0
  148. package/dist/server/stub.d.ts +43 -5
  149. package/dist/server/stub.js +126 -8
  150. package/dist/server/write.d.ts +49 -0
  151. package/dist/server/write.js +223 -0
  152. package/dist/stores/durable-object.d.ts +46 -0
  153. package/dist/stores/durable-object.js +114 -0
  154. package/dist/sw/analytics.d.ts +7 -1
  155. package/dist/sw/analytics.js +16 -2
  156. package/dist/sw/coverage.d.ts +6 -0
  157. package/dist/sw/coverage.js +34 -0
  158. package/dist/sw/dataset-sync.d.ts +76 -0
  159. package/dist/sw/dataset-sync.js +149 -0
  160. package/dist/sw/dataset.d.ts +91 -0
  161. package/dist/sw/dataset.js +318 -0
  162. package/dist/sw/family.d.ts +15 -0
  163. package/dist/sw/family.js +48 -0
  164. package/dist/sw/graph/index.d.ts +31 -1
  165. package/dist/sw/graph/index.js +57 -5
  166. package/dist/sw/graph/invalidate.d.ts +15 -3
  167. package/dist/sw/graph/invalidate.js +42 -4
  168. package/dist/sw/idb.d.ts +2 -0
  169. package/dist/sw/idb.js +80 -11
  170. package/dist/sw/index.d.ts +2 -0
  171. package/dist/sw/index.js +1 -0
  172. package/dist/sw/pack/store.d.ts +128 -0
  173. package/dist/sw/pack/store.js +737 -0
  174. package/dist/sw/partition.d.ts +6 -1
  175. package/dist/sw/partition.js +21 -2
  176. package/dist/sw/policy.d.ts +2 -0
  177. package/dist/sw/policy.js +11 -0
  178. package/dist/sw/principal-dataset.d.ts +64 -0
  179. package/dist/sw/principal-dataset.js +254 -0
  180. package/dist/sw/probe.d.ts +16 -0
  181. package/dist/sw/probe.js +25 -0
  182. package/dist/sw/render.d.ts +7 -2
  183. package/dist/sw/render.js +19 -5
  184. package/dist/sw/replica.d.ts +12 -3
  185. package/dist/sw/replica.js +40 -5
  186. package/dist/sw/router.d.ts +3 -2
  187. package/dist/sw/router.js +15 -2
  188. package/dist/sw/runtime.d.ts +90 -2
  189. package/dist/sw/runtime.js +857 -61
  190. package/dist/sw/scheduler.d.ts +6 -1
  191. package/dist/sw/scheduler.js +22 -5
  192. package/dist/sw/seal.d.ts +13 -0
  193. package/dist/sw/seal.js +13 -4
  194. package/dist/sw/static.d.ts +18 -0
  195. package/dist/sw/static.js +84 -1
  196. package/dist/sw/storage.d.ts +51 -1
  197. package/dist/sw/storage.js +33 -1
  198. package/dist/sw/write.d.ts +147 -0
  199. package/dist/sw/write.js +625 -0
  200. package/dist/version.d.ts +1 -1
  201. package/dist/version.js +1 -2
  202. package/dist/vite/index.d.ts +26 -0
  203. package/dist/vite/index.js +66 -0
  204. package/dist/vue/index.d.ts +36 -0
  205. package/dist/vue/index.js +53 -0
  206. package/package.json +65 -4
@@ -1,5 +1,5 @@
1
1
  import { type CollectorConfig } from '../protocol/collector.js';
2
- import type { SwConfig } from '../protocol/config.js';
2
+ import { type SwConfig } from '../protocol/config.js';
3
3
  import { type Params } from '../protocol/pattern.js';
4
4
  import { type SerializedRoute } from '../protocol/route.js';
5
5
  /** how the worker treats a request; a `static` hit keys its file by pathname plus query string */
@@ -34,7 +34,8 @@ export type Classified = {
34
34
  };
35
35
  /**
36
36
  * Sorts a request without touching it. A request to another origin is a held collector hit, a
37
- * file of an allowed third-party origin, or ignored so the browser handles it natively. A
37
+ * GET a data family answers for that origin, a file of an allowed third-party origin, or ignored so
38
+ * the browser handles it natively. A
38
39
  * same-origin one is a logout request, a queued beacon, a navigation on a declared route, a GET for
39
40
  * a JSON route, a GET for a subresource (cache-first when immutable), a GET of a file a static
40
41
  * route keeps, or a pass-through that the worker still observes for spool headers.
package/dist/sw/router.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { matchCollector } from '../protocol/collector.js';
2
+ import { LIVE_PATH } from '../protocol/config.js';
2
3
  import { matchPattern } from '../protocol/pattern.js';
3
4
  import { matchRoute } from '../protocol/route.js';
4
5
  /** whether `url` falls under an origin the worker may cache files from */
@@ -7,7 +8,8 @@ function thirdPartyOrigin(config, url) {
7
8
  }
8
9
  /**
9
10
  * Sorts a request without touching it. A request to another origin is a held collector hit, a
10
- * file of an allowed third-party origin, or ignored so the browser handles it natively. A
11
+ * GET a data family answers for that origin, a file of an allowed third-party origin, or ignored so
12
+ * the browser handles it natively. A
11
13
  * same-origin one is a logout request, a queued beacon, a navigation on a declared route, a GET for
12
14
  * a JSON route, a GET for a subresource (cache-first when immutable), a GET of a file a static
13
15
  * route keeps, or a pass-through that the worker still observes for spool headers.
@@ -19,10 +21,20 @@ export function classify(config, request, origin) {
19
21
  if (collector && (request.method === 'POST' || request.method === 'GET')) {
20
22
  return { kind: 'collector', collector };
21
23
  }
24
+ if (request.method === 'GET' && !request.headers.has('range')) {
25
+ const remote = config.routes.filter((route) => route.origin === url.origin);
26
+ const hit = matchRoute(remote, url.pathname);
27
+ if (hit?.route.strategy === 'data' && (url.search === '' || hit.route.query)) {
28
+ return { kind: 'data', route: hit.route, path: url.pathname };
29
+ }
30
+ }
22
31
  if (request.method === 'GET' && thirdPartyOrigin(config, url))
23
32
  return { kind: 'thirdparty' };
24
33
  return { kind: 'ignore' };
25
34
  }
35
+ // the stream is the browser's own long-lived request; the worker never sits in it
36
+ if (url.pathname === LIVE_PATH)
37
+ return { kind: 'ignore' };
26
38
  if (config.logout?.some((pattern) => matchPattern(pattern, url.pathname))) {
27
39
  return { kind: 'logout' };
28
40
  }
@@ -30,7 +42,8 @@ export function classify(config, request, origin) {
30
42
  return { kind: 'beacon' };
31
43
  if (request.method !== 'GET')
32
44
  return { kind: 'pass' };
33
- const hit = matchRoute(config.routes, url.pathname);
45
+ // a cross-origin family answers only its own origin's GETs, never the page's paths
46
+ const hit = matchRoute(config.routes.filter((route) => route.origin === undefined), url.pathname);
34
47
  // an artifact is keyed by path alone, so only a route with a query schema answers a query
35
48
  const queried = url.search !== '' && !hit?.route.query;
36
49
  if (request.mode !== 'navigate') {
@@ -1,10 +1,15 @@
1
1
  import { type AssetMode, type SwConfig } from '../protocol/config.js';
2
+ import { type LiveDelta } from '../protocol/envelope.js';
3
+ import { type WriteFlush, type WriteQueued, type WriteRefusal, type WriteResolved, type WriteStatus } from '../protocol/write.js';
4
+ import { type DatasetRefusal } from './dataset-sync.js';
5
+ import { type PackStore } from './pack/store.js';
2
6
  import type { PartitionHost } from './partition.js';
3
7
  import { Ledger, type LedgerEntry, type LedgerTotals } from './provision.js';
4
8
  import { type Renderers } from './render.js';
5
9
  import { Replica } from './replica.js';
6
10
  import { type RoutableInstall } from './routing.js';
7
- import type { ReplicaStorage } from './storage.js';
11
+ import type { DatasetMeta, ReplicaStorage } from './storage.js';
12
+ import { type WriteTimers } from './write.js';
8
13
  /** the slice of a `Cache` the runtime uses */
9
14
  export type CacheLike = {
10
15
  match(request: Request): Promise<Response | undefined>;
@@ -22,6 +27,11 @@ export type CachesLike = {
22
27
  export type RuntimeDeps = {
23
28
  config: SwConfig;
24
29
  storage: ReplicaStorage;
30
+ /**
31
+ * the pack store family routes read their rows from and a served pack is adopted into; default the
32
+ * browser's IndexedDB, and none where there is none, so family routes go to the network
33
+ */
34
+ pack?: PackStore;
25
35
  fetch(input: Request | string, init?: RequestInit): Promise<Response>;
26
36
  caches: CachesLike;
27
37
  /** the worker's own origin */
@@ -39,6 +49,15 @@ export type RuntimeDeps = {
39
49
  partitions?: PartitionHost;
40
50
  /** tells every page of the origin something, such as that a wipe happened */
41
51
  broadcast?(message: unknown): void;
52
+ /** the storage the origin may use and has used; default `navigator.storage.estimate` */
53
+ estimate?(): Promise<{
54
+ quota?: number;
55
+ usage?: number;
56
+ }>;
57
+ /** the timers the declared-write deadlines run on; default the global timers */
58
+ timers?: WriteTimers;
59
+ /** how long the second lease renewal waits for a page to open the stream before it renews; default 3000 */
60
+ liveWaitMs?: number;
42
61
  };
43
62
  /** the worker's answer to a page's `spool:hello` */
44
63
  export type Hello = {
@@ -46,6 +65,10 @@ export type Hello = {
46
65
  protocol: number;
47
66
  version: string;
48
67
  compatible: boolean;
68
+ /** seconds the browser holds the worker script; absent from a worker built before the lease */
69
+ lease?: number;
70
+ /** epoch milliseconds of the worker's last server answer; absent until it has one */
71
+ contact?: number;
49
72
  };
50
73
  /** the worker's answer to `spool:status` */
51
74
  export type Status = {
@@ -80,6 +103,39 @@ export type PartitionStatus = {
80
103
  artifacts: string[];
81
104
  records: string[];
82
105
  projections: string[];
106
+ /** the principal datasets held for this partition; always empty while the flag is off */
107
+ datasets: DatasetMeta[];
108
+ };
109
+ /**
110
+ * the worker's answer to `spool:live-state`: the site's stream setting (null when it has none), the
111
+ * partition held and the lowest cursor a side holds, which a stream starts its catch-up from
112
+ */
113
+ export type LiveState = {
114
+ type: 'spool:live-state';
115
+ live: NonNullable<SwConfig['live']> | null;
116
+ partition: string | null;
117
+ have: number | null;
118
+ };
119
+ /** what a page tells the worker it read from its stream; `partition` is the one the stream was opened for */
120
+ export type LiveMessage = {
121
+ partition: string | null;
122
+ } & ({
123
+ event: 'delta';
124
+ delta: LiveDelta;
125
+ } | {
126
+ event: 'lease';
127
+ leaseMs: number;
128
+ } | {
129
+ event: 'revoked';
130
+ }
131
+ /** a visible session began: the worker counts lease renewals from zero */
132
+ | {
133
+ event: 'session';
134
+ });
135
+ /** the worker's answer to `spool:live`: false when a delta did not follow the cursor the worker holds, so the page connects again */
136
+ export type LiveAck = {
137
+ type: 'spool:live';
138
+ ok: boolean;
83
139
  };
84
140
  /** the worker's answer to `spool:assets`: the mode asked for and whether Static Routing took effect */
85
141
  export type AssetsStatus = {
@@ -93,8 +149,40 @@ export type HeldStatus = {
93
149
  dropped: Record<string, number>;
94
150
  third: string[];
95
151
  };
152
+ /**
153
+ * the worker's answer to `spool:datasets`: what it holds, how each sync ended (counts by
154
+ * `<name>:<status>` or `<name>:refused:<reason>`), the datasets it will not retry this run, the
155
+ * datasets it found damaged and cleared, and whether a page was granted persistent storage
156
+ */
157
+ export type DatasetStatus = {
158
+ type: 'spool:datasets';
159
+ datasets: DatasetMeta[];
160
+ outcomes: Record<string, number>;
161
+ refused: Record<string, DatasetRefusal>;
162
+ evicted: number;
163
+ persisted: boolean | null;
164
+ };
165
+ /** the worker's answer to `spool:write`, `spool:flush`, `spool:writes` and `spool:resolve` */
166
+ export type WriteReply = {
167
+ type: 'spool:write';
168
+ result: WriteQueued | WriteRefusal;
169
+ } | {
170
+ type: 'spool:flush';
171
+ result: ({
172
+ ok: true;
173
+ } & WriteFlush) | WriteRefusal;
174
+ } | {
175
+ type: 'spool:writes';
176
+ result: {
177
+ ok: true;
178
+ writes: WriteStatus[];
179
+ } | WriteRefusal;
180
+ } | {
181
+ type: 'spool:resolve';
182
+ result: WriteResolved | WriteRefusal;
183
+ };
96
184
  /** every reply the worker gives a page */
97
- export type Reply = Hello | Status | ViewId | GraphStatus | LedgerStatus | PartitionStatus | AssetsStatus | HeldStatus;
185
+ export type Reply = WriteReply | Hello | Status | ViewId | DatasetStatus | GraphStatus | LedgerStatus | PartitionStatus | LiveState | LiveAck | AssetsStatus | HeldStatus;
98
186
  /** the object `serve` wires to service worker events */
99
187
  export type Runtime = {
100
188
  /** a response promise for a request the worker handles, `undefined` for one it leaves alone */