@peers-app/peers-sdk 0.7.6 → 0.7.15

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.
@@ -32,10 +32,12 @@ export declare class Connection {
32
32
  exposeRPC<T extends Function>(fn: T): void;
33
33
  emit<T = any>(eventName: string, ...args: any): Promise<T>;
34
34
  on(eventName: string, handler: Function): void;
35
- removeAllListeners(eventName: string): void;
35
+ removeAllListeners(eventName?: string): void;
36
36
  reset(): void;
37
37
  initiateHandshake(serverAddress: string, remoteDeviceInfo?: IDeviceInfo): IDataBox;
38
38
  completeHandshake(boxedHandshake: IDataBox): Promise<IDeviceHandshake>;
39
39
  doHandshake(remoteAddress: string): Promise<IDeviceHandshake>;
40
+ private closeLocal;
41
+ close(): Promise<void>;
40
42
  }
41
43
  export declare function normalizeAddress(address: string): string;
@@ -42,6 +42,11 @@ class Connection {
42
42
  socket.id = (0, utils_1.newid)();
43
43
  }
44
44
  // Only the server side of a connection should expose these methods
45
+ this.exposeRPC('close', () => {
46
+ (0, utils_1.sleep)(100).then(() => {
47
+ this.closeLocal();
48
+ });
49
+ });
45
50
  if (localDeviceServerAddresses) {
46
51
  this.exposeRPC('reset', this.reset.bind(this));
47
52
  this.exposeRPC('getTrustLevel', async (deviceInfo) => {
@@ -121,15 +126,18 @@ class Connection {
121
126
  });
122
127
  }
123
128
  catch (e) {
129
+ if (typeof e?.message === 'string' && e.message.includes('bad public key size')) {
130
+ this.close();
131
+ }
124
132
  console.error(`Error connection.emit for ${eventName}(${JSON.stringify(args)})`, e);
125
133
  return reject(e);
126
134
  }
127
135
  });
128
136
  }
129
137
  on(eventName, handler) {
130
- const handshakeEvents = ['requestDeviceInfo', 'completeHandshake', 'reset', 'getTrustLevel'];
138
+ const safeEvents = ['requestDeviceInfo', 'completeHandshake', 'reset', 'getTrustLevel', 'close'];
131
139
  this.socket.on(eventName, async (args, callback) => {
132
- if (!this.verified && !handshakeEvents.includes(eventName)) {
140
+ if (!this.verified && !safeEvents.includes(eventName)) {
133
141
  console.error(`Ignoring event from unverified connection: ${eventName}`);
134
142
  return callback(`Connection not verified. Handshake must be completed successfully before calling ${eventName}`);
135
143
  }
@@ -148,7 +156,12 @@ class Connection {
148
156
  console.error(`Error handling ${eventName}(${JSON.stringify(args)})`, e);
149
157
  let rpcError = { error: e.message, errorType: 'RPC_ERROR' };
150
158
  if (this._verified && !this.secure) {
151
- rpcError = this.localDevice.signAndBoxDataForDevice(rpcError, this._remoteDeviceInfo);
159
+ try {
160
+ rpcError = this.localDevice.signAndBoxDataForDevice(rpcError, this._remoteDeviceInfo);
161
+ }
162
+ catch (e) {
163
+ console.error('Error signing and boxing RPC error', e);
164
+ }
152
165
  }
153
166
  callback(rpcError);
154
167
  }
@@ -242,6 +255,18 @@ class Connection {
242
255
  console.log(`Connection ${this.connectionId} (${remoteAddress}) verified on client side with trust level ${this.trustLevel}`);
243
256
  return handshakeResponse;
244
257
  }
258
+ closeLocal() {
259
+ this.reset();
260
+ this.removeAllListeners();
261
+ this.socket.disconnect(true);
262
+ }
263
+ async close() {
264
+ await Promise.race([
265
+ this.emit('close'),
266
+ (0, utils_1.sleep)(100),
267
+ ]);
268
+ this.closeLocal();
269
+ }
245
270
  }
246
271
  exports.Connection = Connection;
247
272
  function normalizeAddress(address) {
@@ -5,8 +5,8 @@ export interface ISocket {
5
5
  connected?: boolean;
6
6
  emit(eventName: string, args: any, callback: RPCCallback): void;
7
7
  on(eventName: string, handler: ((...args: any[]) => void)): void;
8
- removeAllListeners(eventName: string): void;
9
- disconnect(): void;
8
+ removeAllListeners(eventName?: string): void;
9
+ disconnect(close?: boolean): void;
10
10
  }
11
11
  export declare enum TrustLevel {
12
12
  Malicious = -20,
@@ -14,7 +14,7 @@ export declare class StreamedSocket implements ISocket {
14
14
  constructor(socket: ISocket, maxChunkSize?: number);
15
15
  get id(): string | undefined;
16
16
  get connected(): boolean | undefined;
17
- disconnect(): void;
17
+ disconnect(close?: boolean): void;
18
18
  private readonly callbacks;
19
19
  emit(eventName: string, args: any, callback: RPCCallback): void;
20
20
  private handlers;
@@ -21,8 +21,8 @@ class StreamedSocket {
21
21
  get connected() {
22
22
  return this.socket.connected;
23
23
  }
24
- disconnect() {
25
- this.socket.disconnect();
24
+ disconnect(close) {
25
+ this.socket.disconnect(close);
26
26
  }
27
27
  callbacks = {};
28
28
  emit(eventName, args, callback) {
@@ -34,6 +34,7 @@ export interface IDeviceConnection {
34
34
  errorRate: number;
35
35
  timestampLastApplied: number;
36
36
  onClose?(): Promise<void>;
37
+ closed?: boolean;
37
38
  }
38
39
  export declare const PeerDeviceConsts: Readonly<{
39
40
  MAX_CONNECTIONS: 30;
package/dist/utils.js CHANGED
@@ -216,11 +216,17 @@ async function retryOnErrorOrTimeout({ fn, connection, opts = {}, }) {
216
216
  // timeout *= 2; // double the timeout for the next attempt
217
217
  timeoutCount++;
218
218
  finalError = undefined;
219
+ if (connection.closed) {
220
+ throw new Error(`connection has been closed`);
221
+ }
219
222
  }
220
223
  catch (error) {
221
224
  finalError = error;
222
225
  connection.errorRate = (connection.errorRate * 0.9) + 0.1;
223
226
  errorCount++;
227
+ if (connection.closed) {
228
+ throw new Error(`connection has been closed`);
229
+ }
224
230
  }
225
231
  console.warn(`Call failed, retrying: timeout is ${timeout}ms, errorCount: ${errorCount} of ${retriesOnError}, timeoutCount: ${timeoutCount} of ${retriesOnTimeout + 1}. Last error:`, finalError);
226
232
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.7.6",
3
+ "version": "0.7.15",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"