Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
10 changes: 10 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ function createHook (meta) {
if (data) {
includeModules = ensureArrayWithBareSpecifiersAndFileUrls(data.include, 'include')
excludeModules = ensureArrayWithBareSpecifiersAndFileUrls(data.exclude, 'exclude')

if (data.addHookMessagePort) {
data.addHookMessagePort.on('message', (modules) => {
if (includeModules === undefined) {
includeModules = []
}

includeModules.push(...modules)
})
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,21 @@ export declare function addHook(hookFn: HookFunction): void
* @param {HookFunction} hookFn The function to be removed.
*/
export declare function removeHook(hookFn: HookFunction): void

/**
* Creates a message channel with a port that can be used to add hooks to the
* list of exclusively included modules.
*
* This can be used to only wrap modules that are Hook'ed, however modules need
* to be hooked before they are imported.
*
* ```ts
* import { register } from 'module'
* import { createAddHookMessageChannel } from 'import-in-the-middle'
*
* const addHookMessagePort = createAddHookMessageChannel()
*
* register('import-in-the-middle/hook.mjs', import.meta.url, { data: { addHookMessagePort }, transferList: [addHookMessagePort] })
* ```
*/
export declare function createAddHookMessageChannel(): MessagePort;
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const path = require('path')
const parse = require('module-details-from-path')
const { fileURLToPath } = require('url')
const { MessageChannel } = require('worker_threads')

const {
importHooks,
Expand All @@ -31,6 +32,18 @@ function callHookFn (hookFn, namespace, name, baseDir) {
}
}

let sendToMessageChannel

function createAddHookMessageChannel () {
const { port1, port2 } = new MessageChannel()

sendToMessageChannel = (modules) => {
port1.postMessage(modules)
}

return port2
}

function Hook (modules, options, hookFn) {
if ((this instanceof Hook) === false) return new Hook(modules, options, hookFn)
if (typeof modules === 'function') {
Expand All @@ -43,6 +56,10 @@ function Hook (modules, options, hookFn) {
}
const internals = options ? options.internals === true : false

if (sendToMessageChannel && Array.isArray(modules)) {
sendToMessageChannel(modules)
}

this._iitmHook = (name, namespace) => {
const filename = name
const isBuiltin = name.startsWith('node:')
Expand Down Expand Up @@ -92,3 +109,4 @@ module.exports = Hook
module.exports.Hook = Hook
module.exports.addHook = addHook
module.exports.removeHook = removeHook
module.exports.createAddHookMessageChannel = createAddHookMessageChannel
13 changes: 13 additions & 0 deletions test/fixtures/import-after.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { strictEqual } from 'assert'
import { sep } from 'path'
import { Hook } from '../../index.js'

const hooked = []

Hook((_, name) => {
hooked.push(name)
})

strictEqual(hooked.length, 1)
strictEqual(hooked[0], 'path')
strictEqual(sep, '@')
14 changes: 14 additions & 0 deletions test/fixtures/import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { register } from 'module'
import { Hook, createAddHookMessageChannel } from '../../index.js'
// We've imported path here to ensure that the hook is still applied later.
import * as path from 'path'

const addHookMessagePort = createAddHookMessageChannel()

register('../../hook.mjs', import.meta.url, { data: { addHookMessagePort }, transferList: [addHookMessagePort] })

Hook(['path'], (exports) => {
exports.sep = '@'
})

console.assert(path.sep !== '@')
7 changes: 7 additions & 0 deletions test/register/v18.19-include-message-port.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { spawnSync } from 'child_process'

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