@data-fair/lib-node 1.0.0 → 1.1.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 +9 -0
- package/events-queue.js +43 -0
- package/package.json +4 -2
- package/test-module-hooks.js +1 -1
- package/test-spies.d.ts +1 -1
- package/test-spies.js +1 -1
- package/mongo-types.d.ts +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Event } from '@data-fair/lib-common-types/event/index.js';
|
|
2
|
+
type EventsQueueOptions = {
|
|
3
|
+
eventsUrl: string;
|
|
4
|
+
eventsSecret: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const start: (options: EventsQueueOptions) => Promise<void>;
|
|
7
|
+
export declare const stop: () => Promise<void>;
|
|
8
|
+
export declare const pushEvent: (event: Omit<Event, "date">) => void;
|
|
9
|
+
export {};
|
package/events-queue.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { internalError } from '@data-fair/lib-node/observer.js'
|
|
2
|
+
import axios from './axios.js'
|
|
3
|
+
let _options
|
|
4
|
+
const options = () => {
|
|
5
|
+
if (!_options) { throw new Error('events queue was not initialized') }
|
|
6
|
+
return _options
|
|
7
|
+
}
|
|
8
|
+
let queue = []
|
|
9
|
+
let stopped = false
|
|
10
|
+
let currentDrainPromise
|
|
11
|
+
const loop = async () => {
|
|
12
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
13
|
+
while (!stopped) {
|
|
14
|
+
await new Promise(resolve => setTimeout(resolve, 3000))
|
|
15
|
+
try {
|
|
16
|
+
currentDrainPromise = drain()
|
|
17
|
+
await currentDrainPromise
|
|
18
|
+
currentDrainPromise = undefined
|
|
19
|
+
} catch (err) {
|
|
20
|
+
internalError('events-queue-drain', err)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
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 } })
|
|
29
|
+
}
|
|
30
|
+
export const start = async (options) => {
|
|
31
|
+
_options = options
|
|
32
|
+
loop()
|
|
33
|
+
}
|
|
34
|
+
export const stop = async () => {
|
|
35
|
+
stopped = true
|
|
36
|
+
if (currentDrainPromise) { await currentDrainPromise }
|
|
37
|
+
await drain()
|
|
38
|
+
}
|
|
39
|
+
export const pushEvent = (event) => {
|
|
40
|
+
if (stopped) { throw new Error('events queue as been stopped') }
|
|
41
|
+
queue.push(event)
|
|
42
|
+
event.date = new Date().toISOString()
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-fair/lib-node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Small nodejs related utility functions for the data-fair stack.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -10,12 +10,14 @@
|
|
|
10
10
|
"author": "",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"scripts": {
|
|
13
|
-
"prepublishOnly": "cd .. && npm run prepublishOnly"
|
|
13
|
+
"prepublishOnly": "cd .. && npm run prepublishOnly",
|
|
14
|
+
"build": "cd .. && npm run build"
|
|
14
15
|
},
|
|
15
16
|
"dependencies": {
|
|
16
17
|
"@data-fair/lib-common-types": "^1.1.0",
|
|
17
18
|
"@data-fair/lib-utils": "1.0.0",
|
|
18
19
|
"agentkeepalive": "^4.5.0",
|
|
20
|
+
"ajv-formats": "^3.0.1",
|
|
19
21
|
"ajv-i18n": "^4.2.0",
|
|
20
22
|
"axios": "^1.7.7",
|
|
21
23
|
"cacheable-lookup": "^7.0.0",
|
package/test-module-hooks.js
CHANGED
|
@@ -7,7 +7,7 @@ export async function load (url, context, nextLoad) {
|
|
|
7
7
|
let source = result.source.toString()
|
|
8
8
|
const matches = source.match(regexp)
|
|
9
9
|
if (matches) {
|
|
10
|
-
source = 'import { emit as __emitTestSpy } from "@data-fair/lib
|
|
10
|
+
source = 'import { emit as __emitTestSpy } from "@data-fair/lib-node/test-spies.js"\n' + source
|
|
11
11
|
source = source.replace(regexp, '__emitTestSpy($1)\n')
|
|
12
12
|
result.source = Buffer.from(source, 'utf-8')
|
|
13
13
|
}
|
package/test-spies.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare function registerModuleHooks(): void;
|
|
2
|
-
export declare function waitFor(eventName: string, timeout?: number): Promise<
|
|
2
|
+
export declare function waitFor<T>(eventName: string, timeout?: number): Promise<T>;
|
|
3
3
|
export declare function emit(eventName: string, data: any): void;
|
package/test-spies.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { register } from 'node:module'
|
|
2
2
|
export function registerModuleHooks () {
|
|
3
|
-
register('@data-fair/lib
|
|
3
|
+
register('@data-fair/lib-node/test-module-hooks.js', { parentURL: import.meta.url })
|
|
4
4
|
}
|
|
5
5
|
const pendingPromises = {}
|
|
6
6
|
let i = 0
|
package/mongo-types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|