Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const path = require('path')
const parse = require('module-details-from-path')
const { fileURLToPath } = require('url')
const { MessageChannel } = require('worker_threads')
const { isBuiltin } = require('module')

const {
importHooks,
Expand Down Expand Up @@ -124,11 +125,17 @@ function Hook (modules, options, hookFn) {

this._iitmHook = (name, namespace, specifier) => {
const filename = name
const isBuiltin = name.startsWith('node:')
const isNodeUrl = name.startsWith('node:')
let baseDir

if (isBuiltin) {
name = name.replace(/^node:/, '')
if (isNodeUrl) {
// Normalize builtin module name to *not* have 'node:' prefix, unless
// required, as it is for 'node:test' and some others. `module.isBuiltin`
// is available in all Node.js versions that have node:-only modules.
const unprefixed = name.slice(5)
if (typeof isBuiltin !== 'function' || isBuiltin(unprefixed)) {
Comment thread
trentm marked this conversation as resolved.
Outdated
name = unprefixed
}
} else {
if (name.startsWith('file://')) {
const stackTraceLimit = Error.stackTraceLimit
Expand Down
12 changes: 12 additions & 0 deletions test/hook/v18.15-node-prefixed-only.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Test that a builtin module that must use 'node:' prefix works.
import { strictEqual } from 'assert'
import Hook from '../../index.js'

Hook(['node:test'], (exports, name) => {
if (name === 'node:test') {
exports.skip = 'iitm was here'
}
})

const { skip } = await import('node:test')
strictEqual(skip, 'iitm was here')