@drupflare/spool 0.4.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.
- package/README.md +206 -1756
- package/dist/angular/index.d.ts +13 -0
- package/dist/angular/index.js +25 -0
- package/dist/angular/pages.d.ts +28 -0
- package/dist/angular/pages.js +85 -0
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/location.d.ts +6 -0
- package/dist/cdn/location.js +8 -0
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/bootstrap.d.ts +57 -0
- package/dist/client/bootstrap.js +307 -0
- package/dist/client/contact.d.ts +24 -0
- package/dist/client/contact.js +66 -0
- package/dist/client/declared.d.ts +23 -0
- package/dist/client/declared.js +68 -0
- package/dist/client/entry.js +91 -2
- package/dist/client/gate.d.ts +1 -0
- package/dist/client/gate.js +148 -0
- package/dist/client/index.d.ts +25 -0
- package/dist/client/index.js +1 -0
- package/dist/client/live.d.ts +66 -0
- package/dist/client/live.js +225 -0
- package/dist/client/navigate-entry.d.ts +1 -0
- package/dist/client/navigate-entry.js +4 -0
- package/dist/client/navigate.d.ts +54 -0
- package/dist/client/navigate.js +272 -0
- package/dist/client/persist.d.ts +2 -0
- package/dist/client/persist.js +9 -0
- package/dist/client/register.d.ts +11 -1
- package/dist/client/register.js +19 -8
- package/dist/express/index.d.ts +11 -0
- package/dist/express/index.js +209 -0
- package/dist/hono/index.d.ts +19 -0
- package/dist/hono/index.js +49 -0
- package/dist/next/index.d.ts +33 -0
- package/dist/next/index.js +41 -0
- package/dist/next/pages.d.ts +22 -0
- package/dist/next/pages.js +39 -0
- package/dist/node/listener.d.ts +3 -0
- package/dist/node/listener.js +57 -2
- package/dist/nuxt/index.d.ts +22 -0
- package/dist/nuxt/index.js +104 -0
- package/dist/nuxt/pages.d.ts +26 -0
- package/dist/nuxt/pages.js +48 -0
- package/dist/nuxt/runtime/index.d.ts +24 -0
- package/dist/nuxt/runtime/index.js +58 -0
- package/dist/plugins/coverage.d.ts +14 -0
- package/dist/plugins/coverage.js +27 -0
- package/dist/plugins/mode.d.ts +19 -0
- package/dist/plugins/mode.js +20 -0
- package/dist/plugins/path.d.ts +16 -0
- package/dist/plugins/path.js +139 -0
- package/dist/plugins/report.d.ts +22 -0
- package/dist/plugins/report.js +18 -0
- package/dist/plugins/routes.d.ts +38 -0
- package/dist/plugins/routes.js +52 -0
- package/dist/plugins/static.d.ts +42 -0
- package/dist/plugins/static.js +97 -0
- package/dist/protocol/bundle.d.ts +111 -0
- package/dist/protocol/bundle.js +428 -0
- package/dist/protocol/config.d.ts +65 -1
- package/dist/protocol/config.js +28 -0
- package/dist/protocol/dataset.d.ts +6 -0
- package/dist/protocol/dataset.js +6 -0
- package/dist/protocol/envelope.d.ts +33 -1
- package/dist/protocol/envelope.js +15 -1
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/index-rows.d.ts +22 -0
- package/dist/protocol/index-rows.js +53 -0
- package/dist/protocol/index.d.ts +2 -0
- package/dist/protocol/index.js +2 -0
- package/dist/protocol/normalise.d.ts +4 -4
- package/dist/protocol/normalise.js +9 -5
- package/dist/protocol/ring.d.ts +58 -0
- package/dist/protocol/ring.js +6 -0
- package/dist/protocol/route.d.ts +57 -11
- package/dist/protocol/route.js +95 -3
- package/dist/protocol/tag.d.ts +22 -2
- package/dist/protocol/tag.js +25 -4
- package/dist/protocol/view.d.ts +2 -0
- package/dist/protocol/view.js +8 -2
- package/dist/protocol/views.d.ts +6 -0
- package/dist/protocol/views.js +15 -0
- package/dist/protocol/write.d.ts +92 -4
- package/dist/protocol/write.js +251 -9
- package/dist/proxy/eligible.d.ts +22 -1
- package/dist/proxy/eligible.js +62 -15
- package/dist/proxy/proxy.js +28 -18
- package/dist/react-router/index.d.ts +20 -0
- package/dist/react-router/index.js +60 -0
- package/dist/react-router/pages.d.ts +34 -0
- package/dist/react-router/pages.js +83 -0
- package/dist/report/demand.d.ts +49 -0
- package/dist/report/demand.js +60 -0
- package/dist/report/economics.d.ts +55 -0
- package/dist/report/economics.js +189 -0
- package/dist/report/headline.d.ts +158 -0
- package/dist/report/headline.js +473 -0
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +2 -0
- package/dist/report/inputs.d.ts +79 -0
- package/dist/report/inputs.js +115 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +5 -1
- package/dist/report/traffic.d.ts +8 -0
- package/dist/report/traffic.js +7 -0
- package/dist/server/bundle.d.ts +55 -0
- package/dist/server/bundle.js +172 -0
- package/dist/server/dataset.d.ts +8 -2
- package/dist/server/dataset.js +51 -4
- package/dist/server/embedded.d.ts +7 -3
- package/dist/server/embedded.js +7 -3
- package/dist/server/index.d.ts +5 -1
- package/dist/server/index.js +5 -1
- package/dist/server/live-sockets.d.ts +54 -0
- package/dist/server/live-sockets.js +131 -0
- package/dist/server/live.d.ts +146 -0
- package/dist/server/live.js +228 -0
- package/dist/server/ring.d.ts +77 -0
- package/dist/server/ring.js +311 -0
- package/dist/server/spool.d.ts +139 -4
- package/dist/server/spool.js +457 -28
- package/dist/server/static.d.ts +42 -0
- package/dist/server/static.js +29 -0
- package/dist/server/stub.d.ts +43 -5
- package/dist/server/stub.js +126 -8
- package/dist/server/write.d.ts +23 -1
- package/dist/server/write.js +96 -0
- package/dist/stores/durable-object.d.ts +46 -0
- package/dist/stores/durable-object.js +114 -0
- package/dist/sw/analytics.d.ts +1 -1
- package/dist/sw/analytics.js +2 -2
- package/dist/sw/coverage.d.ts +6 -0
- package/dist/sw/coverage.js +34 -0
- package/dist/sw/family.d.ts +15 -0
- package/dist/sw/family.js +48 -0
- package/dist/sw/idb.d.ts +2 -0
- package/dist/sw/idb.js +2 -2
- package/dist/sw/index.d.ts +2 -0
- package/dist/sw/index.js +1 -0
- package/dist/sw/pack/store.d.ts +128 -0
- package/dist/sw/pack/store.js +737 -0
- package/dist/sw/policy.d.ts +2 -0
- package/dist/sw/policy.js +11 -0
- package/dist/sw/probe.d.ts +16 -0
- package/dist/sw/probe.js +25 -0
- package/dist/sw/router.d.ts +3 -2
- package/dist/sw/router.js +15 -2
- package/dist/sw/runtime.d.ts +48 -1
- package/dist/sw/runtime.js +548 -68
- package/dist/sw/scheduler.d.ts +6 -1
- package/dist/sw/scheduler.js +22 -5
- package/dist/sw/write.d.ts +41 -1
- package/dist/sw/write.js +188 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -2
- package/dist/vite/index.d.ts +26 -0
- package/dist/vite/index.js +66 -0
- package/dist/vue/index.d.ts +36 -0
- package/dist/vue/index.js +53 -0
- package/package.json +64 -4
package/dist/sw/scheduler.d.ts
CHANGED
|
@@ -76,8 +76,13 @@ export declare class Scheduler {
|
|
|
76
76
|
* next view, when the age is longer.
|
|
77
77
|
*/
|
|
78
78
|
defer(need: Record<string, number>, ageSec: number): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Queues a sync for the next network moment whatever its benefit. `undefined` queues a full sync from
|
|
81
|
+
* the cursor. Returns false when `need` names no id.
|
|
82
|
+
*/
|
|
83
|
+
queue(need: Record<string, number> | undefined, ageSec: number): boolean;
|
|
79
84
|
/** ids queued by {@link Scheduler.defer} */
|
|
80
85
|
get pending(): string[];
|
|
81
|
-
/** runs the queued syncs as one request; true when nothing was queued */
|
|
86
|
+
/** runs the queued syncs as one request, or one full sync when one was queued; true when nothing was queued */
|
|
82
87
|
drain(): Promise<boolean>;
|
|
83
88
|
}
|
package/dist/sw/scheduler.js
CHANGED
|
@@ -51,6 +51,7 @@ export class Scheduler {
|
|
|
51
51
|
#inflight = new Map();
|
|
52
52
|
#policy;
|
|
53
53
|
#pending = {};
|
|
54
|
+
#full = false;
|
|
54
55
|
#pendingAge = 0;
|
|
55
56
|
/** the rate the benefit uses; moves as syncs report what changed */
|
|
56
57
|
rate;
|
|
@@ -104,8 +105,22 @@ export class Scheduler {
|
|
|
104
105
|
});
|
|
105
106
|
if (benefit <= 0)
|
|
106
107
|
return false;
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
return this.queue(need, ageSec);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Queues a sync for the next network moment whatever its benefit. `undefined` queues a full sync from
|
|
112
|
+
* the cursor. Returns false when `need` names no id.
|
|
113
|
+
*/
|
|
114
|
+
queue(need, ageSec) {
|
|
115
|
+
if (need === undefined) {
|
|
116
|
+
this.#full = true;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
if (Object.keys(need).length === 0)
|
|
120
|
+
return false;
|
|
121
|
+
for (const [id, version] of Object.entries(need)) {
|
|
122
|
+
this.#pending[id] = Math.min(version, this.#pending[id] ?? Infinity);
|
|
123
|
+
}
|
|
109
124
|
}
|
|
110
125
|
this.#pendingAge = Math.max(this.#pendingAge, ageSec);
|
|
111
126
|
return true;
|
|
@@ -114,15 +129,17 @@ export class Scheduler {
|
|
|
114
129
|
get pending() {
|
|
115
130
|
return Object.keys(this.#pending).sort();
|
|
116
131
|
}
|
|
117
|
-
/** runs the queued syncs as one request; true when nothing was queued */
|
|
132
|
+
/** runs the queued syncs as one request, or one full sync when one was queued; true when nothing was queued */
|
|
118
133
|
drain() {
|
|
119
134
|
const need = this.#pending;
|
|
120
|
-
|
|
135
|
+
const full = this.#full;
|
|
136
|
+
if (!full && Object.keys(need).length === 0)
|
|
121
137
|
return Promise.resolve(true);
|
|
122
138
|
const age = this.#pendingAge;
|
|
123
139
|
this.#pending = {};
|
|
124
140
|
this.#pendingAge = 0;
|
|
125
|
-
|
|
141
|
+
this.#full = false;
|
|
142
|
+
return this.sync(full ? undefined : need, age);
|
|
126
143
|
}
|
|
127
144
|
async #run(need, ageSec) {
|
|
128
145
|
const { replica, fetchSync, onNetwork } = this.#deps;
|
package/dist/sw/write.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { BundleDelta } from '../protocol/bundle.js';
|
|
2
|
+
import { type IndexRoute } from '../protocol/index-rows.js';
|
|
3
|
+
import { type ResourceWrite, type WriteConfig, type WriteConflict, type WriteFlush, type WriteQueued, type WriteRefusal, type WriteRefusalCode, type WriteResolved, type WriteState, type WriteStatus } from '../protocol/write.js';
|
|
4
|
+
import type { PackStore } from './pack/store.js';
|
|
2
5
|
import { type KeyVault } from './seal.js';
|
|
3
6
|
/** one queued value as the worker holds it, whole and unsealed */
|
|
4
7
|
export type WriteRecord = {
|
|
@@ -19,6 +22,8 @@ export type WriteRecord = {
|
|
|
19
22
|
edits: number;
|
|
20
23
|
/** flushes that failed with the outcome unknown; the same id and value are sent again */
|
|
21
24
|
attempts: number;
|
|
25
|
+
/** epoch ms the class deadline falls due; absent when the class has none or a flush failed */
|
|
26
|
+
due?: number;
|
|
22
27
|
/** an edit made after a request for `value` may have left; it goes out in a later flush */
|
|
23
28
|
next?: {
|
|
24
29
|
value: unknown;
|
|
@@ -56,6 +61,11 @@ export type WriteAnswer = {
|
|
|
56
61
|
status: number;
|
|
57
62
|
body?: unknown;
|
|
58
63
|
};
|
|
64
|
+
/** the timer a write deadline is set on; a test injects one that fires on demand */
|
|
65
|
+
export type WriteTimers = {
|
|
66
|
+
set(fn: () => unknown, ms: number): unknown;
|
|
67
|
+
clear(handle: unknown): void;
|
|
68
|
+
};
|
|
59
69
|
/** what {@link WriteQueue} needs from the worker */
|
|
60
70
|
export type WriteQueueDeps = {
|
|
61
71
|
store: WriteStore;
|
|
@@ -66,6 +76,8 @@ export type WriteQueueDeps = {
|
|
|
66
76
|
send(record: WriteRecord): Promise<WriteAnswer>;
|
|
67
77
|
/** called after a flush that tried at least one record, with counts only */
|
|
68
78
|
onFlush?(flush: WriteFlush): void;
|
|
79
|
+
/** the timer the deadlines use; default the global timers */
|
|
80
|
+
timers?: WriteTimers;
|
|
69
81
|
};
|
|
70
82
|
/**
|
|
71
83
|
* The device queue of one partition for declared writes of the `last-value` kind. One record holds
|
|
@@ -81,6 +93,12 @@ export declare class WriteQueue {
|
|
|
81
93
|
constructor(deps: WriteQueueDeps);
|
|
82
94
|
/** stops the queue for good: no later call sends or stores anything */
|
|
83
95
|
kill(): void;
|
|
96
|
+
/**
|
|
97
|
+
* Sends every queued record when the earliest deadline has passed, and arms the timer for a later
|
|
98
|
+
* one. The worker calls it at a timer fire and at the first contact of each start, so a record
|
|
99
|
+
* whose deadline passed while the worker was stopped goes out once.
|
|
100
|
+
*/
|
|
101
|
+
flushDue(): Promise<WriteFlush>;
|
|
84
102
|
/**
|
|
85
103
|
* Queues `value` as the newest for `(name, key)`. An edit folds into the record already there and
|
|
86
104
|
* keeps the version the first edit started from. When a request for the earlier value may have
|
|
@@ -105,3 +123,25 @@ export declare class WriteQueue {
|
|
|
105
123
|
*/
|
|
106
124
|
resolve(name: string, key: string, use: 'local' | 'server'): Promise<WriteResolved | WriteRefusal>;
|
|
107
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Brings a held pack to the version a declared write's answer reached. The answer's `Spool-Delta` goes
|
|
128
|
+
* first; one that starts at another version, or that the store refuses, costs one delta fetch to the
|
|
129
|
+
* version the answer names. The store checks a delta's digest before it writes, so a refused delta
|
|
130
|
+
* changes nothing. An answer that landed on another origin, after a redirect, applies nothing.
|
|
131
|
+
*/
|
|
132
|
+
/** the declared write a page sent, with its URL, its JSON body and the routes its index tables read */
|
|
133
|
+
export type SentWrite = {
|
|
134
|
+
write: ResourceWrite;
|
|
135
|
+
url: URL;
|
|
136
|
+
body: unknown;
|
|
137
|
+
routes: readonly IndexRoute[];
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* Brings the held pack to the version a declared write's answer reached. The answer's `Spool-Delta` goes
|
|
141
|
+
* first; one that starts at another version, or that the store refuses, costs one delta fetch to the
|
|
142
|
+
* version the answer names. An answer that names no version and sends no delta is predicted from the
|
|
143
|
+
* write's mode when `sent` is given. An answer with a digest and no delta is predicted, and the digest
|
|
144
|
+
* confirms the prediction or drops it at the cost of one delta fetch. An answer that landed on another
|
|
145
|
+
* origin, after a redirect, applies nothing.
|
|
146
|
+
*/
|
|
147
|
+
export declare function settleWrite(response: Response, pack: PackStore, fetchDelta: (from: number, to: number) => Promise<BundleDelta>, expected: string, sent?: SentWrite): Promise<void>;
|
package/dist/sw/write.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HEADERS } from '../protocol/envelope.js';
|
|
2
|
+
import { indexPlan, indexRowsOf } from '../protocol/index-rows.js';
|
|
3
|
+
import { matchPattern } from '../protocol/pattern.js';
|
|
4
|
+
import { applyPatch, isWriteKey, isWriteVersion, newWriteId, WRITE_HEADERS, WRITE_STATUS } from '../protocol/write.js';
|
|
2
5
|
import { seal, unseal } from './seal.js';
|
|
3
6
|
const DB = 'spool-writes';
|
|
4
7
|
const QUEUE = 'queue';
|
|
@@ -127,6 +130,10 @@ export async function destroyWrites(factory, partition) {
|
|
|
127
130
|
db.close();
|
|
128
131
|
}
|
|
129
132
|
}
|
|
133
|
+
const browserTimers = {
|
|
134
|
+
set: (fn, ms) => setTimeout(fn, ms),
|
|
135
|
+
clear: (handle) => clearTimeout(handle)
|
|
136
|
+
};
|
|
130
137
|
const refuse = (code, message) => ({
|
|
131
138
|
ok: false,
|
|
132
139
|
code,
|
|
@@ -159,15 +166,74 @@ function conflictOf(body) {
|
|
|
159
166
|
*/
|
|
160
167
|
export class WriteQueue {
|
|
161
168
|
#deps;
|
|
169
|
+
#timers;
|
|
162
170
|
#tail = Promise.resolve();
|
|
163
171
|
#flushing = null;
|
|
164
172
|
#dead = false;
|
|
173
|
+
#timer;
|
|
165
174
|
constructor(deps) {
|
|
166
175
|
this.#deps = deps;
|
|
176
|
+
this.#timers = deps.timers ?? browserTimers;
|
|
167
177
|
}
|
|
168
178
|
/** stops the queue for good: no later call sends or stores anything */
|
|
169
179
|
kill() {
|
|
170
180
|
this.#dead = true;
|
|
181
|
+
this.#disarm();
|
|
182
|
+
}
|
|
183
|
+
/** the deadline a record of `name` falls due at, when its class has one */
|
|
184
|
+
#dueFor(name, now) {
|
|
185
|
+
const delay = this.#deps.classes().find((c) => c.name === name)?.maxDelayMs;
|
|
186
|
+
return delay === undefined ? undefined : now + delay;
|
|
187
|
+
}
|
|
188
|
+
/** sets the timer for `at` unless an earlier one is set */
|
|
189
|
+
#arm(at) {
|
|
190
|
+
if (this.#dead || (this.#timer !== undefined && this.#timer.at <= at))
|
|
191
|
+
return;
|
|
192
|
+
this.#disarm();
|
|
193
|
+
const handle = this.#timers.set(() => this.#fire(), Math.max(0, at - this.#deps.now()));
|
|
194
|
+
this.#timer = { at, handle };
|
|
195
|
+
}
|
|
196
|
+
#disarm() {
|
|
197
|
+
if (this.#timer !== undefined)
|
|
198
|
+
this.#timers.clear(this.#timer.handle);
|
|
199
|
+
this.#timer = undefined;
|
|
200
|
+
}
|
|
201
|
+
#fire() {
|
|
202
|
+
this.#timer = undefined;
|
|
203
|
+
return this.flushDue().catch(() => ({ queued: 0, flushed: 0, conflicted: 0 }));
|
|
204
|
+
}
|
|
205
|
+
/** the earliest deadline a stored record holds, or undefined when none does */
|
|
206
|
+
async #earliest(after = -Infinity) {
|
|
207
|
+
const dues = (await this.#deps.store.all())
|
|
208
|
+
.filter((r) => r.state !== 'conflicted' && r.due !== undefined)
|
|
209
|
+
.map((r) => r.due)
|
|
210
|
+
.filter((due) => due > after);
|
|
211
|
+
return dues.length > 0 ? Math.min(...dues) : undefined;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Sets the timer to the earliest deadline still ahead, or clears it. A deadline already past is left
|
|
215
|
+
* to a later contact, so a store that keeps failing cannot turn the timer into a loop.
|
|
216
|
+
*/
|
|
217
|
+
async #rearm() {
|
|
218
|
+
const earliest = await this.#earliest(this.#deps.now());
|
|
219
|
+
this.#disarm();
|
|
220
|
+
if (earliest !== undefined)
|
|
221
|
+
this.#arm(earliest);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Sends every queued record when the earliest deadline has passed, and arms the timer for a later
|
|
225
|
+
* one. The worker calls it at a timer fire and at the first contact of each start, so a record
|
|
226
|
+
* whose deadline passed while the worker was stopped goes out once.
|
|
227
|
+
*/
|
|
228
|
+
async flushDue() {
|
|
229
|
+
const earliest = await this.#serial(async () => this.#dead ? undefined : this.#earliest());
|
|
230
|
+
if (earliest === undefined)
|
|
231
|
+
return { queued: 0, flushed: 0, conflicted: 0 };
|
|
232
|
+
if (earliest > this.#deps.now()) {
|
|
233
|
+
this.#arm(earliest);
|
|
234
|
+
return { queued: 0, flushed: 0, conflicted: 0 };
|
|
235
|
+
}
|
|
236
|
+
return this.flush();
|
|
171
237
|
}
|
|
172
238
|
#serial(fn) {
|
|
173
239
|
const run = this.#tail.then(fn, fn);
|
|
@@ -216,6 +282,7 @@ export class WriteQueue {
|
|
|
216
282
|
if (pending >= config.maxPending) {
|
|
217
283
|
return refuse('too-many', `${config.maxPending} keys of "${name}" are already queued`);
|
|
218
284
|
}
|
|
285
|
+
const due = this.#dueFor(name, now);
|
|
219
286
|
const record = {
|
|
220
287
|
name,
|
|
221
288
|
key,
|
|
@@ -227,21 +294,28 @@ export class WriteQueue {
|
|
|
227
294
|
updatedAt: now,
|
|
228
295
|
bytes,
|
|
229
296
|
edits: 1,
|
|
230
|
-
attempts: 0
|
|
297
|
+
attempts: 0,
|
|
298
|
+
...(due === undefined ? {} : { due })
|
|
231
299
|
};
|
|
232
300
|
await this.#deps.store.put(record);
|
|
301
|
+
if (due !== undefined)
|
|
302
|
+
this.#arm(due);
|
|
233
303
|
return { ok: true, state: record.state, edits: 1 };
|
|
234
304
|
}
|
|
235
305
|
const sent = held.state === 'flushing' || held.attempts > 0;
|
|
306
|
+
const due = held.due ?? (held.state === 'conflicted' ? undefined : this.#dueFor(name, now));
|
|
236
307
|
const next = {
|
|
237
308
|
...held,
|
|
238
309
|
updatedAt: now,
|
|
239
310
|
edits: held.edits + 1,
|
|
311
|
+
...(due === undefined ? {} : { due }),
|
|
240
312
|
...(sent && held.state !== 'conflicted'
|
|
241
313
|
? { next: { value, bytes } }
|
|
242
314
|
: { value, bytes })
|
|
243
315
|
};
|
|
244
316
|
await this.#deps.store.put(next);
|
|
317
|
+
if (due !== undefined)
|
|
318
|
+
this.#arm(due);
|
|
245
319
|
return { ok: true, state: next.state, edits: next.edits };
|
|
246
320
|
});
|
|
247
321
|
}
|
|
@@ -319,6 +393,8 @@ export class WriteQueue {
|
|
|
319
393
|
}
|
|
320
394
|
if (counts.queued > 0 && !this.#dead)
|
|
321
395
|
this.#deps.onFlush?.(counts);
|
|
396
|
+
if (!this.#dead)
|
|
397
|
+
await this.#serial(() => this.#rearm());
|
|
322
398
|
return counts;
|
|
323
399
|
}
|
|
324
400
|
/** applies the answer to the record, in one store step; true when the pass should stop */
|
|
@@ -331,8 +407,10 @@ export class WriteQueue {
|
|
|
331
407
|
if (!current)
|
|
332
408
|
return false;
|
|
333
409
|
const retry = async (bumped) => {
|
|
410
|
+
// a failed flush closes the deadline: the record waits for a moment, a start or a new write
|
|
411
|
+
const { due: _due, ...rest } = current;
|
|
334
412
|
await store.put({
|
|
335
|
-
...
|
|
413
|
+
...rest,
|
|
336
414
|
state: 'pending',
|
|
337
415
|
updatedAt: now,
|
|
338
416
|
attempts: current.attempts + (bumped ? 1 : 0)
|
|
@@ -352,6 +430,7 @@ export class WriteQueue {
|
|
|
352
430
|
return false;
|
|
353
431
|
}
|
|
354
432
|
const { value, bytes } = current.next;
|
|
433
|
+
const due = this.#dueFor(current.name, now);
|
|
355
434
|
await store.put({
|
|
356
435
|
name: current.name,
|
|
357
436
|
key: current.key,
|
|
@@ -363,7 +442,8 @@ export class WriteQueue {
|
|
|
363
442
|
queuedAt: current.queuedAt,
|
|
364
443
|
updatedAt: now,
|
|
365
444
|
edits: 1,
|
|
366
|
-
attempts: 0
|
|
445
|
+
attempts: 0,
|
|
446
|
+
...(due === undefined ? {} : { due })
|
|
367
447
|
});
|
|
368
448
|
return false;
|
|
369
449
|
}
|
|
@@ -390,7 +470,7 @@ export class WriteQueue {
|
|
|
390
470
|
}
|
|
391
471
|
/** the record as `conflicted`, keeping the newest local value */
|
|
392
472
|
#park(record, now, why) {
|
|
393
|
-
const { next: later, conflict: _c, refusal: _r, ...rest } = record;
|
|
473
|
+
const { next: later, conflict: _c, refusal: _r, due: _d, ...rest } = record;
|
|
394
474
|
return {
|
|
395
475
|
...rest,
|
|
396
476
|
...(later ? { value: later.value, bytes: later.bytes } : {}),
|
|
@@ -440,3 +520,106 @@ export class WriteQueue {
|
|
|
440
520
|
return { ok: true, flush: await this.flush({ name, key }) };
|
|
441
521
|
}
|
|
442
522
|
}
|
|
523
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
524
|
+
/** the row and its index rows as a prediction; `row` absent predicts the removal of the key */
|
|
525
|
+
function changeOf(plan, dataset, key, row) {
|
|
526
|
+
const entries = [
|
|
527
|
+
[dataset, key, row ?? null],
|
|
528
|
+
...indexRowsOf(plan, dataset, key, row)
|
|
529
|
+
];
|
|
530
|
+
return {
|
|
531
|
+
puts: entries.flatMap(([table, id, value]) => value === null ? [] : [[table, id, value]]),
|
|
532
|
+
deletes: entries.flatMap(([table, id, value]) => value === null ? [[table, id]] : [])
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Predicts the rows a declared write changed when the answer named no version and sent no delta. The
|
|
537
|
+
* prediction is unconfirmed until the next verified delta settles it; `invalidate` predicts nothing.
|
|
538
|
+
*/
|
|
539
|
+
async function predict(response, pack, { write, url, body, routes }) {
|
|
540
|
+
const plan = indexPlan(routes);
|
|
541
|
+
const pathKey = matchPattern(write.pattern, url.pathname)?.[write.key];
|
|
542
|
+
const { dataset } = write;
|
|
543
|
+
if (write.apply === 'invalidate')
|
|
544
|
+
return;
|
|
545
|
+
if (write.apply === 'delete') {
|
|
546
|
+
if (pathKey !== undefined)
|
|
547
|
+
await pack.applyPredicted(changeOf(plan, dataset, pathKey));
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
if (write.apply === 'response') {
|
|
551
|
+
const row = await response
|
|
552
|
+
.clone()
|
|
553
|
+
.json()
|
|
554
|
+
.catch(() => undefined);
|
|
555
|
+
if (!isRecord(row))
|
|
556
|
+
return;
|
|
557
|
+
const id = row[write.key];
|
|
558
|
+
if (typeof id !== 'string' && typeof id !== 'number')
|
|
559
|
+
return;
|
|
560
|
+
const key = String(id);
|
|
561
|
+
if (pathKey !== undefined && pathKey !== key)
|
|
562
|
+
return;
|
|
563
|
+
await pack.applyPredicted(changeOf(plan, dataset, key, row));
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (pathKey === undefined)
|
|
567
|
+
return;
|
|
568
|
+
const held = await pack.getRow(dataset, pathKey);
|
|
569
|
+
if (!held)
|
|
570
|
+
return;
|
|
571
|
+
const next = write.apply === 'merge'
|
|
572
|
+
? isRecord(body)
|
|
573
|
+
? { ...held, ...body }
|
|
574
|
+
: undefined
|
|
575
|
+
: applyPatch(held, body, write.apply);
|
|
576
|
+
if (next)
|
|
577
|
+
await pack.applyPredicted(changeOf(plan, dataset, pathKey, next));
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Brings the held pack to the version a declared write's answer reached. The answer's `Spool-Delta` goes
|
|
581
|
+
* first; one that starts at another version, or that the store refuses, costs one delta fetch to the
|
|
582
|
+
* version the answer names. An answer that names no version and sends no delta is predicted from the
|
|
583
|
+
* write's mode when `sent` is given. An answer with a digest and no delta is predicted, and the digest
|
|
584
|
+
* confirms the prediction or drops it at the cost of one delta fetch. An answer that landed on another
|
|
585
|
+
* origin, after a redirect, applies nothing.
|
|
586
|
+
*/
|
|
587
|
+
export async function settleWrite(response, pack, fetchDelta, expected, sent) {
|
|
588
|
+
if (!response.url || new URL(response.url).origin !== expected)
|
|
589
|
+
return;
|
|
590
|
+
const head = await pack.head();
|
|
591
|
+
if (!head)
|
|
592
|
+
return;
|
|
593
|
+
const header = response.headers.get(HEADERS.delta);
|
|
594
|
+
const inline = header === null ? undefined : JSON.parse(header);
|
|
595
|
+
if (inline?.from === head.version) {
|
|
596
|
+
try {
|
|
597
|
+
await pack.applyDelta(inline);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
catch {
|
|
601
|
+
// refused; the delta route below brings the pack up to the answer's version
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
const vector = response.headers.get(HEADERS.vector);
|
|
605
|
+
const reached = inline?.to ?? (vector === null ? undefined : Number(vector));
|
|
606
|
+
if (reached === undefined) {
|
|
607
|
+
if (sent)
|
|
608
|
+
await predict(response, pack, sent);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
if (!Number.isSafeInteger(reached) || reached <= head.version)
|
|
612
|
+
return;
|
|
613
|
+
const digest = response.headers.get(WRITE_HEADERS.digest);
|
|
614
|
+
if (inline === undefined && sent && digest !== null) {
|
|
615
|
+
await predict(response, pack, sent);
|
|
616
|
+
try {
|
|
617
|
+
await pack.confirmPredicted(reached, digest);
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
catch {
|
|
621
|
+
await pack.dropPredicted();
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
await pack.applyDelta(await fetchDelta(head.version, reached));
|
|
625
|
+
}
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** the package version; the service worker and page runtime compare it on the handshake */
|
|
2
|
-
export declare const VERSION
|
|
2
|
+
export declare const VERSION = "1.0.0";
|
package/dist/version.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Plugin, ResolvedConfig } from 'vite';
|
|
2
|
+
import { type StaticOption } from '../plugins/static.js';
|
|
3
|
+
import type { Spool } from '../server/spool.js';
|
|
4
|
+
export type { StaticBuildContext, StaticBuildOptions, StaticOption } from '../plugins/static.js';
|
|
5
|
+
/** what `spoolVite` takes */
|
|
6
|
+
export type SpoolViteOptions = {
|
|
7
|
+
/** the instance the app's routes were declared for; the dev server answers from it */
|
|
8
|
+
spool: Spool;
|
|
9
|
+
/**
|
|
10
|
+
* Publishes the static tier into the build's output directory when a client build ends: the page
|
|
11
|
+
* runtime, the worker script, the packs and a `_headers` rule, all served by a static host with no
|
|
12
|
+
* Worker invocation. On by default; `false` turns it off, and an object sets `policy`, `url` or a
|
|
13
|
+
* `spool` made from the build output. The dev server does not publish.
|
|
14
|
+
*/
|
|
15
|
+
static?: StaticOption;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* A Vite plugin. In `serve` it injects the page runtime tag into HTML entries and serves the worker
|
|
19
|
+
* script and spool's paths from `spool`, and logs each fetch or XHR GET of the dev server's own
|
|
20
|
+
* origin that no data family covers. In a client `build` it publishes the static tier into the
|
|
21
|
+
* output directory (see `static`). The built HTML gets no tag from the plugin: a build goes to the
|
|
22
|
+
* app's spool server, which adds the same tag to each response.
|
|
23
|
+
*/
|
|
24
|
+
export declare function spoolVite(options: SpoolViteOptions): Omit<Plugin, 'configResolved'> & {
|
|
25
|
+
configResolved(config: ResolvedConfig): void;
|
|
26
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { toNodeListener } from '../node/listener.js';
|
|
3
|
+
import { devCoverage } from '../plugins/coverage.js';
|
|
4
|
+
import { publishStaticBuild } from '../plugins/static.js';
|
|
5
|
+
import { PATHS } from '../protocol/config.js';
|
|
6
|
+
import { injectClientTag } from '../protocol/tag.js';
|
|
7
|
+
/**
|
|
8
|
+
* A Vite plugin. In `serve` it injects the page runtime tag into HTML entries and serves the worker
|
|
9
|
+
* script and spool's paths from `spool`, and logs each fetch or XHR GET of the dev server's own
|
|
10
|
+
* origin that no data family covers. In a client `build` it publishes the static tier into the
|
|
11
|
+
* output directory (see `static`). The built HTML gets no tag from the plugin: a build goes to the
|
|
12
|
+
* app's spool server, which adds the same tag to each response.
|
|
13
|
+
*/
|
|
14
|
+
export function spoolVite(options) {
|
|
15
|
+
const { spool } = options;
|
|
16
|
+
let resolved;
|
|
17
|
+
const handle = (request) => {
|
|
18
|
+
const url = new URL(request.url);
|
|
19
|
+
if (url.pathname === PATHS.worker && request.method === 'GET')
|
|
20
|
+
return spool.stub(request);
|
|
21
|
+
return spool.respond(request);
|
|
22
|
+
};
|
|
23
|
+
// registered before Vite's own middleware, so spool's paths are not caught by the SPA fallback
|
|
24
|
+
const listener = toNodeListener(handle);
|
|
25
|
+
return {
|
|
26
|
+
name: '@drupflare/spool',
|
|
27
|
+
configResolved(config) {
|
|
28
|
+
resolved = config;
|
|
29
|
+
},
|
|
30
|
+
async closeBundle() {
|
|
31
|
+
if (resolved === undefined || resolved.command !== 'build')
|
|
32
|
+
return;
|
|
33
|
+
const environment = this.environment;
|
|
34
|
+
const client = environment
|
|
35
|
+
? environment.config.consumer === 'client'
|
|
36
|
+
: !resolved.build.ssr;
|
|
37
|
+
if (!client)
|
|
38
|
+
return;
|
|
39
|
+
const { logger, root } = resolved;
|
|
40
|
+
await publishStaticBuild({
|
|
41
|
+
spool,
|
|
42
|
+
option: options.static,
|
|
43
|
+
outDir: resolve(root, (environment?.config ?? resolved).build.outDir),
|
|
44
|
+
log: (line) => logger.info(line)
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
configureServer(server) {
|
|
48
|
+
server.middlewares.use(listener);
|
|
49
|
+
const watch = devCoverage(spool.config.routes, (line) => server.config.logger.info(line));
|
|
50
|
+
server.middlewares.use((req, _res, next) => {
|
|
51
|
+
const fetchDest = req.headers['sec-fetch-dest'];
|
|
52
|
+
watch({
|
|
53
|
+
method: req.method ?? 'GET',
|
|
54
|
+
url: `http://${req.headers.host ?? 'localhost'}${req.url ?? '/'}`,
|
|
55
|
+
fetchDest: typeof fetchDest === 'string' ? fetchDest : null
|
|
56
|
+
});
|
|
57
|
+
next();
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
transformIndexHtml(html) {
|
|
61
|
+
return resolved?.command === 'build'
|
|
62
|
+
? html
|
|
63
|
+
: injectClientTag(html, spool.config.clientTag);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
import { type Registration } from '../plugins/mode.js';
|
|
3
|
+
import type { Spool } from '../server/spool.js';
|
|
4
|
+
import { type StaticOption } from '../vite/index.js';
|
|
5
|
+
/** the fields of a vue-router route record the plugin reads; `RouteRecordNormalized` has them */
|
|
6
|
+
export type VueRouteRecord = {
|
|
7
|
+
path: string;
|
|
8
|
+
};
|
|
9
|
+
/** the router the plugin reads: `getRoutes()` of a vue-router instance */
|
|
10
|
+
export type VueRouter = {
|
|
11
|
+
getRoutes(): readonly VueRouteRecord[];
|
|
12
|
+
};
|
|
13
|
+
/** one route as the plugin registers it; `pattern` is set only when the path converts */
|
|
14
|
+
export type VuePage = Registration & {
|
|
15
|
+
path: string;
|
|
16
|
+
pattern?: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Every route of `router` as a registration. The app is a single-page app, so each route renders on
|
|
20
|
+
* the client and registers as the app shell. A path spool cannot express stays a network route.
|
|
21
|
+
*/
|
|
22
|
+
export declare function vuePages(router: VueRouter): VuePage[];
|
|
23
|
+
/** what `spoolVue` takes */
|
|
24
|
+
export type SpoolVueOptions = {
|
|
25
|
+
/** the instance the app's routes were declared for; the dev server answers from it */
|
|
26
|
+
spool: Spool;
|
|
27
|
+
/** the vue-router instance whose routes the development report lists */
|
|
28
|
+
router: VueRouter;
|
|
29
|
+
/** the build-time static tier, as `spoolVite` takes it; on by default, `false` turns it off */
|
|
30
|
+
static?: StaticOption;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* A Vite plugin for a Vue single-page app. It is `spoolVite` plus a development report: at
|
|
34
|
+
* startup it logs the routes that register, and each route that stays on the network with its reason.
|
|
35
|
+
*/
|
|
36
|
+
export declare function spoolVue(options: SpoolVueOptions): Plugin;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { registerMode } from '../plugins/mode.js';
|
|
2
|
+
import { toSpoolPattern } from '../plugins/path.js';
|
|
3
|
+
import { formatDevReport } from '../plugins/report.js';
|
|
4
|
+
import { spoolVite } from '../vite/index.js';
|
|
5
|
+
/**
|
|
6
|
+
* Every route of `router` as a registration. The app is a single-page app, so each route renders on
|
|
7
|
+
* the client and registers as the app shell. A path spool cannot express stays a network route.
|
|
8
|
+
*/
|
|
9
|
+
export function vuePages(router) {
|
|
10
|
+
return router.getRoutes().map((record) => {
|
|
11
|
+
const converted = toSpoolPattern('vue-router', record.path);
|
|
12
|
+
if (converted.kind === 'network') {
|
|
13
|
+
return { path: record.path, register: 'network', reason: converted.reason };
|
|
14
|
+
}
|
|
15
|
+
const registration = registerMode({ rendering: 'client', enumerable: true });
|
|
16
|
+
return { path: record.path, pattern: converted.pattern, ...registration };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A Vite plugin for a Vue single-page app. It is `spoolVite` plus a development report: at
|
|
21
|
+
* startup it logs the routes that register, and each route that stays on the network with its reason.
|
|
22
|
+
*/
|
|
23
|
+
export function spoolVue(options) {
|
|
24
|
+
const base = spoolVite({
|
|
25
|
+
spool: options.spool,
|
|
26
|
+
...(options.static === undefined ? {} : { static: options.static })
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
...base,
|
|
30
|
+
name: '@drupflare/spool/vue',
|
|
31
|
+
configResolved(config) {
|
|
32
|
+
base.configResolved(config);
|
|
33
|
+
if (config.command !== 'serve')
|
|
34
|
+
return;
|
|
35
|
+
const pages = vuePages(options.router);
|
|
36
|
+
config.logger.info(formatDevReport({
|
|
37
|
+
registered: pages.flatMap((page) => page.pattern && page.register !== 'network'
|
|
38
|
+
? [
|
|
39
|
+
{
|
|
40
|
+
pattern: page.pattern,
|
|
41
|
+
register: page.register,
|
|
42
|
+
maxAge: page.maxAge
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
: []),
|
|
46
|
+
network: pages
|
|
47
|
+
.filter((page) => page.register === 'network')
|
|
48
|
+
.map((page) => ({ path: page.path, reason: page.reason })),
|
|
49
|
+
uncovered: []
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|