@dotenvx/dotenvx 2.29.0 → 2.30.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/package.json +3 -3
  3. package/src/cli/actions/armor/down.js +1 -1
  4. package/src/cli/actions/armor/move.js +1 -1
  5. package/src/cli/actions/armor/open.js +1 -1
  6. package/src/cli/actions/armor/pull.js +1 -1
  7. package/src/cli/actions/armor/push.js +1 -1
  8. package/src/cli/actions/armor/up.js +1 -1
  9. package/src/cli/actions/curl.js +2 -2
  10. package/src/cli/actions/decrypt.js +4 -10
  11. package/src/cli/actions/define.js +1 -1
  12. package/src/cli/actions/del.js +3 -5
  13. package/src/cli/actions/encrypt.js +4 -10
  14. package/src/cli/actions/ext/genexample.js +1 -1
  15. package/src/cli/actions/ext/prebuild.js +1 -1
  16. package/src/cli/actions/ext/scan.js +2 -3
  17. package/src/cli/actions/get.js +4 -7
  18. package/src/cli/actions/keychain/down.js +1 -1
  19. package/src/cli/actions/keychain/pull.js +1 -1
  20. package/src/cli/actions/keychain/push.js +1 -1
  21. package/src/cli/actions/keychain/up.js +1 -1
  22. package/src/cli/actions/keypair.js +3 -3
  23. package/src/cli/actions/lock/down.js +2 -3
  24. package/src/cli/actions/lock/up.js +2 -3
  25. package/src/cli/actions/login.js +2 -2
  26. package/src/cli/actions/logout.js +1 -1
  27. package/src/cli/actions/ls.js +1 -1
  28. package/src/cli/actions/protect.js +4 -1
  29. package/src/cli/actions/protectDocker.js +2 -2
  30. package/src/cli/actions/protectProcess.js +4 -1
  31. package/src/cli/actions/protectStdin.js +1 -0
  32. package/src/cli/actions/run.js +5 -6
  33. package/src/cli/actions/set.js +5 -9
  34. package/src/cli/actions/settings/device.js +2 -2
  35. package/src/cli/actions/settings/hostname.js +2 -2
  36. package/src/cli/actions/settings/off.js +1 -1
  37. package/src/cli/actions/settings/on.js +1 -1
  38. package/src/cli/actions/settings/path.js +2 -2
  39. package/src/cli/actions/settings/token.js +2 -2
  40. package/src/cli/actions/settings/username.js +2 -2
  41. package/src/cli/actions/validate.js +2 -2
  42. package/src/cli/commandAction.js +16 -0
  43. package/src/cli/commands/armor.js +10 -9
  44. package/src/cli/commands/custody.js +6 -4
  45. package/src/cli/commands/ext.js +4 -3
  46. package/src/cli/commands/lock.js +3 -2
  47. package/src/cli/commands/native.js +5 -4
  48. package/src/cli/commands/settings.js +8 -7
  49. package/src/cli/dotenvx.js +14 -10
  50. package/src/lib/events/backends/armor.js +31 -0
  51. package/src/lib/events/background.js +59 -0
  52. package/src/lib/events/catalog.js +14 -0
  53. package/src/lib/events/cli.js +40 -0
  54. package/src/lib/events/delivery.js +89 -0
  55. package/src/lib/events/index.js +73 -0
  56. package/src/lib/events/metadata.js +25 -0
  57. package/src/lib/events/sdk.js +44 -0
  58. package/src/lib/events/worker.js +29 -0
  59. package/src/lib/events/workerEnvironment.js +14 -0
  60. package/src/lib/helpers/checklist.js +8 -3
  61. package/src/lib/helpers/executeCommand.js +12 -1
  62. package/src/lib/helpers/hasPlaintextSecrets.js +2 -6
  63. package/src/lib/main.d.ts +14 -0
  64. package/src/lib/main.js +11 -3
@@ -61,14 +61,14 @@ async function validate () {
61
61
  if (spinner) spinner.stop()
62
62
 
63
63
  if (errorCount > 0) {
64
- process.exit(1)
64
+ return { exitCode: 1 }
65
65
  } else {
66
66
  logger.success(`▣ valid ${sourceSummary}`)
67
67
  }
68
68
  } catch (error) {
69
69
  if (spinner) spinner.stop()
70
70
  catchAndLog(error)
71
- process.exit(1)
71
+ return { exitCode: 1, error }
72
72
  }
73
73
  }
74
74
 
@@ -0,0 +1,16 @@
1
+ // Command lifecycle only. Returning an outcome does not opt into event tracking.
2
+ module.exports = function commandAction (action) {
3
+ return async function (...args) {
4
+ const result = await action.apply(this, args)
5
+ if (Number.isInteger(result?.exitCode)) {
6
+ // Pipe writes are asynchronous. Flush both streams before forcing exit so
7
+ // successful output and failure diagnostics cannot be silently truncated.
8
+ await Promise.all([process.stdout, process.stderr].map(stream => {
9
+ if (!stream.writable || stream.destroyed) return Promise.resolve()
10
+ return new Promise(resolve => stream.write('', 'utf8', resolve))
11
+ }))
12
+ process.exit(result.exitCode)
13
+ }
14
+ return result
15
+ }
16
+ }
@@ -1,3 +1,4 @@
1
+ const commandAction = require('../commandAction')
1
2
  const executeDynamic = require('./../../lib/helpers/executeDynamic')
2
3
 
3
4
  function configureArmorCommand (armor) {
@@ -25,7 +26,7 @@ function configureArmorCommand (armor) {
25
26
  .option('--token <token>', 'set token')
26
27
  .option('--team <team>', 'team to armor private key for')
27
28
  .action(function (...args) {
28
- return require('./../actions/armor/up').apply(this, args)
29
+ return commandAction(require('./../actions/armor/up')).apply(this, args)
29
30
  })
30
31
 
31
32
  // dotenvx armor down
@@ -36,7 +37,7 @@ function configureArmorCommand (armor) {
36
37
  .option('--token <token>', 'set token')
37
38
  .option('--team <team>', 'team to dearmor private key from')
38
39
  .action(function (...args) {
39
- return require('./../actions/armor/down').apply(this, args)
40
+ return commandAction(require('./../actions/armor/down')).apply(this, args)
40
41
  })
41
42
 
42
43
  // dotenvx armor push
@@ -47,7 +48,7 @@ function configureArmorCommand (armor) {
47
48
  .option('--token <token>', 'set token')
48
49
  .option('--team <team>', 'team to push armored private key for')
49
50
  .action(function (...args) {
50
- return require('./../actions/armor/push').apply(this, args)
51
+ return commandAction(require('./../actions/armor/push')).apply(this, args)
51
52
  })
52
53
 
53
54
  // dotenvx armor pull
@@ -58,7 +59,7 @@ function configureArmorCommand (armor) {
58
59
  .option('--token <token>', 'set token')
59
60
  .option('--team <team>', 'team to pull armored private key from')
60
61
  .action(function (...args) {
61
- return require('./../actions/armor/pull').apply(this, args)
62
+ return commandAction(require('./../actions/armor/pull')).apply(this, args)
62
63
  })
63
64
 
64
65
  // dotenvx armor open
@@ -67,7 +68,7 @@ function configureArmorCommand (armor) {
67
68
  .description('open armored key (in browser)')
68
69
  .option('-f, --env-file <path>', 'path to your env file')
69
70
  .action(function (...args) {
70
- return require('./../actions/armor/open').apply(this, args)
71
+ return commandAction(require('./../actions/armor/open')).apply(this, args)
71
72
  })
72
73
 
73
74
  // dotenvx armor move
@@ -77,7 +78,7 @@ function configureArmorCommand (armor) {
77
78
  .option('-f, --env-file <path>', 'path to your env file')
78
79
  .option('--token <token>', 'set token')
79
80
  .action(function (...args) {
80
- return require('./../actions/armor/move').apply(this, args)
81
+ return commandAction(require('./../actions/armor/move')).apply(this, args)
81
82
  })
82
83
 
83
84
  // dotenvx armor login
@@ -87,7 +88,7 @@ function configureArmorCommand (armor) {
87
88
  .allowUnknownOption()
88
89
  .option('--hostname <hostname>', 'set Armor ⛨ hostname')
89
90
  .action(function (...args) {
90
- return require('./../actions/login').apply(this, args)
91
+ return commandAction(require('./../actions/login')).apply(this, args)
91
92
  })
92
93
 
93
94
  // dotenvx armor logout
@@ -97,7 +98,7 @@ function configureArmorCommand (armor) {
97
98
  .allowUnknownOption()
98
99
  .option('--hostname <hostname>', 'set Armor ⛨ hostname')
99
100
  .action(function (...args) {
100
- return require('./../actions/logout').apply(this, args)
101
+ return commandAction(require('./../actions/logout')).apply(this, args)
101
102
  })
102
103
 
103
104
  // dotenvx armor status
@@ -105,7 +106,7 @@ function configureArmorCommand (armor) {
105
106
  .command('status')
106
107
  .description('print armor status')
107
108
  .action(function (...args) {
108
- return require('./../actions/armor/status').apply(this, args)
109
+ return commandAction(require('./../actions/armor/status')).apply(this, args)
109
110
  })
110
111
 
111
112
  // dotenvx armor settings
@@ -1,3 +1,5 @@
1
+ const commandAction = require('../commandAction')
2
+
1
3
  function configureCustodyCommand (command, name, custodianId) {
2
4
  command.hook('preAction', async () => {
3
5
  const Session = require('../../db/session')
@@ -15,7 +17,7 @@ function configureCustodyCommand (command, name, custodianId) {
15
17
  .description(description)
16
18
  .option('-f, --env-file <path>', 'path to your env file')
17
19
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
18
- .action(async function () {
20
+ .action(commandAction(async function () {
19
21
  const { logger } = require('../../shared/logger')
20
22
  const createSpinner = require('../../lib/helpers/createSpinner')
21
23
  const armoredKeyDisplay = require('../../lib/helpers/armoredKeyDisplay')
@@ -31,11 +33,11 @@ function configureCustodyCommand (command, name, custodianId) {
31
33
  else logger.info(`○ no change (${display})`)
32
34
  } catch (error) {
33
35
  if (spinner) spinner.stop()
34
- if (error.code === 'PROMPT_CANCELLED') return process.exit(130)
36
+ if (error.code === 'PROMPT_CANCELLED') return { exitCode: 130, error }
35
37
  logger.error(error.message)
36
- process.exit(1)
38
+ return { exitCode: 1, error }
37
39
  }
38
- })
40
+ }))
39
41
  }
40
42
  return command
41
43
  }
@@ -1,3 +1,4 @@
1
+ const commandAction = require('../commandAction')
1
2
  const { Command } = require('@dotenvx/tooling')
2
3
 
3
4
  const help = require('./../help')
@@ -26,7 +27,7 @@ ext.command('ls')
26
27
  .option('-ef, --exclude-env-file <excludeFilenames...>', 'path(s) to exclude from your env file(s) (default: none)')
27
28
  .option('--json', 'output a JSON array of absolute filepaths')
28
29
  .action(function (...args) {
29
- return require('./../actions/ls').apply(this, args)
30
+ return commandAction(require('./../actions/ls')).apply(this, args)
30
31
  })
31
32
 
32
33
  // dotenvx ext genexample
@@ -35,7 +36,7 @@ ext.command('genexample')
35
36
  .argument('[directory]', 'directory to generate from', '.')
36
37
  .option('-f, --env-file <paths...>', 'path(s) to your env file(s)', '.env')
37
38
  .action(function (...args) {
38
- return require('./../actions/ext/genexample').apply(this, args)
39
+ return commandAction(require('./../actions/ext/genexample')).apply(this, args)
39
40
  })
40
41
 
41
42
  // dotenvx ext gitignore
@@ -71,7 +72,7 @@ ext.command('precommit')
71
72
  ext.command('scan')
72
73
  .description('scan for leaked secrets')
73
74
  .action(function (...args) {
74
- return require('./../actions/ext/scan').apply(this, args)
75
+ return commandAction(require('./../actions/ext/scan')).apply(this, args)
75
76
  })
76
77
 
77
78
  // override helpInformation to hide dynamic commands
@@ -1,3 +1,4 @@
1
+ const commandAction = require('../commandAction')
1
2
  function configureLockCommand (lock) {
2
3
  lock.hook('preAction', async () => {
3
4
  const Session = require('./../../db/session')
@@ -17,7 +18,7 @@ function configureLockCommand (lock) {
17
18
  .option('-f, --env-file <path>', 'path to your env file')
18
19
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
19
20
  .action(function (...args) {
20
- return require('./../actions/lock/up').apply(this, args)
21
+ return commandAction(require('./../actions/lock/up')).apply(this, args)
21
22
  })
22
23
 
23
24
  lock
@@ -26,7 +27,7 @@ function configureLockCommand (lock) {
26
27
  .option('-f, --env-file <path>', 'path to your env file')
27
28
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
28
29
  .action(function (...args) {
29
- return require('./../actions/lock/down').apply(this, args)
30
+ return commandAction(require('./../actions/lock/down')).apply(this, args)
30
31
  })
31
32
 
32
33
  return lock
@@ -1,3 +1,4 @@
1
+ const commandAction = require('../commandAction')
1
2
  function configureNativeCommand (native) {
2
3
  native.hook('preAction', async () => {
3
4
  const Session = require('./../../db/session')
@@ -17,7 +18,7 @@ function configureNativeCommand (native) {
17
18
  .option('-f, --env-file <path>', 'path to your env file')
18
19
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
19
20
  .action(function (...args) {
20
- return require('./../actions/keychain/up').apply(this, args)
21
+ return commandAction(require('./../actions/keychain/up')).apply(this, args)
21
22
  })
22
23
 
23
24
  native
@@ -26,7 +27,7 @@ function configureNativeCommand (native) {
26
27
  .option('-f, --env-file <path>', 'path to your env file')
27
28
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
28
29
  .action(function (...args) {
29
- return require('./../actions/keychain/down').apply(this, args)
30
+ return commandAction(require('./../actions/keychain/down')).apply(this, args)
30
31
  })
31
32
 
32
33
  native
@@ -35,7 +36,7 @@ function configureNativeCommand (native) {
35
36
  .option('-f, --env-file <path>', 'path to your env file')
36
37
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
37
38
  .action(function (...args) {
38
- return require('./../actions/keychain/push').apply(this, args)
39
+ return commandAction(require('./../actions/keychain/push')).apply(this, args)
39
40
  })
40
41
 
41
42
  native
@@ -44,7 +45,7 @@ function configureNativeCommand (native) {
44
45
  .option('-f, --env-file <path>', 'path to your env file')
45
46
  .option('-fk, --env-keys-file <path>', 'path to your .env.keys file', '.env.keys')
46
47
  .action(function (...args) {
47
- return require('./../actions/keychain/pull').apply(this, args)
48
+ return commandAction(require('./../actions/keychain/pull')).apply(this, args)
48
49
  })
49
50
 
50
51
  return native
@@ -1,3 +1,4 @@
1
+ const commandAction = require('../commandAction')
1
2
  function configureSettingsCommand (settings) {
2
3
  settings
3
4
  .description('settings')
@@ -9,7 +10,7 @@ function configureSettingsCommand (settings) {
9
10
  .command('username')
10
11
  .description('print your username')
11
12
  .action(function (...args) {
12
- return require('./../actions/settings/username').apply(this, args)
13
+ return commandAction(require('./../actions/settings/username')).apply(this, args)
13
14
  })
14
15
 
15
16
  settings
@@ -17,7 +18,7 @@ function configureSettingsCommand (settings) {
17
18
  .description('print your access token (--unmask)')
18
19
  .option('--unmask', 'unmask access token')
19
20
  .action(function (...args) {
20
- return require('./../actions/settings/token').apply(this, args)
21
+ return commandAction(require('./../actions/settings/token')).apply(this, args)
21
22
  })
22
23
 
23
24
  settings
@@ -25,35 +26,35 @@ function configureSettingsCommand (settings) {
25
26
  .description('print your device pubkey (--unmask)')
26
27
  .option('--unmask', 'unmask device pubkey')
27
28
  .action(function (...args) {
28
- return require('./../actions/settings/device').apply(this, args)
29
+ return commandAction(require('./../actions/settings/device')).apply(this, args)
29
30
  })
30
31
 
31
32
  settings
32
33
  .command('hostname')
33
34
  .description('print hostname')
34
35
  .action(function (...args) {
35
- return require('./../actions/settings/hostname').apply(this, args)
36
+ return commandAction(require('./../actions/settings/hostname')).apply(this, args)
36
37
  })
37
38
 
38
39
  settings
39
40
  .command('path')
40
41
  .description('print path to settings file')
41
42
  .action(function (...args) {
42
- return require('./../actions/settings/path').apply(this, args)
43
+ return commandAction(require('./../actions/settings/path')).apply(this, args)
43
44
  })
44
45
 
45
46
  settings
46
47
  .command('on')
47
48
  .description('turn armor on')
48
49
  .action(function (...args) {
49
- return require('./../actions/settings/on').apply(this, args)
50
+ return commandAction(require('./../actions/settings/on')).apply(this, args)
50
51
  })
51
52
 
52
53
  settings
53
54
  .command('off')
54
55
  .description('turn armor off')
55
56
  .action(function (...args) {
56
- return require('./../actions/settings/off').apply(this, args)
57
+ return commandAction(require('./../actions/settings/off')).apply(this, args)
57
58
  })
58
59
 
59
60
  return settings
@@ -3,6 +3,7 @@
3
3
  /* c8 ignore start */
4
4
  const { Command } = require('@dotenvx/tooling')
5
5
  const program = new Command()
6
+ const commandAction = require('./commandAction')
6
7
 
7
8
  const { setLogLevel } = require('../shared/logger')
8
9
  const help = require('./help')
@@ -220,7 +221,7 @@ program.command('ls', { hidden: true })
220
221
  .option('-ef, --exclude-env-file <excludeFilenames...>', 'path(s) to exclude from your env file(s) (default: none)')
221
222
  .option('--json', 'output a JSON array of absolute filepaths')
222
223
  .action(function (...args) {
223
- return require('./actions/ls').apply(this, args)
224
+ return commandAction(require('./actions/ls')).apply(this, args)
224
225
  })
225
226
 
226
227
  // dotenvx init
@@ -228,7 +229,7 @@ program.command('init', { hidden: true })
228
229
  .description('create an Envfile from .env.example and .env')
229
230
  .option('-f, --env-file <path>', 'file to read variable names from')
230
231
  .action(function () {
231
- return require('./actions/init').apply(this, arguments)
232
+ return commandAction(require('./actions/init')).apply(this, arguments)
232
233
  })
233
234
 
234
235
  // dotenvx define
@@ -236,7 +237,7 @@ program.command('define', { hidden: true })
236
237
  .description('define your environment in an Envfile')
237
238
  .option('-f, --env-file <path>', 'file to read variable names from')
238
239
  .action(function () {
239
- return require('./actions/define').apply(this, arguments)
240
+ return commandAction(require('./actions/define')).apply(this, arguments)
240
241
  })
241
242
 
242
243
  // dotenvx validate
@@ -256,7 +257,7 @@ program.command('validate', { hidden: true })
256
257
  .option('--no-bitwarden', 'disable Bitwarden secret reference resolution')
257
258
  .action(function (...args) {
258
259
  this.envs = envs
259
- return require('./actions/validate').apply(this, args)
260
+ return commandAction(require('./actions/validate')).apply(this, args)
260
261
  })
261
262
 
262
263
  // dotenvx gitignore
@@ -274,7 +275,7 @@ program.command('genexample', { hidden: true })
274
275
  .argument('[directory]', 'directory to generate from', '.')
275
276
  .option('-f, --env-file <paths...>', 'path(s) to your env file(s)', '.env')
276
277
  .action(function (...args) {
277
- return require('./actions/ext/genexample').apply(this, args)
278
+ return commandAction(require('./actions/ext/genexample')).apply(this, args)
278
279
  })
279
280
 
280
281
  // dotenvx precommit
@@ -313,7 +314,7 @@ program.command('doctor', { hidden: true })
313
314
  .description('scan for dotenv loaders')
314
315
  .argument('[directory]', 'directory to scan', '.')
315
316
  .action(function (...args) {
316
- return require('./actions/doctor').apply(this, args)
317
+ return commandAction(require('./actions/doctor')).apply(this, args)
317
318
  })
318
319
 
319
320
  // dotenvx hidden (a menu of top-level commands)
@@ -327,7 +328,7 @@ program.command('hidden')
327
328
  program.command('update')
328
329
  .description('update dotenvx')
329
330
  .action(function (...args) {
330
- return require('./actions/update').apply(this, args)
331
+ return commandAction(require('./actions/update')).apply(this, args)
331
332
  })
332
333
 
333
334
  // dotenvx login (compatibility alias for dotenvx armor login)
@@ -336,7 +337,7 @@ program.command('login', { hidden: true })
336
337
  .allowUnknownOption()
337
338
  .option('--hostname <hostname>', 'set Armor ⛨ hostname')
338
339
  .action(function (...args) {
339
- return require('./actions/login').apply(this, args)
340
+ return commandAction(require('./actions/login')).apply(this, args)
340
341
  })
341
342
 
342
343
  // dotenvx logout (compatibility alias for dotenvx armor logout)
@@ -345,7 +346,7 @@ program.command('logout', { hidden: true })
345
346
  .allowUnknownOption()
346
347
  .option('--hostname <hostname>', 'set Armor ⛨ hostname')
347
348
  .action(function (...args) {
348
- return require('./actions/logout').apply(this, args)
349
+ return commandAction(require('./actions/logout')).apply(this, args)
349
350
  })
350
351
 
351
352
  // dotenvx curl
@@ -362,7 +363,7 @@ program.command('curl', { hidden: true })
362
363
  return
363
364
  }
364
365
 
365
- return require('./actions/curl').apply(this, args)
366
+ return commandAction(require('./actions/curl')).apply(this, args)
366
367
  })
367
368
 
368
369
  // dotenvx help
@@ -415,6 +416,9 @@ program.helpInformation = function () {
415
416
  }
416
417
  /* c8 ignore stop */
417
418
 
419
+ program.command('_deliver-events', { hidden: true })
420
+ .action(() => require('../lib/events/worker')())
421
+
418
422
  require('./commands/fileOptions')(program)
419
423
 
420
424
  program.parse(process.argv)
@@ -0,0 +1,31 @@
1
+ const Session = require('../../../db/session')
2
+ const { http } = require('../../helpers/http')
3
+
4
+ // Radar ingests client-reported activity at POST /api/events.
5
+ // Authentication is captured before `run` loads application environment values.
6
+ module.exports = function armor (options = {}) {
7
+ if (process.env.DOTENVX_NO_ARMOR === 'true' || options.armor === false || options.noArmor === true) return
8
+ const session = new Session()
9
+ if (!options.token && (!session.on() || !session.username())) return
10
+ const token = options.token || session.token()
11
+ if (!token) return
12
+ const hostname = session.hostname()
13
+
14
+ return {
15
+ id: 'armor',
16
+ async send (events, { signal }) {
17
+ const response = await http(`${hostname}/api/events`, {
18
+ method: 'POST',
19
+ signal,
20
+ headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
21
+ body: JSON.stringify({ events })
22
+ })
23
+ // No response data belongs in console output (including errors).
24
+ // Undici can emit an asynchronous AbortError when its body is destroyed,
25
+ // even after the request promise resolves. Handle it before cleanup.
26
+ response.body.on('error', () => {})
27
+ response.body.destroy()
28
+ if (response.statusCode >= 400) throw new Error('Event delivery failed')
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,59 @@
1
+ const { spawn } = require('child_process')
2
+ const Session = require('../../db/session')
3
+ const workerEnvironment = require('./workerEnvironment')
4
+
5
+ // CLI delivery owns no network handles. Credentials are read by the worker.
6
+ module.exports = function createBackground (options = {}) {
7
+ if (process.env.DOTENVX_NO_ARMOR === 'true' || options.armor === false || options.noArmor === true) return
8
+ const session = new Session()
9
+ if (!options.token && (!session.on() || !session.username())) return
10
+ // Capture before run loads application environment values.
11
+ const env = workerEnvironment()
12
+ // pkg otherwise treats the first child argument as a JavaScript entrypoint.
13
+ // Relaunch the embedded CLI so _deliver-events remains a command.
14
+ if (process.pkg) env.PKG_EXECPATH = ''
15
+ const cwd = process.cwd()
16
+ const args = process.pkg ? [] : [process.argv[1]]
17
+ const token = options.token
18
+ let queue = []
19
+ let closing
20
+
21
+ function flush () {
22
+ if (!queue.length) return Promise.resolve()
23
+ const events = queue
24
+ queue = []
25
+ return new Promise(resolve => {
26
+ let child
27
+ let timer
28
+ const done = () => { clearTimeout(timer); resolve() }
29
+ try {
30
+ child = spawn(process.execPath, [...args, '_deliver-events'], {
31
+ cwd, env, detached: true, windowsHide: true, stdio: ['pipe', 'ignore', 'ignore']
32
+ })
33
+ child.on('error', done)
34
+ child.stdin.on('error', done)
35
+ child.unref()
36
+ child.stdin.unref()
37
+ // Only wait for the local pipe write, never worker startup or HTTP.
38
+ timer = setTimeout(() => { child.stdin.destroy(); done() }, 25)
39
+ child.stdin.end(JSON.stringify({ options: { token }, events }), done)
40
+ } catch { done() }
41
+ })
42
+ }
43
+
44
+ return {
45
+ write (event, terminal = false) {
46
+ if (closing) return
47
+ if (queue.length >= 256) {
48
+ if (terminal) queue.pop()
49
+ else return
50
+ }
51
+ queue.push(event)
52
+ },
53
+ flush,
54
+ close () {
55
+ if (!closing) closing = flush()
56
+ return closing
57
+ }
58
+ }
59
+ }
@@ -0,0 +1,14 @@
1
+ // Register public operations here. Internal helpers keep the caller's recorder.
2
+ module.exports = {
3
+ 'cli/run': {},
4
+ 'cli/get': { keyArgument: true },
5
+ 'cli/set': { keyArgument: true },
6
+ 'cli/del': { keyArgument: true },
7
+ 'cli/encrypt': {},
8
+ 'cli/decrypt': {},
9
+ 'cli/keypair': { keyArgument: true },
10
+ 'cli/protect': {},
11
+ 'sdk/config': {},
12
+ 'sdk/get': { keyArgument: true },
13
+ 'sdk/set': { keyArgument: true }
14
+ }
@@ -0,0 +1,40 @@
1
+ const commandAction = require('../../cli/commandAction')
2
+ const createEvents = require('./index')
3
+ const catalog = require('./catalog')
4
+
5
+ module.exports = function trackCli (command, action) {
6
+ return commandAction(async function (...args) {
7
+ const options = typeof this.optsWithGlobals === 'function' ? this.optsWithGlobals() : this.opts()
8
+ const context = this
9
+ const name = `cli/${command}`
10
+ const eventOptions = {}
11
+ for (const [key, value] of Object.entries(options)) {
12
+ const source = typeof this.getOptionValueSourceWithGlobals === 'function'
13
+ ? this.getOptionValueSourceWithGlobals(key)
14
+ : this.getOptionValueSource?.(key)
15
+ if (source === 'cli' || source === 'env') eventOptions[key] = value
16
+ }
17
+ const events = createEvents(name, options, { eventOptions, background: true })
18
+ if (catalog[name]?.keyArgument) events.add({ key: args[0] })
19
+ let code
20
+ let result
21
+ try {
22
+ result = await action.apply(context, args)
23
+ if (Number.isInteger(result?.exitCode)) code = result.exitCode
24
+ if (result?.error) {
25
+ events.fail(result.error)
26
+ code = code ?? 1
27
+ }
28
+ if (Number.isInteger(result?.errorCount)) events.add({ error_count: result.errorCount })
29
+ if (result?.signal) events.add({ signal: result.signal })
30
+ return result
31
+ } catch (error) {
32
+ code = 1
33
+ events.fail(error)
34
+ throw error
35
+ } finally {
36
+ const exitCode = code ?? process.exitCode ?? 0
37
+ await events.complete(exitCode === 0 && !(result?.errorCount > 0) ? 'success' : 'unsuccessful', { exit_code: exitCode })
38
+ }
39
+ })
40
+ }
@@ -0,0 +1,89 @@
1
+ const BATCH_SIZE = 50
2
+ const QUEUE_LIMIT = 256
3
+
4
+ // Transport only. Backend failures cannot escape this boundary.
5
+ module.exports = function createDelivery (backend, timeoutMs = 500) {
6
+ const timeout = Number.isFinite(timeoutMs) && timeoutMs > 0 ? Math.min(timeoutMs, 500) : 500
7
+ const queue = []
8
+ let running
9
+ let controller
10
+ let closing
11
+ let sendBatch
12
+ try {
13
+ if (backend && typeof backend.send === 'function') sendBatch = backend.send.bind(backend)
14
+ } catch {} // Treat an invalid backend as disabled, including throwing getters.
15
+ let closed = !sendBatch
16
+
17
+ function stop () {
18
+ closed = true
19
+ queue.length = 0
20
+ if (controller) controller.abort()
21
+ }
22
+
23
+ async function send (batch) {
24
+ let timer
25
+ controller = new AbortController()
26
+ try {
27
+ await Promise.race([
28
+ Promise.resolve().then(() => sendBatch(batch, { signal: controller.signal })),
29
+ new Promise(resolve => { timer = setTimeout(() => { stop(); resolve() }, timeout) })
30
+ ])
31
+ } catch {
32
+ stop()
33
+ } finally {
34
+ clearTimeout(timer)
35
+ }
36
+ }
37
+
38
+ function pump () {
39
+ if (closed || running || !queue.length) return
40
+ running = (async () => {
41
+ while (queue.length) {
42
+ if (closed) break
43
+ await send(queue.splice(0, BATCH_SIZE))
44
+ }
45
+ })().finally(() => {
46
+ running = undefined
47
+ pump()
48
+ })
49
+ }
50
+
51
+ async function flush () {
52
+ let timer
53
+ try {
54
+ // Overall flush deadline is separate from each batch's deadline.
55
+ await Promise.race([
56
+ (async () => {
57
+ pump()
58
+ let current = running
59
+ while (current) {
60
+ await current
61
+ current = running
62
+ }
63
+ })(),
64
+ new Promise(resolve => { timer = setTimeout(() => { stop(); resolve() }, timeout) })
65
+ ])
66
+ } catch {
67
+ stop()
68
+ } finally {
69
+ clearTimeout(timer)
70
+ }
71
+ }
72
+
73
+ return {
74
+ write (event, terminal = false) {
75
+ if (closed || closing) return
76
+ if (queue.length >= QUEUE_LIMIT) {
77
+ if (!terminal) return
78
+ queue.pop() // Reserve room for the final result.
79
+ }
80
+ queue.push(event)
81
+ pump()
82
+ },
83
+ flush,
84
+ close () {
85
+ if (!closing) closing = flush().finally(stop)
86
+ return closing
87
+ }
88
+ }
89
+ }