Skip to content

Commit

Permalink
added optional argument to specify a list of modules to patch to crea…
Browse files Browse the repository at this point in the history
…teHook
  • Loading branch information
ida613 committed Mar 25, 2024
1 parent 7826ef8 commit 878fadb
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,38 +228,66 @@ function addIitm (url) {
return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
}

function createHook (meta) {
async function resolve (specifier, context, parentResolve) {
const { parentURL = '' } = context
const newSpecifier = deleteIitm(specifier)
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' }
// moduleList is an optional Map specifiying which modules need IITM patching
// format: { moduleName : ['/file1.js', '/file2.js'] }
function createHook (meta, moduleList={}) {

Check failure on line 233 in hook.js

View workflow job for this annotation

GitHub Actions / lint

Operator '=' must be spaced

Check failure on line 233 in hook.js

View workflow job for this annotation

GitHub Actions / lint

Operator '=' must be spaced
async function resolve (specifier, context, parentResolve, moduleList) {
let patch = true
if (moduleList.size) {
patch = false // do not patch unless specifier is in moduleList
if (moduleList.has(specifier)) { // if specifier is a module name present in moduleList
patch = true
} else {
for (let mod of moduleList.key) {

Check failure on line 241 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'mod' is never reassigned. Use 'const' instead

Check failure on line 241 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'mod' is never reassigned. Use 'const' instead
if (specifier.includes(mod)) {
for (let path of moduleList[mod]) {

Check failure on line 243 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'path' is never reassigned. Use 'const' instead

Check failure on line 243 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'path' is never reassigned. Use 'const' instead
if (specifier.endsWith(mod + path) || specifier.endsWith(mod + path + '/')) {
patch = true
continue
}
}
}
}
}
}

if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}
if (patch) {
const { parentURL = '' } = context
const newSpecifier = deleteIitm(specifier)
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' } // early return, find out format
}

// Node.js v21 renames importAssertions to importAttributes
if (
(context.importAssertions && context.importAssertions.type === 'json') ||
(context.importAttributes && context.importAttributes.type === 'json')
) {
return url
}
if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}

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

return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
specifiers.set(url.url, specifier)
return {
url: addIitm(url.url),
shortCircuit: true,
format: url.format
}
}
const newSpecifier = deleteIitm(specifier)
if (isWin && parentURL.indexOf('file:node') === 0) {

Check failure on line 286 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'parentURL' is not defined

Check failure on line 286 in hook.js

View workflow job for this annotation

GitHub Actions / lint

'parentURL' is not defined
context.parentURL = ''
}
const url = await parentResolve(newSpecifier, context, parentResolve)
return { url: url.url, format: url.format }
}

const iitmURL = new URL('lib/register.js', meta.url).toString()
Expand Down

0 comments on commit 878fadb

Please sign in to comment.