Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
echo "rust_toolchain=$PARTIAL_RUST_TOOLCHAINS" >> $GITHUB_OUTPUT
fi

build:
node:
needs: matrix
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -120,3 +120,33 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
slug: neon-bindings/neon
files: target/codecov.json

bun:
needs: matrix
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust-toolchain: ${{fromJson(needs.matrix.outputs.rust_toolchain)}}
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Use Rust ${{ matrix.rust-toolchain }}
uses: dtolnay/rust-toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}

- uses: oven-sh/setup-bun@v2
with:
bun-version: latest

- name: convert lockfile
run: bun install --lockfile-only

- name: bun install
run: bun ci

- name: test
working-directory: ./test/napi
run: bun run test:bun
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ wip
# Node
**/node_modules
npm-debug.log
bun.lock
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bun install --lockfile-only can convert a package-lock.json. To avoid having two lockfiles that we need to keep in sync, the Node lock file gets converted in CI.


# JS build
**/build
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ using a different version of Node and believe it should be supported, let us kno

Older Node version support (minimum v10) may require lower Node-API versions. See the Node [version support matrix](https://nodejs.org/api/n-api.html#node-api-version-matrix) for more details.

### Bun (experimental)
### Bun

[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/oven-sh/bun/issues/158).
[Bun](https://bun.sh/) is an alternate JavaScript runtime that targets Node compatibility. Bun is supported and tested in CI. For details on compatibility see the [Bun support tracking issue](https://github.com/neon-bindings/neon/issues/1128).

### Rust

Expand Down
2 changes: 1 addition & 1 deletion test/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "The Neon Community",
"license": "MIT",
"scripts": {
"install": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"install": "cargo-cp-artifact -ac electron-tests index.node -- cargo build --message-format=json-render-diagnostics",
Copy link
Copy Markdown
Member Author

@kjvalencik kjvalencik Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we install with bun instead of npm, we don't get the automatic package name inference.

"start": "electron .",
"test": "playwright test"
},
Expand Down
14 changes: 8 additions & 6 deletions test/napi/lib/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe("classes", function () {
const message = new Message("test");
const StringBuffer = addon.StringBuffer;
const buffer = new StringBuffer();
const normalizeFn = (f) =>
f.toString().replace(/\{\s+(\[native code\])\s+\}/, "{ $1 }");

assert.strictEqual(message.read.name, "read");
assert.strictEqual(message.append.name, "append");
Expand All @@ -66,15 +68,15 @@ describe("classes", function () {
assert.strictEqual(util.inspect(buffer.trimEnd), "[Function: trimEnd]");

assert.strictEqual(
message.read.toString(),
normalizeFn(message.read),
"function read() { [native code] }"
);
assert.strictEqual(
message.append.toString(),
normalizeFn(message.append),
"function append() { [native code] }"
);
assert.strictEqual(
message.concat.toString(),
normalizeFn(message.concat),
"function concat() { [native code] }"
);

Expand All @@ -92,15 +94,15 @@ describe("classes", function () {
);

assert.strictEqual(
buffer.includes.toString(),
normalizeFn(buffer.includes),
"function includes() { [native code] }"
);
assert.strictEqual(
buffer.trimStart.toString(),
normalizeFn(buffer.trimStart),
"function trimStart() { [native code] }"
);
assert.strictEqual(
buffer.trimEnd.toString(),
normalizeFn(buffer.trimEnd),
"function trimEnd() { [native code] }"
);
});
Expand Down
42 changes: 29 additions & 13 deletions test/napi/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ describe("JsFunction", function () {
});

it("bind a strict JsFunction to a number", function () {
// https://github.com/neon-bindings/neon/issues/1128#usestrict
if (process.versions.bun) {
return this.skip();
}

assert.isTrue(isStrict(STRICT));

// strict mode functions are allowed to have a primitive this binding
Expand Down Expand Up @@ -154,13 +159,23 @@ describe("JsFunction", function () {
});

it("call a JsFunction with the default this", function () {
// https://github.com/neon-bindings/neon/issues/1128#usestrict
if (process.versions.bun) {
return this.skip();
}

addon.call_js_function_with_implicit_this(function () {
"use strict"; // ensure the undefined this isn't replaced with the global object
assert.strictEqual(this, undefined);
});
});

it("exec a JsFunction with the default this", function () {
// https://github.com/neon-bindings/neon/issues/1128#usestrict
if (process.versions.bun) {
return this.skip();
}

addon.exec_js_function_with_implicit_this(function () {
"use strict"; // ensure the undefined this isn't replaced with the global object
assert.strictEqual(this, undefined);
Expand Down Expand Up @@ -349,19 +364,20 @@ describe("JsFunction", function () {
assert.strictEqual(addon.count_called() + 1, addon.count_called());
});

(global.gc ? it : it.skip)(
"should drop function when going out of scope",
function (cb) {
// Run from an `IIFE` to ensure that `f` is out of scope and eligible for garbage
// collection when `global.gc()` is executed.
(() => {
const msg = "Hello, World!";
const f = addon.caller_with_drop_callback(() => msg, cb);
it("should drop function when going out of scope", function (cb) {
if (!global.gc) {
return this.skip();
}

assert.strictEqual(f(), msg);
})();
// Run from an `IIFE` to ensure that `f` is out of scope and eligible for garbage
// collection when `global.gc()` is executed.
(() => {
const msg = "Hello, World!";
const f = addon.caller_with_drop_callback(() => msg, cb);

global.gc();
}
);
assert.strictEqual(f(), msg);
})();

global.gc();
});
});
11 changes: 7 additions & 4 deletions test/napi/lib/threads.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const addon = require("..");
const assert = require("chai").assert;

(function () {
// These tests require GC exposed to shutdown properly; skip if it is not
return typeof global.gc === "function" ? describe : describe.skip;
})()("sync", function () {
describe("sync", function () {
before(function () {
if (!global.gc) {
this.skip();
}
});

let unhandledRejectionListeners = [];

beforeEach(() => {
Expand Down
25 changes: 25 additions & 0 deletions test/napi/lib/typedarrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ describe("Typed arrays", function () {
});

it("correctly constructs a view over a slice of a buffer", function () {
// https://github.com/neon-bindings/neon/issues/1128#byteoffset
if (process.versions.bun) {
return this.skip();
}

var buf = new ArrayBuffer(128);

var a = addon.return_uint32array_from_arraybuffer_region(buf, 16, 4);
Expand Down Expand Up @@ -457,6 +462,11 @@ describe("Typed arrays", function () {
}

it("provides correct metadata when detaching a typed array's buffer", function () {
// https://github.com/neon-bindings/neon/issues/1128#detach
if (process.versions.bun) {
return this.skip();
}

var buf = new ArrayBuffer(16);
var arr = new Uint32Array(buf, 4, 2);
var buf = arr.buffer;
Expand Down Expand Up @@ -492,16 +502,31 @@ describe("Typed arrays", function () {
});

it("provides correct metadata when detaching an escaped typed array's buffer", function () {
// https://github.com/neon-bindings/neon/issues/1128#detach
if (process.versions.bun) {
return this.skip();
}

var buf = new ArrayBuffer(16);
testDetach(new Uint32Array(buf, 4, 2), addon.detach_and_escape, 8, 2, 4);
});

it("provides correct metadata when detaching a casted typed array's buffer", function () {
// https://github.com/neon-bindings/neon/issues/1128#detach
if (process.versions.bun) {
return this.skip();
}

var buf = new ArrayBuffer(16);
testDetach(new Uint32Array(buf, 4, 2), addon.detach_and_cast, 8, 2, 4);
});

it("provides correct metadata when detaching an un-rooted typed array's buffer", function () {
// https://github.com/neon-bindings/neon/issues/1128#detach
if (process.versions.bun) {
return this.skip();
}

var buf = new ArrayBuffer(16);
testDetach(new Uint32Array(buf, 4, 2), addon.detach_and_unroot, 8, 2, 4);
});
Expand Down
15 changes: 10 additions & 5 deletions test/napi/lib/workers.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("Worker / Root Tagging Tests", () => {
describe("Multi-Threaded", () => {
it("should fail to use `get_and_replace`", (cb) => {
const worker = new Worker(__filename);
after(() => worker.terminate());
Copy link
Copy Markdown
Member Author

@kjvalencik kjvalencik Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not ever completing and I'm not sure why. unref() accomplishes the same thing and side steps the issue.

#1128 (comment)

worker.unref();

worker.once("message", (message) => {
assert.ok(/wrong module/.test(message));
Expand All @@ -117,7 +117,7 @@ describe("Worker / Root Tagging Tests", () => {

it("should fail to use `get_or_init`", (cb) => {
const worker = new Worker(__filename);
after(() => worker.terminate());
worker.unref();

worker.once("message", (message) => {
assert.ok(/wrong module/.test(message));
Expand All @@ -129,7 +129,7 @@ describe("Worker / Root Tagging Tests", () => {

it("should fail to use `get_or_init`", (cb) => {
const worker = new Worker(__filename);
after(() => worker.terminate());
worker.unref();

worker.once("message", (message) => {
assert.ok(/wrong module/.test(message));
Expand Down Expand Up @@ -192,7 +192,7 @@ describe("Instance-local storage", () => {
assert(!Number.isNaN(mainThreadId));

const worker = new Worker(__filename);
after(() => worker.terminate());
worker.unref();

worker.once("message", (message) => {
assert.strictEqual(typeof message, "number");
Expand All @@ -206,7 +206,12 @@ describe("Instance-local storage", () => {
worker.postMessage("get_thread_id");
});

it("should be able to exit a worker without a crash", (cb) => {
it("should be able to exit a worker without a crash", function (cb) {
// https://github.com/neon-bindings/neon/issues/1128#terminate
if (process.versions.bun) {
return this.skip();
}

const worker = new Worker(__filename, {
workerData: "notify_when_startup_complete",
});
Expand Down
5 changes: 3 additions & 2 deletions test/napi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"author": "The Neon Community",
"license": "MIT",
"scripts": {
"install": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
"install": "cargo-cp-artifact -ac napi-tests index.node -- cargo build --message-format=json-render-diagnostics",
"mocha": "mocha",
"test": "mocha --v8-expose-gc --timeout 5000 --recursive lib"
"test": "mocha --expose-gc --timeout 5000 --recursive lib",
"test:bun": "bun ../../node_modules/mocha/bin/mocha.js --v8-expose-gc --timeout 5000 --recursive lib"
},
"devDependencies": {
"cargo-cp-artifact": "^0.1.9",
Expand Down
Loading