Skip to content

Commit

Permalink
feat(itkConfig): Support dynamic runtime specification for browser
Browse files Browse the repository at this point in the history
version needs to go into its own module to avoid a circular import.
  • Loading branch information
thewtex committed Oct 19, 2022
1 parent a408e7c commit a461426
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 10 deletions.
18 changes: 17 additions & 1 deletion cypress/e2e/pipeline/run_pipeline.cy.ts
Expand Up @@ -9,6 +9,7 @@ describe('runPipeline', () => {
it('captures stdout and stderr', () => {
cy.window().then(async (win) => {
const itk = win.itk
itk.itkConfig.pipelinesUrl = '/pipelines'

const args = []
const outputs = null
Expand All @@ -19,6 +20,21 @@ describe('runPipeline', () => {
})


it('fetches WASM files from a custom config URL', () => {
cy.window().then(async (win) => {
const itk = win.itk
const configPropertyPipelinesBaseUrl = 'customPipelinesUrl'
itk.itkConfig[configPropertyPipelinesBaseUrl] = '/pipelines'

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, configPropertyPipelinesBaseUrl)
})
})


it('re-uses a WebWorker', () => {
cy.window().then(async (win) => {
const itk = win.itk
Expand Down Expand Up @@ -261,7 +277,7 @@ Click. Perfect success.
})


it('runPipeline writes and reads an itk.Mesh via memory io', async () => {
it('runPipeline writes and reads an itk.Mesh via memory io', () => {
cy.window().then(async (win) => {
const itk = win.itk

Expand Down
6 changes: 3 additions & 3 deletions src/core/index.ts
@@ -1,5 +1,8 @@
// Core API interfaces, data structures, and functions

export { default as itkConfig } from '../itkConfig.js'
export { default as version } from './version.js'

export { default as InterfaceTypes } from './InterfaceTypes.js'

export { default as TextStream } from './TextStream.js'
Expand Down Expand Up @@ -36,6 +39,3 @@ export { default as WorkerPool } from './WorkerPool.js'
export { default as WorkerPoolFunction } from './WorkerPoolFunction.js'
export { default as WorkerPoolProgressCallback } from './WorkerPoolProgressCallback.js'
export { default as WorkerPoolRunTasksResult } from './WorkerPoolRunTasksResult.js'

const version = '0.0.0-semantically-released'
export { version }
1 change: 1 addition & 0 deletions src/core/internal/createWebWorkerPromise.ts
Expand Up @@ -29,6 +29,7 @@ async function createWebWorkerPromise (existingWorker: Worker | null): Promise<c
}

let worker = null
// @ts-ignore: error TS2339: Property 'webWorkersUrl' does not exist on type '{ pipelineWorkerUrl: string; imageIOUrl: string; meshIOUrl: string; pipelinesUrl: string; }
const webWorkersUrl = config.webWorkersUrl
if (typeof webWorkersUrl !== 'undefined') {
console.warn('itkConfig webWorkersUrl is deprecated. Please use pipelineWorkerUrl with the full path to the pipeline worker.')
Expand Down
3 changes: 3 additions & 0 deletions src/core/version.ts
@@ -0,0 +1,3 @@
const version = '0.0.0-semantically-released'

export default version
3 changes: 1 addition & 2 deletions src/itkConfig.ts
@@ -1,7 +1,6 @@
import { version } from './browser/index.js'
import version from './core/version.js'

const itkConfig = {
webWorkersUrl: undefined,
pipelineWorkerUrl: `https://cdn.jsdelivr.net/npm/itk-wasm@${version}/dist/web-workers/min-bundles/pipeline.worker.js`,
imageIOUrl: `https://cdn.jsdelivr.net/npm/itk-image-io@${version}`,
meshIOUrl: `https://cdn.jsdelivr.net/npm/itk-mesh-io@${version}`,
Expand Down
1 change: 0 additions & 1 deletion src/itkConfigDevServer.js
@@ -1,5 +1,4 @@
const itkConfig = {
webWorkersUrl: undefined,
pipelineWorkerUrl: `/web-workers/bundles/pipeline.worker.js`,
imageIOUrl: `/image-io`,
meshIOUrl: `/mesh-io`,
Expand Down
3 changes: 2 additions & 1 deletion src/pipeline/runPipeline.ts
Expand Up @@ -36,7 +36,7 @@ async function loadPipelineModule (pipelinePath: string | URL): Promise<Pipeline
}
}

async function runPipeline (webWorker: Worker | null | boolean, pipelinePath: string | URL, args: string[], outputs: PipelineOutput[] | null, inputs: PipelineInput[] | null): Promise<RunPipelineResult> {
async function runPipeline (webWorker: Worker | null | boolean, pipelinePath: string | URL, args: string[], outputs: PipelineOutput[] | null, inputs: PipelineInput[] | null, configPropertyPipelineBaseUrl: string | URL = 'pipelinesUrl'): Promise<RunPipelineResult> {
if (webWorker === false) {
const pipelineModule = await loadPipelineModule(pipelinePath.toString())
const result = runPipelineEmscripten(pipelineModule, args, outputs, inputs)
Expand Down Expand Up @@ -137,6 +137,7 @@ async function runPipeline (webWorker: Worker | null | boolean, pipelinePath: st
operation: 'runPipeline',
config: config,
pipelinePath: pipelinePath.toString(),
configPropertyPipelineBaseUrl,
args,
outputs,
inputs
Expand Down
3 changes: 2 additions & 1 deletion src/update-versions.cjs
Expand Up @@ -22,5 +22,6 @@ function rewriteVersion(filename) {
fs.writeFileSync(filename, lines.join('\n'))
}

rewriteVersion('dist/core/index.js')
rewriteVersion('src/core/version.ts')
rewriteVersion('dist/core/version.js')
rewriteVersion('dist/itkConfigDevelopment.js')
2 changes: 2 additions & 0 deletions src/web-workers/ITKConfig.ts
Expand Up @@ -4,6 +4,8 @@ interface ITKConfig {
pipelinesUrl: string
imageIOUrl: string
meshIOUrl: string

[key: string]: string | undefined
}

export default ITKConfig
1 change: 1 addition & 0 deletions src/web-workers/RunPipelineInput.ts
Expand Up @@ -4,6 +4,7 @@ import WebWorkerInput from './WebWorkerInput.js'

interface RunPipelineInput extends WebWorkerInput {
pipelinePath: string | object
configPropertyPipelineBaseUrl: string,
args: string[]
outputs: PipelineOutput[]
inputs: PipelineInput[]
Expand Down
6 changes: 5 additions & 1 deletion src/web-workers/pipeline.worker.ts
Expand Up @@ -10,7 +10,11 @@ import IOInput from './IOInput.js'
registerWebworker(async function (input: RunPipelineInput | IOInput) {
let pipelineModule = null
if (input.operation === 'runPipeline') {
pipelineModule = await loadPipelineModule(input.pipelinePath, input.config.pipelinesUrl)
const pipelineBaseUrl = input.config[input.configPropertyPipelineBaseUrl]
if (typeof pipelineBaseUrl === 'undefined') {
throw new Error(`configPropertyPipelineBaseUrl: ${input.configPropertyPipelineBaseUrl} is undefined!`)
}
pipelineModule = await loadPipelineModule(input.pipelinePath, pipelineBaseUrl)
} else if (input.operation === 'readImage') {
pipelineModule = await loadImageIOPipelineModule(input as IOInput, '-read-image')
} else if (input.operation === 'writeImage') {
Expand Down

0 comments on commit a461426

Please sign in to comment.