From 439221f6266660d7716ac01cf168933230574ec0 Mon Sep 17 00:00:00 2001 From: junderw Date: Tue, 19 Sep 2023 14:39:00 -0700 Subject: [PATCH] Fix: bun runtime silently returns empty object for NAPI --- bindings.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/bindings.js b/bindings.js index 5c0eec6..5a2b021 100644 --- a/bindings.js +++ b/bindings.js @@ -1 +1,11 @@ -module.exports = require('./lib/api')(require('node-gyp-build')(__dirname)) +const nativeAddon = require('node-gyp-build')(__dirname) +if (typeof nativeAddon !== 'function') { + // Some new runtimes (bun) don't support N-API + // but the build step incorrectly succeeds. + // The value should be a function, but in bun it returns + // an empty object {} so we use typeof to check that + // it is a function and throw otherwise. + // This throw will cause "keccak" import to fallback to JS. + throw new Error('Native add-on failed to load') +} +module.exports = require('./lib/api')(nativeAddon)