Skip to content

Commit 562f401

Browse files
committed
fix: Make native modules built with Neon work in Bun
1 parent f21146d commit 562f401

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Neon projects to 0.10!
4949
Support for [LTS versions of Node](https://github.com/nodejs/LTS#release-schedule) and current are expected. If you're
5050
using a different version of Node and believe it should be supported, let us know.
5151

52+
### Bun (experimental)
53+
54+
[Bun](https://bun.sh/) is an alternate JavaScript runtime that targets Node compatibility. In many cases Neon modules will work in bun; however, at the time of this writing, some Node-API functions are [not implemented](https://github.com/Jarred-Sumner/bun/issues/158).
55+
5256
### Rust
5357

5458
Neon supports Rust stable version 1.18 and higher. We test on the latest stable, beta, and nightly versions of Rust.

crates/neon/src/sys/bindings/functions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,19 +371,19 @@ pub(super) unsafe fn load(env: Env) -> Result<(), libloading::Error> {
371371
// with `Error: Module did not self-register` if N-API does not exist.
372372
let version = get_version(&host, env).expect("Failed to find N-API version");
373373

374-
napi1::load(&host, version, 1)?;
374+
napi1::load(&host, version, 1);
375375

376376
#[cfg(feature = "napi-4")]
377-
napi4::load(&host, version, 4)?;
377+
napi4::load(&host, version, 4);
378378

379379
#[cfg(feature = "napi-5")]
380-
napi5::load(&host, version, 5)?;
380+
napi5::load(&host, version, 5);
381381

382382
#[cfg(feature = "napi-6")]
383-
napi6::load(&host, version, 6)?;
383+
napi6::load(&host, version, 6);
384384

385385
#[cfg(feature = "napi-8")]
386-
napi8::load(&host, version, 8)?;
386+
napi8::load(&host, version, 8);
387387

388388
Ok(())
389389
}

crates/neon/src/sys/bindings/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ macro_rules! generate {
118118

119119
#[inline(never)]
120120
fn panic_load<T>() -> T {
121-
panic!("Must load N-API bindings")
121+
panic!("Node-API symbol has not been loaded")
122122
}
123123

124124
static mut NAPI: Napi = {
@@ -139,21 +139,31 @@ macro_rules! generate {
139139
host: &libloading::Library,
140140
actual_napi_version: u32,
141141
expected_napi_version: u32,
142-
) -> Result<(), libloading::Error> {
142+
) {
143143
assert!(
144144
actual_napi_version >= expected_napi_version,
145-
"Minimum required N-API version {}, found {}.",
145+
"Minimum required Node-API version {}, found {}.",
146146
expected_napi_version,
147147
actual_napi_version,
148148
);
149149

150+
let print_warn = |err| eprintln!("WARN: {}", err);
151+
150152
NAPI = Napi {
151153
$(
152-
$name: *host.get(napi_name!($name).as_bytes())?,
154+
$name: match host.get(napi_name!($name).as_bytes()) {
155+
Ok(f) => *f,
156+
// Node compatible runtimes may not have full coverage of Node-API
157+
// (e.g., bun). Instead of failing to start, warn on start and
158+
// panic when the API is called.
159+
// https://github.com/Jarred-Sumner/bun/issues/158
160+
Err(err) => {
161+
print_warn(err);
162+
NAPI.$name
163+
},
164+
},
153165
)*
154166
};
155-
156-
Ok(())
157167
}
158168

159169
$(

0 commit comments

Comments
 (0)