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.
- package/README.md +3 -0
- package/ci/cypress/support.js +2 -1
- package/ci/init.js +2 -22
- package/ci/vitest-no-worker-init-node-setup.mjs +2 -1
- package/ci/vitest-no-worker-init-runner-setup.mjs +2 -1
- package/ci/vitest-no-worker-init-setup.mjs +729 -724
- package/ci/vitest-no-worker-init-v5-setup.mjs +2 -1
- package/ext/exporters.js +0 -1
- package/index.d.ts +105 -5
- package/initialize.mjs +5 -2
- package/package.json +41 -36
- package/packages/datadog-esbuild/index.js +16 -70
- package/packages/datadog-esbuild/src/log.js +2 -29
- package/packages/datadog-instrumentations/src/ai.js +6 -1
- package/packages/datadog-instrumentations/src/aws-lambda.js +88 -0
- package/packages/datadog-instrumentations/src/cucumber.js +5 -2
- package/packages/datadog-instrumentations/src/cypress-config.js +20 -9
- package/packages/datadog-instrumentations/src/elevenlabs/activity.js +114 -0
- package/packages/datadog-instrumentations/src/elevenlabs/audio.js +104 -0
- package/packages/datadog-instrumentations/src/elevenlabs/index.js +288 -0
- package/packages/datadog-instrumentations/src/elevenlabs/session.js +444 -0
- package/packages/datadog-instrumentations/src/graphql.js +20 -17
- package/packages/datadog-instrumentations/src/helpers/bundler-esm-exports.js +582 -0
- package/packages/datadog-instrumentations/src/helpers/bundler-log.js +38 -0
- package/packages/datadog-instrumentations/src/helpers/bundler-transform.js +576 -0
- package/packages/datadog-instrumentations/src/helpers/bundler-utils.js +104 -0
- package/packages/datadog-instrumentations/src/helpers/fetch.js +51 -6
- package/packages/datadog-instrumentations/src/helpers/hook.js +29 -1
- package/packages/datadog-instrumentations/src/helpers/hooks.js +3 -7
- package/packages/datadog-instrumentations/src/helpers/lambda-handler.js +189 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/index.js +8 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentation-registry.js +26 -1
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/async-mutex.js +41 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/elevenlabs.js +25 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/graphql.js +0 -36
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-client.js +30 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/modelcontextprotocol-server.js +23 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/next.js +61 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/openai-agents-realtime.js +23 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/postgres.js +33 -4
- package/packages/datadog-instrumentations/src/helpers/rewriter/instrumentations/trpc.js +148 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/target-patterns.json +80 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/targets.json +23 -3
- package/packages/datadog-instrumentations/src/helpers/rewriter/transforms/postgres.js +122 -0
- package/packages/datadog-instrumentations/src/helpers/rewriter/transforms.js +59 -2
- package/packages/datadog-instrumentations/src/ioredis.js +101 -3
- package/packages/datadog-instrumentations/src/microgateway-core.js +1 -2
- package/packages/datadog-instrumentations/src/modelcontextprotocol-client.js +21 -0
- package/packages/datadog-instrumentations/src/modelcontextprotocol-sdk.js +7 -1
- package/packages/datadog-instrumentations/src/modelcontextprotocol-server.js +41 -0
- package/packages/datadog-instrumentations/src/next.js +203 -26
- package/packages/datadog-instrumentations/src/openai-agents-realtime.js +149 -0
- package/packages/datadog-instrumentations/src/openai-realtime/audio-accumulator.js +27 -0
- package/packages/datadog-instrumentations/src/openai-realtime/index.js +17 -6
- package/packages/datadog-instrumentations/src/openai-realtime/server-playback.js +887 -0
- package/packages/datadog-instrumentations/src/openai-realtime/session.js +150 -25
- package/packages/datadog-instrumentations/src/openai-realtime/turns.js +29 -2
- package/packages/datadog-instrumentations/src/otel-sdk-trace.js +11 -9
- package/packages/datadog-instrumentations/src/router.js +35 -12
- package/packages/datadog-instrumentations/src/vitest-main.js +10 -2
- package/packages/datadog-instrumentations/src/webdriverio.js +12 -12
- package/packages/datadog-plugin-async-mutex/src/index.js +50 -0
- package/packages/datadog-plugin-aws-lambda/src/arn.js +41 -0
- package/packages/datadog-plugin-aws-lambda/src/event-type-guards.js +170 -0
- package/packages/datadog-plugin-aws-lambda/src/index.js +106 -0
- package/packages/datadog-plugin-aws-lambda/src/trigger.js +339 -0
- package/packages/datadog-plugin-aws-sdk/src/services/dynamodb.js +7 -0
- package/packages/datadog-plugin-aws-sdk/src/services/kinesis.js +32 -10
- package/packages/datadog-plugin-aws-sdk/src/services/lambda.js +10 -1
- package/packages/datadog-plugin-aws-sdk/src/services/sqs.js +4 -0
- package/packages/datadog-plugin-aws-sdk/src/services/stepfunctions.js +3 -0
- package/packages/datadog-plugin-cypress/src/cypress-plugin.js +50 -13
- package/packages/datadog-plugin-cypress/src/support.mjs +1353 -0
- package/packages/datadog-plugin-elevenlabs/src/index.js +69 -0
- package/packages/datadog-plugin-express/src/code_origin.js +2 -2
- package/packages/datadog-plugin-fastify/src/code_origin.js +2 -2
- package/packages/datadog-plugin-google-cloud-pubsub/src/consumer.js +20 -7
- package/packages/datadog-plugin-google-cloud-pubsub/src/producer.js +21 -52
- package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-push-subscription.js +11 -2
- package/packages/datadog-plugin-google-cloud-pubsub/src/pubsub-request-context.js +25 -4
- package/packages/datadog-plugin-google-cloud-pubsub/src/resource-names.js +24 -0
- package/packages/datadog-plugin-graphql/src/tools/transforms.js +1 -0
- package/packages/datadog-plugin-http/src/client.js +34 -10
- package/packages/datadog-plugin-http2/src/client.js +40 -14
- package/packages/datadog-plugin-modelcontextprotocol-sdk/src/tracing.js +28 -2
- package/packages/datadog-plugin-modelcontextprotocol-sdk/src/utils.js +1 -0
- package/packages/datadog-plugin-mongodb-core/src/query.js +17 -1
- package/packages/datadog-plugin-next/src/index.js +112 -14
- package/packages/datadog-plugin-postgres/src/index.js +118 -41
- package/packages/datadog-plugin-prisma/src/datadog-tracing-helper.js +5 -3
- package/packages/datadog-plugin-supabase/src/supabase-auth-js-gotrueclient-getuser.js +18 -8
- package/packages/datadog-plugin-supabase/src/supabase-functions-js-functionsclient-invoke.js +49 -13
- package/packages/datadog-plugin-supabase/src/supabase-storage-js-handle-request.js +34 -9
- package/packages/datadog-plugin-trpc/src/index.js +15 -0
- package/packages/datadog-plugin-trpc/src/procedure.js +48 -0
- package/packages/datadog-plugin-trpc/src/request.js +81 -0
- package/packages/datadog-plugin-undici/src/index.js +22 -7
- package/packages/datadog-plugin-vitest/src/index.js +3 -2
- package/packages/datadog-plugin-ws/src/close.js +3 -5
- package/packages/datadog-plugin-ws/src/producer.js +1 -4
- package/packages/datadog-plugin-ws/src/receiver.js +3 -5
- package/packages/datadog-plugin-ws/src/server.js +44 -13
- package/packages/datadog-turbopack/index.js +4 -28
- package/packages/datadog-turbopack/src/loader.js +14 -452
- package/packages/datadog-vite/index.js +537 -0
- package/packages/datadog-vite/src/log.js +5 -0
- package/packages/datadog-webpack/index.js +3 -55
- package/packages/datadog-webpack/src/log.js +2 -29
- package/packages/dd-trace/src/aiguard/evaluation.js +62 -1
- package/packages/dd-trace/src/aiguard/index.js +1 -1
- package/packages/dd-trace/src/aiguard/integrations/index.js +6 -5
- package/packages/dd-trace/src/aiguard/sdk.js +8 -2
- package/packages/dd-trace/src/aiguard/tags.js +2 -0
- package/packages/dd-trace/src/appsec/blocking/index.js +21 -1
- package/packages/dd-trace/src/appsec/channels.js +7 -0
- package/packages/dd-trace/src/appsec/downstream_requests.js +105 -0
- package/packages/dd-trace/src/appsec/graphql.js +89 -7
- package/packages/dd-trace/src/appsec/handlers/auth.js +1 -3
- package/packages/dd-trace/src/appsec/handlers/http-request.js +60 -65
- package/packages/dd-trace/src/appsec/handlers/http-response.js +61 -7
- package/packages/dd-trace/src/appsec/handlers/payments.js +23 -27
- package/packages/dd-trace/src/appsec/index.js +6 -0
- package/packages/dd-trace/src/appsec/lambda.js +17 -17
- package/packages/dd-trace/src/appsec/rasp/command_injection.js +4 -4
- package/packages/dd-trace/src/appsec/rasp/index.js +8 -1
- package/packages/dd-trace/src/appsec/rasp/lfi.js +2 -2
- package/packages/dd-trace/src/appsec/rasp/sql_injection.js +7 -17
- package/packages/dd-trace/src/appsec/rasp/ssrf.js +80 -16
- package/packages/dd-trace/src/appsec/rasp/utils.js +33 -3
- package/packages/dd-trace/src/appsec/recommended.json +1358 -94
- package/packages/dd-trace/src/appsec/reporter.js +9 -0
- package/packages/dd-trace/src/appsec/sdk/set_user.js +3 -3
- package/packages/dd-trace/src/appsec/sdk/track_event.js +4 -4
- package/packages/dd-trace/src/appsec/sdk/user_blocking.js +1 -1
- package/packages/dd-trace/src/appsec/user_tracking.js +6 -8
- package/packages/dd-trace/src/appsec/waf/index.js +65 -6
- package/packages/dd-trace/src/appsec/waf/waf_context_wrapper.js +33 -38
- package/packages/dd-trace/src/appsec/waf/waf_manager.js +18 -2
- package/packages/dd-trace/src/before-exit.js +32 -0
- package/packages/dd-trace/src/bootstrap.js +5 -3
- package/packages/dd-trace/src/ci-visibility/early-flake-detection/get-known-tests.js +15 -2
- package/packages/dd-trace/src/ci-visibility/exporters/ci-visibility-exporter.js +12 -34
- package/packages/dd-trace/src/ci-visibility/exporters/git/git_metadata.js +41 -13
- package/packages/dd-trace/src/ci-visibility/exporters/request.js +2 -3
- package/packages/dd-trace/src/ci-visibility/intelligent-test-runner/get-skippable-suites.js +1 -2
- package/packages/dd-trace/src/ci-visibility/requests/get-library-configuration.js +1 -1
- package/packages/dd-trace/src/ci-visibility/telemetry.js +6 -0
- package/packages/dd-trace/src/ci-visibility/test-management/get-test-management-tests.js +1 -1
- package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache-schema.js +9 -134
- package/packages/dd-trace/src/ci-visibility/test-optimization-http-cache.js +11 -156
- package/packages/dd-trace/src/config/config-types.d.ts +3 -0
- package/packages/dd-trace/src/config/defaults.js +5 -0
- package/packages/dd-trace/src/config/generated-config-types.d.ts +46 -4
- package/packages/dd-trace/src/config/index.js +63 -31
- package/packages/dd-trace/src/config/parsers.js +44 -0
- package/packages/dd-trace/src/config/remote-config-layers.js +38 -0
- package/packages/dd-trace/src/config/remote_config.js +2 -1
- package/packages/dd-trace/src/config/supported-configurations.json +172 -10
- package/packages/dd-trace/src/constants.js +1 -0
- package/packages/dd-trace/src/datastreams/fnv.js +27 -0
- package/packages/dd-trace/src/datastreams/pathway.js +113 -28
- package/packages/dd-trace/src/datastreams/processor.js +5 -4
- package/packages/dd-trace/src/debugger/config.js +1 -1
- package/packages/dd-trace/src/debugger/devtools_client/request-options.js +7 -9
- package/packages/dd-trace/src/debugger/devtools_client/send.js +1 -1
- package/packages/dd-trace/src/debugger/devtools_client/state.js +3 -1
- package/packages/dd-trace/src/debugger/devtools_client/tags.js +1 -1
- package/packages/dd-trace/src/debugger/index.js +2 -2
- package/packages/dd-trace/src/debugger/inspect-segment.js +3 -2
- package/packages/dd-trace/src/debugger/pause-duration-histogram.js +6 -2
- package/packages/dd-trace/src/debugger/probe_sampler.js +39 -17
- package/packages/dd-trace/src/debugger/unsupported-runtime.js +2 -0
- package/packages/dd-trace/src/exporter.js +0 -11
- package/packages/dd-trace/src/exporters/common/request.js +81 -48
- package/packages/dd-trace/src/exporters/common/retry.js +12 -7
- package/packages/dd-trace/src/exporters/common/url.js +1 -1
- package/packages/dd-trace/src/git_metadata.js +2 -1
- package/packages/dd-trace/src/id.js +1 -1
- package/packages/dd-trace/src/index.js +6 -3
- package/packages/dd-trace/src/lambda/facade.js +75 -0
- package/packages/dd-trace/src/lambda/handler.js +3 -41
- package/packages/dd-trace/src/lambda/index.js +38 -15
- package/packages/dd-trace/src/lambda/runtime/patch.js +19 -3
- package/packages/dd-trace/src/lambda/wrap.js +44 -0
- package/packages/dd-trace/src/llmobs/constants/tags.js +2 -0
- package/packages/dd-trace/src/llmobs/experiments/experiment.js +10 -5
- package/packages/dd-trace/src/llmobs/plugins/elevenlabs.js +96 -0
- package/packages/dd-trace/src/llmobs/plugins/genai/util.js +19 -6
- package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/index.js +54 -3
- package/packages/dd-trace/src/llmobs/plugins/modelcontextprotocol-sdk/utils.js +81 -1
- package/packages/dd-trace/src/llmobs/plugins/openai/realtime.js +38 -11
- package/packages/dd-trace/src/llmobs/plugins/vertexai.js +10 -2
- package/packages/dd-trace/src/llmobs/sdk.js +19 -0
- package/packages/dd-trace/src/llmobs/span_processor.js +9 -0
- package/packages/dd-trace/src/llmobs/tagger.js +22 -0
- package/packages/dd-trace/src/log/channels.js +4 -0
- package/packages/dd-trace/src/log/writer.js +6 -1
- package/packages/dd-trace/src/openfeature/agentless_configuration_source.js +7 -0
- package/packages/dd-trace/src/openfeature/configuration_source.js +55 -27
- package/packages/dd-trace/src/openfeature/flag-selection.js +152 -0
- package/packages/dd-trace/src/openfeature/flagging_provider.js +19 -1
- package/packages/dd-trace/src/openfeature/writers/base.js +1 -3
- package/packages/dd-trace/src/opentelemetry/logs/index.js +11 -0
- package/packages/dd-trace/src/opentelemetry/logs/otlp_http_log_exporter.js +1 -1
- package/packages/dd-trace/src/opentelemetry/metrics/index.js +7 -1
- package/packages/dd-trace/src/opentelemetry/metrics/otlp_http_metric_exporter.js +1 -1
- package/packages/dd-trace/src/opentelemetry/metrics/otlp_span_stats_exporter.js +4 -1
- package/packages/dd-trace/src/opentelemetry/otlp/otlp_http_exporter_base.js +19 -1
- package/packages/dd-trace/src/opentelemetry/span-ending-hook.js +5 -1
- package/packages/dd-trace/src/opentelemetry/span-helpers.js +6 -1
- package/packages/dd-trace/src/opentelemetry/span.js +2 -2
- package/packages/dd-trace/src/opentelemetry/startup-config.js +174 -0
- package/packages/dd-trace/src/opentelemetry/trace/otlp_http_trace_exporter.js +1 -1
- package/packages/dd-trace/src/opentelemetry/trace/otlp_transformer.js +7 -1
- package/packages/dd-trace/src/opentracing/propagation/tracestate.js +77 -23
- package/packages/dd-trace/src/opentracing/tracer.js +24 -0
- package/packages/dd-trace/src/otel-thread-ctx.js +28 -31
- package/packages/dd-trace/src/plugin_manager.js +34 -7
- package/packages/dd-trace/src/plugins/ci_plugin.js +143 -17
- package/packages/dd-trace/src/plugins/index.js +8 -0
- package/packages/dd-trace/src/plugins/plugin.js +4 -2
- package/packages/dd-trace/src/plugins/tracing.js +1 -1
- package/packages/dd-trace/src/plugins/util/ci.js +52 -1
- package/packages/dd-trace/src/plugins/util/git-cache.js +4 -4
- package/packages/dd-trace/src/plugins/util/git.js +263 -82
- package/packages/dd-trace/src/plugins/util/http-otel-semantics.js +80 -56
- package/packages/dd-trace/src/plugins/util/inferred_proxy.js +12 -4
- package/packages/dd-trace/src/plugins/util/status-validator.js +23 -10
- package/packages/dd-trace/src/plugins/util/tags.js +6 -0
- package/packages/dd-trace/src/plugins/util/test.js +92 -40
- package/packages/dd-trace/src/plugins/util/url.js +18 -7
- package/packages/dd-trace/src/plugins/util/user-provided-git.js +6 -6
- package/packages/dd-trace/src/plugins/util/web.js +65 -10
- package/packages/dd-trace/src/process-tags/index.js +1 -1
- package/packages/dd-trace/src/profiler.js +74 -12
- package/packages/dd-trace/src/profiling/profiler.js +30 -2
- package/packages/dd-trace/src/profiling/profilers/space.js +3 -1
- package/packages/dd-trace/src/profiling/profilers/wall.js +4 -4
- package/packages/dd-trace/src/profiling/remote-config.js +354 -0
- package/packages/dd-trace/src/profiling/runtime-capabilities.js +11 -0
- package/packages/dd-trace/src/profiling/webspan-utils.js +6 -5
- package/packages/dd-trace/src/propagation-hash/index.js +7 -0
- package/packages/dd-trace/src/proxy.js +46 -54
- package/packages/dd-trace/src/span_processor.js +20 -0
- package/packages/dd-trace/src/startup-log.js +8 -6
- package/packages/dd-trace/src/telemetry/metrics.js +2 -1
- package/packages/dd-trace/src/tracer.js +0 -2
- package/packages/dd-trace/src/web-tags-cache.js +8 -9
- package/register.js +2 -1
- package/vendor/dist/@apm-js-collab/code-transformer/LICENSE +489 -0
- package/vendor/dist/@datadog/flagging-core/LICENSE +221 -0
- package/vendor/dist/@datadog/flagging-core/index.js +1 -0
- package/vendor/dist/@datadog/openfeature-node-server/LICENSE +21 -1
- package/vendor/dist/@datadog/openfeature-node-server/rules-based/LICENSE +3 -1
- package/vendor/dist/@datadog/sketches-js/LICENSE +263 -0
- package/vendor/dist/@datadog/source-map/LICENSE +1 -0
- package/vendor/dist/@isaacs/ttlcache/LICENSE +13 -0
- package/vendor/dist/@opentelemetry/core/LICENSE +220 -1
- package/vendor/dist/@opentelemetry/core/index.js +1 -1
- package/vendor/dist/crypto-randomuuid/LICENSE +3 -0
- package/vendor/dist/escape-string-regexp/LICENSE +13 -0
- package/vendor/dist/esquery/LICENSE +2 -0
- package/vendor/dist/https-proxy-agent/LICENSE +80 -1
- package/vendor/dist/istanbul-lib-coverage/LICENSE +2 -0
- package/vendor/dist/jest-docblock/LICENSE +16 -0
- package/vendor/dist/jsonpath-plus/LICENSE +13 -0
- package/vendor/dist/limiter/LICENSE +2 -0
- package/vendor/dist/lodash.sortby/LICENSE +2 -0
- package/vendor/dist/lru-cache/LICENSE +13 -0
- package/vendor/dist/meriyah/LICENSE +13 -0
- package/vendor/dist/module-details-from-path/LICENSE +2 -0
- package/vendor/dist/mutexify/promise/LICENSE +28 -0
- package/vendor/dist/pprof-format/LICENSE +13 -0
- package/vendor/dist/pprof-format/index.js +1 -1
- package/vendor/dist/protobufjs/LICENSE +219 -0
- package/vendor/dist/protobufjs/minimal/LICENSE +219 -0
- package/vendor/dist/proxy-from-env/LICENSE +13 -0
- package/vendor/dist/retry/LICENSE +2 -0
- package/vendor/dist/rfdc/LICENSE +2 -0
- package/vendor/dist/semifies/LICENSE +3 -1
- package/vendor/dist/shell-quote/LICENSE +3 -1
- package/vendor/dist/source-map/LICENSE +1 -0
- package/vendor/dist/source-map/lib/util/LICENSE +1 -0
- package/vendor/dist/tlhunter-sorted-set/LICENSE +2 -0
- package/vendor/dist/ttl-set/LICENSE +28 -0
- package/vite.d.ts +7 -0
- package/vite.js +3 -0
- package/ci/diagnose.js +0 -2215
- package/ci/runbook.md +0 -299
- package/ci/test-optimization-validation/approval-artifacts.js +0 -175
- package/ci/test-optimization-validation/approval.js +0 -386
- package/ci/test-optimization-validation/artifact-id.js +0 -18
- package/ci/test-optimization-validation/blocker-category.js +0 -24
- package/ci/test-optimization-validation/bounded-json.js +0 -120
- package/ci/test-optimization-validation/ci-command-candidate.js +0 -77
- package/ci/test-optimization-validation/ci-discovery.js +0 -192
- package/ci/test-optimization-validation/ci-package-scripts.js +0 -169
- package/ci/test-optimization-validation/ci-remediation.js +0 -283
- package/ci/test-optimization-validation/cli.js +0 -954
- package/ci/test-optimization-validation/command-blocker.js +0 -377
- package/ci/test-optimization-validation/command-output-policy.js +0 -188
- package/ci/test-optimization-validation/command-runner.js +0 -649
- package/ci/test-optimization-validation/environment.js +0 -88
- package/ci/test-optimization-validation/executable-approval.js +0 -47
- package/ci/test-optimization-validation/executable.js +0 -244
- package/ci/test-optimization-validation/execution-lock.js +0 -70
- package/ci/test-optimization-validation/framework-adapters/cucumber-validation.json +0 -3
- package/ci/test-optimization-validation/framework-adapters/cucumber.js +0 -767
- package/ci/test-optimization-validation/framework-adapters/cypress.js +0 -99
- package/ci/test-optimization-validation/framework-adapters/playwright.js +0 -175
- package/ci/test-optimization-validation/framework-adapters/vitest.js +0 -513
- package/ci/test-optimization-validation/generated-file-policy.js +0 -63
- package/ci/test-optimization-validation/generated-files.js +0 -425
- package/ci/test-optimization-validation/generated-test-contract.js +0 -284
- package/ci/test-optimization-validation/generated-verifier.js +0 -235
- package/ci/test-optimization-validation/literal-environment.js +0 -56
- package/ci/test-optimization-validation/literal-glob.js +0 -52
- package/ci/test-optimization-validation/manifest-loader.js +0 -116
- package/ci/test-optimization-validation/manifest-scaffold.js +0 -1276
- package/ci/test-optimization-validation/manifest-schema.js +0 -695
- package/ci/test-optimization-validation/offline-fixtures.js +0 -335
- package/ci/test-optimization-validation/offline-output.js +0 -406
- package/ci/test-optimization-validation/package-check.js +0 -97
- package/ci/test-optimization-validation/payload-normalizer.js +0 -72
- package/ci/test-optimization-validation/plan-writer.js +0 -541
- package/ci/test-optimization-validation/preflight-runner.js +0 -186
- package/ci/test-optimization-validation/project-build-artifact.js +0 -31
- package/ci/test-optimization-validation/redaction.js +0 -319
- package/ci/test-optimization-validation/report-writer.js +0 -666
- package/ci/test-optimization-validation/result-semantics.js +0 -103
- package/ci/test-optimization-validation/runner-command.js +0 -264
- package/ci/test-optimization-validation/runner-contract.js +0 -695
- package/ci/test-optimization-validation/safe-files.js +0 -202
- package/ci/test-optimization-validation/scenarios/auto-test-retries.js +0 -171
- package/ci/test-optimization-validation/scenarios/basic-reporting.js +0 -407
- package/ci/test-optimization-validation/scenarios/ci-wiring.js +0 -666
- package/ci/test-optimization-validation/scenarios/early-flake-detection.js +0 -142
- package/ci/test-optimization-validation/scenarios/helpers.js +0 -621
- package/ci/test-optimization-validation/scenarios/test-management.js +0 -219
- package/ci/test-optimization-validation/source-text.js +0 -84
- package/ci/test-optimization-validation/static-diagnosis.js +0 -166
- package/ci/test-optimization-validation/test-output.js +0 -120
- package/ci/test-optimization-validation/validation-blocker.js +0 -21
- package/ci/validate-test-optimization.js +0 -3
- package/packages/datadog-esbuild/src/resolver.js +0 -284
- package/packages/datadog-esbuild/src/utils.js +0 -318
- package/packages/datadog-plugin-cypress/src/support.js +0 -1348
- package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/index.js +0 -156
- package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/msgpack-to-json.js +0 -287
- package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/payload-projection.js +0 -84
- package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/sink.js +0 -368
- package/packages/dd-trace/src/ci-visibility/exporters/ci-validation/writer.js +0 -59
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
// Export analysis of ES modules for bundler plugins. processModule is copied from the `import-in-the-middle` package
|
|
4
|
+
// with minor modifications (https://www.npmjs.com/package/import-in-the-middle).
|
|
5
|
+
const childProcess = require('node:child_process')
|
|
6
|
+
const fs = require('node:fs')
|
|
7
|
+
const readline = require('node:readline')
|
|
8
|
+
const { fileURLToPath, pathToFileURL } = require('node:url')
|
|
9
|
+
|
|
10
|
+
const { getEnvironmentVariables } = require('../../../dd-trace/src/config/helper')
|
|
11
|
+
const { isESMFile } = require('./bundler-utils')
|
|
12
|
+
|
|
13
|
+
const MAX_PENDING_REQUESTS = 1024
|
|
14
|
+
const MAX_STDERR_LENGTH = 16 * 1024
|
|
15
|
+
const LOAD_OPERATION = 0
|
|
16
|
+
const RESOLVE_OPERATION = 1
|
|
17
|
+
const RESOLVER_SOURCE = String.raw`
|
|
18
|
+
import { createReadStream, createWriteStream } from 'node:fs'
|
|
19
|
+
import readline from 'node:readline'
|
|
20
|
+
|
|
21
|
+
const requests = createReadStream(null, { fd: 3, autoClose: false })
|
|
22
|
+
const responses = createWriteStream(null, { fd: 4, autoClose: false })
|
|
23
|
+
const lines = readline.createInterface({ input: requests })
|
|
24
|
+
|
|
25
|
+
for await (const line of lines) {
|
|
26
|
+
let request
|
|
27
|
+
try {
|
|
28
|
+
request = JSON.parse(line)
|
|
29
|
+
const url = await import.meta.resolve(request.specifier, request.parentURL)
|
|
30
|
+
responses.write(JSON.stringify({ id: request.id, url }) + '\n')
|
|
31
|
+
} catch (error) {
|
|
32
|
+
responses.write(JSON.stringify({
|
|
33
|
+
error: {
|
|
34
|
+
code: error?.code,
|
|
35
|
+
message: String(error?.message ?? error),
|
|
36
|
+
},
|
|
37
|
+
id: request?.id,
|
|
38
|
+
}) + '\n')
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
responses.end()
|
|
42
|
+
`
|
|
43
|
+
|
|
44
|
+
/** @typedef {{ specifier: string, parentURL: string }} StarReexport */
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {{ exportNames: Iterable<string>, starReexports?: StarReexport[] }} ModuleExports
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @typedef {Set<string>|ModuleExports} GetExportsValue
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {[typeof LOAD_OPERATION, URL, object] | [typeof RESOLVE_OPERATION, string, object]} GetExportsOperation
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @typedef {{ done: false, value: GetExportsOperation } | { done: true, value: GetExportsValue }} GetExportsResult
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {{
|
|
64
|
+
* next: (value?: unknown) => GetExportsResult,
|
|
65
|
+
* throw: (error?: unknown) => GetExportsResult,
|
|
66
|
+
* }} GetExportsGenerator
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
class EsmResolver {
|
|
70
|
+
#child
|
|
71
|
+
#closed = false
|
|
72
|
+
#closedPromise
|
|
73
|
+
#failure
|
|
74
|
+
#nextId = 0
|
|
75
|
+
#pending = new Map()
|
|
76
|
+
#readline
|
|
77
|
+
#rejectClosed
|
|
78
|
+
#request
|
|
79
|
+
#resolveClosed
|
|
80
|
+
#stderr = ''
|
|
81
|
+
|
|
82
|
+
constructor () {
|
|
83
|
+
this.#closedPromise = new Promise((resolve, reject) => {
|
|
84
|
+
this.#resolveClosed = resolve
|
|
85
|
+
this.#rejectClosed = reject
|
|
86
|
+
})
|
|
87
|
+
// Mark lifecycle failures handled immediately. close() still returns the original promise.
|
|
88
|
+
this.#closedPromise.catch(() => {})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @param {string} specifier
|
|
93
|
+
* @param {URL|string} parentURL
|
|
94
|
+
* @returns {Promise<string>}
|
|
95
|
+
*/
|
|
96
|
+
resolve (specifier, parentURL) {
|
|
97
|
+
if (this.#closed) return Promise.reject(this.#failure ?? new Error('The ESM resolver is closed'))
|
|
98
|
+
if (this.#pending.size >= MAX_PENDING_REQUESTS) {
|
|
99
|
+
return Promise.reject(new Error(`The ESM resolver has more than ${MAX_PENDING_REQUESTS} pending requests`))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
this.#start()
|
|
104
|
+
} catch (error) {
|
|
105
|
+
this.#failure = error
|
|
106
|
+
this.#closed = true
|
|
107
|
+
this.#resolveClosed()
|
|
108
|
+
return Promise.reject(error)
|
|
109
|
+
}
|
|
110
|
+
const id = this.#nextId++
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
this.#pending.set(id, { reject, resolve })
|
|
113
|
+
this.#request.write(`${JSON.stringify({ id, parentURL: String(parentURL), specifier })}\n`, error => {
|
|
114
|
+
if (error) this.#rejectRequest(id, error)
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @returns {Promise<void>} */
|
|
120
|
+
close () {
|
|
121
|
+
if (!this.#closed) {
|
|
122
|
+
this.#closed = true
|
|
123
|
+
if (this.#child) this.#request.end()
|
|
124
|
+
else this.#resolveClosed()
|
|
125
|
+
}
|
|
126
|
+
return this.#closedPromise
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
#start () {
|
|
130
|
+
if (this.#child) return
|
|
131
|
+
const env = getEnvironmentVariables()
|
|
132
|
+
env.DD_CIVISIBILITY_ENABLED = 'false'
|
|
133
|
+
env.DD_INSTRUMENTATION_TELEMETRY_ENABLED = 'false'
|
|
134
|
+
env.DD_TRACE_ENABLED = 'false'
|
|
135
|
+
this.#child = childProcess.spawn(process.execPath, [
|
|
136
|
+
'--no-warnings',
|
|
137
|
+
'--experimental-import-meta-resolve',
|
|
138
|
+
'--input-type=module',
|
|
139
|
+
'--eval',
|
|
140
|
+
RESOLVER_SOURCE,
|
|
141
|
+
], {
|
|
142
|
+
env,
|
|
143
|
+
stdio: ['ignore', 'ignore', 'pipe', 'pipe', 'pipe'],
|
|
144
|
+
})
|
|
145
|
+
this.#request = this.#child.stdio[3]
|
|
146
|
+
this.#readline = readline.createInterface({ input: this.#child.stdio[4] })
|
|
147
|
+
this.#readline.on('line', this.#handleLine)
|
|
148
|
+
this.#request.once('error', this.#handleError)
|
|
149
|
+
this.#child.stdio[4].once('error', this.#handleError)
|
|
150
|
+
this.#child.stderr.on('data', this.#handleStderr)
|
|
151
|
+
this.#child.once('error', this.#handleError)
|
|
152
|
+
this.#child.once('close', this.#handleClose)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** @param {string} line */
|
|
156
|
+
#handleLine = (line) => {
|
|
157
|
+
let response
|
|
158
|
+
try {
|
|
159
|
+
response = JSON.parse(line)
|
|
160
|
+
} catch (error) {
|
|
161
|
+
this.#fail(new Error(`The ESM resolver returned malformed JSON: ${error.message}`))
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const id = response?.id
|
|
166
|
+
if (!Number.isSafeInteger(id) || !this.#pending.has(id)) {
|
|
167
|
+
this.#fail(new Error('The ESM resolver returned an unknown request identifier'))
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
if (typeof response.url === 'string' && response.error === undefined) {
|
|
171
|
+
const request = this.#pending.get(id)
|
|
172
|
+
this.#pending.delete(id)
|
|
173
|
+
request.resolve(response.url)
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
if (typeof response.error?.message !== 'string' || response.url !== undefined) {
|
|
177
|
+
this.#rejectRequest(id, new Error('The ESM resolver returned a malformed response'))
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const error = /** @type {Error & { code?: string }} */ (new Error(response.error.message))
|
|
182
|
+
if (typeof response.error.code === 'string') error.code = response.error.code
|
|
183
|
+
this.#rejectRequest(id, error)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @param {Buffer|string} chunk */
|
|
187
|
+
#handleStderr = (chunk) => {
|
|
188
|
+
if (this.#stderr.length >= MAX_STDERR_LENGTH) return
|
|
189
|
+
this.#stderr += String(chunk).slice(0, MAX_STDERR_LENGTH - this.#stderr.length)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** @param {Error} error */
|
|
193
|
+
#handleError = (error) => {
|
|
194
|
+
this.#fail(error)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @param {number|null} code
|
|
199
|
+
* @param {string|null} signal
|
|
200
|
+
*/
|
|
201
|
+
#handleClose = (code, signal) => {
|
|
202
|
+
this.#readline?.close()
|
|
203
|
+
this.#closed = true
|
|
204
|
+
let error = this.#failure
|
|
205
|
+
if (!error && (code !== 0 || this.#pending.size > 0)) {
|
|
206
|
+
const reason = signal ? `signal ${signal}` : `status ${code}`
|
|
207
|
+
const stderr = this.#stderr.trim()
|
|
208
|
+
error = new Error(`The ESM resolver exited with ${reason}${stderr ? `: ${stderr}` : ''}`)
|
|
209
|
+
this.#failure = error
|
|
210
|
+
}
|
|
211
|
+
if (error) {
|
|
212
|
+
this.#rejectAll(error)
|
|
213
|
+
this.#rejectClosed(error)
|
|
214
|
+
} else {
|
|
215
|
+
this.#resolveClosed()
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @param {number} id
|
|
221
|
+
* @param {Error} error
|
|
222
|
+
*/
|
|
223
|
+
#rejectRequest (id, error) {
|
|
224
|
+
const request = this.#pending.get(id)
|
|
225
|
+
if (!request) return
|
|
226
|
+
this.#pending.delete(id)
|
|
227
|
+
request.reject(error)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @param {Error} error */
|
|
231
|
+
#fail (error) {
|
|
232
|
+
if (this.#failure) return
|
|
233
|
+
this.#failure = error
|
|
234
|
+
this.#closed = true
|
|
235
|
+
this.#rejectAll(error)
|
|
236
|
+
this.#request?.destroy()
|
|
237
|
+
this.#child?.kill()
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** @param {Error} error */
|
|
241
|
+
#rejectAll (error) {
|
|
242
|
+
for (const request of this.#pending.values()) request.reject(error)
|
|
243
|
+
this.#pending.clear()
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** @returns {EsmResolver} */
|
|
248
|
+
function createEsmResolver () {
|
|
249
|
+
return new EsmResolver()
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Drives the generator returned by import-in-the-middle >=3.1.0 export discovery.
|
|
254
|
+
*
|
|
255
|
+
* @param {GetExportsGenerator} exportsGenerator
|
|
256
|
+
* @param {(url: URL, context: object) => { source: string, format: string }} getSource
|
|
257
|
+
* @param {(specifier: string, context: object) => Promise<{ format: string, url: URL }>} resolve
|
|
258
|
+
* @returns {Promise<ModuleExports>}
|
|
259
|
+
*/
|
|
260
|
+
async function driveGetExportsGenerator (exportsGenerator, getSource, resolve) {
|
|
261
|
+
let next = exportsGenerator.next()
|
|
262
|
+
while (next.done === false) {
|
|
263
|
+
let result
|
|
264
|
+
let failure
|
|
265
|
+
let threw = false
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
const operation = next.value
|
|
269
|
+
const operationType = operation[0]
|
|
270
|
+
|
|
271
|
+
if (operationType === LOAD_OPERATION) {
|
|
272
|
+
result = getSource(operation[1], operation[2])
|
|
273
|
+
} else if (operationType === RESOLVE_OPERATION) {
|
|
274
|
+
// eslint-disable-next-line no-await-in-loop
|
|
275
|
+
result = await resolve(operation[1], operation[2])
|
|
276
|
+
} else {
|
|
277
|
+
throw new Error(`Unsupported import-in-the-middle getExports operation: ${operationType}`)
|
|
278
|
+
}
|
|
279
|
+
} catch (error) {
|
|
280
|
+
threw = true
|
|
281
|
+
failure = error
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
next = threw ? exportsGenerator.throw(failure) : exportsGenerator.next(result)
|
|
285
|
+
}
|
|
286
|
+
return next.value instanceof Set ? { exportNames: next.value } : next.value
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const getExportsImporting = (url) => import(url).then(Object.keys)
|
|
290
|
+
let getExportsModulePromise
|
|
291
|
+
|
|
292
|
+
const loadGetExportsModule = () => {
|
|
293
|
+
if (!getExportsModulePromise) {
|
|
294
|
+
getExportsModulePromise = import('import-in-the-middle/lib/get-exports.mjs')
|
|
295
|
+
}
|
|
296
|
+
return getExportsModulePromise
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @param {URL} srcUrl
|
|
301
|
+
* @param {object} context
|
|
302
|
+
* @param {(url: URL, context: object) => { source: string, format: string }} getSource
|
|
303
|
+
* @param {EsmResolver} resolver
|
|
304
|
+
* @returns {Promise<{
|
|
305
|
+
* exportNames: Iterable<string>,
|
|
306
|
+
* hasModuleExportsCJSDefault: boolean,
|
|
307
|
+
* starReexports?: Array<{ specifier: string, parentURL: string }>
|
|
308
|
+
* }>}
|
|
309
|
+
*/
|
|
310
|
+
const getExports = async (srcUrl, context, getSource, resolver) => {
|
|
311
|
+
const mod = await loadGetExportsModule()
|
|
312
|
+
const exportsGenerator = mod.getExports(srcUrl, context, getSource)
|
|
313
|
+
/**
|
|
314
|
+
* @param {string} specifier
|
|
315
|
+
* @param {{ parentURL: URL }} operationContext
|
|
316
|
+
* @returns {Promise<{ format: string, url: URL }>}
|
|
317
|
+
*/
|
|
318
|
+
const resolve = (specifier, operationContext) => resolveModule(specifier, operationContext, resolver)
|
|
319
|
+
const result = await driveGetExportsGenerator(exportsGenerator, getSource, resolve)
|
|
320
|
+
return {
|
|
321
|
+
exportNames: result.exportNames,
|
|
322
|
+
hasModuleExportsCJSDefault: mod.hasModuleExportsCJSDefault,
|
|
323
|
+
starReexports: result.starReexports,
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function isStarExportLine (line) {
|
|
328
|
+
return /^\* from /.test(line)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function isBareSpecifier (specifier) {
|
|
332
|
+
// Relative and absolute paths are not bare specifiers.
|
|
333
|
+
if (
|
|
334
|
+
specifier.startsWith('.') ||
|
|
335
|
+
specifier.startsWith('/')) {
|
|
336
|
+
return false
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Valid URLs are not bare specifiers. (file:, http:, node:, etc.)
|
|
340
|
+
|
|
341
|
+
if (URL.hasOwnProperty('canParse')) {
|
|
342
|
+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
343
|
+
return !URL.canParse(specifier)
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
try {
|
|
347
|
+
// eslint-disable-next-line no-new
|
|
348
|
+
new URL(specifier)
|
|
349
|
+
return false
|
|
350
|
+
} catch {
|
|
351
|
+
return true
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* @param {string} modulePath
|
|
357
|
+
* @param {string} format
|
|
358
|
+
*/
|
|
359
|
+
function getModuleFormat (modulePath, format) {
|
|
360
|
+
if (modulePath.endsWith('.mts')) return 'module-typescript'
|
|
361
|
+
if (modulePath.endsWith('.cts')) return 'commonjs-typescript'
|
|
362
|
+
if (modulePath.endsWith('.ts')) return `${format}-typescript`
|
|
363
|
+
return format
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Resolves a module with the import conditions used by ESM instrumentation.
|
|
368
|
+
*
|
|
369
|
+
* @param {string} specifier
|
|
370
|
+
* @param {{ parentURL: URL }} context
|
|
371
|
+
* @param {EsmResolver} resolver
|
|
372
|
+
* @returns {Promise<{ format: string, url: URL }>}
|
|
373
|
+
*/
|
|
374
|
+
async function resolveModule (specifier, context, resolver) {
|
|
375
|
+
const url = new URL(await resolver.resolve(specifier, context.parentURL))
|
|
376
|
+
if (url.protocol === 'node:') return { format: 'builtin', url }
|
|
377
|
+
if (url.protocol !== 'file:') throw new Error(`Unsupported ESM resolution URL: ${url.href}`)
|
|
378
|
+
|
|
379
|
+
const resolved = fileURLToPath(url)
|
|
380
|
+
if (resolved.endsWith('.node')) {
|
|
381
|
+
throw new Error(`Unsupported ESM analysis target: ${resolved}`)
|
|
382
|
+
}
|
|
383
|
+
const format = isESMFile(resolved) ? 'module' : 'commonjs'
|
|
384
|
+
return { format, url }
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Generates the pieces of code for the proxy module before the path
|
|
389
|
+
*
|
|
390
|
+
* @param {object} moduleData
|
|
391
|
+
* @param {string} moduleData.path
|
|
392
|
+
* @param {boolean} [moduleData.internal]
|
|
393
|
+
* @param {object} moduleData.context
|
|
394
|
+
* @param {boolean} [moduleData.excludeDefault]
|
|
395
|
+
* @param {Map<string, string>} [moduleData.moduleSources]
|
|
396
|
+
* @param {EsmResolver} [moduleData.resolver]
|
|
397
|
+
* @param {(source: string, options: { loader: 'ts' }) => { code: string }} [moduleData.transform]
|
|
398
|
+
* @param {Set<string>} [activeModules]
|
|
399
|
+
* @returns {Promise<Map>}
|
|
400
|
+
*/
|
|
401
|
+
async function processModule (
|
|
402
|
+
{ path, internal = false, context, excludeDefault = false, moduleSources = new Map(), resolver, transform },
|
|
403
|
+
activeModules
|
|
404
|
+
) {
|
|
405
|
+
const ownsResolver = resolver === undefined
|
|
406
|
+
resolver ??= createEsmResolver()
|
|
407
|
+
try {
|
|
408
|
+
return await processModuleWithResolver(
|
|
409
|
+
{ path, internal, context, excludeDefault, moduleSources, transform },
|
|
410
|
+
activeModules,
|
|
411
|
+
resolver
|
|
412
|
+
)
|
|
413
|
+
} finally {
|
|
414
|
+
if (ownsResolver) await resolver.close()
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* @param {object} moduleData
|
|
420
|
+
* @param {string} moduleData.path
|
|
421
|
+
* @param {boolean} moduleData.internal
|
|
422
|
+
* @param {object} moduleData.context
|
|
423
|
+
* @param {boolean} moduleData.excludeDefault
|
|
424
|
+
* @param {Map<string, string>} moduleData.moduleSources
|
|
425
|
+
* @param {(source: string, options: { loader: 'ts' }) => { code: string }} [moduleData.transform]
|
|
426
|
+
* @param {Set<string>} [activeModules]
|
|
427
|
+
* @param {EsmResolver} resolver
|
|
428
|
+
* @returns {Promise<Map>}
|
|
429
|
+
*/
|
|
430
|
+
async function processModuleWithResolver (
|
|
431
|
+
{ path, internal, context, excludeDefault, moduleSources, transform },
|
|
432
|
+
activeModules,
|
|
433
|
+
resolver
|
|
434
|
+
) {
|
|
435
|
+
let moduleExports, srcUrl
|
|
436
|
+
if (internal) {
|
|
437
|
+
// we can not read and parse of internal modules
|
|
438
|
+
moduleExports = { exportNames: await getExportsImporting(path) }
|
|
439
|
+
} else {
|
|
440
|
+
context = { ...context, format: getModuleFormat(path, context.format) }
|
|
441
|
+
srcUrl = pathToFileURL(path)
|
|
442
|
+
const readSource = (url) => {
|
|
443
|
+
const modulePath = fileURLToPath(url)
|
|
444
|
+
let source = moduleSources.get(modulePath)
|
|
445
|
+
if (source === undefined) {
|
|
446
|
+
source = fs.readFileSync(modulePath, 'utf8')
|
|
447
|
+
moduleSources.set(modulePath, source)
|
|
448
|
+
}
|
|
449
|
+
return source
|
|
450
|
+
}
|
|
451
|
+
const loadSource = (url, { format }) => {
|
|
452
|
+
let source = readSource(url)
|
|
453
|
+
if (transform && (format === 'module-typescript' || format === 'commonjs-typescript')) {
|
|
454
|
+
source = transform(source, { loader: 'ts' }).code
|
|
455
|
+
format = format === 'module-typescript' ? 'module' : 'commonjs'
|
|
456
|
+
}
|
|
457
|
+
return { format, source }
|
|
458
|
+
}
|
|
459
|
+
readSource(srcUrl)
|
|
460
|
+
moduleExports = await getExports(srcUrl, context, loadSource, resolver)
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const starExports = new Set()
|
|
464
|
+
const setters = new Map()
|
|
465
|
+
|
|
466
|
+
const addSetter = (name, setter, isStarExport = false) => {
|
|
467
|
+
if (setters.has(name)) {
|
|
468
|
+
if (isStarExport) {
|
|
469
|
+
// If there's already a matching star export, delete it
|
|
470
|
+
if (starExports.has(name)) {
|
|
471
|
+
setters.delete(name)
|
|
472
|
+
}
|
|
473
|
+
// and return so this is excluded
|
|
474
|
+
return
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// if we already have this export but it is from a * export, overwrite it
|
|
478
|
+
if (starExports.has(name)) {
|
|
479
|
+
starExports.delete(name)
|
|
480
|
+
setters.set(name, setter)
|
|
481
|
+
}
|
|
482
|
+
} else {
|
|
483
|
+
// Store export * exports so we know they can be overridden by explicit
|
|
484
|
+
// named exports
|
|
485
|
+
if (isStarExport) {
|
|
486
|
+
starExports.add(name)
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
setters.set(name, setter)
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
let starReexports = moduleExports.starReexports
|
|
494
|
+
for (const n of moduleExports.exportNames) {
|
|
495
|
+
const isDefault = n === 'default' || (
|
|
496
|
+
n === 'module.exports' &&
|
|
497
|
+
moduleExports.hasModuleExportsCJSDefault &&
|
|
498
|
+
(context.format === 'commonjs' || context.format === 'commonjs-typescript')
|
|
499
|
+
)
|
|
500
|
+
if (isDefault && excludeDefault) continue
|
|
501
|
+
|
|
502
|
+
if (isStarExportLine(n)) {
|
|
503
|
+
starReexports ??= []
|
|
504
|
+
starReexports.push({ parentURL: srcUrl.href, specifier: n.slice('* from '.length) })
|
|
505
|
+
continue
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const variableName = `$dd${Buffer.from(n).toString('hex')}`
|
|
509
|
+
const objectKey = JSON.stringify(n)
|
|
510
|
+
const reExportedName = n === 'default' ? n : objectKey
|
|
511
|
+
|
|
512
|
+
addSetter(n, `
|
|
513
|
+
let ${variableName}
|
|
514
|
+
try {
|
|
515
|
+
${variableName} = _[${objectKey}] = namespace[${objectKey}]
|
|
516
|
+
} catch (error) {
|
|
517
|
+
if (!(error instanceof ReferenceError)) throw error
|
|
518
|
+
}
|
|
519
|
+
export { ${variableName} as ${reExportedName} }
|
|
520
|
+
set[${objectKey}] = (v) => {
|
|
521
|
+
${variableName} = v
|
|
522
|
+
return true
|
|
523
|
+
}
|
|
524
|
+
get[${objectKey}] = () => ${variableName}
|
|
525
|
+
`)
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (starReexports) {
|
|
529
|
+
for (const { parentURL, specifier } of starReexports) {
|
|
530
|
+
const baseUrl = new URL(parentURL)
|
|
531
|
+
const resolvedSpecifier = isBareSpecifier(specifier) ? specifier : new URL(specifier, baseUrl).href
|
|
532
|
+
// The runtime's import conditions and the declaring module's URL own star-export resolution.
|
|
533
|
+
// eslint-disable-next-line no-await-in-loop
|
|
534
|
+
const result = await resolveModule(resolvedSpecifier, { parentURL: baseUrl }, resolver)
|
|
535
|
+
|
|
536
|
+
activeModules ??= new Set([srcUrl.href])
|
|
537
|
+
if (activeModules.has(result.url.href)) continue
|
|
538
|
+
activeModules.add(result.url.href)
|
|
539
|
+
|
|
540
|
+
// eslint-disable-next-line no-await-in-loop
|
|
541
|
+
const subSetters = await processModuleWithResolver({
|
|
542
|
+
path: result.format === 'builtin' ? result.url.href : fileURLToPath(result.url),
|
|
543
|
+
internal: result.format === 'builtin',
|
|
544
|
+
context: { ...context, format: result.format },
|
|
545
|
+
excludeDefault: true,
|
|
546
|
+
moduleSources,
|
|
547
|
+
transform,
|
|
548
|
+
}, activeModules, resolver)
|
|
549
|
+
activeModules.delete(result.url.href)
|
|
550
|
+
|
|
551
|
+
for (const [name, setter] of subSetters.entries()) {
|
|
552
|
+
addSetter(name, setter, true)
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return setters
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Generates the IITM registration wrapper used by bundlers. Export bindings come from processModule; IITM owns the
|
|
562
|
+
* export proxy and hook semantics, just as it does for modules wrapped by its Node.js loader.
|
|
563
|
+
*
|
|
564
|
+
* @param {{ path: string, internal?: boolean, specifier?: string, setters: Iterable<string> }} options
|
|
565
|
+
*/
|
|
566
|
+
function createEsmWrapper ({ path, internal, specifier, setters }) {
|
|
567
|
+
const iitmPath = require.resolve('import-in-the-middle/lib/register.js')
|
|
568
|
+
const toRegister = internal ? path : pathToFileURL(path)
|
|
569
|
+
return `
|
|
570
|
+
import { register } from ${JSON.stringify(iitmPath)};
|
|
571
|
+
import * as namespace from ${JSON.stringify(path)};
|
|
572
|
+
const _ = Object.create(null, { [Symbol.toStringTag]: { value: 'Module' } });
|
|
573
|
+
const set = {};
|
|
574
|
+
const get = {};
|
|
575
|
+
|
|
576
|
+
${[...setters].join(';\n')};
|
|
577
|
+
|
|
578
|
+
register(${JSON.stringify(toRegister)}, _, set, get, ${JSON.stringify(specifier)});
|
|
579
|
+
`
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
module.exports = { createEsmResolver, createEsmWrapper, driveGetExportsGenerator, processModule }
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { format } = require('node:util')
|
|
4
|
+
|
|
5
|
+
const noop = () => {}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} name
|
|
9
|
+
* @returns {import('./bundler-utils').BundlerLog}
|
|
10
|
+
*/
|
|
11
|
+
function createBundlerLog (name) {
|
|
12
|
+
// eslint-disable-next-line eslint-rules/eslint-process-env
|
|
13
|
+
const debug = (process.env.DD_TRACE_DEBUG || '').trim().toLowerCase()
|
|
14
|
+
if (debug !== 'true' && debug !== '1') return { debug: noop, warn: noop }
|
|
15
|
+
|
|
16
|
+
const prefix = `[dd-trace/${name}]`
|
|
17
|
+
return {
|
|
18
|
+
debug (...args) {
|
|
19
|
+
// eslint-disable-next-line no-console
|
|
20
|
+
console.log(formatWithLogPrefix(prefix, ...args))
|
|
21
|
+
},
|
|
22
|
+
warn (...args) {
|
|
23
|
+
// eslint-disable-next-line no-console
|
|
24
|
+
console.warn(formatWithLogPrefix(`${prefix} Warning:`, ...args))
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @param {string} prefix
|
|
31
|
+
* @param {unknown} [str]
|
|
32
|
+
* @param {...unknown} args
|
|
33
|
+
*/
|
|
34
|
+
function formatWithLogPrefix (prefix, str, ...args) {
|
|
35
|
+
return typeof str === 'string' ? format(`${prefix} ${str}`, ...args) : format(prefix, str, ...args)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = createBundlerLog
|