Skip to content

Commit

Permalink
feat: Allow passing of include or exclude list via register
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 3, 2024
1 parent 29c77b5 commit 035be54
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
43 changes: 30 additions & 13 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ function addIitm (url) {
function createHook (meta) {
let cachedResolve
const iitmURL = new URL('lib/register.js', meta.url).toString()
let includeLibs, excludeLibs

async function initialize (options) {
if (options) {
includeLibs = options && Array.isArray(options.include) ? options.include : undefined
excludeLibs = options && Array.isArray(options.exclude) ? options.exclude : undefined
}
}

async function resolve (specifier, context, parentResolve) {
cachedResolve = parentResolve
Expand All @@ -234,39 +242,47 @@ function createHook (meta) {
if (isWin && parentURL.indexOf('file:node') === 0) {
context.parentURL = ''
}
const url = await parentResolve(newSpecifier, context, parentResolve)
if (parentURL === '' && !EXTENSION_RE.test(url.url)) {
entrypoint = url.url
return { url: url.url, format: 'commonjs' }
const result = await parentResolve(newSpecifier, context, parentResolve)
if (parentURL === '' && !EXTENSION_RE.test(result.url)) {
entrypoint = result.url
return { url: result.url, format: 'commonjs' }
}

if (includeLibs && !includeLibs.some(lib => lib === specifier || lib === result.url.url)) {
return result
}

if (excludeLibs && excludeLibs.some(lib => lib === specifier || lib === result.url.url)) {
return result
}

if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
return result
}

// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
(context.importAttributes && context.importAttributes.type === 'json')
) {
return url
return result
}

// If the file is referencing itself, we need to skip adding the iitm search params
if (url.url === parentURL) {
if (result.url === parentURL) {
return {
url: url.url,
url: result.url,
shortCircuit: true,
format: url.format
format: result.format
}
}

specifiers.set(url.url, specifier)
specifiers.set(result.url, specifier)

return {
url: addIitm(url.url),
url: addIitm(result.url),
shortCircuit: true,
format: url.format
format: result.format
}
}

Expand Down Expand Up @@ -337,9 +353,10 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
}

if (NODE_MAJOR >= 17 || (NODE_MAJOR === 16 && NODE_MINOR >= 12)) {
return { load, resolve }
return { initialize, load, resolve }
} else {
return {
initialize,
load,
resolve,
getSource,
Expand Down
4 changes: 2 additions & 2 deletions hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

import { createHook } from './hook.js'

const { load, resolve, getFormat, getSource } = createHook(import.meta)
const { initialize, load, resolve, getFormat, getSource } = createHook(import.meta)

export { load, resolve, getFormat, getSource }
export { initialize, load, resolve, getFormat, getSource }
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Intercept imports in Node.js",
"main": "index.js",
"scripts": {
"test": "c8 --reporter lcov --check-coverage --lines 50 imhotap --files test/{hook,low-level,other,get-esm-exports}/*",
"test": "c8 --reporter lcov --check-coverage --lines 50 imhotap --files test/{hook,low-level,other,get-esm-exports,register}/*",
"test:e2e": "node test/check-exports/test.mjs",
"test:ts": "c8 --reporter lcov imhotap --files test/typescript/*.test.mts",
"coverage": "c8 --reporter html imhotap --files test/{hook,low-level,other,get-esm-exports}/* && echo '\nNow open coverage/index.html\n'",
Expand Down
4 changes: 2 additions & 2 deletions test/generic-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from 'path'

const filename = process.env.IITM_TEST_FILE

export const { load, resolve, getFormat, getSource } =
filename.includes('disabled')
export const { initialize, load, resolve, getFormat, getSource } =
filename.includes('disabled') || filename.includes('register')
? {}
: (path.extname(filename).slice(-2) === 'ts' ? tsLoader : regularLoader)
15 changes: 15 additions & 0 deletions test/register/exclude.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { register } from 'module'
import Hook from '../../index.js'
import { strictEqual } from 'assert'

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

const hooked = []

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

await import('openai')

strictEqual(hooked.includes('util'), false)
16 changes: 16 additions & 0 deletions test/register/include.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { register } from 'module'
import Hook from '../../index.js'
import { strictEqual } from 'assert'

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

const hooked = []

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

await import('openai')

strictEqual(hooked.length, 1)
strictEqual(hooked[0], 'openai')

0 comments on commit 035be54

Please sign in to comment.