From 5e59d2aaf0f317f4fc30eaf0450bf190e6dbf962 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 12 Jun 2024 18:08:29 +0200 Subject: [PATCH 1/8] fix: Fallback to `parentLoad` if parsing fails --- hook.js | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/hook.js b/hook.js index 4ec7679..a8b906b 100644 --- a/hook.js +++ b/hook.js @@ -246,14 +246,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)} @@ -268,6 +270,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 } } From 262561b144e5a86e1d0d637c38ecb0177d9fab58 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 12 Jun 2024 22:25:04 +0200 Subject: [PATCH 2/8] Add test --- test/fixtures/json-attributes.mjs | 5 +++++ test/hook/v18.19-static-import-attributes.mjs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/fixtures/json-attributes.mjs create mode 100644 test/hook/v18.19-static-import-attributes.mjs diff --git a/test/fixtures/json-attributes.mjs b/test/fixtures/json-attributes.mjs new file mode 100644 index 0000000..f3a807a --- /dev/null +++ b/test/fixtures/json-attributes.mjs @@ -0,0 +1,5 @@ +import coolFile from './something.json' with { type: 'json' } + +export default { + data: coolFile.data +} diff --git a/test/hook/v18.19-static-import-attributes.mjs b/test/hook/v18.19-static-import-attributes.mjs new file mode 100644 index 0000000..687cca3 --- /dev/null +++ b/test/hook/v18.19-static-import-attributes.mjs @@ -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') From 08c68777e69ce4df41255dfdc4b3edc71c3f831d Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 00:55:11 +0200 Subject: [PATCH 3/8] change failing test --- test/{other => fixtures}/executable | 0 test/other/import-executable.mjs | 15 ++------------- 2 files changed, 2 insertions(+), 13 deletions(-) rename test/{other => fixtures}/executable (100%) diff --git a/test/other/executable b/test/fixtures/executable similarity index 100% rename from test/other/executable rename to test/fixtures/executable diff --git a/test/other/import-executable.mjs b/test/other/import-executable.mjs index baa3ddf..4533c2a 100644 --- a/test/other/import-executable.mjs +++ b/test/other/import-executable.mjs @@ -2,18 +2,7 @@ // // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. -import { rejects } from 'assert' (async () => { - const [processMajor, processMinor] = process.versions.node.split('.').map(Number) - const extensionlessSupported = processMajor >= 21 || - (processMajor === 20 && processMinor >= 10) || - (processMajor === 18 && processMinor >= 19) - if (extensionlessSupported) { - // Files without extension are supported in Node.js ^21, ^20.10.0, and ^18.19.0 - return - } - await rejects(() => import('./executable'), { - name: 'TypeError', - code: 'ERR_UNKNOWN_FILE_EXTENSION' - }) + const lib = await import('../fixtures/executable') + console.assert(lib) })() From ff6d442528bd19f4f81665d25f321c8a4f9f8594 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 01:03:57 +0200 Subject: [PATCH 4/8] rename --- .../other/{import-executable.mjs => v18.20-import-executable.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/other/{import-executable.mjs => v18.20-import-executable.mjs} (100%) diff --git a/test/other/import-executable.mjs b/test/other/v18.20-import-executable.mjs similarity index 100% rename from test/other/import-executable.mjs rename to test/other/v18.20-import-executable.mjs From 07be485271ca39bbf4fbddef6029aebc225a2bf1 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 01:18:14 +0200 Subject: [PATCH 5/8] change versions --- .../{v18.20-import-executable.mjs => v20-import-executable.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/other/{v18.20-import-executable.mjs => v20-import-executable.mjs} (100%) diff --git a/test/other/v18.20-import-executable.mjs b/test/other/v20-import-executable.mjs similarity index 100% rename from test/other/v18.20-import-executable.mjs rename to test/other/v20-import-executable.mjs From 87bf8363be2c512daa84623a2821ba6d0077ed30 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 01:58:53 +0200 Subject: [PATCH 6/8] change versions --- ...-import-attributes.mjs => v18.20-static-import-attributes.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/hook/{v18.19-static-import-attributes.mjs => v18.20-static-import-attributes.mjs} (100%) diff --git a/test/hook/v18.19-static-import-attributes.mjs b/test/hook/v18.20-static-import-attributes.mjs similarity index 100% rename from test/hook/v18.19-static-import-attributes.mjs rename to test/hook/v18.20-static-import-attributes.mjs From 8575137a285c77489e5a24d55430b8a0998d0f8c Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 02:12:47 +0200 Subject: [PATCH 7/8] another version --- .../{v20-import-executable.mjs => v20.10-import-executable.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/other/{v20-import-executable.mjs => v20.10-import-executable.mjs} (100%) diff --git a/test/other/v20-import-executable.mjs b/test/other/v20.10-import-executable.mjs similarity index 100% rename from test/other/v20-import-executable.mjs rename to test/other/v20.10-import-executable.mjs From 3298731d8aa151ce8d58a9df0b07041bea62b508 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Jun 2024 02:29:31 +0200 Subject: [PATCH 8/8] yet another version --- ...-import-attributes.mjs => v20.10-static-import-attributes.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/hook/{v18.20-static-import-attributes.mjs => v20.10-static-import-attributes.mjs} (100%) diff --git a/test/hook/v18.20-static-import-attributes.mjs b/test/hook/v20.10-static-import-attributes.mjs similarity index 100% rename from test/hook/v18.20-static-import-attributes.mjs rename to test/hook/v20.10-static-import-attributes.mjs