@addmaple/lz4 0.1.1 → 0.1.2

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/dist/node.d.ts CHANGED
@@ -1,2 +1,5 @@
1
- export function init(imports?: WebAssembly.Imports): Promise<void>;
1
+ export interface InitOptions {
2
+ backend?: 'auto' | 'simd' | 'base';
3
+ }
4
+ export function init(imports?: WebAssembly.Imports, opts?: InitOptions): Promise<void>;
2
5
  export * from "./custom.js";
package/dist/node.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { setInstance, registerInit } from "./core.js";
2
- import { instantiateWithFallback } from "./util.js";
2
+ import { instantiateWithBackend } from "./util.js";
3
3
 
4
4
  import { readFile } from "node:fs/promises";
5
5
  import { fileURLToPath } from "node:url";
@@ -7,20 +7,23 @@ import { fileURLToPath } from "node:url";
7
7
  const simdPath = fileURLToPath(new URL("./wasm/lz4.simd.wasm", import.meta.url));
8
8
  const basePath = fileURLToPath(new URL("./wasm/lz4.base.wasm", import.meta.url));
9
9
 
10
- async function getWasmBytes() {
11
- const [simdBytes, baseBytes] = await Promise.all([
12
- readFile(simdPath).catch(() => null),
13
- readFile(basePath).catch(() => null)
14
- ]);
15
- return { simdBytes, baseBytes };
10
+ async function getSimdBytes() {
11
+ return readFile(simdPath);
12
+ }
13
+
14
+ async function getBaseBytes() {
15
+ return readFile(basePath);
16
16
  }
17
17
 
18
18
 
19
19
  let _ready = null;
20
- export function init(imports = {}) {
21
- return (_ready ??= (async () => {
22
- const { simdBytes, baseBytes } = await getWasmBytes();
23
- const { instance } = await instantiateWithFallback(simdBytes, baseBytes, imports);
20
+ let _backend = null;
21
+ export function init(imports = {}, opts = {}) {
22
+ const backend = opts.backend || 'auto';
23
+ if (_ready && _backend === backend) return _ready;
24
+ _backend = backend;
25
+ return (_ready = (async () => {
26
+ const { instance } = await instantiateWithBackend({ getSimdBytes, getBaseBytes, imports, backend });
24
27
  setInstance(instance);
25
28
  })());
26
29
  }
package/dist/util.js CHANGED
@@ -12,3 +12,33 @@ export async function instantiateWithFallback(
12
12
  return { instance, backend: 'wasm' }
13
13
  }
14
14
  }
15
+
16
+ export async function instantiateWithBackend({
17
+ getSimdBytes,
18
+ getBaseBytes,
19
+ imports,
20
+ backend = 'auto',
21
+ }) {
22
+ if (backend === 'base') {
23
+ const baseBytes = await getBaseBytes()
24
+ const { instance } = await WebAssembly.instantiate(baseBytes, imports)
25
+ return { instance, backend: 'wasm' }
26
+ }
27
+
28
+ if (backend === 'simd') {
29
+ const simdBytes = await getSimdBytes()
30
+ const { instance } = await WebAssembly.instantiate(simdBytes, imports)
31
+ return { instance, backend: 'wasm-simd' }
32
+ }
33
+
34
+ // auto: try simd first, then fallback to baseline
35
+ try {
36
+ const simdBytes = await getSimdBytes()
37
+ const { instance } = await WebAssembly.instantiate(simdBytes, imports)
38
+ return { instance, backend: 'wasm-simd' }
39
+ } catch {
40
+ const baseBytes = await getBaseBytes()
41
+ const { instance } = await WebAssembly.instantiate(baseBytes, imports)
42
+ return { instance, backend: 'wasm' }
43
+ }
44
+ }
Binary file
Binary file