@yaks/durable-object 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.
- package/LICENSE +202 -0
- package/README.md +554 -2
- package/_dist/alarm.d.ts +35 -0
- package/_dist/alarm.d.ts.map +1 -0
- package/_dist/ceiling.d.ts +167 -0
- package/_dist/ceiling.d.ts.map +1 -0
- package/_dist/compose.d.ts +79 -0
- package/_dist/compose.d.ts.map +1 -0
- package/_dist/copy.d.ts +67 -0
- package/_dist/copy.d.ts.map +1 -0
- package/_dist/door.d.ts +172 -0
- package/_dist/door.d.ts.map +1 -0
- package/_dist/job.d.ts +26 -0
- package/_dist/job.d.ts.map +1 -0
- package/_dist/keys.d.ts +14 -0
- package/_dist/keys.d.ts.map +1 -0
- package/_dist/mod.d.ts +65 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/object.d.ts +75 -0
- package/_dist/object.d.ts.map +1 -0
- package/_dist/profile.d.ts +32 -0
- package/_dist/profile.d.ts.map +1 -0
- package/_dist/query-memory.d.ts +27 -0
- package/_dist/query-memory.d.ts.map +1 -0
- package/_dist/resets.d.ts +208 -0
- package/_dist/resets.d.ts.map +1 -0
- package/_dist/rewind.d.ts +21 -0
- package/_dist/rewind.d.ts.map +1 -0
- package/_dist/sockets.d.ts +90 -0
- package/_dist/sockets.d.ts.map +1 -0
- package/_dist/sql.d.ts +72 -0
- package/_dist/sql.d.ts.map +1 -0
- package/_dist/store.d.ts +8 -0
- package/_dist/store.d.ts.map +1 -0
- package/alarm.js +126 -0
- package/alarm.js.map +1 -0
- package/alarm.ts +188 -0
- package/ceiling.js +536 -0
- package/ceiling.js.map +1 -0
- package/ceiling.ts +681 -0
- package/compose.js +201 -0
- package/compose.js.map +1 -0
- package/compose.ts +249 -0
- package/copy.js +394 -0
- package/copy.js.map +1 -0
- package/copy.ts +413 -0
- package/deno.json +1 -0
- package/door.js +364 -0
- package/door.js.map +1 -0
- package/door.ts +542 -0
- package/example.ts +108 -0
- package/holds.js +85 -0
- package/holds.js.map +1 -0
- package/holds.ts +72 -0
- package/job.js +151 -0
- package/job.js.map +1 -0
- package/job.ts +166 -0
- package/keys.js +59 -0
- package/keys.js.map +1 -0
- package/keys.ts +85 -0
- package/mod.js +65 -0
- package/mod.js.map +1 -0
- package/mod.ts +167 -0
- package/object.js +247 -0
- package/object.js.map +1 -0
- package/object.ts +385 -0
- package/package.json +68 -4
- package/profile.js +131 -0
- package/profile.js.map +1 -0
- package/profile.ts +150 -0
- package/query-memory.js +157 -0
- package/query-memory.js.map +1 -0
- package/query-memory.ts +240 -0
- package/resets.js +302 -0
- package/resets.js.map +1 -0
- package/resets.ts +353 -0
- package/rewind.js +68 -0
- package/rewind.js.map +1 -0
- package/rewind.ts +91 -0
- package/sockets.js +749 -0
- package/sockets.js.map +1 -0
- package/sockets.ts +991 -0
- package/sql.js +221 -0
- package/sql.js.map +1 -0
- package/sql.ts +330 -0
- package/store.js +58 -0
- package/store.js.map +1 -0
- package/store.ts +80 -0
- package/watches.js +8 -0
- package/watches.js.map +1 -0
- package/watches.ts +23 -0
package/alarm.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Durable Object's one alarm, worked as one loop: everything the object
|
|
3
|
+
* does on its own time (wakes, the effects pool, upkeep, a backlog) is a
|
|
4
|
+
* {@link Work}, and each time the alarm rings every work is given one slice
|
|
5
|
+
* and answers when it is next owed. The alarm is then set for the soonest.
|
|
6
|
+
*
|
|
7
|
+
* - An alarm that answers once its slice is under way (`keep`) stands at a
|
|
8
|
+
* backstop a few seconds out while the slice runs: an instance a deploy or
|
|
9
|
+
* an eviction cuts off mid-slice is rung again, and its replacement takes
|
|
10
|
+
* the work up. One that answers when its slice ends needs none, since the
|
|
11
|
+
* runtime delivers an alarm again when its handler was cut off, and an idle
|
|
12
|
+
* ring writes nothing.
|
|
13
|
+
* - An object past its row ceiling (`./ceiling.ts`) works nothing and is rung
|
|
14
|
+
* again when its window ends; a work it stopped part way is no failure.
|
|
15
|
+
* - A work that fails is reported and tried again a minute on, and the works
|
|
16
|
+
* after it still run.
|
|
17
|
+
* - A ring while a slice is still going starts no second slice.
|
|
18
|
+
*
|
|
19
|
+
* Writes arm the alarm with {@link Clock.arm}, which keeps an alarm already
|
|
20
|
+
* set sooner. The loop is pure over the {@link Alarms} it is handed, so Deno
|
|
21
|
+
* drives it with `./testing.ts`'s stand-in storage.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { clock } from '@yaks/durable-object/alarm'
|
|
25
|
+
* import { assertEquals } from '@std/assert'
|
|
26
|
+
*
|
|
27
|
+
* let at: number | null = null, now = 1000, owed: number[] = []
|
|
28
|
+
* let storage = {
|
|
29
|
+
* getAlarm: () => Promise.resolve(at),
|
|
30
|
+
* setAlarm: (next: number) => Promise.resolve(void (at = next)),
|
|
31
|
+
* }
|
|
32
|
+
* let time = clock(storage, {
|
|
33
|
+
* sweep: () => (owed.push(now), now + 5000),
|
|
34
|
+
* }, { now: () => now })
|
|
35
|
+
* await time.ring()
|
|
36
|
+
* assertEquals([owed, at], [[1000], 6000])
|
|
37
|
+
* ```
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module
|
|
41
|
+
*/ import { LOST } from "@yaks/wake/cloudflare";
|
|
42
|
+
import { stopped } from "./ceiling.js";
|
|
43
|
+
/** How long one ring works. */ export let SLICE = 10_000;
|
|
44
|
+
/** How far out the alarm stands while a slice runs. */ export let BACKSTOP = 15_000;
|
|
45
|
+
/** How long a failed work waits before it is tried again. */ export let RETRY = 60_000;
|
|
46
|
+
/** The loop over `works`, run in the order given. Given as a function, the
|
|
47
|
+
* works are asked for at each ring, so an object can name them once it has
|
|
48
|
+
* opened what they work on. */ export let clock = (storage, works, opts = {})=>{
|
|
49
|
+
let now = opts.now ?? Date.now;
|
|
50
|
+
let slice = opts.slice ?? SLICE;
|
|
51
|
+
let backstop = opts.backstop ?? BACKSTOP;
|
|
52
|
+
let retry = opts.retry ?? RETRY;
|
|
53
|
+
let report = opts.report ?? ((e)=>console.error(e));
|
|
54
|
+
// The alarm a ring took over: the one standing when it began, which the
|
|
55
|
+
// works' answer supersedes even when it is later, or the backstop it set.
|
|
56
|
+
// One armed since, by a write during the slice, stands where it is sooner,
|
|
57
|
+
// and one later than the alarm taken over is owed with the works' answer.
|
|
58
|
+
let standing = null;
|
|
59
|
+
let asked = Infinity;
|
|
60
|
+
let going;
|
|
61
|
+
// The runtime gives an object one alarm, and two handlers' read, compare
|
|
62
|
+
// and write would race, so every change to it waits on the one before.
|
|
63
|
+
let serial = Promise.resolve();
|
|
64
|
+
let change = (body)=>{
|
|
65
|
+
let next = serial.then(async ()=>{
|
|
66
|
+
let held = await storage.getAlarm();
|
|
67
|
+
await body(held != null && held < now() - LOST ? null : held);
|
|
68
|
+
});
|
|
69
|
+
serial = next.catch(()=>{});
|
|
70
|
+
return next;
|
|
71
|
+
};
|
|
72
|
+
let arm = (at)=>change(async (held)=>{
|
|
73
|
+
if (held != null && held == standing) asked = Math.min(asked, at);
|
|
74
|
+
if (held == null || at < held) await storage.setAlarm(at);
|
|
75
|
+
});
|
|
76
|
+
let work = async ()=>{
|
|
77
|
+
let until = now() + slice, next = Infinity;
|
|
78
|
+
let named = typeof works == 'function' ? works() : works;
|
|
79
|
+
for (let [name, run] of Object.entries(named)){
|
|
80
|
+
try {
|
|
81
|
+
let at = await (opts.around ? opts.around(name, ()=>run(until)) : run(until));
|
|
82
|
+
if (at != null) next = Math.min(next, at);
|
|
83
|
+
} catch (e) {
|
|
84
|
+
// Stopped part way, the object takes nothing until its window ends,
|
|
85
|
+
// so the works after this one would only be stopped too.
|
|
86
|
+
if (stopped(e)) {
|
|
87
|
+
next = Math.min(next, now() + e.retry.after);
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
report(e, name);
|
|
91
|
+
next = Math.min(next, now() + retry);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
await change(async (held)=>{
|
|
95
|
+
let mine = standing;
|
|
96
|
+
next = Math.min(next, asked);
|
|
97
|
+
standing = null;
|
|
98
|
+
asked = Infinity;
|
|
99
|
+
if (Number.isFinite(next)) {
|
|
100
|
+
if (held == null || held == mine || next < held) {
|
|
101
|
+
await storage.setAlarm(next);
|
|
102
|
+
}
|
|
103
|
+
} else if (held != null && held == mine) await storage.deleteAlarm?.();
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
let ring = async ()=>{
|
|
107
|
+
let wait = opts.wait?.() ?? 0;
|
|
108
|
+
if (wait) return arm(now() + wait);
|
|
109
|
+
let mine = now() + backstop, was = standing;
|
|
110
|
+
await change(async (held)=>{
|
|
111
|
+
if (opts.keep && (held == null || held == was || mine < held)) {
|
|
112
|
+
await storage.setAlarm(held = mine);
|
|
113
|
+
}
|
|
114
|
+
standing = held;
|
|
115
|
+
});
|
|
116
|
+
if (going) return;
|
|
117
|
+
let run = going = work().finally(()=>going = undefined);
|
|
118
|
+
if (opts.keep) opts.keep(run.catch((e)=>report(e, 'alarm')));
|
|
119
|
+
else await run;
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
arm,
|
|
123
|
+
ring
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=alarm.js.map
|
package/alarm.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm.js","sources":["./alarm.ts"],"names":[],"mappings":"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"}
|
package/alarm.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Durable Object's one alarm, worked as one loop: everything the object
|
|
3
|
+
* does on its own time (wakes, the effects pool, upkeep, a backlog) is a
|
|
4
|
+
* {@link Work}, and each time the alarm rings every work is given one slice
|
|
5
|
+
* and answers when it is next owed. The alarm is then set for the soonest.
|
|
6
|
+
*
|
|
7
|
+
* - An alarm that answers once its slice is under way (`keep`) stands at a
|
|
8
|
+
* backstop a few seconds out while the slice runs: an instance a deploy or
|
|
9
|
+
* an eviction cuts off mid-slice is rung again, and its replacement takes
|
|
10
|
+
* the work up. One that answers when its slice ends needs none, since the
|
|
11
|
+
* runtime delivers an alarm again when its handler was cut off, and an idle
|
|
12
|
+
* ring writes nothing.
|
|
13
|
+
* - An object past its row ceiling (`./ceiling.ts`) works nothing and is rung
|
|
14
|
+
* again when its window ends; a work it stopped part way is no failure.
|
|
15
|
+
* - A work that fails is reported and tried again a minute on, and the works
|
|
16
|
+
* after it still run.
|
|
17
|
+
* - A ring while a slice is still going starts no second slice.
|
|
18
|
+
*
|
|
19
|
+
* Writes arm the alarm with {@link Clock.arm}, which keeps an alarm already
|
|
20
|
+
* set sooner. The loop is pure over the {@link Alarms} it is handed, so Deno
|
|
21
|
+
* drives it with `./testing.ts`'s stand-in storage.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { clock } from '@yaks/durable-object/alarm'
|
|
25
|
+
* import { assertEquals } from '@std/assert'
|
|
26
|
+
*
|
|
27
|
+
* let at: number | null = null, now = 1000, owed: number[] = []
|
|
28
|
+
* let storage = {
|
|
29
|
+
* getAlarm: () => Promise.resolve(at),
|
|
30
|
+
* setAlarm: (next: number) => Promise.resolve(void (at = next)),
|
|
31
|
+
* }
|
|
32
|
+
* let time = clock(storage, {
|
|
33
|
+
* sweep: () => (owed.push(now), now + 5000),
|
|
34
|
+
* }, { now: () => now })
|
|
35
|
+
* await time.ring()
|
|
36
|
+
* assertEquals([owed, at], [[1000], 6000])
|
|
37
|
+
* ```
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @module
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
import { LOST } from '@yaks/wake/cloudflare'
|
|
44
|
+
import { stopped } from './ceiling.js'
|
|
45
|
+
|
|
46
|
+
/** The alarm methods a Durable Object's storage provides. */
|
|
47
|
+
export type Alarms = {
|
|
48
|
+
getAlarm(): Promise<number | null>
|
|
49
|
+
setAlarm(at: number): Promise<void>
|
|
50
|
+
deleteAlarm?(): Promise<void>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* One kind of work the alarm does. Given the instant its slice ends, it does
|
|
55
|
+
* what is due and answers when it is next owed, in ms since the epoch: now or
|
|
56
|
+
* earlier when the slice ended with some left, null when nothing is owed.
|
|
57
|
+
*/
|
|
58
|
+
export type Work = (
|
|
59
|
+
until: number,
|
|
60
|
+
) => Promise<number | null | undefined> | number | null | undefined
|
|
61
|
+
|
|
62
|
+
/** How the loop is run. */
|
|
63
|
+
export type Options = {
|
|
64
|
+
/** The clock; `Date.now` by default. */
|
|
65
|
+
now?: () => number
|
|
66
|
+
/** A work's failure, by the work's name. */
|
|
67
|
+
report?: (error: unknown, work: string) => void
|
|
68
|
+
/** Observe a named work for as long as its slice runs. */
|
|
69
|
+
around?: (name: string, run: () => ReturnType<Work>) => ReturnType<Work>
|
|
70
|
+
/** How long until the object may work again: its ceiling's
|
|
71
|
+
* `stopped(true)`, 0 while it is admitted. */
|
|
72
|
+
wait?: () => number
|
|
73
|
+
/** Given the slice once it is under way, so the alarm answers then: the
|
|
74
|
+
* object's `waitUntil`. Without it the ring answers once the slice ends. */
|
|
75
|
+
keep?: (slice: Promise<void>) => void
|
|
76
|
+
/** How long one ring works: an alarm is one invocation, and the runtime
|
|
77
|
+
* caps its CPU. */
|
|
78
|
+
slice?: number
|
|
79
|
+
/** How far out the alarm stands while a slice it answered before runs. */
|
|
80
|
+
backstop?: number
|
|
81
|
+
/** How long a failed work waits before it is tried again. */
|
|
82
|
+
retry?: number
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** An object's alarm loop. */
|
|
86
|
+
export type Clock = {
|
|
87
|
+
/** Ring at `at` (ms since the epoch), unless the alarm is set sooner. */
|
|
88
|
+
arm(at: number): Promise<void>
|
|
89
|
+
/** The runtime's `alarm()`: one slice of every work, then the alarm set
|
|
90
|
+
* for the soonest owed. */
|
|
91
|
+
ring(): Promise<void>
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** How long one ring works. */
|
|
95
|
+
export let SLICE = 10_000
|
|
96
|
+
/** How far out the alarm stands while a slice runs. */
|
|
97
|
+
export let BACKSTOP = 15_000
|
|
98
|
+
/** How long a failed work waits before it is tried again. */
|
|
99
|
+
export let RETRY = 60_000
|
|
100
|
+
|
|
101
|
+
/** The loop over `works`, run in the order given. Given as a function, the
|
|
102
|
+
* works are asked for at each ring, so an object can name them once it has
|
|
103
|
+
* opened what they work on. */
|
|
104
|
+
export let clock = (
|
|
105
|
+
storage: Alarms,
|
|
106
|
+
works: Record<string, Work> | (() => Record<string, Work>),
|
|
107
|
+
opts: Options = {},
|
|
108
|
+
): Clock => {
|
|
109
|
+
let now = opts.now ?? Date.now
|
|
110
|
+
let slice = opts.slice ?? SLICE
|
|
111
|
+
let backstop = opts.backstop ?? BACKSTOP
|
|
112
|
+
let retry = opts.retry ?? RETRY
|
|
113
|
+
let report = opts.report ?? ((e) => console.error(e))
|
|
114
|
+
// The alarm a ring took over: the one standing when it began, which the
|
|
115
|
+
// works' answer supersedes even when it is later, or the backstop it set.
|
|
116
|
+
// One armed since, by a write during the slice, stands where it is sooner,
|
|
117
|
+
// and one later than the alarm taken over is owed with the works' answer.
|
|
118
|
+
let standing: number | null = null
|
|
119
|
+
let asked = Infinity
|
|
120
|
+
let going: Promise<void> | undefined
|
|
121
|
+
// The runtime gives an object one alarm, and two handlers' read, compare
|
|
122
|
+
// and write would race, so every change to it waits on the one before.
|
|
123
|
+
let serial: Promise<unknown> = Promise.resolve()
|
|
124
|
+
let change = (body: (held: number | null) => Promise<void>) => {
|
|
125
|
+
let next = serial.then(async () => {
|
|
126
|
+
let held = await storage.getAlarm()
|
|
127
|
+
await body(held != null && held < now() - LOST ? null : held)
|
|
128
|
+
})
|
|
129
|
+
serial = next.catch(() => {})
|
|
130
|
+
return next
|
|
131
|
+
}
|
|
132
|
+
let arm = (at: number) =>
|
|
133
|
+
change(async (held) => {
|
|
134
|
+
if (held != null && held == standing) asked = Math.min(asked, at)
|
|
135
|
+
if (held == null || at < held) await storage.setAlarm(at)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
let work = async () => {
|
|
139
|
+
let until = now() + slice, next = Infinity
|
|
140
|
+
let named = typeof works == 'function' ? works() : works
|
|
141
|
+
for (let [name, run] of Object.entries(named)) {
|
|
142
|
+
try {
|
|
143
|
+
let at = await (opts.around
|
|
144
|
+
? opts.around(name, () => run(until))
|
|
145
|
+
: run(until))
|
|
146
|
+
if (at != null) next = Math.min(next, at)
|
|
147
|
+
} catch (e) {
|
|
148
|
+
// Stopped part way, the object takes nothing until its window ends,
|
|
149
|
+
// so the works after this one would only be stopped too.
|
|
150
|
+
if (stopped(e)) {
|
|
151
|
+
next = Math.min(next, now() + e.retry.after)
|
|
152
|
+
break
|
|
153
|
+
}
|
|
154
|
+
report(e, name)
|
|
155
|
+
next = Math.min(next, now() + retry)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
await change(async (held) => {
|
|
159
|
+
let mine = standing
|
|
160
|
+
next = Math.min(next, asked)
|
|
161
|
+
standing = null
|
|
162
|
+
asked = Infinity
|
|
163
|
+
if (Number.isFinite(next)) {
|
|
164
|
+
if (held == null || held == mine || next < held) {
|
|
165
|
+
await storage.setAlarm(next)
|
|
166
|
+
}
|
|
167
|
+
} else if (held != null && held == mine) await storage.deleteAlarm?.()
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let ring = async () => {
|
|
172
|
+
let wait = opts.wait?.() ?? 0
|
|
173
|
+
if (wait) return arm(now() + wait)
|
|
174
|
+
let mine = now() + backstop, was = standing
|
|
175
|
+
await change(async (held) => {
|
|
176
|
+
if (opts.keep && (held == null || held == was || mine < held)) {
|
|
177
|
+
await storage.setAlarm(held = mine)
|
|
178
|
+
}
|
|
179
|
+
standing = held
|
|
180
|
+
})
|
|
181
|
+
if (going) return
|
|
182
|
+
let run = going = work().finally(() => going = undefined)
|
|
183
|
+
if (opts.keep) opts.keep(run.catch((e) => report(e, 'alarm')))
|
|
184
|
+
else await run
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { arm, ring }
|
|
188
|
+
}
|