@pryv/monitor 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pryv/monitor",
3
- "version": "2.4.7",
3
+ "version": "3.0.1",
4
4
  "description": "Extends `pryv` with event-driven notifications for changes on a Pryv.io account",
5
5
  "keywords": [
6
6
  "Pryv",
@@ -20,5 +20,8 @@
20
20
  "license": "BSD-3-Clause",
21
21
  "author": "Pryv S.A. <info@pryv.com> (https://pryv.com)",
22
22
  "main": "src/index.js",
23
- "types": "src/index.d.ts"
23
+ "types": "src/index.d.ts",
24
+ "engines": {
25
+ "node": ">=20.0.0"
26
+ }
24
27
  }
package/src/Monitor.js CHANGED
@@ -9,16 +9,20 @@ const _updateStreams = require('./lib/updateStreams');
9
9
  const Changes = require('./lib/Changes');
10
10
 
11
11
  /**
12
+ * Monitor changes on a Pryv.io account.
13
+ * Emits events when data changes are detected.
12
14
  * @memberof pryv
15
+ * @extends EventEmitter
13
16
  */
14
17
  class Monitor extends EventEmitter {
15
18
  /**
16
- *
17
- * @param {(pryv.APIEndpoint|pryv.Connection)} apiEndpointOrConnection APIEndoint or connection to use
18
- * @param {pryv.Monitor.Scope} [eventsGetScope={}] The Scope to monitor
19
+ * Create a new Monitor
20
+ * @param {(string|Connection)} apiEndpointOrConnection - API endpoint URL or Connection instance
21
+ * @param {MonitorScope} [eventsGetScope={}] - The scope to monitor (events.get parameters)
19
22
  */
20
23
  constructor (apiEndpointOrConnection, eventsGetScope = {}) {
21
24
  super();
25
+ // @ts-ignore - pryv is set at runtime by extendPryvMonitor
22
26
  if (!Monitor.pryv) {
23
27
  throw new Error('package \'@pryv/monitor\' must loaded after package \'pryv\'');
24
28
  }
@@ -30,9 +34,11 @@ class Monitor extends EventEmitter {
30
34
  };
31
35
  Object.assign(this.eventsGetScope, eventsGetScope);
32
36
 
37
+ // @ts-ignore - pryv is set at runtime
33
38
  if (apiEndpointOrConnection instanceof Monitor.pryv.Connection) {
34
39
  this.connection = apiEndpointOrConnection;
35
40
  } else {
41
+ // @ts-ignore - pryv is set at runtime
36
42
  this.connection = new Monitor.pryv.Connection(apiEndpointOrConnection);
37
43
  }
38
44
  this.states = {
@@ -44,8 +50,8 @@ class Monitor extends EventEmitter {
44
50
  }
45
51
 
46
52
  /**
47
- * Start the monitor
48
- * @returns {Monitor} this
53
+ * Start the monitor and perform initial sync
54
+ * @returns {Promise<Monitor>} Promise resolving to this Monitor instance
49
55
  */
50
56
  async start () {
51
57
  if (this.states.started || this.states.starting) return this;
@@ -63,8 +69,8 @@ class Monitor extends EventEmitter {
63
69
  }
64
70
 
65
71
  /**
66
- * request and update of events
67
- * @returns {Monitor} this
72
+ * Request an events update according to the current scope
73
+ * @returns {Promise<Monitor>} Promise resolving to this Monitor instance
68
74
  */
69
75
  async updateEvents () {
70
76
  if (!this.states.started) {
@@ -95,8 +101,8 @@ class Monitor extends EventEmitter {
95
101
  }
96
102
 
97
103
  /**
98
- * request and update of streams
99
- * @returns {Monitor} this
104
+ * Request a streams update
105
+ * @returns {Promise<Monitor>} Promise resolving to this Monitor instance
100
106
  */
101
107
  async updateStreams () {
102
108
  if (!this.states.started) {
@@ -157,17 +163,11 @@ class Monitor extends EventEmitter {
157
163
  return this.states.started;
158
164
  }
159
165
 
160
- /**
161
- * Initialize the updateMethods with this Monitor
162
- * @callback Monitor~UpdateMethod
163
- * @param {Monitor} setMonitor
164
- */
165
-
166
166
  /**
167
167
  * @private
168
- * Called my UpdateMethod to share cross references
168
+ * Called by UpdateMethod to share cross references
169
169
  * Set a custom update method
170
- * @param {Monitor~UpdateMethod} updateMethod - the auto-update method
170
+ * @param {Object} updateMethod - the auto-update method
171
171
  * @returns {Monitor} this
172
172
  */
173
173
  addUpdateMethod (updateMethod) {
@@ -4,9 +4,14 @@
4
4
  */
5
5
  const UpdateMethod = require('./UpdateMethod');
6
6
 
7
+ /**
8
+ * Update method that polls for event changes at a fixed interval.
9
+ * @memberof pryv.Monitor
10
+ * @extends UpdateMethod
11
+ */
7
12
  class EventsTimer extends UpdateMethod {
8
13
  /**
9
- * @param {Number} updateRateMS - the refresh rate in milliseconds
14
+ * @param {number} updateRateMS - The refresh rate in milliseconds (must be > 1)
10
15
  */
11
16
  constructor (updateRateMS) {
12
17
  super();
@@ -6,17 +6,24 @@
6
6
  const UpdateMethod = require('./UpdateMethod');
7
7
  const Changes = require('../lib/Changes');
8
8
 
9
+ /**
10
+ * Update method that uses @pryv/socket.io events for real-time updates.
11
+ * Requires @pryv/socket.io to be loaded.
12
+ * @memberof pryv.Monitor
13
+ * @extends UpdateMethod
14
+ */
9
15
  class Socket extends UpdateMethod {
10
16
  async ready () {
11
17
  if (this.socket) return;
18
+ // @ts-ignore - socket is added by @pryv/socket.io extension
12
19
  if (!this.monitor.connection.socket) {
13
20
  throw new Error('You should load package @pryv/socket.io to use monitor with websockets');
14
21
  }
22
+ // @ts-ignore - socket is added by @pryv/socket.io extension
15
23
  this.socket = await this.monitor.connection.socket.open();
16
24
  this.socket.on('eventsChanged', () => { this.monitor.updateEvents(); });
17
25
  this.socket.on('streamsChanged', () => { this.monitor.updateStreams(); });
18
- /* eslint-disable-next-line node/handle-callback-err */
19
- this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR.error); });
26
+ this.socket.on('error', (error) => { this.monitor.emit(Changes.ERROR, error); });
20
27
  }
21
28
 
22
29
  async stop () {
@@ -4,22 +4,24 @@
4
4
  */
5
5
  const Changes = require('../lib/Changes');
6
6
  /**
7
- * Interface for UpdateMonitor
7
+ * Base class for update methods used by Monitor.
8
+ * Subclass this to create custom update strategies.
8
9
  * @memberof pryv.Monitor
9
- * @constructor {Monitor~UpdateMethod} updateMethod.setMonitor - set only once
10
10
  */
11
11
  class UpdateMethod {
12
12
  /**
13
13
  * Assign a Monitor to this updater.
14
14
  * Usually called by the monitor itself on monitor.addUpdateMethod()
15
- * @param {Monitor} monitor
15
+ * @param {Monitor} monitor - The monitor to attach to
16
16
  */
17
17
  setMonitor (monitor) {
18
18
  if (this.monitor) {
19
19
  throw new Error('An update Method can be assigned to one monitor only');
20
20
  }
21
21
  this.monitor = monitor;
22
+ // @ts-ignore - Changes.READY and Changes.STOP are valid event names
22
23
  monitor.on(Changes.READY, this.ready.bind(this));
24
+ // @ts-ignore
23
25
  monitor.on(Changes.STOP, this.stop.bind(this));
24
26
  if (monitor.started) {
25
27
  this.ready();
@@ -27,15 +29,15 @@ class UpdateMethod {
27
29
  }
28
30
 
29
31
  /**
30
- * Should be overwritten by subclases
31
- * Called with no params, when all update tasks are done.
32
- * Also used at "start" call
32
+ * Called when all update tasks are done and monitor is ready for next update.
33
+ * Override in subclasses to implement custom behavior.
34
+ * @returns {Promise<void>}
33
35
  */
34
36
  async ready () { }
35
37
 
36
38
  /**
37
- * Should be overwritten by subclases
38
- * Called with no params, when monitor is stoped: updater should be stoped too.
39
+ * Called when monitor is stopped. Override to clean up resources.
40
+ * @returns {Promise<void>}
39
41
  */
40
42
  async stop () { }
41
43
  }
package/src/index.d.ts CHANGED
@@ -1,28 +1,40 @@
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 monitoring capabilities.
6
18
  *
7
19
  * Typical usage:
8
20
  * ```ts
9
21
  * import pryv from 'pryv';
10
- * import monitor from '@pryv/monitor';
22
+ * import extendPryvMonitor from '@pryv/monitor';
11
23
  *
12
24
  * extendPryvMonitor(pryv);
13
25
  *
14
- * const monitor = new pryv.Monitor('https://token@user.pryv.me', {
26
+ * const mon = new pryv.Monitor('https://token@user.pryv.me', {
15
27
  * streams: ['data'],
16
28
  * });
17
29
  *
18
- * monitor.on(pryv.Monitor.Changes.EVENT, (event) => {
30
+ * mon.on(pryv.Monitor.Changes.EVENT, (event) => {
19
31
  * // handle new or changed event
20
32
  * });
21
33
  *
22
- * await monitor.start();
34
+ * await mon.start();
23
35
  * ```
24
36
  */
25
- export default function extendPryvMonitor(pryvLib: typeof pryv): typeof pryv.Monitor;
37
+ export default function extendPryvMonitor(pryvLib: PryvLibrary): typeof pryv.Monitor;
26
38
 
27
39
  declare module 'pryv' {
28
40
 
@@ -60,6 +72,9 @@ declare module 'pryv' {
60
72
  export class Monitor extends EventEmitter {
61
73
  constructor(apiEndpointOrConnection: APIEndpoint | Connection, eventsGetScope?: MonitorScope);
62
74
 
75
+ /** The connection used by this monitor */
76
+ readonly connection: Connection;
77
+
63
78
  /**
64
79
  * Start the monitor and perform initial sync.
65
80
  * Resolves with the same monitor instance.
@@ -93,6 +108,7 @@ declare module 'pryv' {
93
108
 
94
109
  /**
95
110
  * Attach an auto-update method implementation.
111
+ * @param updateMethod - The update method to use (e.g., EventsTimer, Socket)
96
112
  */
97
113
  addUpdateMethod(updateMethod: MonitorUpdateMethod): Monitor;
98
114
 
@@ -101,19 +117,18 @@ declare module 'pryv' {
101
117
  *
102
118
  * Events:
103
119
  * - `event` – new or updated event
104
- * - `eventDelete` – deleted event
120
+ * - `eventDelete` – deleted event (with id property)
105
121
  * - `streams` – updated streams list
106
122
  * - `error` – error object
107
123
  * - `ready` – monitor is ready for next update
108
124
  * - `stop` – monitor has been stopped
109
125
  */
110
126
  on(event: 'event', listener: (event: Event) => void): this;
111
- on(event: 'eventDelete', listener: (event: Event) => void): this;
127
+ on(event: 'eventDelete', listener: (deletion: ItemDeletion) => void): this;
112
128
  on(event: 'streams', listener: (streams: Stream[]) => void): this;
113
- on(event: 'error', listener: (error: any) => void): this;
129
+ on(event: 'error', listener: (error: Error | unknown) => void): this;
114
130
  on(event: 'ready', listener: () => void): this;
115
131
  on(event: 'stop', listener: () => void): this;
116
- on(event: string, listener: (...args: any[]) => void): this;
117
132
 
118
133
  /**
119
134
  * Static access to available update methods.
@@ -128,15 +143,35 @@ declare module 'pryv' {
128
143
  * Static enum of change names.
129
144
  */
130
145
  static Changes: typeof MonitorChanges;
146
+
147
+ /** Reference to pryv library (set during extension) */
148
+ static pryv: typeof pryv;
131
149
  }
132
150
 
133
151
  /**
134
- * Base interface for update methods used by Monitor.
152
+ * Base class for update methods used by Monitor.
153
+ * Subclass this to create custom update strategies.
135
154
  */
136
155
  export class MonitorUpdateMethod {
156
+ /** The monitor this update method is attached to */
137
157
  protected monitor?: Monitor;
158
+
159
+ /**
160
+ * Assign a Monitor to this updater.
161
+ * Usually called by the monitor itself on monitor.addUpdateMethod()
162
+ * @param monitor - The monitor to attach to
163
+ */
138
164
  setMonitor(monitor: Monitor): void;
165
+
166
+ /**
167
+ * Called when all update tasks are done and monitor is ready for next update.
168
+ * Override in subclasses to implement custom behavior.
169
+ */
139
170
  ready(): Promise<void>;
171
+
172
+ /**
173
+ * Called when monitor is stopped. Override to clean up resources.
174
+ */
140
175
  stop(): Promise<void>;
141
176
  }
142
177
 
@@ -144,13 +179,23 @@ declare module 'pryv' {
144
179
  * Update method that polls for event changes at a fixed interval.
145
180
  */
146
181
  export class EventsTimerUpdateMethod extends MonitorUpdateMethod {
182
+ /**
183
+ * @param updateRateMS - The refresh rate in milliseconds (must be > 1)
184
+ */
147
185
  constructor(updateRateMS: number);
186
+
187
+ /** The configured update rate in milliseconds */
188
+ readonly updateRateMS: number;
148
189
  }
149
190
 
150
191
  /**
151
- * Update method that uses @pryv/socket.io events.
192
+ * Update method that uses @pryv/socket.io events for real-time updates.
193
+ * Requires @pryv/socket.io to be loaded.
152
194
  */
153
- export class SocketUpdateMethod extends MonitorUpdateMethod {}
195
+ export class SocketUpdateMethod extends MonitorUpdateMethod {
196
+ /** The socket instance (set after ready() is called) */
197
+ protected socket?: SocketIO;
198
+ }
154
199
  }
155
200
 
156
201
 
@@ -5,7 +5,7 @@
5
5
  /**
6
6
  * Enum trigger messages
7
7
  * @readonly
8
- * @enum {pryv.Monitor.Changes}
8
+ * @enum {string}
9
9
  */
10
10
  const Changes = {
11
11
  EVENT: 'event',
@@ -7,7 +7,7 @@ const Changes = require('./Changes');
7
7
  module.exports = async function _updateStreams (monitor) {
8
8
  try {
9
9
  const result = await monitor.connection.get('streams');
10
- if (!result.streams) { throw new Error('Invalid response ' + JSON.streams(result)); }
10
+ if (!result.streams) { throw new Error('Invalid response ' + JSON.stringify(result)); }
11
11
  monitor.emit(Changes.STREAMS, result.streams);
12
12
  } catch (e) {
13
13
  monitor.emit(Changes.ERROR, e);
@@ -3,7 +3,6 @@
3
3
  * [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
4
4
  */
5
5
  /* global expect, pryv, testData */
6
- /* eslint-disable no-unused-expressions */
7
6
 
8
7
  require('../src')(pryv);
9
8
  const testStreamId = global.testStreamId = 'monitor-test';
@@ -6,7 +6,7 @@
6
6
 
7
7
  require('./load-helpers');
8
8
 
9
- describe('Monitor', function () {
9
+ describe('[MONX] Monitor', function () {
10
10
  this.timeout(20000);
11
11
 
12
12
  before(async function () {
@@ -14,22 +14,21 @@ describe('Monitor', function () {
14
14
  await prepareAndCreateBaseStreams();
15
15
  });
16
16
 
17
- describe('init', () => {
18
- it('can be initialized with an apiEndpoint', async () => {
17
+ describe('[MINX] init', () => {
18
+ it('[MINA] can be initialized with an apiEndpoint', async () => {
19
19
  const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 });
20
20
  await monitor.start();
21
21
  });
22
22
 
23
- it('can be initialized with a connection', async () => {
23
+ it('[MINB] can be initialized with a connection', async () => {
24
24
  const monitor = new pryv.Monitor(conn, { limit: 1 });
25
25
  await monitor.start();
26
26
  });
27
27
 
28
- it('throw Error on invalid apiEndpoint', async () => {
28
+ it('[MINC] throw Error on invalid apiEndpoint', async () => {
29
29
  let passed = true;
30
30
  try {
31
- /* eslint-disable-next-line no-unused-vars */
32
- const monitor = new pryv.Monitor('BlipBlop', { limit: 1 });
31
+ pryv.Monitor('BlipBlop', { limit: 1 });
33
32
  passed = false;
34
33
  } catch (e) {
35
34
 
@@ -38,7 +37,7 @@ describe('Monitor', function () {
38
37
  });
39
38
  });
40
39
 
41
- describe('notifications', () => {
40
+ describe('[MNTX] notifications', () => {
42
41
  let monitor = null;
43
42
  beforeEach(async () => {
44
43
  monitor = new pryv.Monitor(conn, { limit: 1 });
@@ -48,7 +47,7 @@ describe('Monitor', function () {
48
47
  monitor.stop();
49
48
  });
50
49
 
51
- it('Load events at start', async function () {
50
+ it('[MNTA] Load events at start', async function () {
52
51
  let count = 0;
53
52
  monitor.on('event', function (event) {
54
53
  count++;
@@ -68,7 +67,7 @@ describe('Monitor', function () {
68
67
  expect(count).to.be.gt(0);
69
68
  });
70
69
 
71
- it('Detect new events added', async function () {
70
+ it('[MNTB] Detect new events added', async function () {
72
71
  let count = 0;
73
72
  await monitor.start();
74
73
 
@@ -7,16 +7,16 @@
7
7
  require('./load-helpers');
8
8
  require('@pryv/socket.io')(pryv);
9
9
 
10
- describe('Monitor + Socket.IO', function () {
10
+ describe('[MSKX] Monitor + Socket.IO', function () {
11
11
  this.timeout(20000);
12
12
 
13
13
  before(async () => {
14
14
  await prepareAndCreateBaseStreams();
15
15
  });
16
16
 
17
- describe('socket updates', function () {
17
+ describe('[MSUX] socket updates', function () {
18
18
  this.timeout(25000);
19
- it('Detect new events added', async function () {
19
+ it('[MSUA] Detect new events added', async function () {
20
20
  const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
21
21
  .addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket());
22
22
  await monitor.start();
@@ -47,8 +47,8 @@ describe('Monitor + Socket.IO', function () {
47
47
  });
48
48
  });
49
49
 
50
- describe('stop', () => {
51
- it('Monitor stops when requested', async function () {
50
+ describe('[MSTX] stop', () => {
51
+ it('[MSTA] Monitor stops when requested', async function () {
52
52
  this.timeout(20000);
53
53
  const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
54
54
  .addUpdateMethod(new pryv.Monitor.UpdateMethod.Socket());
@@ -6,15 +6,15 @@
6
6
 
7
7
  require('./load-helpers');
8
8
 
9
- describe('Monitor + EventsTimer', function () {
9
+ describe('[TIMX] Monitor + EventsTimer', function () {
10
10
  this.timeout(20000);
11
11
 
12
12
  before(async () => {
13
13
  await prepareAndCreateBaseStreams();
14
14
  });
15
15
 
16
- describe('init', () => {
17
- it('throw error if timer is not inistialized with correct time', async () => {
16
+ describe('[TINX] init', () => {
17
+ it('[TINA] throw error if timer is not inistialized with correct time', async () => {
18
18
  const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 });
19
19
  try {
20
20
  /* eslint-disable-next-line no-new */
@@ -26,8 +26,8 @@ describe('Monitor + EventsTimer', function () {
26
26
  });
27
27
  });
28
28
 
29
- describe('timer updates', () => {
30
- it('Detect new events added', async function () {
29
+ describe('[TUPX] timer updates', () => {
30
+ it('[TUPA] Detect new events added', async function () {
31
31
  const monitor = new pryv.Monitor(apiEndpoint, { limit: 1 })
32
32
  .addUpdateMethod(new pryv.Monitor.UpdateMethod.EventsTimer(1));
33
33
  await monitor.start();
@@ -58,8 +58,8 @@ describe('Monitor + EventsTimer', function () {
58
58
  });
59
59
  });
60
60
 
61
- describe('stop', () => {
62
- it('Monitor stops when requested', async function () {
61
+ describe('[TSTX] stop', () => {
62
+ it('[TSTA] Monitor stops when requested', async function () {
63
63
  this.timeout(20000);
64
64
  const monitor = await new pryv.Monitor(apiEndpoint, { limit: 1 })
65
65
  .addUpdateMethod(new pryv.Monitor.UpdateMethod.EventsTimer(1));