dd-trace 6.24.0 → 6.26.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 (352) hide show
  1. package/README.md +3 -0
  2. package/ci/cypress/support.js +2 -1
  3. package/ci/init.js +2 -22
  4. package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
  5. package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
  6. package/ci/vitest-no-worker-init-setup.mjs +729 -724
  7. package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
  8. package/ext/exporters.js +0 -1
  9. package/index.d.ts +105 -5
  10. package/initialize.mjs +5 -2
  11. package/package.json +41 -36
  12. package/packages/datadog-esbuild/index.js +16 -70
  13. package/packages/datadog-esbuild/src/log.js +2 -29
  14. package/packages/datadog-instrumentations/src/ai.js +6 -1
  15. package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
  16. package/packages/datadog-instrumentations/src/cucumber.js +5 -2
  17. package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
  18. package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
  19. package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
  20. package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
  21. package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
  22. package/packages/datadog-instrumentations/src/graphql.js +20 -17
  23. package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +582 -0
  24. package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
  25. package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +576 -0
  26. package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
  27. package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
  28. package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
  29. package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -7
  30. package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
  31. package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +8 -0
  32. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +26 -1
  33. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
  34. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
  35. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
  36. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
  37. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
  38. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +61 -0
  39. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
  40. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
  41. package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
  42. package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +80 -0
  43. package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +23 -3
  44. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
  45. package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
  46. package/packages/datadog-instrumentations/src/ioredis.js +101 -3
  47. package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
  48. package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
  49. package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
  50. package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
  51. package/packages/datadog-instrumentations/src/next.js +203 -26
  52. package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
  53. package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
  54. package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
  55. package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
  56. package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
  57. package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
  58. package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
  59. package/packages/datadog-instrumentations/src/router.js +35 -12
  60. package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
  61. package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
  62. package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
  63. package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
  64. package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
  65. package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
  66. package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
  67. package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
  68. package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
  69. package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
  70. package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
  71. package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
  72. package/packages/datadog-plugin-cypress/src/cypress-plugin.js +50 -13
  73. package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
  74. package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
  75. package/packages/datadog-plugin-express/src/code_origin.js +2 -2
  76. package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
  77. package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +20 -7
  78. package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +21 -52
  79. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +11 -2
  80. package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
  81. package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
  82. package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
  83. package/packages/datadog-plugin-http/src/client.js +34 -10
  84. package/packages/datadog-plugin-http2/src/client.js +40 -14
  85. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
  86. package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
  87. package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
  88. package/packages/datadog-plugin-next/src/index.js +112 -14
  89. package/packages/datadog-plugin-postgres/src/index.js +118 -41
  90. package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
  91. package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
  92. package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
  93. package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
  94. package/packages/datadog-plugin-trpc/src/index.js +15 -0
  95. package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
  96. package/packages/datadog-plugin-trpc/src/request.js +81 -0
  97. package/packages/datadog-plugin-undici/src/index.js +22 -7
  98. package/packages/datadog-plugin-vitest/src/index.js +3 -2
  99. package/packages/datadog-plugin-ws/src/close.js +3 -5
  100. package/packages/datadog-plugin-ws/src/producer.js +1 -4
  101. package/packages/datadog-plugin-ws/src/receiver.js +3 -5
  102. package/packages/datadog-plugin-ws/src/server.js +44 -13
  103. package/packages/datadog-turbopack/index.js +4 -28
  104. package/packages/datadog-turbopack/src/loader.js +14 -452
  105. package/packages/datadog-vite/index.js +537 -0
  106. package/packages/datadog-vite/src/log.js +5 -0
  107. package/packages/datadog-webpack/index.js +3 -55
  108. package/packages/datadog-webpack/src/log.js +2 -29
  109. package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
  110. package/packages/dd-trace/src/aiguard/index.js +1 -1
  111. package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
  112. package/packages/dd-trace/src/aiguard/sdk.js +8 -2
  113. package/packages/dd-trace/src/aiguard/tags.js +2 -0
  114. package/packages/dd-trace/src/appsec/blocking/index.js +21 -1
  115. package/packages/dd-trace/src/appsec/channels.js +7 -0
  116. package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
  117. package/packages/dd-trace/src/appsec/graphql.js +89 -7
  118. package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
  119. package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
  120. package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
  121. package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
  122. package/packages/dd-trace/src/appsec/index.js +6 -0
  123. package/packages/dd-trace/src/appsec/lambda.js +17 -17
  124. package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
  125. package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
  126. package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
  127. package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
  128. package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
  129. package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
  130. package/packages/dd-trace/src/appsec/recommended.json +1358 -94
  131. package/packages/dd-trace/src/appsec/reporter.js +9 -0
  132. package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
  133. package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
  134. package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
  135. package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
  136. package/packages/dd-trace/src/appsec/waf/index.js +65 -6
  137. package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
  138. package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
  139. package/packages/dd-trace/src/before-exit.js +32 -0
  140. package/packages/dd-trace/src/bootstrap.js +5 -3
  141. package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +15 -2
  142. package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +12 -34
  143. package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
  144. package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
  145. package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +1 -2
  146. package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +1 -1
  147. package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
  148. package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +1 -1
  149. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
  150. package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
  151. package/packages/dd-trace/src/config/config-types.d.ts +3 -0
  152. package/packages/dd-trace/src/config/defaults.js +5 -0
  153. package/packages/dd-trace/src/config/generated-config-types.d.ts +46 -4
  154. package/packages/dd-trace/src/config/index.js +63 -31
  155. package/packages/dd-trace/src/config/parsers.js +44 -0
  156. package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
  157. package/packages/dd-trace/src/config/remote_config.js +2 -1
  158. package/packages/dd-trace/src/config/supported-configurations.json +172 -10
  159. package/packages/dd-trace/src/constants.js +1 -0
  160. package/packages/dd-trace/src/datastreams/fnv.js +27 -0
  161. package/packages/dd-trace/src/datastreams/pathway.js +113 -28
  162. package/packages/dd-trace/src/datastreams/processor.js +5 -4
  163. package/packages/dd-trace/src/debugger/config.js +1 -1
  164. package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
  165. package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
  166. package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
  167. package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
  168. package/packages/dd-trace/src/debugger/index.js +2 -2
  169. package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
  170. package/packages/dd-trace/src/debugger/pause-duration-histogram.js +6 -2
  171. package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
  172. package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
  173. package/packages/dd-trace/src/exporter.js +0 -11
  174. package/packages/dd-trace/src/exporters/common/request.js +81 -48
  175. package/packages/dd-trace/src/exporters/common/retry.js +12 -7
  176. package/packages/dd-trace/src/exporters/common/url.js +1 -1
  177. package/packages/dd-trace/src/git_metadata.js +2 -1
  178. package/packages/dd-trace/src/id.js +1 -1
  179. package/packages/dd-trace/src/index.js +6 -3
  180. package/packages/dd-trace/src/lambda/facade.js +75 -0
  181. package/packages/dd-trace/src/lambda/handler.js +3 -41
  182. package/packages/dd-trace/src/lambda/index.js +38 -15
  183. package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
  184. package/packages/dd-trace/src/lambda/wrap.js +44 -0
  185. package/packages/dd-trace/src/llmobs/constants/tags.js +2 -0
  186. package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
  187. package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
  188. package/packages/dd-trace/src/llmobs/plugins/genai/util.js +19 -6
  189. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
  190. package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
  191. package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
  192. package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
  193. package/packages/dd-trace/src/llmobs/sdk.js +19 -0
  194. package/packages/dd-trace/src/llmobs/span_processor.js +9 -0
  195. package/packages/dd-trace/src/llmobs/tagger.js +22 -0
  196. package/packages/dd-trace/src/log/channels.js +4 -0
  197. package/packages/dd-trace/src/log/writer.js +6 -1
  198. package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +7 -0
  199. package/packages/dd-trace/src/openfeature/configuration_source.js +55 -27
  200. package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
  201. package/packages/dd-trace/src/openfeature/flagging_provider.js +19 -1
  202. package/packages/dd-trace/src/openfeature/writers/base.js +1 -3
  203. package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
  204. package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
  205. package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
  206. package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
  207. package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
  208. package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
  209. package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
  210. package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
  211. package/packages/dd-trace/src/opentelemetry/span.js +2 -2
  212. package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
  213. package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
  214. package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
  215. package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
  216. package/packages/dd-trace/src/opentracing/tracer.js +24 -0
  217. package/packages/dd-trace/src/otel-thread-ctx.js +28 -31
  218. package/packages/dd-trace/src/plugin_manager.js +34 -7
  219. package/packages/dd-trace/src/plugins/ci_plugin.js +143 -17
  220. package/packages/dd-trace/src/plugins/index.js +8 -0
  221. package/packages/dd-trace/src/plugins/plugin.js +4 -2
  222. package/packages/dd-trace/src/plugins/tracing.js +1 -1
  223. package/packages/dd-trace/src/plugins/util/ci.js +52 -1
  224. package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
  225. package/packages/dd-trace/src/plugins/util/git.js +263 -82
  226. package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +80 -56
  227. package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
  228. package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
  229. package/packages/dd-trace/src/plugins/util/tags.js +6 -0
  230. package/packages/dd-trace/src/plugins/util/test.js +92 -40
  231. package/packages/dd-trace/src/plugins/util/url.js +18 -7
  232. package/packages/dd-trace/src/plugins/util/user-provided-git.js +6 -6
  233. package/packages/dd-trace/src/plugins/util/web.js +65 -10
  234. package/packages/dd-trace/src/process-tags/index.js +1 -1
  235. package/packages/dd-trace/src/profiler.js +74 -12
  236. package/packages/dd-trace/src/profiling/profiler.js +30 -2
  237. package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
  238. package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
  239. package/packages/dd-trace/src/profiling/remote-config.js +354 -0
  240. package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
  241. package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
  242. package/packages/dd-trace/src/propagation-hash/index.js +7 -0
  243. package/packages/dd-trace/src/proxy.js +46 -54
  244. package/packages/dd-trace/src/span_processor.js +20 -0
  245. package/packages/dd-trace/src/startup-log.js +8 -6
  246. package/packages/dd-trace/src/telemetry/metrics.js +2 -1
  247. package/packages/dd-trace/src/tracer.js +0 -2
  248. package/packages/dd-trace/src/web-tags-cache.js +8 -9
  249. package/register.js +2 -1
  250. package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
  251. package/vendor/dist/@datadog/flagging-core/LICENSE +221 -0
  252. package/vendor/dist/@datadog/flagging-core/index.js +1 -0
  253. package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
  254. package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
  255. package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
  256. package/vendor/dist/@datadog/source-map/LICENSE +1 -0
  257. package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
  258. package/vendor/dist/@opentelemetry/core/LICENSE +220 -1
  259. package/vendor/dist/@opentelemetry/core/index.js +1 -1
  260. package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
  261. package/vendor/dist/escape-string-regexp/LICENSE +13 -0
  262. package/vendor/dist/esquery/LICENSE +2 -0
  263. package/vendor/dist/https-proxy-agent/LICENSE +80 -1
  264. package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
  265. package/vendor/dist/jest-docblock/LICENSE +16 -0
  266. package/vendor/dist/jsonpath-plus/LICENSE +13 -0
  267. package/vendor/dist/limiter/LICENSE +2 -0
  268. package/vendor/dist/lodash.sortby/LICENSE +2 -0
  269. package/vendor/dist/lru-cache/LICENSE +13 -0
  270. package/vendor/dist/meriyah/LICENSE +13 -0
  271. package/vendor/dist/module-details-from-path/LICENSE +2 -0
  272. package/vendor/dist/mutexify/promise/LICENSE +28 -0
  273. package/vendor/dist/pprof-format/LICENSE +13 -0
  274. package/vendor/dist/pprof-format/index.js +1 -1
  275. package/vendor/dist/protobufjs/LICENSE +219 -0
  276. package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
  277. package/vendor/dist/proxy-from-env/LICENSE +13 -0
  278. package/vendor/dist/retry/LICENSE +2 -0
  279. package/vendor/dist/rfdc/LICENSE +2 -0
  280. package/vendor/dist/semifies/LICENSE +3 -1
  281. package/vendor/dist/shell-quote/LICENSE +3 -1
  282. package/vendor/dist/source-map/LICENSE +1 -0
  283. package/vendor/dist/source-map/lib/util/LICENSE +1 -0
  284. package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
  285. package/vendor/dist/ttl-set/LICENSE +28 -0
  286. package/vite.d.ts +7 -0
  287. package/vite.js +3 -0
  288. package/ci/diagnose.js +0 -2215
  289. package/ci/runbook.md +0 -299
  290. package/ci/test-optimization-validation/approval-artifacts.js +0 -175
  291. package/ci/test-optimization-validation/approval.js +0 -386
  292. package/ci/test-optimization-validation/artifact-id.js +0 -18
  293. package/ci/test-optimization-validation/blocker-category.js +0 -24
  294. package/ci/test-optimization-validation/bounded-json.js +0 -120
  295. package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
  296. package/ci/test-optimization-validation/ci-discovery.js +0 -192
  297. package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
  298. package/ci/test-optimization-validation/ci-remediation.js +0 -283
  299. package/ci/test-optimization-validation/cli.js +0 -954
  300. package/ci/test-optimization-validation/command-blocker.js +0 -377
  301. package/ci/test-optimization-validation/command-output-policy.js +0 -188
  302. package/ci/test-optimization-validation/command-runner.js +0 -649
  303. package/ci/test-optimization-validation/environment.js +0 -88
  304. package/ci/test-optimization-validation/executable-approval.js +0 -47
  305. package/ci/test-optimization-validation/executable.js +0 -244
  306. package/ci/test-optimization-validation/execution-lock.js +0 -70
  307. package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
  308. package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
  309. package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
  310. package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
  311. package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
  312. package/ci/test-optimization-validation/generated-file-policy.js +0 -63
  313. package/ci/test-optimization-validation/generated-files.js +0 -425
  314. package/ci/test-optimization-validation/generated-test-contract.js +0 -284
  315. package/ci/test-optimization-validation/generated-verifier.js +0 -235
  316. package/ci/test-optimization-validation/literal-environment.js +0 -56
  317. package/ci/test-optimization-validation/literal-glob.js +0 -52
  318. package/ci/test-optimization-validation/manifest-loader.js +0 -116
  319. package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
  320. package/ci/test-optimization-validation/manifest-schema.js +0 -695
  321. package/ci/test-optimization-validation/offline-fixtures.js +0 -335
  322. package/ci/test-optimization-validation/offline-output.js +0 -406
  323. package/ci/test-optimization-validation/package-check.js +0 -97
  324. package/ci/test-optimization-validation/payload-normalizer.js +0 -72
  325. package/ci/test-optimization-validation/plan-writer.js +0 -541
  326. package/ci/test-optimization-validation/preflight-runner.js +0 -186
  327. package/ci/test-optimization-validation/project-build-artifact.js +0 -31
  328. package/ci/test-optimization-validation/redaction.js +0 -319
  329. package/ci/test-optimization-validation/report-writer.js +0 -666
  330. package/ci/test-optimization-validation/result-semantics.js +0 -103
  331. package/ci/test-optimization-validation/runner-command.js +0 -264
  332. package/ci/test-optimization-validation/runner-contract.js +0 -695
  333. package/ci/test-optimization-validation/safe-files.js +0 -202
  334. package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
  335. package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
  336. package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
  337. package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
  338. package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
  339. package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
  340. package/ci/test-optimization-validation/source-text.js +0 -84
  341. package/ci/test-optimization-validation/static-diagnosis.js +0 -166
  342. package/ci/test-optimization-validation/test-output.js +0 -120
  343. package/ci/test-optimization-validation/validation-blocker.js +0 -21
  344. package/ci/validate-test-optimization.js +0 -3
  345. package/packages/datadog-esbuild/src/resolver.js +0 -284
  346. package/packages/datadog-esbuild/src/utils.js +0 -318
  347. package/packages/datadog-plugin-cypress/src/support.js +0 -1348
  348. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
  349. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
  350. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
  351. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
  352. package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
@@ -1,695 +0,0 @@
1
- 'use strict'
2
-
3
- const fs = require('node:fs')
4
- const path = require('node:path')
5
-
6
- const { environmentNamesEqual, setEnvironmentValue } = require('./environment')
7
-
8
- const RUNNER_OPTIONS = {
9
- cucumber: {
10
- flags: new Set(['--backtrace', '--fail-fast', '--no-strict', '--strict']),
11
- values: new Set([
12
- '-p', '--config', '--import', '--language', '--loader', '--profile', '--require', '--require-module',
13
- '--parallel', '--world-parameters',
14
- ]),
15
- },
16
- cypress: {
17
- flags: new Set(['--component', '--e2e', '--headed', '--headless']),
18
- values: new Set(['--browser', '--config-file']),
19
- },
20
- jest: {
21
- flags: new Set(['--color', '--colors', '--detectLeaks']),
22
- values: new Set(['-c', '--config', '--env', '--runner', '--testEnvironment']),
23
- },
24
- mocha: {
25
- flags: new Set(['--check-leaks', '--enable-source-maps']),
26
- values: new Set(['-r', '-t', '-u', '--config', '--extension', '--loader', '--require', '--timeout', '--ui']),
27
- },
28
- playwright: {
29
- flags: new Set(),
30
- values: new Set(['-c', '--config', '--project']),
31
- },
32
- vitest: {
33
- flags: new Set(['--browser']),
34
- values: new Set(['--config', '--environment', '--project', '--root']),
35
- },
36
- }
37
- const IGNORED_RUNNER_OPTIONS = {
38
- mocha: {
39
- flags: new Set(),
40
- values: new Set(['-R', '--reporter']),
41
- },
42
- }
43
- const OMITTED_RUNNER_OPTIONS = {
44
- vitest: new Set(['--run', '--typecheck']),
45
- }
46
- const SOURCE_SELECTOR_OPTIONS = {
47
- cypress: new Set(['--spec']),
48
- }
49
- const RUNNER_ENVIRONMENT_NAMES = new Set([
50
- 'BABEL_ENV',
51
- 'CI',
52
- 'NODE_ENV',
53
- 'TS_NODE_PROJECT',
54
- 'TZ',
55
- ])
56
- const RUNNER_LAUNCHERS = new Set(['c8', 'cross-env', 'env', 'npx', 'nyc', 'node'])
57
- const MODULE_OPTIONS = new Set([
58
- '-r',
59
- '-u',
60
- '--env',
61
- '--environment',
62
- '--import',
63
- '--loader',
64
- '--require',
65
- '--require-module',
66
- '--runner',
67
- '--testEnvironment',
68
- '--ui',
69
- ])
70
- const FILE_OPTIONS = new Set(['-c', '--config', '--config-file'])
71
- const DIRECTORY_OPTIONS = new Set(['--root'])
72
- const UNSUPPORTED_CONFIGURATION_OPTIONS = {
73
- cypress: new Set(['--config']),
74
- jest: new Set(['--projects']),
75
- mocha: new Set(['--config']),
76
- }
77
- const BUILTIN_MODULE_VALUES = new Map([
78
- ['-u', new Set(['bdd', 'exports', 'qunit', 'tdd'])],
79
- ['--env', new Set(['node'])],
80
- ['--environment', new Set(['node'])],
81
- ['--testEnvironment', new Set(['node'])],
82
- ['--ui', new Set(['bdd', 'exports', 'qunit', 'tdd'])],
83
- ])
84
- const CONTROL_PATTERN = /[\0\r\n;&|`]|\$\(|\$\{/
85
- const ENVIRONMENT_EXPANSION_PATTERN = /\$|%[^%]+%|![^!]+!/
86
-
87
- /**
88
- * Extracts bounded runner configuration from a detected package script.
89
- *
90
- * @param {string} framework framework name
91
- * @param {string | undefined} command detected package script
92
- * @param {string} projectRoot detected project root
93
- * @param {string} repositoryRoot repository root
94
- * @param {string} [platform] target platform
95
- * @returns {{
96
- * environment: Record<string, string>,
97
- * error?: string,
98
- * inputFiles: string[],
99
- * runnerArgs: string[]
100
- * }} runner contract
101
- */
102
- function getRunnerContract (framework, command, projectRoot, repositoryRoot, platform = process.platform) {
103
- const dynamicEnvironmentError = getDynamicRunnerEnvironmentError(command)
104
- if (dynamicEnvironmentError) {
105
- return { environment: {}, error: dynamicEnvironmentError, inputFiles: [], runnerArgs: [] }
106
- }
107
- const invocation = getFrameworkInvocation(command, framework)
108
- if (!invocation) return { environment: {}, inputFiles: [], runnerArgs: [] }
109
- if (invocation.error) {
110
- return { environment: {}, error: invocation.error, inputFiles: [], runnerArgs: [] }
111
- }
112
- const unsupportedOption = getUnsupportedConfigurationOption(framework, invocation)
113
- if (unsupportedOption) {
114
- return {
115
- environment: {},
116
- error: `${unsupportedOption} has configuration semantics that the validator does not preserve`,
117
- inputFiles: [],
118
- runnerArgs: [],
119
- }
120
- }
121
- const unknownOption = getUnknownRunnerOption(framework, invocation)
122
- if (unknownOption) {
123
- return {
124
- environment: {},
125
- error: `${unknownOption} is not preserved by the validator direct-runner contract`,
126
- inputFiles: [],
127
- runnerArgs: [],
128
- }
129
- }
130
- const omittedOptions = getOmittedRunnerOptions(framework, invocation)
131
-
132
- const environment = getRunnerEnvironment(invocation, platform)
133
- const environmentError = getRunnerEnvironmentError(environment, platform)
134
- if (environmentError) {
135
- return { environment: {}, error: `runner environment ${environmentError}`, inputFiles: [], runnerArgs: [] }
136
- }
137
- const runnerArgs = getRunnerArgs(framework, invocation)
138
- const runnerArgsError = getRunnerArgsError(framework, runnerArgs)
139
- if (runnerArgsError) {
140
- return {
141
- environment: {},
142
- error: `runner arguments ${runnerArgsError}`,
143
- inputFiles: [],
144
- runnerArgs: [],
145
- }
146
- }
147
- const inputs = getRunnerInputs(runnerArgs, environment, projectRoot, repositoryRoot)
148
- if (inputs.error) {
149
- return { environment: {}, error: inputs.error, inputFiles: [], runnerArgs: [] }
150
- }
151
- return {
152
- environment,
153
- inputFiles: inputs.files,
154
- omittedOptions,
155
- runnerArgs,
156
- }
157
- }
158
-
159
- function getRunnerConfigurationContract (framework, runnerArgs, environment, projectRoot, repositoryRoot) {
160
- const runnerArgsError = getRunnerArgsError(framework, runnerArgs)
161
- if (runnerArgsError) {
162
- return {
163
- environment: {},
164
- error: `runner arguments ${runnerArgsError}`,
165
- inputFiles: [],
166
- runnerArgs: [],
167
- }
168
- }
169
- const inputs = getRunnerInputs(runnerArgs, environment, projectRoot, repositoryRoot)
170
- if (inputs.error) {
171
- return { environment: {}, error: inputs.error, inputFiles: [], runnerArgs: [] }
172
- }
173
- return { environment, inputFiles: inputs.files, runnerArgs }
174
- }
175
-
176
- /**
177
- * Rejects shell-expanded allowlisted environment assignments before command tokenization.
178
- *
179
- * @param {string | undefined} command detected package script
180
- * @returns {string|undefined} validation error
181
- */
182
- function getDynamicRunnerEnvironmentError (command) {
183
- const source = String(command)
184
- for (const match of source.matchAll(
185
- /(?:^|\s)([A-Za-z_][A-Za-z0-9_]*)=(?:"([^"]*)"|'([^']*)'|([^\s]+))/g
186
- )) {
187
- const value = match[2] ?? match[3] ?? match[4]
188
- if (RUNNER_ENVIRONMENT_NAMES.has(match[1]) && ENVIRONMENT_EXPANSION_PATTERN.test(value)) {
189
- return `runner environment contains an unsafe value for ${match[1]}`
190
- }
191
- }
192
- if (ENVIRONMENT_EXPANSION_PATTERN.test(source)) {
193
- return 'runner command contains shell-expanded values that the validator does not preserve'
194
- }
195
- }
196
-
197
- /**
198
- * Returns literal existing test roots selected by a direct framework invocation.
199
- *
200
- * @param {string} framework framework name
201
- * @param {string | undefined} command detected package script
202
- * @param {string} projectRoot detected project root
203
- * @param {string} repositoryRoot repository root
204
- * @returns {string[]} physical search roots
205
- */
206
- function getRunnerSearchRoots (framework, command, projectRoot, repositoryRoot) {
207
- const invocation = getFrameworkInvocation(command, framework)
208
- const options = RUNNER_OPTIONS[framework]
209
- if (!invocation || invocation.error || !options) return []
210
-
211
- const roots = new Set()
212
- const selectors = SOURCE_SELECTOR_OPTIONS[framework] || new Set()
213
- const tokens = invocation.tokens.slice(invocation.runnerIndex + 1)
214
- for (let index = 0; index < tokens.length; index++) {
215
- const token = tokens[index]
216
- if (token.startsWith('-')) {
217
- const option = token.split('=', 1)[0]
218
- if (DIRECTORY_OPTIONS.has(option)) {
219
- const value = token.includes('=') ? token.slice(token.indexOf('=') + 1) : tokens[++index]
220
- addRunnerSearchRoot(roots, value, projectRoot, repositoryRoot, true)
221
- } else if (selectors.has(option)) {
222
- const value = token.includes('=') ? token.slice(token.indexOf('=') + 1) : tokens[++index]
223
- addRunnerSearchRoot(roots, value, projectRoot, repositoryRoot, false)
224
- } else if (options.values.has(option) && !token.includes('=')) {
225
- index++
226
- }
227
- continue
228
- }
229
- if (['run', 'test'].includes(token)) continue
230
- addRunnerSearchRoot(roots, token, projectRoot, repositoryRoot, false)
231
- if (roots.size === 3) break
232
- }
233
- return [...roots]
234
- }
235
-
236
- /**
237
- * Adds one literal, contained command selector to the representative search roots.
238
- *
239
- * @param {Set<string>} roots accumulated physical roots
240
- * @param {string|undefined} value command selector
241
- * @param {string} projectRoot detected project root
242
- * @param {string} repositoryRoot repository root
243
- * @param {boolean} directory whether the option explicitly requires a directory
244
- */
245
- function addRunnerSearchRoot (roots, value, projectRoot, repositoryRoot, directory) {
246
- if (typeof value !== 'string' || !value) return
247
- const literal = value.split(/[*?{[]/, 1)[0].replace(/(?<![\\/])[\\/]+$/, '')
248
- if (!literal) return
249
- const candidate = path.resolve(projectRoot, literal)
250
- const searchRoot = directory || !path.extname(candidate) ? candidate : path.dirname(candidate)
251
- try {
252
- const physical = fs.realpathSync(searchRoot)
253
- if (fs.statSync(physical).isDirectory() &&
254
- isPathInside(fs.realpathSync(repositoryRoot), physical)) roots.add(physical)
255
- } catch {}
256
- }
257
-
258
- /**
259
- * Returns a configuration-bearing option that the direct-runner contract intentionally rejects.
260
- *
261
- * @param {string} framework framework name
262
- * @param {{runnerIndex: number, tokens: string[]}} invocation parsed invocation
263
- * @returns {string|undefined} unsupported option
264
- */
265
- function getUnsupportedConfigurationOption (framework, invocation) {
266
- const unsupported = UNSUPPORTED_CONFIGURATION_OPTIONS[framework]
267
- if (!unsupported) return
268
- return invocation.tokens.slice(invocation.runnerIndex + 1).find(token => {
269
- return token.startsWith('-') && unsupported.has(token.split('=', 1)[0])
270
- })?.split('=', 1)[0]
271
- }
272
-
273
- /**
274
- * Returns the first runner option whose semantics are neither retained nor explicitly safe to omit.
275
- *
276
- * @param {string} framework framework name
277
- * @param {{runnerIndex: number, tokens: string[]}} invocation parsed invocation
278
- * @returns {string|undefined} unsupported option
279
- */
280
- function getUnknownRunnerOption (framework, invocation) {
281
- const retained = RUNNER_OPTIONS[framework]
282
- if (!retained) return
283
- const ignored = IGNORED_RUNNER_OPTIONS[framework] || { flags: new Set(), values: new Set() }
284
- const omitted = OMITTED_RUNNER_OPTIONS[framework] || new Set()
285
- const selectors = SOURCE_SELECTOR_OPTIONS[framework] || new Set()
286
- const tokens = invocation.tokens.slice(invocation.runnerIndex + 1)
287
-
288
- for (let index = 0; index < tokens.length; index++) {
289
- const token = tokens[index]
290
- if (!token.startsWith('-')) continue
291
- const option = token.split('=', 1)[0]
292
- if (omitted.has(option)) {
293
- if (token !== option) return option
294
- continue
295
- }
296
- if (selectors.has(option)) {
297
- const value = token.includes('=') ? token.slice(token.indexOf('=') + 1) : tokens[index + 1]
298
- if (!value || value.startsWith('-')) return option
299
- if (!token.includes('=')) index++
300
- continue
301
- }
302
- if (retained.flags.has(option) || ignored.flags.has(option)) continue
303
- if (retained.values.has(option) || ignored.values.has(option)) {
304
- if (!token.includes('=') && tokens[index + 1] && !tokens[index + 1].startsWith('-')) index++
305
- continue
306
- }
307
- return option
308
- }
309
- }
310
-
311
- function getOmittedRunnerOptions (framework, invocation) {
312
- const omitted = OMITTED_RUNNER_OPTIONS[framework] || new Set()
313
- const ignored = IGNORED_RUNNER_OPTIONS[framework] || { flags: new Set(), values: new Set() }
314
- return [...new Set(invocation.tokens
315
- .slice(invocation.runnerIndex + 1)
316
- .filter(token => {
317
- if (!token.startsWith('-')) return false
318
- const option = token.split('=', 1)[0]
319
- return omitted.has(option) || ignored.flags.has(option) || ignored.values.has(option)
320
- })
321
- .map(token => token.split('=', 1)[0]))]
322
- }
323
-
324
- function getArgumentOptionValues (args, expected) {
325
- const values = []
326
- for (let index = 0; index < args.length; index++) {
327
- if (args[index].split('=', 1)[0] !== expected) continue
328
- values.push(args[index].includes('=') ? args[index].slice(args[index].indexOf('=') + 1) : args[index + 1])
329
- }
330
- return values
331
- }
332
-
333
- /**
334
- * Selects a project-owned runner only for an exact `node <regular-file>` package script.
335
- *
336
- * @param {string | undefined} command detected package script
337
- * @param {string} projectRoot detected project root
338
- * @param {string} repositoryRoot repository root
339
- * @returns {string|undefined} physical runner path
340
- */
341
- function getProjectNodeRunner (command, projectRoot, repositoryRoot) {
342
- if (CONTROL_PATTERN.test(command || '')) return
343
- const tokens = tokenizeCommand(command)
344
- if (tokens.length !== 2 || !/^node(?:\.exe)?$/i.test(path.basename(tokens[0])) || tokens[1].startsWith('-')) return
345
-
346
- const filename = path.resolve(projectRoot, tokens[1])
347
- try {
348
- const lexicalStat = fs.lstatSync(filename)
349
- const physical = fs.realpathSync(filename)
350
- if (!lexicalStat.isFile() || lexicalStat.isSymbolicLink() ||
351
- !fs.statSync(physical).isFile() ||
352
- !isPathInside(fs.realpathSync(repositoryRoot), physical)) return
353
- return physical
354
- } catch {}
355
- }
356
-
357
- /**
358
- * Validates scaffold-owned runner arguments before they can affect execution.
359
- *
360
- * @param {string} framework framework name
361
- * @param {unknown} args runner arguments
362
- * @returns {string|undefined} validation error
363
- */
364
- function getRunnerArgsError (framework, args) {
365
- if (!Array.isArray(args)) return 'must be an array'
366
- const options = RUNNER_OPTIONS[framework]
367
- if (!options) return 'are unsupported for this framework'
368
-
369
- for (let index = 0; index < args.length; index++) {
370
- const token = args[index]
371
- if (typeof token !== 'string' || !token || CONTROL_PATTERN.test(token)) {
372
- return 'must contain only bounded strings without shell control syntax'
373
- }
374
- const option = token.split('=', 1)[0]
375
- if (options.flags.has(option)) {
376
- if (token !== option &&
377
- !/^--(?:color|colors|detectLeaks)=(?:true|false)$/.test(token)) {
378
- return `contain an invalid value for ${option}`
379
- }
380
- continue
381
- }
382
- if (!options.values.has(option)) return `contain unsupported option ${option}`
383
- if (token.includes('=')) {
384
- if (!token.slice(token.indexOf('=') + 1)) return `contain a missing value for ${option}`
385
- continue
386
- }
387
- const value = args[++index]
388
- if (typeof value !== 'string' || !value || value.startsWith('-') || CONTROL_PATTERN.test(value)) {
389
- return `contain a missing or unsafe value for ${option}`
390
- }
391
- }
392
-
393
- if (framework === 'cucumber') {
394
- const language = getArgumentOptionValues(args, '--language').find(value => value !== 'en')
395
- if (language) {
396
- return `--language ${language} is not supported by the validator-generated English scenarios`
397
- }
398
- }
399
- }
400
-
401
- /**
402
- * Validates the small static environment retained from a runner invocation.
403
- *
404
- * @param {unknown} environment runner environment
405
- * @param {string} [platform] target platform
406
- * @returns {string|undefined} validation error
407
- */
408
- function getRunnerEnvironmentError (environment, platform = process.platform) {
409
- if (!environment || typeof environment !== 'object' || Array.isArray(environment)) return 'must be an object'
410
- for (const [name, value] of Object.entries(environment)) {
411
- if (!getCanonicalRunnerEnvironmentName(name, platform)) return `contains unsupported variable ${name}`
412
- if (typeof value !== 'string' || Buffer.byteLength(value) > 4096 ||
413
- CONTROL_PATTERN.test(value) || ENVIRONMENT_EXPANSION_PATTERN.test(value)) {
414
- return `contains an unsafe value for ${name}`
415
- }
416
- }
417
- }
418
-
419
- /**
420
- * Validates that retained code-loading inputs are contained and approval-bound.
421
- *
422
- * @param {string[]} args runner arguments
423
- * @param {Record<string, string>} environment runner environment
424
- * @param {string} projectRoot project root
425
- * @param {string} repositoryRoot repository root
426
- * @param {string[]} configFiles approval-bound project inputs
427
- * @returns {string|undefined} validation error
428
- */
429
- function getRunnerInputError (args, environment, projectRoot, repositoryRoot, configFiles) {
430
- const inputs = getRunnerInputs(args, environment, projectRoot, repositoryRoot)
431
- if (inputs.error) return inputs.error
432
-
433
- const approved = new Set()
434
- if (configFiles) {
435
- for (const filename of configFiles) {
436
- try {
437
- approved.add(fs.realpathSync(filename))
438
- } catch {}
439
- }
440
- }
441
- const unbound = inputs.files.find(filename => !approved.has(filename))
442
- if (unbound) return `references an input that is not approval-bound: ${unbound}`
443
- }
444
-
445
- /**
446
- * Finds a direct framework executable behind inert launch wrappers.
447
- *
448
- * @param {string | undefined} command detected package script
449
- * @param {string} framework framework name
450
- * @returns {{error?: string, runnerIndex: number, tokens: string[]}|undefined} parsed invocation
451
- */
452
- function getFrameworkInvocation (command, framework) {
453
- if (CONTROL_PATTERN.test(command || '') || command === `direct ${framework} binary`) return
454
- const tokens = tokenizeCommand(command)
455
- const executable = getRunnerExecutableName(framework)
456
- const runnerIndex = tokens.findIndex(token => {
457
- const basename = normalizeRunnerBasename(token)
458
- return basename === executable ||
459
- (framework === 'cucumber' && ['cucumber', 'cucumber-js'].includes(basename))
460
- })
461
- if (runnerIndex === -1) return
462
-
463
- const prefixError = getRunnerPrefixError(tokens.slice(0, runnerIndex))
464
- if (prefixError) return { error: prefixError, runnerIndex, tokens }
465
- return { runnerIndex, tokens }
466
- }
467
-
468
- function getRunnerPrefixError (prefix) {
469
- let coverageLauncher = false
470
- for (let index = 0; index < prefix.length; index++) {
471
- const token = prefix[index]
472
- if (/^[A-Za-z_][A-Za-z0-9_]*=[^;&|`]*$/.test(token)) continue
473
-
474
- const basename = path.basename(token).toLowerCase()
475
- if (RUNNER_LAUNCHERS.has(basename)) {
476
- coverageLauncher = basename === 'c8' || basename === 'nyc'
477
- continue
478
- }
479
- if (coverageLauncher && /^(?:-r|--reporter)=\S+$/.test(token)) continue
480
- if (coverageLauncher && /^(?:-r|--reporter)$/.test(token) &&
481
- prefix[index + 1] && !prefix[index + 1].startsWith('-')) {
482
- index++
483
- continue
484
- }
485
- if (!prefix.some(candidate => RUNNER_LAUNCHERS.has(path.basename(candidate).toLowerCase()))) {
486
- return `runner launch wrapper ${path.basename(token)} is not allowlisted`
487
- }
488
- return 'runner launch wrapper contains options or positional arguments whose semantics the validator does not ' +
489
- 'preserve'
490
- }
491
- }
492
-
493
- /**
494
- * Extracts allowlisted runner options while dropping source selectors and presentation flags.
495
- *
496
- * @param {string} framework framework name
497
- * @param {{runnerIndex: number, tokens: string[]}} invocation parsed invocation
498
- * @returns {string[]} retained options
499
- */
500
- function getRunnerArgs (framework, invocation) {
501
- const options = RUNNER_OPTIONS[framework]
502
- if (!options) return []
503
- const args = []
504
- for (let index = invocation.runnerIndex + 1; index < invocation.tokens.length; index++) {
505
- const token = invocation.tokens[index]
506
- if (!token.startsWith('-')) continue
507
- const option = token.split('=', 1)[0]
508
- if (options.flags.has(option)) {
509
- args.push(token)
510
- } else if (options.values.has(option)) {
511
- args.push(token)
512
- if (!token.includes('=') && invocation.tokens[index + 1] && !invocation.tokens[index + 1].startsWith('-')) {
513
- args.push(invocation.tokens[++index])
514
- }
515
- }
516
- }
517
- return args
518
- }
519
-
520
- /**
521
- * Normalizes executable shims and supported JavaScript runner entrypoints.
522
- *
523
- * @param {string} token command token
524
- */
525
- function normalizeRunnerBasename (token) {
526
- return path.basename(token)
527
- .replace(/\.cmd$/i, '')
528
- .replace(/\.(?:cjs|mjs|js)$/i, '')
529
- .toLowerCase()
530
- }
531
-
532
- /**
533
- * Extracts allowlisted literal environment assignments before the framework runner.
534
- *
535
- * @param {{runnerIndex: number, tokens: string[]}} invocation parsed invocation
536
- * @param {string} [platform] target platform
537
- * @returns {Record<string, string>} retained environment
538
- */
539
- function getRunnerEnvironment (invocation, platform = process.platform) {
540
- const environment = {}
541
- for (const token of invocation.tokens.slice(0, invocation.runnerIndex)) {
542
- const match = /^([A-Za-z_][A-Za-z0-9_]*)=(.*)$/.exec(token)
543
- const name = match && getCanonicalRunnerEnvironmentName(match[1], platform)
544
- if (name && !CONTROL_PATTERN.test(match[2])) {
545
- setEnvironmentValue(environment, name, match[2], platform)
546
- }
547
- }
548
- return environment
549
- }
550
-
551
- function getCanonicalRunnerEnvironmentName (name, platform) {
552
- return [...RUNNER_ENVIRONMENT_NAMES].find(candidate => {
553
- return environmentNamesEqual(candidate, name, platform)
554
- })
555
- }
556
-
557
- /**
558
- * Resolves regular files referenced by retained configuration.
559
- *
560
- * @param {string[]} args runner arguments
561
- * @param {Record<string, string>} environment runner environment
562
- * @param {string} projectRoot project root
563
- * @param {string} repositoryRoot repository root
564
- * @returns {{error?: string, files: string[]}} physical input files
565
- */
566
- function getRunnerInputs (args, environment, projectRoot, repositoryRoot) {
567
- const values = []
568
- for (let index = 0; index < args.length; index++) {
569
- const option = args[index].split('=', 1)[0]
570
- if (!MODULE_OPTIONS.has(option) && !FILE_OPTIONS.has(option) && !DIRECTORY_OPTIONS.has(option)) continue
571
- const value = args[index].includes('=') ? args[index].slice(args[index].indexOf('=') + 1) : args[++index]
572
- values.push({
573
- directory: DIRECTORY_OPTIONS.has(option),
574
- label: option,
575
- module: MODULE_OPTIONS.has(option),
576
- value,
577
- })
578
- }
579
- if (environment.TS_NODE_PROJECT) {
580
- values.push({
581
- directory: false,
582
- label: 'TS_NODE_PROJECT',
583
- module: false,
584
- value: environment.TS_NODE_PROJECT,
585
- })
586
- }
587
-
588
- const files = new Set()
589
- for (const input of values) {
590
- if (input.directory) {
591
- const directoryError = getContainedDirectoryError(input, projectRoot, repositoryRoot)
592
- if (directoryError) return { error: directoryError, files: [] }
593
- continue
594
- }
595
- if (input.module && BUILTIN_MODULE_VALUES.get(input.label)?.has(input.value)) continue
596
- const filename = resolveInputFile(input, projectRoot)
597
- if (!filename) return { error: `${input.label} does not resolve to a repository-contained file`, files: [] }
598
- try {
599
- const physical = fs.realpathSync(filename)
600
- if (!fs.statSync(physical).isFile() || !isPathInside(fs.realpathSync(repositoryRoot), physical)) {
601
- return { error: `${input.label} resolves outside the repository`, files: [] }
602
- }
603
- files.add(physical)
604
- } catch {
605
- return { error: `${input.label} does not resolve to a repository-contained file`, files: [] }
606
- }
607
- }
608
- if (files.size > 20) return { error: 'retains more than 20 code-loading inputs', files: [] }
609
- return { files: [...files].sort() }
610
- }
611
-
612
- /**
613
- * Checks one retained directory selector without traversing it.
614
- *
615
- * @param {{label: string, value: string}} input input descriptor
616
- * @param {string} projectRoot project root
617
- * @param {string} repositoryRoot repository root
618
- * @returns {string|undefined} validation error
619
- */
620
- function getContainedDirectoryError (input, projectRoot, repositoryRoot) {
621
- try {
622
- const physical = fs.realpathSync(path.resolve(projectRoot, input.value))
623
- if (!fs.statSync(physical).isDirectory() || !isPathInside(fs.realpathSync(repositoryRoot), physical)) {
624
- return `${input.label} resolves outside the repository`
625
- }
626
- } catch {
627
- return `${input.label} does not resolve to a repository-contained directory`
628
- }
629
- }
630
-
631
- /**
632
- * Resolves one module or path without loading it.
633
- *
634
- * @param {{module: boolean, value: string}} input input descriptor
635
- * @param {string} projectRoot project root
636
- * @returns {string|undefined} resolved filename
637
- */
638
- function resolveInputFile (input, projectRoot) {
639
- if (!input.value) return
640
- if (!input.module || path.isAbsolute(input.value) || input.value.startsWith('.')) {
641
- return path.resolve(projectRoot, input.value)
642
- }
643
- const projectRelative = path.resolve(projectRoot, input.value)
644
- try {
645
- return require.resolve(projectRelative)
646
- } catch {}
647
- try {
648
- return require.resolve(input.value, { paths: [projectRoot] })
649
- } catch {}
650
- }
651
-
652
- /**
653
- * Tokenizes bounded package-script evidence without expanding variables or invoking a shell.
654
- *
655
- * @param {string | undefined} command package script
656
- * @returns {string[]} inert tokens
657
- */
658
- function tokenizeCommand (command) {
659
- const tokens = []
660
- for (const match of (command || '').matchAll(/"([^"\r\n]*)"|'([^'\r\n]*)'|([^\s"']+)/g)) {
661
- tokens.push(match[1] ?? match[2] ?? match[3])
662
- }
663
- return tokens
664
- }
665
-
666
- /**
667
- * Returns the executable name for a framework.
668
- *
669
- * @param {string} framework framework name
670
- */
671
- function getRunnerExecutableName (framework) {
672
- if (framework === 'cucumber') return 'cucumber-js'
673
- return framework === 'playwright' ? 'playwright' : framework
674
- }
675
-
676
- /**
677
- * Checks physical path containment.
678
- *
679
- * @param {string} root root path
680
- * @param {string} filename candidate path
681
- */
682
- function isPathInside (root, filename) {
683
- const relative = path.relative(path.resolve(root), path.resolve(filename))
684
- return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative))
685
- }
686
-
687
- module.exports = {
688
- getProjectNodeRunner,
689
- getRunnerArgsError,
690
- getRunnerConfigurationContract,
691
- getRunnerContract,
692
- getRunnerEnvironmentError,
693
- getRunnerInputError,
694
- getRunnerSearchRoots,
695
- }