Skip to content

Commit

Permalink
Merge pull request InsightSoftwareConsortium#730 from thewtex/compres…
Browse files Browse the repository at this point in the history
…s-stringify-pipeline-base

Compress stringify pipeline base
  • Loading branch information
thewtex authored Jan 25, 2023
2 parents 0c43e14 + 97f4f76 commit 5d98ee7
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 118 deletions.
334 changes: 299 additions & 35 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"build:workerMinBundles": "rollup -c rollup.worker.min.config.js ./dist/web-workers/pipeline.worker.js && shx cp ./dist/web-workers/min-bundles/pipeline.worker.js ./dist/web-workers/bundles/pipeline.min.worker.js",
"build:webpack": "webpack --mode production --progress --color && webpack --mode development --progress --color",
"build:emscripten": "node ./src/build-emscripten.js",
"build:emscripten:compress-stringify": "node ./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build build",
"build:emscripten:compress-stringify": "node ./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build build ",
"build:bindgen:typescript:compress-stringify": "./src/itk-wasm-cli.js -s packages/compress-stringify -b emscripten-build bindgen --package-name itk-compress-stringify --package-description \"Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.\" --repository 'https://github.com/InsightSoftwareConsortium/itk-wasm'",
"build:wasi": "node ./src/build-wasi.js",
"build:wasi:compress-stringify": "node ./src/itk-wasm-cli.js -i itkwasm/wasi:latest -s packages/compress-stringify -b wasi-build build",
"cypress:open": "npx cypress open",
Expand Down Expand Up @@ -109,7 +110,7 @@
"cypress": "^10.10.0",
"cz-conventional-changelog": "2.1.0",
"file-api": "^0.10.4",
"glob": "^7.1.6",
"glob": "^8.1.0",
"karma": "^6.3.2",
"karma-chrome-launcher": "^3.1.0",
"karma-tap": "^4.2.0",
Expand Down
24 changes: 22 additions & 2 deletions packages/compress-stringify/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.

[**<p align="center">Example</p>**](https://itk-compress-stringify-app.on.fleek.co/ ':include :type=iframe width=100% height=800px')
[Example](https://itk-compress-stringify-app.on.fleek.co/ ':include :type=iframe width=100% height=800px')

[**<p align="center">Documentation</p>**](https://itk-compress-stringify-docs.on.fleek.co/)
[Documentation](https://itk-compress-stringify-docs.on.fleek.co/)

## Installation

Expand All @@ -26,6 +26,8 @@ import {
parseStringDecompress,
setPipelinesBaseUrl,
getPipelinesBaseUrl,
setPipelineWorkerUrl,
getPipelineWorkerUrl,
} from "itk-compress-stringify"
```

Expand Down Expand Up @@ -109,6 +111,24 @@ function setPipelinesBaseUrl(
function getPipelinesBaseUrl() : string | URL
```

#### setPipelineWorkerUrl

*Set base URL for the itk-wasm pipeline worker script when vendored.*

```ts
function setPipelineWorkerUrl(
baseUrl: string | URL
) : void
```

#### getPipelineWorkerUrl

*Get base URL for the itk-wasm pipeline worker script when vendored.*

```ts
function getPipelineWorkerUrl() : string | URL
```

### Node interface

Import:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
viteStaticCopy({
targets: [
{ src: '../../dist/pipelines/*', dest: 'pipelines' },
{ src: '../../dist/web-workers/*', dest: 'web-workers' },
],
})
],
Expand Down
4 changes: 2 additions & 2 deletions packages/compress-stringify/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itk-compress-stringify",
"version": "0.1.0",
"version": "0.3.0",
"description": "Zstandard compression and decompression and base64 encoding and decoding in WebAssembly.",
"type": "module",
"module": "./dist/bundles/itk-compress-stringify.js",
Expand Down Expand Up @@ -38,7 +38,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "^1.0.0-b.57"
"itk-wasm": "^1.0.0-b.62"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
13 changes: 9 additions & 4 deletions packages/compress-stringify/typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import CompressStringifyResult from './compress-stringify-result.js'

import { getPipelinesBaseUrl } from './pipelines-base-url.js'


import { getPipelineWorkerUrl } from './pipeline-worker-url.js'

/**
* Given a binary, compress and optionally base64 encode.
*
Expand Down Expand Up @@ -55,7 +58,7 @@ async function compressStringify(
returnValue,
stderr,
outputs
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())
if (returnValue !== 0) {
throw new Error(stderr)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/compress-stringify/typescript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './pipelines-base-url.js'

export * from './pipeline-worker-url.js'


import CompressStringifyResult from './compress-stringify-result.js'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import ParseStringDecompressResult from './parse-string-decompress-result.js'

import { getPipelinesBaseUrl } from './pipelines-base-url.js'


import { getPipelineWorkerUrl } from './pipeline-worker-url.js'

/**
* Given a binary or string produced with compress-stringify, decompress and optionally base64 decode.
*
Expand Down Expand Up @@ -49,7 +52,7 @@ async function parseStringDecompress(
returnValue,
stderr,
outputs
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())
} = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())
if (returnValue !== 0) {
throw new Error(stderr)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/compress-stringify/typescript/src/pipeline-worker-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import packageJson from '../package.json'
let pipelineWorkerUrl: string | URL | null = `https://cdn.jsdelivr.net/npm/itk-compress-stringify@${packageJson.version}/dist/web-workers/pipeline.worker.js`

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
}

export function getPipelineWorkerUrl (): string | URL | null {
return pipelineWorkerUrl
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
import packageJson from '../package.json'
let pipelinesBaseUrl: string | URL = `https://cdn.jsdelivr.net/npm/itk-compress-stringify@${packageJson.version}/dist/pipelines`

export function setPipelinesBaseUrl (baseUrl: string | URL): void {
pipelinesBaseUrl = baseUrl
Expand Down
10 changes: 8 additions & 2 deletions packages/compress-stringify/typescript/test/browser/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as itkCompressStringify from '../../dist/bundles/itk-compress-stringify.js'

// Use local, vendored WebAssembly module assets
const pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
itkCompressStringify.setPipelinesBaseUrl(pipelinesBaseUrl)
let pipelineWorkerUrl: string | URL | null = new URL('/web-workers/pipeline.worker.js', document.location.origin).href
itkCompressStringify.setPipelineWorkerUrl(pipelineWorkerUrl)


// promise-file-reader
function readAsArrayBuffer (file) {
Expand Down Expand Up @@ -107,7 +113,7 @@ function setupCompressStringify() {
form.addEventListener('submit', async (event) => {
event.preventDefault()

const { webWorker, output } = await itkCompressStringify.compressStringify(null, ...context.inputs, context.options)
const { webWorker, output } = await itkCompressStringify.compressStringify(null, context.inputs[0].slice(), context.options)
webWorker.terminate()

context.outputs.output = output
Expand Down Expand Up @@ -161,7 +167,7 @@ function setupParseStringDecompress() {
form.addEventListener('submit', async (event) => {
event.preventDefault()

const { webWorker, output } = await itkCompressStringify.parseStringDecompress(null, ...context.inputs, context.options)
const { webWorker, output } = await itkCompressStringify.parseStringDecompress(null, context.inputs[0].slice(), context.options)
webWorker.terminate()

context.outputs.output = output
Expand Down
11 changes: 11 additions & 0 deletions src/bindgen/typescript-resources/pipeline-worker-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-expect-error error TS2732: Cannot find module '../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
import packageJson from '../package.json'
let pipelineWorkerUrl: string | URL | null = `https://cdn.jsdelivr.net/npm/<bindgenPackageName>@${packageJson.version as string}/dist/web-workers/pipeline.worker.js`

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
}

export function getPipelineWorkerUrl (): string | URL | null {
return pipelineWorkerUrl
}
4 changes: 3 additions & 1 deletion src/bindgen/typescript-resources/pipelines-base-url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
let pipelinesBaseUrl: string | URL = new URL('/pipelines', document.location.origin).href
// @ts-expect-error error TS2732: Cannot find module '../package.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
import packageJson from '../package.json'
let pipelinesBaseUrl: string | URL = `https://cdn.jsdelivr.net/npm/<bindgenPackageName>@${packageJson.version as string}/dist/pipelines`

export function setPipelinesBaseUrl (baseUrl: string | URL): void {
pipelinesBaseUrl = baseUrl
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/typescript-resources/template.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "^1.0.0-b.57"
"itk-wasm": "^1.0.0-b.62"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/bindgen/typescript-resources/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
viteStaticCopy({
targets: [
{ src: '../../dist/pipelines/*', dest: 'pipelines' },
{ src: '../../dist/web-workers/*', dest: 'web-workers' },
],
})
],
Expand Down
15 changes: 13 additions & 2 deletions src/bindgen/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
}

if (!forNode) {
indexContent += "export * from './pipelines-base-url.js'\n\n"
indexContent += "export * from './pipelines-base-url.js'\n"
indexContent += "export * from './pipeline-worker-url.js'\n"
try {
fs.mkdirSync(path.join(outputDir, 'build'), { recursive: true })
} catch (err) {
Expand Down Expand Up @@ -315,6 +316,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
functionContent += "\nimport path from 'path'\n\n"
} else {
functionContent += "\nimport { getPipelinesBaseUrl } from './pipelines-base-url.js'\n\n"
functionContent += "\nimport { getPipelineWorkerUrl } from './pipeline-worker-url.js'\n\n"
}

const readmeParametersTable = [['Parameter', 'Type', 'Description'],]
Expand Down Expand Up @@ -446,7 +448,7 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
functionContent += ` const {\n returnValue,\n stderr,\n outputs\n } = await runPipelineNode(pipelinePath, args, desiredOutputs, inputs)\n`
} else {
functionContent += `\n const pipelinePath = '${moduleKebabCase}'\n\n`
functionContent += ` const {\n webWorker: usedWebWorker,\n returnValue,\n stderr,\n outputs\n } = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl())\n`
functionContent += ` const {\n webWorker: usedWebWorker,\n returnValue,\n stderr,\n outputs\n } = await runPipeline(webWorker, pipelinePath, args, desiredOutputs, inputs, getPipelinesBaseUrl(), getPipelineWorkerUrl())\n`
}

functionContent += ' if (returnValue !== 0) {\n throw new Error(stderr)\n }\n\n'
Expand Down Expand Up @@ -483,6 +485,15 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
const pipelinesBaseUrlPath = path.join(outputDir, 'src', 'pipelines-base-url.ts')
if (!fs.existsSync(pipelinesBaseUrlPath)) {
fs.copyFileSync(bindgenResource('pipelines-base-url.ts'), pipelinesBaseUrlPath)
let pipelinesBaseUrlPathContent = fs.readFileSync(bindgenResource('pipelines-base-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelinesBaseUrlPathContent = pipelinesBaseUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(pipelinesBaseUrlPath, pipelinesBaseUrlPathContent)
}
const pipelineWorkerUrlPath = path.join(outputDir, 'src', 'pipeline-worker-url.ts')
if (!fs.existsSync(pipelineWorkerUrlPath)) {
let pipelineWorkerUrlPathContent = fs.readFileSync(bindgenResource('pipeline-worker-url.ts'), { encoding: 'utf8', flag: 'r' })
pipelineWorkerUrlPathContent = pipelineWorkerUrlPathContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(pipelineWorkerUrlPath, pipelineWorkerUrlPathContent)
}

const itkConfigPath = path.join(outputDir, 'src', 'itkConfig.js')
Expand Down
3 changes: 3 additions & 0 deletions src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export * from '../core/index.js'

export * from '../core/pipeline-worker-url.js'
export * from '../core/pipelines-base-url.js'

export { default as ReadImageResult } from '../io/ReadImageResult.js'
export { default as ReadMeshResult } from '../io/ReadMeshResult.js'

Expand Down
9 changes: 9 additions & 0 deletions src/core/pipeline-worker-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let pipelineWorkerUrl: string | URL | null | undefined

export function setPipelineWorkerUrl (workerUrl: string | URL | null): void {
pipelineWorkerUrl = workerUrl
}

export function getPipelineWorkerUrl (): string | URL | null | undefined {
return pipelineWorkerUrl
}
9 changes: 9 additions & 0 deletions src/core/pipelines-base-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let pipelinesBaseUrl: string | URL | undefined

export function setPipelinesBaseUrl (baseUrl: string | URL): void {
pipelinesBaseUrl = baseUrl
}

export function getPipelinesBaseUrl (): string | URL | undefined {
return pipelinesBaseUrl
}
Loading

0 comments on commit 5d98ee7

Please sign in to comment.