@dxos/edge-client 0.6.12 → 0.6.13-main.548ca8d
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 +347 -178
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +125 -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 +346 -175
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +155 -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 +647 -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 +126 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/auth.d.ts +22 -0
- package/dist/types/src/auth.d.ts.map +1 -0
- package/dist/types/src/defs.d.ts.map +1 -1
- package/dist/types/src/edge-client.d.ts +24 -13
- package/dist/types/src/edge-client.d.ts.map +1 -1
- package/dist/types/src/edge-http-client.d.ts +35 -0
- package/dist/types/src/edge-http-client.d.ts.map +1 -0
- 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 +3 -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 +21 -0
- package/dist/types/src/testing/test-utils.d.ts.map +1 -0
- package/dist/types/src/utils.d.ts +2 -0
- package/dist/types/src/utils.d.ts.map +1 -0
- package/package.json +29 -14
- package/src/auth.ts +135 -0
- package/src/defs.ts +2 -3
- package/src/edge-client.test.ts +50 -18
- package/src/edge-client.ts +79 -23
- package/src/edge-http-client.ts +151 -0
- package/src/errors.ts +8 -2
- package/src/index.ts +3 -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 +114 -0
- package/src/utils.ts +10 -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
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Protocol,
|
|
3
|
+
getTypename,
|
|
4
|
+
protocol,
|
|
5
|
+
toUint8Array
|
|
6
|
+
} from "./chunk-ZWJXA37R.mjs";
|
|
7
|
+
|
|
1
8
|
// packages/core/mesh/edge-client/src/index.ts
|
|
2
9
|
export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
3
10
|
|
|
@@ -5,123 +12,21 @@ export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
|
5
12
|
import WebSocket from "isomorphic-ws";
|
|
6
13
|
import { Trigger, Event, scheduleTaskInterval, scheduleTask, TriggerState } from "@dxos/async";
|
|
7
14
|
import { Context, LifecycleState as LifecycleState2, Resource as Resource2 } from "@dxos/context";
|
|
8
|
-
import {
|
|
15
|
+
import { randomBytes } from "@dxos/crypto";
|
|
9
16
|
import { log as log2 } from "@dxos/log";
|
|
10
|
-
import { buf
|
|
11
|
-
import { MessageSchema as MessageSchema2 } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
12
|
-
|
|
13
|
-
// packages/core/mesh/edge-client/src/defs.ts
|
|
14
|
-
import { AnySchema } from "@bufbuild/protobuf/wkt";
|
|
15
|
-
import { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
16
|
-
|
|
17
|
-
// packages/core/mesh/edge-client/src/protocol.ts
|
|
18
|
-
import { invariant } from "@dxos/invariant";
|
|
19
|
-
import { buf, bufWkt } from "@dxos/protocols/buf";
|
|
17
|
+
import { buf } from "@dxos/protocols/buf";
|
|
20
18
|
import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
|
|
21
|
-
import {
|
|
22
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
|
|
23
|
-
var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
|
|
24
|
-
var Protocol = class {
|
|
25
|
-
constructor(types) {
|
|
26
|
-
this._typeRegistry = buf.createRegistry(...types);
|
|
27
|
-
}
|
|
28
|
-
get typeRegistry() {
|
|
29
|
-
return this._typeRegistry;
|
|
30
|
-
}
|
|
31
|
-
toJson(message) {
|
|
32
|
-
try {
|
|
33
|
-
return buf.toJson(MessageSchema, message, {
|
|
34
|
-
registry: this.typeRegistry
|
|
35
|
-
});
|
|
36
|
-
} catch (err) {
|
|
37
|
-
return {
|
|
38
|
-
type: this.getPayloadType(message)
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Return the payload with the given type.
|
|
44
|
-
*/
|
|
45
|
-
getPayload(message, type) {
|
|
46
|
-
invariant(message.payload, void 0, {
|
|
47
|
-
F: __dxlog_file,
|
|
48
|
-
L: 40,
|
|
49
|
-
S: this,
|
|
50
|
-
A: [
|
|
51
|
-
"message.payload",
|
|
52
|
-
""
|
|
53
|
-
]
|
|
54
|
-
});
|
|
55
|
-
const payloadTypename = this.getPayloadType(message);
|
|
56
|
-
if (type && type.typeName !== payloadTypename) {
|
|
57
|
-
throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
|
|
58
|
-
}
|
|
59
|
-
invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
|
|
60
|
-
F: __dxlog_file,
|
|
61
|
-
L: 46,
|
|
62
|
-
S: this,
|
|
63
|
-
A: [
|
|
64
|
-
"bufWkt.anyIs(message.payload, type)",
|
|
65
|
-
"`Unexpected payload type: ${payloadTypename}}`"
|
|
66
|
-
]
|
|
67
|
-
});
|
|
68
|
-
const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
|
|
69
|
-
invariant(payload, `Empty payload: ${payloadTypename}}`, {
|
|
70
|
-
F: __dxlog_file,
|
|
71
|
-
L: 48,
|
|
72
|
-
S: this,
|
|
73
|
-
A: [
|
|
74
|
-
"payload",
|
|
75
|
-
"`Empty payload: ${payloadTypename}}`"
|
|
76
|
-
]
|
|
77
|
-
});
|
|
78
|
-
return payload;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Get the payload type.
|
|
82
|
-
*/
|
|
83
|
-
getPayloadType(message) {
|
|
84
|
-
if (!message.payload) {
|
|
85
|
-
return void 0;
|
|
86
|
-
}
|
|
87
|
-
const [, type] = message.payload.typeUrl.split("/");
|
|
88
|
-
return type;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Create a packed message.
|
|
92
|
-
*/
|
|
93
|
-
createMessage(type, { source, target, payload, serviceId }) {
|
|
94
|
-
return buf.create(MessageSchema, {
|
|
95
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
96
|
-
source,
|
|
97
|
-
target,
|
|
98
|
-
serviceId,
|
|
99
|
-
payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
var toUint8Array = async (data) => {
|
|
104
|
-
if (data instanceof Buffer) {
|
|
105
|
-
return bufferToArray(data);
|
|
106
|
-
}
|
|
107
|
-
if (data instanceof Blob) {
|
|
108
|
-
return new Uint8Array(await data.arrayBuffer());
|
|
109
|
-
}
|
|
110
|
-
throw new Error(`Unexpected datatype: ${data}`);
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
// packages/core/mesh/edge-client/src/defs.ts
|
|
114
|
-
var protocol = new Protocol([
|
|
115
|
-
SwarmRequestSchema,
|
|
116
|
-
SwarmResponseSchema,
|
|
117
|
-
TextMessageSchema,
|
|
118
|
-
AnySchema
|
|
119
|
-
]);
|
|
19
|
+
import { schema } from "@dxos/protocols/proto";
|
|
120
20
|
|
|
121
21
|
// packages/core/mesh/edge-client/src/errors.ts
|
|
122
|
-
var
|
|
22
|
+
var EdgeConnectionClosedError = class extends Error {
|
|
23
|
+
constructor() {
|
|
24
|
+
super("Edge connection closed.");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var EdgeIdentityChangedError = class extends Error {
|
|
123
28
|
constructor() {
|
|
124
|
-
super("
|
|
29
|
+
super("Edge identity changed.");
|
|
125
30
|
}
|
|
126
31
|
};
|
|
127
32
|
|
|
@@ -136,7 +41,7 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
136
41
|
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;
|
|
137
42
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
138
43
|
}
|
|
139
|
-
var
|
|
44
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/persistent-lifecycle.ts";
|
|
140
45
|
var INIT_RESTART_DELAY = 100;
|
|
141
46
|
var DEFAULT_MAX_RESTART_DELAY = 5e3;
|
|
142
47
|
var PersistentLifecycle = class extends Resource {
|
|
@@ -157,7 +62,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
157
62
|
log.warn("Restart failed", {
|
|
158
63
|
err
|
|
159
64
|
}, {
|
|
160
|
-
F:
|
|
65
|
+
F: __dxlog_file,
|
|
161
66
|
L: 64,
|
|
162
67
|
S: this,
|
|
163
68
|
C: (f, a) => f(...a)
|
|
@@ -169,7 +74,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
169
74
|
log.warn("Start failed", {
|
|
170
75
|
err
|
|
171
76
|
}, {
|
|
172
|
-
F:
|
|
77
|
+
F: __dxlog_file,
|
|
173
78
|
L: 69,
|
|
174
79
|
S: this,
|
|
175
80
|
C: (f, a) => f(...a)
|
|
@@ -186,7 +91,7 @@ var PersistentLifecycle = class extends Resource {
|
|
|
186
91
|
log(`restarting in ${this._restartAfter}ms`, {
|
|
187
92
|
state: this._lifecycleState
|
|
188
93
|
}, {
|
|
189
|
-
F:
|
|
94
|
+
F: __dxlog_file,
|
|
190
95
|
L: 81,
|
|
191
96
|
S: this,
|
|
192
97
|
C: (f, a) => f(...a)
|
|
@@ -218,17 +123,25 @@ _ts_decorate([
|
|
|
218
123
|
synchronized
|
|
219
124
|
], PersistentLifecycle.prototype, "scheduleRestart", null);
|
|
220
125
|
|
|
126
|
+
// packages/core/mesh/edge-client/src/utils.ts
|
|
127
|
+
var getEdgeUrlWithProtocol = (baseUrl, protocol2) => {
|
|
128
|
+
const isSecure = baseUrl.startsWith("https") || baseUrl.startsWith("wss");
|
|
129
|
+
const url = new URL(baseUrl);
|
|
130
|
+
url.protocol = protocol2 + (isSecure ? "s" : "");
|
|
131
|
+
return url.toString();
|
|
132
|
+
};
|
|
133
|
+
|
|
221
134
|
// packages/core/mesh/edge-client/src/edge-client.ts
|
|
222
|
-
var
|
|
135
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-client.ts";
|
|
223
136
|
var DEFAULT_TIMEOUT = 1e4;
|
|
224
137
|
var SIGNAL_KEEPALIVE_INTERVAL = 5e3;
|
|
225
138
|
var EdgeClient = class extends Resource2 {
|
|
226
|
-
constructor(
|
|
139
|
+
constructor(_identity, _config) {
|
|
227
140
|
super();
|
|
228
|
-
this.
|
|
229
|
-
this._peerKey = _peerKey;
|
|
141
|
+
this._identity = _identity;
|
|
230
142
|
this._config = _config;
|
|
231
143
|
this.reconnect = new Event();
|
|
144
|
+
this.connected = new Event();
|
|
232
145
|
this._persistentLifecycle = new PersistentLifecycle({
|
|
233
146
|
start: async () => this._openWebSocket(),
|
|
234
147
|
stop: async () => this._closeWebSocket(),
|
|
@@ -239,25 +152,27 @@ var EdgeClient = class extends Resource2 {
|
|
|
239
152
|
this._ws = void 0;
|
|
240
153
|
this._keepaliveCtx = void 0;
|
|
241
154
|
this._heartBeatContext = void 0;
|
|
242
|
-
this.
|
|
155
|
+
this._baseUrl = getEdgeUrlWithProtocol(_config.socketEndpoint, "ws");
|
|
243
156
|
}
|
|
244
157
|
// TODO(burdon): Attach logging.
|
|
245
158
|
get info() {
|
|
246
159
|
return {
|
|
247
160
|
open: this.isOpen,
|
|
248
|
-
identity: this.
|
|
249
|
-
device: this.
|
|
161
|
+
identity: this._identity.identityKey,
|
|
162
|
+
device: this._identity.peerKey
|
|
250
163
|
};
|
|
251
164
|
}
|
|
165
|
+
get isConnected() {
|
|
166
|
+
return Boolean(this._ws) && this._ready.state === TriggerState.RESOLVED;
|
|
167
|
+
}
|
|
252
168
|
get identityKey() {
|
|
253
|
-
return this.
|
|
169
|
+
return this._identity.identityKey;
|
|
254
170
|
}
|
|
255
171
|
get peerKey() {
|
|
256
|
-
return this.
|
|
172
|
+
return this._identity.peerKey;
|
|
257
173
|
}
|
|
258
|
-
setIdentity(
|
|
259
|
-
this.
|
|
260
|
-
this._identityKey = identityKey;
|
|
174
|
+
setIdentity(identity) {
|
|
175
|
+
this._identity = identity;
|
|
261
176
|
this._persistentLifecycle.scheduleRestart();
|
|
262
177
|
}
|
|
263
178
|
addListener(listener) {
|
|
@@ -271,8 +186,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
271
186
|
log2("opening...", {
|
|
272
187
|
info: this.info
|
|
273
188
|
}, {
|
|
274
|
-
F:
|
|
275
|
-
L:
|
|
189
|
+
F: __dxlog_file2,
|
|
190
|
+
L: 122,
|
|
276
191
|
S: this,
|
|
277
192
|
C: (f, a) => f(...a)
|
|
278
193
|
});
|
|
@@ -280,8 +195,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
280
195
|
log2.warn("Error while opening connection", {
|
|
281
196
|
err
|
|
282
197
|
}, {
|
|
283
|
-
F:
|
|
284
|
-
L:
|
|
198
|
+
F: __dxlog_file2,
|
|
199
|
+
L: 124,
|
|
285
200
|
S: this,
|
|
286
201
|
C: (f, a) => f(...a)
|
|
287
202
|
});
|
|
@@ -292,31 +207,54 @@ var EdgeClient = class extends Resource2 {
|
|
|
292
207
|
*/
|
|
293
208
|
async _close() {
|
|
294
209
|
log2("closing...", {
|
|
295
|
-
peerKey: this.
|
|
210
|
+
peerKey: this._identity.peerKey
|
|
296
211
|
}, {
|
|
297
|
-
F:
|
|
298
|
-
L:
|
|
212
|
+
F: __dxlog_file2,
|
|
213
|
+
L: 132,
|
|
299
214
|
S: this,
|
|
300
215
|
C: (f, a) => f(...a)
|
|
301
216
|
});
|
|
302
217
|
await this._persistentLifecycle.close();
|
|
303
218
|
}
|
|
304
219
|
async _openWebSocket() {
|
|
305
|
-
|
|
306
|
-
this.
|
|
220
|
+
let protocolHeader;
|
|
221
|
+
if (!this._config.disableAuth) {
|
|
222
|
+
const challenge = randomBytes(32);
|
|
223
|
+
const credential = await this._identity.presentCredentials({
|
|
224
|
+
challenge
|
|
225
|
+
});
|
|
226
|
+
protocolHeader = encodePresentationIntoAuthHeader(credential);
|
|
227
|
+
}
|
|
228
|
+
if (this._ctx.disposed) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const url = new URL(`/ws/${this._identity.identityKey}/${this._identity.peerKey}`, this._baseUrl);
|
|
232
|
+
log2("Opening websocket", {
|
|
233
|
+
url: url.toString(),
|
|
234
|
+
protocolHeader
|
|
235
|
+
}, {
|
|
236
|
+
F: __dxlog_file2,
|
|
237
|
+
L: 151,
|
|
238
|
+
S: this,
|
|
239
|
+
C: (f, a) => f(...a)
|
|
240
|
+
});
|
|
241
|
+
this._ws = new WebSocket(url, protocolHeader ? [
|
|
242
|
+
protocolHeader
|
|
243
|
+
] : []);
|
|
307
244
|
this._ws.onopen = () => {
|
|
308
245
|
log2("opened", this.info, {
|
|
309
|
-
F:
|
|
310
|
-
L:
|
|
246
|
+
F: __dxlog_file2,
|
|
247
|
+
L: 155,
|
|
311
248
|
S: this,
|
|
312
249
|
C: (f, a) => f(...a)
|
|
313
250
|
});
|
|
314
251
|
this._ready.wake();
|
|
252
|
+
this.connected.emit();
|
|
315
253
|
};
|
|
316
254
|
this._ws.onclose = () => {
|
|
317
255
|
log2("closed", this.info, {
|
|
318
|
-
F:
|
|
319
|
-
L:
|
|
256
|
+
F: __dxlog_file2,
|
|
257
|
+
L: 160,
|
|
320
258
|
S: this,
|
|
321
259
|
C: (f, a) => f(...a)
|
|
322
260
|
});
|
|
@@ -327,8 +265,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
327
265
|
error: event.error,
|
|
328
266
|
info: event.message
|
|
329
267
|
}, {
|
|
330
|
-
F:
|
|
331
|
-
L:
|
|
268
|
+
F: __dxlog_file2,
|
|
269
|
+
L: 164,
|
|
332
270
|
S: this,
|
|
333
271
|
C: (f, a) => f(...a)
|
|
334
272
|
});
|
|
@@ -340,13 +278,13 @@ var EdgeClient = class extends Resource2 {
|
|
|
340
278
|
return;
|
|
341
279
|
}
|
|
342
280
|
const data = await toUint8Array(event.data);
|
|
343
|
-
const message =
|
|
281
|
+
const message = buf.fromBinary(MessageSchema, data);
|
|
344
282
|
log2("received", {
|
|
345
|
-
peerKey: this.
|
|
283
|
+
peerKey: this._identity.peerKey,
|
|
346
284
|
payload: protocol.getPayloadType(message)
|
|
347
285
|
}, {
|
|
348
|
-
F:
|
|
349
|
-
L:
|
|
286
|
+
F: __dxlog_file2,
|
|
287
|
+
L: 177,
|
|
350
288
|
S: this,
|
|
351
289
|
C: (f, a) => f(...a)
|
|
352
290
|
});
|
|
@@ -359,8 +297,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
359
297
|
err,
|
|
360
298
|
payload: protocol.getPayloadType(message)
|
|
361
299
|
}, {
|
|
362
|
-
F:
|
|
363
|
-
L:
|
|
300
|
+
F: __dxlog_file2,
|
|
301
|
+
L: 183,
|
|
364
302
|
S: this,
|
|
365
303
|
C: (f, a) => f(...a)
|
|
366
304
|
});
|
|
@@ -372,8 +310,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
372
310
|
timeout: this._config.timeout ?? DEFAULT_TIMEOUT
|
|
373
311
|
});
|
|
374
312
|
this._keepaliveCtx = new Context(void 0, {
|
|
375
|
-
F:
|
|
376
|
-
L:
|
|
313
|
+
F: __dxlog_file2,
|
|
314
|
+
L: 193
|
|
377
315
|
});
|
|
378
316
|
scheduleTaskInterval(this._keepaliveCtx, async () => {
|
|
379
317
|
this._ws?.send("__ping__");
|
|
@@ -386,7 +324,7 @@ var EdgeClient = class extends Resource2 {
|
|
|
386
324
|
return;
|
|
387
325
|
}
|
|
388
326
|
try {
|
|
389
|
-
this._ready.throw(new
|
|
327
|
+
this._ready.throw(this.isOpen ? new EdgeIdentityChangedError() : new EdgeConnectionClosedError());
|
|
390
328
|
this._ready.reset();
|
|
391
329
|
void this._keepaliveCtx?.dispose();
|
|
392
330
|
this._keepaliveCtx = void 0;
|
|
@@ -407,8 +345,8 @@ var EdgeClient = class extends Resource2 {
|
|
|
407
345
|
log2.warn("Error closing websocket", {
|
|
408
346
|
err
|
|
409
347
|
}, {
|
|
410
|
-
F:
|
|
411
|
-
L:
|
|
348
|
+
F: __dxlog_file2,
|
|
349
|
+
L: 229,
|
|
412
350
|
S: this,
|
|
413
351
|
C: (f, a) => f(...a)
|
|
414
352
|
});
|
|
@@ -420,38 +358,32 @@ var EdgeClient = class extends Resource2 {
|
|
|
420
358
|
*/
|
|
421
359
|
async send(message) {
|
|
422
360
|
if (this._ready.state !== TriggerState.RESOLVED) {
|
|
361
|
+
log2("waiting for websocket to become ready", void 0, {
|
|
362
|
+
F: __dxlog_file2,
|
|
363
|
+
L: 239,
|
|
364
|
+
S: this,
|
|
365
|
+
C: (f, a) => f(...a)
|
|
366
|
+
});
|
|
423
367
|
await this._ready.wait({
|
|
424
368
|
timeout: this._config.timeout ?? DEFAULT_TIMEOUT
|
|
425
369
|
});
|
|
426
370
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
""
|
|
434
|
-
]
|
|
435
|
-
});
|
|
436
|
-
invariant2(!message.source || message.source.peerKey === this._peerKey, void 0, {
|
|
437
|
-
F: __dxlog_file3,
|
|
438
|
-
L: 203,
|
|
439
|
-
S: this,
|
|
440
|
-
A: [
|
|
441
|
-
"!message.source || message.source.peerKey === this._peerKey",
|
|
442
|
-
""
|
|
443
|
-
]
|
|
444
|
-
});
|
|
371
|
+
if (!this._ws) {
|
|
372
|
+
throw new EdgeConnectionClosedError();
|
|
373
|
+
}
|
|
374
|
+
if (message.source && (message.source.peerKey !== this._identity.peerKey || message.source.identityKey !== this.identityKey)) {
|
|
375
|
+
throw new EdgeIdentityChangedError();
|
|
376
|
+
}
|
|
445
377
|
log2("sending...", {
|
|
446
|
-
peerKey: this.
|
|
378
|
+
peerKey: this._identity.peerKey,
|
|
447
379
|
payload: protocol.getPayloadType(message)
|
|
448
380
|
}, {
|
|
449
|
-
F:
|
|
450
|
-
L:
|
|
381
|
+
F: __dxlog_file2,
|
|
382
|
+
L: 252,
|
|
451
383
|
S: this,
|
|
452
384
|
C: (f, a) => f(...a)
|
|
453
385
|
});
|
|
454
|
-
this._ws.send(
|
|
386
|
+
this._ws.send(buf.toBinary(MessageSchema, message));
|
|
455
387
|
}
|
|
456
388
|
_onHeartbeat() {
|
|
457
389
|
if (this._lifecycleState !== LifecycleState2.OPEN) {
|
|
@@ -459,17 +391,254 @@ var EdgeClient = class extends Resource2 {
|
|
|
459
391
|
}
|
|
460
392
|
void this._heartBeatContext?.dispose();
|
|
461
393
|
this._heartBeatContext = new Context(void 0, {
|
|
462
|
-
F:
|
|
463
|
-
L:
|
|
394
|
+
F: __dxlog_file2,
|
|
395
|
+
L: 261
|
|
464
396
|
});
|
|
465
397
|
scheduleTask(this._heartBeatContext, () => {
|
|
466
398
|
this._persistentLifecycle.scheduleRestart();
|
|
467
399
|
}, 2 * SIGNAL_KEEPALIVE_INTERVAL);
|
|
468
400
|
}
|
|
469
401
|
};
|
|
402
|
+
var encodePresentationIntoAuthHeader = (presentation) => {
|
|
403
|
+
const encoded = schema.getCodecForType("dxos.halo.credentials.Presentation").encode(presentation);
|
|
404
|
+
const encodedToken = Buffer.from(encoded).toString("base64").replace(/=*$/, "").replaceAll("/", "|");
|
|
405
|
+
return `base64url.bearer.authorization.dxos.org.${encodedToken}`;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
// packages/core/mesh/edge-client/src/auth.ts
|
|
409
|
+
import { createCredential, signPresentation } from "@dxos/credentials";
|
|
410
|
+
import { Keyring } from "@dxos/keyring";
|
|
411
|
+
import { PublicKey } from "@dxos/keys";
|
|
412
|
+
var createDeviceEdgeIdentity = async (signer, key) => {
|
|
413
|
+
return {
|
|
414
|
+
identityKey: key.toHex(),
|
|
415
|
+
peerKey: key.toHex(),
|
|
416
|
+
presentCredentials: async ({ challenge }) => {
|
|
417
|
+
return signPresentation({
|
|
418
|
+
presentation: {
|
|
419
|
+
credentials: [
|
|
420
|
+
// Verifier requires at least one credential in the presentation to establish the subject.
|
|
421
|
+
await createCredential({
|
|
422
|
+
assertion: {
|
|
423
|
+
"@type": "dxos.halo.credentials.Auth"
|
|
424
|
+
},
|
|
425
|
+
issuer: key,
|
|
426
|
+
subject: key,
|
|
427
|
+
signer
|
|
428
|
+
})
|
|
429
|
+
]
|
|
430
|
+
},
|
|
431
|
+
signer,
|
|
432
|
+
signerKey: key,
|
|
433
|
+
nonce: challenge
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
};
|
|
438
|
+
var createChainEdgeIdentity = async (signer, identityKey, peerKey, chain, credentials) => {
|
|
439
|
+
const credentialsToSign = credentials.length > 0 ? credentials : [
|
|
440
|
+
await createCredential({
|
|
441
|
+
assertion: {
|
|
442
|
+
"@type": "dxos.halo.credentials.Auth"
|
|
443
|
+
},
|
|
444
|
+
issuer: identityKey,
|
|
445
|
+
subject: identityKey,
|
|
446
|
+
signer,
|
|
447
|
+
chain,
|
|
448
|
+
signingKey: peerKey
|
|
449
|
+
})
|
|
450
|
+
];
|
|
451
|
+
return {
|
|
452
|
+
identityKey: identityKey.toHex(),
|
|
453
|
+
peerKey: peerKey.toHex(),
|
|
454
|
+
presentCredentials: async ({ challenge }) => {
|
|
455
|
+
return signPresentation({
|
|
456
|
+
presentation: {
|
|
457
|
+
credentials: credentialsToSign
|
|
458
|
+
},
|
|
459
|
+
signer,
|
|
460
|
+
nonce: challenge,
|
|
461
|
+
signerKey: peerKey,
|
|
462
|
+
chain
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
};
|
|
467
|
+
var createEphemeralEdgeIdentity = async () => {
|
|
468
|
+
const keyring = new Keyring();
|
|
469
|
+
const key = await keyring.createKey();
|
|
470
|
+
return createDeviceEdgeIdentity(keyring, key);
|
|
471
|
+
};
|
|
472
|
+
var createTestHaloEdgeIdentity = async (signer, identityKey, deviceKey) => {
|
|
473
|
+
const deviceAdmission = await createCredential({
|
|
474
|
+
assertion: {
|
|
475
|
+
"@type": "dxos.halo.credentials.AuthorizedDevice",
|
|
476
|
+
deviceKey,
|
|
477
|
+
identityKey
|
|
478
|
+
},
|
|
479
|
+
issuer: identityKey,
|
|
480
|
+
subject: deviceKey,
|
|
481
|
+
signer
|
|
482
|
+
});
|
|
483
|
+
return createChainEdgeIdentity(signer, identityKey, deviceKey, {
|
|
484
|
+
credential: deviceAdmission
|
|
485
|
+
}, [
|
|
486
|
+
await createCredential({
|
|
487
|
+
assertion: {
|
|
488
|
+
"@type": "dxos.halo.credentials.Auth"
|
|
489
|
+
},
|
|
490
|
+
issuer: identityKey,
|
|
491
|
+
subject: identityKey,
|
|
492
|
+
signer
|
|
493
|
+
})
|
|
494
|
+
]);
|
|
495
|
+
};
|
|
496
|
+
var createStubEdgeIdentity = () => {
|
|
497
|
+
const identityKey = PublicKey.random();
|
|
498
|
+
const deviceKey = PublicKey.random();
|
|
499
|
+
return {
|
|
500
|
+
identityKey: identityKey.toHex(),
|
|
501
|
+
peerKey: deviceKey.toHex(),
|
|
502
|
+
presentCredentials: async () => {
|
|
503
|
+
throw new Error("Stub identity does not support authentication.");
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
// packages/core/mesh/edge-client/src/edge-http-client.ts
|
|
509
|
+
import { sleep as sleep2 } from "@dxos/async";
|
|
510
|
+
import { Context as Context2 } from "@dxos/context";
|
|
511
|
+
import { log as log3 } from "@dxos/log";
|
|
512
|
+
import { EdgeCallFailedError, EdgeAuthChallengeError } from "@dxos/protocols";
|
|
513
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-http-client.ts";
|
|
514
|
+
var DEFAULT_RETRY_TIMEOUT = 1500;
|
|
515
|
+
var DEFAULT_RETRY_JITTER = 500;
|
|
516
|
+
var DEFAULT_MAX_RETRIES_COUNT = 3;
|
|
517
|
+
var EdgeHttpClient = class {
|
|
518
|
+
constructor(baseUrl) {
|
|
519
|
+
this._baseUrl = getEdgeUrlWithProtocol(baseUrl, "http");
|
|
520
|
+
log3("created", {
|
|
521
|
+
url: this._baseUrl
|
|
522
|
+
}, {
|
|
523
|
+
F: __dxlog_file3,
|
|
524
|
+
L: 30,
|
|
525
|
+
S: this,
|
|
526
|
+
C: (f, a) => f(...a)
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
getCredentialsForNotarization(spaceId, args) {
|
|
530
|
+
return this._call(`/spaces/${spaceId}/notarization`, {
|
|
531
|
+
...args,
|
|
532
|
+
method: "GET"
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
async notarizeCredentials(spaceId, body, args) {
|
|
536
|
+
await this._call(`/spaces/${spaceId}/notarization`, {
|
|
537
|
+
...args,
|
|
538
|
+
body,
|
|
539
|
+
method: "POST"
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
async joinSpaceByInvitation(spaceId, body, args) {
|
|
543
|
+
return this._call(`/spaces/${spaceId}/join`, {
|
|
544
|
+
...args,
|
|
545
|
+
body,
|
|
546
|
+
method: "POST"
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
async _call(path, args) {
|
|
550
|
+
const requestContext = args.context ?? new Context2(void 0, {
|
|
551
|
+
F: __dxlog_file3,
|
|
552
|
+
L: 54
|
|
553
|
+
});
|
|
554
|
+
const shouldRetry = createRetryHandler(args);
|
|
555
|
+
const request = createRequest(args);
|
|
556
|
+
const url = `${this._baseUrl}${path.startsWith("/") ? path.slice(1) : path}`;
|
|
557
|
+
log3.info("call", {
|
|
558
|
+
method: args.method,
|
|
559
|
+
path
|
|
560
|
+
}, {
|
|
561
|
+
F: __dxlog_file3,
|
|
562
|
+
L: 59,
|
|
563
|
+
S: this,
|
|
564
|
+
C: (f, a) => f(...a)
|
|
565
|
+
});
|
|
566
|
+
while (true) {
|
|
567
|
+
let processingError;
|
|
568
|
+
let retryAfterHeaderValue = Number.NaN;
|
|
569
|
+
try {
|
|
570
|
+
const response = await fetch(url, request);
|
|
571
|
+
retryAfterHeaderValue = Number(response.headers.get("Retry-After"));
|
|
572
|
+
if (response.ok) {
|
|
573
|
+
const body = await response.json();
|
|
574
|
+
if (body.success) {
|
|
575
|
+
return body.data;
|
|
576
|
+
}
|
|
577
|
+
if (body.errorData?.type === "auth_challenge" && typeof body.errorData?.challenge === "string") {
|
|
578
|
+
processingError = new EdgeAuthChallengeError(body.errorData.challenge, body.errorData);
|
|
579
|
+
} else {
|
|
580
|
+
processingError = EdgeCallFailedError.fromUnsuccessfulResponse(response, body);
|
|
581
|
+
}
|
|
582
|
+
} else {
|
|
583
|
+
processingError = EdgeCallFailedError.fromHttpFailure(response);
|
|
584
|
+
}
|
|
585
|
+
} catch (error) {
|
|
586
|
+
processingError = EdgeCallFailedError.fromProcessingFailureCause(error);
|
|
587
|
+
}
|
|
588
|
+
if (processingError.isRetryable && await shouldRetry(requestContext, retryAfterHeaderValue)) {
|
|
589
|
+
log3.info("retrying edge request", {
|
|
590
|
+
path,
|
|
591
|
+
processingError
|
|
592
|
+
}, {
|
|
593
|
+
F: __dxlog_file3,
|
|
594
|
+
L: 88,
|
|
595
|
+
S: this,
|
|
596
|
+
C: (f, a) => f(...a)
|
|
597
|
+
});
|
|
598
|
+
} else {
|
|
599
|
+
throw processingError;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
var createRequest = (args) => {
|
|
605
|
+
return {
|
|
606
|
+
method: args.method,
|
|
607
|
+
body: args.body && JSON.stringify(args.body)
|
|
608
|
+
};
|
|
609
|
+
};
|
|
610
|
+
var createRetryHandler = (args) => {
|
|
611
|
+
if (!args.retry || args.retry.count < 1) {
|
|
612
|
+
return async () => false;
|
|
613
|
+
}
|
|
614
|
+
let retries = 0;
|
|
615
|
+
const maxRetries = args.retry.count ?? DEFAULT_MAX_RETRIES_COUNT;
|
|
616
|
+
const baseTimeout = args.retry.timeout ?? DEFAULT_RETRY_TIMEOUT;
|
|
617
|
+
const jitter = args.retry.jitter ?? DEFAULT_RETRY_JITTER;
|
|
618
|
+
return async (ctx, retryAfter) => {
|
|
619
|
+
if (++retries > maxRetries || ctx.disposed) {
|
|
620
|
+
return false;
|
|
621
|
+
}
|
|
622
|
+
if (retryAfter) {
|
|
623
|
+
await sleep2(retryAfter);
|
|
624
|
+
} else {
|
|
625
|
+
const timeout = baseTimeout + Math.random() * jitter;
|
|
626
|
+
await sleep2(timeout);
|
|
627
|
+
}
|
|
628
|
+
return true;
|
|
629
|
+
};
|
|
630
|
+
};
|
|
470
631
|
export {
|
|
471
632
|
EdgeClient,
|
|
633
|
+
EdgeConnectionClosedError,
|
|
634
|
+
EdgeHttpClient,
|
|
635
|
+
EdgeIdentityChangedError,
|
|
472
636
|
Protocol,
|
|
637
|
+
createChainEdgeIdentity,
|
|
638
|
+
createDeviceEdgeIdentity,
|
|
639
|
+
createEphemeralEdgeIdentity,
|
|
640
|
+
createStubEdgeIdentity,
|
|
641
|
+
createTestHaloEdgeIdentity,
|
|
473
642
|
getTypename,
|
|
474
643
|
protocol,
|
|
475
644
|
toUint8Array
|