@drupflare/spool 0.3.0 → 0.4.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 +263 -41
- package/dist/cdn/client.js +1 -1
- package/dist/cdn/manifest.json +7 -7
- package/dist/cdn/spool-sw.js +1 -1
- package/dist/client/entry.js +33 -1
- package/dist/client/register.d.ts +23 -1
- package/dist/client/register.js +53 -7
- package/dist/protocol/config.d.ts +16 -1
- package/dist/protocol/config.js +3 -1
- package/dist/protocol/dataset.d.ts +183 -0
- package/dist/protocol/dataset.js +206 -0
- package/dist/protocol/envelope.d.ts +1 -0
- package/dist/protocol/envelope.js +2 -1
- package/dist/protocol/errors.d.ts +1 -1
- package/dist/protocol/graph.d.ts +23 -4
- package/dist/protocol/graph.js +34 -3
- package/dist/protocol/index.d.ts +3 -0
- package/dist/protocol/index.js +3 -0
- package/dist/protocol/plan.d.ts +100 -0
- package/dist/protocol/plan.js +661 -0
- package/dist/protocol/provision.d.ts +46 -0
- package/dist/protocol/provision.js +38 -0
- package/dist/protocol/query.d.ts +66 -5
- package/dist/protocol/query.js +103 -4
- package/dist/protocol/route.d.ts +6 -2
- package/dist/protocol/route.js +31 -3
- package/dist/protocol/write.d.ts +146 -0
- package/dist/protocol/write.js +101 -0
- package/dist/report/dataset.d.ts +48 -0
- package/dist/report/dataset.js +93 -0
- package/dist/report/derive.d.ts +285 -0
- package/dist/report/derive.js +676 -1
- package/dist/report/economics.d.ts +11 -1
- package/dist/report/economics.js +19 -1
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +2 -0
- package/dist/report/inputs.d.ts +97 -0
- package/dist/report/inputs.js +133 -0
- package/dist/report/institution.d.ts +188 -1
- package/dist/report/institution.js +146 -0
- package/dist/report/loops.d.ts +73 -0
- package/dist/report/loops.js +101 -0
- package/dist/report/measurement.d.ts +1 -1
- package/dist/report/measurement.js +3 -1
- package/dist/report/model.d.ts +2 -0
- package/dist/report/model.js +4 -0
- package/dist/report/thirdparty.d.ts +57 -0
- package/dist/report/thirdparty.js +111 -0
- package/dist/report/trim.d.ts +56 -0
- package/dist/report/trim.js +83 -1
- package/dist/server/dataset.d.ts +48 -0
- package/dist/server/dataset.js +308 -0
- package/dist/server/embedded.d.ts +3 -3
- package/dist/server/embedded.js +3 -3
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +2 -0
- package/dist/server/spool.d.ts +20 -0
- package/dist/server/spool.js +126 -18
- package/dist/server/write.d.ts +27 -0
- package/dist/server/write.js +127 -0
- package/dist/sw/analytics.d.ts +6 -0
- package/dist/sw/analytics.js +14 -0
- package/dist/sw/dataset-sync.d.ts +76 -0
- package/dist/sw/dataset-sync.js +149 -0
- package/dist/sw/dataset.d.ts +91 -0
- package/dist/sw/dataset.js +318 -0
- package/dist/sw/graph/index.d.ts +31 -1
- package/dist/sw/graph/index.js +57 -5
- package/dist/sw/graph/invalidate.d.ts +15 -3
- package/dist/sw/graph/invalidate.js +42 -4
- package/dist/sw/idb.js +78 -9
- package/dist/sw/partition.d.ts +6 -1
- package/dist/sw/partition.js +21 -2
- package/dist/sw/principal-dataset.d.ts +64 -0
- package/dist/sw/principal-dataset.js +254 -0
- package/dist/sw/render.d.ts +7 -2
- package/dist/sw/render.js +19 -5
- package/dist/sw/replica.d.ts +12 -3
- package/dist/sw/replica.js +40 -5
- package/dist/sw/runtime.d.ts +43 -2
- package/dist/sw/runtime.js +340 -24
- package/dist/sw/seal.d.ts +13 -0
- package/dist/sw/seal.js +13 -4
- package/dist/sw/static.d.ts +18 -0
- package/dist/sw/static.js +84 -1
- package/dist/sw/storage.d.ts +51 -1
- package/dist/sw/storage.js +33 -1
- package/dist/sw/write.d.ts +107 -0
- package/dist/sw/write.js +442 -0
- package/package.json +3 -2
package/dist/sw/write.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { isWriteKey, isWriteVersion, newWriteId, WRITE_STATUS } from '../protocol/write.js';
|
|
2
|
+
import { seal, unseal } from './seal.js';
|
|
3
|
+
const DB = 'spool-writes';
|
|
4
|
+
const QUEUE = 'queue';
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
/** a queue in memory, for tests */
|
|
7
|
+
export function memoryWriteStore() {
|
|
8
|
+
const rows = new Map();
|
|
9
|
+
const slot = (name, key) => `${name}\u0000${key}`;
|
|
10
|
+
return {
|
|
11
|
+
get: async (name, key) => structuredClone(rows.get(slot(name, key))),
|
|
12
|
+
put: async (record) => void rows.set(slot(record.name, record.key), structuredClone(record)),
|
|
13
|
+
delete: async (name, key) => void rows.delete(slot(name, key)),
|
|
14
|
+
all: async () => structuredClone([...rows.values()])
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function wrap(request) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
request.onsuccess = () => resolve(request.result);
|
|
20
|
+
request.onerror = () => reject(request.error);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function done(tx) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
tx.oncomplete = () => resolve();
|
|
26
|
+
tx.onerror = () => reject(tx.error);
|
|
27
|
+
tx.onabort = () => reject(tx.error);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const rangeOf = (partition) => IDBKeyRange.bound([partition], [partition, []]);
|
|
31
|
+
/**
|
|
32
|
+
* The queue of one partition in its own database, `spool-writes` at version 1 (the shared `spool`
|
|
33
|
+
* database is never raised: older workers open it at 2). A row is `{ partition, name, key, sealed }`:
|
|
34
|
+
* the partition, class and key stay readable as the keys, and the value, the versions, the write
|
|
35
|
+
* id and the conflict are AES-GCM ciphertext under the partition's persistent key, bound to the
|
|
36
|
+
* partition, class and key as additional data. A persistent key is used for every queue, since a key
|
|
37
|
+
* that lives in the worker's memory would lose an unflushed value when the browser stops the worker.
|
|
38
|
+
* The database is created by the first call that reaches the store.
|
|
39
|
+
*/
|
|
40
|
+
export function idbWriteStore(factory, partition, vault) {
|
|
41
|
+
let opened;
|
|
42
|
+
const open = () => (opened ??= new Promise((resolve, reject) => {
|
|
43
|
+
const request = factory.open(DB, 1);
|
|
44
|
+
request.onupgradeneeded = () => {
|
|
45
|
+
request.result.createObjectStore(QUEUE, { keyPath: ['partition', 'name', 'key'] });
|
|
46
|
+
};
|
|
47
|
+
request.onsuccess = () => {
|
|
48
|
+
const db = request.result;
|
|
49
|
+
db.onversionchange = () => {
|
|
50
|
+
db.close();
|
|
51
|
+
opened = undefined;
|
|
52
|
+
};
|
|
53
|
+
resolve(db);
|
|
54
|
+
};
|
|
55
|
+
request.onerror = () => reject(request.error);
|
|
56
|
+
}));
|
|
57
|
+
const aad = (name, key) => `${partition}\u0000write\u0000${name}\u0000${key}`;
|
|
58
|
+
async function inStore(mode, run) {
|
|
59
|
+
const db = await open();
|
|
60
|
+
const tx = db.transaction(QUEUE, mode);
|
|
61
|
+
const finished = done(tx);
|
|
62
|
+
const result = await run(tx.objectStore(QUEUE));
|
|
63
|
+
await finished;
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
const read = async (row) => row && (await unseal(vault, partition, aad(row.name, row.key), row));
|
|
67
|
+
return {
|
|
68
|
+
get: async (name, key) => read(await inStore('readonly', (s) => wrap(s.get([partition, name, key])))),
|
|
69
|
+
put: async (record) => {
|
|
70
|
+
const key = await vault.get(partition, 'persistent', true);
|
|
71
|
+
if (!key)
|
|
72
|
+
throw new Error('no key');
|
|
73
|
+
const sealed = await seal(key, aad(record.name, record.key), record, 'persistent');
|
|
74
|
+
await inStore('readwrite', async (s) => {
|
|
75
|
+
await wrap(s.put({ partition, name: record.name, key: record.key, ...sealed }));
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
delete: (name, key) => inStore('readwrite', async (s) => void (await wrap(s.delete([partition, name, key])))),
|
|
79
|
+
all: async () => {
|
|
80
|
+
const rows = await inStore('readonly', (s) => wrap(s.getAll(rangeOf(partition))));
|
|
81
|
+
const out = [];
|
|
82
|
+
for (const row of rows) {
|
|
83
|
+
const value = await read(row);
|
|
84
|
+
if (value)
|
|
85
|
+
out.push(value);
|
|
86
|
+
else {
|
|
87
|
+
await inStore('readwrite', async (s) => void (await wrap(s.delete([partition, row.name, row.key]))));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Deletes every row of `partition` from `spool-writes`. A database that was never created stays
|
|
96
|
+
* uncreated: the open is rolled back when it would have made one.
|
|
97
|
+
*/
|
|
98
|
+
export async function destroyWrites(factory, partition) {
|
|
99
|
+
const db = await new Promise((resolve) => {
|
|
100
|
+
const request = factory.open(DB);
|
|
101
|
+
request.onupgradeneeded = () => {
|
|
102
|
+
try {
|
|
103
|
+
request.transaction?.abort();
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// nothing was created
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
request.onsuccess = () => resolve(request.result);
|
|
110
|
+
request.onerror = () => resolve(null);
|
|
111
|
+
request.onblocked = () => resolve(null);
|
|
112
|
+
});
|
|
113
|
+
if (!db)
|
|
114
|
+
return;
|
|
115
|
+
try {
|
|
116
|
+
if (!db.objectStoreNames.contains(QUEUE))
|
|
117
|
+
return;
|
|
118
|
+
const tx = db.transaction(QUEUE, 'readwrite');
|
|
119
|
+
const finished = done(tx);
|
|
120
|
+
tx.objectStore(QUEUE).delete(rangeOf(partition));
|
|
121
|
+
await finished;
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
// the rows are sealed under a key that is shredded alongside
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
db.close();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const refuse = (code, message) => ({
|
|
131
|
+
ok: false,
|
|
132
|
+
code,
|
|
133
|
+
message
|
|
134
|
+
});
|
|
135
|
+
const REFUSAL_CODES = new Set(Object.keys(WRITE_STATUS));
|
|
136
|
+
const codeOf = (body) => {
|
|
137
|
+
const code = body?.code;
|
|
138
|
+
return typeof code === 'string' && REFUSAL_CODES.has(code)
|
|
139
|
+
? code
|
|
140
|
+
: undefined;
|
|
141
|
+
};
|
|
142
|
+
function conflictOf(body) {
|
|
143
|
+
const conflict = body
|
|
144
|
+
?.conflict;
|
|
145
|
+
if (!conflict || !isWriteVersion(conflict.version))
|
|
146
|
+
return undefined;
|
|
147
|
+
return 'value' in conflict
|
|
148
|
+
? { version: conflict.version, value: conflict.value }
|
|
149
|
+
: { version: conflict.version };
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The device queue of one partition for declared writes of the `last-value` kind. One record holds
|
|
153
|
+
* the newest value of a `(class, key)` and the version its first edit started from, and a flush sends
|
|
154
|
+
* one request per record. A record leaves the queue only after the server answers 200; any failure
|
|
155
|
+
* leaves it, and a retry sends the same write id. A 412 or a permanent refusal parks the record as
|
|
156
|
+
* `conflicted` with its local value, and nothing sends it again until `resolve`. Every change to the
|
|
157
|
+
* store runs one at a time, and `kill` stops all of them, so a wipe cannot be followed by a send or
|
|
158
|
+
* a store change.
|
|
159
|
+
*/
|
|
160
|
+
export class WriteQueue {
|
|
161
|
+
#deps;
|
|
162
|
+
#tail = Promise.resolve();
|
|
163
|
+
#flushing = null;
|
|
164
|
+
#dead = false;
|
|
165
|
+
constructor(deps) {
|
|
166
|
+
this.#deps = deps;
|
|
167
|
+
}
|
|
168
|
+
/** stops the queue for good: no later call sends or stores anything */
|
|
169
|
+
kill() {
|
|
170
|
+
this.#dead = true;
|
|
171
|
+
}
|
|
172
|
+
#serial(fn) {
|
|
173
|
+
const run = this.#tail.then(fn, fn);
|
|
174
|
+
this.#tail = run.catch(() => undefined);
|
|
175
|
+
return run;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Queues `value` as the newest for `(name, key)`. An edit folds into the record already there and
|
|
179
|
+
* keeps the version the first edit started from. When a request for the earlier value may have
|
|
180
|
+
* left, the edit waits in `next` so that request and its retry stay byte-identical.
|
|
181
|
+
*/
|
|
182
|
+
write(name, key, value, version) {
|
|
183
|
+
if (this.#dead)
|
|
184
|
+
return Promise.resolve(refuse('no-partition', 'the queue was wiped'));
|
|
185
|
+
const config = this.#deps.classes().find((c) => c.name === name);
|
|
186
|
+
if (!config)
|
|
187
|
+
return Promise.resolve(refuse('unknown-write', `no write class "${name}"`));
|
|
188
|
+
if (!isWriteKey(key))
|
|
189
|
+
return Promise.resolve(refuse('bad-key', 'the key is malformed'));
|
|
190
|
+
if (version === undefined || version === null) {
|
|
191
|
+
return Promise.resolve(refuse('missing-version', 'say which version the edit started from'));
|
|
192
|
+
}
|
|
193
|
+
if (!isWriteVersion(version)) {
|
|
194
|
+
return Promise.resolve(refuse('bad-version', 'the version is malformed'));
|
|
195
|
+
}
|
|
196
|
+
let bytes;
|
|
197
|
+
try {
|
|
198
|
+
const text = JSON.stringify({ value });
|
|
199
|
+
if (value === undefined)
|
|
200
|
+
throw new Error('no value');
|
|
201
|
+
bytes = encoder.encode(text).length;
|
|
202
|
+
}
|
|
203
|
+
catch {
|
|
204
|
+
return Promise.resolve(refuse('bad-body', 'the value is not JSON'));
|
|
205
|
+
}
|
|
206
|
+
if (bytes > config.maxBytes) {
|
|
207
|
+
return Promise.resolve(refuse('too-large', `the value is over ${config.maxBytes} bytes`));
|
|
208
|
+
}
|
|
209
|
+
return this.#serial(async () => {
|
|
210
|
+
if (this.#dead)
|
|
211
|
+
return refuse('no-partition', 'the queue was wiped');
|
|
212
|
+
const now = this.#deps.now();
|
|
213
|
+
const held = await this.#deps.store.get(name, key);
|
|
214
|
+
if (!held) {
|
|
215
|
+
const pending = (await this.#deps.store.all()).filter((r) => r.name === name).length;
|
|
216
|
+
if (pending >= config.maxPending) {
|
|
217
|
+
return refuse('too-many', `${config.maxPending} keys of "${name}" are already queued`);
|
|
218
|
+
}
|
|
219
|
+
const record = {
|
|
220
|
+
name,
|
|
221
|
+
key,
|
|
222
|
+
value,
|
|
223
|
+
base: version,
|
|
224
|
+
id: newWriteId(),
|
|
225
|
+
state: 'pending',
|
|
226
|
+
queuedAt: now,
|
|
227
|
+
updatedAt: now,
|
|
228
|
+
bytes,
|
|
229
|
+
edits: 1,
|
|
230
|
+
attempts: 0
|
|
231
|
+
};
|
|
232
|
+
await this.#deps.store.put(record);
|
|
233
|
+
return { ok: true, state: record.state, edits: 1 };
|
|
234
|
+
}
|
|
235
|
+
const sent = held.state === 'flushing' || held.attempts > 0;
|
|
236
|
+
const next = {
|
|
237
|
+
...held,
|
|
238
|
+
updatedAt: now,
|
|
239
|
+
edits: held.edits + 1,
|
|
240
|
+
...(sent && held.state !== 'conflicted'
|
|
241
|
+
? { next: { value, bytes } }
|
|
242
|
+
: { value, bytes })
|
|
243
|
+
};
|
|
244
|
+
await this.#deps.store.put(next);
|
|
245
|
+
return { ok: true, state: next.state, edits: next.edits };
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/** what the page may see of each queued value */
|
|
249
|
+
list() {
|
|
250
|
+
return this.#serial(async () => {
|
|
251
|
+
if (this.#dead)
|
|
252
|
+
return [];
|
|
253
|
+
const records = await this.#deps.store.all();
|
|
254
|
+
return records
|
|
255
|
+
.sort((a, b) => a.queuedAt - b.queuedAt || (a.key < b.key ? -1 : 1))
|
|
256
|
+
.map((r) => ({
|
|
257
|
+
name: r.name,
|
|
258
|
+
key: r.key,
|
|
259
|
+
state: r.state,
|
|
260
|
+
base: r.base,
|
|
261
|
+
queuedAt: r.queuedAt,
|
|
262
|
+
updatedAt: r.updatedAt,
|
|
263
|
+
bytes: r.bytes,
|
|
264
|
+
attempts: r.attempts,
|
|
265
|
+
...(r.conflict ? { conflict: r.conflict } : {}),
|
|
266
|
+
...(r.refusal ? { refusal: r.refusal } : {})
|
|
267
|
+
}));
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Sends one request per record that is not conflicted, or only `only`'s record, and reports
|
|
272
|
+
* counts. A flush already running is joined. A network failure, a sign-in answer and a turned-off
|
|
273
|
+
* server stop the pass and leave the rest queued.
|
|
274
|
+
*/
|
|
275
|
+
flush(only) {
|
|
276
|
+
if (this.#flushing && !only)
|
|
277
|
+
return this.#flushing;
|
|
278
|
+
const run = this.#run(only);
|
|
279
|
+
if (!only) {
|
|
280
|
+
this.#flushing = run.finally(() => {
|
|
281
|
+
this.#flushing = null;
|
|
282
|
+
});
|
|
283
|
+
return this.#flushing;
|
|
284
|
+
}
|
|
285
|
+
return run;
|
|
286
|
+
}
|
|
287
|
+
async #run(only) {
|
|
288
|
+
const counts = { queued: 0, flushed: 0, conflicted: 0 };
|
|
289
|
+
if (this.#dead)
|
|
290
|
+
return counts;
|
|
291
|
+
const records = await this.#serial(async () => this.#dead ? [] : (await this.#deps.store.all()).filter((r) => r.state !== 'conflicted'));
|
|
292
|
+
for (const listed of records) {
|
|
293
|
+
if (only && (listed.name !== only.name || listed.key !== only.key))
|
|
294
|
+
continue;
|
|
295
|
+
const sending = await this.#serial(async () => {
|
|
296
|
+
if (this.#dead)
|
|
297
|
+
return undefined;
|
|
298
|
+
const current = await this.#deps.store.get(listed.name, listed.key);
|
|
299
|
+
if (!current || current.state === 'conflicted')
|
|
300
|
+
return undefined;
|
|
301
|
+
const record = { ...current, state: 'flushing' };
|
|
302
|
+
await this.#deps.store.put(record);
|
|
303
|
+
return record;
|
|
304
|
+
});
|
|
305
|
+
if (!sending)
|
|
306
|
+
continue;
|
|
307
|
+
counts.queued += 1;
|
|
308
|
+
let answer;
|
|
309
|
+
try {
|
|
310
|
+
answer = await this.#deps.send(sending);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
answer = undefined;
|
|
314
|
+
}
|
|
315
|
+
// a store that fails leaves the record `flushing`; the retry sends the same id and body
|
|
316
|
+
const stop = await this.#serial(() => this.#settle(sending, answer, counts)).catch(() => true);
|
|
317
|
+
if (stop)
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
if (counts.queued > 0 && !this.#dead)
|
|
321
|
+
this.#deps.onFlush?.(counts);
|
|
322
|
+
return counts;
|
|
323
|
+
}
|
|
324
|
+
/** applies the answer to the record, in one store step; true when the pass should stop */
|
|
325
|
+
async #settle(sent, answer, counts) {
|
|
326
|
+
if (this.#dead)
|
|
327
|
+
return true;
|
|
328
|
+
const { store } = this.#deps;
|
|
329
|
+
const now = this.#deps.now();
|
|
330
|
+
const current = await store.get(sent.name, sent.key);
|
|
331
|
+
if (!current)
|
|
332
|
+
return false;
|
|
333
|
+
const retry = async (bumped) => {
|
|
334
|
+
await store.put({
|
|
335
|
+
...current,
|
|
336
|
+
state: 'pending',
|
|
337
|
+
updatedAt: now,
|
|
338
|
+
attempts: current.attempts + (bumped ? 1 : 0)
|
|
339
|
+
});
|
|
340
|
+
return true;
|
|
341
|
+
};
|
|
342
|
+
if (!answer)
|
|
343
|
+
return retry(true);
|
|
344
|
+
const { status, body } = answer;
|
|
345
|
+
if (status === 200) {
|
|
346
|
+
const version = body?.version;
|
|
347
|
+
if (!isWriteVersion(version))
|
|
348
|
+
return retry(true);
|
|
349
|
+
counts.flushed += 1;
|
|
350
|
+
if (!current.next) {
|
|
351
|
+
await store.delete(current.name, current.key);
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
const { value, bytes } = current.next;
|
|
355
|
+
await store.put({
|
|
356
|
+
name: current.name,
|
|
357
|
+
key: current.key,
|
|
358
|
+
value,
|
|
359
|
+
bytes,
|
|
360
|
+
base: version,
|
|
361
|
+
id: newWriteId(),
|
|
362
|
+
state: 'pending',
|
|
363
|
+
queuedAt: current.queuedAt,
|
|
364
|
+
updatedAt: now,
|
|
365
|
+
edits: 1,
|
|
366
|
+
attempts: 0
|
|
367
|
+
});
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
const code = codeOf(body);
|
|
371
|
+
if (status === 412) {
|
|
372
|
+
const conflict = conflictOf(body);
|
|
373
|
+
if (conflict) {
|
|
374
|
+
counts.conflicted += 1;
|
|
375
|
+
await store.put(this.#park(current, now, { conflict }));
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
counts.conflicted += 1;
|
|
379
|
+
await store.put(this.#park(current, now, { refusal: 'unavailable' }));
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
if (status === 401 || status === 429 || code === 'writes-off' || code === 'sign-in') {
|
|
383
|
+
return retry(false);
|
|
384
|
+
}
|
|
385
|
+
if (status >= 500 || status === 408)
|
|
386
|
+
return retry(true);
|
|
387
|
+
counts.conflicted += 1;
|
|
388
|
+
await store.put(this.#park(current, now, { refusal: code ?? 'unavailable' }));
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
/** the record as `conflicted`, keeping the newest local value */
|
|
392
|
+
#park(record, now, why) {
|
|
393
|
+
const { next: later, conflict: _c, refusal: _r, ...rest } = record;
|
|
394
|
+
return {
|
|
395
|
+
...rest,
|
|
396
|
+
...(later ? { value: later.value, bytes: later.bytes } : {}),
|
|
397
|
+
state: 'conflicted',
|
|
398
|
+
updatedAt: now,
|
|
399
|
+
...why
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Settles a conflicted record. `local` keeps the local value, re-bases it onto the server's
|
|
404
|
+
* version, gives it a new write id and flushes that key. `server` drops the local value and
|
|
405
|
+
* returns what the server held.
|
|
406
|
+
*/
|
|
407
|
+
async resolve(name, key, use) {
|
|
408
|
+
if (this.#dead)
|
|
409
|
+
return refuse('no-partition', 'the queue was wiped');
|
|
410
|
+
if (use !== 'local' && use !== 'server') {
|
|
411
|
+
return refuse('bad-body', 'resolve with "local" or "server"');
|
|
412
|
+
}
|
|
413
|
+
const outcome = await this.#serial(async () => {
|
|
414
|
+
if (this.#dead)
|
|
415
|
+
return refuse('no-partition', 'the queue was wiped');
|
|
416
|
+
const held = await this.#deps.store.get(name, key);
|
|
417
|
+
if (!held)
|
|
418
|
+
return refuse('no-record', 'nothing is queued for that key');
|
|
419
|
+
if (held.state !== 'conflicted') {
|
|
420
|
+
return refuse('not-conflicted', 'that record has no conflict to resolve');
|
|
421
|
+
}
|
|
422
|
+
if (use === 'server') {
|
|
423
|
+
await this.#deps.store.delete(name, key);
|
|
424
|
+
return { ok: true, ...(held.conflict ? { server: held.conflict } : {}) };
|
|
425
|
+
}
|
|
426
|
+
const { conflict, refusal: _r, ...rest } = held;
|
|
427
|
+
await this.#deps.store.put({
|
|
428
|
+
...rest,
|
|
429
|
+
base: conflict?.version ?? held.base,
|
|
430
|
+
id: newWriteId(),
|
|
431
|
+
state: 'pending',
|
|
432
|
+
updatedAt: this.#deps.now(),
|
|
433
|
+
edits: 1,
|
|
434
|
+
attempts: 0
|
|
435
|
+
});
|
|
436
|
+
return { ok: true };
|
|
437
|
+
});
|
|
438
|
+
if (!outcome.ok || use === 'server')
|
|
439
|
+
return outcome;
|
|
440
|
+
return { ok: true, flush: await this.flush({ name, key }) };
|
|
441
|
+
}
|
|
442
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drupflare/spool",
|
|
3
3
|
"description": "Client-Side Page Rendering and Execution Framework",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -89,7 +89,8 @@
|
|
|
89
89
|
"measure:payload": "bun scripts/measure/payload.ts",
|
|
90
90
|
"measure:trim": "bun scripts/measure/trim.ts",
|
|
91
91
|
"measure:nav-replay": "bun scripts/measure/nav-replay.ts",
|
|
92
|
-
"measure:embed-markup": "bun scripts/measure/embed-markup.ts"
|
|
92
|
+
"measure:embed-markup": "bun scripts/measure/embed-markup.ts",
|
|
93
|
+
"measure:avoidable-loops": "bun scripts/measure/avoidable-loops.ts"
|
|
93
94
|
},
|
|
94
95
|
"devDependencies": {
|
|
95
96
|
"@azure/functions": "^4.16.5",
|