@yaks/api 0.0.0-stage → 0.2.36

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 (152) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +956 -2
  3. package/_dist/actor.d.ts +59 -0
  4. package/_dist/actor.d.ts.map +1 -0
  5. package/_dist/address.d.ts +33 -0
  6. package/_dist/address.d.ts.map +1 -0
  7. package/_dist/asset.d.ts +15 -0
  8. package/_dist/asset.d.ts.map +1 -0
  9. package/_dist/batches.d.ts +4 -0
  10. package/_dist/batches.d.ts.map +1 -0
  11. package/_dist/body.d.ts +7 -0
  12. package/_dist/body.d.ts.map +1 -0
  13. package/_dist/deno.d.ts +38 -0
  14. package/_dist/deno.d.ts.map +1 -0
  15. package/_dist/doors.d.ts +134 -0
  16. package/_dist/doors.d.ts.map +1 -0
  17. package/_dist/graph.d.ts +17 -0
  18. package/_dist/graph.d.ts.map +1 -0
  19. package/_dist/headers.d.ts +5 -0
  20. package/_dist/headers.d.ts.map +1 -0
  21. package/_dist/log.d.ts +253 -0
  22. package/_dist/log.d.ts.map +1 -0
  23. package/_dist/mod.d.ts +71 -0
  24. package/_dist/mod.d.ts.map +1 -0
  25. package/_dist/publish.d.ts +7 -0
  26. package/_dist/publish.d.ts.map +1 -0
  27. package/_dist/reached.d.ts +145 -0
  28. package/_dist/reached.d.ts.map +1 -0
  29. package/_dist/receipts.d.ts +153 -0
  30. package/_dist/receipts.d.ts.map +1 -0
  31. package/_dist/refuse.d.ts +51 -0
  32. package/_dist/refuse.d.ts.map +1 -0
  33. package/_dist/relay.d.ts +3 -0
  34. package/_dist/relay.d.ts.map +1 -0
  35. package/_dist/request.d.ts +79 -0
  36. package/_dist/request.d.ts.map +1 -0
  37. package/_dist/requests.d.ts +7 -0
  38. package/_dist/requests.d.ts.map +1 -0
  39. package/_dist/route.d.ts +71 -0
  40. package/_dist/route.d.ts.map +1 -0
  41. package/_dist/routed.d.ts +33 -0
  42. package/_dist/routed.d.ts.map +1 -0
  43. package/_dist/routes.d.ts +46 -0
  44. package/_dist/routes.d.ts.map +1 -0
  45. package/_dist/save.d.ts +6 -0
  46. package/_dist/save.d.ts.map +1 -0
  47. package/_dist/socket.d.ts +73 -0
  48. package/_dist/socket.d.ts.map +1 -0
  49. package/_dist/subs.d.ts +134 -0
  50. package/_dist/subs.d.ts.map +1 -0
  51. package/_dist/timing.d.ts +8 -0
  52. package/_dist/timing.d.ts.map +1 -0
  53. package/_dist/tools.d.ts +80 -0
  54. package/_dist/tools.d.ts.map +1 -0
  55. package/_dist/vocab.d.ts +6 -0
  56. package/_dist/vocab.d.ts.map +1 -0
  57. package/access.js +32 -0
  58. package/access.js.map +1 -0
  59. package/access.ts +42 -0
  60. package/actor.js +75 -0
  61. package/actor.js.map +1 -0
  62. package/actor.ts +106 -0
  63. package/address.js +42 -0
  64. package/address.js.map +1 -0
  65. package/address.ts +49 -0
  66. package/admission.js +71 -0
  67. package/admission.js.map +1 -0
  68. package/admission.ts +64 -0
  69. package/asset.js +37 -0
  70. package/asset.js.map +1 -0
  71. package/asset.ts +47 -0
  72. package/batches.js +92 -0
  73. package/batches.js.map +1 -0
  74. package/batches.ts +96 -0
  75. package/body.js +51 -0
  76. package/body.js.map +1 -0
  77. package/body.ts +82 -0
  78. package/deno.js +88 -0
  79. package/deno.js.map +1 -0
  80. package/deno.json +1 -0
  81. package/deno.ts +150 -0
  82. package/doors.js +435 -0
  83. package/doors.js.map +1 -0
  84. package/doors.ts +570 -0
  85. package/graph.js +12 -0
  86. package/graph.js.map +1 -0
  87. package/graph.ts +23 -0
  88. package/headers.js +5 -0
  89. package/headers.js.map +1 -0
  90. package/headers.ts +9 -0
  91. package/interest.js +224 -0
  92. package/interest.js.map +1 -0
  93. package/interest.ts +264 -0
  94. package/log.js +1011 -0
  95. package/log.js.map +1 -0
  96. package/log.ts +1035 -0
  97. package/mod.js +69 -0
  98. package/mod.js.map +1 -0
  99. package/mod.ts +142 -0
  100. package/package.json +75 -4
  101. package/peer_query.js +164 -0
  102. package/peer_query.js.map +1 -0
  103. package/peer_query.ts +158 -0
  104. package/publish.js +7 -0
  105. package/publish.js.map +1 -0
  106. package/publish.ts +16 -0
  107. package/reached.js +456 -0
  108. package/reached.js.map +1 -0
  109. package/reached.ts +549 -0
  110. package/receipts.js +543 -0
  111. package/receipts.js.map +1 -0
  112. package/receipts.ts +749 -0
  113. package/refuse.js +120 -0
  114. package/refuse.js.map +1 -0
  115. package/refuse.ts +179 -0
  116. package/relay.js +249 -0
  117. package/relay.js.map +1 -0
  118. package/relay.ts +284 -0
  119. package/request.js +218 -0
  120. package/request.js.map +1 -0
  121. package/request.ts +235 -0
  122. package/requests.js +250 -0
  123. package/requests.js.map +1 -0
  124. package/requests.ts +257 -0
  125. package/route.js +139 -0
  126. package/route.js.map +1 -0
  127. package/route.ts +228 -0
  128. package/routed.js +25 -0
  129. package/routed.js.map +1 -0
  130. package/routed.ts +53 -0
  131. package/routes.js +154 -0
  132. package/routes.js.map +1 -0
  133. package/routes.ts +201 -0
  134. package/save.js +341 -0
  135. package/save.js.map +1 -0
  136. package/save.ts +393 -0
  137. package/socket.js +398 -0
  138. package/socket.js.map +1 -0
  139. package/socket.ts +496 -0
  140. package/subs.js +2295 -0
  141. package/subs.js.map +1 -0
  142. package/subs.ts +2979 -0
  143. package/timing.js +17 -0
  144. package/timing.js.map +1 -0
  145. package/timing.ts +26 -0
  146. package/tools.js +251 -0
  147. package/tools.js.map +1 -0
  148. package/tools.ts +315 -0
  149. package/vocab.js +18 -0
  150. package/vocab.js.map +1 -0
  151. package/vocab.json +93 -0
  152. package/vocab.ts +18 -0
package/receipts.js ADDED
@@ -0,0 +1,543 @@
1
+ /**
2
+ * A keyed write and its receipt: committed once, and every resend told what
3
+ * the first send was told.
4
+ *
5
+ * Every host keeps its receipts in one place, its write log (`./log.ts`),
6
+ * plain SQLite tables on the graph's own connection, and inserts each in the
7
+ * transaction that commits its write.
8
+ *
9
+ * - Every receipt is named by its writer and key (`./log.ts` `receiptKey`),
10
+ * so two writers' same key never meet, and an outbox key's is bound to its
11
+ * body. @yaks/api's own door keeps one with its graph's hooks
12
+ * ({@link receipts}, {@link received}); the request token is an in-process
13
+ * capability, not client input.
14
+ * - A host's door over the log ({@link door}) also keeps the input it could
15
+ * not commit yet: while its row ceiling stopped it, while it could not
16
+ * start, or while it failed, and replays it in order once the object may
17
+ * write again; {@link landing} completes each write in the transaction
18
+ * that commits it.
19
+ *
20
+ * @module
21
+ */ import { decode } from "@yaks/vocab/wire";
22
+ import { token } from "@yaks/graph";
23
+ import { Refused } from "@yaks/graph/errors";
24
+ import { published } from "./publish.js";
25
+ import { json, refuse } from "./refuse.js";
26
+ import { aside, dead, done, durable, fingerprinted, first, fits, held, IDEMPOTENCY, keep, landed, named, next, opened, owned, parked, raise, receiptKey, received as logged, remembered, replayed, said, started, taken, unstarted, waiting } from "./log.js";
27
+ let pending = new Map();
28
+ let requestKey = '$apiReceipt';
29
+ class Replay extends Error {
30
+ answer;
31
+ constructor(answer){
32
+ super('committed write replay'), this.answer = answer;
33
+ this.name = 'Refused';
34
+ }
35
+ }
36
+ // The log, raised once a connection (log.ts `raise`): a host whose schema
37
+ // names it stands already, and one that does not gains it at its first
38
+ // keyed write.
39
+ let raised = new WeakSet();
40
+ const writeLog = Symbol('API write log');
41
+ let standing = (db)=>{
42
+ let at = db.connection ?? db;
43
+ if (raised.has(at)) return;
44
+ raise(db);
45
+ raised.add(at);
46
+ };
47
+ /** API-owned graph hooks over the host's write log (`db`), also loaded by
48
+ * the box's graph facet: a keyed write's receipt is inserted in the
49
+ * transaction that commits it, and a resend is told what it was told. */ export let receipts = (db)=>{
50
+ let writes = new WeakMap();
51
+ let plugin = {
52
+ [writeLog]: db,
53
+ name: '@yaks/api/receipts',
54
+ requests: [
55
+ requestKey
56
+ ],
57
+ authorize: (bundles, _tx, _error, context)=>{
58
+ let marked = bundles.find((b)=>requestKey in b);
59
+ if (!marked) return bundles;
60
+ let key = marked[requestKey];
61
+ let held = typeof key == 'string' ? pending.get(key) : undefined;
62
+ if (!held || !context?.transaction) {
63
+ throw new Refused('invalid API receipt capability');
64
+ }
65
+ let name = held.receipt.headers.get(IDEMPOTENCY);
66
+ // Its body bound: another body under the same key is refused here.
67
+ let was = first(db, name, held.receipt);
68
+ if (was?.state == 'applied') {
69
+ let vocab = context.graph.vocab;
70
+ throw new Replay(decode(vocab, JSON.parse(was.answer)));
71
+ }
72
+ writes.set(context.transaction, held);
73
+ return bundles.flatMap((b)=>{
74
+ if (!(requestKey in b)) return [
75
+ b
76
+ ];
77
+ let { [requestKey]: _key, ...rest } = b;
78
+ return held.empty ? [] : [
79
+ rest
80
+ ];
81
+ });
82
+ },
83
+ hooks: {
84
+ commit: (bundles, _tx, _error, context)=>{
85
+ let held = context?.transaction && writes.get(context.transaction);
86
+ if (!held) return bundles;
87
+ let vocab = context.graph.vocab;
88
+ logged(db, {
89
+ request: held.receipt
90
+ }, ()=>published(vocab, bundles));
91
+ return bundles;
92
+ }
93
+ }
94
+ };
95
+ return plugin;
96
+ };
97
+ /** Apply with a receipt named by its writer and key (log.ts `receiptKey`),
98
+ * kept in the graph's registered write log in the write's own transaction.
99
+ * A standalone door may supply the graph's own connection as `db`.
100
+ * It is kept as every receipt under
101
+ * that key is: an outbox key's for good and bound to its exact body, since a
102
+ * durable browser outbox may replay after an arbitrary delay, and any other's
103
+ * for the resend window. A host with no log refuses a keyed write
104
+ * rather than apply it without the receipt that makes a resend safe. */ export let received = async (graph, db, key, body, actor, bundles, options)=>{
105
+ let registered = graph.plugins.find((p)=>p[writeLog])?.[writeLog];
106
+ let log = registered ?? db;
107
+ if (!log) throw new Refused('Idempotency-Key needs a host with a write log');
108
+ standing(log);
109
+ if (!registered) graph.use(receipts(log));
110
+ if (bundles.some((b)=>requestKey in b)) {
111
+ throw new Refused('invalid API receipt capability');
112
+ }
113
+ let capability = crypto.randomUUID();
114
+ pending.set(capability, {
115
+ receipt: named(receiptKey(actor, key), token(body)),
116
+ empty: !bundles.length
117
+ });
118
+ try {
119
+ let marked = bundles.length ? bundles.map((b, i)=>i ? b : {
120
+ ...b,
121
+ [requestKey]: capability
122
+ }) : [
123
+ {
124
+ entity: {
125
+ eid: crypto.randomUUID()
126
+ },
127
+ $quiet: true,
128
+ [requestKey]: capability
129
+ }
130
+ ];
131
+ return await graph.apply(marked, options);
132
+ } catch (error) {
133
+ if (error instanceof Replay) return error.answer;
134
+ throw error;
135
+ } finally{
136
+ pending.delete(capability);
137
+ }
138
+ };
139
+ /** The hook that completes a write in the transaction that commits its
140
+ * batch: a kept write leaves the log, or stays `applied` with its answer
141
+ * (`landed`), and a live keyed request's receipt is inserted (`received`). A
142
+ * resend cannot see one without the other. `during` names the write for the
143
+ * one synchronous `apply()` it wraps; every other batch (an effect's, a
144
+ * tick's, one applied after an await) finds none named.
145
+ *
146
+ * ```ts
147
+ * import { equal } from '@yaks/testing'
148
+ * import { graph } from '@yaks/graph'
149
+ * import { ram } from '@yaks/ram'
150
+ * import { loadVocab } from '@yaks/vocab'
151
+ * import { open } from '@yaks/sqlite/db'
152
+ * import { keep, next, raise } from './log.ts'
153
+ * import { landing } from './receipts.ts'
154
+ * let vocab = loadVocab({ $defs: { doc: { component: true, type: 'object',
155
+ * properties: { title: { type: 'string' } } } } })
156
+ * let db = open(':memory:')
157
+ * raise(db)
158
+ * let hook = landing({ db, vocab, now: Date.now })
159
+ * let g = graph({ vocab, storage: ram(vocab), plugins: [hook.plugin] })
160
+ * let sent = new Request('http://store/apply', { method: 'POST', body: '[]' })
161
+ * let seq = keep(db, sent, '[]')
162
+ * await hook.during(seq, () => g.apply([{ entity: { eid: 'a' }, doc: { title: 'A' } }]))
163
+ * equal(next(db), null)
164
+ * ```
165
+ */ export let landing = (log)=>{
166
+ let at = null;
167
+ let plugin = {
168
+ name: '@yaks/api/log',
169
+ // Logging follows a successful write and adds no admission refusal.
170
+ admission: ()=>true,
171
+ hooks: {
172
+ commit: (bundles)=>{
173
+ if (at != null) {
174
+ let answer = ()=>published(log.vocab, bundles);
175
+ if (typeof at == 'number') landed(log.db, at, answer, log.now);
176
+ else logged(log.db, at, answer, log.now);
177
+ at = null;
178
+ }
179
+ return bundles;
180
+ }
181
+ }
182
+ };
183
+ return {
184
+ plugin,
185
+ during: (mark, apply)=>{
186
+ at = mark;
187
+ try {
188
+ return apply();
189
+ } finally{
190
+ at = null;
191
+ }
192
+ }
193
+ };
194
+ };
195
+ // A JSON answer's headers, for one the write log kept as text.
196
+ let JSONED = {
197
+ headers: {
198
+ 'content-type': 'application/json'
199
+ }
200
+ };
201
+ /**
202
+ * The one drain of a host's write log, from its oldest waiting write, and
203
+ * the live callers it answers as each lands. What it leaves `running` (a
204
+ * reset cut it off) or `failed` is the host's to take up, since only the
205
+ * host knows whether a write it started may be tried again: a Store asks for
206
+ * review (log.ts `interrupted`), a Builder hears a line in the transaction
207
+ * that lands it, so it replays one again.
208
+ */ let replaying = (host)=>{
209
+ let { now, report } = host;
210
+ let retry = host.retry ?? 60_000;
211
+ let stage = host.stage ?? ((_name, work)=>work());
212
+ // The replay in progress, one at a time; whether the last one stopped
213
+ // because the log itself failed, which the alarm or the next incarnation
214
+ // takes up; and the live callers waiting on a kept write, by its place.
215
+ let draining = null;
216
+ let stuck = false;
217
+ let callers = new Map();
218
+ let wait = ()=>host.window.refusal()?.retry.after ?? 0;
219
+ // The alarm, `ms` from now: when this object's window ends.
220
+ let later = (ms)=>host.arm(now() + ms, 'stopped');
221
+ let tell = (seq, answer)=>{
222
+ let caller = callers.get(seq);
223
+ callers.delete(seq);
224
+ caller?.(answer);
225
+ };
226
+ /** The answer for a write kept while this object is stopped, `ms` more,
227
+ * and the alarm that comes back for it then. */ let waits = (seq, ms)=>{
228
+ void later(ms);
229
+ let s = Math.ceil(ms / 1000);
230
+ let answer = parked(seq, `this app is past the rows it may write for ${s} s`);
231
+ answer.headers.set('retry-after', String(s));
232
+ return answer;
233
+ };
234
+ /** The caller's answer for a kept write, and the alarm that comes back for
235
+ * it. */ let park = (seq, why)=>{
236
+ void host.arm(now() + retry);
237
+ return parked(seq, why);
238
+ };
239
+ /** One kept write, applied: out of the log when it commits or is refused
240
+ * as asked, and set aside when it fails (`aside`), its failure reported
241
+ * where the host's commit caught it. `live` is a write whose caller is
242
+ * still here to be told its refusal; a replay's refusal has nobody to
243
+ * tell, so it stays in the log with its reason and is reported. */ let land = async (k, live)=>{
244
+ try {
245
+ let r = await host.commit(replayed(k), k.seq);
246
+ // The window's to wait out (`drain`), never the write's refusal.
247
+ if (r.status == 429 && wait()) return r;
248
+ if (r.status < 300) done(host.db, k.seq);
249
+ else if (r.status >= 500) aside(host.db, k.seq, await said(r.clone()));
250
+ else if (live) done(host.db, k.seq);
251
+ else {
252
+ let why = await said(r.clone());
253
+ dead(host.db, k.seq, why);
254
+ report(new Error(`a kept write no longer applies: ${why}`), 'write replay');
255
+ }
256
+ return r;
257
+ } catch (e) {
258
+ if (host.window.stops(e)) throw e;
259
+ report(e, 'write replay');
260
+ if (held(host.db, k.seq)) aside(host.db, k.seq, String(e));
261
+ return refuse(e);
262
+ }
263
+ };
264
+ /** The log from its oldest waiting write to its newest, one at a time. A
265
+ * write that fails is set aside (`land`) and the ones behind it go on;
266
+ * only the log itself failing stops the pass, and then the alarm or the
267
+ * next incarnation starts another. Each write whose caller is still
268
+ * waiting is answered as it lands; the ones left waiting are told they are
269
+ * kept. */ let drain = ()=>{
270
+ if (draining) return draining;
271
+ let run = async ()=>{
272
+ // Yield once, so `draining` is set before the loop can end and clear
273
+ // it: a write that arrives in between must find the loop running.
274
+ await null;
275
+ let at = null;
276
+ try {
277
+ for(let k = at = next(host.db); k; k = at = next(host.db, k.seq)){
278
+ let live = callers.has(k.seq);
279
+ started(host.db, k.seq);
280
+ let r = await land(k, live);
281
+ // Stopped before it applied: it waits again as it was, and every
282
+ // write behind it with it, until the window ends.
283
+ let ms = r.status == 429 ? wait() : 0;
284
+ if (ms) {
285
+ let seq = k.seq;
286
+ host.window.free(()=>unstarted(host.db, seq));
287
+ tell(seq, waits(seq, ms));
288
+ break;
289
+ }
290
+ let kept = r.status >= 500 && held(host.db, k.seq);
291
+ // A storage failure can escape even the log's error handler. Keep
292
+ // the live caller reachable until every response read is done, so
293
+ // the failure cleanup below can still answer it.
294
+ tell(k.seq, kept ? parked(k.seq, await said(r)) : r);
295
+ }
296
+ } catch (e) {
297
+ if (host.window.stops(e)) {
298
+ let seq = at?.seq;
299
+ if (seq != null) host.window.free(()=>unstarted(host.db, seq));
300
+ } else {
301
+ stuck = true;
302
+ report(e, 'write replay');
303
+ }
304
+ }
305
+ // Synchronously after the last look at the log: nothing can be kept
306
+ // between that look and this line without finding the loop gone.
307
+ draining = null;
308
+ let ms = wait();
309
+ for (let [seq, caller] of callers){
310
+ caller(ms ? waits(seq, ms) : park(seq, 'earlier writes to this app are still waiting'));
311
+ }
312
+ if (ms && !callers.size) void later(ms);
313
+ callers.clear();
314
+ };
315
+ return draining = stage('replay', run);
316
+ };
317
+ return {
318
+ drain,
319
+ tell,
320
+ waits,
321
+ park,
322
+ /** Whether a replay may start now: none is running, and the log did
323
+ * not fail last time. */ idle: ()=>!stuck && !draining,
324
+ stuck: ()=>stuck,
325
+ /** What a live caller is told once its kept write lands. */ heard: (seq)=>new Promise((r)=>{
326
+ let other = callers.get(seq);
327
+ callers.set(seq, other ? (a)=>(other(a.clone()), r(a)) : r);
328
+ }),
329
+ settle () {
330
+ let going = draining;
331
+ if (going) return stage('settle', ()=>going);
332
+ // While stopped, what the log holds waits for the alarm.
333
+ if (wait()) return;
334
+ if (!stuck && waiting(host.db)) return drain();
335
+ },
336
+ mend () {
337
+ stuck = false;
338
+ return drain();
339
+ }
340
+ };
341
+ };
342
+ /**
343
+ * The replay of a host's write log, for a host whose input comes in at a
344
+ * door of its own and is kept there (log.ts `keep`): a space's Builder keeps
345
+ * a person's lines so, and replays them one turn at a time.
346
+ *
347
+ * ```ts
348
+ * import { equal } from '@yaks/testing'
349
+ * import { open } from '@yaks/sqlite/db'
350
+ * import { keep, next, raise } from './log.ts'
351
+ * import { replays } from './receipts.ts'
352
+ * let db = open(':memory:')
353
+ * raise(db)
354
+ * let heard: string[] = []
355
+ * let log = replays({
356
+ * db,
357
+ * now: Date.now,
358
+ * arm: () => {},
359
+ * report: () => {},
360
+ * window: {
361
+ * refusal: () => null,
362
+ * free: (body) => body(),
363
+ * stops: (_e): _e is never => false,
364
+ * },
365
+ * commit: async (r) => (heard.push(await r.text()), new Response('[]')),
366
+ * })
367
+ * keep(db, new Request('http://builder/say', { method: 'POST' }), 'hello')
368
+ * await log.drain()
369
+ * equal([heard, next(db)], [['hello'], null])
370
+ * ```
371
+ */ export let replays = (host)=>{
372
+ let { drain, settle, mend } = replaying(host);
373
+ return {
374
+ drain,
375
+ settle,
376
+ mend
377
+ };
378
+ };
379
+ /**
380
+ * A host's write door over its log. A live keyed write commits its mutation
381
+ * and successful receipt together, with no transient log stages. Input that
382
+ * cannot commit is kept and replayed in order by the one drain that runs at a
383
+ * time ({@link replays}). A write that finds the object stopped is kept and
384
+ * told when to look again, and the alarm armed for the window's end replays
385
+ * it. One that finds the object refusing to start, or the log itself
386
+ * failing, is answered 202 and waits in the log. A resend (its idempotency
387
+ * key already in the log) is the write it repeats: told what that one was
388
+ * told, or waiting beside it.
389
+ */ export let door = (host)=>{
390
+ let { now, report } = host;
391
+ let { drain, tell, waits, park, idle, stuck, heard, settle, mend } = replaying(host);
392
+ /** A write that arrives while this object may not write: kept in the log,
393
+ * which the window lets through (`free`), as `pending`, and told when to
394
+ * look again. A resend of one kept so is told the same; one of a write the
395
+ * log already settled waits, and hears what it was once the object
396
+ * writes. */ let hold = (request, body, stop)=>{
397
+ let key = request.headers.get(IDEMPOTENCY);
398
+ let was = host.window.free(()=>key ? first(host.db, key, request) : null);
399
+ if (was && was.state != 'pending' && was.state != 'running') {
400
+ return refuse(stop);
401
+ }
402
+ let seq = was?.seq ?? host.window.free(()=>keep(host.db, request, body, now));
403
+ return waits(seq, stop.retry.after);
404
+ };
405
+ /** A write in at `/apply`, its key named by its writer from here on. */ let write = async (came)=>{
406
+ let request = owned(came, host.writer(came));
407
+ // A binary write is kept as it came, since decoding it needs a start
408
+ // that may not run, and is decoded as it lands.
409
+ let { body, told } = await taken(request);
410
+ let sent = ()=>new Request(request, {
411
+ body: opened(request.headers, body)
412
+ });
413
+ let key = request.headers.get(IDEMPOTENCY);
414
+ let seq;
415
+ let was;
416
+ let unkept = async ()=>{
417
+ let req = sent();
418
+ return await host.ready(req) ?? host.serve(req);
419
+ };
420
+ if (!fits(body) || !host.keeps(told)) {
421
+ // A browser keeps retrying its intent until acceptance. Never apply it
422
+ // without the receipt that makes a later resend safe.
423
+ if (durable(key)) {
424
+ return refuse(new Refused('outbox writes must fit in the write log and cannot carry secret values'));
425
+ }
426
+ return unkept();
427
+ }
428
+ // Authority is the replay's to check (the host's commit), as for every
429
+ // kept write.
430
+ let stop = host.window.refusal();
431
+ if (stop) return hold(fingerprinted(request, body), body, stop);
432
+ if (durable(key)) {
433
+ try {
434
+ await host.authorize(request);
435
+ request = fingerprinted(request, body);
436
+ } catch (e) {
437
+ if (host.window.stops(e)) {
438
+ return hold(fingerprinted(request, body), body, e);
439
+ }
440
+ return refuse(e);
441
+ }
442
+ }
443
+ try {
444
+ was = key ? first(host.db, key, request) : null;
445
+ // No pending/running row for a successful live write. Readiness may
446
+ // yield, so check the key again afterwards and inside the commit,
447
+ // immediately before synchronous apply. Concurrent same-key requests
448
+ // cannot mutate twice.
449
+ if (!was && key) {
450
+ let no = await host.ready(request);
451
+ was = key ? first(host.db, key, request) : null;
452
+ if (!was && !no && idle() && !waiting(host.db)) {
453
+ let r = await host.commit(sent(), {
454
+ request
455
+ });
456
+ // Its transaction unwound when the window refused it mid-way.
457
+ let stop = r.status == 429 ? host.window.refusal() : null;
458
+ if (stop) return hold(request, body, stop);
459
+ if (r.status < 500) return host.answer(request, r);
460
+ // An error after the transaction must not enqueue its mutation
461
+ // again.
462
+ let committed = key ? first(host.db, key, request) : null;
463
+ if (committed?.state == 'applied') {
464
+ return host.answer(request, new Response(committed.answer, JSONED));
465
+ }
466
+ seq = keep(host.db, request, body, now);
467
+ aside(host.db, seq, await said(r.clone()));
468
+ return park(seq, await said(r));
469
+ }
470
+ }
471
+ seq = was?.seq ?? keep(host.db, request, body, now);
472
+ } catch (e) {
473
+ if (e instanceof Refused) {
474
+ return e.status == 503 ? json({
475
+ error: e.name,
476
+ message: e.message
477
+ }, 503) : refuse(e);
478
+ }
479
+ if (host.window.stops(e)) return hold(request, body, e);
480
+ report(e, 'write log');
481
+ // Readiness can throw too. Keep that input if storage still works;
482
+ // never bypass a keyed receipt after an error and risk a second
483
+ // mutation.
484
+ if (key) {
485
+ try {
486
+ let was = first(host.db, key, request);
487
+ if (was?.state == 'applied') {
488
+ return host.answer(request, new Response(was.answer, JSONED));
489
+ }
490
+ let seq = was?.seq ?? keep(host.db, request, body, now);
491
+ aside(host.db, seq, String(e));
492
+ return park(seq, String(e));
493
+ } catch {
494
+ return refuse(e);
495
+ }
496
+ }
497
+ return unkept();
498
+ }
499
+ if (was?.state == 'applied') {
500
+ return host.answer(request, new Response(was.answer, JSONED));
501
+ }
502
+ if (was?.state == 'refused') return refuse(new Refused(was.why));
503
+ if (was?.state == 'failed') return parked(seq, was.why, was.audit);
504
+ if (was?.state == 'interrupted' || was?.state == 'unreviewed') {
505
+ return parked(seq, was.why, true);
506
+ }
507
+ // Heard before yielding, so a replay a startup effect drains meanwhile
508
+ // answers it and treats its refusal as a live one.
509
+ let answer = heard(seq);
510
+ let no = await host.ready(request);
511
+ if (no || stuck()) {
512
+ tell(seq, park(seq, no ? host.refused() ?? 'this app could not start' : 'earlier writes to this app are still waiting'));
513
+ } else void drain();
514
+ return answer.then((response)=>host.answer(request, response));
515
+ };
516
+ let { plugin, during } = landing(host);
517
+ return {
518
+ plugin,
519
+ write,
520
+ drain,
521
+ async bound (request, text, check = false) {
522
+ let key = request.headers.get(IDEMPOTENCY);
523
+ if (!durable(key)) return request;
524
+ await host.authorize(request);
525
+ request = fingerprinted(request, text);
526
+ // A rehearsal never comes in at `write`: its key is named here.
527
+ let answer = check ? remembered(host.db, receiptKey(host.writer(request), key), request) : null;
528
+ return answer ? host.answer(request, answer) : request;
529
+ },
530
+ applying (request, mark, apply) {
531
+ if (mark != null && typeof mark != 'number') {
532
+ let key = mark.request.headers.get(IDEMPOTENCY);
533
+ let was = key ? first(host.db, key, request) : null;
534
+ if (was?.state == 'applied') return new Response(was.answer, JSONED);
535
+ if (was) return parked(was.seq, was.why, was.audit);
536
+ }
537
+ return during(mark, apply);
538
+ },
539
+ settle,
540
+ mend
541
+ };
542
+ };
543
+ //# sourceMappingURL=receipts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"receipts.js","sources":["./receipts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,SAAS,MAAM,gCAAqC;AACpD,SAKE,KAAK,2BACwB;AAE/B,SAAS,OAAO,kCAAuC;AACvD,SAAS,SAAS,uBAAsB;AACxC,SAAS,IAAI,EAAE,MAAM,sBAAqB;AAC1C,SACE,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,aAAa,EACb,KAAK,EACL,IAAI,EACJ,IAAI,EACJ,WAAW,EACX,IAAI,EAEJ,MAAM,EACN,KAAK,EACL,IAAI,EACJ,MAAM,EACN,KAAK,EACL,MAAM,EACN,KAAK,EAEL,UAAU,EACV,YAAY,MAAM,EAClB,UAAU,EACV,QAAQ,EACR,IAAI,EAEJ,OAAO,EACP,KAAK,EACL,SAAS,EACT,OAAO,mBACQ;AAKjB,IAAI,UAAU,IAAI;AAClB,IAAI,aAAa;AAEjB,MAAM,eAAe;;EACnB,YAAY,AAAO,MAAgB,CAAE;IACnC,KAAK,CAAC,gCADW,SAAA;IAEjB,IAAI,CAAC,IAAI,GAAG;EACd;AACF;AAEA,0EAA0E;AAC1E,uEAAuE;AACvE,eAAe;AACf,IAAI,SAAS,IAAI;A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