@data-fair/lib-node 2.2.2 → 2.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.2.2",
3
+ "version": "2.3.0",
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
@@ -1,4 +1,6 @@
1
1
  import { register } from 'node:module'
2
+ import debugModule from 'debug'
3
+ const debug = debugModule('test-spies')
2
4
  export function registerModuleHooks () {
3
5
  register('@data-fair/lib-node/test-module-hooks.js', { parentURL: import.meta.url })
4
6
  }
@@ -8,6 +10,7 @@ let i = 0
8
10
  export function waitFor (eventName, timeout = 2000) {
9
11
  i++
10
12
  const id = i
13
+ debug(`Waiting for test spy ${eventName} (id=${id}, timeout: ${timeout}ms)`)
11
14
  // better stack traces
12
15
  // see https://github.com/axios/axios/issues/2387#issuecomment-652242713
13
16
  Error.stackTraceLimit = 0
@@ -17,16 +20,21 @@ export function waitFor (eventName, timeout = 2000) {
17
20
  const p = new Promise((resolve, reject) => {
18
21
  pendingPromises[eventName] = pendingPromises[eventName] ?? []
19
22
  pendingPromises[eventName].push({ resolve, id })
20
- setTimeout(() => reject(new Error(`Timeout waiting for test spy ${eventName}`)), timeout)
23
+ setTimeout(() => reject(timeoutError), timeout)
21
24
  })
22
25
  p.finally(() => {
26
+ debug(`Cleaning up test spy ${eventName} (id=${id})`)
23
27
  pendingPromises[eventName] = pendingPromises[eventName].filter(p => p.id !== id)
24
28
  if (pendingPromises[eventName].length === 0) { delete pendingPromises[eventName] }
25
29
  })
26
30
  return p
27
31
  }
28
32
  export function emit (eventName, data) {
29
- if (!pendingPromises[eventName]) { return }
33
+ debug(`Emitting test spy ${eventName} (${pendingPromises[eventName]?.length} listeners)`)
34
+ if (!pendingPromises[eventName]?.length) {
35
+ console.debug(`No listener for test spy ${eventName}`)
36
+ return
37
+ }
30
38
  for (const p of pendingPromises[eventName]) {
31
39
  p.resolve(data)
32
40
  }
@@ -5,4 +5,4 @@ export interface UpgradeScript {
5
5
  description: string;
6
6
  exec: (db: Db, debug: Debugger) => Promise<void>;
7
7
  }
8
- export default function (db: Db, locks: Locks, basePath?: string): Promise<void>;
8
+ export default function (db: Db, locks: Locks, basePath?: string, bootstrap?: () => Promise<undefined | string>): Promise<void>;
@@ -4,7 +4,7 @@ import { readdirSync, readFileSync } from 'fs'
4
4
  import Debug from 'debug'
5
5
  const debug = Debug('upgrade')
6
6
  // chose the proper scripts to execute, then run them
7
- export default async function (db, locks, basePath = './') {
7
+ export default async function (db, locks, basePath = './', bootstrap) {
8
8
  const ack = await locks.acquire('upgrade')
9
9
  if (!ack) {
10
10
  console.warn('upgrade scripts lock is already acquired, skip them')
@@ -15,13 +15,13 @@ export default async function (db, locks, basePath = './') {
15
15
  // are not considered "up" and the previous versions keep running in the mean time
16
16
  } else {
17
17
  try {
18
- await runScripts(db, basePath)
18
+ await runScripts(db, basePath, bootstrap)
19
19
  } finally {
20
20
  await locks.release('upgrade')
21
21
  }
22
22
  }
23
23
  }
24
- async function runScripts (db, basePath) {
24
+ async function runScripts (db, basePath, bootstrap) {
25
25
  const dir = path.resolve(basePath)
26
26
  const pjsonPath = path.join(dir, 'package.json')
27
27
  debug('read service info from ' + pjsonPath)
@@ -30,22 +30,33 @@ async function runScripts (db, basePath) {
30
30
  debug(`service=${pjson.name} version=${pjson.version}`)
31
31
  const services = db.collection('services')
32
32
  const service = await services.findOne({ id: pjson.name })
33
- const version = service?.version
33
+ let previousPersion = service?.version
34
34
  debug('list scripts from ' + scriptsRoot)
35
35
  let scripts = await listScripts(scriptsRoot)
36
- if (!version) {
37
- debug('No service version found in database, this is probably a fresh install')
38
- scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
36
+ if (previousPersion) {
37
+ debug(`Current service version from database : ${previousPersion}`)
39
38
  } else {
40
- debug(`Current service version from database : ${version}`)
41
- scripts = scripts.filter(scriptDef => scriptDef.version !== 'init')
39
+ debug('No service version found in database, this is the first time the upgrade system is run')
40
+ scripts = scripts.filter(scriptDef => scriptDef.version === 'init')
41
+ if (bootstrap) {
42
+ previousPersion = await bootstrap()
43
+ if (previousPersion) {
44
+ debug('Bootstrap function returned version %s', previousPersion)
45
+ } else {
46
+ debug('Bootstrap function returned no version, this is fresh install, do not run any script')
47
+ }
48
+ } else {
49
+ debug('No bootstrap function, it could be a fresh install, do not run any script')
50
+ }
42
51
  }
43
- for (const scriptDef of scripts) {
44
- if (semver.gte(scriptDef.version, version)) {
45
- for (const scriptName of scriptDef.names) {
46
- const script = (await import(path.join(scriptsRoot, scriptDef.version, scriptName))).default
47
- debug('Apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
48
- await script.exec(db, Debug(`upgrade:${scriptDef.version}:${scriptName}`))
52
+ if (previousPersion) {
53
+ for (const scriptDef of scripts) {
54
+ if (semver.gte(scriptDef.version, previousPersion)) {
55
+ for (const scriptName of scriptDef.names) {
56
+ const script = (await import(path.join(scriptsRoot, scriptDef.version, scriptName))).default
57
+ debug('Apply script %s/%s : %s', scriptDef.version, scriptName, script.description)
58
+ await script.exec(db, Debug(`upgrade:${scriptDef.version}:${scriptName}`))
59
+ }
49
60
  }
50
61
  }
51
62
  }