Skip to content

Commit

Permalink
feat: Support absolute paths for include
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Dec 11, 2024
1 parent 9bd539e commit 68eafc3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const { URL } = require('url')
const { URL, fileURLToPath } = require('url')
const { inspect } = require('util')
const { builtinModules } = require('module')
const specifiers = new Map()
Expand Down Expand Up @@ -341,7 +341,7 @@ function createHook (meta) {
return each.test(result.url)
}

return each === specifier || each === result.url
return each === specifier || each === result.url || (result.url.startsWith('file:') && each === fileURLToPath(result.url))
}

if (includeModules && !includeModules.some(match)) {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/import-absolute-after.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { strictEqual } from 'assert'
import * as foo from './foo.mjs'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'

const fooPath = join(dirname(fileURLToPath(import.meta.url)), 'foo.mjs')

strictEqual(typeof foo.foo, 'function')
strictEqual(global.hooked.length, 1)
strictEqual(global.hooked[0], fooPath)
18 changes: 18 additions & 0 deletions test/fixtures/import-absolute.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { register } from 'module'
import { Hook, createAddHookMessageChannel } from '../../index.js'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'

const fooPath = join(dirname(fileURLToPath(import.meta.url)), 'foo.mjs')

const { registerOptions, waitForAllMessagesAcknowledged } = createAddHookMessageChannel()

register('../../hook.mjs', import.meta.url, registerOptions)

global.hooked = []

Hook([fooPath], (_, name) => {
global.hooked.push(name)
})

await waitForAllMessagesAcknowledged()
14 changes: 14 additions & 0 deletions test/register/v18.19-include-message-port-absolute-path.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { spawnSync } from 'child_process'

const out = spawnSync(process.execPath,
['--import', './test/fixtures/import-absolute.mjs', './test/fixtures/import-absolute-after.mjs'],
{ stdio: 'inherit', env: {} }
)

if (out.error) {
console.error(out.error)
}
if (out.status !== 0) {
console.error(`Expected exit code 0, got ${out.status}`)
}
process.exit(out.status)

0 comments on commit 68eafc3

Please sign in to comment.