@pryv/socket.io 1.0.4 → 2.3.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/README.md +41 -47
- package/package.json +14 -25
- package/src/SocketIO.js +47 -46
- package/src/browser-index.js +10 -8
- package/src/index.js +13 -10
- package/test/{socketio.test.js → socket.io.test.js} +49 -46
- package/.mocharc.js +0 -11
- package/LICENSE.md +0 -27
- package/examples/index.html +0 -131
- package/examples/screenshot.png +0 -0
- package/examples/simplesocket.js +0 -20
- package/scripts/setup-environment-dev.sh +0 -28
- package/test/helpers.js +0 -13
- package/webpack.config.js +0 -38
package/README.md
CHANGED
|
@@ -1,66 +1,63 @@
|
|
|
1
|
-
# Socket.
|
|
1
|
+
# Socket.IO add-on for `pryv`
|
|
2
2
|
|
|
3
|
-
Extends Pryv
|
|
3
|
+
Extends the [Pryv JavaScript library](https://github.com/pryv/lib-js) with Socket.IO transport and notifications.
|
|
4
4
|
|
|
5
|
-
## Setup
|
|
6
5
|
|
|
7
|
-
|
|
6
|
+
## Usage
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
The add-on extends `pryv.Connection` instances with a `socket` property.
|
|
10
9
|
|
|
11
|
-
`npm install pryv @pryv/socket.io`
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
### Importing
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
const Pryv = require('pryv');
|
|
17
|
-
require('@pryv/socket.io')(Pryv);
|
|
18
|
-
```
|
|
13
|
+
#### NPM
|
|
19
14
|
|
|
20
|
-
|
|
15
|
+
`npm install --save pryv @pryv/socket.io`, then in your code (the add-on must be loaded **once only**):
|
|
21
16
|
|
|
22
|
-
|
|
17
|
+
```js
|
|
18
|
+
const pryv = require('pryv');
|
|
19
|
+
require('@pryv/socket.io')(pryv);
|
|
20
|
+
```
|
|
23
21
|
|
|
22
|
+
#### `<script>` tag
|
|
23
|
+
|
|
24
|
+
`pryv-socket.io.js` must be loaded **after** `pryv.js`:
|
|
24
25
|
|
|
25
26
|
```html
|
|
26
27
|
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
27
|
-
<script src="https://api.pryv.com/lib-js
|
|
28
|
+
<script src="https://api.pryv.com/lib-js/pryv-socket.io.js"></script>
|
|
28
29
|
```
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- ES6: `https://api.pryv.com/lib-js-socket.io/pryv-socket.io-es6.js`
|
|
33
|
-
- Socket.io + Monitor + Lib-js: `https://api.pryv.com/lib-js/pryv-socket.io-monitor.js`.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
## Usage
|
|
31
|
+
Other distributions available:
|
|
37
32
|
|
|
38
|
-
|
|
33
|
+
- ES6 version: `https://api.pryv.com/lib-js-socket.io/pryv-socket.io-es6.js`
|
|
34
|
+
- `pryv` library bundled with Socket.IO and Monitor add-ons: `https://api.pryv.com/lib-js/pryv-socket.io-monitor.js`.
|
|
39
35
|
|
|
40
|
-
- `Connection.socket.open()` is an asynchronous call that opens the socket.io connection. It throws errors on failure.
|
|
41
36
|
|
|
42
|
-
|
|
37
|
+
### Using `connection.socket`
|
|
43
38
|
|
|
44
|
-
-
|
|
39
|
+
Once the add-on is loaded, `pryv.Connection` instances expose the `socket` property.
|
|
45
40
|
|
|
46
|
-
|
|
41
|
+
- `connection.socket.open()` (asynchronous) opens the Socket.IO connection. It throws errors on failure.
|
|
42
|
+
- `connection.socket.api()` is identical to `Connection.api()` but using the Socket.IO transport (see the [library README](https://github.com/pryv/lib-js#api-calls))
|
|
43
|
+
- `connection.socket.on({event-name}, callback)` registers an event listener. Possible event names are:
|
|
44
|
+
- `eventsChanged`: when one or multiples events are deleted, changed or added.
|
|
45
|
+
- `streamsChanged`: when one or multiples streams are deleted, changed or added.
|
|
46
|
+
- `accessChanged`: when an access is deleted or added.
|
|
47
|
+
- `error`: on error. The callback will receive the error as first argument.
|
|
47
48
|
|
|
48
|
-
- `eventsChanged` : Fired when one or multiples events are deleted, changed or added.
|
|
49
|
-
- `streamsChanged`: Fired when one or multiples streams are deleted, changed or added.
|
|
50
|
-
- `accessChanged`: Fired when an access is deleted or added.
|
|
51
|
-
- `error`: Fired on error. The callback will eventually receive the error as first argument.
|
|
52
49
|
|
|
53
|
-
|
|
50
|
+
### Examples
|
|
54
51
|
|
|
55
|
-
|
|
52
|
+
#### Node.js
|
|
56
53
|
|
|
57
|
-
```
|
|
58
|
-
const
|
|
59
|
-
require('@pryv/socket.io')(
|
|
54
|
+
```js
|
|
55
|
+
const pryv = require('pryv');
|
|
56
|
+
require('@pryv/socket.io')(pryv);
|
|
60
57
|
|
|
61
58
|
const apiEndpoint = 'https://{token}@my-computer.rec.la:4443/{username}/';
|
|
62
|
-
(async () => {
|
|
63
|
-
const conn = new
|
|
59
|
+
(async () => {
|
|
60
|
+
const conn = new pryv.Connection(apiEndpoint);
|
|
64
61
|
try {
|
|
65
62
|
await conn.socket.open();
|
|
66
63
|
conn.socket.on('eventsChanged', async () => {
|
|
@@ -73,7 +70,7 @@ const apiEndpoint = 'https://{token}@my-computer.rec.la:4443/{username}/';
|
|
|
73
70
|
})();
|
|
74
71
|
```
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
#### Browser
|
|
77
74
|
|
|
78
75
|
```html
|
|
79
76
|
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
@@ -95,19 +92,16 @@ const apiEndpoint = 'https://{token}@my-computer.rec.la:4443/{username}/';
|
|
|
95
92
|
</script>
|
|
96
93
|
```
|
|
97
94
|
|
|
98
|
-
|
|
95
|
+
#### Example web app
|
|
99
96
|
|
|
100
|
-
|
|
97
|
+
See [here](`../../examples/socket.io.html`) for a simple app that allows to log in a Pryv.io platform, register to monitor events changes and create notes. You can try it running [there](https://api.pryv.com/lib-js/examples/socket.io.html).
|
|
101
98
|
|
|
102
|
-
The `./examples/index.html` file is a simple demo app that allows to log in a Pryv.io platform, register to monitor events changes and create notes.
|
|
103
99
|
|
|
104
|
-
|
|
100
|
+
## Contributing
|
|
105
101
|
|
|
106
|
-
|
|
102
|
+
See the [Pryv JavaScript library README](https://github.com/pryv/lib-js#contributing)
|
|
107
103
|
|
|
108
|
-
*Prerequisites*: Node 12
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
- Build pryv.js library for browsers: `npm run build`, the result is published in `./dist`
|
|
112
|
-
- Node Tests: `npm run test`
|
|
105
|
+
## License
|
|
113
106
|
|
|
107
|
+
[BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,36 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pryv/socket.io",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Extends `pryv`
|
|
5
|
-
"main": "src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "mocha --reporter spec --exit test/*.test.js",
|
|
8
|
-
"setup": "./scripts/setup-environment-dev.sh",
|
|
9
|
-
"build": "webpack"
|
|
10
|
-
},
|
|
11
|
-
"repository": {
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git://github.com/pryv/lib-js-socket.io"
|
|
14
|
-
},
|
|
15
|
-
"bugs": {
|
|
16
|
-
"url": "https://github.com/pryv/lib-js-socket.io/issues"
|
|
17
|
-
},
|
|
3
|
+
"version": "2.3.0",
|
|
4
|
+
"description": "Extends `pryv` with Socket.IO transport",
|
|
18
5
|
"keywords": [
|
|
19
6
|
"Pryv",
|
|
20
7
|
"Pryv.io",
|
|
21
|
-
"Socket.
|
|
8
|
+
"Socket.IO",
|
|
22
9
|
"WebSockets"
|
|
23
10
|
],
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"@pryv/lib-js-common": "git+https://github.com/pryv/lib-js-common.git#1.0.3",
|
|
28
|
-
"chai": "^4.2.0",
|
|
29
|
-
"cuid": "^2.1.8",
|
|
30
|
-
"mocha": "^8.2.1",
|
|
31
|
-
"pryv": "^2.1.2"
|
|
11
|
+
"homepage": "https://github.com/pryv/lib-js/tree/master/components/pryv-socket.io#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/pryv/lib-js/issues"
|
|
32
14
|
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git://github.com/pryv/lib-js"
|
|
18
|
+
},
|
|
19
|
+
"license": "BSD-3-Clause",
|
|
20
|
+
"author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
|
|
21
|
+
"main": "src/index.js",
|
|
33
22
|
"dependencies": {
|
|
34
|
-
"socket.io-client": "^
|
|
23
|
+
"socket.io-client": "^4.5.1"
|
|
35
24
|
}
|
|
36
25
|
}
|
package/src/SocketIO.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
const io = require('socket.io-client');
|
|
2
|
-
const { resolve } = require('path');
|
|
3
6
|
const { EventEmitter } = require('events');
|
|
4
7
|
|
|
5
8
|
const EVENTS = ['eventsChanged', 'streamsChanged', 'accessesChanged', 'disconnect', 'error'];
|
|
6
9
|
|
|
7
10
|
class SocketIO extends EventEmitter {
|
|
8
|
-
|
|
9
|
-
constructor(connection) {
|
|
11
|
+
constructor (connection) {
|
|
10
12
|
super();
|
|
11
13
|
this.connection = connection;
|
|
12
14
|
this.connecting = false;
|
|
@@ -18,53 +20,52 @@ class SocketIO extends EventEmitter {
|
|
|
18
20
|
* @throws {Error} On connection failures
|
|
19
21
|
* @return {SocketIO} this
|
|
20
22
|
*/
|
|
21
|
-
open() {
|
|
22
|
-
return new Promise(
|
|
23
|
+
async open () {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
23
25
|
if (this._io) return resolve(this);
|
|
24
|
-
if (this.connecting) return reject(
|
|
26
|
+
if (this.connecting) return reject(new Error('open() process in course'));
|
|
25
27
|
this.connecting = true;
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
this.connection.username()
|
|
30
|
+
.then(username => {
|
|
31
|
+
const socketEndpoint = this.connection.endpoint + username + '?auth=' + this.connection.token;
|
|
32
|
+
this._io = io(socketEndpoint, { forceNew: true });
|
|
33
|
+
|
|
34
|
+
// handle failure
|
|
35
|
+
for (const errcode of ['connect_error', 'connection_failed', 'error', 'connection_timeout']) {
|
|
36
|
+
const myCode = errcode;
|
|
37
|
+
this._io.on(errcode, (e) => {
|
|
38
|
+
if (!this.connecting) return; // do not care about errors if connected
|
|
39
|
+
|
|
40
|
+
this._io = null;
|
|
41
|
+
this.connecting = false;
|
|
42
|
+
if (e === null) { e = myCode; }
|
|
43
|
+
if (!(e instanceof Error)) { e = new Error(e); }
|
|
44
|
+
|
|
45
|
+
try { this._io.close(); } catch (ex) { }
|
|
46
|
+
return reject(e);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// handle success
|
|
51
|
+
this._io.on('connect', () => {
|
|
52
|
+
this.connecting = false;
|
|
53
|
+
registerListeners(this);
|
|
54
|
+
resolve(this);
|
|
55
|
+
});
|
|
56
|
+
})
|
|
57
|
+
.catch(e => {
|
|
44
58
|
this._io = null;
|
|
45
59
|
this.connecting = false;
|
|
46
|
-
if (e === null) { e = myCode; }
|
|
47
|
-
if (! (e instanceof Error)) { e = new Error(e); }
|
|
48
|
-
|
|
49
|
-
try { this._io.close(); } catch (ex) { }
|
|
50
60
|
return reject(e);
|
|
51
61
|
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// handle success
|
|
55
|
-
this._io.on('connect', () => {
|
|
56
|
-
this.connecting = false;
|
|
57
|
-
registerListeners(this);
|
|
58
|
-
resolve(this);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
62
|
});
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
/**
|
|
65
66
|
* Close the socket
|
|
66
67
|
*/
|
|
67
|
-
close() {
|
|
68
|
+
close () {
|
|
68
69
|
checkOpen(this);
|
|
69
70
|
this._io.close();
|
|
70
71
|
}
|
|
@@ -75,7 +76,7 @@ class SocketIO extends EventEmitter {
|
|
|
75
76
|
* @param {Function} listener The callback function
|
|
76
77
|
* @return {EventEmitter};
|
|
77
78
|
*/
|
|
78
|
-
on(eventName, listener) {
|
|
79
|
+
on (eventName, listener) {
|
|
79
80
|
checkOpen(this);
|
|
80
81
|
if (EVENTS.indexOf(eventName) < 0) {
|
|
81
82
|
throw new Error('Unkown event [' + eventName + ']. Allowed events are: ' + EVENTS);
|
|
@@ -86,28 +87,28 @@ class SocketIO extends EventEmitter {
|
|
|
86
87
|
/**
|
|
87
88
|
* Identical to Connection.api() using socket.io transport
|
|
88
89
|
*/
|
|
89
|
-
async api(arrayOfAPICalls, progress) {
|
|
90
|
+
async api (arrayOfAPICalls, progress) {
|
|
90
91
|
checkOpen(this);
|
|
91
|
-
function httpHandler(batchCall) {
|
|
92
|
-
return new Promise((resolve, reject) => {
|
|
92
|
+
function httpHandler (batchCall) {
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
93
94
|
this._io.emit('callBatch', batchCall, function (err, res) {
|
|
94
95
|
if (err) return reject(err);
|
|
95
96
|
resolve(res);
|
|
96
97
|
});
|
|
97
98
|
});
|
|
98
|
-
}
|
|
99
|
+
}
|
|
99
100
|
return await this.connection._chunkedBatchCall(arrayOfAPICalls, progress, httpHandler.bind(this));
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
|
|
103
104
|
// private method to fence the usage of socket before being open
|
|
104
|
-
function checkOpen(socket) {
|
|
105
|
+
function checkOpen (socket) {
|
|
105
106
|
if (!socket._io) throw new Error('Initialize socket.io with connection.socket.open() before');
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
// private method to register to all events for an open socket
|
|
109
110
|
// and relay it.
|
|
110
|
-
function registerListeners(socket) {
|
|
111
|
+
function registerListeners (socket) {
|
|
111
112
|
for (const event of EVENTS) {
|
|
112
113
|
socket._io.on(event, (...args) => {
|
|
113
114
|
socket.emit(event, ...args);
|
|
@@ -115,7 +116,7 @@ function registerListeners(socket) {
|
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
|
|
118
|
-
module.exports = function(Connection) {
|
|
119
|
+
module.exports = function (Connection) {
|
|
119
120
|
Object.defineProperty(Connection.prototype, 'socket', {
|
|
120
121
|
get: function () {
|
|
121
122
|
if (this._socket) return this._socket;
|
|
@@ -123,4 +124,4 @@ module.exports = function(Connection) {
|
|
|
123
124
|
return this._socket;
|
|
124
125
|
}
|
|
125
126
|
});
|
|
126
|
-
}
|
|
127
|
+
};
|
package/src/browser-index.js
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
|
|
2
|
-
/*global
|
|
6
|
+
/* global pryv */
|
|
3
7
|
|
|
4
8
|
/**
|
|
5
9
|
* This entry point is for Browser only
|
|
6
|
-
* It extends
|
|
10
|
+
* It extends `pryv` with Socket.IO capabilities
|
|
7
11
|
*/
|
|
8
|
-
const
|
|
12
|
+
const extendPryv = require('./index.js');
|
|
9
13
|
(function () {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
throw '"Pryv" is not accessible, add <script src="https://api.pryv.com/lib-js/pryv.js"></script> in your html file, before socket.io';
|
|
14
|
+
if (pryv == null) {
|
|
15
|
+
throw new Error('"pryv" is not accessible, add <script src="https://api.pryv.com/lib-js/pryv.js"></script> in your html file, before socket.io');
|
|
13
16
|
}
|
|
14
|
-
|
|
17
|
+
extendPryv(pryv);
|
|
15
18
|
})();
|
|
16
|
-
|
package/src/index.js
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
1
5
|
const SocketIO = require('./SocketIO');
|
|
2
6
|
|
|
3
7
|
/**
|
|
4
|
-
* Load
|
|
5
|
-
* @param {
|
|
8
|
+
* Load Socket.IO capabilities onto `pryv`
|
|
9
|
+
* @param {pryv} pryv `pryv` library @see https://github.com/pryv/lib-js
|
|
6
10
|
*/
|
|
7
|
-
module.exports = function(
|
|
8
|
-
console.log('lib
|
|
11
|
+
module.exports = function (pryv) {
|
|
12
|
+
console.log('"pryv" lib version', pryv.version);
|
|
9
13
|
// check version here
|
|
10
|
-
if (
|
|
11
|
-
throw new Error('
|
|
14
|
+
if (pryv.Connection.SocketIO) {
|
|
15
|
+
throw new Error('Socket.IO add-on already loaded');
|
|
12
16
|
}
|
|
13
17
|
// sharing cross references
|
|
14
|
-
|
|
15
|
-
SocketIO(
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
+
pryv.Connection.SocketIO = SocketIO;
|
|
19
|
+
SocketIO(pryv.Connection);
|
|
20
|
+
};
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
|
|
4
|
+
*/
|
|
5
|
+
/* global describe, it, before, after, beforeEach, afterEach, expect, pryv, testData */
|
|
6
|
+
/* eslint-disable no-unused-expressions */
|
|
7
|
+
|
|
4
8
|
const cuid = require('cuid');
|
|
5
|
-
const { apiEndpointWithToken } = require('../../lib-js/test/test-data');
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
require('@pryv/socket.io')(pryv);
|
|
11
|
+
|
|
12
|
+
let conn = null;
|
|
8
13
|
const testStreamId = 'socket-test';
|
|
9
14
|
|
|
10
|
-
describe('
|
|
15
|
+
describe('Socket.IO', function () {
|
|
11
16
|
this.timeout(3000);
|
|
12
|
-
let apiEndpoint;
|
|
17
|
+
let apiEndpoint;
|
|
13
18
|
let apiEndpointBogusToken;
|
|
14
19
|
let apiEndpointBogusUsername;
|
|
15
20
|
|
|
@@ -17,12 +22,12 @@ describe('SocketIO', function () {
|
|
|
17
22
|
this.timeout(5000);
|
|
18
23
|
await testData.prepare();
|
|
19
24
|
apiEndpoint = testData.apiEndpointWithToken;
|
|
20
|
-
apiEndpointBogusToken =
|
|
21
|
-
apiEndpointBogusUsername =
|
|
25
|
+
apiEndpointBogusToken = pryv.Service.buildAPIEndpoint(testData.serviceInfo, testData.username, 'toto');
|
|
26
|
+
apiEndpointBogusUsername = pryv.Service.buildAPIEndpoint(testData.serviceInfo, 'totototototo', testData.token);
|
|
22
27
|
});
|
|
23
28
|
|
|
24
29
|
before(async () => {
|
|
25
|
-
conn = new
|
|
30
|
+
conn = new pryv.Connection(apiEndpoint);
|
|
26
31
|
const res = await conn.api([{
|
|
27
32
|
method: 'streams.create',
|
|
28
33
|
params: {
|
|
@@ -36,56 +41,58 @@ describe('SocketIO', function () {
|
|
|
36
41
|
expect(res[0].error.id).to.equal('item-already-exists');
|
|
37
42
|
});
|
|
38
43
|
|
|
39
|
-
describe('init on invalid endpoint', () => {
|
|
44
|
+
describe('init on invalid endpoint', () => {
|
|
40
45
|
it('Should throw an error "Not Found" or ENOTFOUND when user is not known', async () => {
|
|
41
|
-
conn = new
|
|
46
|
+
conn = new pryv.Connection(apiEndpointBogusUsername);
|
|
42
47
|
try {
|
|
43
|
-
|
|
48
|
+
await conn.socket.open();
|
|
44
49
|
} catch (e) {
|
|
45
50
|
if (e.message === 'Not Found' || e.message.startsWith('getaddrinfo ENOTFOUND')) {
|
|
46
51
|
return;
|
|
47
52
|
}
|
|
48
|
-
|
|
53
|
+
if (typeof document !== 'undefined') { // in browser
|
|
54
|
+
console.warn('Error found by messages are not consistent among browsers:', e.message);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
throw new Error('Error message should be NotFound or ENOTFOUND and received: ' + e.message);
|
|
49
58
|
}
|
|
50
|
-
throw 'Should throw an error';
|
|
59
|
+
throw new Error('Should throw an error');
|
|
51
60
|
});
|
|
52
61
|
|
|
53
62
|
it('Should throw an error "Unauthorized" when token is invalid', async () => {
|
|
54
|
-
conn = new
|
|
63
|
+
conn = new pryv.Connection(apiEndpointBogusToken);
|
|
55
64
|
try {
|
|
56
|
-
|
|
65
|
+
await conn.socket.open();
|
|
57
66
|
} catch (e) {
|
|
58
67
|
return expect(e.response.text).to.include('Cannot find access from token.');
|
|
59
68
|
}
|
|
60
69
|
throw new Error('Should throw an error');
|
|
61
70
|
});
|
|
62
|
-
|
|
63
71
|
});
|
|
64
72
|
|
|
65
73
|
describe('init on valid endpoint', () => {
|
|
66
74
|
beforeEach(async () => {
|
|
67
|
-
conn = new
|
|
75
|
+
conn = new pryv.Connection(apiEndpoint);
|
|
68
76
|
});
|
|
69
77
|
|
|
70
78
|
afterEach(() => {
|
|
71
79
|
conn = null;
|
|
72
80
|
});
|
|
73
81
|
|
|
74
|
-
it('Should throw an error if conn.socket.api() is called before being open()', async() => {
|
|
75
|
-
try {
|
|
76
|
-
|
|
82
|
+
it('Should throw an error if conn.socket.api() is called before being open()', async () => {
|
|
83
|
+
try {
|
|
84
|
+
await conn.socket.api([{ method: 'events.get', params: {} }]);
|
|
77
85
|
} catch (e) {
|
|
78
|
-
return expect(e.message).to.
|
|
79
|
-
}
|
|
86
|
+
return expect(e.message).to.equal('Initialize socket.io with connection.socket.open() before');
|
|
87
|
+
}
|
|
80
88
|
throw new Error('Should throw an error');
|
|
81
89
|
});
|
|
82
90
|
|
|
83
|
-
|
|
84
91
|
it('Should throw an error if conn.socket.on() is called before being open()', async () => {
|
|
85
92
|
try {
|
|
86
|
-
|
|
93
|
+
await conn.socket.on('eventsChanged');
|
|
87
94
|
} catch (e) {
|
|
88
|
-
return expect(e.message).to.
|
|
95
|
+
return expect(e.message).to.equal('Initialize socket.io with connection.socket.open() before');
|
|
89
96
|
}
|
|
90
97
|
throw new Error('Should throw an error');
|
|
91
98
|
});
|
|
@@ -96,10 +103,9 @@ describe('SocketIO', function () {
|
|
|
96
103
|
});
|
|
97
104
|
});
|
|
98
105
|
|
|
99
|
-
|
|
100
|
-
describe('socket.api', () => {
|
|
106
|
+
describe('socket.api', () => {
|
|
101
107
|
before(async () => {
|
|
102
|
-
conn = new
|
|
108
|
+
conn = new pryv.Connection(apiEndpoint);
|
|
103
109
|
await conn.socket.open();
|
|
104
110
|
});
|
|
105
111
|
|
|
@@ -108,18 +114,17 @@ describe('SocketIO', function () {
|
|
|
108
114
|
});
|
|
109
115
|
|
|
110
116
|
it('Handle correctly batch calls', async () => {
|
|
111
|
-
const res = await conn.socket.api([{method: 'streams.get', params: {}}]);
|
|
117
|
+
const res = await conn.socket.api([{ method: 'streams.get', params: {} }]);
|
|
112
118
|
expect(res[0]).to.exist;
|
|
113
119
|
expect(res[0].streams).to.exist;
|
|
114
120
|
});
|
|
115
121
|
|
|
116
|
-
// we don't test further .api() as it relies on the implementation of
|
|
117
|
-
});
|
|
118
|
-
|
|
122
|
+
// we don't test further .api() as it relies on the implementation of pryv.Connection
|
|
123
|
+
});
|
|
119
124
|
|
|
120
125
|
describe('notification', () => {
|
|
121
126
|
before(async () => {
|
|
122
|
-
conn = new
|
|
127
|
+
conn = new pryv.Connection(apiEndpoint);
|
|
123
128
|
await conn.socket.open();
|
|
124
129
|
});
|
|
125
130
|
|
|
@@ -128,40 +133,38 @@ describe('SocketIO', function () {
|
|
|
128
133
|
});
|
|
129
134
|
|
|
130
135
|
it('Fails on requesting an invalid notifcation', async () => {
|
|
131
|
-
try {
|
|
136
|
+
try {
|
|
132
137
|
conn.socket.on('Bogus', () => {});
|
|
133
138
|
} catch (e) {
|
|
134
|
-
return expect(e.message).to.
|
|
139
|
+
return expect(e.message).to.equal('Unkown event [Bogus]. Allowed events are: eventsChanged,streamsChanged,accessesChanged,disconnect,error');
|
|
135
140
|
}
|
|
136
141
|
throw new Error('Should fail');
|
|
137
142
|
});
|
|
138
143
|
|
|
139
|
-
it('Catches eventChanges', (done) => {
|
|
140
|
-
function onEventChanged() {
|
|
144
|
+
it('Catches eventChanges', (done) => {
|
|
145
|
+
function onEventChanged () {
|
|
141
146
|
return done();
|
|
142
147
|
}
|
|
143
148
|
conn.socket.on('eventsChanged', onEventChanged);
|
|
144
|
-
conn.api([{ method: 'events.create', params: { type: 'note/txt', streamId: testStreamId }}]);
|
|
149
|
+
conn.api([{ method: 'events.create', params: { type: 'note/txt', streamId: testStreamId, content: 'hello' } }]);
|
|
145
150
|
});
|
|
146
151
|
|
|
147
|
-
|
|
148
152
|
it('Catches streamChanges', (done) => {
|
|
149
|
-
function onStreamChange() {
|
|
153
|
+
function onStreamChange () {
|
|
150
154
|
return done();
|
|
151
155
|
}
|
|
152
156
|
conn.socket.on('streamsChanged', onStreamChange);
|
|
153
|
-
|
|
157
|
+
conn.api([{ method: 'streams.create', params: { id: cuid(), name: cuid(), parentId: testStreamId } }]);
|
|
154
158
|
});
|
|
155
159
|
|
|
156
160
|
/** Keep this test the last on of this sequence */
|
|
157
161
|
it('Catches disconnect', (done) => {
|
|
158
|
-
function onDisconnect(reason) {
|
|
162
|
+
function onDisconnect (reason) {
|
|
159
163
|
expect(reason).to.equal('io client disconnect');
|
|
160
164
|
return done();
|
|
161
165
|
}
|
|
162
166
|
conn.socket.on('disconnect', onDisconnect);
|
|
163
167
|
conn.socket.close();
|
|
164
168
|
});
|
|
165
|
-
});
|
|
166
|
-
|
|
169
|
+
});
|
|
167
170
|
});
|
package/.mocharc.js
DELETED
package/LICENSE.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
Copyright (c) 2020, Pryv S.A.
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without modification,
|
|
5
|
-
are permitted provided that the following conditions are met:
|
|
6
|
-
|
|
7
|
-
- Redistributions of source code must retain the above copyright notice, this
|
|
8
|
-
list of conditions and the following disclaimer.
|
|
9
|
-
|
|
10
|
-
- Redistributions in binary form must reproduce the above copyright notice, this
|
|
11
|
-
list of conditions and the following disclaimer in the documentation and/or
|
|
12
|
-
other materials provided with the distribution.
|
|
13
|
-
|
|
14
|
-
- Neither the name of Pryv nor the names of its
|
|
15
|
-
contributors may be used to endorse or promote products derived from
|
|
16
|
-
this software without specific prior written permission.
|
|
17
|
-
|
|
18
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
19
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
20
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
21
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
22
|
-
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
25
|
-
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
27
|
-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/examples/index.html
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
<!doctype html>
|
|
2
|
-
<html>
|
|
3
|
-
<!--
|
|
4
|
-
This file is moved in dist/ during build process.
|
|
5
|
-
It should be opened in the same directory than pryv-socket.io.js built
|
|
6
|
-
-->
|
|
7
|
-
<head>
|
|
8
|
-
<meta charset="UTF-8" />
|
|
9
|
-
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
10
|
-
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
|
11
|
-
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
|
|
12
|
-
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
|
|
13
|
-
<link rel="stylesheet" type="text/css" href="https://api.pryv.com/style/pryv.min.css">
|
|
14
|
-
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,400">
|
|
15
|
-
|
|
16
|
-
<title>Pryv Socket.io - Javascript</title>
|
|
17
|
-
<script src="https://api.pryv.com/lib-js/pryv.js"></script>
|
|
18
|
-
<script src="pryv-socket.io.js"></script>
|
|
19
|
-
</head>
|
|
20
|
-
|
|
21
|
-
<body>
|
|
22
|
-
<div class="container">
|
|
23
|
-
<h1>Socket.io - Pryv Examples</h1>
|
|
24
|
-
<div class="card">
|
|
25
|
-
<span id="pryv-button"></span>
|
|
26
|
-
<div class="card-body">
|
|
27
|
-
<h2 class="card-title">When Logged-In - create a note</h2>
|
|
28
|
-
<input type='text' id='create-note' placeholder='Content' value='' />
|
|
29
|
-
<button onClick='createNoteEvent()'>Save Note</button>
|
|
30
|
-
<h2>Console</h2>
|
|
31
|
-
<textarea id='console' cols=50 rows=20></textarea>
|
|
32
|
-
<br>
|
|
33
|
-
<small>Source code of this demo app on <a href="https://github.com/pryv/lib-js-socket.io/blob/master/examples/index.html">GitHub</a></small>
|
|
34
|
-
</div>
|
|
35
|
-
</div>
|
|
36
|
-
</body>
|
|
37
|
-
|
|
38
|
-
<script>
|
|
39
|
-
// --- usual boiler plate
|
|
40
|
-
var $console = document.getElementById('console'),
|
|
41
|
-
$noteContent = document.getElementById('create-note');
|
|
42
|
-
|
|
43
|
-
var connection = null;
|
|
44
|
-
|
|
45
|
-
var authSettings = {
|
|
46
|
-
spanButtonID: 'pryv-button', // span id the DOM that will be replaced by the Service specific button
|
|
47
|
-
onStateChange: pryvAuthStateChange, // event Listener for Authentication steps
|
|
48
|
-
authRequest: { // See: https://api.pryv.com/reference/#auth-request
|
|
49
|
-
requestingAppId: 'lib-js-socket-io',
|
|
50
|
-
languageCode: 'en', // optional (default english)
|
|
51
|
-
requestedPermissions: [
|
|
52
|
-
{
|
|
53
|
-
streamId: '*',
|
|
54
|
-
level: 'read'
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
streamId: 'diary',
|
|
58
|
-
defaultName: 'Diary',
|
|
59
|
-
level: 'contribute'
|
|
60
|
-
}
|
|
61
|
-
],
|
|
62
|
-
clientData: {
|
|
63
|
-
'app-web-auth:description': {
|
|
64
|
-
'type': 'note/txt', 'content': 'I\'m watching events and adding notes to diary.'
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
function pryvAuthStateChange(state) { // called each time the authentication state changed
|
|
71
|
-
logToConsole('##pryvAuthStateChange ' + state.id);
|
|
72
|
-
if (state.id === Pryv.Browser.AuthStates.AUTHORIZED) {
|
|
73
|
-
connection = new Pryv.Connection(state.apiEndpoint);
|
|
74
|
-
logToConsole('# Auth succeeded for user ' + connection.apiEndpoint);
|
|
75
|
-
initializeSocket(connection);
|
|
76
|
-
}
|
|
77
|
-
if (state.id === Pryv.Browser.AuthStates.LOGOUT) {
|
|
78
|
-
connection = null;
|
|
79
|
-
logToConsole('# Logout');
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function createNoteEvent() {
|
|
84
|
-
if (connection == null) {
|
|
85
|
-
alert('Log-in first');
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
connection.api([{
|
|
89
|
-
method: 'events.create',
|
|
90
|
-
params: {
|
|
91
|
-
streamId: 'diary',
|
|
92
|
-
type: 'note/txt',
|
|
93
|
-
content: $noteContent.value
|
|
94
|
-
}
|
|
95
|
-
}]);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function logToConsole(text) {
|
|
99
|
-
$console.value += text + '\n';
|
|
100
|
-
$console.scrollTop = $console.scrollHeight;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// following the APP GUIDELINES: https://api.pryv.com/guides/app-guidelines/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// --- socket.io specific
|
|
107
|
-
(async function () {
|
|
108
|
-
const serviceInfoUrl = Pryv.Browser.serviceInfoFromUrl() || 'https://reg.pryv.me/service/info';
|
|
109
|
-
var service = await Pryv.Browser.setupAuth(authSettings, serviceInfoUrl);
|
|
110
|
-
})();
|
|
111
|
-
|
|
112
|
-
async function initializeSocket() {
|
|
113
|
-
const conn = connection;
|
|
114
|
-
try {
|
|
115
|
-
await conn.socket.open();
|
|
116
|
-
conn.socket.on('eventsChanged', async () => {
|
|
117
|
-
logToConsole('Socket Received "eventsChanged" message');
|
|
118
|
-
let res = await conn.socket.api([
|
|
119
|
-
{
|
|
120
|
-
method: 'events.get',
|
|
121
|
-
params: { limit: 1 }
|
|
122
|
-
}]);
|
|
123
|
-
logToConsole('Last event' + JSON.stringify(res[0], null, 2));
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
} catch (e) {
|
|
127
|
-
logToConsole('Error >>>>>>' + e.message);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
</script>
|
|
131
|
-
</html>
|
package/examples/screenshot.png
DELETED
|
Binary file
|
package/examples/simplesocket.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const Pryv = require('pryv');
|
|
2
|
-
require('../src/')(Pryv);
|
|
3
|
-
|
|
4
|
-
let apiEndpoint = 'https://ckbcft5ai0004z60sbsmv56mc@my-computer.rec.la:4443/perki/';
|
|
5
|
-
|
|
6
|
-
const conn = new Pryv.Connection(apiEndpoint);
|
|
7
|
-
|
|
8
|
-
(async () => {
|
|
9
|
-
try {
|
|
10
|
-
const s = await conn.socket.open();
|
|
11
|
-
s.on('eventsChanged',async () => {
|
|
12
|
-
console.log('Got eventsChanged');
|
|
13
|
-
const res = await conn.socket.api([{ method: 'events.get', params: {limit: 1 } }]);
|
|
14
|
-
console.log('Last event: ', JSON.stringify(res, null, 2));
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
} catch (e) {
|
|
18
|
-
console.log('Error: ', e.message);
|
|
19
|
-
}
|
|
20
|
-
})();
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
#!/bin/sh
|
|
2
|
-
|
|
3
|
-
# working dir fix
|
|
4
|
-
scriptsFolder=$(cd $(dirname "$0"); pwd)
|
|
5
|
-
cd $scriptsFolder/..
|
|
6
|
-
|
|
7
|
-
# check for basic prerequisites
|
|
8
|
-
hash git 2>&- || { echo >&2 "I require git."; exit 1; }
|
|
9
|
-
hash npm 2>&- || { echo >&2 "I require Node and NPM."; exit 1; }
|
|
10
|
-
|
|
11
|
-
echo "
|
|
12
|
-
Installing Node modules if necessary...
|
|
13
|
-
"
|
|
14
|
-
npm install
|
|
15
|
-
|
|
16
|
-
distFolder=dist
|
|
17
|
-
if [ ! -d $distFolder ]
|
|
18
|
-
then
|
|
19
|
-
echo "
|
|
20
|
-
Setting up '$distFolder' folder for publishing to GitHub pages...
|
|
21
|
-
"
|
|
22
|
-
git clone -b gh-pages git@github.com:pryv/lib-js-socket.io.git $distFolder
|
|
23
|
-
fi
|
|
24
|
-
|
|
25
|
-
echo "
|
|
26
|
-
|
|
27
|
-
OK!
|
|
28
|
-
"
|
package/test/helpers.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Loaded by .mocharc.js for node tests
|
|
3
|
-
*/
|
|
4
|
-
const chai = require('chai');
|
|
5
|
-
const Pryv = require('pryv');
|
|
6
|
-
require('../src')(Pryv); // Loading SocketIO on Pryv
|
|
7
|
-
const testData = require('../node_modules/pryv/test/test-data.js');
|
|
8
|
-
global.chai = chai;
|
|
9
|
-
global.Pryv = Pryv;
|
|
10
|
-
global.testData = testData;
|
|
11
|
-
global.should = chai.should();
|
|
12
|
-
global.expect = chai.expect;
|
|
13
|
-
|
package/webpack.config.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const CopyPlugin = require('copy-webpack-plugin');
|
|
3
|
-
const { webpackBabelConfig } = require('@pryv/lib-js-common');
|
|
4
|
-
|
|
5
|
-
module.exports = [
|
|
6
|
-
{ // es6
|
|
7
|
-
mode: 'production',
|
|
8
|
-
entry: {
|
|
9
|
-
'pryv-socket.io': './src/browser-index.js'
|
|
10
|
-
},
|
|
11
|
-
output: {
|
|
12
|
-
filename: '[name]-es6.js',
|
|
13
|
-
path: path.resolve(__dirname, 'dist')
|
|
14
|
-
},
|
|
15
|
-
devtool: 'source-map'
|
|
16
|
-
},
|
|
17
|
-
{ // es5
|
|
18
|
-
mode: 'production',
|
|
19
|
-
entry: {
|
|
20
|
-
'pryv-socket.io': ['core-js/stable','./src/browser-index.js']
|
|
21
|
-
},
|
|
22
|
-
output: {
|
|
23
|
-
filename: '[name].js',
|
|
24
|
-
path: path.resolve(__dirname, 'dist')
|
|
25
|
-
},
|
|
26
|
-
plugins: [
|
|
27
|
-
new CopyPlugin({
|
|
28
|
-
patterns: [
|
|
29
|
-
{ from: 'examples/index.html', to: 'index.html' },
|
|
30
|
-
],
|
|
31
|
-
}),
|
|
32
|
-
],
|
|
33
|
-
module: webpackBabelConfig,
|
|
34
|
-
devtool: 'source-map'
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
];
|