@peers-app/peers-sdk 0.7.6 → 0.7.7

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) => {
@@ -127,9 +132,9 @@ class Connection {
127
132
  });
128
133
  }
129
134
  on(eventName, handler) {
130
- const handshakeEvents = ['requestDeviceInfo', 'completeHandshake', 'reset', 'getTrustLevel'];
135
+ const safeEvents = ['requestDeviceInfo', 'completeHandshake', 'reset', 'getTrustLevel', 'close'];
131
136
  this.socket.on(eventName, async (args, callback) => {
132
- if (!this.verified && !handshakeEvents.includes(eventName)) {
137
+ if (!this.verified && !safeEvents.includes(eventName)) {
133
138
  console.error(`Ignoring event from unverified connection: ${eventName}`);
134
139
  return callback(`Connection not verified. Handshake must be completed successfully before calling ${eventName}`);
135
140
  }
@@ -242,6 +247,18 @@ class Connection {
242
247
  console.log(`Connection ${this.connectionId} (${remoteAddress}) verified on client side with trust level ${this.trustLevel}`);
243
248
  return handshakeResponse;
244
249
  }
250
+ closeLocal() {
251
+ this.reset();
252
+ this.removeAllListeners();
253
+ this.socket.disconnect(true);
254
+ }
255
+ async close() {
256
+ await Promise.race([
257
+ this.emit('close'),
258
+ (0, utils_1.sleep)(100),
259
+ ]);
260
+ this.closeLocal();
261
+ }
245
262
  }
246
263
  exports.Connection = Connection;
247
264
  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.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"