From 307a4d0e82bd8a541c90afa805608c6d7351eaad Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 25 Jun 2024 11:36:47 +0100 Subject: [PATCH 1/2] fix: Skip wrapping CJS modules which result in invalid indenifiers --- hook.js | 26 ++++++++++---------------- package.json | 3 ++- test/fixtures/invalid-identifier.js | 1 + test/hook/invalid-identifier.mjs | 4 ++++ 4 files changed, 17 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/invalid-identifier.js create mode 100644 test/hook/invalid-identifier.mjs diff --git a/hook.js b/hook.js index 8e534e0..62d1868 100644 --- a/hook.js +++ b/hook.js @@ -3,6 +3,7 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc. const { URL } = require('url') +const { isIdentifierName } = require('@babel/helper-validator-identifier') const specifiers = new Map() const isWin = process.platform === 'win32' @@ -133,6 +134,10 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, const setters = new Map() const addSetter = (name, setter, isStarExport = false) => { + if (!isIdentifierName(name)) { + throw new Error(`'${name}' is not a valid ESM identifier name`) + } + if (setters.has(name)) { if (isStarExport) { // If there's already a matching star export, delete it @@ -261,7 +266,8 @@ function createHook (meta) { } } - async function getSource (url, context, parentGetSource) { + // For Node.js 16.12.0 and higher. + async function load (url, context, parentGetSource) { if (hasIitm(url)) { const realUrl = deleteIitm(url) @@ -273,6 +279,8 @@ function createHook (meta) { parentResolve: cachedResolve }) return { + shortCircuit: true, + format: 'module', source: ` import { register } from '${iitmURL}' import * as namespace from ${JSON.stringify(realUrl)} @@ -313,27 +321,13 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea return parentGetSource(url, context, parentGetSource) } - // For Node.js 16.12.0 and higher. - async function load (url, context, parentLoad) { - if (hasIitm(url)) { - const { source } = await getSource(url, context, parentLoad) - return { - source, - shortCircuit: true, - format: 'module' - } - } - - return parentLoad(url, context, parentLoad) - } - if (NODE_MAJOR >= 17 || (NODE_MAJOR === 16 && NODE_MINOR >= 12)) { return { load, resolve } } else { return { load, resolve, - getSource, + getSource: load, getFormat (url, context, parentGetFormat) { if (hasIitm(url)) { return { diff --git a/package.json b/package.json index d9c2c34..0e03ef8 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "acorn": "^8.8.2", "acorn-import-attributes": "^1.9.5", "cjs-module-lexer": "^1.2.2", - "module-details-from-path": "^1.0.3" + "module-details-from-path": "^1.0.3", + "@babel/helper-validator-identifier": "^7.24.7" } } diff --git a/test/fixtures/invalid-identifier.js b/test/fixtures/invalid-identifier.js new file mode 100644 index 0000000..c604f80 --- /dev/null +++ b/test/fixtures/invalid-identifier.js @@ -0,0 +1 @@ +exports['one.two'] = () => console.log('b') diff --git a/test/hook/invalid-identifier.mjs b/test/hook/invalid-identifier.mjs new file mode 100644 index 0000000..6f4acf0 --- /dev/null +++ b/test/hook/invalid-identifier.mjs @@ -0,0 +1,4 @@ +import * as lib from '../fixtures/invalid-identifier.js' +import { strictEqual } from 'assert' + +strictEqual(typeof lib['one.two'], 'function') From f20179c6716a50106967a43c94be21d3f7822c61 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Tue, 25 Jun 2024 12:58:06 +0100 Subject: [PATCH 2/2] change error message --- hook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hook.js b/hook.js index 62d1868..eaac130 100644 --- a/hook.js +++ b/hook.js @@ -135,7 +135,7 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve, const addSetter = (name, setter, isStarExport = false) => { if (!isIdentifierName(name)) { - throw new Error(`'${name}' is not a valid ESM identifier name`) + throw new Error(`'${name}' export is not a valid identifier name`) } if (setters.has(name)) {