-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support 'node:'-prefixed core modules #53
Conversation
Since v16.0.0, v14.18.0 node has supported a 'node:'-prefix on require() of core modules. They typically are an alias for unprefixed builtin modules, e.g. 'http' and 'node:http' yield the same module. However, new core modules may be added only with the prefix, e.g. 'node:test' added in node v18.0.0. https://nodejs.org/api/modules.html#core-modules This change adds support for a require-in-the-middle hook to capture the node-prefixed aliases. I.e. `Hook(['http', ...], onRequire)` will hook into `require('node:http')` and `require('http')`. Refs: elastic/apm-agent-nodejs#2816
I'll re-request review once I get tests passing. |
Can't actually have a CI test with node v6.0 because the
Full npm install error
|
isCore = moduleName => { | ||
// Prefer `resolve.core` lookup to `resolve.isCore(moduleName)` because the | ||
// latter is doing version range matches for every call. | ||
return !!resolve.core[moduleName] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
REVIEW NOTE: The resolve
module has a core
property that looks like this:
> process.version
'v16.16.0'
> resolve.core
{
assert: true,
'node:assert': true,
'assert/strict': true,
'node:assert/strict': true,
async_hooks: true,
'node:async_hooks': true,
...
'node:test': false,
...
Those true/false values are resolved from node version ranges against the current process.version
. For example node:test
is not a core module in node v16.16.0 that I was running this with. This makes resolve.core
a fast way to look up if a given module id is core for the current node version. This obviates the need for #42. (@harttle sorry that your PR for this never got merged.) It is also available back to the supported node v6, so we don't need the filename.includes(path.sep)
fallback.
From https://nodejs.org/api/modules.html#core-modules
The list of core modules that can be loaded without using the node: prefix is exposed as module.builtinModules.
In node v18, module.builtinModules
does not include node:test
. So, our replacing usage of module.builtinModules
handles this case as well.
Since v16.0.0, v14.18.0 node has supported a 'node:'-prefix on
require() of core modules. They typically are an alias for unprefixed
builtin modules, e.g. 'http' and 'node:http' yield the same module.
However, new core modules may be added only with the prefix, e.g.
'node:test' added in node v18.0.0.
https://nodejs.org/api/modules.html#core-modules
This change adds support for a require-in-the-middle hook to capture
the node-prefixed aliases. I.e.
Hook(['http', ...], onRequire)
willhook into
require('node:http')
andrequire('http')
.Refs: elastic/apm-agent-nodejs#2816
@astorm Would you like to review this? I'll get a draft PR for elastic/apm-agent-nodejs#2816 as well that includes a test case of instrumenting
require('node:http')
once we have a release of this the APM agent can upgrade to.