@pryv/monitor 1.0.6 → 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.
@@ -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 Pryv*/
6
+ /* global pryv */
3
7
 
4
8
  /**
5
9
  * This entry point is for Browser only
6
- * It extends "Pryv" with Monitor capabilities
10
+ * It extends `pryv` with Monitor capabilities
7
11
  */
8
- const extendsPryv = require('./index.js');
12
+ const extendPryv = require('./index.js');
9
13
  (function () {
10
-
11
- if (Pryv == null) {
12
- throw '"Pryv" is not accessible, add <script src="https://api.pryv.com/lib-js/pryv.js"></script> in your html file, before pryv-monitor.js';
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 pryv-monitor.js');
13
16
  }
14
- extendsPryv(Pryv);
17
+ extendPryv(pryv);
15
18
  })();
16
-
package/src/index.js CHANGED
@@ -1,26 +1,31 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const Monitor = require('./Monitor');
2
6
  const Changes = require('./lib/Changes');
3
7
 
4
8
  Monitor.Changes = Changes;
5
9
 
6
10
  /**
7
- * Load Monitor capabilities onto Pryv
8
- * @param {Pryv} Pryv - Pryv lib-js library @see https://github.com/pryv/lib-js
11
+ * Load Monitor capabilities onto `pryv`
12
+ * @param {pryv} pryv `pryv` library @see https://github.com/pryv/lib-js
9
13
  */
10
- module.exports = function(Pryv) {
11
- console.log('Pryv version', Pryv.version);
14
+ module.exports = function (pryv) {
15
+ console.log('Pryv version', pryv.version);
12
16
  // check version here
13
- if (Pryv.Monitor) {
17
+ if (pryv.Monitor) {
14
18
  throw new Error('Monitor already loaded');
15
19
  }
16
20
  // sharing cross references
17
- Pryv.Monitor = Monitor;
18
- Monitor.Pryv = Pryv;
21
+ pryv.Monitor = Monitor;
22
+ // TODO: remove deprecated `Pryv` alias with next major version
23
+ Monitor.pryv = Monitor.Pryv = pryv;
19
24
  return Monitor;
20
- }
25
+ };
21
26
 
22
27
  /**
23
- * @typedef Pryv.Monitor.Changes
28
+ * @typedef pryv.Monitor.Changes
24
29
  * @property {string} EVENT "event" fired on new or changed event
25
30
  * @property {string} EVENT_DELETE "eventDelete"
26
31
  * @property {string} STREAMS "streams"
@@ -29,11 +34,10 @@ module.exports = function(Pryv) {
29
34
  * @property {string} STOP "stop"
30
35
  */
31
36
 
32
-
33
37
  /**
34
38
  * A scope corresponding to EventGetParameters @see https://l.rec.la:4443/reference#get-events
35
39
  * Property `limit` cannot be specified;
36
- * @typedef {Object} Pryv.Monitor.Scope
40
+ * @typedef {Object} pryv.Monitor.Scope
37
41
  * @property {timestamp} [fromTime=TIMERANGE_MIN] (in seconds)
38
42
  * @property {timestamp} [toTime=TIMERANGE_MAX] (in seconds)
39
43
  * @property {string[]} [streams] - array of streamIds
@@ -45,4 +49,3 @@ module.exports = function(Pryv) {
45
49
  * @property {boolean} [includeDeletions]
46
50
  * @property {timestamp} modifiedSince - (in seconds) only events modified after this date
47
51
  */
48
-
@@ -1,7 +1,11 @@
1
- /**
2
- * Enum trigger messages
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /**
6
+ * Enum trigger messages
3
7
  * @readonly
4
- * @enum {Pryv.Monitor.Changes}
8
+ * @enum {pryv.Monitor.Changes}
5
9
  */
6
10
  const Changes = {
7
11
  EVENT: 'event',
@@ -12,4 +16,4 @@ const Changes = {
12
16
  STOP: 'stop'
13
17
  };
14
18
 
15
- module.exports = Changes;
19
+ module.exports = Changes;
@@ -1,14 +1,18 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const Changes = require('./Changes');
2
6
 
3
- module.exports = async function _updateEvents(monitor) {
4
- function forEachEvent(event) {
7
+ module.exports = async function _updateEvents (monitor) {
8
+ function forEachEvent (event) {
5
9
  // update eventsGetScope with "latest modified" information found
6
10
  if (event.modified > monitor.eventsGetScope.modifiedSince) {
7
11
  monitor.eventsGetScope.modifiedSince = event.modified;
8
12
  }
9
13
  if (event.deleted) {
10
- // event.delete is actually the date it was deleted.
11
- // use it as "modified" information
14
+ // event.delete is actually the date it was deleted.
15
+ // use it as "modified" information
12
16
  if (event.deleted > monitor.eventsGetScope.modifiedSince) {
13
17
  monitor.eventsGetScope.modifiedSince = event.deleted;
14
18
  }
@@ -22,4 +26,4 @@ module.exports = async function _updateEvents(monitor) {
22
26
  } catch (e) {
23
27
  monitor.emit(Changes.ERROR, e);
24
28
  }
25
- }
29
+ };
@@ -1,11 +1,15 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
1
5
  const Changes = require('./Changes');
2
6
 
3
- module.exports = async function _updateStreams(monitor) {
7
+ module.exports = async function _updateStreams (monitor) {
4
8
  try {
5
9
  const result = await monitor.connection.get('streams');
6
- if (!result.streams) { throw new Error('Invalid response ' + JSON.streams(result)) }
10
+ if (!result.streams) { throw new Error('Invalid response ' + JSON.streams(result)); }
7
11
  monitor.emit(Changes.STREAMS, result.streams);
8
12
  } catch (e) {
9
13
  monitor.emit(Changes.ERROR, e);
10
14
  }
11
- }
15
+ };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /* global expect, pryv, testData */
6
+ /* eslint-disable no-unused-expressions */
7
+
8
+ require('../src')(pryv);
9
+ const testStreamId = global.testStreamId = 'monitor-test';
10
+
11
+ global.prepareAndCreateBaseStreams = async () => {
12
+ await testData.prepare();
13
+ global.apiEndpoint = testData.apiEndpointWithToken;
14
+ global.conn = new pryv.Connection(global.apiEndpoint);
15
+ const res = await global.conn.api([{
16
+ method: 'streams.create',
17
+ params: {
18
+ id: testStreamId,
19
+ name: testStreamId
20
+ }
21
+ }]);
22
+ expect(res[0]).to.exist;
23
+ if (res[0].stream) return;
24
+ expect(res[0].error).to.exist;
25
+ expect(res[0].error.id).to.equal('item-already-exists');
26
+ };
@@ -1,49 +1,51 @@
1
- /*global
2
- Pryv, chai, should, testData, conn, apiEndpoint, prepareAndcreateBaseStreams
3
- */
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /* global describe, it, before, beforeEach, afterEach, expect, pryv, conn, apiEndpoint, prepareAndCreateBaseStreams */
4
6
 
5
- const Pryv = require("pryv");
7
+ require('./load-helpers');
6
8
 
7
9
  describe('Monitor', function () {
8
10
  this.timeout(3000);
9
11
 
10
12
  before(async function () {
11
13
  this.timeout(5000);
12
- await prepareAndcreateBaseStreams();
14
+ await prepareAndCreateBaseStreams();
13
15
  });
14
16
 
15
17
  describe('init', () => {
16
18
  it('can be initialized with an apiEndpoint', async () => {
17
- const monitor = new Pryv.Monitor(apiEndpoint, { limit: 1 });
19
+ const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 });
18
20
  await monitor.start();
19
21
  });
20
22
 
21
23
  it('can be initialized with a connection', async () => {
22
- const monitor = new Pryv.Monitor(conn, { limit: 1 });
24
+ const monitor = new pryv.Monitor(conn, { limit: 1 });
23
25
  await monitor.start();
24
26
  });
25
27
 
26
28
  it('throw Error on invalid apiEndpoint', async () => {
27
- const passed = true;
29
+ let passed = true;
28
30
  try {
29
- const monitor = new Pryv.Monitor('BlipBlop', { limit: 1 });
31
+ /* eslint-disable-next-line no-unused-vars */
32
+ const monitor = new pryv.Monitor('BlipBlop', { limit: 1 });
30
33
  passed = false;
31
- } catch(e) {
34
+ } catch (e) {
32
35
 
33
36
  }
34
37
  expect(passed).to.equal(true);
35
38
  });
36
-
37
39
  });
38
40
 
39
41
  describe('notifications', () => {
40
42
  let monitor = null;
41
43
  beforeEach(async () => {
42
- monitor = new Pryv.Monitor(conn, { limit: 1 });
44
+ monitor = new pryv.Monitor(conn, { limit: 1 });
43
45
  });
44
46
 
45
47
  afterEach(async () => {
46
- monitor.stop()
48
+ monitor.stop();
47
49
  });
48
50
 
49
51
  it('Load events at start', async function () {
@@ -56,22 +58,22 @@ describe('Monitor', function () {
56
58
  {
57
59
  method: 'events.create',
58
60
  params: {
59
- streamId: testStreamId,
61
+ streamId: global.testStreamId,
60
62
  type: 'note/txt',
61
63
  content: 'hello monitor'
62
64
  }
63
65
  }
64
- ])
65
- await new Promise(r => setTimeout(r, 2000));
66
+ ]);
67
+ await new Promise(resolve => setTimeout(resolve, 2000));
66
68
  expect(count).to.be.gt(0);
67
69
  });
68
70
 
69
71
  it('Detect new events added', async function () {
70
- let count = 0;
72
+ let count = 0;
71
73
  await monitor.start();
72
74
 
73
75
  const eventData = {
74
- streamId: testStreamId,
76
+ streamId: global.testStreamId,
75
77
  type: 'note/txt',
76
78
  content: 'hello monitor ' + new Date()
77
79
  };
@@ -80,17 +82,15 @@ describe('Monitor', function () {
80
82
  expect(event.content).to.equal(eventData.content);
81
83
  count++;
82
84
  });
83
- const res = await conn.api([
85
+ await conn.api([
84
86
  {
85
87
  method: 'events.create',
86
88
  params: eventData
87
89
  }
88
90
  ]);
89
91
  await monitor.updateEvents(); // trigger refresh
90
- await new Promise(r => setTimeout(r, 2000));
92
+ await new Promise(resolve => setTimeout(resolve, 2000));
91
93
  expect(count).to.be.gt(0);
92
94
  });
93
-
94
-
95
95
  });
96
96
  });
@@ -1,26 +1,30 @@
1
- /*global
2
- Pryv, chai, should, testData, conn, apiEndpoint, creaBaseStreams
3
- */
4
- require('@pryv/socket.io')(Pryv);
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /* global describe, it, before, expect, pryv, conn, apiEndpoint, prepareAndCreateBaseStreams */
5
6
 
6
- describe('Monitor + Socket.io', function () {
7
+ require('./load-helpers');
8
+ require('@pryv/socket.io')(pryv);
9
+
10
+ describe('Monitor + Socket.IO', function () {
7
11
  this.timeout(3000);
8
12
 
9
13
  before(async () => {
10
- await prepareAndcreateBaseStreams();
14
+ await prepareAndCreateBaseStreams();
11
15
  });
12
16
 
13
17
  describe('socket updates', function () {
14
18
  this.timeout(5000);
15
19
  it('Detect new events added', async function () {
16
- const monitor = new Pryv.Monitor(apiEndpoint, { limit: 1 })
17
- .addUpdateMethod(new Pryv.Monitor.UpdateMethod.Socket());
20
+ const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
21
+ .addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket());
18
22
  await monitor.start();
19
-
23
+
20
24
  let count = 0;
21
25
 
22
26
  const eventData = {
23
- streamId: testStreamId,
27
+ streamId: global.testStreamId,
24
28
  type: 'note/txt',
25
29
  content: 'hello monitor ' + new Date()
26
30
  };
@@ -29,33 +33,32 @@ describe('Monitor + Socket.io', function () {
29
33
  expect(event.content).to.equal(eventData.content);
30
34
  count++;
31
35
  });
32
- await new Promise(r => setTimeout(r, 1000));
33
- const res = await conn.api([
36
+ await new Promise(resolve => setTimeout(resolve, 1000));
37
+ await conn.api([
34
38
  {
35
39
  method: 'events.create',
36
40
  params: eventData
37
41
  }
38
42
  ]);
39
43
 
40
- await new Promise(r => setTimeout(r, 2000));
44
+ await new Promise(resolve => setTimeout(resolve, 2000));
41
45
  monitor.stop();
42
46
  expect(count).to.be.gt(0);
43
-
44
47
  });
45
48
  });
46
49
 
47
50
  describe('stop', () => {
48
51
  it('Monitor stops when requested', async function () {
49
52
  this.timeout(4000);
50
- const monitor = new Pryv.Monitor(apiEndpoint, { limit: 1 }).
51
- addUpdateMethod(new Pryv.Monitor.UpdateMethod.Socket());
53
+ const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
54
+ .addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket());
52
55
  await monitor.start();
53
56
  let count = 0;
54
- await new Promise(r => setTimeout(r, 1000));
57
+ await new Promise(resolve => setTimeout(resolve, 1000));
55
58
  monitor.stop();
56
59
 
57
60
  const eventData = {
58
- streamId: testStreamId,
61
+ streamId: global.testStreamId,
59
62
  type: 'note/txt',
60
63
  content: 'hello monitor ' + new Date()
61
64
  };
@@ -63,16 +66,15 @@ describe('Monitor + Socket.io', function () {
63
66
  monitor.on('event', function (event) {
64
67
  count++;
65
68
  });
66
- const res = await conn.api([
69
+ await conn.api([
67
70
  {
68
71
  method: 'events.create',
69
72
  params: eventData
70
73
  }
71
74
  ]);
72
75
 
73
- await new Promise(r => setTimeout(r, 1000));
74
- expect(count).to.equals(0);
76
+ await new Promise(resolve => setTimeout(resolve, 1000));
77
+ expect(count).to.equal(0);
75
78
  });
76
79
  });
77
-
78
80
  });
@@ -1,38 +1,38 @@
1
- /*global
2
- Pryv, chai, should, testData, conn, apiEndpoint, creaBaseStreams
3
- */
4
-
5
- const testData = require("pryv/test/test-data");
6
- const Pryv = require("pryv");
1
+ /**
2
+ * @license
3
+ * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
+ */
5
+ /* global describe, it, before, expect, pryv, conn, apiEndpoint, prepareAndCreateBaseStreams */
7
6
 
7
+ require('./load-helpers');
8
8
 
9
9
  describe('Monitor + EventsTimer', function () {
10
10
  this.timeout(3000);
11
11
 
12
12
  before(async () => {
13
- await prepareAndcreateBaseStreams();
13
+ await prepareAndCreateBaseStreams();
14
14
  });
15
15
 
16
16
  describe('init', () => {
17
17
  it('throw error if timer is not inistialized with correct time', async () => {
18
- const monitor = new Pryv.Monitor(apiEndpoint, { limit: 1 });
18
+ const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 });
19
19
  try {
20
- new Pryv.Monitor.UpdateMethod.EventsTimer(monitor, 'Rt');
20
+ /* eslint-disable-next-line no-new */
21
+ new pryv.Monitor.UpdateMethod.EventsTimer(monitor, 'Rt');
21
22
  } catch (e) {
22
23
  return expect(e.message).to.equal('Monitor timer refresh rate is not valid. It should be a number > 1');
23
24
  }
24
- throw new Error('Should thow error')
25
+ throw new Error('Should thow error');
25
26
  });
26
27
  });
27
28
 
28
29
  describe('timer updates', () => {
29
30
  it('Detect new events added', async function () {
30
- const monitor = new Pryv.Monitor(apiEndpoint, { limit: 1 })
31
- .addUpdateMethod(new Pryv.Monitor.UpdateMethod.EventsTimer(1));
31
+ const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
32
+ .addUpdateMethod(new pryv.Monitor.UpdateMethod.EventsTimer(1));
32
33
  await monitor.start();
33
34
  let count = 0;
34
35
 
35
-
36
36
  // listener is added "after" so we don't get events loaded at start
37
37
  monitor.on('event', function (event) {
38
38
  expect(event.content).to.equal(eventData.content);
@@ -40,34 +40,33 @@ describe('Monitor + EventsTimer', function () {
40
40
  });
41
41
 
42
42
  const eventData = {
43
- streamId: testStreamId,
43
+ streamId: global.testStreamId,
44
44
  type: 'note/txt',
45
45
  content: 'hello monitor ' + new Date()
46
46
  };
47
47
 
48
- const res = await conn.api([
48
+ await conn.api([
49
49
  {
50
50
  method: 'events.create',
51
51
  params: eventData
52
52
  }
53
53
  ]);
54
54
 
55
- await new Promise(r => setTimeout(r, 2000));
55
+ await new Promise(resolve => setTimeout(resolve, 2000));
56
56
  monitor.stop();
57
57
  expect(count).to.be.gt(0);
58
-
59
58
  });
60
59
  });
61
60
 
62
61
  describe('stop', () => {
63
62
  it('Monitor stops when requested', async function () {
64
63
  this.timeout(4000);
65
- const monitor = await new Pryv.Monitor(apiEndpoint, { limit: 1 })
66
- .addUpdateMethod(new Pryv.Monitor.UpdateMethod.EventsTimer(1));
64
+ const monitor = await new pryv.Monitor(apiEndpoint, { limit: 1 })
65
+ .addUpdateMethod(new pryv.Monitor.UpdateMethod.EventsTimer(1));
67
66
  await monitor.start();
68
67
 
69
68
  let count = 0;
70
- await new Promise(r => setTimeout(r, 1000));
69
+ await new Promise(resolve => setTimeout(resolve, 1000));
71
70
  monitor.stop();
72
71
 
73
72
  // listener is added "after" so we don't get events loaded at start
@@ -76,21 +75,20 @@ describe('Monitor + EventsTimer', function () {
76
75
  });
77
76
 
78
77
  const eventData = {
79
- streamId: testStreamId,
78
+ streamId: global.testStreamId,
80
79
  type: 'note/txt',
81
80
  content: 'hello monitor ' + new Date()
82
81
  };
83
82
 
84
- const res = await conn.api([
83
+ await conn.api([
85
84
  {
86
85
  method: 'events.create',
87
86
  params: eventData
88
87
  }
89
88
  ]);
90
89
 
91
- await new Promise(r => setTimeout(r, 1000));
92
- expect(count).to.equals(0);
90
+ await new Promise(resolve => setTimeout(resolve, 1000));
91
+ expect(count).to.equal(0);
93
92
  });
94
93
  });
95
-
96
94
  });
package/.mocharc.js DELETED
@@ -1,11 +0,0 @@
1
- module.exports = {
2
- diff: true,
3
- extension: ['js'],
4
- opts: false,
5
- package: './package.json',
6
- reporter: 'spec',
7
- slow: 75,
8
- timeout: 2000,
9
- require: 'test/helpers.js',
10
- ui: 'bdd'
11
- };
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.