Skip to content

Commit 2af8199

Browse files
committed
more resilient builtin require check
1 parent d0a0e91 commit 2af8199

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

packages/datadog-esbuild/index.js

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
const NAMESPACE = 'datadog'
22

33
const instrumented = Object.keys(require('../datadog-instrumentations/src/helpers/hooks.js'))
4-
const builtins = new Set(require('module').builtinModules)
5-
const packages = new Set()
4+
const rawBuiltins = require('module').builtinModules
5+
6+
const builtins = new Set()
7+
8+
for (const builtin of rawBuiltins) {
9+
builtins.add(builtin)
10+
builtins.add(`node:${builtin}`)
11+
}
12+
13+
const packagesOfInterest = new Set()
614

715
const DEBUG = !!process.env.DD_TRACE_DEBUG
816

@@ -12,7 +20,7 @@ const DEBUG = !!process.env.DD_TRACE_DEBUG
1220
for (const pkg of instrumented) {
1321
if (builtins.has(pkg)) continue
1422
if (pkg.startsWith('node:')) continue
15-
packages.add(pkg)
23+
packagesOfInterest.add(pkg)
1624
}
1725

1826
const DC_CHANNEL = 'dd-trace:bundledModuleLoadStart'
@@ -23,7 +31,7 @@ module.exports.setup = function (build) {
2331
build.onResolve({ filter: /.*/ }, args => {
2432
const packageName = args.path
2533

26-
if (args.namespace === 'file' && packages.has(packageName)) {
34+
if (args.namespace === 'file' && packagesOfInterest.has(packageName)) {
2735
// The file namespace is used when requiring files from disk in userland
2836
const pathToPackageJson = require.resolve(`${packageName}/package.json`, { paths: [ args.resolveDir ] })
2937
const pkg = require(pathToPackageJson)
@@ -45,7 +53,7 @@ module.exports.setup = function (build) {
4553
// The datadog namespace is used when requiring files that are injected during the onLoad stage
4654
// see note in onLoad
4755

48-
if (packageName.startsWith('node:')) return
56+
if (builtins.has(packageName)) return
4957

5058
return {
5159
path: require.resolve(packageName, { paths: [ args.resolveDir ] }),
@@ -59,8 +67,7 @@ module.exports.setup = function (build) {
5967
// eslint-disable-next-line no-console
6068
console.log(`load ${args.path}@${args.pluginData.version}`)
6169
}
62-
// TODO: relying on prefixing internal packages with `node:` in this intermediary module for now.
63-
// If this causes an issue we'll need to update the logic for determining if a module is internal.
70+
6471
// JSON.stringify adds double quotes. For perf gain could simply add in quotes when we know it's safe.
6572
const contents = `
6673
const dc = require('node:diagnostics_channel');
@@ -71,29 +78,13 @@ module.exports.setup = function (build) {
7178
path: ${JSON.stringify(args.path)},
7279
version: ${JSON.stringify(args.pluginData.version)}
7380
};
74-
// if (!ch.hasSubscribers) console.error('missing subscriber! ${JSON.stringify(DC_CHANNEL + ':' + args.path)}');
75-
ch.publish(payload); // allow subscriber to mutate payload
81+
ch.publish(payload);
7682
module.exports = payload.module;
77-
// module.exports.__DATADOG_VERSION = ${JSON.stringify(args.pluginData.version)};
7883
`
7984
// https://esbuild.github.io/plugins/#on-load-results
8085
return {
8186
contents,
8287
loader: 'js'
8388
}
8489
})
85-
}
86-
87-
/**
88-
* This could be a convenience to expose a list of known externals to the application.
89-
* Devs are free to use this list, ignore it, or merge it with their application-specific list.
90-
* TODO: Sadly, esbuild does not allow unanticipated keys on the exported object.
91-
* Could do `{ plugin: { name, setup }, knownExternals }`
92-
* and have app call `plugins: [require('dd-trace/esbuild').plugin]` instead...
93-
*/
94-
// module.exports.knownExternals = [
95-
// 'pg-native', // peer dep
96-
// 'graphql/language/visitor',
97-
// 'graphql/language/printer',
98-
// 'graphql/utilities',
99-
// ];
90+
}

0 commit comments

Comments
 (0)