Skip to content

Commit 195553e

Browse files
committed
feat(itk-wasm): add pthreadSupportAvailable
1 parent 07179af commit 195553e

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

packages/core/typescript/itk-wasm/src/pipeline/index-common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { default as pthreadSupportAvailable } from './pthread-support-available.js'
2+
13
export type { default as ItkWasmEmscriptenModule } from './itk-wasm-emscripten-module.js'
24

35
export type { default as PipelineInput } from './pipeline-input.js'

packages/core/typescript/itk-wasm/src/pipeline/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// itk-wasm Browser pipeline functions
1+
// ITK-Wasm Browser pipeline functions
22

33
export { default as runPipeline } from './run-pipeline.js'
44
export { default as createWebWorker } from './create-web-worker.js'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function pthreadSupportAvailable(): boolean {
2+
const haveSharedArrayBuffer =
3+
typeof globalThis.SharedArrayBuffer === 'function'
4+
5+
// Emscripten ENVIRONMENT_IS_NODE
6+
const isNode =
7+
typeof process == 'object' &&
8+
typeof process.versions == 'object' &&
9+
typeof process.versions.node == 'string' &&
10+
// @ts-ignore: ts(2339)
11+
process.type != 'renderer'
12+
13+
const isCrossOriginIsolated =
14+
typeof crossOriginIsolated !== 'undefined'
15+
? crossOriginIsolated
16+
: globalThis.crossOriginIsolated || false
17+
18+
if (isNode) {
19+
return haveSharedArrayBuffer
20+
}
21+
22+
return haveSharedArrayBuffer && isCrossOriginIsolated
23+
}
24+
25+
export default pthreadSupportAvailable
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test from 'ava'
2+
3+
import { pthreadSupportAvailable } from '../../../dist/index-node.js'
4+
5+
test('node pthread support available', (t) => {
6+
t.truthy(pthreadSupportAvailable())
7+
})

0 commit comments

Comments
 (0)