Skip to content
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

Merged
merged 4 commits into from
Aug 10, 2022

Conversation

trentm
Copy link
Member

@trentm trentm commented Aug 10, 2022

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


@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.

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
@trentm trentm requested a review from astorm August 10, 2022 00:01
@trentm trentm self-assigned this Aug 10, 2022
@trentm trentm removed the request for review from astorm August 10, 2022 00:04
@trentm
Copy link
Member Author

trentm commented Aug 10, 2022

I'll re-request review once I get tests passing.

@trentm
Copy link
Member Author

trentm commented Aug 10, 2022

Can't actually have a CI test with node v6.0 because the npm install fails with:

...
npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror     at andLogAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:31:3)
...
Full npm install error
% npm install
npm ERR! Darwin 21.6.0
npm ERR! argv "/Users/trentm/.nvm/versions/node/v6.0.0/bin/node" "/Users/trentm/.nvm/versions/node/v6.0.0/bin/npm" "install"
npm ERR! node v6.0.0
npm ERR! npm  v3.8.6
npm ERR! code EMISSINGARG

npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror     at andLogAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:31:3)
npm ERR! typeerror     at fetchPackageMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:51:22)
npm ERR! typeerror     at resolveWithNewModule (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:455:12)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:456:7
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:37:12
npm ERR! typeerror     at addRequestedAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:82:5)
npm ERR! typeerror     at returnAndAddMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:117:7)
npm ERR! typeerror     at pickVersionFromRegistryDocument (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:134:20)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror This is an error with npm itself. Please report this error at:
npm ERR! typeerror     <https://github.com/npm/npm/issues>
npm ERR! Darwin 21.6.0
npm ERR! argv "/Users/trentm/.nvm/versions/node/v6.0.0/bin/node" "/Users/trentm/.nvm/versions/node/v6.0.0/bin/npm" "install"
npm ERR! node v6.0.0
npm ERR! npm  v3.8.6
npm ERR! code EMISSINGARG

npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror     at andLogAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:31:3)
npm ERR! typeerror     at fetchPackageMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:51:22)
npm ERR! typeerror     at resolveWithNewModule (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:455:12)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:456:7
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:37:12
npm ERR! typeerror     at addRequestedAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:82:5)
npm ERR! typeerror     at returnAndAddMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:117:7)
npm ERR! typeerror     at pickVersionFromRegistryDocument (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:134:20)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror This is an error with npm itself. Please report this error at:
npm ERR! typeerror     <https://github.com/npm/npm/issues>
npm ERR! Darwin 21.6.0
npm ERR! argv "/Users/trentm/.nvm/versions/node/v6.0.0/bin/node" "/Users/trentm/.nvm/versions/node/v6.0.0/bin/npm" "install"
npm ERR! node v6.0.0
npm ERR! npm  v3.8.6
npm ERR! code EMISSINGARG

npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror     at andLogAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:31:3)
npm ERR! typeerror     at fetchPackageMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:51:22)
npm ERR! typeerror     at resolveWithNewModule (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:455:12)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:456:7
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:37:12
npm ERR! typeerror     at addRequestedAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:82:5)
npm ERR! typeerror     at returnAndAddMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:117:7)
npm ERR! typeerror     at pickVersionFromRegistryDocument (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:134:20)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror This is an error with npm itself. Please report this error at:
npm ERR! typeerror     <https://github.com/npm/npm/issues>
npm ERR! Darwin 21.6.0
npm ERR! argv "/Users/trentm/.nvm/versions/node/v6.0.0/bin/node" "/Users/trentm/.nvm/versions/node/v6.0.0/bin/npm" "install"
npm ERR! node v6.0.0
npm ERR! npm  v3.8.6
npm ERR! code EMISSINGARG

npm ERR! typeerror Error: Missing required argument #1
npm ERR! typeerror     at andLogAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:31:3)
npm ERR! typeerror     at fetchPackageMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:51:22)
npm ERR! typeerror     at resolveWithNewModule (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:455:12)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/install/deps.js:456:7
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:37:12
npm ERR! typeerror     at addRequestedAndFinish (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:82:5)
npm ERR! typeerror     at returnAndAddMetadata (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:117:7)
npm ERR! typeerror     at pickVersionFromRegistryDocument (/Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/lib/fetch-package-metadata.js:134:20)
npm ERR! typeerror     at /Users/trentm/.nvm/versions/node/v6.0.0/lib/node_modules/npm/node_modules/iferr/index.js:13:50
npm ERR! typeerror This is an error with npm itself. Please report this error at:
npm ERR! typeerror     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/trentm/el/require-in-the-middle2/npm-debug.log

@trentm trentm requested a review from astorm August 10, 2022 17:00
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]
Copy link
Member Author

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.

@trentm trentm added the agent-nodejs Make available for APM Agents project planning. label Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants