@data-fair/lib-node 1.3.1 → 1.4.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
@@ -1,4 +1,5 @@
1
1
  import { type Event } from '@data-fair/lib-common-types/event/index.js';
2
+ import { type Notification } from '@data-fair/lib-common-types/notification/index.js';
2
3
  type EventsQueueOptions = {
3
4
  eventsUrl: string;
4
5
  eventsSecret: string;
@@ -6,4 +7,5 @@ type EventsQueueOptions = {
6
7
  export declare const start: (options: EventsQueueOptions) => Promise<void>;
7
8
  export declare const stop: () => Promise<void>;
8
9
  export declare const pushEvent: (event: Omit<Event, "date">) => void;
10
+ export declare const pushNotification: (notification: Omit<Notification, "date">) => void;
9
11
  export {};
package/events-queue.js CHANGED
@@ -5,7 +5,8 @@ const options = () => {
5
5
  if (!_options) { throw new Error('events queue was not initialized') }
6
6
  return _options
7
7
  }
8
- let queue = []
8
+ let eventsQueue = []
9
+ let notificationsQueue = []
9
10
  let stopped = false
10
11
  let currentDrainPromise
11
12
  const loop = async () => {
@@ -22,10 +23,18 @@ const loop = async () => {
22
23
  }
23
24
  }
24
25
  const drain = async () => {
25
- if (queue.length === 0) { return }
26
- const events = queue
27
- queue = []
28
- await axios.post(options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': options().eventsSecret } })
26
+ if (eventsQueue.length) {
27
+ const events = eventsQueue
28
+ eventsQueue = []
29
+ await axios.post(options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': options().eventsSecret } })
30
+ }
31
+ if (notificationsQueue.length) {
32
+ const notifications = notificationsQueue
33
+ notificationsQueue = []
34
+ for (const notification of notifications) {
35
+ await axios.post(options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': options().eventsSecret } })
36
+ }
37
+ }
29
38
  }
30
39
  export const start = async (options) => {
31
40
  _options = options
@@ -37,7 +46,12 @@ export const stop = async () => {
37
46
  await drain()
38
47
  }
39
48
  export const pushEvent = (event) => {
40
- if (stopped) { throw new Error('events queue as been stopped') }
41
- queue.push(event)
49
+ if (stopped) { throw new Error('events queue has been stopped') }
42
50
  event.date = new Date().toISOString()
51
+ eventsQueue.push(event)
52
+ }
53
+ export const pushNotification = (notification) => {
54
+ if (stopped) { throw new Error('notifications queue has been stopped') }
55
+ notification.date = new Date().toISOString()
56
+ notificationsQueue.push(notification)
43
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -14,7 +14,7 @@
14
14
  "build": "cd .. && npm run build"
15
15
  },
16
16
  "dependencies": {
17
- "@data-fair/lib-common-types": "^1.1.0",
17
+ "@data-fair/lib-common-types": "^1.5.0",
18
18
  "@data-fair/lib-utils": "^1.1.0",
19
19
  "agentkeepalive": "^4.5.0",
20
20
  "ajv-formats": "^3.0.1",