dd-trace 5.135.0 → 5.136.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 (250) hide show
  1. package/ci/init.js +2 -22
  2. package/ext/exporters.js +0 -1
  3. package/index.d.ts +105 -5
  4. package/initialize.mjs +5 -2
  5. package/lambda.d.ts +39 -0
  6. package/lambda.js +3 -0
  7. package/package.json +11 -9
  8. package/packages/datadog-esbuild/index.js +8 -55
  9. package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
  10. package/packages/datadog-instrumentations/src/cucumber.js +5 -2
  11. package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
  12. package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
  13. package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
  14. package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
  15. package/packages/datadog-instrumentations/src/graphql.js +20 -17
  16. package/packages/{datadog-esbuild/src/utils.js → datadog-instrumentations/src/helpers/bundler-esm-exports.js} +282 -40
  17. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +573 -0
  18. package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
  19. package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
  20. package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -7
  21. package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
  22. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +2 -0
  23. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +18 -1
  24. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
  25. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
  26. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
  27. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +40 -0
  28. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
  29. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
  30. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +50 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +17 -3
  32. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
  33. package/packages/datadog-instrumentations/src/ioredis.js +101 -3
  34. package/packages/datadog-instrumentations/src/next.js +19 -6
  35. package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
  36. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
  37. package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
  38. package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
  39. package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
  40. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
  41. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
  42. package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
  43. package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
  44. package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
  45. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +22 -11
  46. package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
  47. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +10 -7
  48. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +19 -52
  49. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +4 -2
  50. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
  51. package/packages/datadog-plugin-http/src/client.js +32 -9
  52. package/packages/datadog-plugin-http2/src/client.js +38 -13
  53. package/packages/datadog-plugin-next/src/index.js +112 -14
  54. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
  55. package/packages/datadog-plugin-trpc/src/index.js +15 -0
  56. package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
  57. package/packages/datadog-plugin-trpc/src/request.js +81 -0
  58. package/packages/datadog-plugin-undici/src/index.js +22 -7
  59. package/packages/datadog-turbopack/index.js +4 -28
  60. package/packages/datadog-turbopack/src/loader.js +14 -452
  61. package/packages/datadog-webpack/index.js +3 -55
  62. package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
  63. package/packages/dd-trace/src/aiguard/sdk.js +8 -2
  64. package/packages/dd-trace/src/aiguard/tags.js +2 -0
  65. package/packages/dd-trace/src/appsec/blocking/index.js +4 -0
  66. package/packages/dd-trace/src/appsec/graphql.js +91 -6
  67. package/packages/dd-trace/src/appsec/recommended.json +1287 -90
  68. package/packages/dd-trace/src/before-exit.js +32 -0
  69. package/packages/dd-trace/src/bootstrap.js +5 -3
  70. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +15 -2
  71. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +12 -34
  72. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
  73. package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
  74. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +1 -2
  75. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +1 -1
  76. package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
  77. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +1 -1
  78. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
  79. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
  80. package/packages/dd-trace/src/config/config-types.d.ts +3 -0
  81. package/packages/dd-trace/src/config/defaults.js +5 -0
  82. package/packages/dd-trace/src/config/generated-config-types.d.ts +42 -2
  83. package/packages/dd-trace/src/config/index.js +17 -5
  84. package/packages/dd-trace/src/config/parsers.js +9 -0
  85. package/packages/dd-trace/src/config/supported-configurations.json +158 -4
  86. package/packages/dd-trace/src/constants.js +1 -0
  87. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +2 -1
  88. package/packages/dd-trace/src/exporter.js +0 -11
  89. package/packages/dd-trace/src/exporters/common/request.js +81 -48
  90. package/packages/dd-trace/src/exporters/common/retry.js +12 -7
  91. package/packages/dd-trace/src/exporters/common/url.js +1 -1
  92. package/packages/dd-trace/src/id.js +1 -1
  93. package/packages/dd-trace/src/lambda/facade.js +74 -0
  94. package/packages/dd-trace/src/lambda/handler.js +3 -41
  95. package/packages/dd-trace/src/lambda/index.js +38 -15
  96. package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
  97. package/packages/dd-trace/src/lambda/wrap.js +44 -0
  98. package/packages/dd-trace/src/llmobs/constants/tags.js +2 -0
  99. package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
  100. package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
  101. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +19 -6
  102. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
  103. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
  104. package/packages/dd-trace/src/llmobs/sdk.js +19 -0
  105. package/packages/dd-trace/src/llmobs/span_processor.js +9 -0
  106. package/packages/dd-trace/src/llmobs/tagger.js +22 -0
  107. package/packages/dd-trace/src/log/channels.js +4 -0
  108. package/packages/dd-trace/src/log/writer.js +6 -1
  109. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +4 -0
  110. package/packages/dd-trace/src/openfeature/configuration_source.js +35 -27
  111. package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
  112. package/packages/dd-trace/src/openfeature/flagging_provider.js +19 -1
  113. package/packages/dd-trace/src/openfeature/writers/base.js +1 -3
  114. package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
  115. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
  116. package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
  117. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
  118. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
  119. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
  120. package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
  121. package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
  122. package/packages/dd-trace/src/opentelemetry/span.js +2 -2
  123. package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
  124. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
  125. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
  126. package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
  127. package/packages/dd-trace/src/opentracing/tracer.js +24 -0
  128. package/packages/dd-trace/src/otel-thread-ctx.js +28 -31
  129. package/packages/dd-trace/src/plugin_manager.js +34 -7
  130. package/packages/dd-trace/src/plugins/ci_plugin.js +41 -10
  131. package/packages/dd-trace/src/plugins/index.js +6 -0
  132. package/packages/dd-trace/src/plugins/plugin.js +4 -2
  133. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  134. package/packages/dd-trace/src/plugins/util/ci.js +18 -1
  135. package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
  136. package/packages/dd-trace/src/plugins/util/git.js +263 -82
  137. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +75 -46
  138. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
  139. package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
  140. package/packages/dd-trace/src/plugins/util/tags.js +6 -0
  141. package/packages/dd-trace/src/plugins/util/test.js +64 -39
  142. package/packages/dd-trace/src/plugins/util/url.js +10 -2
  143. package/packages/dd-trace/src/plugins/util/user-provided-git.js +2 -2
  144. package/packages/dd-trace/src/plugins/util/web.js +65 -10
  145. package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
  146. package/packages/dd-trace/src/proxy.js +33 -53
  147. package/packages/dd-trace/src/span_processor.js +20 -0
  148. package/packages/dd-trace/src/startup-log.js +8 -6
  149. package/packages/dd-trace/src/telemetry/metrics.js +2 -1
  150. package/packages/dd-trace/src/tracer.js +0 -2
  151. package/packages/dd-trace/src/web-tags-cache.js +8 -9
  152. package/register.js +2 -1
  153. package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
  154. package/vendor/dist/@datadog/flagging-core/LICENSE +221 -0
  155. package/vendor/dist/@datadog/flagging-core/index.js +1 -0
  156. package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
  157. package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
  158. package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
  159. package/vendor/dist/@datadog/source-map/LICENSE +1 -0
  160. package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
  161. package/vendor/dist/@opentelemetry/core/LICENSE +209 -1
  162. package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
  163. package/vendor/dist/escape-string-regexp/LICENSE +13 -0
  164. package/vendor/dist/esquery/LICENSE +2 -0
  165. package/vendor/dist/https-proxy-agent/LICENSE +80 -1
  166. package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
  167. package/vendor/dist/jest-docblock/LICENSE +16 -0
  168. package/vendor/dist/jsonpath-plus/LICENSE +13 -0
  169. package/vendor/dist/limiter/LICENSE +2 -0
  170. package/vendor/dist/lodash.sortby/LICENSE +2 -0
  171. package/vendor/dist/lru-cache/LICENSE +13 -0
  172. package/vendor/dist/meriyah/LICENSE +13 -0
  173. package/vendor/dist/module-details-from-path/LICENSE +2 -0
  174. package/vendor/dist/mutexify/promise/LICENSE +28 -0
  175. package/vendor/dist/pprof-format/LICENSE +13 -0
  176. package/vendor/dist/pprof-format/index.js +1 -1
  177. package/vendor/dist/protobufjs/LICENSE +219 -0
  178. package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
  179. package/vendor/dist/proxy-from-env/LICENSE +13 -0
  180. package/vendor/dist/retry/LICENSE +2 -0
  181. package/vendor/dist/rfdc/LICENSE +2 -0
  182. package/vendor/dist/semifies/LICENSE +3 -1
  183. package/vendor/dist/shell-quote/LICENSE +3 -1
  184. package/vendor/dist/source-map/LICENSE +1 -0
  185. package/vendor/dist/source-map/lib/util/LICENSE +1 -0
  186. package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
  187. package/vendor/dist/ttl-set/LICENSE +28 -0
  188. package/ci/diagnose.js +0 -2215
  189. package/ci/runbook.md +0 -299
  190. package/ci/test-optimization-validation/approval-artifacts.js +0 -175
  191. package/ci/test-optimization-validation/approval.js +0 -386
  192. package/ci/test-optimization-validation/artifact-id.js +0 -18
  193. package/ci/test-optimization-validation/blocker-category.js +0 -24
  194. package/ci/test-optimization-validation/bounded-json.js +0 -120
  195. package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
  196. package/ci/test-optimization-validation/ci-discovery.js +0 -192
  197. package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
  198. package/ci/test-optimization-validation/ci-remediation.js +0 -283
  199. package/ci/test-optimization-validation/cli.js +0 -954
  200. package/ci/test-optimization-validation/command-blocker.js +0 -377
  201. package/ci/test-optimization-validation/command-output-policy.js +0 -188
  202. package/ci/test-optimization-validation/command-runner.js +0 -649
  203. package/ci/test-optimization-validation/environment.js +0 -88
  204. package/ci/test-optimization-validation/executable-approval.js +0 -47
  205. package/ci/test-optimization-validation/executable.js +0 -244
  206. package/ci/test-optimization-validation/execution-lock.js +0 -70
  207. package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
  208. package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
  209. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
  210. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
  211. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
  212. package/ci/test-optimization-validation/generated-file-policy.js +0 -63
  213. package/ci/test-optimization-validation/generated-files.js +0 -425
  214. package/ci/test-optimization-validation/generated-test-contract.js +0 -284
  215. package/ci/test-optimization-validation/generated-verifier.js +0 -235
  216. package/ci/test-optimization-validation/literal-environment.js +0 -56
  217. package/ci/test-optimization-validation/literal-glob.js +0 -52
  218. package/ci/test-optimization-validation/manifest-loader.js +0 -116
  219. package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
  220. package/ci/test-optimization-validation/manifest-schema.js +0 -695
  221. package/ci/test-optimization-validation/offline-fixtures.js +0 -335
  222. package/ci/test-optimization-validation/offline-output.js +0 -406
  223. package/ci/test-optimization-validation/package-check.js +0 -97
  224. package/ci/test-optimization-validation/payload-normalizer.js +0 -72
  225. package/ci/test-optimization-validation/plan-writer.js +0 -541
  226. package/ci/test-optimization-validation/preflight-runner.js +0 -186
  227. package/ci/test-optimization-validation/project-build-artifact.js +0 -31
  228. package/ci/test-optimization-validation/redaction.js +0 -319
  229. package/ci/test-optimization-validation/report-writer.js +0 -666
  230. package/ci/test-optimization-validation/result-semantics.js +0 -103
  231. package/ci/test-optimization-validation/runner-command.js +0 -264
  232. package/ci/test-optimization-validation/runner-contract.js +0 -695
  233. package/ci/test-optimization-validation/safe-files.js +0 -202
  234. package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
  235. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
  236. package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
  237. package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
  238. package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
  239. package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
  240. package/ci/test-optimization-validation/source-text.js +0 -84
  241. package/ci/test-optimization-validation/static-diagnosis.js +0 -166
  242. package/ci/test-optimization-validation/test-output.js +0 -120
  243. package/ci/test-optimization-validation/validation-blocker.js +0 -21
  244. package/ci/validate-test-optimization.js +0 -3
  245. package/packages/datadog-esbuild/src/resolver.js +0 -284
  246. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
  247. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
  248. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
  249. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
  250. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
@@ -1,513 +0,0 @@
1
- 'use strict'
2
-
3
- const fs = require('node:fs')
4
- const path = require('node:path')
5
-
6
- const { matchesLiteralGlob } = require('../literal-glob')
7
- const { maskJavaScriptComments, maskJavaScriptNonCode } = require('../source-text')
8
-
9
- const CONFIG_PATTERN = /^(?:vite\.config|vitest\.(?:config|workspace))\.[cm]?[jt]s$/
10
- const WORKSPACE_CONFIG_PATTERN = /^vitest\.workspace\.[cm]?[jt]s$/
11
- const DIRECT_EXPORT_PATTERN = /(?:export\s+default|module\s*\.\s*exports\s*=)\s*$/
12
- const CONFIG_CALL_PATTERN =
13
- /(?:export\s+default|module\s*\.\s*exports\s*=)\s*defineConfig\s*\(\s*$/
14
- const WORKSPACE_ARRAY_CALL_PATTERN =
15
- /(?:export\s+default|module\s*\.\s*exports\s*=)\s*defineWorkspace\s*\(\s*$/
16
- const PROJECT_CALL_PATTERN =
17
- /(?:export\s+default|module\s*\.\s*exports\s*=)\s*defineProject\s*\(\s*$/
18
- const LITERAL_PROJECT_PATTERN = /^[A-Za-z0-9_.:@/-]+$/
19
-
20
- /**
21
- * Binds one literal `--project` selector to one statically named Vitest project.
22
- *
23
- * Customer configuration is read as text only. Dynamic names, roots, includes, and ambiguous matches fail closed.
24
- *
25
- * @param {object} input project inputs
26
- * @param {string[]} input.configFiles approval-bound configuration files
27
- * @param {string[]} input.projectFiles bounded project files
28
- * @param {string} input.projectRoot detected project root
29
- * @param {string[]} input.runnerArgs retained Vitest arguments
30
- * @returns {{
31
- * configFile?: string,
32
- * error?: string,
33
- * excludePatterns?: string[],
34
- * files?: string[],
35
- * includePatterns?: string[],
36
- * root?: string
37
- * }|undefined} project binding
38
- */
39
- function bindLiteralProject ({ configFiles, projectFiles, projectRoot, runnerArgs }) {
40
- const projects = getOptionValues(runnerArgs, '--project')
41
- if (projects.length === 0) return
42
- if (projects.length !== 1 || !LITERAL_PROJECT_PATTERN.test(projects[0])) {
43
- return { error: '--project must select exactly one literal project name' }
44
- }
45
-
46
- const name = projects[0]
47
- const roots = getOptionValues(runnerArgs, '--root')
48
- if (roots.length > 1) {
49
- return { error: '--project cannot be bound with more than one Vitest root' }
50
- }
51
- const effectiveRoot = roots.length === 1
52
- ? getPhysicalDirectory(path.resolve(projectRoot, roots[0]))
53
- : getPhysicalDirectory(projectRoot)
54
- if (!effectiveRoot || !isContainedDirectory(projectRoot, effectiveRoot)) {
55
- return { error: 'the selected Vitest root is not a project-contained directory' }
56
- }
57
-
58
- const selectedConfigs = getSelectedConfigFiles({ configFiles, effectiveRoot, projectRoot, runnerArgs })
59
- if (selectedConfigs.error) return selectedConfigs
60
- const bindings = []
61
- for (const configFile of selectedConfigs.files) {
62
- const source = readText(configFile)
63
- if (source === undefined) continue
64
- for (const property of getLiteralStringProperties(source, 'name')) {
65
- if (property.value !== name) continue
66
- const project = getProjectObject(
67
- source,
68
- property.index,
69
- WORKSPACE_CONFIG_PATTERN.test(path.basename(configFile))
70
- )
71
- if (!project) continue
72
- const projectName = getLiteralProperty(project.source, 'name')
73
- if (projectName.dynamic || projectName.value !== name) continue
74
- const binding = getBindingFromObject({
75
- bindingRoot: effectiveRoot,
76
- configFile,
77
- projectFiles,
78
- projectEntry: project.entrySource,
79
- projectObject: project.source,
80
- projectRoot,
81
- standaloneProject: project.standalone,
82
- })
83
- if (binding.error) return binding
84
- bindings.push(binding)
85
- }
86
- }
87
-
88
- if (bindings.length !== 1) {
89
- return {
90
- error: bindings.length === 0
91
- ? `--project ${JSON.stringify(name)} does not map to one statically named Vitest project`
92
- : `--project ${JSON.stringify(name)} maps to more than one Vitest project`,
93
- }
94
- }
95
- return bindings[0]
96
- }
97
-
98
- function getSelectedConfigFiles ({ configFiles, effectiveRoot, projectRoot, runnerArgs }) {
99
- const explicitConfigs = getOptionValues(runnerArgs, '--config')
100
- if (explicitConfigs.length > 1) {
101
- return { error: '--project cannot be bound with more than one explicit Vitest config' }
102
- }
103
- if (explicitConfigs.length === 1) {
104
- const explicitConfig = getPhysicalPath(path.resolve(projectRoot, explicitConfigs[0]))
105
- const selected = configFiles.find(filename => filename === explicitConfig)
106
- return selected
107
- ? { files: [selected] }
108
- : { error: 'the explicit Vitest config is not an approval-bound regular file' }
109
- }
110
-
111
- return {
112
- files: configFiles.filter(filename => {
113
- return path.dirname(filename) === effectiveRoot && CONFIG_PATTERN.test(path.basename(filename))
114
- }),
115
- }
116
- }
117
-
118
- function getBindingFromObject ({
119
- bindingRoot,
120
- configFile,
121
- projectFiles,
122
- projectEntry,
123
- projectObject,
124
- projectRoot,
125
- standaloneProject,
126
- }) {
127
- if (/\.\.\./.test(maskJavaScriptNonCode(projectEntry))) {
128
- return { error: 'the selected Vitest project uses dynamic spread composition' }
129
- }
130
-
131
- const rootProperty = getLiteralProperty(projectObject, 'root')
132
- if (rootProperty.dynamic) return { error: 'the selected Vitest project has a dynamic root' }
133
-
134
- const rootBase = standaloneProject ? path.dirname(configFile) : bindingRoot
135
- const root = rootProperty.value
136
- ? path.resolve(rootBase, rootProperty.value)
137
- : standaloneProject
138
- ? path.dirname(configFile)
139
- : bindingRoot
140
- if (!isContainedDirectory(projectRoot, root)) {
141
- return { error: 'the selected Vitest project root is not a repository-contained directory' }
142
- }
143
-
144
- const include = getLiteralStringArray(projectObject, 'include')
145
- if (include.dynamic) return { error: 'the selected Vitest project has dynamic include patterns' }
146
- const exclude = getLiteralStringArray(projectObject, 'exclude')
147
- if (exclude.dynamic) return { error: 'the selected Vitest project has dynamic exclude patterns' }
148
- const project = {
149
- excludePatterns: exclude.values,
150
- includePatterns: include.values,
151
- root,
152
- }
153
- const files = projectFiles.filter(filename => matchesProjectFile(project, filename))
154
- return { configFile, files, ...project }
155
- }
156
-
157
- function supportsGeneratedFiles (project, strategy) {
158
- return strategy.scenarios.every(scenario => {
159
- return matchesProjectFile(project, scenario.testIdentities[0].file)
160
- })
161
- }
162
-
163
- function matchesProjectFile (project, filename) {
164
- const relative = path.relative(project.root, filename)
165
- if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) return false
166
- const normalized = relative.replaceAll('\\', '/')
167
- const included = project.includePatterns.length === 0 ||
168
- project.includePatterns.some(pattern => matchesLiteralGlob(normalized, pattern))
169
- return included && !project.excludePatterns.some(pattern => matchesLiteralGlob(normalized, pattern))
170
- }
171
-
172
- /**
173
- * Returns the literal project object that owns a matching name property.
174
- *
175
- * @param {string} source Vitest configuration source
176
- * @param {number} nameIndex matching name property offset
177
- * @param {boolean} workspaceConfig whether the source is a Vitest workspace config
178
- * @returns {{entrySource: string, source: string, standalone: boolean}|undefined} bounded project object
179
- */
180
- function getProjectObject (source, nameIndex, workspaceConfig) {
181
- const objectRanges = getObjectRanges(source)
182
- const ranges = objectRanges
183
- .filter(range => range.start < nameIndex && range.end > nameIndex)
184
- .sort((left, right) => (left.end - left.start) - (right.end - right.start))
185
- if (ranges.length === 0) return
186
-
187
- // Vitest accepts both `{ name }` and `{ test: { name } }` project entries.
188
- const inner = ranges[0]
189
- const parent = ranges[1]
190
- const parentPrefix = parent && maskJavaScriptComments(source.slice(parent.start + 1, inner.start))
191
- const nestedTestObject = /(?:^|,)\s*(?:test|(["'])test\1)\s*:\s*$/.test(parentPrefix || '')
192
- if (nestedTestObject &&
193
- getDirectPropertyPositions(source.slice(parent.start + 1, parent.end), 'test').length !== 1) return
194
- const selected = nestedTestObject ? parent : inner
195
- const standalone = isStandaloneProjectObject(source, selected)
196
- if (!standalone &&
197
- !isTestProjectsEntry(source, selected, objectRanges) &&
198
- !(workspaceConfig && isWorkspaceProjectEntry(source, selected))) return
199
- return {
200
- entrySource: source.slice(selected.start + 1, selected.end),
201
- source: source.slice(inner.start + 1, inner.end),
202
- standalone,
203
- }
204
- }
205
-
206
- /**
207
- * Reports whether an object is the direct literal argument to defineProject.
208
- *
209
- * @param {string} source Vitest configuration source
210
- * @param {{end: number, start: number}} range candidate object range
211
- */
212
- function isStandaloneProjectObject (source, range) {
213
- const before = maskJavaScriptNonCode(source.slice(0, range.start))
214
- const after = maskJavaScriptNonCode(source.slice(range.end + 1))
215
- return PROJECT_CALL_PATTERN.test(before) && /^\s*(?:,\s*)?\)[\s;]*$/.test(after)
216
- }
217
-
218
- /**
219
- * Reports whether an object is a direct entry in a literal test.projects array.
220
- *
221
- * @param {string} source Vitest configuration source
222
- * @param {{end: number, start: number}} range candidate object range
223
- * @param {{end: number, start: number}[]} objectRanges source object ranges
224
- */
225
- function isTestProjectsEntry (source, range, objectRanges) {
226
- const projectsArray = getDirectContainingArray(source, range)
227
- if (!projectsArray) return false
228
-
229
- const containers = objectRanges
230
- .filter(object => object.start < projectsArray.start && object.end > projectsArray.end)
231
- .sort((left, right) => (left.end - left.start) - (right.end - right.start))
232
- const testObject = containers[0]
233
- const configObject = containers[1]
234
- if (!testObject || !configObject) return false
235
-
236
- const projectsPrefix = maskJavaScriptComments(source.slice(testObject.start + 1, projectsArray.start))
237
- if (!/(?:^|,)\s*(?:projects|"projects"|'projects')\s*:\s*$/.test(projectsPrefix) ||
238
- getDirectPropertyPositions(
239
- source.slice(testObject.start + 1, testObject.end),
240
- 'projects'
241
- ).length !== 1) return false
242
-
243
- const testPrefix = maskJavaScriptComments(source.slice(configObject.start + 1, testObject.start))
244
- return /(?:^|,)\s*(?:test|"test"|'test')\s*:\s*$/.test(testPrefix) &&
245
- getDirectPropertyPositions(source.slice(configObject.start + 1, configObject.end), 'test').length === 1 &&
246
- isExportedConfigObject(source, configObject)
247
- }
248
-
249
- /**
250
- * Reports whether an object is the direct exported config or defineConfig argument.
251
- *
252
- * @param {string} source Vitest configuration source
253
- * @param {{end: number, start: number}} range candidate config object range
254
- */
255
- function isExportedConfigObject (source, range) {
256
- const before = maskJavaScriptNonCode(source.slice(0, range.start))
257
- const after = maskJavaScriptNonCode(source.slice(range.end + 1))
258
- return (DIRECT_EXPORT_PATTERN.test(before) && /^[\s;]*$/.test(after)) ||
259
- (CONFIG_CALL_PATTERN.test(before) && /^\s*(?:,\s*)?\)[\s;]*$/.test(after))
260
- }
261
-
262
- /**
263
- * Reports whether an object is a direct entry in the exported array of a Vitest workspace config.
264
- *
265
- * @param {string} source Vitest workspace configuration source
266
- * @param {{end: number, start: number}} range candidate object range
267
- */
268
- function isWorkspaceProjectEntry (source, range) {
269
- const workspaceArray = getDirectContainingArray(source, range)
270
- if (!workspaceArray) return false
271
-
272
- const before = maskJavaScriptNonCode(source.slice(0, workspaceArray.start))
273
- const after = maskJavaScriptNonCode(source.slice(workspaceArray.end + 1))
274
- return (DIRECT_EXPORT_PATTERN.test(before) && /^[\s;]*$/.test(after)) ||
275
- (WORKSPACE_ARRAY_CALL_PATTERN.test(before) && /^\s*(?:,\s*)?\)[\s;]*$/.test(after))
276
- }
277
-
278
- /**
279
- * Returns the smallest array containing an object as a direct entry.
280
- *
281
- * @param {string} source JavaScript-like source
282
- * @param {{end: number, start: number}} range candidate object range
283
- * @returns {{end: number, start: number}|undefined} containing direct array
284
- */
285
- function getDirectContainingArray (source, range) {
286
- const arrays = getDelimitedRanges(source, '[', ']')
287
- .filter(array => array.start < range.start && array.end > range.end)
288
- .sort((left, right) => (left.end - left.start) - (right.end - right.start))
289
- const directArray = arrays[0]
290
- if (!directArray) return
291
-
292
- const entryPrefix = maskJavaScriptComments(source.slice(directArray.start, range.start)).trimEnd()
293
- const entrySuffix = maskJavaScriptComments(source.slice(range.end + 1, directArray.end + 1)).trimStart()
294
- if (['[', ','].includes(entryPrefix.at(-1)) && [']', ','].includes(entrySuffix[0])) return directArray
295
- if (/(?:^|\[|,)\s*defineProject\s*\(\s*$/.test(entryPrefix) && /^,?\s*\)\s*[\],]/.test(entrySuffix)) {
296
- return directArray
297
- }
298
- }
299
-
300
- /**
301
- * Finds balanced object ranges while ignoring comments and strings.
302
- *
303
- * @param {string} source JavaScript-like source
304
- * @returns {{end: number, start: number}[]} object ranges
305
- */
306
- function getObjectRanges (source) {
307
- return getDelimitedRanges(source, '{', '}')
308
- }
309
-
310
- /**
311
- * Finds balanced delimiter ranges while ignoring comments and strings.
312
- *
313
- * @param {string} source JavaScript-like source
314
- * @param {string} open opening delimiter
315
- * @param {string} close closing delimiter
316
- * @returns {{end: number, start: number}[]} delimiter ranges
317
- */
318
- function getDelimitedRanges (source, open, close) {
319
- const ranges = []
320
- const stack = []
321
- let quote
322
- let lineComment = false
323
- let blockComment = false
324
- for (let index = 0; index < source.length; index++) {
325
- const character = source[index]
326
- const next = source[index + 1]
327
- if (lineComment) {
328
- if (character === '\n') lineComment = false
329
- continue
330
- }
331
- if (blockComment) {
332
- if (character === '*' && next === '/') {
333
- blockComment = false
334
- index++
335
- }
336
- continue
337
- }
338
- if (quote) {
339
- if (character === '\\') index++
340
- else if (character === quote) quote = undefined
341
- continue
342
- }
343
- if (character === '/' && next === '/') {
344
- lineComment = true
345
- index++
346
- } else if (character === '/' && next === '*') {
347
- blockComment = true
348
- index++
349
- } else if (character === '"' || character === "'" || character === '`') {
350
- quote = character
351
- } else if (character === open) {
352
- stack.push(index)
353
- } else if (character === close) {
354
- const start = stack.pop()
355
- if (start !== undefined) ranges.push({ end: index, start })
356
- }
357
- }
358
- return ranges
359
- }
360
-
361
- function getLiteralStringProperties (source, property) {
362
- return getPropertyPositions(source, property).flatMap(({ index, valueStart }) => {
363
- const literal = /^(["'])([^"'\\]+)\1/.exec(source.slice(valueStart))
364
- return literal ? { index, value: literal[2] } : []
365
- })
366
- }
367
-
368
- function getPropertyPositions (source, property) {
369
- const properties = []
370
- let quote
371
- let lineComment = false
372
- let blockComment = false
373
- let previousCodeCharacter = '{'
374
- for (let index = 0; index < source.length; index++) {
375
- const character = source[index]
376
- const next = source[index + 1]
377
- if (lineComment) {
378
- if (character === '\n') lineComment = false
379
- continue
380
- }
381
- if (blockComment) {
382
- if (character === '*' && next === '/') {
383
- blockComment = false
384
- index++
385
- }
386
- continue
387
- }
388
- if (quote) {
389
- if (character === '\\') index++
390
- else if (character === quote) quote = undefined
391
- continue
392
- }
393
- if (character === '/' && next === '/') {
394
- lineComment = true
395
- index++
396
- continue
397
- }
398
- if (character === '/' && next === '*') {
399
- blockComment = true
400
- index++
401
- continue
402
- }
403
- if (character === '"' || character === "'" || character === '`') {
404
- const match = new RegExp(String.raw`^(["'])${property}\1\s*:\s*`).exec(source.slice(index))
405
- if (match && ['{', ','].includes(previousCodeCharacter)) {
406
- properties.push({ index, valueStart: index + match[0].length })
407
- previousCodeCharacter = ':'
408
- index += match[0].length - 1
409
- continue
410
- }
411
- quote = character
412
- continue
413
- }
414
- if (!source.startsWith(property, index) ||
415
- !['{', ','].includes(previousCodeCharacter) ||
416
- /[A-Za-z0-9_$]/.test(source[index - 1] || '') ||
417
- /[A-Za-z0-9_$]/.test(source[index + property.length] || '')) {
418
- if (!/\s/.test(character)) previousCodeCharacter = character
419
- continue
420
- }
421
-
422
- const match = new RegExp(String.raw`^${property}\s*:\s*`).exec(source.slice(index))
423
- if (!match) {
424
- previousCodeCharacter = character
425
- continue
426
- }
427
- properties.push({ index, valueStart: index + match[0].length })
428
- previousCodeCharacter = ':'
429
- index += match[0].length - 1
430
- }
431
- return properties
432
- }
433
-
434
- function getLiteralProperty (source, property) {
435
- const properties = getDirectPropertyPositions(source, property)
436
- if (properties.length === 0) return {}
437
- if (properties.length !== 1) return { dynamic: true }
438
- const literal = /^(["'])([^"'\\]+)\1/.exec(source.slice(properties[0].valueStart))
439
- if (literal) return { value: literal[2] }
440
- return { dynamic: true }
441
- }
442
-
443
- function getLiteralStringArray (source, property) {
444
- const properties = getDirectPropertyPositions(source, property)
445
- if (properties.length === 0) return { values: [] }
446
- if (properties.length !== 1) return { dynamic: true, values: [] }
447
- const arrayMatch = /^\[([\s\S]{0,8192}?)\]/.exec(source.slice(properties[0].valueStart))
448
- if (!arrayMatch) return { dynamic: true, values: [] }
449
- const syntax = maskJavaScriptComments(arrayMatch[1])
450
- const values = [...syntax.matchAll(/(["'])([^"'\\]+)\1/g)].map(match => match[2])
451
- const residue = syntax.replaceAll(/(["'])([^"'\\]+)\1/g, '').replaceAll(',', '').trim()
452
- return residue ? { dynamic: true, values: [] } : { values }
453
- }
454
-
455
- /**
456
- * Returns matching properties that are not nested inside another object.
457
- *
458
- * @param {string} source JavaScript object contents
459
- * @param {string} property property name
460
- * @returns {{index: number, valueStart: number}[]} direct property positions
461
- */
462
- function getDirectPropertyPositions (source, property) {
463
- const nestedObjects = getObjectRanges(source)
464
- return getPropertyPositions(source, property).filter(position => {
465
- return !nestedObjects.some(object => object.start < position.index && object.end > position.index)
466
- })
467
- }
468
-
469
- function getOptionValues (args, expected) {
470
- const values = []
471
- for (let index = 0; index < args.length; index++) {
472
- if (args[index].split('=', 1)[0] !== expected) continue
473
- values.push(args[index].includes('=') ? args[index].slice(args[index].indexOf('=') + 1) : args[++index])
474
- }
475
- return values.filter(Boolean)
476
- }
477
-
478
- function isContainedDirectory (root, candidate) {
479
- try {
480
- const physicalRoot = fs.realpathSync(root)
481
- const physicalCandidate = fs.realpathSync(candidate)
482
- const relative = path.relative(physicalRoot, physicalCandidate)
483
- return fs.statSync(physicalCandidate).isDirectory() &&
484
- (relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative)))
485
- } catch {
486
- return false
487
- }
488
- }
489
-
490
- function readText (filename) {
491
- try {
492
- const stat = fs.statSync(filename)
493
- if (!stat.isFile() || stat.size > 512 * 1024) return
494
- return fs.readFileSync(filename, 'utf8')
495
- } catch {}
496
- }
497
-
498
- function getPhysicalPath (filename) {
499
- try {
500
- const stat = fs.lstatSync(filename)
501
- if (!stat.isFile() || stat.isSymbolicLink()) return
502
- return fs.realpathSync(filename)
503
- } catch {}
504
- }
505
-
506
- function getPhysicalDirectory (directory) {
507
- try {
508
- const physical = fs.realpathSync(directory)
509
- if (fs.statSync(physical).isDirectory()) return physical
510
- } catch {}
511
- }
512
-
513
- module.exports = { bindLiteralProject, supportsGeneratedFiles }
@@ -1,63 +0,0 @@
1
- 'use strict'
2
-
3
- const { hasUnsafeInvisibleCharacter, sanitizeString } = require('./redaction')
4
-
5
- const MAX_GENERATED_FILES = 8
6
- const MAX_GENERATED_FILE_BYTES = 32 * 1024
7
- const MAX_GENERATED_FILE_LINES = 256
8
- const MAX_GENERATED_LINE_BYTES = 4096
9
-
10
- /**
11
- * Returns why generated source is unsafe to write and execute.
12
- *
13
- * @param {unknown} contentLines generated source lines
14
- * @returns {string|undefined} policy violation
15
- */
16
- function getGeneratedFileContentError (contentLines) {
17
- if (!Array.isArray(contentLines) || contentLines.some(line => typeof line !== 'string')) return
18
- if (contentLines.length > MAX_GENERATED_FILE_LINES) {
19
- return `must contain at most ${MAX_GENERATED_FILE_LINES} lines`
20
- }
21
-
22
- let totalBytes = 1
23
- for (const line of contentLines) {
24
- if (line.includes('\n') || line.includes('\r') || hasUnsafeControlCharacter(line)) {
25
- return 'must use one printable source line per contentLines entry'
26
- }
27
- if (Buffer.byteLength(line) > MAX_GENERATED_LINE_BYTES) {
28
- return `must not contain a line larger than ${MAX_GENERATED_LINE_BYTES} bytes`
29
- }
30
- if (sanitizeString(line) !== line) {
31
- return 'must contain only synthetic source and no secret-like values'
32
- }
33
- totalBytes += Buffer.byteLength(line) + 1
34
- }
35
-
36
- if (totalBytes > MAX_GENERATED_FILE_BYTES) {
37
- return `must be at most ${MAX_GENERATED_FILE_BYTES} bytes`
38
- }
39
-
40
- const source = contentLines.join('\n')
41
- if (/\bwriteFileSync\s*\(/.test(source) && /\bnew\s+URL\s*\(/.test(source)) {
42
- return 'must derive generated state-file paths from fileURLToPath(import.meta.url) without new URL because ' +
43
- 'browser-like test environments may replace the global URL constructor'
44
- }
45
- }
46
-
47
- function hasUnsafeControlCharacter (value) {
48
- if (hasUnsafeInvisibleCharacter(value)) return true
49
-
50
- for (const character of value) {
51
- const code = character.charCodeAt(0)
52
- if (code <= 0x08 || code === 0x0B || code === 0x0C || (code >= 0x0E && code <= 0x1F) ||
53
- code === 0x7F) {
54
- return true
55
- }
56
- }
57
- return false
58
- }
59
-
60
- module.exports = {
61
- MAX_GENERATED_FILES,
62
- getGeneratedFileContentError,
63
- }