@data-fair/lib-node 2.1.4 → 2.2.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/events-queue.d.ts CHANGED
@@ -4,8 +4,19 @@ type EventsQueueOptions = {
4
4
  eventsUrl: string;
5
5
  eventsSecret: string;
6
6
  };
7
- export declare const start: (options: EventsQueueOptions) => Promise<void>;
8
- export declare const stop: () => Promise<void>;
9
- export declare const pushEvent: (event: Omit<Event, "date">) => void;
10
- export declare const pushNotification: (notification: Omit<Notification, "date">) => void;
11
- export {};
7
+ export declare class EventsQueue {
8
+ _options: EventsQueueOptions | undefined;
9
+ eventsQueue: Event[];
10
+ notificationsQueue: Notification[];
11
+ stopped: boolean;
12
+ currentDrainPromise: Promise<void> | undefined;
13
+ options(): EventsQueueOptions;
14
+ start: (options: EventsQueueOptions) => Promise<void>;
15
+ stop: () => Promise<void>;
16
+ loop(): Promise<void>;
17
+ drain(): Promise<void>;
18
+ pushEvent(event: Omit<Event, 'date'>): void;
19
+ pushNotification(notification: Omit<Notification, 'date'>): void;
20
+ }
21
+ declare const eventsQueue: EventsQueue;
22
+ export default eventsQueue;
package/events-queue.js CHANGED
@@ -2,64 +2,74 @@ import { internalError } from '@data-fair/lib-node/observer.js'
2
2
  import axios from './axios.js'
3
3
  import Debug from 'debug'
4
4
  const debug = Debug('events-queue')
5
- let _options
6
- const options = () => {
7
- if (!_options) { throw new Error('events queue was not initialized') }
8
- return _options
9
- }
10
- let eventsQueue = []
11
- let notificationsQueue = []
12
- let stopped = false
13
- let currentDrainPromise
14
- const loop = async () => {
15
- // eslint-disable-next-line no-unmodified-loop-condition
16
- while (!stopped) {
17
- await new Promise(resolve => setTimeout(resolve, 3000))
18
- try {
19
- currentDrainPromise = drain()
20
- await currentDrainPromise
21
- currentDrainPromise = undefined
22
- } catch (err) {
23
- internalError('events-queue-drain', err)
24
- }
5
+ export class EventsQueue {
6
+ _options
7
+ eventsQueue = []
8
+ notificationsQueue = []
9
+ stopped = false
10
+ currentDrainPromise
11
+ options () {
12
+ if (!this._options) { throw new Error('events queue was not initialized') }
13
+ return this._options
25
14
  }
26
- }
27
- const drain = async () => {
28
- if (eventsQueue.length) {
29
- const events = eventsQueue
30
- eventsQueue = []
31
- debug('drain events', events.length)
32
- await axios.post(options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': options().eventsSecret } })
15
+
16
+ start = async (options) => {
17
+ if (this._options) { throw new Error('events queue was already initialized') }
18
+ this._options = options
19
+ this.loop()
20
+ }
21
+
22
+ stop = async () => {
23
+ this.stopped = true
24
+ if (this.currentDrainPromise) { await this.currentDrainPromise }
25
+ await this.drain()
33
26
  }
34
- if (notificationsQueue.length) {
35
- const notifications = notificationsQueue
36
- notificationsQueue = []
37
- debug('drain notifications', notifications.length)
38
- for (const notification of notifications) {
39
- await axios.post(options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': options().eventsSecret } })
27
+
28
+ async loop () {
29
+ while (!this.stopped) {
30
+ await new Promise(resolve => setTimeout(resolve, 3000))
31
+ try {
32
+ this.currentDrainPromise = this.drain()
33
+ await this.currentDrainPromise
34
+ this.currentDrainPromise = undefined
35
+ } catch (err) {
36
+ internalError('events-queue-drain', err)
37
+ }
40
38
  }
41
39
  }
40
+
41
+ async drain () {
42
+ if (this.eventsQueue.length) {
43
+ const events = this.eventsQueue
44
+ this.eventsQueue = []
45
+ debug('drain events', events.length)
46
+ await axios.post(this.options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': this.options().eventsSecret } })
47
+ }
48
+ if (this.notificationsQueue.length) {
49
+ const notifications = this.notificationsQueue
50
+ this.notificationsQueue = []
51
+ debug('drain notifications', notifications.length)
52
+ for (const notification of notifications) {
53
+ await axios.post(this.options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': this.options().eventsSecret } })
54
+ }
55
+ }
56
+ }
57
+
58
+ pushEvent (event) {
59
+ this.options()
60
+ if (this.stopped) { throw new Error('events queue has been stopped') }
61
+ event.date = new Date().toISOString()
62
+ debug('pushEvent', event)
63
+ this.eventsQueue.push(event)
64
+ }
65
+
66
+ pushNotification (notification) {
67
+ this.options()
68
+ if (this.stopped) { throw new Error('notifications queue has been stopped') }
69
+ notification.date = new Date().toISOString()
70
+ debug('pushNotification', notification)
71
+ this.notificationsQueue.push(notification)
72
+ }
42
73
  }
43
- export const start = async (options) => {
44
- _options = options
45
- loop()
46
- }
47
- export const stop = async () => {
48
- stopped = true
49
- if (currentDrainPromise) { await currentDrainPromise }
50
- await drain()
51
- }
52
- export const pushEvent = (event) => {
53
- options()
54
- if (stopped) { throw new Error('events queue has been stopped') }
55
- event.date = new Date().toISOString()
56
- debug('pushEvent', event)
57
- eventsQueue.push(event)
58
- }
59
- export const pushNotification = (notification) => {
60
- options()
61
- if (stopped) { throw new Error('notifications queue has been stopped') }
62
- notification.date = new Date().toISOString()
63
- debug('pushNotification', notification)
64
- notificationsQueue.push(notification)
65
- }
74
+ const eventsQueue = new EventsQueue()
75
+ export default eventsQueue
package/locks.js CHANGED
@@ -21,6 +21,7 @@ export class Locks {
21
21
  }
22
22
 
23
23
  start = async (db, ttl = 60) => {
24
+ if (this._db) { throw new Error('locks utils was already initialized') }
24
25
  this._db = db
25
26
  await this.collection.createIndex({ pid: 1 })
26
27
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.1.4",
3
+ "version": "2.2.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [