Skip to content

Commit fdf7384

Browse files
authored
Merge pull request #850 from thewtex/run-pipeline-node-pass-mounts
runPipelineNode pass mounts
2 parents 7a8181d + 889b7b4 commit fdf7384

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

docs/api/browser_pipelines.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ These functions return the [`WebWorker`](https://developer.mozilla.org/en-US/doc
1212
## `runPipeline`
1313

1414
```ts
15-
runPipelineNode(webWorker: Worker | null | boolean,
15+
runPipeline(webWorker: Worker | null | boolean,
1616
pipelinePath: string | URL,
1717
args: string[],
1818
outputs: PipelineOutput[] | null,

docs/api/node_pipelines.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ Similar to the [web browser API](./browser_pipelines.html), most of these functi
1212
runPipelineNode(pipelinePath: string,
1313
args: string[],
1414
outputs: PipelineOutput[],
15-
inputs: PipelineInput[] | null):
15+
inputs: PipelineInput[] | null,
16+
mountContainingDirs?: Set<string>):
1617
Promise<{
1718
returnValue: number,
1819
stdout: string,
@@ -49,6 +50,10 @@ A JavaScript Array of [`PipelineInput`](https://github.com/InsightSoftwareConsor
4950
- `data` contains the corresponding data for the interface type.
5051
- `path` is the optional file path on the filesystem to read after execution has completed.
5152

53+
### `mountContainingDirs`
54+
55+
Filepaths on the local filesystem whose dirname will be mounted to provide the wasm module direct access to the file.
56+
5257
### Result
5358

5459
Promise resolving a JavaScript object with the properties:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"commit": "git cz",
2727
"bindgen": "node ./src/itk-wasm-cli.js bindgen ./dist/dicom/src ./dist/dicom/public/pipelines/*.wasm",
2828
"build": "npm run build:testData && npm run build:emscripten && npm run build:tsc && npm run build:tscWorkersModuleLoader && npm run build:tscWebWorkers && npm run build:workerBundles && npm run build:workerMinBundles && npm run build:webpack && node ./src/io/internal/packages/package-json-gen.cjs && npm run build:emscripten:packages",
29-
"build:testData": "dam download packages/dicom/test/data packages/dicom/test/data.tar.gz bafybeic2ckitzhl5b476fgfewt7ilel3mkzi5x66o6gga5s7n34g2t36xm https://w3s.link/ipfs/bafkreibxuanogkwccski66azxafdpjjj3sbua6g3pqdvwwwe72h3d6agoi",
29+
"build:testData": "dam download packages/dicom/test/data packages/dicom/test/data.tar.gz bafybeic2ckitzhl5b476fgfewt7ilel3mkzi5x66o6gga5s7n34g2t36xm https://github.com/InsightSoftwareConsortium/itk-wasm/releases/download/itk-wasm-v1.0.0-b.113/test-data.tar.gz https://w3s.link/ipfs/bafkreibxuanogkwccski66azxafdpjjj3sbua6g3pqdvwwwe72h3d6agoi",
3030
"build:debug": "npm run build:emscripten -- --debug && npm run build:tsc && npm run build:tscWorkersModuleLoader && npm run build:tscWebWorkers && npm run build:workerBundles && npm run build:workerMinBundles && npm run build:webpack -- --mode development",
3131
"build:tsc": "tsc --pretty",
3232
"build:tscWorkersModuleLoader": "tsc --types --lib es2017,webworker --rootDir ./src/ --outDir ./dist/ --moduleResolution node --target es2017 --module es2020 --strict --forceConsistentCasingInFileNames --declaration ./src/core/internal/loadEmscriptenModuleWebWorker.ts",

packages/dicom/typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@
6464
"type": "git",
6565
"url": "https://github.com/InsightSoftwareConsortium/itk-wasm"
6666
}
67-
}
67+
}

src/emscripten-module/itkJSPost.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/** Given an absolute path to a file, mount its containing directory in the
22
* Emscripten virtual filesystem. Only relevant when within the Node.js
3-
* environment. If the containing directory already exists with the
4-
* Emscripten filesystem, it will not be mounted. */
3+
* environment. */
54
Module.mountContainingDir = function (filePath) {
65
if (!ENVIRONMENT_IS_NODE) {
76
return
87
}
98
var path = require('path')
109
var containingDir = path.dirname(filePath)
11-
// If the directory already exists, abort
12-
if (FS.isDir(containingDir) || containingDir === '/') {
13-
return
10+
// If the root, abort
11+
if (containingDir === '/') {
12+
throw new Error('Cannot mount root directory')
1413
}
1514

1615
var currentDir = '/'

src/io/readImageLocalFile.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import loadEmscriptenModule from '../core/internal/loadEmscriptenModuleNode.js'
1212
import runPipelineEmscripten from '../pipeline/internal/runPipelineEmscripten.js'
1313
import PipelineEmscriptenModule from '../pipeline/PipelineEmscriptenModule.js'
1414
import findLocalImageIOPath from './internal/findLocalImageIOPath.js'
15+
import runPipelineNode from '../pipeline/runPipelineNode.js'
1516
import InterfaceTypes from '../core/InterfaceTypes.js'
1617
import PipelineInput from '../pipeline/PipelineInput.js'
1718
import CastImageOptions from '../core/CastImageOptions.js'
@@ -57,10 +58,9 @@ async function readImageLocalFile (filePath: string, options?: CastImageOptions)
5758
}
5859

5960
const modulePath = path.join(imageIOsPath, io as string + '-read-image.js')
60-
const readImageModule = await loadEmscriptenModule(modulePath) as PipelineEmscriptenModule
61-
const mountedFilePath = readImageModule.mountContainingDir(absoluteFilePath)
62-
const { outputs } = runPipelineEmscripten(readImageModule, args, desiredOutputs, inputs)
63-
readImageModule.unmountContainingDir(mountedFilePath)
61+
const mountContainingDirs = new Set([absoluteFilePath])
62+
const { outputs } = await runPipelineNode(modulePath, args, desiredOutputs, inputs, mountContainingDirs)
63+
6464
let result = outputs[0].data as Image
6565

6666
if (typeof options !== 'undefined') {

src/itk-wasm-cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import pythonBindgen from './bindgen/python.js'
1313
const program = new Command()
1414

1515
// Array of types that will require an import from itk-wasm
16-
const defaultImageTag = '20230526-f226ba4e'
16+
const defaultImageTag = '20230605-8a333e7c'
1717

1818
function processCommonOptions(wasiDefault=false) {
1919
const options = program.opts()

src/pipeline/runPipelineNode.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ async function runPipelineNode (
1010
pipelinePath: string,
1111
args: string[],
1212
outputs: PipelineOutput[],
13-
inputs: PipelineInput[] | null
13+
inputs: PipelineInput[] | null,
14+
mountContainingDirs?: Set<string>
1415
): Promise<RunPipelineResult> {
1516
const Module = (await loadEmscriptenModuleNode(
1617
pipelinePath
1718
)) as PipelineEmscriptenModule
19+
const mountedDirs: Set<string> = new Set()
20+
if (typeof mountContainingDirs !== 'undefined') {
21+
mountContainingDirs.forEach((filePath) => {
22+
mountedDirs.add(Module.mountContainingDir(filePath))
23+
})
24+
}
1825
const result = runPipelineEmscripten(Module, args, outputs, inputs)
26+
if (typeof mountContainingDirs !== 'undefined') {
27+
mountedDirs.forEach((filePath) => {
28+
Module.unmountContainingDir(filePath)
29+
})
30+
}
1931
return result
2032
}
2133

0 commit comments

Comments
 (0)