From a7e12e4432355bda9247b88e58d9be1bd421c8e2 Mon Sep 17 00:00:00 2001 From: Jonathan Underwood Date: Wed, 20 Sep 2023 00:33:27 -0700 Subject: [PATCH] Fix: bun runtime silently returns empty object for NAPI (#28) --- 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)