Skip to content

Commit 1a0b895

Browse files
authored
Merge pull request #80 from ssssota/initialize-with-wasm
feat: initializing with wasm binary
2 parents 3165fba + e0d727d commit 1a0b895

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/libraw.wasm/src/index.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as typ from "typed-cstruct";
22

33
import {
4-
libraw_data_t,
54
libraw_imgother_t,
65
libraw_iparams_t,
76
libraw_lensinfo_t,
@@ -23,6 +22,9 @@ import type {
2322
LibRawWasmModule,
2423
} from "./types/index.js";
2524

25+
type PromiseOr<T> = T | Promise<T>;
26+
type WasmInput = PromiseOr<Response | ArrayBuffer>;
27+
2628
export class LibRaw implements Disposable {
2729
private static modulePromise: Promise<LibRawWasmModule> | undefined;
2830
private static module: LibRawWasmModule;
@@ -34,14 +36,29 @@ export class LibRaw implements Disposable {
3436
constructor() {
3537
this.setup();
3638
}
37-
static async initialize() {
39+
static async initialize(wasm?: WasmInput) {
3840
if (LibRaw.modulePromise === undefined) {
39-
const mod: Promise<LibRawWasmModule> = initializeLibRawWasm();
41+
const mod: Promise<LibRawWasmModule> = initializeLibRawWasm({
42+
instantiateWasm: LibRaw.createInstantiateWasm(wasm),
43+
});
4044
LibRaw.modulePromise = mod;
4145
LibRaw.module = await mod;
4246
}
4347
return await LibRaw.modulePromise;
4448
}
49+
private static createInstantiateWasm(wasm?: WasmInput) {
50+
if (wasm === undefined) return undefined;
51+
return async (
52+
importObject: WebAssembly.Imports,
53+
cb: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,
54+
) => {
55+
const wasmResponse = await wasm;
56+
const instantiated = await (wasmResponse instanceof Response
57+
? WebAssembly.instantiateStreaming(wasmResponse, importObject)
58+
: WebAssembly.instantiate(wasmResponse, importObject));
59+
cb(instantiated.instance, instantiated.module);
60+
};
61+
}
4562
async waitUntilReady() {
4663
await LibRaw.modulePromise;
4764
}

tests/wasm.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as fs from "node:fs/promises";
2+
import * as path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { LibRaw } from "libraw.wasm";
5+
import { expect, it } from "vitest";
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = path.dirname(__filename);
8+
const wasmPath = path.join(
9+
__dirname,
10+
"node_modules",
11+
"libraw.wasm",
12+
"dist",
13+
"libraw.wasm",
14+
);
15+
16+
it("specify wasm binary", async () => {
17+
const wasm = fs.readFile(wasmPath).then((buf) => new Uint8Array(buf).buffer);
18+
await LibRaw.initialize(wasm);
19+
expect(LibRaw.version()).toMatchInlineSnapshot(`"0.22.0-Devel202502"`);
20+
expect(LibRaw.versionNumber()).toMatchInlineSnapshot("5632");
21+
});

0 commit comments

Comments
 (0)