Skip to content

Commit 88605a7

Browse files
authored
fix: Fallback to parentLoad if parsing fails (#104)
Closes #101
1 parent 1c6f7b0 commit 88605a7

File tree

6 files changed

+59
-27
lines changed

6 files changed

+59
-27
lines changed

hook.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,16 @@ function createHook (meta) {
264264
async function getSource (url, context, parentGetSource) {
265265
if (hasIitm(url)) {
266266
const realUrl = deleteIitm(url)
267-
const setters = await processModule({
268-
srcUrl: realUrl,
269-
context,
270-
parentGetSource,
271-
parentResolve: cachedResolve
272-
})
273-
return {
274-
source: `
267+
268+
try {
269+
const setters = await processModule({
270+
srcUrl: realUrl,
271+
context,
272+
parentGetSource,
273+
parentResolve: cachedResolve
274+
})
275+
return {
276+
source: `
275277
import { register } from '${iitmURL}'
276278
import * as namespace from ${JSON.stringify(realUrl)}
277279
@@ -286,6 +288,25 @@ ${Array.from(setters.values()).join('\n')}
286288
287289
register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(realUrl))})
288290
`
291+
}
292+
} catch (cause) {
293+
// If there are other ESM loader hooks registered as well as iitm,
294+
// depending on the order they are registered, source might not be
295+
// JavaScript.
296+
//
297+
// If we fail to parse a module for exports, we should fall back to the
298+
// parent loader. These modules will not be wrapped with proxies and
299+
// cannot be Hook'ed but at least this does not take down the entire app
300+
// and block iitm from being used.
301+
//
302+
// We log the error because there might be bugs in iitm and without this
303+
// it would be very tricky to debug
304+
const err = new Error(`'import-in-the-middle' failed to wrap '${realUrl}'`)
305+
err.cause = cause
306+
console.warn(err)
307+
308+
// Revert back to the non-iitm URL
309+
url = realUrl
289310
}
290311
}
291312

File renamed without changes.

test/fixtures/json-attributes.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import coolFile from './something.json' with { type: 'json' }
2+
3+
export default {
4+
data: coolFile.data
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2+
//
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4+
5+
import jsonMjs from '../fixtures/json-attributes.mjs'
6+
import { strictEqual } from 'assert'
7+
8+
// Acorn does not support import attributes so an error is logged but the import
9+
// still works!
10+
//
11+
// Hook((exports, name) => {
12+
// if (name.match(/json\.mjs/)) {
13+
// exports.default.data += '-dawg'
14+
// }
15+
// })
16+
17+
strictEqual(jsonMjs.data, 'dog')

test/other/import-executable.mjs

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2+
//
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4+
5+
(async () => {
6+
const lib = await import('../fixtures/executable')
7+
console.assert(lib)
8+
})()

0 commit comments

Comments
 (0)