Skip to content

Commit

Permalink
fix: Fallback to parentLoad if parsing fails (#104)
Browse files Browse the repository at this point in the history
Closes #101
  • Loading branch information
timfish authored Jun 14, 2024
1 parent 1c6f7b0 commit 88605a7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 27 deletions.
37 changes: 29 additions & 8 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,16 @@ function createHook (meta) {
async function getSource (url, context, parentGetSource) {
if (hasIitm(url)) {
const realUrl = deleteIitm(url)
const setters = await processModule({
srcUrl: realUrl,
context,
parentGetSource,
parentResolve: cachedResolve
})
return {
source: `

try {
const setters = await processModule({
srcUrl: realUrl,
context,
parentGetSource,
parentResolve: cachedResolve
})
return {
source: `
import { register } from '${iitmURL}'
import * as namespace from ${JSON.stringify(realUrl)}
Expand All @@ -286,6 +288,25 @@ ${Array.from(setters.values()).join('\n')}
register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(realUrl))})
`
}
} catch (cause) {
// If there are other ESM loader hooks registered as well as iitm,
// depending on the order they are registered, source might not be
// JavaScript.
//
// If we fail to parse a module for exports, we should fall back to the
// parent loader. These modules will not be wrapped with proxies and
// cannot be Hook'ed but at least this does not take down the entire app
// and block iitm from being used.
//
// We log the error because there might be bugs in iitm and without this
// it would be very tricky to debug
const err = new Error(`'import-in-the-middle' failed to wrap '${realUrl}'`)
err.cause = cause
console.warn(err)

// Revert back to the non-iitm URL
url = realUrl
}
}

Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions test/fixtures/json-attributes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import coolFile from './something.json' with { type: 'json' }

export default {
data: coolFile.data
}
17 changes: 17 additions & 0 deletions test/hook/v20.10-static-import-attributes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import jsonMjs from '../fixtures/json-attributes.mjs'
import { strictEqual } from 'assert'

// Acorn does not support import attributes so an error is logged but the import
// still works!
//
// Hook((exports, name) => {
// if (name.match(/json\.mjs/)) {
// exports.default.data += '-dawg'
// }
// })

strictEqual(jsonMjs.data, 'dog')
19 changes: 0 additions & 19 deletions test/other/import-executable.mjs

This file was deleted.

8 changes: 8 additions & 0 deletions test/other/v20.10-import-executable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

(async () => {
const lib = await import('../fixtures/executable')
console.assert(lib)
})()

0 comments on commit 88605a7

Please sign in to comment.