From 1bb66b15393c89379d0ed3d68837c1a3519c9890 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Jul 2024 15:42:38 +0200 Subject: [PATCH 1/4] fix: Don't wrap native modules --- hook.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hook.js b/hook.js index 1752bcf..c6aebda 100644 --- a/hook.js +++ b/hook.js @@ -297,6 +297,10 @@ function createHook (meta) { return result } + if (!result.url.endsWith('.node')) { + return result + } + // Node.js v21 renames importAssertions to importAttributes if ( (context.importAssertions && context.importAssertions.type === 'json') || From 275b4e891a4e7b2ab41724e9538ab1e1c26723b6 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Jul 2024 17:10:50 +0200 Subject: [PATCH 2/4] Fix --- hook.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hook.js b/hook.js index c6aebda..7cda35c 100644 --- a/hook.js +++ b/hook.js @@ -297,7 +297,8 @@ function createHook (meta) { return result } - if (!result.url.endsWith('.node')) { + // We don't want to attempt to wrap native modules + if (result.url.endsWith('.node')) { return result } From 2a92851feebd299078abb8f7dadda54561fb2304 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Jul 2024 17:43:43 +0200 Subject: [PATCH 3/4] Add test --- lib/get-exports.js | 5 +++++ package.json | 1 + test/fixtures/native-modules/darwin-arm64.js | 1 + test/fixtures/native-modules/darwin-x64.js | 1 + test/fixtures/native-modules/linux-arm64.js | 1 + test/fixtures/native-modules/linux-x64.js | 1 + test/fixtures/native-modules/win32-arm64.js | 1 + test/fixtures/native-modules/win32-x64.js | 1 + test/hook/native-modules.mjs | 10 ++++++++++ 9 files changed, 22 insertions(+) create mode 100644 test/fixtures/native-modules/darwin-arm64.js create mode 100644 test/fixtures/native-modules/darwin-x64.js create mode 100644 test/fixtures/native-modules/linux-arm64.js create mode 100644 test/fixtures/native-modules/linux-x64.js create mode 100644 test/fixtures/native-modules/win32-arm64.js create mode 100644 test/fixtures/native-modules/win32-x64.js create mode 100644 test/hook/native-modules.mjs diff --git a/lib/get-exports.js b/lib/get-exports.js index 4b90d2c..ce7576d 100644 --- a/lib/get-exports.js +++ b/lib/get-exports.js @@ -48,6 +48,11 @@ async function getCjsExports (url, context, parentLoad, source) { } // Resolve the re-exported module relative to the current module. const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href + + if (newUrl.endsWith('.node')) { + return + } + for (const each of await getExports(newUrl, context, parentLoad)) { full.add(each) } diff --git a/package.json b/package.json index 45d4a06..6333d31 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@babel/core": "^7.23.7", "@babel/eslint-parser": "^7.23.3", "@babel/plugin-syntax-import-assertions": "^7.23.3", + "@node-rs/crc32": "^1.10.3", "@react-email/components": "^0.0.19", "@types/node": "^18.0.6", "c8": "^7.8.0", diff --git a/test/fixtures/native-modules/darwin-arm64.js b/test/fixtures/native-modules/darwin-arm64.js new file mode 100644 index 0000000..917fbfb --- /dev/null +++ b/test/fixtures/native-modules/darwin-arm64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-darwin-arm64') diff --git a/test/fixtures/native-modules/darwin-x64.js b/test/fixtures/native-modules/darwin-x64.js new file mode 100644 index 0000000..3ba9795 --- /dev/null +++ b/test/fixtures/native-modules/darwin-x64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-darwin-x64') diff --git a/test/fixtures/native-modules/linux-arm64.js b/test/fixtures/native-modules/linux-arm64.js new file mode 100644 index 0000000..5e4f832 --- /dev/null +++ b/test/fixtures/native-modules/linux-arm64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-linux-arm64-gnu') diff --git a/test/fixtures/native-modules/linux-x64.js b/test/fixtures/native-modules/linux-x64.js new file mode 100644 index 0000000..7386d1d --- /dev/null +++ b/test/fixtures/native-modules/linux-x64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-linux-x64-gnu') diff --git a/test/fixtures/native-modules/win32-arm64.js b/test/fixtures/native-modules/win32-arm64.js new file mode 100644 index 0000000..8c6a092 --- /dev/null +++ b/test/fixtures/native-modules/win32-arm64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-win32-arm64-msvc') diff --git a/test/fixtures/native-modules/win32-x64.js b/test/fixtures/native-modules/win32-x64.js new file mode 100644 index 0000000..1eedbc9 --- /dev/null +++ b/test/fixtures/native-modules/win32-x64.js @@ -0,0 +1 @@ +module.exports = require('@node-rs/crc32-win32-x64-msvc') diff --git a/test/hook/native-modules.mjs b/test/hook/native-modules.mjs new file mode 100644 index 0000000..f478729 --- /dev/null +++ b/test/hook/native-modules.mjs @@ -0,0 +1,10 @@ +import { strictEqual } from 'assert' + +// We dynamically import a specific file that imports the +// native module for this platform and architecture. +// +// This way we know the file exists and the native module can +// be loaded. +const lib = await import(`../fixtures/native-modules/${process.platform}-${process.arch}.js`) + +strictEqual(typeof lib.default.crc32, 'function') From 38f5c383f1ef371401d61ac116b0a2864c2150be Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Mon, 15 Jul 2024 17:45:31 +0200 Subject: [PATCH 4/4] Only test on v14+ --- test/hook/{native-modules.mjs => v14-native-modules.mjs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/hook/{native-modules.mjs => v14-native-modules.mjs} (100%) diff --git a/test/hook/native-modules.mjs b/test/hook/v14-native-modules.mjs similarity index 100% rename from test/hook/native-modules.mjs rename to test/hook/v14-native-modules.mjs