@dxos/edge-client 0.8.3 → 0.8.4-main.05e74ebcff

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.
Files changed (75) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/{browser/chunk-VHS3XEIX.mjs → neutral/chunk-ZIQ5T3A7.mjs} +20 -50
  3. package/dist/lib/{browser/chunk-VHS3XEIX.mjs.map → neutral/chunk-ZIQ5T3A7.mjs.map} +3 -3
  4. package/dist/lib/{browser → neutral}/edge-ws-muxer.mjs +1 -1
  5. package/dist/lib/neutral/index.mjs +1238 -0
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/lib/{browser → neutral}/testing/index.mjs +53 -33
  9. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  10. package/dist/types/src/auth.d.ts.map +1 -1
  11. package/dist/types/src/edge-client.d.ts +18 -15
  12. package/dist/types/src/edge-client.d.ts.map +1 -1
  13. package/dist/types/src/edge-http-client.d.ts +132 -37
  14. package/dist/types/src/edge-http-client.d.ts.map +1 -1
  15. package/dist/types/src/edge-http-client.test.d.ts +2 -0
  16. package/dist/types/src/edge-http-client.test.d.ts.map +1 -0
  17. package/dist/types/src/edge-identity.d.ts.map +1 -1
  18. package/dist/types/src/edge-ws-connection.d.ts +21 -0
  19. package/dist/types/src/edge-ws-connection.d.ts.map +1 -1
  20. package/dist/types/src/edge-ws-muxer.d.ts.map +1 -1
  21. package/dist/types/src/errors.d.ts.map +1 -1
  22. package/dist/types/src/http-client.d.ts +25 -0
  23. package/dist/types/src/http-client.d.ts.map +1 -0
  24. package/dist/types/src/http-client.test.d.ts +2 -0
  25. package/dist/types/src/http-client.test.d.ts.map +1 -0
  26. package/dist/types/src/index.d.ts +4 -3
  27. package/dist/types/src/index.d.ts.map +1 -1
  28. package/dist/types/src/protocol.d.ts +1 -1
  29. package/dist/types/src/protocol.d.ts.map +1 -1
  30. package/dist/types/src/testing/index.d.ts +1 -0
  31. package/dist/types/src/testing/index.d.ts.map +1 -1
  32. package/dist/types/src/testing/test-server.d.ts +9 -0
  33. package/dist/types/src/testing/test-server.d.ts.map +1 -0
  34. package/dist/types/src/testing/test-utils.d.ts +3 -3
  35. package/dist/types/src/testing/test-utils.d.ts.map +1 -1
  36. package/dist/types/src/utils.d.ts +1 -1
  37. package/dist/types/src/utils.d.ts.map +1 -1
  38. package/dist/types/tsconfig.tsbuildinfo +1 -1
  39. package/package.json +34 -30
  40. package/src/edge-client.test.ts +20 -15
  41. package/src/edge-client.ts +90 -43
  42. package/src/edge-http-client.test.ts +23 -0
  43. package/src/edge-http-client.ts +606 -161
  44. package/src/edge-ws-connection.ts +131 -9
  45. package/src/edge-ws-muxer.ts +1 -1
  46. package/src/http-client.test.ts +58 -0
  47. package/src/http-client.ts +77 -0
  48. package/src/index.ts +4 -3
  49. package/src/testing/index.ts +1 -0
  50. package/src/testing/test-server.ts +45 -0
  51. package/src/testing/test-utils.ts +9 -9
  52. package/src/websocket.test.ts +1 -1
  53. package/dist/lib/browser/index.mjs +0 -1034
  54. package/dist/lib/browser/index.mjs.map +0 -7
  55. package/dist/lib/browser/meta.json +0 -1
  56. package/dist/lib/browser/testing/index.mjs.map +0 -7
  57. package/dist/lib/node/chunk-XNHBUTNB.cjs +0 -317
  58. package/dist/lib/node/chunk-XNHBUTNB.cjs.map +0 -7
  59. package/dist/lib/node/edge-ws-muxer.cjs +0 -33
  60. package/dist/lib/node/edge-ws-muxer.cjs.map +0 -7
  61. package/dist/lib/node/index.cjs +0 -1060
  62. package/dist/lib/node/index.cjs.map +0 -7
  63. package/dist/lib/node/meta.json +0 -1
  64. package/dist/lib/node/testing/index.cjs +0 -169
  65. package/dist/lib/node/testing/index.cjs.map +0 -7
  66. package/dist/lib/node-esm/chunk-HGQUUFIJ.mjs +0 -299
  67. package/dist/lib/node-esm/chunk-HGQUUFIJ.mjs.map +0 -7
  68. package/dist/lib/node-esm/edge-ws-muxer.mjs +0 -12
  69. package/dist/lib/node-esm/edge-ws-muxer.mjs.map +0 -7
  70. package/dist/lib/node-esm/index.mjs +0 -1035
  71. package/dist/lib/node-esm/index.mjs.map +0 -7
  72. package/dist/lib/node-esm/meta.json +0 -1
  73. package/dist/lib/node-esm/testing/index.mjs +0 -141
  74. package/dist/lib/node-esm/testing/index.mjs.map +0 -7
  75. /package/dist/lib/{browser → neutral}/edge-ws-muxer.mjs.map +0 -0
@@ -1,1034 +0,0 @@
1
- import {
2
- CLOUDFLARE_MESSAGE_MAX_BYTES,
3
- CLOUDFLARE_RPC_MAX_BYTES,
4
- Protocol,
5
- WebSocketMuxer,
6
- getTypename,
7
- protocol,
8
- toUint8Array
9
- } from "./chunk-VHS3XEIX.mjs";
10
-
11
- // packages/core/mesh/edge-client/src/index.ts
12
- export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
13
-
14
- // packages/core/mesh/edge-client/src/edge-client.ts
15
- import { Trigger, scheduleMicroTask, TriggerState, PersistentLifecycle, Event } from "@dxos/async";
16
- import { Resource as Resource2 } from "@dxos/context";
17
- import { log as log2, logInfo as logInfo2 } from "@dxos/log";
18
- import { EdgeStatus } from "@dxos/protocols/proto/dxos/client/services";
19
-
20
- // packages/core/mesh/edge-client/src/edge-identity.ts
21
- import { invariant } from "@dxos/invariant";
22
- import { schema } from "@dxos/protocols/proto";
23
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-identity.ts";
24
- var handleAuthChallenge = async (failedResponse, identity) => {
25
- invariant(failedResponse.status === 401, void 0, {
26
- F: __dxlog_file,
27
- L: 21,
28
- S: void 0,
29
- A: [
30
- "failedResponse.status === 401",
31
- ""
32
- ]
33
- });
34
- const headerValue = failedResponse.headers.get("Www-Authenticate");
35
- invariant(headerValue?.startsWith("VerifiablePresentation challenge="), void 0, {
36
- F: __dxlog_file,
37
- L: 24,
38
- S: void 0,
39
- A: [
40
- "headerValue?.startsWith('VerifiablePresentation challenge=')",
41
- ""
42
- ]
43
- });
44
- const challenge = headerValue?.slice("VerifiablePresentation challenge=".length);
45
- invariant(challenge, void 0, {
46
- F: __dxlog_file,
47
- L: 27,
48
- S: void 0,
49
- A: [
50
- "challenge",
51
- ""
52
- ]
53
- });
54
- const presentation = await identity.presentCredentials({
55
- challenge: Buffer.from(challenge, "base64")
56
- });
57
- return schema.getCodecForType("dxos.halo.credentials.Presentation").encode(presentation);
58
- };
59
-
60
- // packages/core/mesh/edge-client/src/edge-ws-connection.ts
61
- import WebSocket from "isomorphic-ws";
62
- import { scheduleTask, scheduleTaskInterval } from "@dxos/async";
63
- import { Context, Resource } from "@dxos/context";
64
- import { invariant as invariant2 } from "@dxos/invariant";
65
- import { log, logInfo } from "@dxos/log";
66
- import { EdgeWebsocketProtocol } from "@dxos/protocols";
67
- import { buf } from "@dxos/protocols/buf";
68
- import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
69
- function _ts_decorate(decorators, target, key, desc) {
70
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
71
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
72
- 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;
73
- return c > 3 && r && Object.defineProperty(target, key, r), r;
74
- }
75
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-ws-connection.ts";
76
- var SIGNAL_KEEPALIVE_INTERVAL = 4e3;
77
- var SIGNAL_KEEPALIVE_TIMEOUT = 12e3;
78
- var EdgeWsConnection = class extends Resource {
79
- constructor(_identity, _connectionInfo, _callbacks) {
80
- super(), this._identity = _identity, this._connectionInfo = _connectionInfo, this._callbacks = _callbacks;
81
- }
82
- get info() {
83
- return {
84
- open: this.isOpen,
85
- identity: this._identity.identityKey,
86
- device: this._identity.peerKey
87
- };
88
- }
89
- send(message) {
90
- invariant2(this._ws, void 0, {
91
- F: __dxlog_file2,
92
- L: 52,
93
- S: this,
94
- A: [
95
- "this._ws",
96
- ""
97
- ]
98
- });
99
- invariant2(this._wsMuxer, void 0, {
100
- F: __dxlog_file2,
101
- L: 53,
102
- S: this,
103
- A: [
104
- "this._wsMuxer",
105
- ""
106
- ]
107
- });
108
- log("sending...", {
109
- peerKey: this._identity.peerKey,
110
- payload: protocol.getPayloadType(message)
111
- }, {
112
- F: __dxlog_file2,
113
- L: 54,
114
- S: this,
115
- C: (f, a) => f(...a)
116
- });
117
- if (this._ws?.protocol.includes(EdgeWebsocketProtocol.V0)) {
118
- const binary = buf.toBinary(MessageSchema, message);
119
- if (binary.length > CLOUDFLARE_MESSAGE_MAX_BYTES) {
120
- log.error("Message dropped because it was too large (>1MB).", {
121
- byteLength: binary.byteLength,
122
- serviceId: message.serviceId,
123
- payload: protocol.getPayloadType(message)
124
- }, {
125
- F: __dxlog_file2,
126
- L: 58,
127
- S: this,
128
- C: (f, a) => f(...a)
129
- });
130
- return;
131
- }
132
- this._ws.send(binary);
133
- } else {
134
- this._wsMuxer.send(message).catch((e) => log.catch(e, void 0, {
135
- F: __dxlog_file2,
136
- L: 67,
137
- S: this,
138
- C: (f, a) => f(...a)
139
- }));
140
- }
141
- }
142
- async _open() {
143
- const baseProtocols = [
144
- ...Object.values(EdgeWebsocketProtocol)
145
- ];
146
- this._ws = new WebSocket(this._connectionInfo.url.toString(), this._connectionInfo.protocolHeader ? [
147
- ...baseProtocols,
148
- this._connectionInfo.protocolHeader
149
- ] : [
150
- ...baseProtocols
151
- ]);
152
- const muxer = new WebSocketMuxer(this._ws);
153
- this._wsMuxer = muxer;
154
- this._ws.onopen = () => {
155
- if (this.isOpen) {
156
- log("connected", void 0, {
157
- F: __dxlog_file2,
158
- L: 84,
159
- S: this,
160
- C: (f, a) => f(...a)
161
- });
162
- this._callbacks.onConnected();
163
- this._scheduleHeartbeats();
164
- } else {
165
- log.verbose("connected after becoming inactive", {
166
- currentIdentity: this._identity
167
- }, {
168
- F: __dxlog_file2,
169
- L: 88,
170
- S: this,
171
- C: (f, a) => f(...a)
172
- });
173
- }
174
- };
175
- this._ws.onclose = (event) => {
176
- if (this.isOpen) {
177
- log.warn("disconnected while being open", {
178
- code: event.code,
179
- reason: event.reason
180
- }, {
181
- F: __dxlog_file2,
182
- L: 93,
183
- S: this,
184
- C: (f, a) => f(...a)
185
- });
186
- this._callbacks.onRestartRequired();
187
- muxer.destroy();
188
- }
189
- };
190
- this._ws.onerror = (event) => {
191
- if (this.isOpen) {
192
- log.warn("edge connection socket error", {
193
- error: event.error,
194
- info: event.message
195
- }, {
196
- F: __dxlog_file2,
197
- L: 100,
198
- S: this,
199
- C: (f, a) => f(...a)
200
- });
201
- this._callbacks.onRestartRequired();
202
- } else {
203
- log.verbose("error ignored on closed connection", {
204
- error: event.error
205
- }, {
206
- F: __dxlog_file2,
207
- L: 103,
208
- S: this,
209
- C: (f, a) => f(...a)
210
- });
211
- }
212
- };
213
- this._ws.onmessage = async (event) => {
214
- if (!this.isOpen) {
215
- log.verbose("message ignored on closed connection", {
216
- event: event.type
217
- }, {
218
- F: __dxlog_file2,
219
- L: 111,
220
- S: this,
221
- C: (f, a) => f(...a)
222
- });
223
- return;
224
- }
225
- if (event.data === "__pong__") {
226
- this._rescheduleHeartbeatTimeout();
227
- return;
228
- }
229
- const bytes = await toUint8Array(event.data);
230
- if (!this.isOpen) {
231
- return;
232
- }
233
- const message = this._ws?.protocol?.includes(EdgeWebsocketProtocol.V0) ? buf.fromBinary(MessageSchema, bytes) : muxer.receiveData(bytes);
234
- if (message) {
235
- log("received", {
236
- from: message.source,
237
- payload: protocol.getPayloadType(message)
238
- }, {
239
- F: __dxlog_file2,
240
- L: 128,
241
- S: this,
242
- C: (f, a) => f(...a)
243
- });
244
- this._callbacks.onMessage(message);
245
- }
246
- };
247
- }
248
- async _close() {
249
- void this._inactivityTimeoutCtx?.dispose().catch(() => {
250
- });
251
- try {
252
- this._ws?.close();
253
- this._ws = void 0;
254
- this._wsMuxer?.destroy();
255
- this._wsMuxer = void 0;
256
- } catch (err) {
257
- if (err instanceof Error && err.message.includes("WebSocket is closed before the connection is established.")) {
258
- return;
259
- }
260
- log.warn("Error closing websocket", {
261
- err
262
- }, {
263
- F: __dxlog_file2,
264
- L: 146,
265
- S: this,
266
- C: (f, a) => f(...a)
267
- });
268
- }
269
- }
270
- _scheduleHeartbeats() {
271
- invariant2(this._ws, void 0, {
272
- F: __dxlog_file2,
273
- L: 151,
274
- S: this,
275
- A: [
276
- "this._ws",
277
- ""
278
- ]
279
- });
280
- scheduleTaskInterval(this._ctx, async () => {
281
- this._ws?.send("__ping__");
282
- }, SIGNAL_KEEPALIVE_INTERVAL);
283
- this._ws.send("__ping__");
284
- this._rescheduleHeartbeatTimeout();
285
- }
286
- _rescheduleHeartbeatTimeout() {
287
- if (!this.isOpen) {
288
- return;
289
- }
290
- void this._inactivityTimeoutCtx?.dispose();
291
- this._inactivityTimeoutCtx = new Context(void 0, {
292
- F: __dxlog_file2,
293
- L: 170
294
- });
295
- scheduleTask(this._inactivityTimeoutCtx, () => {
296
- if (this.isOpen) {
297
- log.warn("restart due to inactivity timeout", void 0, {
298
- F: __dxlog_file2,
299
- L: 175,
300
- S: this,
301
- C: (f, a) => f(...a)
302
- });
303
- this._callbacks.onRestartRequired();
304
- }
305
- }, SIGNAL_KEEPALIVE_TIMEOUT);
306
- }
307
- };
308
- _ts_decorate([
309
- logInfo
310
- ], EdgeWsConnection.prototype, "info", null);
311
-
312
- // packages/core/mesh/edge-client/src/errors.ts
313
- var EdgeConnectionClosedError = class extends Error {
314
- constructor() {
315
- super("Edge connection closed.");
316
- }
317
- };
318
- var EdgeIdentityChangedError = class extends Error {
319
- constructor() {
320
- super("Edge identity changed.");
321
- }
322
- };
323
-
324
- // packages/core/mesh/edge-client/src/utils.ts
325
- var getEdgeUrlWithProtocol = (baseUrl, protocol2) => {
326
- const isSecure = baseUrl.startsWith("https") || baseUrl.startsWith("wss");
327
- const url = new URL(baseUrl);
328
- url.protocol = protocol2 + (isSecure ? "s" : "");
329
- return url.toString();
330
- };
331
-
332
- // packages/core/mesh/edge-client/src/edge-client.ts
333
- function _ts_decorate2(decorators, target, key, desc) {
334
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
335
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
336
- 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;
337
- return c > 3 && r && Object.defineProperty(target, key, r), r;
338
- }
339
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-client.ts";
340
- var DEFAULT_TIMEOUT = 1e4;
341
- var EdgeClient = class extends Resource2 {
342
- constructor(_identity, _config) {
343
- super(), this._identity = _identity, this._config = _config, this.statusChanged = new Event(), this._persistentLifecycle = new PersistentLifecycle({
344
- start: async () => this._connect(),
345
- stop: async (state) => this._disconnect(state)
346
- }), this._messageListeners = /* @__PURE__ */ new Set(), this._reconnectListeners = /* @__PURE__ */ new Set(), this._currentConnection = void 0, this._ready = new Trigger(), this._isActive = (connection) => connection === this._currentConnection;
347
- this._baseWsUrl = getEdgeUrlWithProtocol(_config.socketEndpoint, "ws");
348
- this._baseHttpUrl = getEdgeUrlWithProtocol(_config.socketEndpoint, "http");
349
- }
350
- get info() {
351
- return {
352
- open: this.isOpen,
353
- status: this.status,
354
- identity: this._identity.identityKey,
355
- device: this._identity.peerKey
356
- };
357
- }
358
- get status() {
359
- return Boolean(this._currentConnection) && this._ready.state === TriggerState.RESOLVED ? EdgeStatus.CONNECTED : EdgeStatus.NOT_CONNECTED;
360
- }
361
- get identityKey() {
362
- return this._identity.identityKey;
363
- }
364
- get peerKey() {
365
- return this._identity.peerKey;
366
- }
367
- setIdentity(identity) {
368
- if (identity.identityKey !== this._identity.identityKey || identity.peerKey !== this._identity.peerKey) {
369
- log2("Edge identity changed", {
370
- identity,
371
- oldIdentity: this._identity
372
- }, {
373
- F: __dxlog_file3,
374
- L: 99,
375
- S: this,
376
- C: (f, a) => f(...a)
377
- });
378
- this._identity = identity;
379
- this._closeCurrentConnection(new EdgeIdentityChangedError());
380
- void this._persistentLifecycle.scheduleRestart();
381
- }
382
- }
383
- onMessage(listener) {
384
- this._messageListeners.add(listener);
385
- return () => this._messageListeners.delete(listener);
386
- }
387
- onReconnected(listener) {
388
- this._reconnectListeners.add(listener);
389
- if (this._ready.state === TriggerState.RESOLVED) {
390
- scheduleMicroTask(this._ctx, () => {
391
- if (this._reconnectListeners.has(listener)) {
392
- try {
393
- listener();
394
- } catch (error) {
395
- log2.catch(error, void 0, {
396
- F: __dxlog_file3,
397
- L: 121,
398
- S: this,
399
- C: (f, a) => f(...a)
400
- });
401
- }
402
- }
403
- });
404
- }
405
- return () => this._reconnectListeners.delete(listener);
406
- }
407
- /**
408
- * Open connection to messaging service.
409
- */
410
- async _open() {
411
- log2("opening...", {
412
- info: this.info
413
- }, {
414
- F: __dxlog_file3,
415
- L: 133,
416
- S: this,
417
- C: (f, a) => f(...a)
418
- });
419
- this._persistentLifecycle.open().catch((err) => {
420
- log2.warn("Error while opening connection", {
421
- err
422
- }, {
423
- F: __dxlog_file3,
424
- L: 135,
425
- S: this,
426
- C: (f, a) => f(...a)
427
- });
428
- });
429
- }
430
- /**
431
- * Close connection and free resources.
432
- */
433
- async _close() {
434
- log2("closing...", {
435
- peerKey: this._identity.peerKey
436
- }, {
437
- F: __dxlog_file3,
438
- L: 143,
439
- S: this,
440
- C: (f, a) => f(...a)
441
- });
442
- this._closeCurrentConnection();
443
- await this._persistentLifecycle.close();
444
- }
445
- async _connect() {
446
- if (this._ctx.disposed) {
447
- return void 0;
448
- }
449
- const identity = this._identity;
450
- const path = `/ws/${identity.identityKey}/${identity.peerKey}`;
451
- const protocolHeader = this._config.disableAuth ? void 0 : await this._createAuthHeader(path);
452
- if (this._identity !== identity) {
453
- log2("identity changed during auth header request", void 0, {
454
- F: __dxlog_file3,
455
- L: 157,
456
- S: this,
457
- C: (f, a) => f(...a)
458
- });
459
- return void 0;
460
- }
461
- const restartRequired = new Trigger();
462
- const url = new URL(path, this._baseWsUrl);
463
- log2("Opening websocket", {
464
- url: url.toString(),
465
- protocolHeader
466
- }, {
467
- F: __dxlog_file3,
468
- L: 163,
469
- S: this,
470
- C: (f, a) => f(...a)
471
- });
472
- const connection = new EdgeWsConnection(identity, {
473
- url,
474
- protocolHeader
475
- }, {
476
- onConnected: () => {
477
- if (this._isActive(connection)) {
478
- this._ready.wake();
479
- this._notifyReconnected();
480
- } else {
481
- log2.verbose("connected callback ignored, because connection is not active", void 0, {
482
- F: __dxlog_file3,
483
- L: 173,
484
- S: this,
485
- C: (f, a) => f(...a)
486
- });
487
- }
488
- },
489
- onRestartRequired: () => {
490
- if (this._isActive(connection)) {
491
- this._closeCurrentConnection();
492
- void this._persistentLifecycle.scheduleRestart();
493
- } else {
494
- log2.verbose("restart requested by inactive connection", void 0, {
495
- F: __dxlog_file3,
496
- L: 181,
497
- S: this,
498
- C: (f, a) => f(...a)
499
- });
500
- }
501
- restartRequired.wake();
502
- },
503
- onMessage: (message) => {
504
- if (this._isActive(connection)) {
505
- this._notifyMessageReceived(message);
506
- } else {
507
- log2.verbose("ignored a message on inactive connection", {
508
- from: message.source,
509
- type: message.payload?.typeUrl
510
- }, {
511
- F: __dxlog_file3,
512
- L: 189,
513
- S: this,
514
- C: (f, a) => f(...a)
515
- });
516
- }
517
- }
518
- });
519
- this._currentConnection = connection;
520
- await connection.open();
521
- await Promise.race([
522
- this._ready.wait({
523
- timeout: this._config.timeout ?? DEFAULT_TIMEOUT
524
- }),
525
- restartRequired
526
- ]);
527
- return connection;
528
- }
529
- async _disconnect(state) {
530
- await state.close();
531
- this.statusChanged.emit(this.status);
532
- }
533
- _closeCurrentConnection(error = new EdgeConnectionClosedError()) {
534
- this._currentConnection = void 0;
535
- this._ready.throw(error);
536
- this._ready.reset();
537
- this.statusChanged.emit(this.status);
538
- }
539
- _notifyReconnected() {
540
- this.statusChanged.emit(this.status);
541
- for (const listener of this._reconnectListeners) {
542
- try {
543
- listener();
544
- } catch (err) {
545
- log2.error("ws reconnect listener failed", {
546
- err
547
- }, {
548
- F: __dxlog_file3,
549
- L: 225,
550
- S: this,
551
- C: (f, a) => f(...a)
552
- });
553
- }
554
- }
555
- }
556
- _notifyMessageReceived(message) {
557
- for (const listener of this._messageListeners) {
558
- try {
559
- listener(message);
560
- } catch (err) {
561
- log2.error("ws incoming message processing failed", {
562
- err,
563
- payload: protocol.getPayloadType(message)
564
- }, {
565
- F: __dxlog_file3,
566
- L: 235,
567
- S: this,
568
- C: (f, a) => f(...a)
569
- });
570
- }
571
- }
572
- }
573
- /**
574
- * Send message.
575
- * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.
576
- */
577
- async send(message) {
578
- if (this._ready.state !== TriggerState.RESOLVED) {
579
- log2("waiting for websocket to become ready", void 0, {
580
- F: __dxlog_file3,
581
- L: 246,
582
- S: this,
583
- C: (f, a) => f(...a)
584
- });
585
- await this._ready.wait({
586
- timeout: this._config.timeout ?? DEFAULT_TIMEOUT
587
- });
588
- }
589
- if (!this._currentConnection) {
590
- throw new EdgeConnectionClosedError();
591
- }
592
- if (message.source && (message.source.peerKey !== this._identity.peerKey || message.source.identityKey !== this.identityKey)) {
593
- throw new EdgeIdentityChangedError();
594
- }
595
- this._currentConnection.send(message);
596
- }
597
- async _createAuthHeader(path) {
598
- const httpUrl = new URL(path, this._baseHttpUrl);
599
- httpUrl.protocol = getEdgeUrlWithProtocol(this._baseWsUrl.toString(), "http");
600
- const response = await fetch(httpUrl, {
601
- method: "GET"
602
- });
603
- if (response.status === 401) {
604
- return encodePresentationWsAuthHeader(await handleAuthChallenge(response, this._identity));
605
- } else {
606
- log2.warn("no auth challenge from edge", {
607
- status: response.status,
608
- statusText: response.statusText
609
- }, {
610
- F: __dxlog_file3,
611
- L: 271,
612
- S: this,
613
- C: (f, a) => f(...a)
614
- });
615
- return void 0;
616
- }
617
- }
618
- };
619
- _ts_decorate2([
620
- logInfo2
621
- ], EdgeClient.prototype, "info", null);
622
- var encodePresentationWsAuthHeader = (encodedPresentation) => {
623
- const encodedToken = Buffer.from(encodedPresentation).toString("base64").replace(/=*$/, "").replaceAll("/", "|");
624
- return `base64url.bearer.authorization.dxos.org.${encodedToken}`;
625
- };
626
-
627
- // packages/core/mesh/edge-client/src/auth.ts
628
- import { createCredential, signPresentation } from "@dxos/credentials";
629
- import { invariant as invariant3 } from "@dxos/invariant";
630
- import { Keyring } from "@dxos/keyring";
631
- import { PublicKey } from "@dxos/keys";
632
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/auth.ts";
633
- var createDeviceEdgeIdentity = async (signer, key) => {
634
- return {
635
- identityKey: key.toHex(),
636
- peerKey: key.toHex(),
637
- presentCredentials: async ({ challenge }) => {
638
- return signPresentation({
639
- presentation: {
640
- credentials: [
641
- // Verifier requires at least one credential in the presentation to establish the subject.
642
- await createCredential({
643
- assertion: {
644
- "@type": "dxos.halo.credentials.Auth"
645
- },
646
- issuer: key,
647
- subject: key,
648
- signer
649
- })
650
- ]
651
- },
652
- signer,
653
- signerKey: key,
654
- nonce: challenge
655
- });
656
- }
657
- };
658
- };
659
- var createChainEdgeIdentity = async (signer, identityKey, peerKey, chain, credentials) => {
660
- const credentialsToSign = credentials.length > 0 ? credentials : [
661
- await createCredential({
662
- assertion: {
663
- "@type": "dxos.halo.credentials.Auth"
664
- },
665
- issuer: identityKey,
666
- subject: identityKey,
667
- signer,
668
- chain,
669
- signingKey: peerKey
670
- })
671
- ];
672
- return {
673
- identityKey: identityKey.toHex(),
674
- peerKey: peerKey.toHex(),
675
- presentCredentials: async ({ challenge }) => {
676
- invariant3(chain, void 0, {
677
- F: __dxlog_file4,
678
- L: 75,
679
- S: void 0,
680
- A: [
681
- "chain",
682
- ""
683
- ]
684
- });
685
- return signPresentation({
686
- presentation: {
687
- credentials: credentialsToSign
688
- },
689
- signer,
690
- nonce: challenge,
691
- signerKey: peerKey,
692
- chain
693
- });
694
- }
695
- };
696
- };
697
- var createEphemeralEdgeIdentity = async () => {
698
- const keyring = new Keyring();
699
- const key = await keyring.createKey();
700
- return createDeviceEdgeIdentity(keyring, key);
701
- };
702
- var createTestHaloEdgeIdentity = async (signer, identityKey, deviceKey) => {
703
- const deviceAdmission = await createCredential({
704
- assertion: {
705
- "@type": "dxos.halo.credentials.AuthorizedDevice",
706
- deviceKey,
707
- identityKey
708
- },
709
- issuer: identityKey,
710
- subject: deviceKey,
711
- signer
712
- });
713
- return createChainEdgeIdentity(signer, identityKey, deviceKey, {
714
- credential: deviceAdmission
715
- }, [
716
- await createCredential({
717
- assertion: {
718
- "@type": "dxos.halo.credentials.Auth"
719
- },
720
- issuer: identityKey,
721
- subject: identityKey,
722
- signer
723
- })
724
- ]);
725
- };
726
- var createStubEdgeIdentity = () => {
727
- const identityKey = PublicKey.random();
728
- const deviceKey = PublicKey.random();
729
- return {
730
- identityKey: identityKey.toHex(),
731
- peerKey: deviceKey.toHex(),
732
- presentCredentials: async () => {
733
- throw new Error("Stub identity does not support authentication.");
734
- }
735
- };
736
- };
737
-
738
- // packages/core/mesh/edge-client/src/edge-http-client.ts
739
- import { sleep } from "@dxos/async";
740
- import { Context as Context2 } from "@dxos/context";
741
- import { log as log3 } from "@dxos/log";
742
- import { EdgeCallFailedError, EdgeAuthChallengeError } from "@dxos/protocols";
743
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-http-client.ts";
744
- var DEFAULT_RETRY_TIMEOUT = 1500;
745
- var DEFAULT_RETRY_JITTER = 500;
746
- var DEFAULT_MAX_RETRIES_COUNT = 3;
747
- var EdgeHttpClient = class {
748
- constructor(baseUrl) {
749
- this._baseUrl = getEdgeUrlWithProtocol(baseUrl, "http");
750
- log3("created", {
751
- url: this._baseUrl
752
- }, {
753
- F: __dxlog_file5,
754
- L: 53,
755
- S: this,
756
- C: (f, a) => f(...a)
757
- });
758
- }
759
- get baseUrl() {
760
- return this._baseUrl;
761
- }
762
- setIdentity(identity) {
763
- if (this._edgeIdentity?.identityKey !== identity.identityKey || this._edgeIdentity?.peerKey !== identity.peerKey) {
764
- this._edgeIdentity = identity;
765
- this._authHeader = void 0;
766
- }
767
- }
768
- createAgent(body, args) {
769
- return this._call("/agents/create", {
770
- ...args,
771
- method: "POST",
772
- body
773
- });
774
- }
775
- getAgentStatus(request, args) {
776
- return this._call(`/users/${request.ownerIdentityKey.toHex()}/agent/status`, {
777
- ...args,
778
- method: "GET"
779
- });
780
- }
781
- getCredentialsForNotarization(spaceId, args) {
782
- return this._call(`/spaces/${spaceId}/notarization`, {
783
- ...args,
784
- method: "GET"
785
- });
786
- }
787
- async notarizeCredentials(spaceId, body, args) {
788
- await this._call(`/spaces/${spaceId}/notarization`, {
789
- ...args,
790
- body,
791
- method: "POST"
792
- });
793
- }
794
- async joinSpaceByInvitation(spaceId, body, args) {
795
- return this._call(`/spaces/${spaceId}/join`, {
796
- ...args,
797
- body,
798
- method: "POST"
799
- });
800
- }
801
- async recoverIdentity(body, args) {
802
- return this._call("/identity/recover", {
803
- ...args,
804
- body,
805
- method: "POST"
806
- });
807
- }
808
- async executeWorkflow(spaceId, graphId, input, args) {
809
- return this._call(`/workflows/${spaceId}/${graphId}`, {
810
- ...args,
811
- body: input,
812
- method: "POST"
813
- });
814
- }
815
- async uploadFunction(pathParts, body, args) {
816
- const path = [
817
- "functions",
818
- ...pathParts.functionId ? [
819
- pathParts.functionId
820
- ] : []
821
- ].join("/");
822
- return this._call(path, {
823
- ...args,
824
- body,
825
- method: "PUT"
826
- });
827
- }
828
- async initiateOAuthFlow(body, args) {
829
- return this._call("/oauth/initiate", {
830
- ...args,
831
- body,
832
- method: "POST"
833
- });
834
- }
835
- async queryQueue(subspaceTag, spaceId, query, args) {
836
- const { queueId } = query;
837
- const queryParams = new URLSearchParams();
838
- if (query.after != null) {
839
- queryParams.set("after", query.after);
840
- }
841
- if (query.before != null) {
842
- queryParams.set("before", query.before);
843
- }
844
- if (query.limit != null) {
845
- queryParams.set("limit", query.limit.toString());
846
- }
847
- if (query.reverse != null) {
848
- queryParams.set("reverse", query.reverse.toString());
849
- }
850
- if (query.objectIds != null) {
851
- queryParams.set("objectIds", query.objectIds.join(","));
852
- }
853
- return this._call(`/spaces/${subspaceTag}/${spaceId}/queue/${queueId}/query?${queryParams.toString()}`, {
854
- ...args,
855
- method: "GET"
856
- });
857
- }
858
- async insertIntoQueue(subspaceTag, spaceId, queueId, objects, args) {
859
- return this._call(`/spaces/${subspaceTag}/${spaceId}/queue/${queueId}`, {
860
- ...args,
861
- body: {
862
- objects
863
- },
864
- method: "POST"
865
- });
866
- }
867
- async deleteFromQueue(subspaceTag, spaceId, queueId, objectIds, args) {
868
- return this._call(`/spaces/${subspaceTag}/${spaceId}/queue/${queueId}`, {
869
- ...args,
870
- query: {
871
- ids: objectIds.join(",")
872
- },
873
- method: "DELETE"
874
- });
875
- }
876
- async createSpace(body, args) {
877
- return this._call("/spaces/create", {
878
- ...args,
879
- body,
880
- method: "POST"
881
- });
882
- }
883
- async _call(path, args) {
884
- const requestContext = args.context ?? new Context2(void 0, {
885
- F: __dxlog_file5,
886
- L: 192
887
- });
888
- const shouldRetry = createRetryHandler(args);
889
- let url = `${this._baseUrl}${path.startsWith("/") ? path.slice(1) : path}`;
890
- if (args.query) {
891
- const queryParams = new URLSearchParams();
892
- for (const [key, value] of Object.entries(args.query)) {
893
- queryParams.set(key, value.toString());
894
- }
895
- url += `?${queryParams.toString()}`;
896
- }
897
- log3("call", {
898
- method: args.method,
899
- path,
900
- request: args.body
901
- }, {
902
- F: __dxlog_file5,
903
- L: 204,
904
- S: this,
905
- C: (f, a) => f(...a)
906
- });
907
- let handledAuth = false;
908
- let authHeader = this._authHeader;
909
- while (true) {
910
- let processingError;
911
- let retryAfterHeaderValue = Number.NaN;
912
- try {
913
- const request = createRequest(args, authHeader);
914
- const response = await fetch(url, request);
915
- retryAfterHeaderValue = Number(response.headers.get("Retry-After"));
916
- if (response.ok) {
917
- const body = await response.json();
918
- if (body.success) {
919
- return body.data;
920
- }
921
- log3("unsuccessful edge response", {
922
- path,
923
- body
924
- }, {
925
- F: __dxlog_file5,
926
- L: 223,
927
- S: this,
928
- C: (f, a) => f(...a)
929
- });
930
- if (body.errorData?.type === "auth_challenge" && typeof body.errorData?.challenge === "string") {
931
- processingError = new EdgeAuthChallengeError(body.errorData.challenge, body.errorData);
932
- } else {
933
- processingError = EdgeCallFailedError.fromUnsuccessfulResponse(response, body);
934
- }
935
- } else if (response.status === 401 && !handledAuth) {
936
- authHeader = await this._handleUnauthorized(response);
937
- handledAuth = true;
938
- continue;
939
- } else {
940
- processingError = EdgeCallFailedError.fromHttpFailure(response);
941
- }
942
- } catch (error) {
943
- processingError = EdgeCallFailedError.fromProcessingFailureCause(error);
944
- }
945
- if (processingError.isRetryable && await shouldRetry(requestContext, retryAfterHeaderValue)) {
946
- log3("retrying edge request", {
947
- path,
948
- processingError
949
- }, {
950
- F: __dxlog_file5,
951
- L: 242,
952
- S: this,
953
- C: (f, a) => f(...a)
954
- });
955
- } else {
956
- throw processingError;
957
- }
958
- }
959
- }
960
- async _handleUnauthorized(response) {
961
- if (!this._edgeIdentity) {
962
- log3.warn("edge unauthorized response received before identity was set", void 0, {
963
- F: __dxlog_file5,
964
- L: 251,
965
- S: this,
966
- C: (f, a) => f(...a)
967
- });
968
- throw EdgeCallFailedError.fromHttpFailure(response);
969
- }
970
- const challenge = await handleAuthChallenge(response, this._edgeIdentity);
971
- this._authHeader = encodeAuthHeader(challenge);
972
- log3("auth header updated", void 0, {
973
- F: __dxlog_file5,
974
- L: 256,
975
- S: this,
976
- C: (f, a) => f(...a)
977
- });
978
- return this._authHeader;
979
- }
980
- };
981
- var createRetryHandler = (args) => {
982
- if (!args.retry || args.retry.count < 1) {
983
- return async () => false;
984
- }
985
- let retries = 0;
986
- const maxRetries = args.retry.count ?? DEFAULT_MAX_RETRIES_COUNT;
987
- const baseTimeout = args.retry.timeout ?? DEFAULT_RETRY_TIMEOUT;
988
- const jitter = args.retry.jitter ?? DEFAULT_RETRY_JITTER;
989
- return async (ctx, retryAfter) => {
990
- if (++retries > maxRetries || ctx.disposed) {
991
- return false;
992
- }
993
- if (retryAfter) {
994
- await sleep(retryAfter);
995
- } else {
996
- const timeout = baseTimeout + Math.random() * jitter;
997
- await sleep(timeout);
998
- }
999
- return true;
1000
- };
1001
- };
1002
- var createRequest = (args, authHeader) => {
1003
- return {
1004
- method: args.method,
1005
- body: args.body && JSON.stringify(args.body),
1006
- headers: authHeader ? {
1007
- Authorization: authHeader
1008
- } : void 0
1009
- };
1010
- };
1011
- var encodeAuthHeader = (challenge) => {
1012
- const encodedChallenge = Buffer.from(challenge).toString("base64");
1013
- return `VerifiablePresentation pb;base64,${encodedChallenge}`;
1014
- };
1015
- export {
1016
- CLOUDFLARE_MESSAGE_MAX_BYTES,
1017
- CLOUDFLARE_RPC_MAX_BYTES,
1018
- EdgeClient,
1019
- EdgeConnectionClosedError,
1020
- EdgeHttpClient,
1021
- EdgeIdentityChangedError,
1022
- Protocol,
1023
- WebSocketMuxer,
1024
- createChainEdgeIdentity,
1025
- createDeviceEdgeIdentity,
1026
- createEphemeralEdgeIdentity,
1027
- createStubEdgeIdentity,
1028
- createTestHaloEdgeIdentity,
1029
- getTypename,
1030
- handleAuthChallenge,
1031
- protocol,
1032
- toUint8Array
1033
- };
1034
- //# sourceMappingURL=index.mjs.map