@minigraf/wasi 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @minigraf/wasi
2
+
3
+ WASI WebAssembly package for Minigraf.
4
+
5
+ This package contains the `wasm32-wasip1` CLI binary as
6
+ `minigraf-wasi.wasm` plus a small Node.js ESM loader. It is intended for
7
+ server-side WASM runtimes such as Wasmtime, Wasmer, and Node's WASI support.
8
+
9
+ ```js
10
+ import { WASI } from "node:wasi";
11
+ import { startMinigrafWasi } from "@minigraf/wasi";
12
+
13
+ const wasi = new WASI({
14
+ version: "preview1",
15
+ args: ["minigraf"],
16
+ env: process.env,
17
+ preopens: { "/tmp": "/tmp" },
18
+ });
19
+
20
+ await startMinigrafWasi(wasi);
21
+ ```
22
+
23
+ Use `MINIGRAF_WASI_WASM_PATH` or the `wasmPath` option to point the loader at
24
+ an alternate `.wasm` file during tests or custom packaging.
package/index.d.ts ADDED
@@ -0,0 +1,47 @@
1
+ export interface MinigrafWasiOptions {
2
+ wasmPath?: string;
3
+ }
4
+
5
+ export interface WasiWithImportObject {
6
+ getImportObject(): WebAssembly.Imports;
7
+ start?(instance: WebAssembly.Instance): void;
8
+ }
9
+
10
+ export interface WasiWithLegacyImport {
11
+ wasiImport: Record<string, WebAssembly.ImportValue>;
12
+ start?(instance: WebAssembly.Instance): void;
13
+ }
14
+
15
+ export type MinigrafWasiRuntime =
16
+ | WasiWithImportObject
17
+ | WasiWithLegacyImport
18
+ | ({ wasi_snapshot_preview1: Record<string, WebAssembly.ImportValue> } & {
19
+ start?(instance: WebAssembly.Instance): void;
20
+ });
21
+
22
+ export function getMinigrafWasiPath(options?: MinigrafWasiOptions): string;
23
+
24
+ export function readMinigrafWasi(
25
+ options?: MinigrafWasiOptions,
26
+ ): Promise<ArrayBuffer>;
27
+
28
+ export function compileMinigrafWasi(
29
+ options?: MinigrafWasiOptions,
30
+ ): Promise<WebAssembly.Module>;
31
+
32
+ export function instantiateMinigrafWasi(
33
+ imports?: WebAssembly.Imports,
34
+ options?: MinigrafWasiOptions,
35
+ ): Promise<WebAssembly.Instance>;
36
+
37
+ export function instantiateMinigrafWasiWith(
38
+ wasi: MinigrafWasiRuntime,
39
+ options?: MinigrafWasiOptions,
40
+ ): Promise<WebAssembly.Instance>;
41
+
42
+ export function startMinigrafWasi(
43
+ wasi: MinigrafWasiRuntime & {
44
+ start(instance: WebAssembly.Instance): void;
45
+ },
46
+ options?: MinigrafWasiOptions,
47
+ ): Promise<WebAssembly.Instance>;
package/index.js ADDED
@@ -0,0 +1,66 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { fileURLToPath } from "node:url";
3
+
4
+ const DEFAULT_WASM_URL = new URL("./minigraf-wasi.wasm", import.meta.url);
5
+
6
+ export function getMinigrafWasiPath(options = {}) {
7
+ return (
8
+ options.wasmPath ??
9
+ process.env.MINIGRAF_WASI_WASM_PATH ??
10
+ fileURLToPath(DEFAULT_WASM_URL)
11
+ );
12
+ }
13
+
14
+ export async function readMinigrafWasi(options = {}) {
15
+ const buffer = await readFile(getMinigrafWasiPath(options));
16
+ return buffer.buffer.slice(
17
+ buffer.byteOffset,
18
+ buffer.byteOffset + buffer.byteLength,
19
+ );
20
+ }
21
+
22
+ export async function compileMinigrafWasi(options = {}) {
23
+ return WebAssembly.compile(await readMinigrafWasi(options));
24
+ }
25
+
26
+ export async function instantiateMinigrafWasi(imports = {}, options = {}) {
27
+ const module = await compileMinigrafWasi(options);
28
+ return WebAssembly.instantiate(module, imports);
29
+ }
30
+
31
+ export async function instantiateMinigrafWasiWith(wasi, options = {}) {
32
+ return instantiateMinigrafWasi(importObjectFromWasi(wasi), options);
33
+ }
34
+
35
+ export async function startMinigrafWasi(wasi, options = {}) {
36
+ const instance = await instantiateMinigrafWasiWith(wasi, options);
37
+
38
+ if (typeof wasi.start !== "function") {
39
+ throw new TypeError("WASI object must expose start(instance)");
40
+ }
41
+
42
+ wasi.start(instance);
43
+ return instance;
44
+ }
45
+
46
+ function importObjectFromWasi(wasi) {
47
+ if (!wasi || typeof wasi !== "object") {
48
+ throw new TypeError("WASI object is required");
49
+ }
50
+
51
+ if (typeof wasi.getImportObject === "function") {
52
+ return wasi.getImportObject();
53
+ }
54
+
55
+ if (wasi.wasiImport) {
56
+ return { wasi_snapshot_preview1: wasi.wasiImport };
57
+ }
58
+
59
+ if (wasi.wasi_snapshot_preview1) {
60
+ return wasi;
61
+ }
62
+
63
+ throw new TypeError(
64
+ "WASI object must expose getImportObject(), wasiImport, or wasi_snapshot_preview1",
65
+ );
66
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@minigraf/wasi",
3
+ "type": "module",
4
+ "collaborators": [
5
+ "Aditya Mukhopadhyay"
6
+ ],
7
+ "description": "WASI WebAssembly build of Minigraf, a bi-temporal Datalog graph database",
8
+ "version": "1.0.0",
9
+ "license": "MIT OR Apache-2.0",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/project-minigraf/minigraf"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "index.d.ts",
17
+ "minigraf-wasi.wasm",
18
+ "README.md"
19
+ ],
20
+ "main": "index.js",
21
+ "types": "index.d.ts",
22
+ "scripts": {
23
+ "test": "node --test package.test.js"
24
+ },
25
+ "keywords": [
26
+ "graph",
27
+ "datalog",
28
+ "bitemporal",
29
+ "embedded",
30
+ "database",
31
+ "wasi",
32
+ "webassembly"
33
+ ]
34
+ }