@data-fair/lib-node 2.2.0 → 2.2.2

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.js CHANGED
@@ -28,13 +28,9 @@ export class EventsQueue {
28
28
  async loop () {
29
29
  while (!this.stopped) {
30
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
- }
31
+ this.currentDrainPromise = this.drain()
32
+ await this.currentDrainPromise
33
+ this.currentDrainPromise = undefined
38
34
  }
39
35
  }
40
36
 
@@ -43,14 +39,28 @@ export class EventsQueue {
43
39
  const events = this.eventsQueue
44
40
  this.eventsQueue = []
45
41
  debug('drain events', events.length)
46
- await axios.post(this.options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': this.options().eventsSecret } })
42
+ try {
43
+ await axios.post(this.options().eventsUrl + '/api/events', events, { headers: { 'x-secret-key': this.options().eventsSecret } })
44
+ } catch (err) {
45
+ internalError('events-queue-push-event', err)
46
+ // retry later
47
+ if (err.status >= 500 && events.length < 1000) { this.eventsQueue = events.concat(this.eventsQueue) }
48
+ }
47
49
  }
48
50
  if (this.notificationsQueue.length) {
49
51
  const notifications = this.notificationsQueue
50
52
  this.notificationsQueue = []
51
53
  debug('drain notifications', notifications.length)
52
54
  for (const notification of notifications) {
53
- await axios.post(this.options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': this.options().eventsSecret } })
55
+ try {
56
+ await axios.post(this.options().eventsUrl + '/api/notifications', notification, { headers: { 'x-secret-key': this.options().eventsSecret } })
57
+ } catch (err) {
58
+ internalError('events-queue-push-notif', err)
59
+ // retry later
60
+ if (err.status >= 500 && (notifications.length + this.notificationsQueue.length) < 1000) {
61
+ this.notificationsQueue.unshift()
62
+ }
63
+ }
54
64
  }
55
65
  }
56
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [
package/test-spies.js CHANGED
@@ -2,11 +2,18 @@ import { register } from 'node:module'
2
2
  export function registerModuleHooks () {
3
3
  register('@data-fair/lib-node/test-module-hooks.js', { parentURL: import.meta.url })
4
4
  }
5
+ const { stackTraceLimit } = Error
5
6
  const pendingPromises = {}
6
7
  let i = 0
7
8
  export function waitFor (eventName, timeout = 2000) {
8
9
  i++
9
10
  const id = i
11
+ // better stack traces
12
+ // see https://github.com/axios/axios/issues/2387#issuecomment-652242713
13
+ Error.stackTraceLimit = 0
14
+ const timeoutError = new Error(`Timeout waiting for test spy ${eventName}`)
15
+ Error.stackTraceLimit = stackTraceLimit
16
+ Error.captureStackTrace(timeoutError, waitFor)
10
17
  const p = new Promise((resolve, reject) => {
11
18
  pendingPromises[eventName] = pendingPromises[eventName] ?? []
12
19
  pendingPromises[eventName].push({ resolve, id })