dd-trace 5.96.0 → 5.97.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 (114) hide show
  1. package/index.d.ts +34 -0
  2. package/package.json +9 -7
  3. package/packages/datadog-esbuild/index.js +20 -9
  4. package/packages/datadog-instrumentations/src/child_process.js +7 -17
  5. package/packages/datadog-instrumentations/src/crypto.js +1 -2
  6. package/packages/datadog-instrumentations/src/cucumber.js +4 -1
  7. package/packages/datadog-instrumentations/src/cypress-config.js +324 -0
  8. package/packages/datadog-instrumentations/src/cypress.js +86 -4
  9. package/packages/datadog-instrumentations/src/dns.js +1 -2
  10. package/packages/datadog-instrumentations/src/express.js +4 -4
  11. package/packages/datadog-instrumentations/src/fs.js +27 -29
  12. package/packages/datadog-instrumentations/src/graphql.js +1 -1
  13. package/packages/datadog-instrumentations/src/helpers/bundler-register.js +41 -13
  14. package/packages/datadog-instrumentations/src/helpers/hook.js +31 -6
  15. package/packages/datadog-instrumentations/src/helpers/hooks.js +12 -19
  16. package/packages/datadog-instrumentations/src/helpers/instrument.js +27 -13
  17. package/packages/datadog-instrumentations/src/helpers/register.js +103 -142
  18. package/packages/datadog-instrumentations/src/http/client.js +2 -3
  19. package/packages/datadog-instrumentations/src/http/server.js +2 -5
  20. package/packages/datadog-instrumentations/src/http2/client.js +1 -3
  21. package/packages/datadog-instrumentations/src/http2/server.js +1 -3
  22. package/packages/datadog-instrumentations/src/jest.js +13 -4
  23. package/packages/datadog-instrumentations/src/limitd-client.js +1 -1
  24. package/packages/datadog-instrumentations/src/mocha/utils.js +4 -1
  25. package/packages/datadog-instrumentations/src/net.js +2 -8
  26. package/packages/datadog-instrumentations/src/pino.js +1 -1
  27. package/packages/datadog-instrumentations/src/playwright.js +4 -1
  28. package/packages/datadog-instrumentations/src/prisma.js +1 -2
  29. package/packages/datadog-instrumentations/src/selenium.js +4 -1
  30. package/packages/datadog-instrumentations/src/sequelize.js +1 -1
  31. package/packages/datadog-instrumentations/src/url.js +1 -3
  32. package/packages/datadog-instrumentations/src/vitest.js +5 -1
  33. package/packages/datadog-instrumentations/src/vm.js +1 -3
  34. package/packages/datadog-plugin-aws-sdk/src/base.js +4 -3
  35. package/packages/datadog-plugin-cucumber/src/index.js +7 -3
  36. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +57 -5
  37. package/packages/datadog-plugin-graphql/src/resolve.js +1 -1
  38. package/packages/datadog-plugin-jest/src/index.js +4 -2
  39. package/packages/datadog-plugin-kafkajs/src/batch-consumer.js +31 -4
  40. package/packages/datadog-plugin-mocha/src/index.js +5 -2
  41. package/packages/datadog-plugin-next/src/index.js +2 -14
  42. package/packages/datadog-plugin-openai/src/services.js +1 -0
  43. package/packages/datadog-webpack/index.js +3 -3
  44. package/packages/dd-trace/index.js +12 -10
  45. package/packages/dd-trace/src/agent/url.js +2 -2
  46. package/packages/dd-trace/src/aiguard/sdk.js +4 -0
  47. package/packages/dd-trace/src/appsec/blocking.js +3 -0
  48. package/packages/dd-trace/src/appsec/iast/iast-plugin.js +1 -1
  49. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/evidence-redaction/sensitive-handler.js +1 -1
  50. package/packages/dd-trace/src/appsec/iast/vulnerabilities-formatter/utils.js +1 -1
  51. package/packages/dd-trace/src/appsec/remote_config.js +1 -0
  52. package/packages/dd-trace/src/appsec/sdk/index.js +4 -0
  53. package/packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js +6 -1
  54. package/packages/dd-trace/src/ci-visibility/test-api-manual/test-api-manual-plugin.js +4 -0
  55. package/packages/dd-trace/src/config/defaults.js +316 -146
  56. package/packages/dd-trace/src/config/generated-config-types.d.ts +4 -1
  57. package/packages/dd-trace/src/config/helper.js +59 -10
  58. package/packages/dd-trace/src/config/index.js +569 -1505
  59. package/packages/dd-trace/src/config/parsers.js +256 -0
  60. package/packages/dd-trace/src/config/remote_config.js +59 -2
  61. package/packages/dd-trace/src/config/supported-configurations.json +350 -433
  62. package/packages/dd-trace/src/crashtracking/crashtracker.js +7 -1
  63. package/packages/dd-trace/src/crashtracking/index.js +1 -7
  64. package/packages/dd-trace/src/debugger/index.js +1 -1
  65. package/packages/dd-trace/src/dogstatsd.js +12 -9
  66. package/packages/dd-trace/src/encode/0.4.js +1 -1
  67. package/packages/dd-trace/src/exporters/agent/writer.js +7 -1
  68. package/packages/dd-trace/src/exporters/common/request.js +9 -0
  69. package/packages/dd-trace/src/exporters/common/writer.js +12 -2
  70. package/packages/dd-trace/src/heap_snapshots.js +3 -0
  71. package/packages/dd-trace/src/index.js +5 -2
  72. package/packages/dd-trace/src/lambda/runtime/ritm.js +6 -6
  73. package/packages/dd-trace/src/llmobs/index.js +4 -1
  74. package/packages/dd-trace/src/llmobs/plugins/ai/index.js +5 -1
  75. package/packages/dd-trace/src/llmobs/plugins/ai/util.js +60 -12
  76. package/packages/dd-trace/src/llmobs/plugins/bedrockruntime.js +4 -2
  77. package/packages/dd-trace/src/llmobs/sdk.js +12 -8
  78. package/packages/dd-trace/src/llmobs/span_processor.js +1 -1
  79. package/packages/dd-trace/src/llmobs/tagger.js +9 -6
  80. package/packages/dd-trace/src/llmobs/writers/base.js +2 -0
  81. package/packages/dd-trace/src/llmobs/writers/util.js +3 -0
  82. package/packages/dd-trace/src/log/index.js +26 -55
  83. package/packages/dd-trace/src/log/writer.js +7 -19
  84. package/packages/dd-trace/src/noop/proxy.js +8 -0
  85. package/packages/dd-trace/src/opentelemetry/logs/index.js +1 -1
  86. package/packages/dd-trace/src/opentelemetry/metrics/index.js +1 -1
  87. package/packages/dd-trace/src/opentracing/propagation/text_map.js +9 -4
  88. package/packages/dd-trace/src/payload-tagging/config/index.js +6 -5
  89. package/packages/dd-trace/src/plugin_manager.js +8 -6
  90. package/packages/dd-trace/src/plugins/ci_plugin.js +4 -0
  91. package/packages/dd-trace/src/plugins/plugin.js +7 -4
  92. package/packages/dd-trace/src/process-tags/index.js +3 -0
  93. package/packages/dd-trace/src/profiler.js +27 -2
  94. package/packages/dd-trace/src/profiling/config.js +73 -241
  95. package/packages/dd-trace/src/profiling/exporter_cli.js +1 -4
  96. package/packages/dd-trace/src/profiling/exporters/event_serializer.js +6 -2
  97. package/packages/dd-trace/src/profiling/profiler.js +56 -44
  98. package/packages/dd-trace/src/profiling/profilers/events.js +2 -3
  99. package/packages/dd-trace/src/profiling/profilers/wall.js +89 -6
  100. package/packages/dd-trace/src/profiling/ssi-heuristics.js +4 -1
  101. package/packages/dd-trace/src/propagation-hash/index.js +2 -1
  102. package/packages/dd-trace/src/proxy.js +32 -3
  103. package/packages/dd-trace/src/remote_config/index.js +3 -0
  104. package/packages/dd-trace/src/require-package-json.js +8 -4
  105. package/packages/dd-trace/src/ritm.js +58 -26
  106. package/packages/dd-trace/src/runtime_metrics/index.js +3 -0
  107. package/packages/dd-trace/src/runtime_metrics/runtime_metrics.js +3 -0
  108. package/packages/dd-trace/src/sampler.js +1 -1
  109. package/packages/dd-trace/src/standalone/index.js +3 -0
  110. package/packages/dd-trace/src/telemetry/index.js +2 -3
  111. package/packages/dd-trace/src/telemetry/send-data.js +5 -19
  112. package/packages/dd-trace/src/telemetry/session-propagation.js +19 -44
  113. package/packages/dd-trace/src/telemetry/telemetry.js +28 -171
  114. package/packages/dd-trace/src/util.js +0 -9
@@ -146,7 +146,7 @@ function wrapAppUse (use) {
146
146
  }
147
147
  }
148
148
 
149
- addHook({ name: 'express', versions: ['>=4'], file: ['lib/express.js'] }, express => {
149
+ addHook({ name: 'express', versions: ['>=4'], file: 'lib/express.js' }, express => {
150
150
  shimmer.wrap(express.application, 'handle', wrapHandle)
151
151
  shimmer.wrap(express.application, 'all', wrapAppAll)
152
152
  shimmer.wrap(express.application, 'route', wrapAppRoute)
@@ -224,19 +224,19 @@ function wrapProcessParamsMethod (requestPositionInArguments) {
224
224
  }
225
225
  }
226
226
 
227
- addHook({ name: 'express', versions: ['>=4.0.0 <4.3.0'], file: ['lib/express.js'] }, express => {
227
+ addHook({ name: 'express', versions: ['>=4.0.0 <4.3.0'], file: 'lib/express.js' }, express => {
228
228
  shimmer.wrap(express.Router, 'process_params', wrapProcessParamsMethod(1))
229
229
  return express
230
230
  })
231
231
 
232
- addHook({ name: 'express', versions: ['>=4.3.0 <5.0.0'], file: ['lib/express.js'] }, express => {
232
+ addHook({ name: 'express', versions: ['>=4.3.0 <5.0.0'], file: 'lib/express.js' }, express => {
233
233
  shimmer.wrap(express.Router, 'process_params', wrapProcessParamsMethod(2))
234
234
  return express
235
235
  })
236
236
 
237
237
  const queryReadCh = channel('datadog:express:query:finish')
238
238
 
239
- addHook({ name: 'express', file: ['lib/request.js'], versions: ['>=5.0.0'] }, request => {
239
+ addHook({ name: 'express', file: 'lib/request.js', versions: ['>=5.0.0'] }, request => {
240
240
  shimmer.wrap(request, 'query', function (originalGet) {
241
241
  return function wrappedGet () {
242
242
  const query = originalGet.call(this)
@@ -84,37 +84,35 @@ const paramsByFileHandleMethods = {
84
84
  writeFile: ['data', 'options'],
85
85
  writev: ['buffers', 'position'],
86
86
  }
87
- const names = ['fs', 'node:fs']
88
- for (const name of names) {
89
- addHook({ name }, fs => {
90
- const asyncMethods = Object.keys(paramsByMethod)
91
- const syncMethods = asyncMethods.map(name => `${name}Sync`)
92
-
93
- massWrap(fs, asyncMethods, createWrapFunction())
94
- massWrap(fs, syncMethods, createWrapFunction())
95
- massWrap(fs.promises, asyncMethods, createWrapFunction('promises.'))
96
-
97
- wrap(fs.realpath, 'native', createWrapFunction('', 'realpath.native'))
98
- wrap(fs.realpathSync, 'native', createWrapFunction('', 'realpath.native'))
99
- wrap(fs.promises.realpath, 'native', createWrapFunction('', 'realpath.native'))
100
-
101
- wrap(fs, 'createReadStream', wrapCreateStream)
102
- wrap(fs, 'createWriteStream', wrapCreateStream)
103
- if (fs.Dir) {
104
- wrap(fs.Dir.prototype, 'close', createWrapFunction('dir.'))
105
- wrap(fs.Dir.prototype, 'closeSync', createWrapFunction('dir.'))
106
- wrap(fs.Dir.prototype, 'read', createWrapFunction('dir.'))
107
- wrap(fs.Dir.prototype, 'readSync', createWrapFunction('dir.'))
108
- wrap(fs.Dir.prototype, Symbol.asyncIterator, createWrapDirAsyncIterator())
109
- }
87
+ addHook({ name: 'fs' }, fs => {
88
+ const asyncMethods = Object.keys(paramsByMethod)
89
+ const syncMethods = asyncMethods.map(name => `${name}Sync`)
90
+
91
+ massWrap(fs, asyncMethods, createWrapFunction())
92
+ massWrap(fs, syncMethods, createWrapFunction())
93
+ massWrap(fs.promises, asyncMethods, createWrapFunction('promises.'))
94
+
95
+ wrap(fs.realpath, 'native', createWrapFunction('', 'realpath.native'))
96
+ wrap(fs.realpathSync, 'native', createWrapFunction('', 'realpath.native'))
97
+ wrap(fs.promises.realpath, 'native', createWrapFunction('', 'realpath.native'))
98
+
99
+ wrap(fs, 'createReadStream', wrapCreateStream)
100
+ wrap(fs, 'createWriteStream', wrapCreateStream)
101
+ if (fs.Dir) {
102
+ wrap(fs.Dir.prototype, 'close', createWrapFunction('dir.'))
103
+ wrap(fs.Dir.prototype, 'closeSync', createWrapFunction('dir.'))
104
+ wrap(fs.Dir.prototype, 'read', createWrapFunction('dir.'))
105
+ wrap(fs.Dir.prototype, 'readSync', createWrapFunction('dir.'))
106
+ wrap(fs.Dir.prototype, Symbol.asyncIterator, createWrapDirAsyncIterator())
107
+ }
110
108
 
111
- wrap(fs, 'unwatchFile', createWatchWrapFunction())
112
- wrap(fs, 'watch', createWatchWrapFunction())
113
- wrap(fs, 'watchFile', createWatchWrapFunction())
109
+ wrap(fs, 'unwatchFile', createWatchWrapFunction())
110
+ wrap(fs, 'watch', createWatchWrapFunction())
111
+ wrap(fs, 'watchFile', createWatchWrapFunction())
112
+
113
+ return fs
114
+ })
114
115
 
115
- return fs
116
- })
117
- }
118
116
  function isFirstMethodReturningFileHandle (original) {
119
117
  return !kHandle && original.name === 'open'
120
118
  }
@@ -171,7 +171,7 @@ function wrapExecute (execute) {
171
171
  args,
172
172
  docSource: documentSources.get(document),
173
173
  source,
174
- fields: {},
174
+ fields: Object.create(null),
175
175
  abortController: new AbortController(),
176
176
  }
177
177
 
@@ -45,45 +45,73 @@ if (!dc.unsubscribe) {
45
45
  dc.unsubscribe = (channel, cb) => {
46
46
  if (dc.channel(channel).hasSubscribers) {
47
47
  dc.channel(channel).unsubscribe(cb)
48
+ return true
48
49
  }
50
+ return false
49
51
  }
50
52
  }
51
53
 
52
- function doHook (payload) {
53
- const hook = hooks[payload.package]
54
+ /**
55
+ * @param {string} name
56
+ */
57
+ function doHook (name) {
58
+ const hook = hooks[name] ?? hooks[`node:${name}`]
54
59
  if (!hook) {
55
- log.error('esbuild-wrapped %s missing in list of hooks', payload.package)
60
+ log.error('esbuild-wrapped %s missing in list of hooks', name)
56
61
  return
57
62
  }
58
63
 
59
64
  const hookFn = hook.fn ?? hook
60
65
  if (typeof hookFn !== 'function') {
61
- log.error('esbuild-wrapped hook %s is not a function', payload.package)
66
+ log.error('esbuild-wrapped hook %s is not a function', name)
62
67
  return
63
68
  }
64
69
 
65
70
  try {
66
71
  hookFn()
67
72
  } catch {
68
- log.error('esbuild-wrapped %s hook failed', payload.package)
73
+ log.error('esbuild-wrapped %s hook failed', name)
69
74
  }
70
75
  }
71
76
 
72
- dc.subscribe(CHANNEL, (payload) => {
73
- doHook(payload)
77
+ /** @type {Set<string>} */
78
+ const instrumentedNodeModules = new Set()
74
79
 
75
- if (!instrumentations[payload.package]) {
76
- log.error('esbuild-wrapped %s missing in list of instrumentations', payload.package)
80
+ /** @typedef {{ package: string, module: unknown, version: string, path: string }} Payload */
81
+ dc.subscribe(CHANNEL, (message) => {
82
+ const payload = /** @type {Payload} */ (message)
83
+ const name = payload.package
84
+
85
+ const isPrefixedWithNode = name.startsWith('node:')
86
+
87
+ const isNodeModule = isPrefixedWithNode || !hooks[name]
88
+
89
+ if (isNodeModule) {
90
+ const nodeName = isPrefixedWithNode ? name.slice(5) : name
91
+ // Used for node: prefixed modules to prevent double instrumentation.
92
+ if (instrumentedNodeModules.has(nodeName)) {
93
+ return
94
+ }
95
+ instrumentedNodeModules.add(nodeName)
96
+ }
97
+
98
+ doHook(name)
99
+
100
+ const instrumentation = instrumentations[name] ?? instrumentations[`node:${name}`]
101
+
102
+ if (!instrumentation) {
103
+ log.error('esbuild-wrapped %s missing in list of instrumentations', name)
77
104
  return
78
105
  }
79
106
 
80
- for (const { name, file, versions, hook } of instrumentations[payload.package]) {
81
- if (payload.path !== filename(name, file)) continue
82
- if (!matchVersion(payload.version, versions)) continue
107
+ for (const { file, versions, hook } of instrumentation) {
108
+ if (payload.path !== filename(name, file) || !matchVersion(payload.version, versions)) {
109
+ continue
110
+ }
83
111
 
84
112
  try {
85
113
  loadChannel.publish({ name, version: payload.version, file })
86
- payload.module = hook(payload.module, payload.version)
114
+ payload.module = hook(payload.module, payload.version) ?? payload.module
87
115
  } catch (e) {
88
116
  log.error('Error executing bundler hook', e)
89
117
  }
@@ -1,17 +1,41 @@
1
1
  'use strict'
2
+
2
3
  const path = require('path')
4
+
3
5
  const iitm = require('../../../dd-trace/src/iitm')
4
6
  const ritm = require('../../../dd-trace/src/ritm')
7
+ const log = require('../../../dd-trace/src/log')
8
+ const requirePackageJson = require('../../../dd-trace/src/require-package-json')
9
+
10
+ /**
11
+ * @param {string} moduleBaseDir
12
+ * @returns {string|undefined}
13
+ */
14
+ function getVersion (moduleBaseDir) {
15
+ if (moduleBaseDir) {
16
+ return requirePackageJson(moduleBaseDir, /** @type {import('module').Module} */ (module)).version
17
+ }
18
+
19
+ return process.version
20
+ }
5
21
 
6
22
  /**
7
23
  * This is called for every package/internal-module that dd-trace supports instrumentation for
8
24
  * In practice, `modules` is always an array with a single entry.
9
25
  *
26
+ * @overload
27
+ * @param {string[]} modules list of modules to hook into
28
+ * @param {object} hookOptions hook options
29
+ * @param {Function} onrequire callback to be executed upon encountering module
30
+ */
31
+ /**
32
+ * @overload
10
33
  * @param {string[]} modules list of modules to hook into
11
34
  * @param {object} hookOptions hook options
12
35
  * @param {Function} onrequire callback to be executed upon encountering module
13
36
  */
14
37
  function Hook (modules, hookOptions, onrequire) {
38
+ // TODO: Rewrite this to use class syntax. The same should be done for ritm.
15
39
  if (!(this instanceof Hook)) return new Hook(modules, hookOptions, onrequire)
16
40
 
17
41
  if (typeof hookOptions === 'function') {
@@ -42,6 +66,13 @@ function Hook (modules, hookOptions, onrequire) {
42
66
  return result
43
67
  }
44
68
 
69
+ try {
70
+ moduleVersion ||= getVersion(moduleBaseDir)
71
+ } catch (error) {
72
+ log.error('Error getting version for "%s": %s', moduleName, error.message, error)
73
+ return
74
+ }
75
+
45
76
  if (
46
77
  isIitm &&
47
78
  moduleExports.default &&
@@ -66,10 +97,4 @@ function Hook (modules, hookOptions, onrequire) {
66
97
  })
67
98
  }
68
99
 
69
- Hook.prototype.unhook = function () {
70
- this._ritmHook.unhook()
71
- this._iitmHook.unhook()
72
- this._patched = Object.create(null)
73
- }
74
-
75
100
  module.exports = Hook
@@ -1,6 +1,18 @@
1
1
  'use strict'
2
2
 
3
3
  module.exports = {
4
+ // Only list unprefixed node modules. They will automatically be instrumented as prefixed and unprefixed.
5
+ child_process: () => require('../child_process'),
6
+ crypto: () => require('../crypto'),
7
+ dns: () => require('../dns'),
8
+ fs: { serverless: false, fn: () => require('../fs') },
9
+ http: () => require('../http'),
10
+ http2: () => require('../http2'),
11
+ https: () => require('../http'),
12
+ net: () => require('../net'),
13
+ url: () => require('../url'),
14
+ vm: () => require('../vm'),
15
+ // Non Node.js modules
4
16
  '@anthropic-ai/sdk': { esmFirst: true, fn: () => require('../anthropic') },
5
17
  '@apollo/server': () => require('../apollo-server'),
6
18
  '@apollo/gateway': () => require('../apollo'),
@@ -47,31 +59,24 @@ module.exports = {
47
59
  bullmq: () => require('../bullmq'),
48
60
  bunyan: () => require('../bunyan'),
49
61
  'cassandra-driver': () => require('../cassandra-driver'),
50
- child_process: () => require('../child_process'),
51
62
  connect: () => require('../connect'),
52
63
  cookie: () => require('../cookie'),
53
64
  'cookie-parser': () => require('../cookie-parser'),
54
65
  couchbase: () => require('../couchbase'),
55
- crypto: () => require('../crypto'),
56
66
  cypress: () => require('../cypress'),
57
67
  'dd-trace-api': () => require('../dd-trace-api'),
58
- dns: () => require('../dns'),
59
68
  elasticsearch: () => require('../elasticsearch'),
60
69
  express: () => require('../express'),
61
70
  'express-mongo-sanitize': () => require('../express-mongo-sanitize'),
62
71
  'express-session': () => require('../express-session'),
63
72
  fastify: () => require('../fastify'),
64
73
  'find-my-way': () => require('../find-my-way'),
65
- fs: { serverless: false, fn: () => require('../fs') },
66
74
  'generic-pool': () => require('../generic-pool'),
67
75
  graphql: () => require('../graphql'),
68
76
  grpc: () => require('../grpc'),
69
77
  handlebars: () => require('../handlebars'),
70
78
  hapi: () => require('../hapi'),
71
79
  hono: { esmFirst: true, fn: () => require('../hono') },
72
- http: () => require('../http'),
73
- http2: () => require('../http2'),
74
- https: () => require('../http'),
75
80
  ioredis: () => require('../ioredis'),
76
81
  iovalkey: () => require('../iovalkey'),
77
82
  'jest-circus': () => require('../jest'),
@@ -103,18 +108,8 @@ module.exports = {
103
108
  multer: () => require('../multer'),
104
109
  mysql: () => require('../mysql'),
105
110
  mysql2: () => require('../mysql2'),
106
- net: () => require('../net'),
107
111
  next: () => require('../next'),
108
112
  'node-serialize': () => require('../node-serialize'),
109
- 'node:child_process': () => require('../child_process'),
110
- 'node:crypto': () => require('../crypto'),
111
- 'node:dns': () => require('../dns'),
112
- 'node:http': () => require('../http'),
113
- 'node:http2': () => require('../http2'),
114
- 'node:https': () => require('../http'),
115
- 'node:net': () => require('../net'),
116
- 'node:url': () => require('../url'),
117
- 'node:vm': () => require('../vm'),
118
113
  nyc: () => require('../nyc'),
119
114
  oracledb: () => require('../oracledb'),
120
115
  openai: { esmFirst: true, fn: () => require('../openai') },
@@ -142,9 +137,7 @@ module.exports = {
142
137
  tedious: () => require('../tedious'),
143
138
  tinypool: { esmFirst: true, fn: () => require('../vitest') },
144
139
  undici: () => require('../undici'),
145
- url: () => require('../url'),
146
140
  vitest: { esmFirst: true, fn: () => require('../vitest') },
147
- vm: () => require('../vm'),
148
141
  when: () => require('../when'),
149
142
  winston: () => require('../winston'),
150
143
  workerpool: () => require('../mocha'),
@@ -1,11 +1,23 @@
1
1
  'use strict'
2
2
 
3
3
  const { AsyncResource } = require('async_hooks')
4
- const dc = require('dc-polyfill')
4
+ const dc = /** @type {typeof import('node:diagnostics_channel')} */ (require('dc-polyfill'))
5
5
  const instrumentations = require('./instrumentations')
6
6
  const rewriterInstrumentations = require('./rewriter/instrumentations')
7
7
 
8
+ /**
9
+ * @typedef {import('node:diagnostics_channel').Channel} Channel
10
+ * @typedef {import('node:diagnostics_channel').TracingChannel} TracingChannel
11
+ */
12
+
13
+ /**
14
+ * @type {Record<string, Channel>}
15
+ */
8
16
  const channelMap = {}
17
+ /**
18
+ * @param {string} name
19
+ * @returns {Channel}
20
+ */
9
21
  exports.channel = function (name) {
10
22
  const maybe = channelMap[name]
11
23
  if (maybe) return maybe
@@ -14,7 +26,14 @@ exports.channel = function (name) {
14
26
  return ch
15
27
  }
16
28
 
29
+ /**
30
+ * @type {Record<string, TracingChannel>}
31
+ */
17
32
  const tracingChannelMap = {}
33
+ /**
34
+ * @param {string} name
35
+ * @returns {TracingChannel}
36
+ */
18
37
  exports.tracingChannel = function (name) {
19
38
  const maybe = tracingChannelMap[name]
20
39
  if (maybe) return maybe
@@ -34,24 +53,19 @@ exports.getHooks = function getHooks (names) {
34
53
 
35
54
  /**
36
55
  * @param {object} args
37
- * @param {string|string[]} args.name module name
38
- * @param {string[]} args.versions array of semver range strings
56
+ * @param {string} args.name module name
57
+ * @param {string[]} [args.versions] array of semver range strings
39
58
  * @param {string} [args.file='index.js'] path to file within package to instrument
40
59
  * @param {string} [args.filePattern] pattern to match files within package to instrument
41
- * @param {boolean} [args.patchDefault] whether to patch the default export
42
- * @param {(moduleExports: unknown, version: string) => unknown} hook
60
+ * @param {boolean} [args.patchDefault=true] whether to patch the default export
61
+ * @param {(moduleExports: unknown, version: string, isIitm?: boolean) => unknown} [hook] Patches module exports
43
62
  */
44
63
  exports.addHook = function addHook ({ name, versions, file, filePattern, patchDefault }, hook) {
45
- if (typeof name === 'string') {
46
- name = [name]
64
+ if (!instrumentations[name]) {
65
+ instrumentations[name] = []
47
66
  }
48
67
 
49
- for (const val of name) {
50
- if (!instrumentations[val]) {
51
- instrumentations[val] = []
52
- }
53
- instrumentations[val].push({ name: val, versions, file, filePattern, hook, patchDefault })
54
- }
68
+ instrumentations[name].push({ versions, file, filePattern, hook, patchDefault })
55
69
  }
56
70
 
57
71
  exports.AsyncResource = AsyncResource