Skip to content

Commit c2c1c51

Browse files
authored
Merge pull request #1093 from pritamrungta/main
feat(itk-wasm): #1087 - Add query params options to pipeline URL
2 parents d010a4d + 212e022 commit c2c1c51

File tree

12 files changed

+71
-18
lines changed

12 files changed

+71
-18
lines changed

packages/core/typescript/itk-wasm/cypress/e2e/pipeline/run-pipeline.cy.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ describe('runPipeline', () => {
3434
})
3535
})
3636

37+
it('fetches Wasm files from a custom URL and query params', () => {
38+
cy.window().then(async (win) => {
39+
const itk = win.itk
40+
41+
const args = []
42+
const outputs = null
43+
const inputs = null
44+
const stdoutStderrPath = 'stdout-stderr-test'
45+
const { webWorker, returnValue, stdout, stderr } = await itk.runPipeline(stdoutStderrPath, args, outputs, inputs, { pipelineBaseUrl, pipelineWorkerUrl, pipelineQueryParams: {key: 'value'} })
46+
})
47+
})
48+
3749
it('fetches Wasm files from a custom pipelineBaseUrl string', () => {
3850
cy.window().then(async (win) => {
3951
const itk = win.itk

packages/core/typescript/itk-wasm/src/pipeline/create-web-worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios from 'axios'
2+
import RunPipelineOptions from './run-pipeline-options'
23

3-
async function createWebWorker (pipelineWorkerUrl?: string | null): Promise<Worker> {
4+
async function createWebWorker (pipelineWorkerUrl?: string | null, queryParams?: RunPipelineOptions['pipelineQueryParams']): Promise<Worker> {
45
const workerUrl = pipelineWorkerUrl
56
let worker = null
67
if (workerUrl === null) {
@@ -10,7 +11,7 @@ async function createWebWorker (pipelineWorkerUrl?: string | null): Promise<Work
1011
worker = new Worker(new URL('./web-workers/itk-wasm-pipeline.worker.js', import.meta.url), { type: 'module' })
1112
} else {
1213
if ((workerUrl as string).startsWith('http')) {
13-
const response = await axios.get((workerUrl as string), { responseType: 'blob' })
14+
const response = await axios.get((workerUrl as string), { responseType: 'blob', params: queryParams })
1415
const workerObjectUrl = URL.createObjectURL(response.data as Blob)
1516
worker = new Worker(workerObjectUrl, { type: 'module' })
1617
} else {

packages/core/typescript/itk-wasm/src/pipeline/create-worker-proxy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Comlink from 'comlink'
33
import WorkerProxy from './web-workers/worker-proxy.js'
44
import createWebWorker from './create-web-worker.js'
55
import ItkWorker from './itk-worker.js'
6+
import RunPipelineOptions from './run-pipeline-options.js'
67

78
interface createWorkerProxyResult {
89
workerProxy: WorkerProxy
@@ -24,7 +25,7 @@ function workerToWorkerProxy (worker: Worker): createWorkerProxyResult {
2425
}
2526

2627
// Internal function to create a web worker proxy
27-
async function createWorkerProxy (existingWorker: Worker | null, pipelineWorkerUrl?: string | null): Promise<createWorkerProxyResult> {
28+
async function createWorkerProxy (existingWorker: Worker | null, pipelineWorkerUrl?: string | null, queryParams?: RunPipelineOptions['pipelineQueryParams']): Promise<createWorkerProxyResult> {
2829
let workerProxy: WorkerProxy
2930
if (existingWorker != null) {
3031
// See if we have a worker promise attached the worker, if so reuse it. This ensures
@@ -38,7 +39,7 @@ async function createWorkerProxy (existingWorker: Worker | null, pipelineWorkerU
3839
}
3940
}
4041

41-
const worker = await createWebWorker(pipelineWorkerUrl)
42+
const worker = await createWebWorker(pipelineWorkerUrl, queryParams)
4243

4344
return workerToWorkerProxy(worker)
4445
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export type { default as ItkWorker } from './itk-worker.js'
66

77
export * from './pipeline-worker-url.js'
88
export * from './pipelines-base-url.js'
9+
export * from './pipelines-query-params.js'
910
export * from './default-web-worker.js'

packages/core/typescript/itk-wasm/src/pipeline/internal/load-emscripten-module-main-thread.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import axios from 'axios'
22

33
import EmscriptenModule from '../itk-wasm-emscripten-module.js'
4+
import RunPipelineOptions from '../run-pipeline-options.js'
45

5-
async function loadEmscriptenModuleMainThread (moduleRelativePathOrURL: string | URL, baseUrl?: string): Promise<EmscriptenModule> {
6+
async function loadEmscriptenModuleMainThread (moduleRelativePathOrURL: string | URL, baseUrl?: string, queryParams?: RunPipelineOptions['pipelineQueryParams']): Promise<EmscriptenModule> {
67
let modulePrefix: string = 'unknown'
78
if (typeof moduleRelativePathOrURL !== 'string') {
89
modulePrefix = moduleRelativePathOrURL.href
@@ -18,7 +19,7 @@ async function loadEmscriptenModuleMainThread (moduleRelativePathOrURL: string |
1819
modulePrefix = modulePrefix.substring(0, modulePrefix.length - 5)
1920
}
2021
const wasmBinaryPath = `${modulePrefix}.wasm`
21-
const response = await axios.get(wasmBinaryPath, { responseType: 'arraybuffer' })
22+
const response = await axios.get(wasmBinaryPath, { responseType: 'arraybuffer', params: queryParams })
2223
const wasmBinary = response.data
2324
const fullModulePath = `${modulePrefix}.js`
2425
const result = await import(/* webpackIgnore: true */ /* @vite-ignore */ fullModulePath)

packages/core/typescript/itk-wasm/src/pipeline/internal/load-emscripten-module-web-worker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import axios from 'axios'
33
import { ZSTDDecoder } from '@thewtex/zstddec'
44

55
import ITKWasmEmscriptenModule from '../itk-wasm-emscripten-module.js'
6+
import RunPipelineOptions from '../run-pipeline-options.js'
7+
68
const decoder = new ZSTDDecoder()
79
let decoderInitialized = false
810

911
// Load the Emscripten module in the browser in a WebWorker.
1012
//
1113
// baseUrl is usually taken from 'getPipelinesBaseUrl()', but a different value
1214
// could be passed.
13-
async function loadEmscriptenModuleWebWorker (moduleRelativePathOrURL: string | URL, baseUrl: string): Promise<ITKWasmEmscriptenModule> {
15+
async function loadEmscriptenModuleWebWorker (moduleRelativePathOrURL: string | URL, baseUrl: string, queryParams?: RunPipelineOptions['pipelineQueryParams']): Promise<ITKWasmEmscriptenModule> {
1416
let modulePrefix = null
1517
if (typeof moduleRelativePathOrURL !== 'string') {
1618
modulePrefix = moduleRelativePathOrURL.href
@@ -26,7 +28,7 @@ async function loadEmscriptenModuleWebWorker (moduleRelativePathOrURL: string |
2628
modulePrefix = modulePrefix.substring(0, modulePrefix.length - 5)
2729
}
2830
const wasmBinaryPath = `${modulePrefix}.wasm`
29-
const response = await axios.get(`${wasmBinaryPath}.zst`, { responseType: 'arraybuffer' })
31+
const response = await axios.get(`${wasmBinaryPath}.zst`, { responseType: 'arraybuffer', params: queryParams })
3032
if (!decoderInitialized) {
3133
await decoder.init()
3234
decoderInitialized = true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import RunPipelineOptions from "./run-pipeline-options"
2+
3+
let pipelinesQueryParams: RunPipelineOptions['pipelineQueryParams'] | undefined
4+
5+
export function setPipelinesQueryParams (queryParams: RunPipelineOptions['pipelineQueryParams']): void {
6+
pipelinesQueryParams = queryParams
7+
}
8+
9+
export function getPipelinesQueryParams (): RunPipelineOptions['pipelineQueryParams'] | undefined {
10+
return pipelinesQueryParams
11+
}

packages/core/typescript/itk-wasm/src/pipeline/run-pipeline-options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ interface RunPipelineOptions {
55
/** Url where WebAssembly pipelines are hosted. */
66
pipelineBaseUrl?: string | URL
77

8+
/** Query params to use when requesting for WebAssembly pipelines */
9+
pipelineQueryParams?: Record<string, string>
10+
811
/** Url where the pipeline web worker is hosted. */
912
pipelineWorkerUrl?: string | URL | null
1013

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import polyDataTransferables from './internal/poly-data-transferables.js'
2525
import TypedArray from '../typed-array.js'
2626
import RunPipelineWorkerResult from './web-workers/run-pipeline-worker-result.js'
2727
import { getPipelinesBaseUrl } from './pipelines-base-url.js'
28+
import { getPipelinesQueryParams } from './pipelines-query-params.js'
2829
import { getPipelineWorkerUrl } from './pipeline-worker-url.js'
2930

3031
// To cache loaded pipeline modules
@@ -46,9 +47,18 @@ function defaultPipelinesBaseUrl (): string {
4647
return result as string
4748
}
4849

50+
function defaultPipelinesQueryParams (): RunPipelineOptions['pipelineQueryParams'] {
51+
let result = getPipelinesQueryParams()
52+
if (typeof result === 'undefined') {
53+
result = {}
54+
}
55+
return result
56+
}
57+
4958
async function loadPipelineModule (
5059
pipelinePath: string | URL,
51-
pipelineBaseUrl?: string | URL
60+
pipelineBaseUrl?: string | URL,
61+
pipelineQueryParams?: RunPipelineOptions['pipelineQueryParams']
5262
): Promise<PipelineEmscriptenModule> {
5363
let moduleRelativePathOrURL: string | URL = pipelinePath as string
5464
let pipeline = pipelinePath as string
@@ -61,7 +71,8 @@ async function loadPipelineModule (
6171
} else {
6272
const pipelineModule = (await loadEmscriptenModuleMainThread(
6373
pipelinePath,
64-
pipelineBaseUrl?.toString() ?? defaultPipelinesBaseUrl()
74+
pipelineBaseUrl?.toString() ?? defaultPipelinesBaseUrl(),
75+
pipelineQueryParams ?? defaultPipelinesQueryParams()
6576
)) as PipelineEmscriptenModule
6677
pipelineToModule.set(pipeline, pipelineModule)
6778
return pipelineModule
@@ -83,15 +94,21 @@ async function runPipeline (
8394
const webWorker = options?.webWorker ?? null
8495

8596
if (webWorker === false) {
86-
const pipelineModule = await loadPipelineModule(pipelinePath.toString(), options?.pipelineBaseUrl)
97+
const pipelineModule = await loadPipelineModule(
98+
pipelinePath.toString(),
99+
options?.pipelineBaseUrl,
100+
options?.pipelineQueryParams ?? defaultPipelinesQueryParams(),
101+
)
87102
const result = runPipelineEmscripten(pipelineModule, args, outputs, inputs)
88103
return result
89104
}
90105
let worker = webWorker
91106
const pipelineWorkerUrl = options?.pipelineWorkerUrl ?? defaultPipelineWorkerUrl()
92107
const pipelineWorkerUrlString = typeof pipelineWorkerUrl !== 'string' && typeof pipelineWorkerUrl?.href !== 'undefined' ? pipelineWorkerUrl.href : pipelineWorkerUrl
93108
const { workerProxy, worker: usedWorker } = await createWorkerProxy(
94-
worker as Worker | null, pipelineWorkerUrlString as string | undefined | null
109+
worker as Worker | null,
110+
pipelineWorkerUrlString as string | undefined | null,
111+
options?.pipelineQueryParams ?? defaultPipelinesQueryParams(),
95112
)
96113
worker = usedWorker
97114
const transferables: Array<ArrayBuffer | TypedArray | null> = []
@@ -130,7 +147,8 @@ async function runPipeline (
130147
pipelineBaseUrlString as string,
131148
args,
132149
outputs,
133-
transferedInputs
150+
transferedInputs,
151+
options?.pipelineQueryParams ?? defaultPipelinesQueryParams()
134152
)
135153
return {
136154
returnValue: result.returnValue,

packages/core/typescript/itk-wasm/src/pipeline/web-workers/itk-wasm-pipeline.worker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import PipelineInput from '../pipeline-input.js'
55
import RunPipelineWorkerResult from './run-pipeline-worker-result.js'
66
import loadPipelineModule from './load-pipeline-module.js'
77
import runPipeline from './run-pipeline.js'
8+
import RunPipelineOptions from '../run-pipeline-options.js'
89

910
const workerOperations = {
10-
runPipeline: async function (pipelinePath: string, pipelineBaseUrl: string, args: string[], outputs: PipelineOutput[] | null, inputs: PipelineInput[] | null): Promise<RunPipelineWorkerResult> {
11-
const pipelineModule = await loadPipelineModule(pipelinePath, pipelineBaseUrl)
11+
runPipeline: async function (pipelinePath: string, pipelineBaseUrl: string, args: string[], outputs: PipelineOutput[] | null, inputs: PipelineInput[] | null, pipelineQueryParams?: RunPipelineOptions['pipelineQueryParams']): Promise<RunPipelineWorkerResult> {
12+
const pipelineModule = await loadPipelineModule(pipelinePath, pipelineBaseUrl, pipelineQueryParams)
1213
return await runPipeline(pipelineModule, args, outputs, inputs)
1314
}
1415
}

0 commit comments

Comments
 (0)