@peers-app/peers-sdk 0.14.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/dist/context/data-context.d.ts +4 -4
  2. package/dist/context/data-context.js +1 -1
  3. package/dist/context/index.d.ts +3 -3
  4. package/dist/context/index.js +4 -0
  5. package/dist/context/user-context-singleton.js +13 -14
  6. package/dist/context/user-context.d.ts +4 -4
  7. package/dist/context/user-context.js +48 -31
  8. package/dist/data/assistants.d.ts +1 -1
  9. package/dist/data/assistants.js +35 -24
  10. package/dist/data/change-tracking.d.ts +8 -8
  11. package/dist/data/change-tracking.js +45 -39
  12. package/dist/data/channels.js +5 -5
  13. package/dist/data/data-locks.d.ts +2 -2
  14. package/dist/data/data-locks.js +21 -23
  15. package/dist/data/data-locks.test.js +73 -75
  16. package/dist/data/device-sync-info.d.ts +1 -1
  17. package/dist/data/device-sync-info.js +4 -4
  18. package/dist/data/devices.d.ts +1 -1
  19. package/dist/data/devices.js +9 -12
  20. package/dist/data/embeddings.js +14 -11
  21. package/dist/data/files/file-read-stream.d.ts +2 -2
  22. package/dist/data/files/file-read-stream.js +23 -14
  23. package/dist/data/files/file-write-stream.d.ts +2 -2
  24. package/dist/data/files/file-write-stream.js +8 -8
  25. package/dist/data/files/file.types.d.ts +2 -2
  26. package/dist/data/files/file.types.js +17 -11
  27. package/dist/data/files/files.d.ts +6 -6
  28. package/dist/data/files/files.js +17 -19
  29. package/dist/data/files/files.test.js +213 -214
  30. package/dist/data/files/index.d.ts +4 -4
  31. package/dist/data/files/index.js +4 -4
  32. package/dist/data/group-member-roles.js +2 -2
  33. package/dist/data/group-members.d.ts +5 -5
  34. package/dist/data/group-members.js +27 -18
  35. package/dist/data/group-members.test.js +73 -73
  36. package/dist/data/group-permissions.d.ts +3 -3
  37. package/dist/data/group-permissions.js +13 -11
  38. package/dist/data/group-share.d.ts +2 -2
  39. package/dist/data/group-share.js +29 -24
  40. package/dist/data/groups.d.ts +4 -4
  41. package/dist/data/groups.js +27 -19
  42. package/dist/data/groups.test.js +44 -44
  43. package/dist/data/index.d.ts +6 -6
  44. package/dist/data/index.js +6 -6
  45. package/dist/data/knowledge/peer-types.js +9 -9
  46. package/dist/data/messages.d.ts +5 -5
  47. package/dist/data/messages.js +43 -30
  48. package/dist/data/orm/client-proxy.data-source.d.ts +4 -4
  49. package/dist/data/orm/client-proxy.data-source.js +10 -12
  50. package/dist/data/orm/cursor.d.ts +1 -1
  51. package/dist/data/orm/cursor.js +2 -2
  52. package/dist/data/orm/cursor.test.js +92 -93
  53. package/dist/data/orm/data-query.d.ts +3 -3
  54. package/dist/data/orm/data-query.js +24 -18
  55. package/dist/data/orm/data-query.mongo.d.ts +1 -1
  56. package/dist/data/orm/data-query.mongo.js +49 -51
  57. package/dist/data/orm/data-query.mongo.test.js +173 -204
  58. package/dist/data/orm/data-query.sqlite.d.ts +1 -1
  59. package/dist/data/orm/data-query.sqlite.js +84 -73
  60. package/dist/data/orm/data-query.sqlite.test.js +164 -176
  61. package/dist/data/orm/data-query.test.js +216 -224
  62. package/dist/data/orm/decorators.js +3 -3
  63. package/dist/data/orm/dependency-injection.test.js +53 -56
  64. package/dist/data/orm/doc.d.ts +4 -4
  65. package/dist/data/orm/doc.js +17 -21
  66. package/dist/data/orm/event-registry.d.ts +1 -1
  67. package/dist/data/orm/event-registry.test.js +16 -16
  68. package/dist/data/orm/factory.d.ts +2 -2
  69. package/dist/data/orm/factory.js +33 -33
  70. package/dist/data/orm/index.d.ts +10 -10
  71. package/dist/data/orm/index.js +10 -10
  72. package/dist/data/orm/multi-cursors.d.ts +1 -1
  73. package/dist/data/orm/multi-cursors.js +6 -6
  74. package/dist/data/orm/multi-cursors.test.js +152 -144
  75. package/dist/data/orm/sql.data-source.d.ts +7 -7
  76. package/dist/data/orm/sql.data-source.js +88 -93
  77. package/dist/data/orm/sql.data-source.test.js +109 -101
  78. package/dist/data/orm/subscribable.data-source.d.ts +4 -4
  79. package/dist/data/orm/subscribable.data-source.js +5 -5
  80. package/dist/data/orm/table-container-events.test.js +34 -26
  81. package/dist/data/orm/table-container.d.ts +6 -6
  82. package/dist/data/orm/table-container.js +33 -21
  83. package/dist/data/orm/table-container.test.js +64 -53
  84. package/dist/data/orm/table-definitions.system.d.ts +3 -3
  85. package/dist/data/orm/table-definitions.system.js +3 -3
  86. package/dist/data/orm/table-definitions.type.d.ts +5 -5
  87. package/dist/data/orm/table-dependencies.d.ts +2 -2
  88. package/dist/data/orm/table.d.ts +5 -5
  89. package/dist/data/orm/table.event-source.test.js +105 -115
  90. package/dist/data/orm/table.js +35 -34
  91. package/dist/data/orm/types.d.ts +3 -3
  92. package/dist/data/orm/types.js +26 -25
  93. package/dist/data/orm/types.test.js +166 -92
  94. package/dist/data/package-permissions.d.ts +1 -1
  95. package/dist/data/package-permissions.js +2 -2
  96. package/dist/data/package-version-permissions.d.ts +1 -1
  97. package/dist/data/package-version-permissions.js +2 -2
  98. package/dist/data/package-versions.d.ts +9 -9
  99. package/dist/data/package-versions.js +47 -33
  100. package/dist/data/packages.d.ts +2 -2
  101. package/dist/data/packages.js +36 -18
  102. package/dist/data/packages.utils.d.ts +2 -2
  103. package/dist/data/packages.utils.js +4 -4
  104. package/dist/data/persistent-vars.d.ts +15 -15
  105. package/dist/data/persistent-vars.js +165 -154
  106. package/dist/data/table-definitions-table.d.ts +5 -5
  107. package/dist/data/table-definitions-table.js +13 -12
  108. package/dist/data/tool-tests.js +6 -6
  109. package/dist/data/tools.js +29 -19
  110. package/dist/data/user-permissions.d.ts +1 -1
  111. package/dist/data/user-permissions.js +5 -5
  112. package/dist/data/user-permissions.test.js +90 -88
  113. package/dist/data/user-trust-levels.js +10 -10
  114. package/dist/data/users.d.ts +4 -4
  115. package/dist/data/users.js +16 -15
  116. package/dist/data/voice-messages.d.ts +2 -2
  117. package/dist/data/voice-messages.js +13 -13
  118. package/dist/data/welcome-modal.pvar.js +3 -1
  119. package/dist/data/workflow-logs.js +26 -18
  120. package/dist/data/workflow-runs.d.ts +6 -6
  121. package/dist/data/workflow-runs.js +70 -44
  122. package/dist/data/workflows.d.ts +2 -2
  123. package/dist/data/workflows.js +7 -9
  124. package/dist/device/binary-peer-connection-v2.d.ts +7 -7
  125. package/dist/device/binary-peer-connection-v2.js +32 -28
  126. package/dist/device/binary-peer-connection-v2.test.js +80 -67
  127. package/dist/device/binary-peer-connection.d.ts +7 -7
  128. package/dist/device/binary-peer-connection.js +29 -28
  129. package/dist/device/binary-peer-connection.test.js +35 -31
  130. package/dist/device/connection.d.ts +5 -5
  131. package/dist/device/connection.js +59 -48
  132. package/dist/device/connection.test.js +74 -68
  133. package/dist/device/device-election.d.ts +2 -2
  134. package/dist/device/device-election.js +25 -20
  135. package/dist/device/device-election.test.js +35 -36
  136. package/dist/device/device.d.ts +2 -2
  137. package/dist/device/device.js +10 -4
  138. package/dist/device/device.test.js +16 -17
  139. package/dist/device/get-trust-level-fn.d.ts +2 -2
  140. package/dist/device/get-trust-level-fn.js +22 -11
  141. package/dist/device/get-trust-level-fn.test.js +58 -58
  142. package/dist/device/socket-io-binary-peer.d.ts +1 -1
  143. package/dist/device/socket-io-binary-peer.js +16 -13
  144. package/dist/device/socket.type.d.ts +2 -2
  145. package/dist/device/streamed-socket.d.ts +2 -2
  146. package/dist/device/streamed-socket.js +8 -8
  147. package/dist/device/streamed-socket.test.js +40 -40
  148. package/dist/device/tx-encoding.test.js +77 -77
  149. package/dist/events.d.ts +1 -1
  150. package/dist/events.js +5 -2
  151. package/dist/group-invite/group-invite.js +110 -19
  152. package/dist/group-invite/group-invite.pvars.d.ts +2 -2
  153. package/dist/group-invite/group-invite.pvars.js +21 -13
  154. package/dist/group-invite/group-invite.types.d.ts +1 -1
  155. package/dist/group-invite/index.d.ts +3 -3
  156. package/dist/group-invite/index.js +1 -1
  157. package/dist/index.d.ts +25 -24
  158. package/dist/index.js +30 -25
  159. package/dist/keys.d.ts +3 -3
  160. package/dist/keys.js +31 -30
  161. package/dist/keys.test.js +69 -61
  162. package/dist/logging/console-logger.d.ts +1 -1
  163. package/dist/logging/console-logger.js +35 -40
  164. package/dist/logging/console-logger.test.js +115 -115
  165. package/dist/logging/console-logs.table.d.ts +3 -3
  166. package/dist/logging/console-logs.table.js +28 -23
  167. package/dist/mentions.js +16 -12
  168. package/dist/observable.d.ts +2 -2
  169. package/dist/observable.js +15 -9
  170. package/dist/observable.test.js +47 -47
  171. package/dist/package-loader/get-require.js +3 -4
  172. package/dist/package-loader/package-loader.d.ts +2 -2
  173. package/dist/package-loader/package-loader.js +52 -34
  174. package/dist/peers-ui/peers-ui.d.ts +2 -2
  175. package/dist/peers-ui/peers-ui.js +2 -4
  176. package/dist/peers-ui/peers-ui.types.d.ts +3 -3
  177. package/dist/peers-ui/peers-ui.types.js +0 -1
  178. package/dist/rpc-types.d.ts +61 -59
  179. package/dist/rpc-types.js +61 -55
  180. package/dist/serial-json.d.ts +1 -1
  181. package/dist/serial-json.js +50 -43
  182. package/dist/serial-json.test.js +22 -22
  183. package/dist/system-ids.js +8 -8
  184. package/dist/tools/index.d.ts +1 -1
  185. package/dist/tools/tools-factory.d.ts +1 -1
  186. package/dist/tools/tools-factory.js +2 -2
  187. package/dist/types/assistant-runner-args.d.ts +3 -3
  188. package/dist/types/peer-device.d.ts +1 -1
  189. package/dist/types/peers-package.d.ts +3 -3
  190. package/dist/types/workflow-logger.d.ts +1 -1
  191. package/dist/types/workflow-run-context.d.ts +4 -4
  192. package/dist/types/workflow.d.ts +4 -4
  193. package/dist/types/workflow.js +27 -14
  194. package/dist/types/zod-types.d.ts +2 -1
  195. package/dist/types/zod-types.js +9 -3
  196. package/dist/user-connect/connection-code.d.ts +1 -1
  197. package/dist/user-connect/connection-code.js +7 -7
  198. package/dist/user-connect/connection-code.test.js +106 -106
  199. package/dist/user-connect/index.d.ts +3 -3
  200. package/dist/user-connect/index.js +1 -1
  201. package/dist/user-connect/user-connect.pvars.js +13 -11
  202. package/dist/user-connect/user-connect.types.d.ts +3 -3
  203. package/dist/users.query.d.ts +2 -2
  204. package/dist/users.query.js +40 -30
  205. package/dist/utils.d.ts +2 -2
  206. package/dist/utils.js +34 -32
  207. package/dist/utils.test.js +12 -8
  208. package/dist/workflow-log-formatter.d.ts +1 -1
  209. package/dist/workflow-log-formatter.js +17 -18
  210. package/package.json +14 -8
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const keys_1 = require("../keys");
4
- const device_1 = require("./device");
5
4
  const utils_1 = require("../utils");
6
5
  const binary_peer_connection_1 = require("./binary-peer-connection");
6
+ const device_1 = require("./device");
7
7
  // Mock binary peer for testing - simulates WebRTC peers
8
8
  class MockBinaryPeer {
9
9
  dataHandler;
@@ -11,26 +11,26 @@ class MockBinaryPeer {
11
11
  drainHandler;
12
12
  otherPeer;
13
13
  on(event, handler) {
14
- if (event === 'data') {
14
+ if (event === "data") {
15
15
  this.dataHandler = handler;
16
16
  }
17
- else if (event === 'close') {
17
+ else if (event === "close") {
18
18
  this.closeHandler = handler;
19
19
  }
20
- else if (event === 'drain') {
20
+ else if (event === "drain") {
21
21
  this.drainHandler = handler;
22
22
  }
23
23
  }
24
24
  send(data) {
25
25
  if (this.otherPeer?.dataHandler) {
26
- const bytes = typeof data === 'string'
26
+ const bytes = typeof data === "string"
27
27
  ? Buffer.from(data)
28
28
  : Buffer.isBuffer(data)
29
29
  ? data
30
30
  : Buffer.from(data);
31
31
  // Simulate async delivery
32
32
  setImmediate(() => {
33
- this.otherPeer.dataHandler(bytes);
33
+ this.otherPeer?.dataHandler?.(bytes);
34
34
  });
35
35
  }
36
36
  }
@@ -53,41 +53,45 @@ async function getMockPeerPair() {
53
53
  rPeer,
54
54
  };
55
55
  }
56
- describe('binary-peer-connection', () => {
57
- describe('MockBinaryPeer', () => {
58
- it('should exchange data between mock peers', async () => {
56
+ describe("binary-peer-connection", () => {
57
+ describe("MockBinaryPeer", () => {
58
+ it("should exchange data between mock peers", async () => {
59
59
  const { iPeer, rPeer } = await getMockPeerPair();
60
- let iPeerDataReceived = '';
61
- iPeer.on('data', (data) => {
60
+ let iPeerDataReceived = "";
61
+ iPeer.on("data", (data) => {
62
62
  iPeerDataReceived += data.toString();
63
63
  });
64
- let rPeerDataReceived = '';
65
- rPeer.on('data', (data) => {
64
+ let rPeerDataReceived = "";
65
+ rPeer.on("data", (data) => {
66
66
  rPeerDataReceived += data.toString();
67
67
  });
68
- rPeer.send('rPeer');
69
- iPeer.send('iPeer');
68
+ rPeer.send("rPeer");
69
+ iPeer.send("iPeer");
70
70
  await (0, utils_1.sleep)(50);
71
- expect(iPeerDataReceived).toBe('rPeer');
72
- expect(rPeerDataReceived).toBe('iPeer');
71
+ expect(iPeerDataReceived).toBe("rPeer");
72
+ expect(rPeerDataReceived).toBe("iPeer");
73
73
  // Clean up
74
74
  iPeer.destroy();
75
75
  rPeer.destroy();
76
76
  });
77
77
  });
78
- describe('wrapBinaryPeer', () => {
79
- it('should successfully handshake over wrtc protocol', async () => {
78
+ describe("wrapBinaryPeer", () => {
79
+ it("should successfully handshake over wrtc protocol", async () => {
80
80
  const { iPeer, rPeer } = await getMockPeerPair();
81
81
  const userId = `00000000000000000user001`;
82
82
  const keys = (0, keys_1.newKeys)();
83
83
  const connectionId = `00000000000000000conn001`;
84
84
  const iPeerDeviceId = `000000000000000device001`;
85
85
  const iPeerDevice = new device_1.Device(userId, iPeerDeviceId, keys);
86
- const iPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, iPeer, iPeerDevice, true, { protocol: 'wrtc' });
86
+ const iPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, iPeer, iPeerDevice, true, {
87
+ protocol: "wrtc",
88
+ });
87
89
  const rPeerDeviceId = `000000000000000device002`;
88
90
  const rPeerDevice = new device_1.Device(userId, rPeerDeviceId, keys);
89
- const rPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, rPeer, rPeerDevice, false, { protocol: 'wrtc' });
90
- await iPeerConnection.doHandshake('wrtc://' + connectionId);
91
+ const rPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, rPeer, rPeerDevice, false, {
92
+ protocol: "wrtc",
93
+ });
94
+ await iPeerConnection.doHandshake(`wrtc://${connectionId}`);
91
95
  expect(rPeerConnection.remoteDeviceInfo).toBeDefined();
92
96
  expect(rPeerConnection.remoteDeviceInfo?.deviceId).toBe(iPeerDeviceId);
93
97
  expect(iPeerConnection.remoteDeviceInfo).toBeDefined();
@@ -96,7 +100,7 @@ describe('binary-peer-connection', () => {
96
100
  iPeer.destroy();
97
101
  rPeer.destroy();
98
102
  });
99
- it('should set secureLocal/secureRemote when markTransportSecure is true', async () => {
103
+ it("should set secureLocal/secureRemote when markTransportSecure is true", async () => {
100
104
  const { iPeer, rPeer } = await getMockPeerPair();
101
105
  const userId = `00000000000000000user001`;
102
106
  const keys = (0, keys_1.newKeys)();
@@ -104,14 +108,14 @@ describe('binary-peer-connection', () => {
104
108
  const iPeerDeviceId = `000000000000000device001`;
105
109
  const iPeerDevice = new device_1.Device(userId, iPeerDeviceId, keys);
106
110
  const iPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, iPeer, iPeerDevice, true, {
107
- protocol: 'wrtc',
108
- markTransportSecure: true
111
+ protocol: "wrtc",
112
+ markTransportSecure: true,
109
113
  });
110
114
  const rPeerDeviceId = `000000000000000device002`;
111
115
  const rPeerDevice = new device_1.Device(userId, rPeerDeviceId, keys);
112
116
  const rPeerConnection = (0, binary_peer_connection_1.wrapBinaryPeer)(connectionId, rPeer, rPeerDevice, false, {
113
- protocol: 'wrtc',
114
- markTransportSecure: true
117
+ protocol: "wrtc",
118
+ markTransportSecure: true,
115
119
  });
116
120
  expect(iPeerConnection.secureLocal).toBe(true);
117
121
  expect(iPeerConnection.secureRemote).toBe(true);
@@ -122,8 +126,8 @@ describe('binary-peer-connection', () => {
122
126
  rPeer.destroy();
123
127
  });
124
128
  });
125
- describe('backwards compatibility', () => {
126
- it('wrapWrtc should work as alias and set secure flags', async () => {
129
+ describe("backwards compatibility", () => {
130
+ it("wrapWrtc should work as alias and set secure flags", async () => {
127
131
  const { iPeer, rPeer } = await getMockPeerPair();
128
132
  const userId = `00000000000000000user001`;
129
133
  const keys = (0, keys_1.newKeys)();
@@ -137,14 +141,14 @@ describe('binary-peer-connection', () => {
137
141
  // wrapWrtc should set secure flags by default (transport-level encryption)
138
142
  expect(iPeerConnection.secureLocal).toBe(true);
139
143
  expect(iPeerConnection.secureRemote).toBe(true);
140
- await iPeerConnection.doHandshake('wrtc://' + connectionId);
144
+ await iPeerConnection.doHandshake(`wrtc://${connectionId}`);
141
145
  expect(rPeerConnection.remoteDeviceInfo).toBeDefined();
142
146
  expect(rPeerConnection.remoteDeviceInfo?.deviceId).toBe(iPeerDeviceId);
143
147
  // Clean up
144
148
  iPeer.destroy();
145
149
  rPeer.destroy();
146
150
  });
147
- it('binary peers should bypass StreamedSocket (handlesOwnEncoding)', async () => {
151
+ it("binary peers should bypass StreamedSocket (handlesOwnEncoding)", async () => {
148
152
  const { iPeer } = await getMockPeerPair();
149
153
  const userId = `00000000000000000user001`;
150
154
  const keys = (0, keys_1.newKeys)();
@@ -1,7 +1,7 @@
1
- import { IDataBox } from "../keys";
2
- import { Device } from "./device";
3
- import { IDeviceHandshake, IDeviceInfo } from "../data";
4
- import { ISocket, TrustLevel } from "./socket.type";
1
+ import type { IDeviceHandshake, IDeviceInfo } from "../data";
2
+ import { type IDataBox } from "../keys";
3
+ import type { Device } from "./device";
4
+ import { type ISocket, TrustLevel } from "./socket.type";
5
5
  export type GetTrustLevel = (deviceInfo: IDeviceInfo, registerNew?: boolean) => Promise<TrustLevel>;
6
6
  export declare class Connection {
7
7
  readonly localDevice: Device;
@@ -11,7 +11,7 @@ export declare class Connection {
11
11
  private _remoteDeviceInfo;
12
12
  secureLocal: boolean;
13
13
  secureRemote: boolean;
14
- overrideEncryption?: 'encrypt-traffic-regardless-of-protocol' | 'dont-encrypt-traffic-after-handshake';
14
+ overrideEncryption?: "encrypt-traffic-regardless-of-protocol" | "dont-encrypt-traffic-after-handshake";
15
15
  trustLevel: TrustLevel;
16
16
  onHandshakeComplete: ((deviceInfo: IDeviceInfo) => void) | undefined;
17
17
  timeoutMs: number;
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Connection = void 0;
4
4
  exports.normalizeAddress = normalizeAddress;
5
- const utils_1 = require("../utils");
6
5
  const keys_1 = require("../keys");
6
+ const utils_1 = require("../utils");
7
7
  const socket_type_1 = require("./socket.type");
8
8
  const HANDSHAKE_TIMESTAMP_TOLERANCE_MS = 300_000; // 5 minutes
9
9
  class Connection {
@@ -12,10 +12,10 @@ class Connection {
12
12
  getTrustLevel;
13
13
  _verified = false;
14
14
  _remoteDeviceInfo = {
15
- deviceId: '',
16
- userId: '',
17
- publicKey: '',
18
- publicBoxKey: '',
15
+ deviceId: "",
16
+ userId: "",
17
+ publicKey: "",
18
+ publicBoxKey: "",
19
19
  };
20
20
  secureLocal = false;
21
21
  secureRemote = false;
@@ -41,27 +41,27 @@ class Connection {
41
41
  socket.id = (0, utils_1.newid)();
42
42
  }
43
43
  // Only the server side of a connection should expose these methods
44
- this.exposeRPC('close', () => {
44
+ this.exposeRPC("close", () => {
45
45
  (0, utils_1.sleep)(100).then(() => {
46
46
  this.closeLocal();
47
47
  });
48
48
  });
49
- this.exposeRPC('ping', () => {
49
+ this.exposeRPC("ping", () => {
50
50
  return Date.now();
51
51
  });
52
52
  if (localDeviceServerAddresses) {
53
- this.exposeRPC('reset', this.reset.bind(this));
54
- this.exposeRPC('getTrustLevel', async (deviceInfo) => {
53
+ this.exposeRPC("reset", this.reset.bind(this));
54
+ this.exposeRPC("getTrustLevel", async (deviceInfo) => {
55
55
  const trustLevel = await this.getTrustLevel(deviceInfo, false);
56
56
  console.log(`Sending client trust level ${trustLevel}`);
57
57
  return trustLevel;
58
58
  });
59
- this.exposeRPC('requestDeviceInfo', this.localDevice.getDeviceInfo.bind(this.localDevice));
60
- this.exposeRPC('completeHandshake', async (handshakeBox) => {
59
+ this.exposeRPC("requestDeviceInfo", this.localDevice.getDeviceInfo.bind(this.localDevice));
60
+ this.exposeRPC("completeHandshake", async (handshakeBox) => {
61
61
  const handshakeResponse = await this.completeHandshake(handshakeBox);
62
62
  return handshakeResponse;
63
63
  });
64
- this.exposeRPC('requestSecure', () => {
64
+ this.exposeRPC("requestSecure", () => {
65
65
  this.secureRemote = true;
66
66
  return !this.encryptTraffic;
67
67
  });
@@ -82,10 +82,10 @@ class Connection {
82
82
  * - Insecure WebSockets (ws/http): Data travels over unencrypted channel
83
83
  */
84
84
  get encryptTraffic() {
85
- if (this.overrideEncryption === 'dont-encrypt-traffic-after-handshake') {
85
+ if (this.overrideEncryption === "dont-encrypt-traffic-after-handshake") {
86
86
  return false;
87
87
  }
88
- if (this.overrideEncryption === 'encrypt-traffic-regardless-of-protocol') {
88
+ if (this.overrideEncryption === "encrypt-traffic-regardless-of-protocol") {
89
89
  return true;
90
90
  }
91
91
  // Encrypt only if EITHER side is using an insecure protocol
@@ -95,7 +95,7 @@ class Connection {
95
95
  return { ...this._remoteDeviceInfo };
96
96
  }
97
97
  get connectionId() {
98
- return this.socket.id || '';
98
+ return this.socket.id || "";
99
99
  }
100
100
  /**
101
101
  * Get connection throughput stats with calculated rates.
@@ -158,9 +158,9 @@ class Connection {
158
158
  };
159
159
  }
160
160
  exposeRPC(fn, name) {
161
- if (typeof name === 'function') {
161
+ if (typeof name === "function") {
162
162
  const _fn = name;
163
- // @ts-ignore
163
+ // @ts-expect-error
164
164
  name = fn;
165
165
  fn = _fn;
166
166
  }
@@ -188,7 +188,7 @@ class Connection {
188
188
  err = this.localDevice.unwrapResponse(err);
189
189
  return reject(err);
190
190
  }
191
- if (eventName === 'requestSecure') {
191
+ if (eventName === "requestSecure") {
192
192
  this.secureRemote = this.localDevice.unwrapResponse(result);
193
193
  result = this.secureRemote;
194
194
  }
@@ -205,7 +205,7 @@ class Connection {
205
205
  });
206
206
  }
207
207
  catch (e) {
208
- if (typeof e?.message === 'string' && e.message.includes('bad public key size')) {
208
+ if (typeof e?.message === "string" && e.message.includes("bad public key size")) {
209
209
  this.close();
210
210
  }
211
211
  console.error(`Error connection.emit for ${eventName}(${JSON.stringify(args)})`, e);
@@ -214,7 +214,13 @@ class Connection {
214
214
  });
215
215
  }
216
216
  on(eventName, handler) {
217
- const safeEvents = ['requestDeviceInfo', 'completeHandshake', 'reset', 'getTrustLevel', 'close'];
217
+ const safeEvents = [
218
+ "requestDeviceInfo",
219
+ "completeHandshake",
220
+ "reset",
221
+ "getTrustLevel",
222
+ "close",
223
+ ];
218
224
  this.socket.on(eventName, async (args, callback) => {
219
225
  if (!this.verified && !safeEvents.includes(eventName)) {
220
226
  console.error(`Ignoring event from unverified connection: ${eventName}`);
@@ -236,13 +242,13 @@ class Connection {
236
242
  }
237
243
  catch (e) {
238
244
  console.error(`Error handling ${eventName}(${JSON.stringify(args)})`, e);
239
- let rpcError = { error: e.message, errorType: 'RPC_ERROR' };
245
+ let rpcError = { error: e.message, errorType: "RPC_ERROR" };
240
246
  if (this._verified && this.encryptTraffic) {
241
247
  try {
242
248
  rpcError = this.localDevice.signAndBoxDataForDevice(rpcError, this._remoteDeviceInfo);
243
249
  }
244
250
  catch (e) {
245
- console.error('Error signing and boxing RPC error', e);
251
+ console.error("Error signing and boxing RPC error", e);
246
252
  }
247
253
  }
248
254
  callback(rpcError);
@@ -255,10 +261,10 @@ class Connection {
255
261
  reset() {
256
262
  this._connectionAddress = undefined;
257
263
  this._remoteDeviceInfo = {
258
- deviceId: '',
259
- userId: '',
260
- publicKey: '',
261
- publicBoxKey: '',
264
+ deviceId: "",
265
+ userId: "",
266
+ publicKey: "",
267
+ publicBoxKey: "",
262
268
  };
263
269
  this._verified = false;
264
270
  this.secureLocal = false;
@@ -280,11 +286,12 @@ class Connection {
280
286
  throw new Error(`Remote device's system clock is too far out of sync`);
281
287
  }
282
288
  const clientServerAddress = normalizeAddress(signedHandshake.contents.serverAddress);
283
- const thisServerAddress = this.localDeviceServerAddresses?.find(a => a === clientServerAddress) ||
289
+ const thisServerAddress = this.localDeviceServerAddresses?.find((a) => a === clientServerAddress) ||
284
290
  this.localDeviceServerAddresses?.[0] ||
285
- 'unknown';
291
+ "unknown";
286
292
  // only set local secure if the server address matches the client's address and the address is using https
287
- this.secureLocal = clientServerAddress === thisServerAddress && thisServerAddress.startsWith('https');
293
+ this.secureLocal =
294
+ clientServerAddress === thisServerAddress && thisServerAddress.startsWith("https");
288
295
  this._connectionAddress = thisServerAddress;
289
296
  const handshakeResponse = this.localDevice.handshakeResponse(signedHandshake, this.connectionId, thisServerAddress);
290
297
  this._remoteDeviceInfo = {
@@ -296,8 +303,8 @@ class Connection {
296
303
  this.trustLevel = await this.getTrustLevel(signedHandshake.contents, true);
297
304
  if (this.trustLevel < socket_type_1.TrustLevel.Unknown) {
298
305
  this.reset();
299
- this.emit('reset');
300
- throw new Error('Untrusted connection');
306
+ this.emit("reset");
307
+ throw new Error("Untrusted connection");
301
308
  }
302
309
  // console.log(`Connection ${this.connectionId} verified on server side with trust level ${this.trustLevel}`);
303
310
  this._verified = true;
@@ -311,37 +318,41 @@ class Connection {
311
318
  async doHandshake(remoteAddress) {
312
319
  remoteAddress = normalizeAddress(remoteAddress);
313
320
  this.reset();
314
- await this.emit('reset');
315
- const remoteDeviceInfoSigned = await this.emit('requestDeviceInfo');
321
+ await this.emit("reset");
322
+ const remoteDeviceInfoSigned = await this.emit("requestDeviceInfo");
316
323
  const remoteDeviceInfo = (0, keys_1.openSignedObject)(remoteDeviceInfoSigned);
317
324
  if (remoteDeviceInfoSigned.publicKey !== remoteDeviceInfo.publicKey) {
318
- throw new Error('Device info signing key does not match claimed identity key');
325
+ throw new Error("Device info signing key does not match claimed identity key");
319
326
  }
320
327
  const handshake = await this.initiateHandshake(remoteAddress, remoteDeviceInfo);
321
- const handshakeResponseBox = await this.emit('completeHandshake', handshake);
328
+ const handshakeResponseBox = await this.emit("completeHandshake", handshake);
322
329
  const handshakeResponse = await this.localDevice.openBoxedAndSignedData(handshakeResponseBox);
323
330
  if (handshakeResponse.connectionId !== this.connectionId) {
324
331
  throw new Error(`Invalid connectionId ${handshakeResponse.connectionId}, expected ${this.connectionId}`);
325
332
  }
326
- if (remoteDeviceInfo.publicKey != handshakeResponse.publicKey || remoteDeviceInfo.publicBoxKey != handshakeResponse.publicBoxKey) {
327
- throw new Error('Inconsistent public keys');
333
+ if (remoteDeviceInfo.publicKey !== handshakeResponse.publicKey ||
334
+ remoteDeviceInfo.publicBoxKey !== handshakeResponse.publicBoxKey) {
335
+ throw new Error("Inconsistent public keys");
328
336
  }
329
- if (remoteDeviceInfo.userId != handshakeResponse.userId || remoteDeviceInfo.deviceId != handshakeResponse.deviceId) {
330
- throw new Error('Inconsistent device info');
337
+ if (remoteDeviceInfo.userId !== handshakeResponse.userId ||
338
+ remoteDeviceInfo.deviceId !== handshakeResponse.deviceId) {
339
+ throw new Error("Inconsistent device info");
331
340
  }
332
341
  this._verified = true;
333
342
  this._connectionAddress = remoteAddress;
334
- const secureProtocols = ['https', 'wss', 'wrtc'];
343
+ const secureProtocols = ["https", "wss", "wrtc"];
335
344
  // const secureProtocols = ['https', 'wss',]
336
- this.secureLocal = secureProtocols.some(s => remoteAddress.startsWith(s)) && handshakeResponse.serverAddress === remoteAddress;
345
+ this.secureLocal =
346
+ secureProtocols.some((s) => remoteAddress.startsWith(s)) &&
347
+ handshakeResponse.serverAddress === remoteAddress;
337
348
  if (this.secureLocal) {
338
- await this.emit('requestSecure');
349
+ await this.emit("requestSecure");
339
350
  }
340
351
  this.trustLevel = await this.getTrustLevel(remoteDeviceInfo, true);
341
352
  if (this.trustLevel < socket_type_1.TrustLevel.Unknown) {
342
353
  this.reset();
343
- this.emit('reset');
344
- throw new Error('Untrusted connection');
354
+ this.emit("reset");
355
+ throw new Error("Untrusted connection");
345
356
  }
346
357
  // console.log(`Connection ${this.connectionId} (${remoteAddress}) verified on client side with trust level ${this.trustLevel}`);
347
358
  return handshakeResponse;
@@ -353,10 +364,10 @@ class Connection {
353
364
  }
354
365
  async ping() {
355
366
  if (!this._verified) {
356
- throw new Error('Cannot ping unverified connection');
367
+ throw new Error("Cannot ping unverified connection");
357
368
  }
358
369
  const startTime = Date.now();
359
- await this.emit('ping');
370
+ await this.emit("ping");
360
371
  return Date.now() - startTime;
361
372
  }
362
373
  closed = false;
@@ -365,7 +376,7 @@ class Connection {
365
376
  return;
366
377
  this.closed = true;
367
378
  await Promise.race([
368
- this.emit('close').catch(err => {
379
+ this.emit("close").catch((_err) => {
369
380
  // swallow error
370
381
  // console.error('Error sending close event to remote device', err);
371
382
  }),
@@ -376,5 +387,5 @@ class Connection {
376
387
  }
377
388
  exports.Connection = Connection;
378
389
  function normalizeAddress(address) {
379
- return address.toLowerCase().replace(/\/$/, '');
390
+ return address.toLowerCase().replace(/\/$/, "");
380
391
  }