@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/object.ts
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one Durable Object class: an object whose graph is opened from a
|
|
3
|
+
* {@link Spec}, served at its door, followed live over its sockets, and
|
|
4
|
+
* worked on its own time by one alarm (`./alarm.ts`).
|
|
5
|
+
*
|
|
6
|
+
* The platform requires a class, so this is one; what differs between kinds
|
|
7
|
+
* of object is data, the spec. A subclass hands it to `super`, or answers
|
|
8
|
+
* it from `specify()` where it names the subclass's own members:
|
|
9
|
+
*
|
|
10
|
+
* - `open` composes what the object holds, once an instance: its door, the
|
|
11
|
+
* subscriptions its sockets serve, and the works its alarm runs. One that
|
|
12
|
+
* fails is opened again by the next entry; a failed open never leaves the
|
|
13
|
+
* object rejecting every call.
|
|
14
|
+
* - Every entry (`fetch`, `alarm`, a socket's frame, and a subclass's own
|
|
15
|
+
* through `enter`, such as an RPC method) opens first and wakes the sockets
|
|
16
|
+
* the object inherited from hibernation, so a write made by the request
|
|
17
|
+
* that woke it reaches them. A failure is reported, by its entry.
|
|
18
|
+
* - The object's row ceiling (`./ceiling.ts`) is made before any statement,
|
|
19
|
+
* with the spec's report. Its async key-value calls, through `keys`, count
|
|
20
|
+
* against it as statements do. A door's statement past a window is answered as
|
|
21
|
+
* `Stopped` with how long to wait; an alarm past one rings again then.
|
|
22
|
+
* - A new deploy restarts the object idle: nothing runs at the first entry on
|
|
23
|
+
* a new `version` but arming the alarm, which takes up what is owed.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Durable, type State } from '@yaks/durable-object/object'
|
|
27
|
+
* import { assertEquals } from '@std/assert'
|
|
28
|
+
*
|
|
29
|
+
* class Hello extends Durable {
|
|
30
|
+
* constructor(ctx: State) {
|
|
31
|
+
* super(ctx, {
|
|
32
|
+
* open: () => Promise.resolve({ serve: () => new Response('hello') }),
|
|
33
|
+
* report: (e) => console.error(e),
|
|
34
|
+
* })
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
* // The runtime's own state; a door that reads nothing needs none of it.
|
|
38
|
+
* let ctx = { storage: {} } as State
|
|
39
|
+
* let answer = await new Hello(ctx).fetch(new Request('https://do/'))
|
|
40
|
+
* assertEquals(await answer.text(), 'hello')
|
|
41
|
+
* ```
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @module
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
import { refuse } from '@yaks/api/refuse'
|
|
48
|
+
import type { RecoverableListening } from 'jsr:@yaks/api@0.2.36'
|
|
49
|
+
import {
|
|
50
|
+
type Alarms,
|
|
51
|
+
type Clock,
|
|
52
|
+
clock,
|
|
53
|
+
type Options as Timing,
|
|
54
|
+
type Work,
|
|
55
|
+
} from './alarm.js'
|
|
56
|
+
import {
|
|
57
|
+
bounded,
|
|
58
|
+
type Ceiling,
|
|
59
|
+
type Kv,
|
|
60
|
+
type Options as Limits,
|
|
61
|
+
stopped,
|
|
62
|
+
} from './ceiling.js'
|
|
63
|
+
import { type Keys, keys } from './keys.js'
|
|
64
|
+
import type { Hibernation, Sockets, Wire } from './sockets.js'
|
|
65
|
+
import type { DurableStorage } from './sql.ts'
|
|
66
|
+
|
|
67
|
+
/** The slice of `DurableObjectState` the class uses. */
|
|
68
|
+
export type State = Hibernation & {
|
|
69
|
+
id?: { name?: string }
|
|
70
|
+
storage: DurableStorage & Alarms & { kv?: Kv }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What an object opened: its door, its subscriptions and its works. */
|
|
74
|
+
export type Opened = {
|
|
75
|
+
/** Every request the object is sent. A socket is accepted here, through
|
|
76
|
+
* the object's {@link Durable.accept}, after whatever the door checks. An
|
|
77
|
+
* object with no door, called only through its own entries, answers every
|
|
78
|
+
* request 404. */
|
|
79
|
+
serve?(request: Request): Response | Promise<Response>
|
|
80
|
+
/** The subscriptions its sockets serve; without them it takes none. */
|
|
81
|
+
subs?: RecoverableListening
|
|
82
|
+
/** What its alarm works, in order (`./alarm.ts`). */
|
|
83
|
+
works?: Record<string, Work>
|
|
84
|
+
/** Whether the object takes a socket's frame or close now. One it does
|
|
85
|
+
* not is let go unanswered, before anything is asked of its sockets, a
|
|
86
|
+
* clock probe included. */
|
|
87
|
+
admit?(ws: Wire): boolean | Promise<boolean>
|
|
88
|
+
/** The sockets serving `ws` now, where the object makes its own over
|
|
89
|
+
* subscriptions it remakes (`subs` makes them once), or keeps several sets
|
|
90
|
+
* of them, asked once a frame or close is admitted and a clock probe
|
|
91
|
+
* answered. None hangs a frame's socket up: the page must open another. */
|
|
92
|
+
live?(ws: Wire): Sockets | undefined | Promise<Sockets | undefined>
|
|
93
|
+
/** A frame from one of its sockets, given `take`, the sockets' own reading
|
|
94
|
+
* of it, which answers whether they took it: the object reads frames
|
|
95
|
+
* itself, or around them. */
|
|
96
|
+
frame?(
|
|
97
|
+
ws: Wire,
|
|
98
|
+
data: string | ArrayBuffer,
|
|
99
|
+
take: () => Promise<boolean>,
|
|
100
|
+
): unknown
|
|
101
|
+
/** One of its sockets went away, once its sockets let it go. */
|
|
102
|
+
closed?(ws: Wire): unknown
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** What makes one kind of object. */
|
|
106
|
+
export type Spec = {
|
|
107
|
+
/** Compose what the object holds, once an instance, given the object: its
|
|
108
|
+
* state, its clock and its ceiling; nothing by default. */
|
|
109
|
+
open?(object: Durable): Promise<Opened>
|
|
110
|
+
/** A failure the object caught, by the entry it was caught in. */
|
|
111
|
+
report(error: unknown, entry: string): unknown
|
|
112
|
+
/** The row ceiling's options, refusing by default. Without them the
|
|
113
|
+
* object counts against the ceiling its storage already has: the one named
|
|
114
|
+
* first, or one that reports and refuses nothing. */
|
|
115
|
+
ceiling?: Limits
|
|
116
|
+
/** The alarm's timing (`./alarm.ts`). */
|
|
117
|
+
alarm?: Pick<
|
|
118
|
+
Timing,
|
|
119
|
+
'slice' | 'backstop' | 'retry' | 'keep' | 'now' | 'around'
|
|
120
|
+
>
|
|
121
|
+
/** The deploy the object runs, whose first entry arms the alarm. */
|
|
122
|
+
version?: string
|
|
123
|
+
/** Each entry's work, run inside what the object measures it by. */
|
|
124
|
+
around?<T>(entry: string, work: () => Promise<T>): Promise<T>
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Where the deploy an object last ran is kept, in its synchronous kv.
|
|
128
|
+
let VERSION = '@yaks/durable-object:version'
|
|
129
|
+
|
|
130
|
+
/** One Durable Object, of the kind its spec makes. */
|
|
131
|
+
export class Durable {
|
|
132
|
+
/** The object's own state: its storage and its sockets. */
|
|
133
|
+
readonly ctx: State
|
|
134
|
+
#spec?: Spec
|
|
135
|
+
#ceiling?: Ceiling
|
|
136
|
+
#clock?: Clock
|
|
137
|
+
#keys?: Keys
|
|
138
|
+
#opened?: Promise<Opened>
|
|
139
|
+
#held?: Opened
|
|
140
|
+
#live?: Sockets
|
|
141
|
+
#restarted = false
|
|
142
|
+
|
|
143
|
+
/** Without a `spec`, the object asks its `specify()` for one. */
|
|
144
|
+
constructor(ctx: State, spec?: Spec) {
|
|
145
|
+
this.ctx = ctx
|
|
146
|
+
this.#spec = spec
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** What makes this kind of object, asked once, at the object's first
|
|
150
|
+
* entry or its ceiling: a subclass whose spec names its own members
|
|
151
|
+
* answers it here. */
|
|
152
|
+
protected specify(): Spec {
|
|
153
|
+
throw new Error(`${this.constructor.name} has no spec`)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** What makes this object. */
|
|
157
|
+
get spec(): Spec {
|
|
158
|
+
return this.#spec ??= this.specify()
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The object's row ceiling, made at its first statement through the
|
|
162
|
+
* object. A subclass that opens a driver of its own before (`open` is the
|
|
163
|
+
* object's) names its ceiling first, with `this.ceiling` at the end of its
|
|
164
|
+
* constructor: every driver counts against the ceiling made first. */
|
|
165
|
+
get ceiling(): Ceiling {
|
|
166
|
+
let named = this.spec.ceiling
|
|
167
|
+
return this.#ceiling ??= named
|
|
168
|
+
? bounded(this.ctx.storage, { refuse: true, ...named })
|
|
169
|
+
: bounded(this.ctx.storage)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** The object's async key-value storage, each call counted against its
|
|
173
|
+
* ceiling as a statement is (`./keys.ts`). */
|
|
174
|
+
get keys(): Keys {
|
|
175
|
+
this.ceiling
|
|
176
|
+
// Every SQLite-backed object's storage has them; `State` leaves them to
|
|
177
|
+
// the stand-ins of objects that keep none.
|
|
178
|
+
return this.#keys ??= keys(this.ctx.storage as State['storage'] & Keys)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The object's alarm: a write that owes work arms it. */
|
|
182
|
+
get clock(): Clock {
|
|
183
|
+
return this.#clock ??= clock(
|
|
184
|
+
this.ctx.storage,
|
|
185
|
+
() => this.#held?.works ?? {},
|
|
186
|
+
{
|
|
187
|
+
wait: () => this.ceiling.stopped(true),
|
|
188
|
+
report: (e, work) => this.spec.report(e, `alarm ${work}`),
|
|
189
|
+
...this.spec.alarm,
|
|
190
|
+
},
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The object's sockets, once it has opened subscriptions. */
|
|
195
|
+
get live(): Sockets | undefined {
|
|
196
|
+
return this.#live
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** What the object holds, opened once an instance; its sockets woken. */
|
|
200
|
+
async open(): Promise<Opened> {
|
|
201
|
+
let open = this.spec.open ?? (() => Promise.resolve<Opened>({}))
|
|
202
|
+
this.#opened ??= open(this).then(
|
|
203
|
+
async (opened) => {
|
|
204
|
+
this.ceiling
|
|
205
|
+
// The sockets' plumbing is loaded only by an object that has some.
|
|
206
|
+
if (opened.subs) {
|
|
207
|
+
let { sockets } = await import('./sockets.js')
|
|
208
|
+
this.#live = sockets(opened.subs, this.ctx)
|
|
209
|
+
}
|
|
210
|
+
return this.#held = opened
|
|
211
|
+
},
|
|
212
|
+
(e) => {
|
|
213
|
+
this.#opened = undefined
|
|
214
|
+
throw e
|
|
215
|
+
},
|
|
216
|
+
)
|
|
217
|
+
let opened = await this.#opened
|
|
218
|
+
await this.#live?.wake()
|
|
219
|
+
await this.#restart()
|
|
220
|
+
return opened
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// The first entry on a new deploy arms the alarm, once an instance.
|
|
224
|
+
async #restart() {
|
|
225
|
+
let { version } = this.spec, kv = this.ctx.storage.kv
|
|
226
|
+
if (this.#restarted || !version || !kv) return
|
|
227
|
+
this.#restarted = true
|
|
228
|
+
if (kv.get(VERSION) == version) return
|
|
229
|
+
kv.put(VERSION, version)
|
|
230
|
+
await this.clock.arm(this.spec.alarm?.now?.() ?? Date.now())
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
#around<T>(entry: string, work: () => Promise<T>): Promise<T> {
|
|
234
|
+
return this.spec.around ? this.spec.around(entry, work) : work()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A socket for this object's subscriptions: the door's to call. */
|
|
238
|
+
accept(...args: Parameters<Sockets['accept']>): Response {
|
|
239
|
+
if (!this.#live) throw new Error('this object serves no subscriptions')
|
|
240
|
+
return this.#live.accept(...args)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A request: the door's answer, `Stopped`'s with how long to wait, or a
|
|
244
|
+
* 503 for a failure the door did not answer, which is reported. */
|
|
245
|
+
async fetch(request: Request): Promise<Response> {
|
|
246
|
+
let { pathname } = new URL(request.url)
|
|
247
|
+
return await this.#enter(
|
|
248
|
+
`${request.method} ${pathname}`,
|
|
249
|
+
'fetch',
|
|
250
|
+
(opened) =>
|
|
251
|
+
opened.serve
|
|
252
|
+
? opened.serve(request)
|
|
253
|
+
: Response.json({ error: 'not found' }, { status: 404 }),
|
|
254
|
+
).catch((error) =>
|
|
255
|
+
stopped(error)
|
|
256
|
+
? refuse(error)
|
|
257
|
+
: Response.json({ error: 'temporarily unavailable' }, { status: 503 })
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** An entry of the subclass's own, such as an RPC method: `work`, given
|
|
262
|
+
* what the object opened, inside `around` as `entry`. What it throws is
|
|
263
|
+
* thrown on to its caller, and a failure other than the ceiling's
|
|
264
|
+
* `Stopped` is reported by `entry` first, as a request's is. */
|
|
265
|
+
protected enter<T>(
|
|
266
|
+
entry: string,
|
|
267
|
+
work: (opened: Opened) => T | Promise<T>,
|
|
268
|
+
): Promise<T> {
|
|
269
|
+
return this.#enter(entry, entry, work)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// An entry, named `name` to `around` and `entry` to `report`: opened, its
|
|
273
|
+
// sockets woken, and a failure reported, with the alarm a second on to
|
|
274
|
+
// take up what the object owes.
|
|
275
|
+
#enter<T>(
|
|
276
|
+
name: string,
|
|
277
|
+
entry: string,
|
|
278
|
+
work: (opened: Opened) => T | Promise<T>,
|
|
279
|
+
): Promise<T> {
|
|
280
|
+
return this.#around(name, async () => {
|
|
281
|
+
try {
|
|
282
|
+
return await work(await this.open())
|
|
283
|
+
} catch (error) {
|
|
284
|
+
if (!stopped(error)) {
|
|
285
|
+
await this.spec.report(error, entry)
|
|
286
|
+
await this.#later(1000)
|
|
287
|
+
}
|
|
288
|
+
throw error
|
|
289
|
+
}
|
|
290
|
+
})
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** The runtime's clock going off: one slice of every work. */
|
|
294
|
+
alarm(): Promise<void> {
|
|
295
|
+
return this.#around('alarm', async () => {
|
|
296
|
+
let wait = this.ceiling.stopped(true)
|
|
297
|
+
if (wait) return await this.#later(wait)
|
|
298
|
+
try {
|
|
299
|
+
await this.open()
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (stopped(error)) return await this.#later(error.retry.after)
|
|
302
|
+
await this.spec.report(error, 'alarm')
|
|
303
|
+
return await this.#later(this.spec.alarm?.retry ?? 60_000)
|
|
304
|
+
}
|
|
305
|
+
await this.clock.ring()
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// The alarm `ms` from now, or sooner where it stands so.
|
|
310
|
+
#later(ms: number): Promise<void> {
|
|
311
|
+
let now = this.spec.alarm?.now?.() ?? Date.now()
|
|
312
|
+
return this.clock.arm(now + ms).catch((e) => {
|
|
313
|
+
this.spec.report(e, 'arm')
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// What a socket's entry finds: what the object opened, if it takes the
|
|
318
|
+
// socket now.
|
|
319
|
+
async #admitted(ws: Wire): Promise<Opened | undefined> {
|
|
320
|
+
let opened = await this.open()
|
|
321
|
+
if (!opened.admit || await opened.admit(ws)) return opened
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// The sockets serving `ws` now.
|
|
325
|
+
#sockets(opened: Opened, ws: Wire) {
|
|
326
|
+
return opened.live ? opened.live(ws) : this.#live
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** A frame from one of the object's sockets. A clock probe is answered
|
|
330
|
+
* once the frame is admitted, before the sockets are asked; each frame
|
|
331
|
+
* they read runs inside `around`, as `ws <kind>`. */
|
|
332
|
+
webSocketMessage(ws: Wire, data: string | ArrayBuffer): void | Promise<void> {
|
|
333
|
+
let received = Date.now()
|
|
334
|
+
return this.#around('socket', async () => {
|
|
335
|
+
try {
|
|
336
|
+
let opened = await this.#admitted(ws)
|
|
337
|
+
if (!opened) return
|
|
338
|
+
let { probed } = await import('./sockets.js')
|
|
339
|
+
if (probed(ws, data, received)) return
|
|
340
|
+
let take = async () => {
|
|
341
|
+
let live = await this.#sockets(opened, ws)
|
|
342
|
+
if (!live) return hangUp(ws)
|
|
343
|
+
await live.message(
|
|
344
|
+
ws,
|
|
345
|
+
data,
|
|
346
|
+
(kind, work) =>
|
|
347
|
+
this.#around(`ws ${kind}`, async () => await work()),
|
|
348
|
+
)
|
|
349
|
+
return true
|
|
350
|
+
}
|
|
351
|
+
await (opened.frame ? opened.frame(ws, data, take) : take())
|
|
352
|
+
} catch (error) {
|
|
353
|
+
await this.spec.report(error, 'webSocketMessage')
|
|
354
|
+
}
|
|
355
|
+
})
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** One of its sockets went away. */
|
|
359
|
+
webSocketClose(ws: Wire): void | Promise<void> {
|
|
360
|
+
return this.#around('socket close', async () => {
|
|
361
|
+
try {
|
|
362
|
+
let opened = await this.#admitted(ws)
|
|
363
|
+
if (!opened) return
|
|
364
|
+
await (await this.#sockets(opened, ws))?.close(ws)
|
|
365
|
+
await opened.closed?.(ws)
|
|
366
|
+
} catch (error) {
|
|
367
|
+
await this.spec.report(error, 'webSocketClose')
|
|
368
|
+
}
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** One of its sockets failed, which is it going away. */
|
|
373
|
+
webSocketError(ws: Wire, _error: unknown): void | Promise<void> {
|
|
374
|
+
return this.webSocketClose(ws)
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// A socket the object will not serve, closed for the page to open another:
|
|
379
|
+
// it was not taken.
|
|
380
|
+
let hangUp = (ws: Wire) => {
|
|
381
|
+
try {
|
|
382
|
+
ws.close?.(1012, 'refused')
|
|
383
|
+
} catch { /* already gone */ }
|
|
384
|
+
return false
|
|
385
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/durable-object",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/durable-object",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./sql": {
|
|
12
|
+
"types": "./_dist/sql.d.ts",
|
|
13
|
+
"default": "./sql.js"
|
|
14
|
+
},
|
|
15
|
+
"./store": {
|
|
16
|
+
"types": "./_dist/store.d.ts",
|
|
17
|
+
"default": "./store.js"
|
|
18
|
+
},
|
|
19
|
+
"./resets": {
|
|
20
|
+
"types": "./_dist/resets.d.ts",
|
|
21
|
+
"default": "./resets.js"
|
|
22
|
+
},
|
|
23
|
+
"./ceiling": {
|
|
24
|
+
"types": "./_dist/ceiling.d.ts",
|
|
25
|
+
"default": "./ceiling.js"
|
|
26
|
+
},
|
|
27
|
+
"./keys": {
|
|
28
|
+
"types": "./_dist/keys.d.ts",
|
|
29
|
+
"default": "./keys.js"
|
|
30
|
+
},
|
|
31
|
+
"./alarm": {
|
|
32
|
+
"types": "./_dist/alarm.d.ts",
|
|
33
|
+
"default": "./alarm.js"
|
|
34
|
+
},
|
|
35
|
+
"./object": {
|
|
36
|
+
"types": "./_dist/object.d.ts",
|
|
37
|
+
"default": "./object.js"
|
|
38
|
+
},
|
|
39
|
+
"./door": {
|
|
40
|
+
"types": "./_dist/door.d.ts",
|
|
41
|
+
"default": "./door.js"
|
|
42
|
+
},
|
|
43
|
+
"./compose": {
|
|
44
|
+
"types": "./_dist/compose.d.ts",
|
|
45
|
+
"default": "./compose.js"
|
|
46
|
+
},
|
|
47
|
+
"./rewind": {
|
|
48
|
+
"types": "./_dist/rewind.d.ts",
|
|
49
|
+
"default": "./rewind.js"
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"license": "Apache-2.0",
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@yaks/api": "0.2.36",
|
|
55
|
+
"@yaks/blob": "0.2.36",
|
|
56
|
+
"@yaks/effects": "0.2.36",
|
|
57
|
+
"@yaks/fp": "0.2.36",
|
|
58
|
+
"@yaks/graph": "0.2.36",
|
|
59
|
+
"@yaks/host": "0.2.36",
|
|
60
|
+
"@yaks/secrets": "0.2.36",
|
|
61
|
+
"@yaks/sql": "0.2.36",
|
|
62
|
+
"@yaks/sqlite": "0.2.36",
|
|
63
|
+
"@yaks/trace": "0.2.36",
|
|
64
|
+
"@yaks/vocab": "0.2.36",
|
|
65
|
+
"@yaks/wake": "0.2.36",
|
|
66
|
+
"fflate": "0.8.3"
|
|
67
|
+
},
|
|
68
|
+
"description": "Provides graph storage on a Cloudflare Durable Object's embedded SQLite and connects hibernatable WebSockets to query subscriptions.",
|
|
69
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
70
|
+
}
|
package/profile.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// A bounded, in-memory summary of the rows a Durable Object's SQL statements
|
|
2
|
+
// cost. The adapter observes cursor counters; the host names an invocation and
|
|
3
|
+
// decides when to flush. No statement values or rows leave this module.
|
|
4
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
let empty = ()=>({
|
|
6
|
+
calls: 0,
|
|
7
|
+
rowsRead: 0,
|
|
8
|
+
rowsWritten: 0
|
|
9
|
+
});
|
|
10
|
+
export let group = ()=>({
|
|
11
|
+
total: empty(),
|
|
12
|
+
other: empty(),
|
|
13
|
+
shapes: new Map()
|
|
14
|
+
});
|
|
15
|
+
let add = (to, from)=>{
|
|
16
|
+
to.calls += from.calls;
|
|
17
|
+
to.rowsRead += from.rowsRead;
|
|
18
|
+
to.rowsWritten += from.rowsWritten;
|
|
19
|
+
};
|
|
20
|
+
let weight = (c)=>c.rowsRead + c.rowsWritten;
|
|
21
|
+
// Two independent 32-bit streams keep the key small without retaining a long
|
|
22
|
+
// SQL shape in memory. The readable prefix is capped separately.
|
|
23
|
+
export let id = (s)=>{
|
|
24
|
+
let a = 2166136261;
|
|
25
|
+
let b = 2246822519;
|
|
26
|
+
for(let i = 0; i < s.length; i++){
|
|
27
|
+
let ch = s.charCodeAt(i);
|
|
28
|
+
a = Math.imul(a ^ ch, 16777619);
|
|
29
|
+
b = Math.imul(b ^ ch, 3266489917);
|
|
30
|
+
}
|
|
31
|
+
return `${s.length.toString(36)}-${(a >>> 0).toString(36)}-${(b >>> 0).toString(36)}`;
|
|
32
|
+
};
|
|
33
|
+
export let tally = (into, sample, limit)=>{
|
|
34
|
+
let cost = {
|
|
35
|
+
calls: 1,
|
|
36
|
+
rowsRead: sample.rowsRead,
|
|
37
|
+
rowsWritten: sample.rowsWritten
|
|
38
|
+
};
|
|
39
|
+
add(into.total, cost);
|
|
40
|
+
let key = id(sample.shape);
|
|
41
|
+
let entry = into.shapes.get(key);
|
|
42
|
+
if (!entry) {
|
|
43
|
+
if (into.shapes.size == limit) {
|
|
44
|
+
let rare = [
|
|
45
|
+
...into.shapes.values()
|
|
46
|
+
].reduce((a, b)=>weight(a) <= weight(b) ? a : b);
|
|
47
|
+
if (weight(cost) > weight(rare)) {
|
|
48
|
+
into.shapes.delete(rare.id);
|
|
49
|
+
add(into.other, rare);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (into.shapes.size < limit) {
|
|
53
|
+
entry = {
|
|
54
|
+
...empty(),
|
|
55
|
+
id: key,
|
|
56
|
+
shape: sample.shape.slice(0, 240)
|
|
57
|
+
};
|
|
58
|
+
into.shapes.set(key, entry);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
add(entry ?? into.other, cost);
|
|
62
|
+
};
|
|
63
|
+
export let finish = (g, reads, writes)=>{
|
|
64
|
+
let ranked = [
|
|
65
|
+
...g.shapes.values()
|
|
66
|
+
].sort((a, b)=>b.rowsRead - a.rowsRead);
|
|
67
|
+
let top = ranked.slice(0, reads);
|
|
68
|
+
let written = [
|
|
69
|
+
...ranked
|
|
70
|
+
].sort((a, b)=>b.rowsWritten - a.rowsWritten);
|
|
71
|
+
for (let entry of written){
|
|
72
|
+
if (top.length == reads + writes) break;
|
|
73
|
+
if (!top.includes(entry)) top.push(entry);
|
|
74
|
+
}
|
|
75
|
+
for (let entry of ranked)if (!top.includes(entry)) add(g.other, entry);
|
|
76
|
+
return {
|
|
77
|
+
total: g.total,
|
|
78
|
+
statements: top,
|
|
79
|
+
other: g.other
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
/** Report the first nonempty window at once, then at most once per minute
|
|
83
|
+
* while this object stays awake. Shape and invocation buckets have fixed caps;
|
|
84
|
+
* `other` sums anything beyond them. `run` keeps a label across awaits, even
|
|
85
|
+
* when another request runs on the same object before the first resumes. */ export let profile = (emit, now = Date.now)=>{
|
|
86
|
+
let here = new AsyncLocalStorage();
|
|
87
|
+
let since = null;
|
|
88
|
+
let total = group();
|
|
89
|
+
let operations = new Map();
|
|
90
|
+
let reported = false;
|
|
91
|
+
let first = true;
|
|
92
|
+
let observe = (sample)=>{
|
|
93
|
+
since ??= now();
|
|
94
|
+
tally(total, sample, 64);
|
|
95
|
+
let kind = here.getStore() ?? 'unattributed';
|
|
96
|
+
if (!operations.has(kind) && operations.size >= 15) kind = 'other';
|
|
97
|
+
let one = operations.get(kind);
|
|
98
|
+
if (!one) operations.set(kind, one = group());
|
|
99
|
+
tally(one, sample, 24);
|
|
100
|
+
};
|
|
101
|
+
let flush = (at = now())=>{
|
|
102
|
+
if (since == null || !first && at - since < 60_000) return;
|
|
103
|
+
let all = finish(total, 10, 2);
|
|
104
|
+
let byKind = [
|
|
105
|
+
...operations
|
|
106
|
+
].map(([kind, g])=>({
|
|
107
|
+
kind,
|
|
108
|
+
...finish(g, 5, 2)
|
|
109
|
+
}));
|
|
110
|
+
try {
|
|
111
|
+
emit({
|
|
112
|
+
ms: at - since,
|
|
113
|
+
...all,
|
|
114
|
+
operations: byKind
|
|
115
|
+
});
|
|
116
|
+
} catch (e) {
|
|
117
|
+
if (!reported) console.error('row profile report failed', e);
|
|
118
|
+
reported = true;
|
|
119
|
+
}
|
|
120
|
+
since = null;
|
|
121
|
+
first = false;
|
|
122
|
+
total = group();
|
|
123
|
+
operations = new Map();
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
observe,
|
|
127
|
+
run: (kind, work)=>here.run(kind, work),
|
|
128
|
+
flush
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=profile.js.map
|
package/profile.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.js","sources":["./profile.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,+EAA+E;AAC/E,wEAAwE;AAExE,SAAS,iBAAiB,QAAQ,mBAAkB;AAgCpD,IAAI,QAAQ,IAAY,CAAC;IAAE,OAAO;IAAG,UAAU;IAAG,aAAa;EAAE,CAAC;AAClE,OAAO,IAAI,QAAQ,IAAa,CAAC;IAC/B,OAAO;IACP,OAAO;IACP,QAAQ,IAAI;EACd,CAAC,EAAC;AACF,IAAI,MAAM,CAAC,IAAU;EACnB,GAAG,KAAK,IAAI,KAAK,KAAK;EACtB,GAAG,QAAQ,IAAI,KAAK,QAAQ;EAC5B,GAAG,WAAW,IAAI,KAAK,WAAW;AACpC;AACA,IAAI,SAAS,CAAC,IAAY,EAAE,QAAQ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