Skip to content

Commit

Permalink
feat(runPipeline): Support specification of pipelineWorkerUrl
Browse files Browse the repository at this point in the history
Use case: set to null so vite / webworker vendor.
  • Loading branch information
thewtex committed Jan 20, 2023
1 parent 9a6082d commit bc7f95a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
14 changes: 14 additions & 0 deletions cypress/e2e/pipeline/runPipeline.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ describe('runPipeline', () => {
})
})

it('fetches WASM files from a custom pipelineWorkerUrl string', () => {
cy.window().then(async (win) => {
const itk = win.itk
const pipelineBaseUrl = '/pipelines'
const pipelineWorkerUrl = '/web-workers/bundles/pipeline.worker.js'

const args = []
const outputs = null
const inputs = null
const stdoutStderrPath = 'stdout-stderr-test'
const { webWorker, returnValue, stdout, stderr } = await itk.runPipeline(null, stdoutStderrPath, args, outputs, inputs, pipelineBaseUrl, pipelineWorkerUrl)
})
})

it('re-uses a WebWorker', () => {
cy.window().then(async (win) => {
const itk = win.itk
Expand Down
12 changes: 6 additions & 6 deletions src/core/createWebWorkerPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface itkWorker extends Worker {
}

// Internal function to create a web worker promise
async function createWebWorkerPromise (existingWorker: Worker | null): Promise<createWebWorkerPromiseResult> {
async function createWebWorkerPromise (existingWorker: Worker | null, pipelineWorkerUrl?: string): Promise<createWebWorkerPromiseResult> {
let workerPromise: typeof WebworkerPromise
if (existingWorker != null) {
// See if we have a worker promise attached the worker, if so reuse it. This ensures
Expand All @@ -28,6 +28,7 @@ async function createWebWorkerPromise (existingWorker: Worker | null): Promise<c
return await Promise.resolve({ webworkerPromise: workerPromise, worker: existingWorker })
}

const workerUrl = typeof pipelineWorkerUrl === 'undefined' ? config.pipelineWorkerUrl : pipelineWorkerUrl
let worker = null
// @ts-expect-error: error TS2339: Property 'webWorkersUrl' does not exist on type '{ pipelineWorkerUrl: string; imageIOUrl: string; meshIOUrl: string; pipelinesUrl: string; }
const webWorkersUrl = config.webWorkersUrl
Expand All @@ -44,7 +45,7 @@ async function createWebWorkerPromise (existingWorker: Worker | null): Promise<c
} else {
worker = new Worker(`${webWorkerString}/${min}bundles/pipeline.worker.js`)
}
} else if (config.pipelineWorkerUrl === null) {
} else if (workerUrl === null) {
// Use the version built with the bundler
//
// Bundlers, e.g. WebPack, see these paths at build time
Expand All @@ -54,12 +55,11 @@ async function createWebWorkerPromise (existingWorker: Worker | null): Promise<c
// https://bugzilla.mozilla.org/show_bug.cgi?id=1540913
worker = new Worker(new URL('../web-workers/pipeline.worker.js', import.meta.url))
} else {
const pipelineWorkerUrl = config.pipelineWorkerUrl
if (pipelineWorkerUrl.startsWith('http')) {
const response = await axios.get(pipelineWorkerUrl, { responseType: 'blob' })
if (workerUrl.startsWith('http')) {
const response = await axios.get(workerUrl, { responseType: 'blob' })
worker = new Worker(URL.createObjectURL(response.data as Blob))
} else {
worker = new Worker(pipelineWorkerUrl)
worker = new Worker(workerUrl)
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/pipeline/runPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ async function runPipeline (
args: string[],
outputs: PipelineOutput[] | null,
inputs: PipelineInput[] | null,
pipelineBaseUrl: string | URL = 'pipelinesUrl'
pipelineBaseUrl: string | URL = 'pipelinesUrl',
pipelineWorkerUrl?: string | URL
): Promise<RunPipelineResult> {
if (webWorker === false) {
const pipelineModule = await loadPipelineModule(pipelinePath.toString())
const result = runPipelineEmscripten(pipelineModule, args, outputs, inputs)
return result
}
let worker = webWorker
const pipelineWorkerUrlString = typeof pipelineWorkerUrl !== 'string' && typeof pipelineWorkerUrl?.href !== 'undefined' ? pipelineWorkerUrl.href : pipelineWorkerUrl
const { webworkerPromise, worker: usedWorker } = await createWebWorkerPromise(
worker as Worker | null
worker as Worker | null, pipelineWorkerUrlString as string | undefined
)
worker = usedWorker
const transferables: ArrayBuffer[] = []
Expand Down

0 comments on commit bc7f95a

Please sign in to comment.