Skip to content

ChainSafe/bun-ffi-z

Repository files navigation

bun-ffi-z

Library to use and publish shared libraries for bun:ffi.

Usage

  1. Add bun-ffi-z dependency
bun install @chainsafe/bun-ffi-z
  1. Add bun-ffi-z section to package.json
  "bun-ffi-z": {
    "name": "example",
    "targets": [
      "linux-x64-gnu",
      "linux-arm64-gnu",
      "linux-x64-musl",
      "linux-arm64-musl",
      "darwin-x64",
      "darwin-arm64",
      "win32-x64"
    ],
    "zigCwd": "zig"
  }
  1. Use bun-ffi-z to select the proper library
import {openLibrary} from "@chainsafe/bun-ffi-z";

const lib = await openLibrary(
  import.meta.dirname,
  {
    add: {
      args: ["u32", "u32"],
      returns: "u32"
    }
  }
)

export const symbols = lib.symbols;
export const close = lib.close;
  1. Build shared library for native host
bun-ffi-z build
  1. On publish CI, prepare an "artifacts" subdirectory which contains all prebuilt binaries
  - name: Download all artifacts
    if: ${{ steps.release.outputs.release_created }}
    uses: actions/download-artifact@v4
    with:
      path: artifacts
  1. Prepare targetPackages subdirectory by copying prebuilt binaries inside "artifacts" folder prepared above
bun-ffi-z prepublish --artifacts artifacts

Note that both targetPackages and artifacts are relative to bun folder.

  1. Publish the root package and all target packages prepared above in targetPackages subdirectory
bun-ffi-z publish

License

MIT

About

Use and publish zig shared libraries for `bun:ffi`

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •