@dxos/edge-client 0.6.11 → 0.6.12-main.5a87ad5
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/dist/lib/browser/chunk-ZWJXA37R.mjs +113 -0
- package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +57 -157
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +122 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node/chunk-ANV2HBEH.cjs +136 -0
- package/dist/lib/node/chunk-ANV2HBEH.cjs.map +7 -0
- package/dist/lib/node/index.cjs +56 -154
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +152 -0
- package/dist/lib/node/testing/index.cjs.map +7 -0
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs +115 -0
- package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +378 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/lib/node-esm/testing/index.mjs +123 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/defs.d.ts.map +1 -1
- package/dist/types/src/edge-client.d.ts +3 -2
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +4 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/protocol.d.ts +2 -2
- package/dist/types/src/protocol.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/test-utils.d.ts +20 -0
- package/dist/types/src/testing/test-utils.d.ts.map +1 -0
- package/package.json +26 -14
- package/src/defs.ts +2 -3
- package/src/edge-client.test.ts +11 -11
- package/src/edge-client.ts +17 -8
- package/src/errors.ts +8 -2
- package/src/index.ts +1 -0
- package/src/persistent-lifecycle.test.ts +2 -2
- package/src/protocol.test.ts +1 -2
- package/src/protocol.ts +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/test-utils.ts +111 -0
- package/src/websocket.test.ts +5 -4
- package/dist/types/src/test-utils.d.ts +0 -11
- package/dist/types/src/test-utils.d.ts.map +0 -1
- package/src/test-utils.ts +0 -49
package/dist/lib/node/index.cjs
CHANGED
|
@@ -30,129 +30,34 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
var node_exports = {};
|
|
31
31
|
__export(node_exports, {
|
|
32
32
|
EdgeClient: () => EdgeClient,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
EdgeConnectionClosedError: () => EdgeConnectionClosedError,
|
|
34
|
+
EdgeIdentityChangedError: () => EdgeIdentityChangedError,
|
|
35
|
+
Protocol: () => import_chunk_ANV2HBEH.Protocol,
|
|
36
|
+
getTypename: () => import_chunk_ANV2HBEH.getTypename,
|
|
37
|
+
protocol: () => import_chunk_ANV2HBEH.protocol,
|
|
38
|
+
toUint8Array: () => import_chunk_ANV2HBEH.toUint8Array
|
|
37
39
|
});
|
|
38
40
|
module.exports = __toCommonJS(node_exports);
|
|
41
|
+
var import_chunk_ANV2HBEH = require("./chunk-ANV2HBEH.cjs");
|
|
39
42
|
__reExport(node_exports, require("@dxos/protocols/buf/dxos/edge/messenger_pb"), module.exports);
|
|
40
43
|
var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
|
|
41
44
|
var import_async = require("@dxos/async");
|
|
42
45
|
var import_context = require("@dxos/context");
|
|
43
|
-
var import_invariant = require("@dxos/invariant");
|
|
44
46
|
var import_log = require("@dxos/log");
|
|
45
47
|
var import_buf = require("@dxos/protocols/buf");
|
|
46
48
|
var import_messenger_pb = require("@dxos/protocols/buf/dxos/edge/messenger_pb");
|
|
47
|
-
var import_wkt = require("@bufbuild/protobuf/wkt");
|
|
48
|
-
var import_messenger_pb2 = require("@dxos/protocols/buf/dxos/edge/messenger_pb");
|
|
49
|
-
var import_invariant2 = require("@dxos/invariant");
|
|
50
|
-
var import_buf2 = require("@dxos/protocols/buf");
|
|
51
|
-
var import_messenger_pb3 = require("@dxos/protocols/buf/dxos/edge/messenger_pb");
|
|
52
|
-
var import_util = require("@dxos/util");
|
|
53
49
|
var import_async2 = require("@dxos/async");
|
|
54
50
|
var import_context2 = require("@dxos/context");
|
|
55
51
|
var import_debug = require("@dxos/debug");
|
|
56
52
|
var import_log2 = require("@dxos/log");
|
|
57
|
-
var
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
constructor(types) {
|
|
61
|
-
this._typeRegistry = import_buf2.buf.createRegistry(...types);
|
|
62
|
-
}
|
|
63
|
-
get typeRegistry() {
|
|
64
|
-
return this._typeRegistry;
|
|
65
|
-
}
|
|
66
|
-
toJson(message) {
|
|
67
|
-
try {
|
|
68
|
-
return import_buf2.buf.toJson(import_messenger_pb3.MessageSchema, message, {
|
|
69
|
-
registry: this.typeRegistry
|
|
70
|
-
});
|
|
71
|
-
} catch (err) {
|
|
72
|
-
return {
|
|
73
|
-
type: this.getPayloadType(message)
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Return the payload with the given type.
|
|
79
|
-
*/
|
|
80
|
-
getPayload(message, type) {
|
|
81
|
-
(0, import_invariant2.invariant)(message.payload, void 0, {
|
|
82
|
-
F: __dxlog_file,
|
|
83
|
-
L: 40,
|
|
84
|
-
S: this,
|
|
85
|
-
A: [
|
|
86
|
-
"message.payload",
|
|
87
|
-
""
|
|
88
|
-
]
|
|
89
|
-
});
|
|
90
|
-
const payloadTypename = this.getPayloadType(message);
|
|
91
|
-
if (type && type.typeName !== payloadTypename) {
|
|
92
|
-
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
93
|
-
}
|
|
94
|
-
(0, import_invariant2.invariant)(import_buf2.bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
95
|
-
F: __dxlog_file,
|
|
96
|
-
L: 46,
|
|
97
|
-
S: this,
|
|
98
|
-
A: [
|
|
99
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
100
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
101
|
-
]
|
|
102
|
-
});
|
|
103
|
-
const payload = import_buf2.bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
104
|
-
(0, import_invariant2.invariant)(payload, `Empty payload: ${payloadTypename}}`, {
|
|
105
|
-
F: __dxlog_file,
|
|
106
|
-
L: 48,
|
|
107
|
-
S: this,
|
|
108
|
-
A: [
|
|
109
|
-
"payload",
|
|
110
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
111
|
-
]
|
|
112
|
-
});
|
|
113
|
-
return payload;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get the payload type.
|
|
117
|
-
*/
|
|
118
|
-
getPayloadType(message) {
|
|
119
|
-
if (!message.payload) {
|
|
120
|
-
return void 0;
|
|
121
|
-
}
|
|
122
|
-
const [, type] = message.payload.typeUrl.split("/");
|
|
123
|
-
return type;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Create a packed message.
|
|
127
|
-
*/
|
|
128
|
-
createMessage(type, { source, target, payload, serviceId }) {
|
|
129
|
-
return import_buf2.buf.create(import_messenger_pb3.MessageSchema, {
|
|
130
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
131
|
-
source,
|
|
132
|
-
target,
|
|
133
|
-
serviceId,
|
|
134
|
-
payload: payload ? import_buf2.bufWkt.anyPack(type, import_buf2.buf.create(type, payload)) : void 0
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
var toUint8Array = async (data) => {
|
|
139
|
-
if (data instanceof Buffer) {
|
|
140
|
-
return (0, import_util.bufferToArray)(data);
|
|
141
|
-
}
|
|
142
|
-
if (data instanceof Blob) {
|
|
143
|
-
return new Uint8Array(await data.arrayBuffer());
|
|
53
|
+
var EdgeConnectionClosedError = class extends Error {
|
|
54
|
+
constructor() {
|
|
55
|
+
super("Edge connection closed.");
|
|
144
56
|
}
|
|
145
|
-
throw new Error(`Unexpected datatype: ${data}`);
|
|
146
57
|
};
|
|
147
|
-
var
|
|
148
|
-
import_messenger_pb2.SwarmRequestSchema,
|
|
149
|
-
import_messenger_pb2.SwarmResponseSchema,
|
|
150
|
-
import_messenger_pb2.TextMessageSchema,
|
|
151
|
-
import_wkt.AnySchema
|
|
152
|
-
]);
|
|
153
|
-
var WebsocketClosedError = class extends Error {
|
|
58
|
+
var EdgeIdentityChangedError = class extends Error {
|
|
154
59
|
constructor() {
|
|
155
|
-
super("
|
|
60
|
+
super("Edge identity changed.");
|
|
156
61
|
}
|
|
157
62
|
};
|
|
158
63
|
function _ts_decorate(decorators, target, key, desc) {
|
|
@@ -161,7 +66,7 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
161
66
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
162
67
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
163
68
|
}
|
|
164
|
-
var
|
|
69
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/persistent-lifecycle.ts";
|
|
165
70
|
var INIT_RESTART_DELAY = 100;
|
|
166
71
|
var DEFAULT_MAX_RESTART_DELAY = 5e3;
|
|
167
72
|
var PersistentLifecycle = class extends import_context2.Resource {
|
|
@@ -182,7 +87,7 @@ var PersistentLifecycle = class extends import_context2.Resource {
|
|
|
182
87
|
import_log2.log.warn("Restart failed", {
|
|
183
88
|
err
|
|
184
89
|
}, {
|
|
185
|
-
F:
|
|
90
|
+
F: __dxlog_file,
|
|
186
91
|
L: 64,
|
|
187
92
|
S: this,
|
|
188
93
|
C: (f, a) => f(...a)
|
|
@@ -194,7 +99,7 @@ var PersistentLifecycle = class extends import_context2.Resource {
|
|
|
194
99
|
import_log2.log.warn("Start failed", {
|
|
195
100
|
err
|
|
196
101
|
}, {
|
|
197
|
-
F:
|
|
102
|
+
F: __dxlog_file,
|
|
198
103
|
L: 69,
|
|
199
104
|
S: this,
|
|
200
105
|
C: (f, a) => f(...a)
|
|
@@ -211,7 +116,7 @@ var PersistentLifecycle = class extends import_context2.Resource {
|
|
|
211
116
|
(0, import_log2.log)(`restarting in ${this._restartAfter}ms`, {
|
|
212
117
|
state: this._lifecycleState
|
|
213
118
|
}, {
|
|
214
|
-
F:
|
|
119
|
+
F: __dxlog_file,
|
|
215
120
|
L: 81,
|
|
216
121
|
S: this,
|
|
217
122
|
C: (f, a) => f(...a)
|
|
@@ -242,7 +147,7 @@ _ts_decorate([
|
|
|
242
147
|
_ts_decorate([
|
|
243
148
|
import_async2.synchronized
|
|
244
149
|
], PersistentLifecycle.prototype, "scheduleRestart", null);
|
|
245
|
-
var
|
|
150
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-client.ts";
|
|
246
151
|
var DEFAULT_TIMEOUT = 1e4;
|
|
247
152
|
var SIGNAL_KEEPALIVE_INTERVAL = 5e3;
|
|
248
153
|
var EdgeClient = class extends import_context.Resource {
|
|
@@ -252,6 +157,7 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
252
157
|
this._peerKey = _peerKey;
|
|
253
158
|
this._config = _config;
|
|
254
159
|
this.reconnect = new import_async.Event();
|
|
160
|
+
this.connected = new import_async.Event();
|
|
255
161
|
this._persistentLifecycle = new PersistentLifecycle({
|
|
256
162
|
start: async () => this._openWebSocket(),
|
|
257
163
|
stop: async () => this._closeWebSocket(),
|
|
@@ -262,7 +168,6 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
262
168
|
this._ws = void 0;
|
|
263
169
|
this._keepaliveCtx = void 0;
|
|
264
170
|
this._heartBeatContext = void 0;
|
|
265
|
-
this._protocol = this._config.protocol ?? protocol;
|
|
266
171
|
}
|
|
267
172
|
// TODO(burdon): Attach logging.
|
|
268
173
|
get info() {
|
|
@@ -294,8 +199,8 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
294
199
|
(0, import_log.log)("opening...", {
|
|
295
200
|
info: this.info
|
|
296
201
|
}, {
|
|
297
|
-
F:
|
|
298
|
-
L:
|
|
202
|
+
F: __dxlog_file2,
|
|
203
|
+
L: 100,
|
|
299
204
|
S: this,
|
|
300
205
|
C: (f, a) => f(...a)
|
|
301
206
|
});
|
|
@@ -303,8 +208,8 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
303
208
|
import_log.log.warn("Error while opening connection", {
|
|
304
209
|
err
|
|
305
210
|
}, {
|
|
306
|
-
F:
|
|
307
|
-
L:
|
|
211
|
+
F: __dxlog_file2,
|
|
212
|
+
L: 102,
|
|
308
213
|
S: this,
|
|
309
214
|
C: (f, a) => f(...a)
|
|
310
215
|
});
|
|
@@ -317,8 +222,8 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
317
222
|
(0, import_log.log)("closing...", {
|
|
318
223
|
peerKey: this._peerKey
|
|
319
224
|
}, {
|
|
320
|
-
F:
|
|
321
|
-
L:
|
|
225
|
+
F: __dxlog_file2,
|
|
226
|
+
L: 110,
|
|
322
227
|
S: this,
|
|
323
228
|
C: (f, a) => f(...a)
|
|
324
229
|
});
|
|
@@ -329,16 +234,17 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
329
234
|
this._ws = new import_isomorphic_ws.default(url);
|
|
330
235
|
this._ws.onopen = () => {
|
|
331
236
|
(0, import_log.log)("opened", this.info, {
|
|
332
|
-
F:
|
|
333
|
-
L:
|
|
237
|
+
F: __dxlog_file2,
|
|
238
|
+
L: 119,
|
|
334
239
|
S: this,
|
|
335
240
|
C: (f, a) => f(...a)
|
|
336
241
|
});
|
|
337
242
|
this._ready.wake();
|
|
243
|
+
this.connected.emit();
|
|
338
244
|
};
|
|
339
245
|
this._ws.onclose = () => {
|
|
340
246
|
(0, import_log.log)("closed", this.info, {
|
|
341
|
-
F:
|
|
247
|
+
F: __dxlog_file2,
|
|
342
248
|
L: 124,
|
|
343
249
|
S: this,
|
|
344
250
|
C: (f, a) => f(...a)
|
|
@@ -350,7 +256,7 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
350
256
|
error: event.error,
|
|
351
257
|
info: event.message
|
|
352
258
|
}, {
|
|
353
|
-
F:
|
|
259
|
+
F: __dxlog_file2,
|
|
354
260
|
L: 128,
|
|
355
261
|
S: this,
|
|
356
262
|
C: (f, a) => f(...a)
|
|
@@ -362,13 +268,13 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
362
268
|
this._onHeartbeat();
|
|
363
269
|
return;
|
|
364
270
|
}
|
|
365
|
-
const data = await toUint8Array(event.data);
|
|
271
|
+
const data = await (0, import_chunk_ANV2HBEH.toUint8Array)(event.data);
|
|
366
272
|
const message = import_buf.buf.fromBinary(import_messenger_pb.MessageSchema, data);
|
|
367
273
|
(0, import_log.log)("received", {
|
|
368
274
|
peerKey: this._peerKey,
|
|
369
|
-
payload: protocol.getPayloadType(message)
|
|
275
|
+
payload: import_chunk_ANV2HBEH.protocol.getPayloadType(message)
|
|
370
276
|
}, {
|
|
371
|
-
F:
|
|
277
|
+
F: __dxlog_file2,
|
|
372
278
|
L: 141,
|
|
373
279
|
S: this,
|
|
374
280
|
C: (f, a) => f(...a)
|
|
@@ -380,9 +286,9 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
380
286
|
} catch (err) {
|
|
381
287
|
import_log.log.error("processing", {
|
|
382
288
|
err,
|
|
383
|
-
payload: protocol.getPayloadType(message)
|
|
289
|
+
payload: import_chunk_ANV2HBEH.protocol.getPayloadType(message)
|
|
384
290
|
}, {
|
|
385
|
-
F:
|
|
291
|
+
F: __dxlog_file2,
|
|
386
292
|
L: 147,
|
|
387
293
|
S: this,
|
|
388
294
|
C: (f, a) => f(...a)
|
|
@@ -395,7 +301,7 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
395
301
|
timeout: this._config.timeout ?? DEFAULT_TIMEOUT
|
|
396
302
|
});
|
|
397
303
|
this._keepaliveCtx = new import_context.Context(void 0, {
|
|
398
|
-
F:
|
|
304
|
+
F: __dxlog_file2,
|
|
399
305
|
L: 154
|
|
400
306
|
});
|
|
401
307
|
(0, import_async.scheduleTaskInterval)(this._keepaliveCtx, async () => {
|
|
@@ -409,7 +315,7 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
409
315
|
return;
|
|
410
316
|
}
|
|
411
317
|
try {
|
|
412
|
-
this._ready.throw(new
|
|
318
|
+
this._ready.throw(this.isOpen ? new EdgeIdentityChangedError() : new EdgeConnectionClosedError());
|
|
413
319
|
this._ready.reset();
|
|
414
320
|
void this._keepaliveCtx?.dispose();
|
|
415
321
|
this._keepaliveCtx = void 0;
|
|
@@ -430,7 +336,7 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
430
336
|
import_log.log.warn("Error closing websocket", {
|
|
431
337
|
err
|
|
432
338
|
}, {
|
|
433
|
-
F:
|
|
339
|
+
F: __dxlog_file2,
|
|
434
340
|
L: 190,
|
|
435
341
|
S: this,
|
|
436
342
|
C: (f, a) => f(...a)
|
|
@@ -443,34 +349,28 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
443
349
|
*/
|
|
444
350
|
async send(message) {
|
|
445
351
|
if (this._ready.state !== import_async.TriggerState.RESOLVED) {
|
|
352
|
+
(0, import_log.log)("waiting for websocket to become ready", void 0, {
|
|
353
|
+
F: __dxlog_file2,
|
|
354
|
+
L: 200,
|
|
355
|
+
S: this,
|
|
356
|
+
C: (f, a) => f(...a)
|
|
357
|
+
});
|
|
446
358
|
await this._ready.wait({
|
|
447
359
|
timeout: this._config.timeout ?? DEFAULT_TIMEOUT
|
|
448
360
|
});
|
|
449
361
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
""
|
|
457
|
-
]
|
|
458
|
-
});
|
|
459
|
-
(0, import_invariant.invariant)(!message.source || message.source.peerKey === this._peerKey, void 0, {
|
|
460
|
-
F: __dxlog_file3,
|
|
461
|
-
L: 203,
|
|
462
|
-
S: this,
|
|
463
|
-
A: [
|
|
464
|
-
"!message.source || message.source.peerKey === this._peerKey",
|
|
465
|
-
""
|
|
466
|
-
]
|
|
467
|
-
});
|
|
362
|
+
if (!this._ws) {
|
|
363
|
+
throw new EdgeConnectionClosedError();
|
|
364
|
+
}
|
|
365
|
+
if (message.source && (message.source.peerKey !== this._peerKey || message.source.identityKey !== this.identityKey)) {
|
|
366
|
+
throw new EdgeIdentityChangedError();
|
|
367
|
+
}
|
|
468
368
|
(0, import_log.log)("sending...", {
|
|
469
369
|
peerKey: this._peerKey,
|
|
470
|
-
payload: protocol.getPayloadType(message)
|
|
370
|
+
payload: import_chunk_ANV2HBEH.protocol.getPayloadType(message)
|
|
471
371
|
}, {
|
|
472
|
-
F:
|
|
473
|
-
L:
|
|
372
|
+
F: __dxlog_file2,
|
|
373
|
+
L: 213,
|
|
474
374
|
S: this,
|
|
475
375
|
C: (f, a) => f(...a)
|
|
476
376
|
});
|
|
@@ -482,8 +382,8 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
482
382
|
}
|
|
483
383
|
void this._heartBeatContext?.dispose();
|
|
484
384
|
this._heartBeatContext = new import_context.Context(void 0, {
|
|
485
|
-
F:
|
|
486
|
-
L:
|
|
385
|
+
F: __dxlog_file2,
|
|
386
|
+
L: 222
|
|
487
387
|
});
|
|
488
388
|
(0, import_async.scheduleTask)(this._heartBeatContext, () => {
|
|
489
389
|
this._persistentLifecycle.scheduleRestart();
|
|
@@ -493,6 +393,8 @@ var EdgeClient = class extends import_context.Resource {
|
|
|
493
393
|
// Annotate the CommonJS export names for ESM import in node:
|
|
494
394
|
0 && (module.exports = {
|
|
495
395
|
EdgeClient,
|
|
396
|
+
EdgeConnectionClosedError,
|
|
397
|
+
EdgeIdentityChangedError,
|
|
496
398
|
Protocol,
|
|
497
399
|
getTypename,
|
|
498
400
|
protocol,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/index.ts", "../../../src/edge-client.ts", "../../../src/
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nexport * from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nexport * from './edge-client';\nexport * from './defs';\nexport * from './protocol';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport WebSocket from 'isomorphic-ws';\n\nimport { Trigger, Event, scheduleTaskInterval, scheduleTask, TriggerState } from '@dxos/async';\nimport { Context, LifecycleState, Resource, type Lifecycle } from '@dxos/context';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { buf } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { protocol } from './defs';\nimport { WebsocketClosedError } from './errors';\nimport { PersistentLifecycle } from './persistent-lifecycle';\nimport { type Protocol, toUint8Array } from './protocol';\n\nconst DEFAULT_TIMEOUT = 10_000;\nconst SIGNAL_KEEPALIVE_INTERVAL = 5_000;\n\nexport type MessageListener = (message: Message) => void | Promise<void>;\n\nexport interface EdgeConnection extends Required<Lifecycle> {\n reconnect: Event;\n\n get info(): any;\n get identityKey(): string;\n get peerKey(): string;\n get isOpen(): boolean;\n setIdentity(params: { peerKey: string; identityKey: string }): void;\n addListener(listener: MessageListener): () => void;\n send(message: Message): Promise<void>;\n}\n\nexport type MessengerConfig = {\n socketEndpoint: string;\n timeout?: number;\n protocol?: Protocol;\n};\n\n/**\n * Messenger client.\n */\nexport class EdgeClient extends Resource implements EdgeConnection {\n public reconnect = new Event();\n private readonly _persistentLifecycle = new PersistentLifecycle({\n start: async () => this._openWebSocket(),\n stop: async () => this._closeWebSocket(),\n onRestart: async () => this.reconnect.emit(),\n });\n\n private readonly _listeners = new Set<MessageListener>();\n private readonly _protocol: Protocol;\n private _ready = new Trigger();\n private _ws?: WebSocket = undefined;\n private _keepaliveCtx?: Context = undefined;\n private _heartBeatContext?: Context = undefined;\n\n constructor(\n private _identityKey: string,\n private _peerKey: string,\n private readonly _config: MessengerConfig,\n ) {\n super();\n this._protocol = this._config.protocol ?? protocol;\n }\n\n // TODO(burdon): Attach logging.\n public get info() {\n return {\n open: this.isOpen,\n identity: this._identityKey,\n device: this._peerKey,\n };\n }\n\n get identityKey() {\n return this._identityKey;\n }\n\n get peerKey() {\n return this._peerKey;\n }\n\n setIdentity({ peerKey, identityKey }: { peerKey: string; identityKey: string }) {\n this._peerKey = peerKey;\n this._identityKey = identityKey;\n this._persistentLifecycle.scheduleRestart();\n }\n\n public addListener(listener: MessageListener): () => void {\n this._listeners.add(listener);\n return () => this._listeners.delete(listener);\n }\n\n /**\n * Open connection to messaging service.\n */\n protected override async _open() {\n log('opening...', { info: this.info });\n this._persistentLifecycle.open().catch((err) => {\n log.warn('Error while opening connection', { err });\n });\n }\n\n /**\n * Close connection and free resources.\n */\n protected override async _close() {\n log('closing...', { peerKey: this._peerKey });\n await this._persistentLifecycle.close();\n }\n\n private async _openWebSocket() {\n const url = new URL(`/ws/${this._identityKey}/${this._peerKey}`, this._config.socketEndpoint);\n this._ws = new WebSocket(url);\n\n this._ws.onopen = () => {\n log('opened', this.info);\n this._ready.wake();\n };\n this._ws.onclose = () => {\n log('closed', this.info);\n this._persistentLifecycle.scheduleRestart();\n };\n this._ws.onerror = (event) => {\n log.warn('EdgeClient socket error', { error: event.error, info: event.message });\n this._persistentLifecycle.scheduleRestart();\n };\n /**\n * https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data\n */\n this._ws.onmessage = async (event) => {\n if (event.data === '__pong__') {\n this._onHeartbeat();\n return;\n }\n const data = await toUint8Array(event.data);\n const message = buf.fromBinary(MessageSchema, data);\n log('received', { peerKey: this._peerKey, payload: protocol.getPayloadType(message) });\n if (message) {\n for (const listener of this._listeners) {\n try {\n await listener(message);\n } catch (err) {\n log.error('processing', { err, payload: protocol.getPayloadType(message) });\n }\n }\n }\n };\n\n await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });\n this._keepaliveCtx = new Context();\n scheduleTaskInterval(\n this._keepaliveCtx,\n async () => {\n // TODO(mykola): use RFC6455 ping/pong once implemented in the browser?\n // Cloudflare's worker responds to this `without interrupting hibernation`. https://developers.cloudflare.com/durable-objects/api/websockets/#setwebsocketautoresponse\n this._ws?.send('__ping__');\n },\n SIGNAL_KEEPALIVE_INTERVAL,\n );\n this._ws.send('__ping__');\n this._onHeartbeat();\n }\n\n private async _closeWebSocket() {\n if (!this._ws) {\n return;\n }\n try {\n this._ready.throw(new WebsocketClosedError());\n this._ready.reset();\n void this._keepaliveCtx?.dispose();\n this._keepaliveCtx = undefined;\n void this._heartBeatContext?.dispose();\n this._heartBeatContext = undefined;\n\n // NOTE: Remove event handlers to avoid scheduling restart.\n this._ws.onopen = () => {};\n this._ws.onclose = () => {};\n this._ws.onerror = () => {};\n this._ws.close();\n this._ws = undefined;\n } catch (err) {\n if (err instanceof Error && err.message.includes('WebSocket is closed before the connection is established.')) {\n return;\n }\n log.warn('Error closing websocket', { err });\n }\n }\n\n /**\n * Send message.\n * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.\n */\n public async send(message: Message): Promise<void> {\n if (this._ready.state !== TriggerState.RESOLVED) {\n await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });\n }\n invariant(this._ws);\n invariant(!message.source || message.source.peerKey === this._peerKey);\n log('sending...', { peerKey: this._peerKey, payload: protocol.getPayloadType(message) });\n this._ws.send(buf.toBinary(MessageSchema, message));\n }\n\n private _onHeartbeat() {\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n void this._heartBeatContext?.dispose();\n this._heartBeatContext = new Context();\n scheduleTask(\n this._heartBeatContext,\n () => {\n this._persistentLifecycle.scheduleRestart();\n },\n 2 * SIGNAL_KEEPALIVE_INTERVAL,\n );\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { AnySchema } from '@bufbuild/protobuf/wkt';\n\nimport { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { Protocol } from './protocol';\n\nexport const protocol = new Protocol([SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema, AnySchema]);\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { buf, bufWkt } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema, type Peer as PeerProto } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\nimport { bufferToArray } from '@dxos/util';\n\nexport type PeerData = Partial<PeerProto>;\n\nexport const getTypename = (typeName: string) => `type.googleapis.com/${typeName}`;\n\n/**\n * NOTE: The type registry should be extended with all message types.\n */\nexport class Protocol {\n private readonly _typeRegistry: buf.Registry;\n\n constructor(types: buf.DescMessage[]) {\n this._typeRegistry = buf.createRegistry(...types);\n }\n\n get typeRegistry(): buf.Registry {\n return this._typeRegistry;\n }\n\n toJson(message: Message): any {\n try {\n return buf.toJson(MessageSchema, message, { registry: this.typeRegistry });\n } catch (err) {\n return { type: this.getPayloadType(message) };\n }\n }\n\n /**\n * Return the payload with the given type.\n */\n getPayload<Desc extends buf.DescMessage>(message: Message, type: Desc): buf.MessageShape<Desc> {\n invariant(message.payload);\n const payloadTypename = this.getPayloadType(message);\n if (type && type.typeName !== payloadTypename) {\n throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);\n }\n\n invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`);\n const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry) as buf.MessageShape<Desc>;\n invariant(payload, `Empty payload: ${payloadTypename}}`);\n return payload;\n }\n\n /**\n * Get the payload type.\n */\n getPayloadType(message: Message): string | undefined {\n if (!message.payload) {\n return undefined;\n }\n\n const [, type] = message.payload.typeUrl.split('/');\n return type;\n }\n\n /**\n * Create a packed message.\n */\n createMessage<Desc extends buf.DescMessage>(\n type: Desc,\n {\n source,\n target,\n payload,\n serviceId,\n }: {\n source?: PeerData;\n target?: PeerData[];\n payload?: buf.MessageInitShape<Desc>;\n serviceId?: string;\n },\n ) {\n return buf.create(MessageSchema, {\n timestamp: new Date().toISOString(),\n source,\n target,\n serviceId,\n payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : undefined,\n });\n }\n}\n\n/**\n * Convert websocket data to Uint8Array.\n */\nexport const toUint8Array = async (data: any): Promise<Uint8Array> => {\n // Node.\n if (data instanceof Buffer) {\n return bufferToArray(data);\n }\n\n // Browser.\n if (data instanceof Blob) {\n return new Uint8Array(await (data as Blob).arrayBuffer());\n }\n\n throw new Error(`Unexpected datatype: ${data}`);\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport class WebsocketClosedError extends Error {\n constructor() {\n super('WebSocket connection closed');\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { DeferredTask, sleep, synchronized } from '@dxos/async';\nimport { cancelWithContext, LifecycleState, Resource } from '@dxos/context';\nimport { warnAfterTimeout } from '@dxos/debug';\nimport { log } from '@dxos/log';\n\nconst INIT_RESTART_DELAY = 100;\nconst DEFAULT_MAX_RESTART_DELAY = 5000;\n\nexport type PersistentLifecycleParams = {\n /**\n * Create connection.\n * If promise resolves successfully, connection is considered established.\n */\n start: () => Promise<void>;\n\n /**\n * Reset connection to initial state.\n */\n stop: () => Promise<void>;\n\n /**\n * Called after successful start.\n */\n onRestart?: () => Promise<void>;\n\n /**\n * Maximum delay between restartion attempts.\n * Default: 5000ms\n */\n maxRestartDelay?: number;\n};\n\n/**\n * Handles restarts (e.g. persists connection).\n * Restarts are scheduled with exponential backoff.\n */\nexport class PersistentLifecycle extends Resource {\n private readonly _start: () => Promise<void>;\n private readonly _stop: () => Promise<void>;\n private readonly _onRestart?: () => Promise<void>;\n private readonly _maxRestartDelay: number;\n\n private _restartTask?: DeferredTask = undefined;\n private _restartAfter = 0;\n\n constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }: PersistentLifecycleParams) {\n super();\n this._start = start;\n this._stop = stop;\n this._onRestart = onRestart;\n this._maxRestartDelay = maxRestartDelay;\n }\n\n @synchronized\n protected override async _open() {\n this._restartTask = new DeferredTask(this._ctx, async () => {\n try {\n await this._restart();\n } catch (err) {\n log.warn('Restart failed', { err });\n this._restartTask?.schedule();\n }\n });\n await this._start().catch((err) => {\n log.warn('Start failed', { err });\n this._restartTask?.schedule();\n });\n }\n\n protected override async _close() {\n await this._restartTask?.join();\n await this._stop();\n this._restartTask = undefined;\n }\n\n private async _restart() {\n log(`restarting in ${this._restartAfter}ms`, { state: this._lifecycleState });\n await this._stop();\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n await cancelWithContext(this._ctx!, sleep(this._restartAfter));\n this._restartAfter = Math.min(Math.max(this._restartAfter * 2, INIT_RESTART_DELAY), this._maxRestartDelay);\n\n // May fail if the connection is not established.\n await warnAfterTimeout(5_000, 'Connection establishment takes too long', () => this._start());\n\n this._restartAfter = 0;\n await this._onRestart?.();\n }\n\n /**\n * Scheduling restart should be done from outside.\n */\n @synchronized\n scheduleRestart() {\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n this._restartTask!.schedule();\n }\n}\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../../../src/index.ts", "../../../src/edge-client.ts", "../../../src/errors.ts", "../../../src/persistent-lifecycle.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nexport * from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nexport * from './edge-client';\nexport * from './defs';\nexport * from './protocol';\nexport * from './errors';\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport WebSocket from 'isomorphic-ws';\n\nimport { Trigger, Event, scheduleTaskInterval, scheduleTask, TriggerState } from '@dxos/async';\nimport { Context, LifecycleState, Resource, type Lifecycle } from '@dxos/context';\nimport { log } from '@dxos/log';\nimport { buf } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { protocol } from './defs';\nimport { EdgeConnectionClosedError, EdgeIdentityChangedError } from './errors';\nimport { PersistentLifecycle } from './persistent-lifecycle';\nimport { type Protocol, toUint8Array } from './protocol';\n\nconst DEFAULT_TIMEOUT = 10_000;\nconst SIGNAL_KEEPALIVE_INTERVAL = 5_000;\n\nexport type MessageListener = (message: Message) => void | Promise<void>;\n\nexport interface EdgeConnection extends Required<Lifecycle> {\n connected: Event;\n reconnect: Event;\n\n get info(): any;\n get identityKey(): string;\n get peerKey(): string;\n get isOpen(): boolean;\n setIdentity(params: { peerKey: string; identityKey: string }): void;\n addListener(listener: MessageListener): () => void;\n send(message: Message): Promise<void>;\n}\n\nexport type MessengerConfig = {\n socketEndpoint: string;\n timeout?: number;\n protocol?: Protocol;\n};\n\n/**\n * Messenger client.\n */\nexport class EdgeClient extends Resource implements EdgeConnection {\n public readonly reconnect = new Event();\n public readonly connected = new Event();\n private readonly _persistentLifecycle = new PersistentLifecycle({\n start: async () => this._openWebSocket(),\n stop: async () => this._closeWebSocket(),\n onRestart: async () => this.reconnect.emit(),\n });\n\n private readonly _listeners = new Set<MessageListener>();\n private _ready = new Trigger();\n private _ws?: WebSocket = undefined;\n private _keepaliveCtx?: Context = undefined;\n private _heartBeatContext?: Context = undefined;\n\n constructor(\n private _identityKey: string,\n private _peerKey: string,\n private readonly _config: MessengerConfig,\n ) {\n super();\n }\n\n // TODO(burdon): Attach logging.\n public get info() {\n return {\n open: this.isOpen,\n identity: this._identityKey,\n device: this._peerKey,\n };\n }\n\n get identityKey() {\n return this._identityKey;\n }\n\n get peerKey() {\n return this._peerKey;\n }\n\n setIdentity({ peerKey, identityKey }: { peerKey: string; identityKey: string }) {\n this._peerKey = peerKey;\n this._identityKey = identityKey;\n this._persistentLifecycle.scheduleRestart();\n }\n\n public addListener(listener: MessageListener): () => void {\n this._listeners.add(listener);\n return () => this._listeners.delete(listener);\n }\n\n /**\n * Open connection to messaging service.\n */\n protected override async _open() {\n log('opening...', { info: this.info });\n this._persistentLifecycle.open().catch((err) => {\n log.warn('Error while opening connection', { err });\n });\n }\n\n /**\n * Close connection and free resources.\n */\n protected override async _close() {\n log('closing...', { peerKey: this._peerKey });\n await this._persistentLifecycle.close();\n }\n\n private async _openWebSocket() {\n const url = new URL(`/ws/${this._identityKey}/${this._peerKey}`, this._config.socketEndpoint);\n this._ws = new WebSocket(url);\n\n this._ws.onopen = () => {\n log('opened', this.info);\n this._ready.wake();\n this.connected.emit();\n };\n this._ws.onclose = () => {\n log('closed', this.info);\n this._persistentLifecycle.scheduleRestart();\n };\n this._ws.onerror = (event) => {\n log.warn('EdgeClient socket error', { error: event.error, info: event.message });\n this._persistentLifecycle.scheduleRestart();\n };\n /**\n * https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/data\n */\n this._ws.onmessage = async (event) => {\n if (event.data === '__pong__') {\n this._onHeartbeat();\n return;\n }\n const data = await toUint8Array(event.data);\n const message = buf.fromBinary(MessageSchema, data);\n log('received', { peerKey: this._peerKey, payload: protocol.getPayloadType(message) });\n if (message) {\n for (const listener of this._listeners) {\n try {\n await listener(message);\n } catch (err) {\n log.error('processing', { err, payload: protocol.getPayloadType(message) });\n }\n }\n }\n };\n\n await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });\n this._keepaliveCtx = new Context();\n scheduleTaskInterval(\n this._keepaliveCtx,\n async () => {\n // TODO(mykola): use RFC6455 ping/pong once implemented in the browser?\n // Cloudflare's worker responds to this `without interrupting hibernation`. https://developers.cloudflare.com/durable-objects/api/websockets/#setwebsocketautoresponse\n this._ws?.send('__ping__');\n },\n SIGNAL_KEEPALIVE_INTERVAL,\n );\n this._ws.send('__ping__');\n this._onHeartbeat();\n }\n\n private async _closeWebSocket() {\n if (!this._ws) {\n return;\n }\n try {\n this._ready.throw(this.isOpen ? new EdgeIdentityChangedError() : new EdgeConnectionClosedError());\n this._ready.reset();\n void this._keepaliveCtx?.dispose();\n this._keepaliveCtx = undefined;\n void this._heartBeatContext?.dispose();\n this._heartBeatContext = undefined;\n\n // NOTE: Remove event handlers to avoid scheduling restart.\n this._ws.onopen = () => {};\n this._ws.onclose = () => {};\n this._ws.onerror = () => {};\n this._ws.close();\n this._ws = undefined;\n } catch (err) {\n if (err instanceof Error && err.message.includes('WebSocket is closed before the connection is established.')) {\n return;\n }\n log.warn('Error closing websocket', { err });\n }\n }\n\n /**\n * Send message.\n * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.\n */\n public async send(message: Message): Promise<void> {\n if (this._ready.state !== TriggerState.RESOLVED) {\n log('waiting for websocket to become ready');\n await this._ready.wait({ timeout: this._config.timeout ?? DEFAULT_TIMEOUT });\n }\n if (!this._ws) {\n throw new EdgeConnectionClosedError();\n }\n if (\n message.source &&\n (message.source.peerKey !== this._peerKey || message.source.identityKey !== this.identityKey)\n ) {\n throw new EdgeIdentityChangedError();\n }\n\n log('sending...', { peerKey: this._peerKey, payload: protocol.getPayloadType(message) });\n this._ws.send(buf.toBinary(MessageSchema, message));\n }\n\n private _onHeartbeat() {\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n void this._heartBeatContext?.dispose();\n this._heartBeatContext = new Context();\n scheduleTask(\n this._heartBeatContext,\n () => {\n this._persistentLifecycle.scheduleRestart();\n },\n 2 * SIGNAL_KEEPALIVE_INTERVAL,\n );\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nexport class EdgeConnectionClosedError extends Error {\n constructor() {\n super('Edge connection closed.');\n }\n}\n\nexport class EdgeIdentityChangedError extends Error {\n constructor() {\n super('Edge identity changed.');\n }\n}\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { DeferredTask, sleep, synchronized } from '@dxos/async';\nimport { cancelWithContext, LifecycleState, Resource } from '@dxos/context';\nimport { warnAfterTimeout } from '@dxos/debug';\nimport { log } from '@dxos/log';\n\nconst INIT_RESTART_DELAY = 100;\nconst DEFAULT_MAX_RESTART_DELAY = 5000;\n\nexport type PersistentLifecycleParams = {\n /**\n * Create connection.\n * If promise resolves successfully, connection is considered established.\n */\n start: () => Promise<void>;\n\n /**\n * Reset connection to initial state.\n */\n stop: () => Promise<void>;\n\n /**\n * Called after successful start.\n */\n onRestart?: () => Promise<void>;\n\n /**\n * Maximum delay between restartion attempts.\n * Default: 5000ms\n */\n maxRestartDelay?: number;\n};\n\n/**\n * Handles restarts (e.g. persists connection).\n * Restarts are scheduled with exponential backoff.\n */\nexport class PersistentLifecycle extends Resource {\n private readonly _start: () => Promise<void>;\n private readonly _stop: () => Promise<void>;\n private readonly _onRestart?: () => Promise<void>;\n private readonly _maxRestartDelay: number;\n\n private _restartTask?: DeferredTask = undefined;\n private _restartAfter = 0;\n\n constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }: PersistentLifecycleParams) {\n super();\n this._start = start;\n this._stop = stop;\n this._onRestart = onRestart;\n this._maxRestartDelay = maxRestartDelay;\n }\n\n @synchronized\n protected override async _open() {\n this._restartTask = new DeferredTask(this._ctx, async () => {\n try {\n await this._restart();\n } catch (err) {\n log.warn('Restart failed', { err });\n this._restartTask?.schedule();\n }\n });\n await this._start().catch((err) => {\n log.warn('Start failed', { err });\n this._restartTask?.schedule();\n });\n }\n\n protected override async _close() {\n await this._restartTask?.join();\n await this._stop();\n this._restartTask = undefined;\n }\n\n private async _restart() {\n log(`restarting in ${this._restartAfter}ms`, { state: this._lifecycleState });\n await this._stop();\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n await cancelWithContext(this._ctx!, sleep(this._restartAfter));\n this._restartAfter = Math.min(Math.max(this._restartAfter * 2, INIT_RESTART_DELAY), this._maxRestartDelay);\n\n // May fail if the connection is not established.\n await warnAfterTimeout(5_000, 'Connection establishment takes too long', () => this._start());\n\n this._restartAfter = 0;\n await this._onRestart?.();\n }\n\n /**\n * Scheduling restart should be done from outside.\n */\n @synchronized\n scheduleRestart() {\n if (this._lifecycleState !== LifecycleState.OPEN) {\n return;\n }\n this._restartTask!.schedule();\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,yBAAc;ACAd,2BAAsB;AAEtB,mBAAiF;AACjF,qBAAkE;AAClE,iBAAoB;AACpB,iBAAoB;AACpB,0BAA4C;AEN5C,IAAAA,gBAAkD;AAClD,IAAAC,kBAA4D;AAC5D,mBAAiC;AACjC,IAAAC,cAAoB;ADHb,IAAMC,4BAAN,cAAwCC,MAAAA;EAC7CC,cAAc;AACZ,UAAM,yBAAA;EACR;AACF;AAEO,IAAMC,2BAAN,cAAuCF,MAAAA;EAC5CC,cAAc;AACZ,UAAM,wBAAA;EACR;AACF;;;;;;;;ACLA,IAAME,qBAAqB;AAC3B,IAAMC,4BAA4B;AA8B3B,IAAMC,sBAAN,cAAkCC,yBAAAA;EASvCL,YAAY,EAAEM,OAAOC,MAAMC,WAAWC,kBAAkBN,0BAAyB,GAA+B;AAC9G,UAAK;AAJCO,SAAAA,eAA8BC;AAC9BC,SAAAA,gBAAgB;AAItB,SAAKC,SAASP;AACd,SAAKQ,QAAQP;AACb,SAAKQ,aAAaP;AAClB,SAAKQ,mBAAmBP;EAC1B;EAEA,MACyBQ,QAAQ;AAC/B,SAAKP,eAAe,IAAIQ,2BAAa,KAAKC,MAAM,YAAA;AAC9C,UAAI;AACF,cAAM,KAAKC,SAAQ;MACrB,SAASC,KAAK;AACZC,wBAAIC,KAAK,kBAAkB;UAAEF;QAAI,GAAA;;;;;;AACjC,aAAKX,cA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AAA;IAAS,GAAA;;;;;;AACtF,SAAK1B,IAAIyC,KAAKV,eAAIkB,SAAShB,mCAAeP,OAAAA,CAAAA;EAC5C;EAEQG,eAAe;AACrB,QAAI,KAAKzD,oBAAoBC,eAAAA,eAAeC,MAAM;AAChD;IACF;AACA,SAAK,KAAK4B,mBAAmB0C,QAAAA;AAC7B,SAAK1C,oBAAoB,IAAIqC,uBAAAA,QAAAA;;;;AAC7BW,mCACE,KAAKhD,mBACL,MAAA;AACE,WAAKV,qBAAqBX,gBAAe;IAC3C,GACA,IAAIG,yBAAAA;EAER;AACF;",
|
|
6
|
+
"names": ["import_async", "import_context", "import_log", "EdgeConnectionClosedError", "Error", "constructor", "EdgeIdentityChangedError", "INIT_RESTART_DELAY", "DEFAULT_MAX_RESTART_DELAY", "PersistentLifecycle", "Resource", "start", "stop", "onRestart", "maxRestartDelay", "_restartTask", "undefined", "_restartAfter", "_start", "_stop", "_onRestart", "_maxRestartDelay", "_open", "DeferredTask", "_ctx", "_restart", "err", "log", "warn", "schedule", "catch", "_close", "join", "state", "_lifecycleState", "LifecycleState", "OPEN", "cancelWithContext", "sleep", "Math", "min", "max", "warnAfterTimeout", "scheduleRestart", "synchronized", "DEFAULT_TIMEOUT", "SIGNAL_KEEPALIVE_INTERVAL", "EdgeClient", "_identityKey", "_peerKey", "_config", "reconnect", "Event", "connected", "_persistentLifecycle", "_openWebSocket", "_closeWebSocket", "emit", "_listeners", "Set", "_ready", "Trigger", "_ws", "_keepaliveCtx", "_heartBeatContext", "info", "open", "isOpen", "identity", "device", "identityKey", "peerKey", "setIdentity", "addListener", "listener", "add", "delete", "close", "url", "URL", "socketEndpoint", "WebSocket", "onopen", "wake", "onclose", "onerror", "event", "error", "message", "onmessage", "data", "_onHeartbeat", "toUint8Array", "buf", "fromBinary", "MessageSchema", "payload", "protocol", "getPayloadType", "wait", "timeout", "Context", "scheduleTaskInterval", "send", "throw", "reset", "dispose", "includes", "TriggerState", "RESOLVED", "source", "toBinary", "scheduleTask"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/core/mesh/edge-client/src/protocol.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/core/mesh/edge-client/src/protocol.ts":{"bytes":10334,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"packages/core/mesh/edge-client/src/defs.ts":{"bytes":1607,"imports":[{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"packages/core/mesh/edge-client/src/protocol.ts","kind":"import-statement","original":"./protocol"}],"format":"esm"},"packages/core/mesh/edge-client/src/errors.ts":{"bytes":1265,"imports":[],"format":"esm"},"packages/core/mesh/edge-client/src/persistent-lifecycle.ts":{"bytes":10868,"imports":[{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"format":"esm"},"packages/core/mesh/edge-client/src/edge-client.ts":{"bytes":27085,"imports":[{"path":"isomorphic-ws","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"packages/core/mesh/edge-client/src/defs.ts","kind":"import-statement","original":"./defs"},{"path":"packages/core/mesh/edge-client/src/errors.ts","kind":"import-statement","original":"./errors"},{"path":"packages/core/mesh/edge-client/src/persistent-lifecycle.ts","kind":"import-statement","original":"./persistent-lifecycle"},{"path":"packages/core/mesh/edge-client/src/protocol.ts","kind":"import-statement","original":"./protocol"}],"format":"esm"},"packages/core/mesh/edge-client/src/index.ts":{"bytes":935,"imports":[{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"packages/core/mesh/edge-client/src/edge-client.ts","kind":"import-statement","original":"./edge-client"},{"path":"packages/core/mesh/edge-client/src/defs.ts","kind":"import-statement","original":"./defs"},{"path":"packages/core/mesh/edge-client/src/protocol.ts","kind":"import-statement","original":"./protocol"},{"path":"packages/core/mesh/edge-client/src/errors.ts","kind":"import-statement","original":"./errors"}],"format":"esm"},"packages/core/mesh/edge-client/src/testing/test-utils.ts":{"bytes":12455,"imports":[{"path":"isomorphic-ws","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"packages/core/mesh/edge-client/src/defs.ts","kind":"import-statement","original":"../defs"},{"path":"packages/core/mesh/edge-client/src/protocol.ts","kind":"import-statement","original":"../protocol"}],"format":"esm"},"packages/core/mesh/edge-client/src/testing/index.ts":{"bytes":516,"imports":[{"path":"packages/core/mesh/edge-client/src/testing/test-utils.ts","kind":"import-statement","original":"./test-utils"}],"format":"esm"}},"outputs":{"packages/core/mesh/edge-client/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":18217},"packages/core/mesh/edge-client/dist/lib/node/index.cjs":{"imports":[{"path":"packages/core/mesh/edge-client/dist/lib/node/chunk-ANV2HBEH.cjs","kind":"import-statement"},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"isomorphic-ws","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/context","kind":"import-statement","external":true},{"path":"@dxos/debug","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true}],"exports":["EdgeClient","EdgeConnectionClosedError","EdgeIdentityChangedError","Protocol","getTypename","protocol","toUint8Array"],"entryPoint":"packages/core/mesh/edge-client/src/index.ts","inputs":{"packages/core/mesh/edge-client/src/index.ts":{"bytesInOutput":60},"packages/core/mesh/edge-client/src/edge-client.ts":{"bytesInOutput":6943},"packages/core/mesh/edge-client/src/errors.ts":{"bytesInOutput":232},"packages/core/mesh/edge-client/src/persistent-lifecycle.ts":{"bytesInOutput":3015}},"bytes":10784},"packages/core/mesh/edge-client/dist/lib/node/testing/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":6341},"packages/core/mesh/edge-client/dist/lib/node/testing/index.cjs":{"imports":[{"path":"packages/core/mesh/edge-client/dist/lib/node/chunk-ANV2HBEH.cjs","kind":"import-statement"},{"path":"isomorphic-ws","kind":"import-statement","external":true},{"path":"@dxos/async","kind":"import-statement","external":true},{"path":"@dxos/log","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true}],"exports":["DEFAULT_PORT","createTestEdgeWsServer"],"entryPoint":"packages/core/mesh/edge-client/src/testing/index.ts","inputs":{"packages/core/mesh/edge-client/src/testing/test-utils.ts":{"bytesInOutput":3276},"packages/core/mesh/edge-client/src/testing/index.ts":{"bytesInOutput":0}},"bytes":3493},"packages/core/mesh/edge-client/dist/lib/node/chunk-ANV2HBEH.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":5676},"packages/core/mesh/edge-client/dist/lib/node/chunk-ANV2HBEH.cjs":{"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf","kind":"import-statement","external":true},{"path":"@dxos/protocols/buf/dxos/edge/messenger_pb","kind":"import-statement","external":true}],"exports":["Protocol","getTypename","protocol","toUint8Array"],"inputs":{"packages/core/mesh/edge-client/src/protocol.ts":{"bytesInOutput":2600},"packages/core/mesh/edge-client/src/defs.ts":{"bytesInOutput":298}},"bytes":3106}}}
|