11import * as typ from "typed-cstruct" ;
22
33import {
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+
2628export 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 }
0 commit comments