Skip to content

API reference / @jsprismarine/native-library

@jsprismarine/native-library ​

License: MIT OR MPL-2.0Join the Discord Servernpm

Finds, verifies and installs the prebuilt native libraries JSPrismarine's packages load.

JSPrismarine's packages that call C through node:ffi, such as @jsprismarine/native-network, @jsprismarine/script-host and @jsprismarine/native-worldgen, publish their library as one prebuilt file per platform on a GitHub release. This package holds what they share to get that file onto a machine and check it before it is loaded.

What a package gets ​

A package describes its library once:

ts
import { NativeLibrary } from '@jsprismarine/native-library';

import manifest from '../native/manifest.json' with { type: 'json' };

export const library = new NativeLibrary({
    packageName: '@jsprismarine/example',
    library: 'libjsp_example',
    envPrefix: 'JSP_EXAMPLE',
    packageDir: packageRoot(),
    manifest,
    buildScripts: ['scripts/build-native.mts']
});

From that, library.install({ packageDir: library.packageDir }) is the package's install script: it downloads the build for the machine from the release the manifest names, checks its SHA-256, and stores it under prebuilds/<platform>/. library.locate() finds that file, or the one the package's _LIBRARY variable names, and checks its digest against the manifest or the build.json a local build:native wrote beside it. Locating never downloads. When there is no file, the error says how to let the install script run under npm, pnpm and Bun.

library.forPlatform() and library.provideEmbedded() let a single-file executable carry the library and write it out on first start.

VariableWhat it does
<envPrefix>_LIBRARYLoads this file instead of the installed build, checked as well.
<envPrefix>_SKIP_DOWNLOAD=1The install script downloads nothing.
<envPrefix>_BASE_URLDownloads from another host, or reads from a directory.

Other modules ​

  • @jsprismarine/native-library/ffi opens a library through node:ffi under Node.js and bun:ffi under Bun, from one table of C signatures, so a package binds its interface once for both runtimes.
  • @jsprismarine/native-library/platform names a machine's platform key, such as linux-x64-musl, and a library's file names on each operating system.
  • @jsprismarine/native-library/manifest parses a package's native/manifest.json.
  • @jsprismarine/native-library/nativeHash hashes a checkout's native sources, the hash a release of builds is named after.

The error reference lists what it throws.

Modules ​