-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWasmLoadUtils.ts
65 lines (56 loc) · 1.61 KB
/
WasmLoadUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* Copyright (c) Microblink Ltd. All rights reserved.
*/
import * as WasmFeatureDetect from "wasm-feature-detect";
import { WasmType } from "./WasmType";
/* eslint-disable max-len */
/**
* Safari 16 shipped with WASM threads support, but it didn't ship with nested
* workers support, so an extra check is needed
* https://github.com/GoogleChromeLabs/squoosh/pull/1325/files#diff-904900db64cd3f48b0e765dbbdc6a218a7ea74a199671bde82a8944a904db86f
*/
/* eslint-enable max-len */
export default async function checkThreadsSupport(): Promise<boolean>
{
const supportsWasmThreads = await WasmFeatureDetect.threads();
if ( !supportsWasmThreads ) return false;
if ( !( "importScripts" in self ) )
{
throw Error( "Not implemented" );
}
return "Worker" in self;
}
export async function detectWasmType(): Promise<WasmType>
{
// determine if all features required for advanced WASM are available
// currently, advanced wasm requires SIMD
const haveSIMD = await WasmFeatureDetect.simd();
const threadsSupported = await checkThreadsSupport();
if ( haveSIMD )
{
if ( threadsSupported )
{
return WasmType.AdvancedWithThreads;
}
else
{
return WasmType.Advanced;
}
}
else
{
return WasmType.Basic;
}
}
export function wasmFolder( wasmType: WasmType ): string
{
switch ( wasmType )
{
case WasmType.AdvancedWithThreads:
return "advanced-threads";
case WasmType.Advanced:
return "advanced";
case WasmType.Basic:
return "basic";
}
}