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
14 changes: 14 additions & 0 deletions packages/fff-bun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ The correct native binary for your platform is installed automatically via platf

If the platform package isn't available, the postinstall script will attempt to download from GitHub releases as a fallback.

### Platforms without a prebuilt package

On a platform that has no `@ff-labs/fff-bin-*` package, build the C library from
source and point `@ff-labs/fff-bun` at it with `FFF_C_LIB`:

```bash
cargo build --release -p fff-c
FFF_C_LIB=target/release/libfff_c.so bun ./app.ts
```

`FFF_C_LIB` is checked before the platform package and the local dev build, so it
also works for a regular `bun add @ff-labs/fff-bun` install. If the path does not
exist, resolution fails with an explicit error rather than silently falling back.

### Standalone executables (`bun build --compile`)

`@ff-labs/fff-bun` embeds the native library into single-file executables built
Expand Down
3 changes: 2 additions & 1 deletion packages/fff-bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"darwin",
"linux",
"win32",
"android"
"android",
"freebsd"
],
"cpu": [
"x64",
Expand Down
24 changes: 21 additions & 3 deletions packages/fff-bun/src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* Binary resolution utilities for fff
*
* Resolves the native library from:
* 1. Platform-specific npm package (e.g. @ff-labs/fff-bin-darwin-arm64)
* 2. Local dev build (target/release or target/debug)
* 1. The FFF_C_LIB environment variable (explicit path to libfff_c.so/.dylib/.dll)
* 2. Platform-specific npm package (e.g. @ff-labs/fff-bin-darwin-arm64)
* 3. Local dev build (target/release or target/debug)
*/

import { existsSync } from "node:fs";
Expand Down Expand Up @@ -54,7 +55,13 @@ export function binaryExists(): boolean {
* in the same directory.
*/
function resolveFromNpmPackage(): string | null {
const packageName = getNpmPackageName();
let packageName: string;
try {
packageName = getNpmPackageName();
} catch {
// Unsupported platform, or no platform package mapped for it.
return null;
}

try {
// Use createRequire to resolve the platform package's location
Expand Down Expand Up @@ -105,12 +112,23 @@ function isDevWorkspace(): boolean {
* Find the native library binary.
*
* Resolution order:
* - FFF_C_LIB (if set) always wins
* - Dev workspace: local dev build first, then npm package
* - Production: npm package first, then dev build
*
* @returns Absolute path to the library, or null if not found
*/
export function findBinary(): string | null {
// Explicit override for platforms without a prebuilt package (for example a
// libfff_c.so built from source on FreeBSD, or any custom build).
const override = process.env["FFF_C_LIB"];
if (override) {
if (!existsSync(override)) {
throw new Error(`FFF_C_LIB points to a file that does not exist: ${override}`);
}
return override;
}

if (isDevWorkspace()) {
// 1. Local bin/ directory (populated by `make prepare-bun`)
const binPath = join(getPackageDir(), "bin", getLibFilename());
Expand Down
2 changes: 2 additions & 0 deletions packages/fff-bun/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function getTriple(): string {
osName = detectLinuxLibc();
} else if (platform === "win32") {
osName = "pc-windows-msvc";
} else if (platform === "freebsd") {
osName = "unknown-freebsd";
} else {
throw new Error(`Unsupported platform: ${platform}`);
}
Expand Down