durability 3.0.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +400 -25
- package/dist/client.d.ts +94 -0
- package/dist/client.js +445 -0
- package/dist/durability-alarms.d.ts +14 -1
- package/dist/durability-alarms.js +121 -22
- package/dist/durability.d.ts +33 -2
- package/dist/durability.js +126 -63
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +38 -0
- package/dist/experimental.d.ts +8 -0
- package/dist/experimental.js +7 -0
- package/dist/fanout.d.ts +25 -2
- package/dist/fanout.js +273 -93
- package/dist/index.d.ts +8 -7
- package/dist/index.js +3 -2
- package/dist/lifecycle.d.ts +8 -0
- package/dist/lifecycle.js +22 -0
- package/dist/log.d.ts +201 -0
- package/dist/log.js +705 -0
- package/dist/migrations.d.ts +28 -1
- package/dist/migrations.js +100 -0
- package/dist/scheduler.d.ts +35 -20
- package/dist/scheduler.js +112 -90
- package/dist/serialization.d.ts +5 -0
- package/dist/serialization.js +16 -0
- package/dist/socket-protocol.d.ts +95 -0
- package/dist/socket-protocol.js +71 -0
- package/dist/socket.d.ts +92 -0
- package/dist/socket.js +273 -0
- package/dist/storage.d.ts +39 -128
- package/dist/storage.js +64 -68
- package/dist/tracked-source.d.ts +87 -0
- package/dist/tracked-source.js +7 -0
- package/dist/tracker.d.ts +96 -0
- package/dist/tracker.js +171 -0
- package/package.json +19 -5
- package/dist/kv-storage.d.ts +0 -2
- package/dist/kv-storage.js +0 -421
- package/dist/sqlite-storage.d.ts +0 -2
- package/dist/sqlite-storage.js +0 -251
package/dist/client.js
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _Connection_instances, _Connection_config, _Connection_url, _Connection_transforms, _Connection_pending, _Connection_handlers, _Connection_statusHandlers, _Connection_resyncHandlers, _Connection_subscribes, _Connection_acknowledged, _Connection_status, _Connection_ws, _Connection_generation, _Connection_nextId, _Connection_lastSeq, _Connection_attempt, _Connection_retryTimer, _Connection_pingTimer, _Connection_lastReceivedAt, _Connection_send, _Connection_subscribe, _Connection_subscribed, _Connection_flush, _Connection_connect, _Connection_receive, _Connection_settle, _Connection_dispatch, _Connection_invoke, _Connection_disconnected, _Connection_abandon, _Connection_startPing, _Connection_stopTimers, _Connection_setStatus, _DurabilitySocketClient_config, _DurabilitySocketClient_transforms;
|
|
13
|
+
import { reportFailure } from './errors.js';
|
|
14
|
+
import { parseServerFrame, socketProtocolErrorCode, socketProtocolVersion, } from './socket-protocol.js';
|
|
15
|
+
import { exponential, jitter } from './utils.js';
|
|
16
|
+
/** Rejection for calls still waiting when `close()` was called. */
|
|
17
|
+
export class SocketClosedError extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super('Socket connection was closed');
|
|
20
|
+
this.name = 'SocketClosedError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Rejection for calls that were sent but unanswered when the connection
|
|
25
|
+
* dropped. They are never replayed: the server may or may not have run them.
|
|
26
|
+
*/
|
|
27
|
+
export class SocketConnectionLostError extends Error {
|
|
28
|
+
constructor(method) {
|
|
29
|
+
super(`Connection lost while "${method}" was in flight`);
|
|
30
|
+
this.name = 'SocketConnectionLostError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Rejection for calls unanswered within the configured timeout. */
|
|
34
|
+
export class SocketTimeoutError extends Error {
|
|
35
|
+
constructor(method, timeoutMs) {
|
|
36
|
+
super(`"${method}" timed out after ${timeoutMs}ms`);
|
|
37
|
+
this.name = 'SocketTimeoutError';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** The server sent a frame this client cannot interpret; the connection is abandoned. */
|
|
41
|
+
export class SocketProtocolError extends Error {
|
|
42
|
+
constructor(message) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.name = 'SocketProtocolError';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A handler on the server threw; `name` and `message` are the server's. */
|
|
48
|
+
export class SocketRemoteError extends Error {
|
|
49
|
+
constructor(error) {
|
|
50
|
+
super(error.message);
|
|
51
|
+
this.name = error.name;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const pingText = JSON.stringify({ v: socketProtocolVersion, type: 'ping' });
|
|
55
|
+
const pongText = JSON.stringify({ v: socketProtocolVersion, type: 'pong' });
|
|
56
|
+
class Connection {
|
|
57
|
+
constructor(config, id, transforms) {
|
|
58
|
+
_Connection_instances.add(this);
|
|
59
|
+
_Connection_config.set(this, void 0);
|
|
60
|
+
_Connection_url.set(this, void 0);
|
|
61
|
+
_Connection_transforms.set(this, void 0);
|
|
62
|
+
_Connection_pending.set(this, new Map());
|
|
63
|
+
_Connection_handlers.set(this, new Map());
|
|
64
|
+
_Connection_statusHandlers.set(this, new Set());
|
|
65
|
+
_Connection_resyncHandlers.set(this, new Set());
|
|
66
|
+
_Connection_subscribes.set(this, new Map());
|
|
67
|
+
_Connection_acknowledged.set(this, new Set());
|
|
68
|
+
_Connection_status.set(this, 'disconnected');
|
|
69
|
+
_Connection_ws.set(this, void 0);
|
|
70
|
+
_Connection_generation.set(this, 0);
|
|
71
|
+
_Connection_nextId.set(this, 0);
|
|
72
|
+
_Connection_lastSeq.set(this, void 0);
|
|
73
|
+
_Connection_attempt.set(this, 0);
|
|
74
|
+
_Connection_retryTimer.set(this, void 0);
|
|
75
|
+
_Connection_pingTimer.set(this, void 0);
|
|
76
|
+
_Connection_lastReceivedAt.set(this, 0);
|
|
77
|
+
__classPrivateFieldSet(this, _Connection_config, config, "f");
|
|
78
|
+
__classPrivateFieldSet(this, _Connection_url, config.url(id), "f");
|
|
79
|
+
__classPrivateFieldSet(this, _Connection_transforms, transforms, "f");
|
|
80
|
+
}
|
|
81
|
+
get status() {
|
|
82
|
+
return __classPrivateFieldGet(this, _Connection_status, "f");
|
|
83
|
+
}
|
|
84
|
+
call(method, input) {
|
|
85
|
+
const run = (index, currentInput) => {
|
|
86
|
+
const transform = __classPrivateFieldGet(this, _Connection_transforms, "f")[index];
|
|
87
|
+
if (!transform) {
|
|
88
|
+
return __classPrivateFieldGet(this, _Connection_instances, "m", _Connection_send).call(this, method, currentInput);
|
|
89
|
+
}
|
|
90
|
+
return transform({
|
|
91
|
+
method,
|
|
92
|
+
input: currentInput,
|
|
93
|
+
next: (nextInput = currentInput) => run(index + 1, nextInput),
|
|
94
|
+
});
|
|
95
|
+
};
|
|
96
|
+
return run(0, input);
|
|
97
|
+
}
|
|
98
|
+
on(name, handler) {
|
|
99
|
+
let handlers = __classPrivateFieldGet(this, _Connection_handlers, "f").get(name);
|
|
100
|
+
if (!handlers) {
|
|
101
|
+
handlers = new Set();
|
|
102
|
+
__classPrivateFieldGet(this, _Connection_handlers, "f").set(name, handlers);
|
|
103
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_subscribe).call(this);
|
|
104
|
+
}
|
|
105
|
+
handlers.add(handler);
|
|
106
|
+
return () => {
|
|
107
|
+
handlers.delete(handler);
|
|
108
|
+
if (handlers.size === 0 && __classPrivateFieldGet(this, _Connection_handlers, "f").get(name) === handlers) {
|
|
109
|
+
__classPrivateFieldGet(this, _Connection_handlers, "f").delete(name);
|
|
110
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_subscribe).call(this);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
onStatus(handler) {
|
|
115
|
+
__classPrivateFieldGet(this, _Connection_statusHandlers, "f").add(handler);
|
|
116
|
+
handler(__classPrivateFieldGet(this, _Connection_status, "f"));
|
|
117
|
+
return () => {
|
|
118
|
+
__classPrivateFieldGet(this, _Connection_statusHandlers, "f").delete(handler);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
onResync(handler) {
|
|
122
|
+
__classPrivateFieldGet(this, _Connection_resyncHandlers, "f").add(handler);
|
|
123
|
+
return () => {
|
|
124
|
+
__classPrivateFieldGet(this, _Connection_resyncHandlers, "f").delete(handler);
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
close() {
|
|
128
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") === 'closed') {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'closed');
|
|
132
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_stopTimers).call(this);
|
|
133
|
+
__classPrivateFieldSet(this, _Connection_generation, __classPrivateFieldGet(this, _Connection_generation, "f") + 1, "f");
|
|
134
|
+
for (const pending of __classPrivateFieldGet(this, _Connection_pending, "f").values()) {
|
|
135
|
+
clearTimeout(pending.timer);
|
|
136
|
+
pending.reject(new SocketClosedError());
|
|
137
|
+
}
|
|
138
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").clear();
|
|
139
|
+
__classPrivateFieldGet(this, _Connection_handlers, "f").clear();
|
|
140
|
+
__classPrivateFieldGet(this, _Connection_statusHandlers, "f").clear();
|
|
141
|
+
__classPrivateFieldGet(this, _Connection_resyncHandlers, "f").clear();
|
|
142
|
+
__classPrivateFieldGet(this, _Connection_ws, "f")?.close(1000, 'Client closed');
|
|
143
|
+
__classPrivateFieldSet(this, _Connection_ws, undefined, "f");
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
_Connection_config = new WeakMap(), _Connection_url = new WeakMap(), _Connection_transforms = new WeakMap(), _Connection_pending = new WeakMap(), _Connection_handlers = new WeakMap(), _Connection_statusHandlers = new WeakMap(), _Connection_resyncHandlers = new WeakMap(), _Connection_subscribes = new WeakMap(), _Connection_acknowledged = new WeakMap(), _Connection_status = new WeakMap(), _Connection_ws = new WeakMap(), _Connection_generation = new WeakMap(), _Connection_nextId = new WeakMap(), _Connection_lastSeq = new WeakMap(), _Connection_attempt = new WeakMap(), _Connection_retryTimer = new WeakMap(), _Connection_pingTimer = new WeakMap(), _Connection_lastReceivedAt = new WeakMap(), _Connection_instances = new WeakSet(), _Connection_send = function _Connection_send(method, input) {
|
|
147
|
+
var _a;
|
|
148
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") === 'closed') {
|
|
149
|
+
return Promise.reject(new SocketClosedError());
|
|
150
|
+
}
|
|
151
|
+
const id = String(__classPrivateFieldSet(this, _Connection_nextId, (_a = __classPrivateFieldGet(this, _Connection_nextId, "f"), ++_a), "f"));
|
|
152
|
+
const frame = {
|
|
153
|
+
v: socketProtocolVersion,
|
|
154
|
+
type: 'call',
|
|
155
|
+
id,
|
|
156
|
+
method,
|
|
157
|
+
input,
|
|
158
|
+
};
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
const pending = {
|
|
161
|
+
method,
|
|
162
|
+
text: JSON.stringify(frame),
|
|
163
|
+
sent: false,
|
|
164
|
+
resolve,
|
|
165
|
+
reject,
|
|
166
|
+
timer: setTimeout(() => {
|
|
167
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").delete(id);
|
|
168
|
+
reject(new SocketTimeoutError(method, __classPrivateFieldGet(this, _Connection_config, "f").timeoutMs));
|
|
169
|
+
}, __classPrivateFieldGet(this, _Connection_config, "f").timeoutMs),
|
|
170
|
+
};
|
|
171
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").set(id, pending);
|
|
172
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_flush).call(this);
|
|
173
|
+
});
|
|
174
|
+
}, _Connection_subscribe = function _Connection_subscribe() {
|
|
175
|
+
var _a;
|
|
176
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") !== 'connected') {
|
|
177
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_connect).call(this);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const names = [...__classPrivateFieldGet(this, _Connection_handlers, "f").keys()];
|
|
181
|
+
const frame = {
|
|
182
|
+
v: socketProtocolVersion,
|
|
183
|
+
type: 'subscribe',
|
|
184
|
+
id: `subscribe:${__classPrivateFieldSet(this, _Connection_nextId, (_a = __classPrivateFieldGet(this, _Connection_nextId, "f"), ++_a), "f")}`,
|
|
185
|
+
names,
|
|
186
|
+
...(__classPrivateFieldGet(this, _Connection_lastSeq, "f") === undefined ? {} : { since: __classPrivateFieldGet(this, _Connection_lastSeq, "f") }),
|
|
187
|
+
};
|
|
188
|
+
__classPrivateFieldGet(this, _Connection_subscribes, "f").set(frame.id, names);
|
|
189
|
+
__classPrivateFieldGet(this, _Connection_ws, "f")?.send(JSON.stringify(frame));
|
|
190
|
+
}, _Connection_subscribed = function _Connection_subscribed(frame) {
|
|
191
|
+
__classPrivateFieldSet(this, _Connection_lastSeq, frame.seq, "f");
|
|
192
|
+
const names = __classPrivateFieldGet(this, _Connection_subscribes, "f").get(frame.id) ?? [];
|
|
193
|
+
__classPrivateFieldGet(this, _Connection_subscribes, "f").delete(frame.id);
|
|
194
|
+
const fresh = frame.replayed
|
|
195
|
+
? names.filter((name) => !__classPrivateFieldGet(this, _Connection_acknowledged, "f").has(name))
|
|
196
|
+
: names;
|
|
197
|
+
__classPrivateFieldSet(this, _Connection_acknowledged, new Set(names), "f");
|
|
198
|
+
if (fresh.length === 0) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
for (const handler of __classPrivateFieldGet(this, _Connection_resyncHandlers, "f")) {
|
|
202
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_invoke).call(this, handler, { names: fresh }, 'resync');
|
|
203
|
+
}
|
|
204
|
+
}, _Connection_flush = function _Connection_flush() {
|
|
205
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") !== 'connected' || !__classPrivateFieldGet(this, _Connection_ws, "f")) {
|
|
206
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_connect).call(this);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
for (const pending of __classPrivateFieldGet(this, _Connection_pending, "f").values()) {
|
|
210
|
+
if (pending.sent) {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
pending.sent = true;
|
|
214
|
+
__classPrivateFieldGet(this, _Connection_ws, "f").send(pending.text);
|
|
215
|
+
}
|
|
216
|
+
}, _Connection_connect = function _Connection_connect() {
|
|
217
|
+
var _a;
|
|
218
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") !== 'disconnected' || __classPrivateFieldGet(this, _Connection_retryTimer, "f") !== undefined) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'connecting');
|
|
222
|
+
const generation = __classPrivateFieldSet(this, _Connection_generation, (_a = __classPrivateFieldGet(this, _Connection_generation, "f"), ++_a), "f");
|
|
223
|
+
const ws = new (__classPrivateFieldGet(this, _Connection_config, "f").webSocket)(__classPrivateFieldGet(this, _Connection_url, "f"));
|
|
224
|
+
__classPrivateFieldSet(this, _Connection_ws, ws, "f");
|
|
225
|
+
const current = () => generation === __classPrivateFieldGet(this, _Connection_generation, "f");
|
|
226
|
+
ws.addEventListener('open', () => {
|
|
227
|
+
if (!current()) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
__classPrivateFieldSet(this, _Connection_attempt, 0, "f");
|
|
231
|
+
__classPrivateFieldSet(this, _Connection_lastReceivedAt, Date.now(), "f");
|
|
232
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'connected');
|
|
233
|
+
if (__classPrivateFieldGet(this, _Connection_handlers, "f").size > 0) {
|
|
234
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_subscribe).call(this);
|
|
235
|
+
}
|
|
236
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_flush).call(this);
|
|
237
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_startPing).call(this);
|
|
238
|
+
});
|
|
239
|
+
ws.addEventListener('message', (message) => {
|
|
240
|
+
if (!current()) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_receive).call(this, typeof message.data === 'string' ? message.data : '');
|
|
244
|
+
});
|
|
245
|
+
ws.addEventListener('close', (closed) => {
|
|
246
|
+
if (!current()) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_disconnected).call(this, closed.code);
|
|
250
|
+
});
|
|
251
|
+
ws.addEventListener('error', () => {
|
|
252
|
+
if (current()) {
|
|
253
|
+
ws.close();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
}, _Connection_receive = function _Connection_receive(text) {
|
|
257
|
+
__classPrivateFieldSet(this, _Connection_lastReceivedAt, Date.now(), "f");
|
|
258
|
+
if (text === pongText) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const frame = parseServerFrame(text);
|
|
262
|
+
if (!frame) {
|
|
263
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_abandon).call(this, new SocketProtocolError('Received an invalid frame'));
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
switch (frame.type) {
|
|
267
|
+
case 'event':
|
|
268
|
+
__classPrivateFieldSet(this, _Connection_lastSeq, frame.seq, "f");
|
|
269
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_dispatch).call(this, frame.name, frame.payload);
|
|
270
|
+
return;
|
|
271
|
+
case 'subscribed':
|
|
272
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_subscribed).call(this, frame);
|
|
273
|
+
return;
|
|
274
|
+
case 'result':
|
|
275
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_settle).call(this, frame, (pending) => pending.resolve(frame.output));
|
|
276
|
+
return;
|
|
277
|
+
case 'error':
|
|
278
|
+
if (__classPrivateFieldGet(this, _Connection_subscribes, "f").delete(frame.id)) {
|
|
279
|
+
reportFailure('durability.socket.subscribe_failed', {}, frame.error);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_settle).call(this, frame, (pending) => pending.reject(new SocketRemoteError(frame.error)));
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}, _Connection_settle = function _Connection_settle(frame, settle) {
|
|
286
|
+
const pending = __classPrivateFieldGet(this, _Connection_pending, "f").get(frame.id);
|
|
287
|
+
if (!pending) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").delete(frame.id);
|
|
291
|
+
clearTimeout(pending.timer);
|
|
292
|
+
settle(pending);
|
|
293
|
+
}, _Connection_dispatch = function _Connection_dispatch(name, payload) {
|
|
294
|
+
for (const handler of __classPrivateFieldGet(this, _Connection_handlers, "f").get(name) ?? []) {
|
|
295
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_invoke).call(this, handler, payload, name);
|
|
296
|
+
}
|
|
297
|
+
}, _Connection_invoke = function _Connection_invoke(handler, payload, name) {
|
|
298
|
+
try {
|
|
299
|
+
handler(payload);
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
reportFailure('durability.socket.handler_failed', { name }, error);
|
|
303
|
+
}
|
|
304
|
+
}, _Connection_disconnected = function _Connection_disconnected(code) {
|
|
305
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_stopTimers).call(this);
|
|
306
|
+
__classPrivateFieldSet(this, _Connection_ws, undefined, "f");
|
|
307
|
+
__classPrivateFieldGet(this, _Connection_subscribes, "f").clear();
|
|
308
|
+
for (const [id, pending] of __classPrivateFieldGet(this, _Connection_pending, "f")) {
|
|
309
|
+
if (!pending.sent) {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").delete(id);
|
|
313
|
+
clearTimeout(pending.timer);
|
|
314
|
+
pending.reject(new SocketConnectionLostError(pending.method));
|
|
315
|
+
}
|
|
316
|
+
if (code === socketProtocolErrorCode) {
|
|
317
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_abandon).call(this, new SocketProtocolError('Server rejected the protocol'));
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_setStatus).call(this, 'disconnected');
|
|
321
|
+
__classPrivateFieldSet(this, _Connection_attempt, __classPrivateFieldGet(this, _Connection_attempt, "f") + 1, "f");
|
|
322
|
+
__classPrivateFieldSet(this, _Connection_retryTimer, setTimeout(() => {
|
|
323
|
+
__classPrivateFieldSet(this, _Connection_retryTimer, undefined, "f");
|
|
324
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_connect).call(this);
|
|
325
|
+
}, jitter(exponential(__classPrivateFieldGet(this, _Connection_attempt, "f"), 250, 60000))), "f");
|
|
326
|
+
}, _Connection_abandon = function _Connection_abandon(error) {
|
|
327
|
+
reportFailure('durability.socket.protocol_error', {}, error);
|
|
328
|
+
for (const pending of __classPrivateFieldGet(this, _Connection_pending, "f").values()) {
|
|
329
|
+
clearTimeout(pending.timer);
|
|
330
|
+
pending.reject(error);
|
|
331
|
+
}
|
|
332
|
+
__classPrivateFieldGet(this, _Connection_pending, "f").clear();
|
|
333
|
+
this.close();
|
|
334
|
+
}, _Connection_startPing = function _Connection_startPing() {
|
|
335
|
+
const interval = __classPrivateFieldGet(this, _Connection_config, "f").pingIntervalMs;
|
|
336
|
+
__classPrivateFieldSet(this, _Connection_pingTimer, setInterval(() => {
|
|
337
|
+
if (Date.now() - __classPrivateFieldGet(this, _Connection_lastReceivedAt, "f") > interval * 2) {
|
|
338
|
+
__classPrivateFieldGet(this, _Connection_ws, "f")?.close(4000, 'Keep-alive timeout');
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
__classPrivateFieldGet(this, _Connection_ws, "f")?.send(pingText);
|
|
342
|
+
}, interval), "f");
|
|
343
|
+
}, _Connection_stopTimers = function _Connection_stopTimers() {
|
|
344
|
+
if (__classPrivateFieldGet(this, _Connection_pingTimer, "f") !== undefined) {
|
|
345
|
+
clearInterval(__classPrivateFieldGet(this, _Connection_pingTimer, "f"));
|
|
346
|
+
__classPrivateFieldSet(this, _Connection_pingTimer, undefined, "f");
|
|
347
|
+
}
|
|
348
|
+
if (__classPrivateFieldGet(this, _Connection_retryTimer, "f") !== undefined) {
|
|
349
|
+
clearTimeout(__classPrivateFieldGet(this, _Connection_retryTimer, "f"));
|
|
350
|
+
__classPrivateFieldSet(this, _Connection_retryTimer, undefined, "f");
|
|
351
|
+
}
|
|
352
|
+
}, _Connection_setStatus = function _Connection_setStatus(status) {
|
|
353
|
+
if (__classPrivateFieldGet(this, _Connection_status, "f") === status) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
__classPrivateFieldSet(this, _Connection_status, status, "f");
|
|
357
|
+
for (const handler of __classPrivateFieldGet(this, _Connection_statusHandlers, "f")) {
|
|
358
|
+
__classPrivateFieldGet(this, _Connection_instances, "m", _Connection_invoke).call(this, handler, status, 'status');
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
const defaultWebSocket = () => {
|
|
362
|
+
if (typeof WebSocket === 'undefined') {
|
|
363
|
+
throw new Error('No WebSocket implementation; pass `webSocket` in the client config');
|
|
364
|
+
}
|
|
365
|
+
return WebSocket;
|
|
366
|
+
};
|
|
367
|
+
/**
|
|
368
|
+
* Browser client for a {@link DurabilitySocket}. Connections open lazily,
|
|
369
|
+
* reconnect with jittered backoff, and resubscribe from the last seen event.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
* ```ts
|
|
373
|
+
* const client = new DurabilitySocketClient<TodoList['socket']>({
|
|
374
|
+
* url: (id) => `wss://example.com/todos/${id}`,
|
|
375
|
+
* });
|
|
376
|
+
* const conn = client.connect('personal');
|
|
377
|
+
* const todos = await conn.rpc.list();
|
|
378
|
+
* conn.on.created((todo) => render(todo));
|
|
379
|
+
* conn.on.resync(() => reload());
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
export class DurabilitySocketClient {
|
|
383
|
+
constructor(config, transforms = []) {
|
|
384
|
+
_DurabilitySocketClient_config.set(this, void 0);
|
|
385
|
+
_DurabilitySocketClient_transforms.set(this, void 0);
|
|
386
|
+
__classPrivateFieldSet(this, _DurabilitySocketClient_config, {
|
|
387
|
+
url: config.url,
|
|
388
|
+
timeoutMs: config.timeoutMs ?? 120000,
|
|
389
|
+
pingIntervalMs: config.pingIntervalMs ?? 30000,
|
|
390
|
+
webSocket: config.webSocket ?? defaultWebSocket(),
|
|
391
|
+
}, "f");
|
|
392
|
+
__classPrivateFieldSet(this, _DurabilitySocketClient_transforms, transforms, "f");
|
|
393
|
+
}
|
|
394
|
+
/** Returns a client that also applies `transform` around every call. */
|
|
395
|
+
with(transform) {
|
|
396
|
+
return new DurabilitySocketClient(__classPrivateFieldGet(this, _DurabilitySocketClient_config, "f"), [
|
|
397
|
+
...__classPrivateFieldGet(this, _DurabilitySocketClient_transforms, "f"),
|
|
398
|
+
transform,
|
|
399
|
+
]);
|
|
400
|
+
}
|
|
401
|
+
connect(id) {
|
|
402
|
+
const connection = new Connection(__classPrivateFieldGet(this, _DurabilitySocketClient_config, "f"), id, __classPrivateFieldGet(this, _DurabilitySocketClient_transforms, "f"));
|
|
403
|
+
const rpc = new Proxy({}, {
|
|
404
|
+
get: (_target, method) => typeof method === 'string'
|
|
405
|
+
? (input) => connection.call(method, input)
|
|
406
|
+
: undefined,
|
|
407
|
+
});
|
|
408
|
+
const on = new Proxy({}, {
|
|
409
|
+
get: (_target, name) => {
|
|
410
|
+
if (name === 'status') {
|
|
411
|
+
return (handler) => connection.onStatus(handler);
|
|
412
|
+
}
|
|
413
|
+
if (name === 'resync') {
|
|
414
|
+
return (handler) => connection.onResync(handler);
|
|
415
|
+
}
|
|
416
|
+
return typeof name === 'string'
|
|
417
|
+
? (handler) => connection.on(name, handler)
|
|
418
|
+
: undefined;
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
return {
|
|
422
|
+
rpc,
|
|
423
|
+
on,
|
|
424
|
+
get status() {
|
|
425
|
+
return connection.status;
|
|
426
|
+
},
|
|
427
|
+
close: () => connection.close(),
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
_DurabilitySocketClient_config = new WeakMap(), _DurabilitySocketClient_transforms = new WeakMap();
|
|
432
|
+
/** Logs every call's input, output or error, and duration. */
|
|
433
|
+
export const logging = (log = console.log) => async ({ method, input, next }) => {
|
|
434
|
+
const startedAt = Date.now();
|
|
435
|
+
try {
|
|
436
|
+
const output = await next();
|
|
437
|
+
log({ method, input, output, durationMs: Date.now() - startedAt });
|
|
438
|
+
return output;
|
|
439
|
+
}
|
|
440
|
+
catch (error) {
|
|
441
|
+
log({ method, input, error, durationMs: Date.now() - startedAt });
|
|
442
|
+
throw error;
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
export { socketShape, } from './socket-protocol.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type LifecycleHook } from './lifecycle.js';
|
|
1
|
+
import { type DurabilityLifecycleEvent, type LifecycleHook } from './lifecycle.js';
|
|
2
2
|
import { type DurabilityMigrationResult } from './migrations.js';
|
|
3
3
|
import { type DurabilityAlarmMethodOptions, type ExecutionPolicyOptions } from './policy.js';
|
|
4
4
|
import { type SchedulerAttachment } from './scheduler.js';
|
|
@@ -39,6 +39,12 @@ export type DurabilityAlarmsConfig<Names extends string = string> = DurabilityAl
|
|
|
39
39
|
/** Alarm handlers keyed by the scheduling method names they become. */
|
|
40
40
|
handlers: Record<Names, DurableAlarmHandler>;
|
|
41
41
|
};
|
|
42
|
+
/** A lifecycle event for one of this instance's named alarms. */
|
|
43
|
+
export type DurabilityAlarmEvent = Extract<DurabilityLifecycleEvent, {
|
|
44
|
+
entityKind: 'named_alarm';
|
|
45
|
+
}> | Extract<DurabilityLifecycleEvent, {
|
|
46
|
+
type: 'purged';
|
|
47
|
+
}>;
|
|
42
48
|
/** Schedules (or replaces) the named alarm's pending occurrence. */
|
|
43
49
|
export type DurableNamedAlarm = (scheduledTime: number) => Promise<void>;
|
|
44
50
|
declare class DurabilityAlarmsCore<Names extends string> {
|
|
@@ -48,6 +54,11 @@ declare class DurabilityAlarmsCore<Names extends string> {
|
|
|
48
54
|
static migrate(context: Pick<DurableObjectState, 'storage'>, target?: string | null): DurabilityMigrationResult;
|
|
49
55
|
/** Runs due named alarms and re-arms the physical alarm. */
|
|
50
56
|
alarm(info?: AlarmInvocationInfo): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Attaches a best-effort lifecycle hook alongside `onLifecycleEvent` and
|
|
59
|
+
* returns a function that detaches it.
|
|
60
|
+
*/
|
|
61
|
+
lifecycle(hook: (event: DurabilityAlarmEvent) => void | Promise<void>): () => void;
|
|
51
62
|
/**
|
|
52
63
|
* Destructively removes named alarm records created strictly before the
|
|
53
64
|
* timestamp, regardless of status, and returns how many were removed.
|
|
@@ -82,6 +93,8 @@ declare class DurabilityAlarmsCore<Names extends string> {
|
|
|
82
93
|
* ```
|
|
83
94
|
*/
|
|
84
95
|
export type DurabilityAlarms<Names extends string> = DurabilityAlarmsCore<Names> & Record<Names, DurableNamedAlarm>;
|
|
96
|
+
/** Any `DurabilityAlarms` instance, without the index signature of `DurabilityAlarms<string>`. */
|
|
97
|
+
export type AnyDurabilityAlarms = DurabilityAlarmsCore<string>;
|
|
85
98
|
export interface DurabilityAlarmsConstructor {
|
|
86
99
|
new <const Names extends string>(config: DurabilityAlarmsConfig<Names>): DurabilityAlarms<Names>;
|
|
87
100
|
migrate: typeof DurabilityAlarmsCore.migrate;
|