@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/mod.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/durable-object — a yaks graph inside a Cloudflare **Durable Object**:
|
|
3
|
+
* its embedded SQLite is the storage, and its hibernatable WebSockets are the
|
|
4
|
+
* live-sync fan-out.
|
|
5
|
+
*
|
|
6
|
+
* A Durable Object is a single-threaded, strongly-consistent home for one
|
|
7
|
+
* graph. `ctx.storage.sql` is a synchronous SQLite engine, so
|
|
8
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}'s `apply()` stays
|
|
9
|
+
* synchronous here; `ctx.storage.transactionSync` is the transaction it runs
|
|
10
|
+
* its in-transaction phases inside.
|
|
11
|
+
*
|
|
12
|
+
* ## Two halves
|
|
13
|
+
* {@link storage} implements the `Storage` interface — in one line of
|
|
14
|
+
* composition:
|
|
15
|
+
* {@link driver} turns the object's SQLite into a
|
|
16
|
+
* {@link https://jsr.io/@yaks/sql | @yaks/sql} `Driver`, and @yaks/sqlite over it
|
|
17
|
+
* owns the schema, the compiled reads, the patches and the cascading deletes.
|
|
18
|
+
* No SQL is written twice.
|
|
19
|
+
*
|
|
20
|
+
* {@link sockets} is the plumbing between the object's WebSockets and
|
|
21
|
+
* {@link https://jsr.io/@yaks/api | @yaks/api}'s subscriptions — accept a
|
|
22
|
+
* socket for hibernation, pass its frames to the registry, and rebuild the
|
|
23
|
+
* subscriptions of a woken object from what its sockets hold. What a
|
|
24
|
+
* subscription means lives in @yaks/api; only the Cloudflare-specific plumbing
|
|
25
|
+
* is here.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { graph } from '@yaks/graph'
|
|
29
|
+
* import { api, subscriptions } from '@yaks/api'
|
|
30
|
+
* import { sockets, storage } from '@yaks/durable-object'
|
|
31
|
+
*
|
|
32
|
+
* // let store = storage(ctx.storage, vocab)
|
|
33
|
+
* // store.install()
|
|
34
|
+
* // let g = graph({ storage: store, vocab })
|
|
35
|
+
* // let subs = subscriptions(g)
|
|
36
|
+
* // let live = sockets(subs, ctx)
|
|
37
|
+
* // let handler = api({ graph: g, subs })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* {@link Durable} is the whole object as a class, the one place a class is
|
|
41
|
+
* the platform's requirement: a spec opens its graph, door and sockets, and
|
|
42
|
+
* {@link clock} works everything it does on its own time from its one alarm.
|
|
43
|
+
*
|
|
44
|
+
* And from the caller's side, the door to an object: {@link stubOf} takes a
|
|
45
|
+
* namespace's stub by name, placed where a hint says; {@link doorOf} builds
|
|
46
|
+
* every request through it from scratch; and {@link resending} sends a call
|
|
47
|
+
* again when the runtime reset the object or lost the call, while that is
|
|
48
|
+
* safe.
|
|
49
|
+
*
|
|
50
|
+
* @module
|
|
51
|
+
*/ export { clearStorage, driver, freely, prohibited, reserved } from "./sql.js";
|
|
52
|
+
export { queryJson, queryMemory, QueryPage } from "./query-memory.js";
|
|
53
|
+
export { align, arrived, chunks, copied, ending, first, overwritten, page, plan, planFor, raised, reread, stale, tallied, tips } from "./copy.js";
|
|
54
|
+
export { storage } from "./store.js";
|
|
55
|
+
export { keys } from "./keys.js";
|
|
56
|
+
export { job, jobs } from "./job.js";
|
|
57
|
+
export { BACKSTOP, clock, RETRY, SLICE } from "./alarm.js";
|
|
58
|
+
export { Durable } from "./object.js";
|
|
59
|
+
export { composed, lent, planned, ROLES } from "./compose.js";
|
|
60
|
+
export { doorOf, finite, IDEMPOTENCY, received, resending, reset, stubOf, thrown, WHOLE } from "./door.js";
|
|
61
|
+
export { backoff, evicted, jittered, killed, LOOK, moved, opaque, replaced, retried, starved, vacating } from "./resets.js";
|
|
62
|
+
export { profile } from "./profile.js";
|
|
63
|
+
export { bounded, ceiling, FAR, Runaway, SCAN, Scan, Stopped, stopped, WINDOWS } from "./ceiling.js";
|
|
64
|
+
export { socketRenewals, sockets } from "./sockets.js";
|
|
65
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkDC,GAED,SACE,YAAY,EACZ,MAAM,EAGN,MAAM,EAGN,UAAU,EACV,QAAQ,mBAIO;AACjB,SAAS,SAAS,EAAE,WAAW,EAAE,SAAS,4BAA2B;AACrE,SACE,KAAK,EACL,OAAO,EAGP,MAAM,EAEN,MAAM,EAEN,MAAM,EACN,KAAK,EACL,WAAW,EACX,IAAI,EAEJ,IAAI,EACJ,OAAO,EACP,MAAM,EACN,MAAM,EACN,KAAK,EACL,OAAO,EACP,IAAI,oBACY;AAClB,SAAS,OAAO,qBAAgC;AAChD,SAAoB,IAAI,oBAAmB;AAC3C,SAAS,GAAG,EAAa,IAAI,mBAAkB;AAC/C,SAEE,QAAQ,EAER,KAAK,EACL,KAAK,EACL,KAAK,qBAEY;AACnB,SAAS,OAAO,sBAAyD;AACzE,SAEE,QAAQ,EAGR,IAAI,EACJ,OAAO,EACP,KAAK,uBACc;AACrB,SAGE,MAAM,EACN,MAAM,EACN,WAAW,EAEX,QAAQ,EAER,SAAS,EACT,KAAK,EAGL,MAAM,EACN,MAAM,EACN,KAAK,oBACW;AAClB,SACE,OAAO,EACP,OAAO,EACP,QAAQ,EACR,MAAM,EACN,IAAI,EACJ,KAAK,EACL,MAAM,EAEN,QAAQ,EACR,OAAO,EACP,OAAO,EACP,QAAQ,sBACU;AACpB,SAAS,OAAO,uBAAoC;AACpD,SACE,OAAO,EAEP,OAAO,EACP,GAAG,EAGH,OAAO,EACP,IAAI,EACJ,IAAI,EAEJ,OAAO,EACP,OAAO,EAGP,OAAO,uBACY;AACrB,SAEE,cAAc,EAEd,OAAO,uBAEY"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/durable-object — a yaks graph inside a Cloudflare **Durable Object**:
|
|
3
|
+
* its embedded SQLite is the storage, and its hibernatable WebSockets are the
|
|
4
|
+
* live-sync fan-out.
|
|
5
|
+
*
|
|
6
|
+
* A Durable Object is a single-threaded, strongly-consistent home for one
|
|
7
|
+
* graph. `ctx.storage.sql` is a synchronous SQLite engine, so
|
|
8
|
+
* {@link https://jsr.io/@yaks/graph | @yaks/graph}'s `apply()` stays
|
|
9
|
+
* synchronous here; `ctx.storage.transactionSync` is the transaction it runs
|
|
10
|
+
* its in-transaction phases inside.
|
|
11
|
+
*
|
|
12
|
+
* ## Two halves
|
|
13
|
+
* {@link storage} implements the `Storage` interface — in one line of
|
|
14
|
+
* composition:
|
|
15
|
+
* {@link driver} turns the object's SQLite into a
|
|
16
|
+
* {@link https://jsr.io/@yaks/sql | @yaks/sql} `Driver`, and @yaks/sqlite over it
|
|
17
|
+
* owns the schema, the compiled reads, the patches and the cascading deletes.
|
|
18
|
+
* No SQL is written twice.
|
|
19
|
+
*
|
|
20
|
+
* {@link sockets} is the plumbing between the object's WebSockets and
|
|
21
|
+
* {@link https://jsr.io/@yaks/api | @yaks/api}'s subscriptions — accept a
|
|
22
|
+
* socket for hibernation, pass its frames to the registry, and rebuild the
|
|
23
|
+
* subscriptions of a woken object from what its sockets hold. What a
|
|
24
|
+
* subscription means lives in @yaks/api; only the Cloudflare-specific plumbing
|
|
25
|
+
* is here.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { graph } from '@yaks/graph'
|
|
29
|
+
* import { api, subscriptions } from '@yaks/api'
|
|
30
|
+
* import { sockets, storage } from '@yaks/durable-object'
|
|
31
|
+
*
|
|
32
|
+
* // let store = storage(ctx.storage, vocab)
|
|
33
|
+
* // store.install()
|
|
34
|
+
* // let g = graph({ storage: store, vocab })
|
|
35
|
+
* // let subs = subscriptions(g)
|
|
36
|
+
* // let live = sockets(subs, ctx)
|
|
37
|
+
* // let handler = api({ graph: g, subs })
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* {@link Durable} is the whole object as a class, the one place a class is
|
|
41
|
+
* the platform's requirement: a spec opens its graph, door and sockets, and
|
|
42
|
+
* {@link clock} works everything it does on its own time from its one alarm.
|
|
43
|
+
*
|
|
44
|
+
* And from the caller's side, the door to an object: {@link stubOf} takes a
|
|
45
|
+
* namespace's stub by name, placed where a hint says; {@link doorOf} builds
|
|
46
|
+
* every request through it from scratch; and {@link resending} sends a call
|
|
47
|
+
* again when the runtime reset the object or lost the call, while that is
|
|
48
|
+
* safe.
|
|
49
|
+
*
|
|
50
|
+
* @module
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
export {
|
|
54
|
+
clearStorage,
|
|
55
|
+
driver,
|
|
56
|
+
type DurableSql,
|
|
57
|
+
type DurableStorage,
|
|
58
|
+
freely,
|
|
59
|
+
type Measure,
|
|
60
|
+
type Observe,
|
|
61
|
+
prohibited,
|
|
62
|
+
reserved,
|
|
63
|
+
type Sample,
|
|
64
|
+
type SqlCursor,
|
|
65
|
+
type SqlValue,
|
|
66
|
+
} from './sql.js'
|
|
67
|
+
export { queryJson, queryMemory, QueryPage } from './query-memory.js'
|
|
68
|
+
export {
|
|
69
|
+
align,
|
|
70
|
+
arrived,
|
|
71
|
+
type Cell,
|
|
72
|
+
type Chunk,
|
|
73
|
+
chunks,
|
|
74
|
+
type Copied,
|
|
75
|
+
copied,
|
|
76
|
+
type Cursor,
|
|
77
|
+
ending,
|
|
78
|
+
first,
|
|
79
|
+
overwritten,
|
|
80
|
+
page,
|
|
81
|
+
type Plan,
|
|
82
|
+
plan,
|
|
83
|
+
planFor,
|
|
84
|
+
raised,
|
|
85
|
+
reread,
|
|
86
|
+
stale,
|
|
87
|
+
tallied,
|
|
88
|
+
tips,
|
|
89
|
+
} from './copy.js'
|
|
90
|
+
export { storage, type Store } from './store.js'
|
|
91
|
+
export { type Keys, keys } from './keys.js'
|
|
92
|
+
export { job, type Jobs, jobs } from './job.js'
|
|
93
|
+
export {
|
|
94
|
+
type Alarms,
|
|
95
|
+
BACKSTOP,
|
|
96
|
+
type Clock,
|
|
97
|
+
clock,
|
|
98
|
+
RETRY,
|
|
99
|
+
SLICE,
|
|
100
|
+
type Work,
|
|
101
|
+
} from './alarm.js'
|
|
102
|
+
export { Durable, type Opened, type Spec, type State } from './object.js'
|
|
103
|
+
export {
|
|
104
|
+
type Assembling,
|
|
105
|
+
composed,
|
|
106
|
+
type Composing,
|
|
107
|
+
type Kind,
|
|
108
|
+
lent,
|
|
109
|
+
planned,
|
|
110
|
+
ROLES,
|
|
111
|
+
} from './compose.js'
|
|
112
|
+
export {
|
|
113
|
+
type Call,
|
|
114
|
+
type Door,
|
|
115
|
+
doorOf,
|
|
116
|
+
finite,
|
|
117
|
+
IDEMPOTENCY,
|
|
118
|
+
type Namespace,
|
|
119
|
+
received,
|
|
120
|
+
type Resend,
|
|
121
|
+
resending,
|
|
122
|
+
reset,
|
|
123
|
+
type Send,
|
|
124
|
+
type Stub,
|
|
125
|
+
stubOf,
|
|
126
|
+
thrown,
|
|
127
|
+
WHOLE,
|
|
128
|
+
} from './door.js'
|
|
129
|
+
export {
|
|
130
|
+
backoff,
|
|
131
|
+
evicted,
|
|
132
|
+
jittered,
|
|
133
|
+
killed,
|
|
134
|
+
LOOK,
|
|
135
|
+
moved,
|
|
136
|
+
opaque,
|
|
137
|
+
type Own,
|
|
138
|
+
replaced,
|
|
139
|
+
retried,
|
|
140
|
+
starved,
|
|
141
|
+
vacating,
|
|
142
|
+
} from './resets.js'
|
|
143
|
+
export { profile, type Summary } from './profile.js'
|
|
144
|
+
export {
|
|
145
|
+
bounded,
|
|
146
|
+
type Ceiling,
|
|
147
|
+
ceiling,
|
|
148
|
+
FAR,
|
|
149
|
+
type Kv,
|
|
150
|
+
type Report,
|
|
151
|
+
Runaway,
|
|
152
|
+
SCAN,
|
|
153
|
+
Scan,
|
|
154
|
+
type Spent,
|
|
155
|
+
Stopped,
|
|
156
|
+
stopped,
|
|
157
|
+
type Told,
|
|
158
|
+
type Window,
|
|
159
|
+
WINDOWS,
|
|
160
|
+
} from './ceiling.js'
|
|
161
|
+
export {
|
|
162
|
+
type Hibernation,
|
|
163
|
+
socketRenewals,
|
|
164
|
+
type Sockets,
|
|
165
|
+
sockets,
|
|
166
|
+
type Wire,
|
|
167
|
+
} from './sockets.js'
|
package/object.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
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
|
+
*/ import { refuse } from "@yaks/api/refuse";
|
|
46
|
+
import { clock } from "./alarm.js";
|
|
47
|
+
import { bounded, stopped } from "./ceiling.js";
|
|
48
|
+
import { keys } from "./keys.js";
|
|
49
|
+
// Where the deploy an object last ran is kept, in its synchronous kv.
|
|
50
|
+
let VERSION = '@yaks/durable-object:version';
|
|
51
|
+
/** One Durable Object, of the kind its spec makes. */ export class Durable {
|
|
52
|
+
/** The object's own state: its storage and its sockets. */ ctx;
|
|
53
|
+
#spec;
|
|
54
|
+
#ceiling;
|
|
55
|
+
#clock;
|
|
56
|
+
#keys;
|
|
57
|
+
#opened;
|
|
58
|
+
#held;
|
|
59
|
+
#live;
|
|
60
|
+
#restarted = false;
|
|
61
|
+
/** Without a `spec`, the object asks its `specify()` for one. */ constructor(ctx, spec){
|
|
62
|
+
this.ctx = ctx;
|
|
63
|
+
this.#spec = spec;
|
|
64
|
+
}
|
|
65
|
+
/** What makes this kind of object, asked once, at the object's first
|
|
66
|
+
* entry or its ceiling: a subclass whose spec names its own members
|
|
67
|
+
* answers it here. */ specify() {
|
|
68
|
+
throw new Error(`${this.constructor.name} has no spec`);
|
|
69
|
+
}
|
|
70
|
+
/** What makes this object. */ get spec() {
|
|
71
|
+
return this.#spec ??= this.specify();
|
|
72
|
+
}
|
|
73
|
+
/** The object's row ceiling, made at its first statement through the
|
|
74
|
+
* object. A subclass that opens a driver of its own before (`open` is the
|
|
75
|
+
* object's) names its ceiling first, with `this.ceiling` at the end of its
|
|
76
|
+
* constructor: every driver counts against the ceiling made first. */ get ceiling() {
|
|
77
|
+
let named = this.spec.ceiling;
|
|
78
|
+
return this.#ceiling ??= named ? bounded(this.ctx.storage, {
|
|
79
|
+
refuse: true,
|
|
80
|
+
...named
|
|
81
|
+
}) : bounded(this.ctx.storage);
|
|
82
|
+
}
|
|
83
|
+
/** The object's async key-value storage, each call counted against its
|
|
84
|
+
* ceiling as a statement is (`./keys.ts`). */ get keys() {
|
|
85
|
+
this.ceiling;
|
|
86
|
+
// Every SQLite-backed object's storage has them; `State` leaves them to
|
|
87
|
+
// the stand-ins of objects that keep none.
|
|
88
|
+
return this.#keys ??= keys(this.ctx.storage);
|
|
89
|
+
}
|
|
90
|
+
/** The object's alarm: a write that owes work arms it. */ get clock() {
|
|
91
|
+
return this.#clock ??= clock(this.ctx.storage, ()=>this.#held?.works ?? {}, {
|
|
92
|
+
wait: ()=>this.ceiling.stopped(true),
|
|
93
|
+
report: (e, work)=>this.spec.report(e, `alarm ${work}`),
|
|
94
|
+
...this.spec.alarm
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/** The object's sockets, once it has opened subscriptions. */ get live() {
|
|
98
|
+
return this.#live;
|
|
99
|
+
}
|
|
100
|
+
/** What the object holds, opened once an instance; its sockets woken. */ async open() {
|
|
101
|
+
let open = this.spec.open ?? (()=>Promise.resolve({}));
|
|
102
|
+
this.#opened ??= open(this).then(async (opened)=>{
|
|
103
|
+
this.ceiling;
|
|
104
|
+
// The sockets' plumbing is loaded only by an object that has some.
|
|
105
|
+
if (opened.subs) {
|
|
106
|
+
let { sockets } = await import("./sockets.js");
|
|
107
|
+
this.#live = sockets(opened.subs, this.ctx);
|
|
108
|
+
}
|
|
109
|
+
return this.#held = opened;
|
|
110
|
+
}, (e)=>{
|
|
111
|
+
this.#opened = undefined;
|
|
112
|
+
throw e;
|
|
113
|
+
});
|
|
114
|
+
let opened = await this.#opened;
|
|
115
|
+
await this.#live?.wake();
|
|
116
|
+
await this.#restart();
|
|
117
|
+
return opened;
|
|
118
|
+
}
|
|
119
|
+
// The first entry on a new deploy arms the alarm, once an instance.
|
|
120
|
+
async #restart() {
|
|
121
|
+
let { version } = this.spec, kv = this.ctx.storage.kv;
|
|
122
|
+
if (this.#restarted || !version || !kv) return;
|
|
123
|
+
this.#restarted = true;
|
|
124
|
+
if (kv.get(VERSION) == version) return;
|
|
125
|
+
kv.put(VERSION, version);
|
|
126
|
+
await this.clock.arm(this.spec.alarm?.now?.() ?? Date.now());
|
|
127
|
+
}
|
|
128
|
+
#around(entry, work) {
|
|
129
|
+
return this.spec.around ? this.spec.around(entry, work) : work();
|
|
130
|
+
}
|
|
131
|
+
/** A socket for this object's subscriptions: the door's to call. */ accept(...args) {
|
|
132
|
+
if (!this.#live) throw new Error('this object serves no subscriptions');
|
|
133
|
+
return this.#live.accept(...args);
|
|
134
|
+
}
|
|
135
|
+
/** A request: the door's answer, `Stopped`'s with how long to wait, or a
|
|
136
|
+
* 503 for a failure the door did not answer, which is reported. */ async fetch(request) {
|
|
137
|
+
let { pathname } = new URL(request.url);
|
|
138
|
+
return await this.#enter(`${request.method} ${pathname}`, 'fetch', (opened)=>opened.serve ? opened.serve(request) : Response.json({
|
|
139
|
+
error: 'not found'
|
|
140
|
+
}, {
|
|
141
|
+
status: 404
|
|
142
|
+
})).catch((error)=>stopped(error) ? refuse(error) : Response.json({
|
|
143
|
+
error: 'temporarily unavailable'
|
|
144
|
+
}, {
|
|
145
|
+
status: 503
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
/** An entry of the subclass's own, such as an RPC method: `work`, given
|
|
149
|
+
* what the object opened, inside `around` as `entry`. What it throws is
|
|
150
|
+
* thrown on to its caller, and a failure other than the ceiling's
|
|
151
|
+
* `Stopped` is reported by `entry` first, as a request's is. */ enter(entry, work) {
|
|
152
|
+
return this.#enter(entry, entry, work);
|
|
153
|
+
}
|
|
154
|
+
// An entry, named `name` to `around` and `entry` to `report`: opened, its
|
|
155
|
+
// sockets woken, and a failure reported, with the alarm a second on to
|
|
156
|
+
// take up what the object owes.
|
|
157
|
+
#enter(name, entry, work) {
|
|
158
|
+
return this.#around(name, async ()=>{
|
|
159
|
+
try {
|
|
160
|
+
return await work(await this.open());
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (!stopped(error)) {
|
|
163
|
+
await this.spec.report(error, entry);
|
|
164
|
+
await this.#later(1000);
|
|
165
|
+
}
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/** The runtime's clock going off: one slice of every work. */ alarm() {
|
|
171
|
+
return this.#around('alarm', async ()=>{
|
|
172
|
+
let wait = this.ceiling.stopped(true);
|
|
173
|
+
if (wait) return await this.#later(wait);
|
|
174
|
+
try {
|
|
175
|
+
await this.open();
|
|
176
|
+
} catch (error) {
|
|
177
|
+
if (stopped(error)) return await this.#later(error.retry.after);
|
|
178
|
+
await this.spec.report(error, 'alarm');
|
|
179
|
+
return await this.#later(this.spec.alarm?.retry ?? 60_000);
|
|
180
|
+
}
|
|
181
|
+
await this.clock.ring();
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
// The alarm `ms` from now, or sooner where it stands so.
|
|
185
|
+
#later(ms) {
|
|
186
|
+
let now = this.spec.alarm?.now?.() ?? Date.now();
|
|
187
|
+
return this.clock.arm(now + ms).catch((e)=>{
|
|
188
|
+
this.spec.report(e, 'arm');
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
// What a socket's entry finds: what the object opened, if it takes the
|
|
192
|
+
// socket now.
|
|
193
|
+
async #admitted(ws) {
|
|
194
|
+
let opened = await this.open();
|
|
195
|
+
if (!opened.admit || await opened.admit(ws)) return opened;
|
|
196
|
+
}
|
|
197
|
+
// The sockets serving `ws` now.
|
|
198
|
+
#sockets(opened, ws) {
|
|
199
|
+
return opened.live ? opened.live(ws) : this.#live;
|
|
200
|
+
}
|
|
201
|
+
/** A frame from one of the object's sockets. A clock probe is answered
|
|
202
|
+
* once the frame is admitted, before the sockets are asked; each frame
|
|
203
|
+
* they read runs inside `around`, as `ws <kind>`. */ webSocketMessage(ws, data) {
|
|
204
|
+
let received = Date.now();
|
|
205
|
+
return this.#around('socket', async ()=>{
|
|
206
|
+
try {
|
|
207
|
+
let opened = await this.#admitted(ws);
|
|
208
|
+
if (!opened) return;
|
|
209
|
+
let { probed } = await import("./sockets.js");
|
|
210
|
+
if (probed(ws, data, received)) return;
|
|
211
|
+
let take = async ()=>{
|
|
212
|
+
let live = await this.#sockets(opened, ws);
|
|
213
|
+
if (!live) return hangUp(ws);
|
|
214
|
+
await live.message(ws, data, (kind, work)=>this.#around(`ws ${kind}`, async ()=>await work()));
|
|
215
|
+
return true;
|
|
216
|
+
};
|
|
217
|
+
await (opened.frame ? opened.frame(ws, data, take) : take());
|
|
218
|
+
} catch (error) {
|
|
219
|
+
await this.spec.report(error, 'webSocketMessage');
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/** One of its sockets went away. */ webSocketClose(ws) {
|
|
224
|
+
return this.#around('socket close', async ()=>{
|
|
225
|
+
try {
|
|
226
|
+
let opened = await this.#admitted(ws);
|
|
227
|
+
if (!opened) return;
|
|
228
|
+
await (await this.#sockets(opened, ws))?.close(ws);
|
|
229
|
+
await opened.closed?.(ws);
|
|
230
|
+
} catch (error) {
|
|
231
|
+
await this.spec.report(error, 'webSocketClose');
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/** One of its sockets failed, which is it going away. */ webSocketError(ws, _error) {
|
|
236
|
+
return this.webSocketClose(ws);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// A socket the object will not serve, closed for the page to open another:
|
|
240
|
+
// it was not taken.
|
|
241
|
+
let hangUp = (ws)=>{
|
|
242
|
+
try {
|
|
243
|
+
ws.close?.(1012, 'refused');
|
|
244
|
+
} catch {}
|
|
245
|
+
return false;
|
|
246
|
+
};
|
|
247
|
+
//# sourceMappingURL=object.js.map
|
package/object.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.js","sources":["./object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA4CC,GAED,SAAS,MAAM,gCAAqC;AAEpD,SAGE,KAAK,qBAGY;AACnB,SACE,OAAO,EAIP,OAAO,uBACY;AACrB,SAAoB,IAAI,oBAAmB;AAgE3C,sEAAsE;AACtE,IAAI,UAAU;AAEd,oDAAoD,GACpD,OAAO,MAAM;EACX,yDAAyD,GACzD,AAAS,IAAU;EACnB,CAAA,IAAK,CAAO;EACZ,CAAA,OAAQ,CAAU;EAClB,CAAA,KAAM,CAAQ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