@pryv/socket.io 2.4.6 → 3.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pryv/socket.io",
3
- "version": "2.4.6",
3
+ "version": "3.0.0",
4
4
  "description": "Extends `pryv` with Socket.IO transport",
5
5
  "keywords": [
6
6
  "Pryv",
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
17
- "url": "git://github.com/pryv/lib-js"
17
+ "url": "git://github.com/pryv/lib-js.git"
18
18
  },
19
19
  "license": "BSD-3-Clause",
20
20
  "author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
@@ -22,5 +22,8 @@
22
22
  "types": "src/index.d.ts",
23
23
  "dependencies": {
24
24
  "socket.io-client": "^4.5.1"
25
+ },
26
+ "engines": {
27
+ "node": ">=20.0.0"
25
28
  }
26
29
  }
package/src/SocketIO.js CHANGED
@@ -7,18 +7,29 @@ const { EventEmitter } = require('events');
7
7
 
8
8
  const EVENTS = ['eventsChanged', 'streamsChanged', 'accessesChanged', 'disconnect', 'error'];
9
9
 
10
+ /**
11
+ * Socket.IO transport for a Connection.
12
+ * Use connection.socket to access the instance associated with a Connection.
13
+ * @memberof pryv
14
+ * @extends EventEmitter
15
+ */
10
16
  class SocketIO extends EventEmitter {
17
+ /**
18
+ * @param {Connection} connection - The connection to bind to
19
+ */
11
20
  constructor (connection) {
12
21
  super();
22
+ /** @type {Connection} */
13
23
  this.connection = connection;
24
+ /** @type {boolean} */
14
25
  this.connecting = false;
15
26
  this._io = null;
16
27
  }
17
28
 
18
29
  /**
19
- * Open the scocket stream
30
+ * Open the Socket.IO stream
20
31
  * @throws {Error} On connection failures
21
- * @return {SocketIO} this
32
+ * @returns {Promise<SocketIO>} Promise resolving to this SocketIO instance
22
33
  */
23
34
  async open () {
24
35
  return new Promise((resolve, reject) => {
@@ -29,6 +40,7 @@ class SocketIO extends EventEmitter {
29
40
  this.connection.username()
30
41
  .then(username => {
31
42
  const socketEndpoint = this.connection.endpoint + username + '?auth=' + this.connection.token;
43
+ // @ts-ignore - io is callable in socket.io-client
32
44
  this._io = io(socketEndpoint, { forceNew: true });
33
45
 
34
46
  // handle failure
@@ -71,21 +83,26 @@ class SocketIO extends EventEmitter {
71
83
  }
72
84
 
73
85
  /**
74
- * Add listner
75
- * @param {string} eventName - one of 'eventsChanged', 'streamsChanged', 'accessesChanged', 'disconnect', 'error'
76
- * @param {Function} listener The callback function
77
- * @return {EventEmitter};
86
+ * Add listener for Socket.IO events
87
+ * @param {('eventsChanged'|'streamsChanged'|'accessesChanged'|'disconnect'|'error')} eventName - The event to listen for
88
+ * @param {Function} listener - The callback function
89
+ * @returns {SocketIO} this
78
90
  */
91
+ // @ts-ignore - overriding EventEmitter.on with restricted signature
79
92
  on (eventName, listener) {
80
93
  checkOpen(this);
81
94
  if (EVENTS.indexOf(eventName) < 0) {
82
95
  throw new Error('Unkown event [' + eventName + ']. Allowed events are: ' + EVENTS);
83
96
  }
97
+ // @ts-ignore
84
98
  return super.on(eventName, listener);
85
99
  }
86
100
 
87
101
  /**
88
- * Identical to Connection.api() using socket.io transport
102
+ * Identical to Connection.api() but using Socket.IO transport
103
+ * @param {Array<MethodCall>} arrayOfAPICalls - Array of Method Calls
104
+ * @param {Function} [progress] - Return percentage of progress (0 - 100)
105
+ * @returns {Promise<Array>} Promise to Array of results matching each method call in order
89
106
  */
90
107
  async api (arrayOfAPICalls, progress) {
91
108
  checkOpen(this);
package/src/index.d.ts CHANGED
@@ -1,6 +1,18 @@
1
- import pryv from 'pryv';
1
+ import pryv, { Service, Connection, Auth, Browser, utils } from 'pryv';
2
2
  import { EventEmitter } from 'events';
3
3
 
4
+ /**
5
+ * Type for the pryv library parameter - accepts both default and namespace imports
6
+ */
7
+ export type PryvLibrary = {
8
+ Service: typeof Service;
9
+ Connection: typeof Connection;
10
+ Auth: typeof Auth;
11
+ Browser: typeof Browser;
12
+ utils: typeof utils;
13
+ version?: string;
14
+ };
15
+
4
16
  /**
5
17
  * Extends a `pryv` instance with Socket.IO capabilities.
6
18
  *
@@ -18,9 +30,16 @@ import { EventEmitter } from 'events';
18
30
  * });
19
31
  * ```
20
32
  */
21
- export default function extendPryvSocketIO(pryvLib: typeof pryv): void;
33
+ export default function extendPryvSocketIO(pryvLib: PryvLibrary): void;
22
34
 
23
35
  declare module 'pryv' {
36
+ export type SocketIOEventName =
37
+ | 'eventsChanged'
38
+ | 'streamsChanged'
39
+ | 'accessesChanged'
40
+ | 'disconnect'
41
+ | 'error';
42
+
24
43
  /**
25
44
  * Socket.IO transport for a `Connection`.
26
45
  *
@@ -30,6 +49,12 @@ declare module 'pryv' {
30
49
  export class SocketIO extends EventEmitter {
31
50
  constructor(connection: Connection);
32
51
 
52
+ /** The connection this socket is bound to */
53
+ readonly connection: Connection;
54
+
55
+ /** True while the socket is in the process of connecting */
56
+ readonly connecting: boolean;
57
+
33
58
  /**
34
59
  * Open the Socket.IO stream.
35
60
  * @throws Error on connection failures
@@ -54,21 +79,18 @@ declare module 'pryv' {
54
79
  * Listen to Socket.IO events emitted by the Pryv.io backend.
55
80
  *
56
81
  * Supported events:
57
- * - `eventsChanged`
58
- * - `streamsChanged`
59
- * - `accessesChanged`
60
- * - `disconnect`
61
- * - `error`
82
+ * - `eventsChanged` - Events have been modified
83
+ * - `streamsChanged` - Streams have been modified
84
+ * - `accessesChanged` - Accesses have been modified
85
+ * - `disconnect` - Socket disconnected
86
+ * - `error` - An error occurred
62
87
  */
63
- on(
64
- eventName:
65
- | 'eventsChanged'
66
- | 'streamsChanged'
67
- | 'accessesChanged'
68
- | 'disconnect'
69
- | 'error',
70
- listener: (...args: any[]) => void
71
- ): this;
88
+ on(event: 'eventsChanged', listener: () => void): this;
89
+ on(event: 'streamsChanged', listener: () => void): this;
90
+ on(event: 'accessesChanged', listener: () => void): this;
91
+ on(event: 'disconnect', listener: (reason: string) => void): this;
92
+ on(event: 'error', listener: (error: Error | unknown) => void): this;
93
+ on(event: SocketIOEventName, listener: (...args: unknown[]) => void): this;
72
94
  }
73
95
 
74
96
  export class Connection {
@@ -3,7 +3,6 @@
3
3
  * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
4
  */
5
5
  /* global describe, it, before, after, beforeEach, afterEach, expect, pryv, testData */
6
- /* eslint-disable no-unused-expressions */
7
6
 
8
7
  const cuid = require('cuid');
9
8
 
@@ -47,7 +46,7 @@ describe('Socket.IO', function () {
47
46
  try {
48
47
  await conn.socket.open();
49
48
  } catch (e) {
50
- if (e.message === 'Not Found' || e.message.startsWith('getaddrinfo ENOTFOUND')) {
49
+ if (e.message === 'Not Found' || e.message.startsWith('getaddrinfo ENOTFOUND') || e.message === 'fetch failed') {
51
50
  return;
52
51
  }
53
52
  if (typeof document !== 'undefined') { // in browser
@@ -64,7 +63,7 @@ describe('Socket.IO', function () {
64
63
  try {
65
64
  await conn.socket.open();
66
65
  } catch (e) {
67
- return expect(e.response.text).to.include('Cannot find access from token.');
66
+ return expect(e.message).to.include('Cannot find access from token.');
68
67
  }
69
68
  throw new Error('Should throw an error');
70
69
  });