@pryv/socket.io 2.4.7 → 3.0.1
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 +4 -1
- package/src/SocketIO.js +24 -7
- package/src/index.d.ts +38 -16
- package/test/socket.io.test.js +17 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pryv/socket.io",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Extends `pryv` with Socket.IO transport",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Pryv",
|
|
@@ -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
|
|
30
|
+
* Open the Socket.IO stream
|
|
20
31
|
* @throws {Error} On connection failures
|
|
21
|
-
* @
|
|
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
|
|
75
|
-
* @param {
|
|
76
|
-
* @param {Function} listener The callback function
|
|
77
|
-
* @
|
|
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
|
|
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:
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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 {
|
package/test/socket.io.test.js
CHANGED
|
@@ -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
|
|
|
@@ -12,7 +11,7 @@ require('@pryv/socket.io')(pryv);
|
|
|
12
11
|
let conn = null;
|
|
13
12
|
const testStreamId = 'socket-test';
|
|
14
13
|
|
|
15
|
-
describe('Socket.IO', function () {
|
|
14
|
+
describe('[SKIX] Socket.IO', function () {
|
|
16
15
|
this.timeout(20000);
|
|
17
16
|
let apiEndpoint;
|
|
18
17
|
let apiEndpointBogusToken;
|
|
@@ -41,13 +40,13 @@ describe('Socket.IO', function () {
|
|
|
41
40
|
expect(res[0].error.id).to.equal('item-already-exists');
|
|
42
41
|
});
|
|
43
42
|
|
|
44
|
-
describe('init on invalid endpoint', () => {
|
|
45
|
-
it('Should throw an error "Not Found" or ENOTFOUND when user is not known', async () => {
|
|
43
|
+
describe('[SIEX] init on invalid endpoint', () => {
|
|
44
|
+
it('[SIEA] Should throw an error "Not Found" or ENOTFOUND when user is not known', async () => {
|
|
46
45
|
conn = new pryv.Connection(apiEndpointBogusUsername);
|
|
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
|
|
@@ -59,18 +58,18 @@ describe('Socket.IO', function () {
|
|
|
59
58
|
throw new Error('Should throw an error');
|
|
60
59
|
});
|
|
61
60
|
|
|
62
|
-
it('Should throw an error "Unauthorized" when token is invalid', async () => {
|
|
61
|
+
it('[SIEB] Should throw an error "Unauthorized" when token is invalid', async () => {
|
|
63
62
|
conn = new pryv.Connection(apiEndpointBogusToken);
|
|
64
63
|
try {
|
|
65
64
|
await conn.socket.open();
|
|
66
65
|
} catch (e) {
|
|
67
|
-
return expect(e.
|
|
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
|
});
|
|
71
70
|
});
|
|
72
71
|
|
|
73
|
-
describe('init on valid endpoint', () => {
|
|
72
|
+
describe('[SIVX] init on valid endpoint', () => {
|
|
74
73
|
beforeEach(async () => {
|
|
75
74
|
conn = new pryv.Connection(apiEndpoint);
|
|
76
75
|
});
|
|
@@ -79,7 +78,7 @@ describe('Socket.IO', function () {
|
|
|
79
78
|
conn = null;
|
|
80
79
|
});
|
|
81
80
|
|
|
82
|
-
it('Should throw an error if conn.socket.api() is called before being open()', async () => {
|
|
81
|
+
it('[SIVA] Should throw an error if conn.socket.api() is called before being open()', async () => {
|
|
83
82
|
try {
|
|
84
83
|
await conn.socket.api([{ method: 'events.get', params: {} }]);
|
|
85
84
|
} catch (e) {
|
|
@@ -88,7 +87,7 @@ describe('Socket.IO', function () {
|
|
|
88
87
|
throw new Error('Should throw an error');
|
|
89
88
|
});
|
|
90
89
|
|
|
91
|
-
it('Should throw an error if conn.socket.on() is called before being open()', async () => {
|
|
90
|
+
it('[SIVB] Should throw an error if conn.socket.on() is called before being open()', async () => {
|
|
92
91
|
try {
|
|
93
92
|
await conn.socket.on('eventsChanged');
|
|
94
93
|
} catch (e) {
|
|
@@ -97,13 +96,13 @@ describe('Socket.IO', function () {
|
|
|
97
96
|
throw new Error('Should throw an error');
|
|
98
97
|
});
|
|
99
98
|
|
|
100
|
-
it('Correct initialization should return socket instance', async () => {
|
|
99
|
+
it('[SIVC] Correct initialization should return socket instance', async () => {
|
|
101
100
|
const socket = await conn.socket.open();
|
|
102
101
|
expect(socket._io).to.exist;
|
|
103
102
|
});
|
|
104
103
|
});
|
|
105
104
|
|
|
106
|
-
describe('socket.api', () => {
|
|
105
|
+
describe('[SAPX] socket.api', () => {
|
|
107
106
|
before(async () => {
|
|
108
107
|
conn = new pryv.Connection(apiEndpoint);
|
|
109
108
|
await conn.socket.open();
|
|
@@ -113,7 +112,7 @@ describe('Socket.IO', function () {
|
|
|
113
112
|
conn = null;
|
|
114
113
|
});
|
|
115
114
|
|
|
116
|
-
it('Handle correctly batch calls', async () => {
|
|
115
|
+
it('[SAPA] Handle correctly batch calls', async () => {
|
|
117
116
|
const res = await conn.socket.api([{ method: 'streams.get', params: {} }]);
|
|
118
117
|
expect(res[0]).to.exist;
|
|
119
118
|
expect(res[0].streams).to.exist;
|
|
@@ -122,7 +121,7 @@ describe('Socket.IO', function () {
|
|
|
122
121
|
// we don't test further .api() as it relies on the implementation of pryv.Connection
|
|
123
122
|
});
|
|
124
123
|
|
|
125
|
-
describe('notification', () => {
|
|
124
|
+
describe('[SNTX] notification', () => {
|
|
126
125
|
before(async () => {
|
|
127
126
|
conn = new pryv.Connection(apiEndpoint);
|
|
128
127
|
await conn.socket.open();
|
|
@@ -132,7 +131,7 @@ describe('Socket.IO', function () {
|
|
|
132
131
|
conn = null;
|
|
133
132
|
});
|
|
134
133
|
|
|
135
|
-
it('Fails on requesting an invalid notifcation', async () => {
|
|
134
|
+
it('[SNTA] Fails on requesting an invalid notifcation', async () => {
|
|
136
135
|
try {
|
|
137
136
|
conn.socket.on('Bogus', () => {});
|
|
138
137
|
} catch (e) {
|
|
@@ -141,7 +140,7 @@ describe('Socket.IO', function () {
|
|
|
141
140
|
throw new Error('Should fail');
|
|
142
141
|
});
|
|
143
142
|
|
|
144
|
-
it('Catches eventChanges', (done) => {
|
|
143
|
+
it('[SNTB] Catches eventChanges', (done) => {
|
|
145
144
|
function onEventChanged () {
|
|
146
145
|
return done();
|
|
147
146
|
}
|
|
@@ -149,7 +148,7 @@ describe('Socket.IO', function () {
|
|
|
149
148
|
conn.api([{ method: 'events.create', params: { type: 'note/txt', streamId: testStreamId, content: 'hello' } }]);
|
|
150
149
|
});
|
|
151
150
|
|
|
152
|
-
it('Catches streamChanges', (done) => {
|
|
151
|
+
it('[SNTC] Catches streamChanges', (done) => {
|
|
153
152
|
function onStreamChange () {
|
|
154
153
|
return done();
|
|
155
154
|
}
|
|
@@ -158,7 +157,7 @@ describe('Socket.IO', function () {
|
|
|
158
157
|
});
|
|
159
158
|
|
|
160
159
|
/** Keep this test the last on of this sequence */
|
|
161
|
-
it('Catches disconnect', (done) => {
|
|
160
|
+
it('[SNTD] Catches disconnect', (done) => {
|
|
162
161
|
function onDisconnect (reason) {
|
|
163
162
|
expect(reason).to.equal('io client disconnect');
|
|
164
163
|
return done();
|