@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.
Files changed (49) hide show
  1. package/dist/lib/browser/chunk-ZWJXA37R.mjs +113 -0
  2. package/dist/lib/browser/chunk-ZWJXA37R.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +57 -157
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +122 -0
  7. package/dist/lib/browser/testing/index.mjs.map +7 -0
  8. package/dist/lib/node/chunk-ANV2HBEH.cjs +136 -0
  9. package/dist/lib/node/chunk-ANV2HBEH.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +56 -154
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +152 -0
  14. package/dist/lib/node/testing/index.cjs.map +7 -0
  15. package/dist/lib/node-esm/chunk-HNVT57AU.mjs +115 -0
  16. package/dist/lib/node-esm/chunk-HNVT57AU.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +378 -0
  18. package/dist/lib/node-esm/index.mjs.map +7 -0
  19. package/dist/lib/node-esm/meta.json +1 -0
  20. package/dist/lib/node-esm/testing/index.mjs +123 -0
  21. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  22. package/dist/types/src/defs.d.ts.map +1 -1
  23. package/dist/types/src/edge-client.d.ts +3 -2
  24. package/dist/types/src/edge-client.d.ts.map +1 -1
  25. package/dist/types/src/errors.d.ts +4 -1
  26. package/dist/types/src/errors.d.ts.map +1 -1
  27. package/dist/types/src/index.d.ts +1 -0
  28. package/dist/types/src/index.d.ts.map +1 -1
  29. package/dist/types/src/protocol.d.ts +2 -2
  30. package/dist/types/src/protocol.d.ts.map +1 -1
  31. package/dist/types/src/testing/index.d.ts +2 -0
  32. package/dist/types/src/testing/index.d.ts.map +1 -0
  33. package/dist/types/src/testing/test-utils.d.ts +20 -0
  34. package/dist/types/src/testing/test-utils.d.ts.map +1 -0
  35. package/package.json +26 -14
  36. package/src/defs.ts +2 -3
  37. package/src/edge-client.test.ts +11 -11
  38. package/src/edge-client.ts +17 -8
  39. package/src/errors.ts +8 -2
  40. package/src/index.ts +1 -0
  41. package/src/persistent-lifecycle.test.ts +2 -2
  42. package/src/protocol.test.ts +1 -2
  43. package/src/protocol.ts +2 -2
  44. package/src/testing/index.ts +5 -0
  45. package/src/testing/test-utils.ts +111 -0
  46. package/src/websocket.test.ts +5 -4
  47. package/dist/types/src/test-utils.d.ts +0 -11
  48. package/dist/types/src/test-utils.d.ts.map +0 -1
  49. package/src/test-utils.ts +0 -49
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var testing_exports = {};
30
+ __export(testing_exports, {
31
+ DEFAULT_PORT: () => DEFAULT_PORT,
32
+ createTestEdgeWsServer: () => createTestEdgeWsServer
33
+ });
34
+ module.exports = __toCommonJS(testing_exports);
35
+ var import_chunk_ANV2HBEH = require("../chunk-ANV2HBEH.cjs");
36
+ var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
37
+ var import_async = require("@dxos/async");
38
+ var import_log = require("@dxos/log");
39
+ var import_buf = require("@dxos/protocols/buf");
40
+ var import_messenger_pb = require("@dxos/protocols/buf/dxos/edge/messenger_pb");
41
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/testing/test-utils.ts";
42
+ var DEFAULT_PORT = 8080;
43
+ var createTestEdgeWsServer = async (port = DEFAULT_PORT, params) => {
44
+ const server = new import_isomorphic_ws.default.Server({
45
+ port,
46
+ verifyClient: createConnectionDelayHandler(params)
47
+ });
48
+ let connection;
49
+ const messageSink = [];
50
+ const closeTrigger = new import_async.Trigger();
51
+ const sendResponseMessage = createResponseSender(() => connection);
52
+ server.on("connection", (ws) => {
53
+ connection = ws;
54
+ ws.on("error", (err) => import_log.log.catch(err, void 0, {
55
+ F: __dxlog_file,
56
+ L: 34,
57
+ S: void 0,
58
+ C: (f, a) => f(...a)
59
+ }));
60
+ ws.on("message", async (data) => {
61
+ if (String(data) === "__ping__") {
62
+ ws.send("__pong__");
63
+ return;
64
+ }
65
+ const { request, requestPayload } = await decodeRequest(params, data);
66
+ if (params?.messageHandler) {
67
+ const responsePayload = await params.messageHandler(requestPayload);
68
+ if (responsePayload && connection) {
69
+ sendResponseMessage(request, responsePayload);
70
+ }
71
+ }
72
+ (0, import_log.log)("message", {
73
+ payload: requestPayload
74
+ }, {
75
+ F: __dxlog_file,
76
+ L: 47,
77
+ S: void 0,
78
+ C: (f, a) => f(...a)
79
+ });
80
+ messageSink.push(requestPayload);
81
+ });
82
+ ws.on("close", () => {
83
+ connection = void 0;
84
+ closeTrigger.wake();
85
+ });
86
+ });
87
+ return {
88
+ server,
89
+ messageSink,
90
+ endpoint: `ws://localhost:${port}`,
91
+ cleanup: () => server.close(),
92
+ currentConnection: () => connection,
93
+ sendResponseMessage,
94
+ closeConnection: () => {
95
+ closeTrigger.reset();
96
+ connection.close(1011);
97
+ return closeTrigger.wait();
98
+ }
99
+ };
100
+ };
101
+ var createConnectionDelayHandler = (params) => {
102
+ return (_, callback) => {
103
+ if (params?.admitConnection) {
104
+ (0, import_log.log)("delaying edge connection admission", void 0, {
105
+ F: __dxlog_file,
106
+ L: 75,
107
+ S: void 0,
108
+ C: (f, a) => f(...a)
109
+ });
110
+ void params.admitConnection.wait().then(() => {
111
+ callback(true);
112
+ (0, import_log.log)("edge connection admitted", void 0, {
113
+ F: __dxlog_file,
114
+ L: 78,
115
+ S: void 0,
116
+ C: (f, a) => f(...a)
117
+ });
118
+ });
119
+ } else {
120
+ callback(true);
121
+ }
122
+ };
123
+ };
124
+ var createResponseSender = (connection) => {
125
+ return (request, responsePayload) => {
126
+ const recipient = request.source;
127
+ connection().send(import_buf.buf.toBinary(import_messenger_pb.MessageSchema, import_buf.buf.create(import_messenger_pb.MessageSchema, {
128
+ source: {
129
+ identityKey: recipient.identityKey,
130
+ peerKey: recipient.peerKey
131
+ },
132
+ serviceId: request.serviceId,
133
+ payload: {
134
+ value: responsePayload
135
+ }
136
+ })));
137
+ };
138
+ };
139
+ var decodeRequest = async (params, data) => {
140
+ const request = import_buf.buf.fromBinary(import_messenger_pb.MessageSchema, await (0, import_chunk_ANV2HBEH.toUint8Array)(data));
141
+ const requestPayload = params?.payloadDecoder ? params.payloadDecoder(request.payload.value) : import_chunk_ANV2HBEH.protocol.getPayload(request, import_messenger_pb.TextMessageSchema);
142
+ return {
143
+ request,
144
+ requestPayload
145
+ };
146
+ };
147
+ // Annotate the CommonJS export names for ESM import in node:
148
+ 0 && (module.exports = {
149
+ DEFAULT_PORT,
150
+ createTestEdgeWsServer
151
+ });
152
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/testing/test-utils.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport WebSocket from 'isomorphic-ws';\n\nimport { Trigger } from '@dxos/async';\nimport { log } from '@dxos/log';\nimport { buf } from '@dxos/protocols/buf';\nimport { MessageSchema, TextMessageSchema, type Message } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\n\nimport { protocol } from '../defs';\nimport { toUint8Array } from '../protocol';\n\nexport const DEFAULT_PORT = 8080;\n\ntype TestEdgeWsServerParams = {\n admitConnection?: Trigger;\n payloadDecoder?: (payload: Uint8Array) => any;\n messageHandler?: (payload: any) => Promise<Uint8Array | undefined>;\n};\n\nexport const createTestEdgeWsServer = async (port = DEFAULT_PORT, params?: TestEdgeWsServerParams) => {\n const server = new WebSocket.Server({ port, verifyClient: createConnectionDelayHandler(params) });\n\n let connection: WebSocket | undefined;\n\n const messageSink: any[] = [];\n const closeTrigger = new Trigger();\n const sendResponseMessage = createResponseSender(() => connection!);\n\n server.on('connection', (ws) => {\n connection = ws;\n ws.on('error', (err) => log.catch(err));\n ws.on('message', async (data) => {\n if (String(data) === '__ping__') {\n ws.send('__pong__');\n return;\n }\n const { request, requestPayload } = await decodeRequest(params, data);\n if (params?.messageHandler) {\n const responsePayload = await params.messageHandler(requestPayload);\n if (responsePayload && connection) {\n sendResponseMessage(request, responsePayload);\n }\n }\n log('message', { payload: requestPayload });\n messageSink.push(requestPayload);\n });\n\n ws.on('close', () => {\n connection = undefined;\n closeTrigger.wake();\n });\n });\n\n return {\n server,\n messageSink,\n endpoint: `ws://localhost:${port}`,\n cleanup: () => server.close(),\n currentConnection: () => connection,\n sendResponseMessage,\n closeConnection: () => {\n closeTrigger.reset();\n connection!.close(1011);\n return closeTrigger.wait();\n },\n };\n};\n\nconst createConnectionDelayHandler = (params: TestEdgeWsServerParams | undefined) => {\n return (_: any, callback: (admit: boolean) => void) => {\n if (params?.admitConnection) {\n log('delaying edge connection admission');\n void params.admitConnection.wait().then(() => {\n callback(true);\n log('edge connection admitted');\n });\n } else {\n callback(true);\n }\n };\n};\n\nconst createResponseSender = (connection: () => WebSocket) => {\n return (request: Message, responsePayload: Uint8Array) => {\n const recipient = request.source!;\n connection().send(\n buf.toBinary(\n MessageSchema,\n buf.create(MessageSchema, {\n source: {\n identityKey: recipient.identityKey,\n peerKey: recipient.peerKey,\n },\n serviceId: request.serviceId!,\n payload: { value: responsePayload },\n }),\n ),\n );\n };\n};\n\nconst decodeRequest = async (params: TestEdgeWsServerParams | undefined, data: any) => {\n const request = buf.fromBinary(MessageSchema, await toUint8Array(data));\n const requestPayload = params?.payloadDecoder\n ? params.payloadDecoder(request.payload!.value!)\n : protocol.getPayload(request, TextMessageSchema);\n return { request, requestPayload };\n};\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,2BAAsB;AAEtB,mBAAwB;AACxB,iBAAoB;AACpB,iBAAoB;AACpB,0BAA+D;;AAKxD,IAAMA,eAAe;AAQrB,IAAMC,yBAAyB,OAAOC,OAAOF,cAAcG,WAAAA;AAChE,QAAMC,SAAS,IAAIC,qBAAAA,QAAUC,OAAO;IAAEJ;IAAMK,cAAcC,6BAA6BL,MAAAA;EAAQ,CAAA;AAE/F,MAAIM;AAEJ,QAAMC,cAAqB,CAAA;AAC3B,QAAMC,eAAe,IAAIC,qBAAAA;AACzB,QAAMC,sBAAsBC,qBAAqB,MAAML,UAAAA;AAEvDL,SAAOW,GAAG,cAAc,CAACC,OAAAA;AACvBP,iBAAaO;AACbA,OAAGD,GAAG,SAAS,CAACE,QAAQC,eAAIC,MAAMF,KAAAA,QAAAA;;;;;;AAClCD,OAAGD,GAAG,WAAW,OAAOK,SAAAA;AACtB,UAAIC,OAAOD,I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vBC,SAASP,UAAUO;MACrB;MACAC,WAAW5B,QAAQ4B;MACnBvB,SAAS;QAAEwB,OAAOzB;MAAgB;IACpC,CAAA,CAAA,CAAA;EAGN;AACF;AAEA,IAAMF,gBAAgB,OAAOtB,QAA4CiB,SAAAA;AACvE,QAAMG,UAAUsB,eAAIQ,WAAWN,mCAAe,UAAMO,oCAAalC,IAAAA,CAAAA;AACjE,QAAMI,iBAAiBrB,QAAQoD,iBAC3BpD,OAAOoD,eAAehC,QAAQK,QAASwB,KAAK,IAC5CI,+BAASC,WAAWlC,SAASmC,qCAAAA;AACjC,SAAO;IAAEnC;IAASC;EAAe;AACnC;",
6
+ "names": ["DEFAULT_PORT", "createTestEdgeWsServer", "port", "params", "server", "WebSocket", "Server", "verifyClient", "createConnectionDelayHandler", "connection", "messageSink", "closeTrigger", "Trigger", "sendResponseMessage", "createResponseSender", "on", "ws", "err", "log", "catch", "data", "String", "send", "request", "requestPayload", "decodeRequest", "messageHandler", "responsePayload", "payload", "push", "undefined", "wake", "endpoint", "cleanup", "close", "currentConnection", "closeConnection", "reset", "wait", "_", "callback", "admitConnection", "then", "recipient", "source", "buf", "toBinary", "MessageSchema", "create", "identityKey", "peerKey", "serviceId", "value", "fromBinary", "toUint8Array", "payloadDecoder", "protocol", "getPayload", "TextMessageSchema"]
7
+ }
@@ -0,0 +1,115 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+
3
+ // packages/core/mesh/edge-client/src/protocol.ts
4
+ import { invariant } from "@dxos/invariant";
5
+ import { buf, bufWkt } from "@dxos/protocols/buf";
6
+ import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
7
+ import { bufferToArray } from "@dxos/util";
8
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/protocol.ts";
9
+ var getTypename = (typeName) => `type.googleapis.com/${typeName}`;
10
+ var Protocol = class {
11
+ constructor(types) {
12
+ this._typeRegistry = buf.createRegistry(...types);
13
+ }
14
+ get typeRegistry() {
15
+ return this._typeRegistry;
16
+ }
17
+ toJson(message) {
18
+ try {
19
+ return buf.toJson(MessageSchema, message, {
20
+ registry: this.typeRegistry
21
+ });
22
+ } catch (err) {
23
+ return {
24
+ type: this.getPayloadType(message)
25
+ };
26
+ }
27
+ }
28
+ /**
29
+ * Return the payload with the given type.
30
+ */
31
+ getPayload(message, type) {
32
+ invariant(message.payload, void 0, {
33
+ F: __dxlog_file,
34
+ L: 40,
35
+ S: this,
36
+ A: [
37
+ "message.payload",
38
+ ""
39
+ ]
40
+ });
41
+ const payloadTypename = this.getPayloadType(message);
42
+ if (type && type.typeName !== payloadTypename) {
43
+ throw new Error(`Unexpected payload type: ${payloadTypename}; expected ${type.typeName}`);
44
+ }
45
+ invariant(bufWkt.anyIs(message.payload, type), `Unexpected payload type: ${payloadTypename}}`, {
46
+ F: __dxlog_file,
47
+ L: 46,
48
+ S: this,
49
+ A: [
50
+ "bufWkt.anyIs(message.payload, type)",
51
+ "`Unexpected payload type: ${payloadTypename}}`"
52
+ ]
53
+ });
54
+ const payload = bufWkt.anyUnpack(message.payload, this.typeRegistry);
55
+ invariant(payload, `Empty payload: ${payloadTypename}}`, {
56
+ F: __dxlog_file,
57
+ L: 48,
58
+ S: this,
59
+ A: [
60
+ "payload",
61
+ "`Empty payload: ${payloadTypename}}`"
62
+ ]
63
+ });
64
+ return payload;
65
+ }
66
+ /**
67
+ * Get the payload type.
68
+ */
69
+ getPayloadType(message) {
70
+ if (!message.payload) {
71
+ return void 0;
72
+ }
73
+ const [, type] = message.payload.typeUrl.split("/");
74
+ return type;
75
+ }
76
+ /**
77
+ * Create a packed message.
78
+ */
79
+ createMessage(type, { source, target, payload, serviceId }) {
80
+ return buf.create(MessageSchema, {
81
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
82
+ source,
83
+ target,
84
+ serviceId,
85
+ payload: payload ? bufWkt.anyPack(type, buf.create(type, payload)) : void 0
86
+ });
87
+ }
88
+ };
89
+ var toUint8Array = async (data) => {
90
+ if (data instanceof Buffer) {
91
+ return bufferToArray(data);
92
+ }
93
+ if (data instanceof Blob) {
94
+ return new Uint8Array(await data.arrayBuffer());
95
+ }
96
+ throw new Error(`Unexpected datatype: ${data}`);
97
+ };
98
+
99
+ // packages/core/mesh/edge-client/src/defs.ts
100
+ import { bufWkt as bufWkt2 } from "@dxos/protocols/buf";
101
+ import { SwarmRequestSchema, SwarmResponseSchema, TextMessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
102
+ var protocol = new Protocol([
103
+ SwarmRequestSchema,
104
+ SwarmResponseSchema,
105
+ TextMessageSchema,
106
+ bufWkt2.AnySchema
107
+ ]);
108
+
109
+ export {
110
+ getTypename,
111
+ Protocol,
112
+ toUint8Array,
113
+ protocol
114
+ };
115
+ //# sourceMappingURL=chunk-HNVT57AU.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/protocol.ts", "../../../src/defs.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { buf, bufWkt } from '@dxos/protocols/buf';\nimport { type Message, MessageSchema, type PeerSchema } from '@dxos/protocols/buf/dxos/edge/messenger_pb';\nimport { bufferToArray } from '@dxos/util';\n\nexport type PeerData = buf.MessageInitShape<typeof PeerSchema>;\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\nimport { bufWkt } from '@dxos/protocols/buf';\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, bufWkt.AnySchema]);\n"],
5
+ "mappings": ";;;AAIA,SAASA,iBAAiB;AAC1B,SAASC,KAAKC,cAAc;AAC5B,SAAuBC,qBAAsC;AAC7D,SAASC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OAAOkC,QAAQnB,MAAMhB,IAAI+B,OAAOf,MAAMG,OAAAA,CAAAA,IAAYK;IACvE,CAAA;EACF;AACF;AAKO,IAAMY,eAAe,OAAOC,SAAAA;AAEjC,MAAIA,gBAAgBC,QAAQ;AAC1B,WAAOnC,cAAckC,IAAAA;EACvB;AAGA,MAAIA,gBAAgBE,MAAM;AACxB,WAAO,IAAIC,WAAW,MAAOH,KAAcI,YAAW,CAAA;EACxD;AAEA,QAAM,IAAIpB,MAAM,wBAAwBgB,IAAAA,EAAM;AAChD;;;ACrGA,SAASK,UAAAA,eAAc;AACvB,SAASC,oBAAoBC,qBAAqBC,yBAAyB;AAIpE,IAAMC,WAAW,IAAIC,SAAS;EAACC;EAAoBC;EAAqBC;EAAmBC,QAAOC;CAAU;",
6
+ "names": ["invariant", "buf", "bufWkt", "MessageSchema", "bufferToArray", "getTypename", "typeName", "Protocol", "constructor", "types", "_typeRegistry", "createRegistry", "typeRegistry", "toJson", "message", "registry", "err", "type", "getPayloadType", "getPayload", "payload", "payloadTypename", "Error", "anyIs", "anyUnpack", "undefined", "typeUrl", "split", "createMessage", "source", "target", "serviceId", "create", "timestamp", "Date", "toISOString", "anyPack", "toUint8Array", "data", "Buffer", "Blob", "Uint8Array", "arrayBuffer", "bufWkt", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "protocol", "Protocol", "SwarmRequestSchema", "SwarmResponseSchema", "TextMessageSchema", "bufWkt", "AnySchema"]
7
+ }
@@ -0,0 +1,378 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ Protocol,
4
+ getTypename,
5
+ protocol,
6
+ toUint8Array
7
+ } from "./chunk-HNVT57AU.mjs";
8
+
9
+ // packages/core/mesh/edge-client/src/index.ts
10
+ export * from "@dxos/protocols/buf/dxos/edge/messenger_pb";
11
+
12
+ // packages/core/mesh/edge-client/src/edge-client.ts
13
+ import WebSocket from "isomorphic-ws";
14
+ import { Trigger, Event, scheduleTaskInterval, scheduleTask, TriggerState } from "@dxos/async";
15
+ import { Context, LifecycleState as LifecycleState2, Resource as Resource2 } from "@dxos/context";
16
+ import { log as log2 } from "@dxos/log";
17
+ import { buf } from "@dxos/protocols/buf";
18
+ import { MessageSchema } from "@dxos/protocols/buf/dxos/edge/messenger_pb";
19
+
20
+ // packages/core/mesh/edge-client/src/errors.ts
21
+ var EdgeConnectionClosedError = class extends Error {
22
+ constructor() {
23
+ super("Edge connection closed.");
24
+ }
25
+ };
26
+ var EdgeIdentityChangedError = class extends Error {
27
+ constructor() {
28
+ super("Edge identity changed.");
29
+ }
30
+ };
31
+
32
+ // packages/core/mesh/edge-client/src/persistent-lifecycle.ts
33
+ import { DeferredTask, sleep, synchronized } from "@dxos/async";
34
+ import { cancelWithContext, LifecycleState, Resource } from "@dxos/context";
35
+ import { warnAfterTimeout } from "@dxos/debug";
36
+ import { log } from "@dxos/log";
37
+ function _ts_decorate(decorators, target, key, desc) {
38
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
39
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
40
+ 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;
41
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
42
+ }
43
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/persistent-lifecycle.ts";
44
+ var INIT_RESTART_DELAY = 100;
45
+ var DEFAULT_MAX_RESTART_DELAY = 5e3;
46
+ var PersistentLifecycle = class extends Resource {
47
+ constructor({ start, stop, onRestart, maxRestartDelay = DEFAULT_MAX_RESTART_DELAY }) {
48
+ super();
49
+ this._restartTask = void 0;
50
+ this._restartAfter = 0;
51
+ this._start = start;
52
+ this._stop = stop;
53
+ this._onRestart = onRestart;
54
+ this._maxRestartDelay = maxRestartDelay;
55
+ }
56
+ async _open() {
57
+ this._restartTask = new DeferredTask(this._ctx, async () => {
58
+ try {
59
+ await this._restart();
60
+ } catch (err) {
61
+ log.warn("Restart failed", {
62
+ err
63
+ }, {
64
+ F: __dxlog_file,
65
+ L: 64,
66
+ S: this,
67
+ C: (f, a) => f(...a)
68
+ });
69
+ this._restartTask?.schedule();
70
+ }
71
+ });
72
+ await this._start().catch((err) => {
73
+ log.warn("Start failed", {
74
+ err
75
+ }, {
76
+ F: __dxlog_file,
77
+ L: 69,
78
+ S: this,
79
+ C: (f, a) => f(...a)
80
+ });
81
+ this._restartTask?.schedule();
82
+ });
83
+ }
84
+ async _close() {
85
+ await this._restartTask?.join();
86
+ await this._stop();
87
+ this._restartTask = void 0;
88
+ }
89
+ async _restart() {
90
+ log(`restarting in ${this._restartAfter}ms`, {
91
+ state: this._lifecycleState
92
+ }, {
93
+ F: __dxlog_file,
94
+ L: 81,
95
+ S: this,
96
+ C: (f, a) => f(...a)
97
+ });
98
+ await this._stop();
99
+ if (this._lifecycleState !== LifecycleState.OPEN) {
100
+ return;
101
+ }
102
+ await cancelWithContext(this._ctx, sleep(this._restartAfter));
103
+ this._restartAfter = Math.min(Math.max(this._restartAfter * 2, INIT_RESTART_DELAY), this._maxRestartDelay);
104
+ await warnAfterTimeout(5e3, "Connection establishment takes too long", () => this._start());
105
+ this._restartAfter = 0;
106
+ await this._onRestart?.();
107
+ }
108
+ /**
109
+ * Scheduling restart should be done from outside.
110
+ */
111
+ scheduleRestart() {
112
+ if (this._lifecycleState !== LifecycleState.OPEN) {
113
+ return;
114
+ }
115
+ this._restartTask.schedule();
116
+ }
117
+ };
118
+ _ts_decorate([
119
+ synchronized
120
+ ], PersistentLifecycle.prototype, "_open", null);
121
+ _ts_decorate([
122
+ synchronized
123
+ ], PersistentLifecycle.prototype, "scheduleRestart", null);
124
+
125
+ // packages/core/mesh/edge-client/src/edge-client.ts
126
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/core/mesh/edge-client/src/edge-client.ts";
127
+ var DEFAULT_TIMEOUT = 1e4;
128
+ var SIGNAL_KEEPALIVE_INTERVAL = 5e3;
129
+ var EdgeClient = class extends Resource2 {
130
+ constructor(_identityKey, _peerKey, _config) {
131
+ super();
132
+ this._identityKey = _identityKey;
133
+ this._peerKey = _peerKey;
134
+ this._config = _config;
135
+ this.reconnect = new Event();
136
+ this.connected = new Event();
137
+ this._persistentLifecycle = new PersistentLifecycle({
138
+ start: async () => this._openWebSocket(),
139
+ stop: async () => this._closeWebSocket(),
140
+ onRestart: async () => this.reconnect.emit()
141
+ });
142
+ this._listeners = /* @__PURE__ */ new Set();
143
+ this._ready = new Trigger();
144
+ this._ws = void 0;
145
+ this._keepaliveCtx = void 0;
146
+ this._heartBeatContext = void 0;
147
+ }
148
+ // TODO(burdon): Attach logging.
149
+ get info() {
150
+ return {
151
+ open: this.isOpen,
152
+ identity: this._identityKey,
153
+ device: this._peerKey
154
+ };
155
+ }
156
+ get identityKey() {
157
+ return this._identityKey;
158
+ }
159
+ get peerKey() {
160
+ return this._peerKey;
161
+ }
162
+ setIdentity({ peerKey, identityKey }) {
163
+ this._peerKey = peerKey;
164
+ this._identityKey = identityKey;
165
+ this._persistentLifecycle.scheduleRestart();
166
+ }
167
+ addListener(listener) {
168
+ this._listeners.add(listener);
169
+ return () => this._listeners.delete(listener);
170
+ }
171
+ /**
172
+ * Open connection to messaging service.
173
+ */
174
+ async _open() {
175
+ log2("opening...", {
176
+ info: this.info
177
+ }, {
178
+ F: __dxlog_file2,
179
+ L: 100,
180
+ S: this,
181
+ C: (f, a) => f(...a)
182
+ });
183
+ this._persistentLifecycle.open().catch((err) => {
184
+ log2.warn("Error while opening connection", {
185
+ err
186
+ }, {
187
+ F: __dxlog_file2,
188
+ L: 102,
189
+ S: this,
190
+ C: (f, a) => f(...a)
191
+ });
192
+ });
193
+ }
194
+ /**
195
+ * Close connection and free resources.
196
+ */
197
+ async _close() {
198
+ log2("closing...", {
199
+ peerKey: this._peerKey
200
+ }, {
201
+ F: __dxlog_file2,
202
+ L: 110,
203
+ S: this,
204
+ C: (f, a) => f(...a)
205
+ });
206
+ await this._persistentLifecycle.close();
207
+ }
208
+ async _openWebSocket() {
209
+ const url = new URL(`/ws/${this._identityKey}/${this._peerKey}`, this._config.socketEndpoint);
210
+ this._ws = new WebSocket(url);
211
+ this._ws.onopen = () => {
212
+ log2("opened", this.info, {
213
+ F: __dxlog_file2,
214
+ L: 119,
215
+ S: this,
216
+ C: (f, a) => f(...a)
217
+ });
218
+ this._ready.wake();
219
+ this.connected.emit();
220
+ };
221
+ this._ws.onclose = () => {
222
+ log2("closed", this.info, {
223
+ F: __dxlog_file2,
224
+ L: 124,
225
+ S: this,
226
+ C: (f, a) => f(...a)
227
+ });
228
+ this._persistentLifecycle.scheduleRestart();
229
+ };
230
+ this._ws.onerror = (event) => {
231
+ log2.warn("EdgeClient socket error", {
232
+ error: event.error,
233
+ info: event.message
234
+ }, {
235
+ F: __dxlog_file2,
236
+ L: 128,
237
+ S: this,
238
+ C: (f, a) => f(...a)
239
+ });
240
+ this._persistentLifecycle.scheduleRestart();
241
+ };
242
+ this._ws.onmessage = async (event) => {
243
+ if (event.data === "__pong__") {
244
+ this._onHeartbeat();
245
+ return;
246
+ }
247
+ const data = await toUint8Array(event.data);
248
+ const message = buf.fromBinary(MessageSchema, data);
249
+ log2("received", {
250
+ peerKey: this._peerKey,
251
+ payload: protocol.getPayloadType(message)
252
+ }, {
253
+ F: __dxlog_file2,
254
+ L: 141,
255
+ S: this,
256
+ C: (f, a) => f(...a)
257
+ });
258
+ if (message) {
259
+ for (const listener of this._listeners) {
260
+ try {
261
+ await listener(message);
262
+ } catch (err) {
263
+ log2.error("processing", {
264
+ err,
265
+ payload: protocol.getPayloadType(message)
266
+ }, {
267
+ F: __dxlog_file2,
268
+ L: 147,
269
+ S: this,
270
+ C: (f, a) => f(...a)
271
+ });
272
+ }
273
+ }
274
+ }
275
+ };
276
+ await this._ready.wait({
277
+ timeout: this._config.timeout ?? DEFAULT_TIMEOUT
278
+ });
279
+ this._keepaliveCtx = new Context(void 0, {
280
+ F: __dxlog_file2,
281
+ L: 154
282
+ });
283
+ scheduleTaskInterval(this._keepaliveCtx, async () => {
284
+ this._ws?.send("__ping__");
285
+ }, SIGNAL_KEEPALIVE_INTERVAL);
286
+ this._ws.send("__ping__");
287
+ this._onHeartbeat();
288
+ }
289
+ async _closeWebSocket() {
290
+ if (!this._ws) {
291
+ return;
292
+ }
293
+ try {
294
+ this._ready.throw(this.isOpen ? new EdgeIdentityChangedError() : new EdgeConnectionClosedError());
295
+ this._ready.reset();
296
+ void this._keepaliveCtx?.dispose();
297
+ this._keepaliveCtx = void 0;
298
+ void this._heartBeatContext?.dispose();
299
+ this._heartBeatContext = void 0;
300
+ this._ws.onopen = () => {
301
+ };
302
+ this._ws.onclose = () => {
303
+ };
304
+ this._ws.onerror = () => {
305
+ };
306
+ this._ws.close();
307
+ this._ws = void 0;
308
+ } catch (err) {
309
+ if (err instanceof Error && err.message.includes("WebSocket is closed before the connection is established.")) {
310
+ return;
311
+ }
312
+ log2.warn("Error closing websocket", {
313
+ err
314
+ }, {
315
+ F: __dxlog_file2,
316
+ L: 190,
317
+ S: this,
318
+ C: (f, a) => f(...a)
319
+ });
320
+ }
321
+ }
322
+ /**
323
+ * Send message.
324
+ * NOTE: The message is guaranteed to be delivered but the service must respond with a message to confirm processing.
325
+ */
326
+ async send(message) {
327
+ if (this._ready.state !== TriggerState.RESOLVED) {
328
+ log2("waiting for websocket to become ready", void 0, {
329
+ F: __dxlog_file2,
330
+ L: 200,
331
+ S: this,
332
+ C: (f, a) => f(...a)
333
+ });
334
+ await this._ready.wait({
335
+ timeout: this._config.timeout ?? DEFAULT_TIMEOUT
336
+ });
337
+ }
338
+ if (!this._ws) {
339
+ throw new EdgeConnectionClosedError();
340
+ }
341
+ if (message.source && (message.source.peerKey !== this._peerKey || message.source.identityKey !== this.identityKey)) {
342
+ throw new EdgeIdentityChangedError();
343
+ }
344
+ log2("sending...", {
345
+ peerKey: this._peerKey,
346
+ payload: protocol.getPayloadType(message)
347
+ }, {
348
+ F: __dxlog_file2,
349
+ L: 213,
350
+ S: this,
351
+ C: (f, a) => f(...a)
352
+ });
353
+ this._ws.send(buf.toBinary(MessageSchema, message));
354
+ }
355
+ _onHeartbeat() {
356
+ if (this._lifecycleState !== LifecycleState2.OPEN) {
357
+ return;
358
+ }
359
+ void this._heartBeatContext?.dispose();
360
+ this._heartBeatContext = new Context(void 0, {
361
+ F: __dxlog_file2,
362
+ L: 222
363
+ });
364
+ scheduleTask(this._heartBeatContext, () => {
365
+ this._persistentLifecycle.scheduleRestart();
366
+ }, 2 * SIGNAL_KEEPALIVE_INTERVAL);
367
+ }
368
+ };
369
+ export {
370
+ EdgeClient,
371
+ EdgeConnectionClosedError,
372
+ EdgeIdentityChangedError,
373
+ Protocol,
374
+ getTypename,
375
+ protocol,
376
+ toUint8Array
377
+ };
378
+ //# sourceMappingURL=index.mjs.map