Skip to content

Commit

Permalink
feat(emscripten): load and decode .wasm.zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Sep 6, 2023
1 parent 7baa1cf commit 3456c69
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
27 changes: 21 additions & 6 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@
"markdown-table": "^3.0.3",
"mime-types": "^2.1.35",
"wasm-feature-detect": "^1.5.1",
"webworker-promise": "^0.4.2"
"webworker-promise": "^0.4.2",
"@thewtex/zstddec": "^0.1.1"
},
"bin": {
"itk-wasm": "./src/itk-wasm-cli.js"
Expand Down
1 change: 1 addition & 0 deletions src/bindgen/typescript/typescript-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function typescriptBindings (outputDir, buildDir, wasmBinaries, options, forNode
if (err.code !== 'EEXIST') throw err
}
fs.copyFileSync(wasmBinaryRelativePath, path.join(distPipelinesDir, path.basename(wasmBinaryRelativePath)))
fs.copyFileSync(`${wasmBinaryRelativePath}.zstd`, path.join(distPipelinesDir, `${path.basename(wasmBinaryRelativePath)}.zstd`))
const prefix = wasmBinaryRelativePath.substring(0, wasmBinaryRelativePath.length-5)
fs.copyFileSync(`${prefix}.js`, path.join(distPipelinesDir, `${path.basename(prefix)}.js`))
fs.copyFileSync(`${prefix}.umd.js`, path.join(distPipelinesDir, `${path.basename(prefix)}.umd.js`))
Expand Down
7 changes: 7 additions & 0 deletions src/build-emscripten.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ if (options.copyBuildArtifacts) {
}
let imageIOFiles = glob.sync(path.join(buildDir, 'image-io', '*.js'))
imageIOFiles = imageIOFiles.concat(glob.sync(path.join(buildDir, 'image-io', '*.wasm')))
imageIOFiles = imageIOFiles.concat(glob.sync(path.join(buildDir, 'image-io', '*.wasm.zstd')))
imageIOFiles = imageIOFiles.filter((fn) => !fn.endsWith('.umd.wasm'))
imageIOFiles = imageIOFiles.filter((fn) => !fn.endsWith('.umd.wasm.zstd'))
const copyImageIOModules = function (imageIOFile, callback) {
const io = path.basename(imageIOFile)
const output = path.join('dist', 'image-io', io)
Expand All @@ -154,7 +156,9 @@ if (options.copyBuildArtifacts) {
}
let meshIOFiles = glob.sync(path.join(buildDir, 'mesh-io', '*.js'))
meshIOFiles = meshIOFiles.concat(glob.sync(path.join(buildDir, 'mesh-io', '*.wasm')))
meshIOFiles = meshIOFiles.concat(glob.sync(path.join(buildDir, 'mesh-io', '*.wasm.zstd')))
meshIOFiles = meshIOFiles.filter((fn) => !fn.endsWith('.umd.wasm'))
meshIOFiles = meshIOFiles.filter((fn) => !fn.endsWith('.umd.wasm.zstd'))
const copyMeshIOModules = function (meshIOFile, callback) {
const io = path.basename(meshIOFile)
const output = path.join('dist', 'mesh-io', io)
Expand All @@ -168,7 +172,9 @@ if (options.copyBuildArtifacts) {
}
let dicomFiles = glob.sync(path.join(buildDir, 'dicom', '*.js'))
dicomFiles = dicomFiles.concat(glob.sync(path.join(buildDir, 'dicom', '*.wasm')))
dicomFiles = dicomFiles.concat(glob.sync(path.join(buildDir, 'dicom', '*.wasm.zstd')))
dicomFiles = dicomFiles.filter((fn) => !fn.endsWith('.umd.wasm'))
dicomFiles = dicomFiles.filter((fn) => !fn.endsWith('.umd.wasm.zstd'))
const copyDICOMModules = function (dicomFile, callback) {
const io = path.basename(dicomFile)
const output = path.join('dist', 'dicom', 'public', 'pipelines', io)
Expand Down Expand Up @@ -219,6 +225,7 @@ if (options.buildTestPipelines) {
}
let pipelineFiles = glob.sync(path.join(pipelinePath, 'emscripten-build', '*.js'))
pipelineFiles = pipelineFiles.concat(glob.sync(path.join(pipelinePath, 'emscripten-build', '*.wasm')))
pipelineFiles = pipelineFiles.concat(glob.sync(path.join(pipelinePath, 'emscripten-build', '*.wasm.zstd')))
pipelineFiles.forEach((file) => {
const filename = path.basename(file)
const output = path.join('dist', 'pipelines', filename)
Expand Down
13 changes: 11 additions & 2 deletions src/core/internal/loadEmscriptenModuleWebWorker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import axios from 'axios'

import { ZSTDDecoder } from '@thewtex/zstddec'
const decoder = new ZSTDDecoder()
let decoderInitialized = false

import ITKWasmEmscriptenModule from '../ITKWasmEmscriptenModule.js'
import camelCase from './camelCase.js'

Expand All @@ -26,8 +30,13 @@ async function loadEmscriptenModuleWebWorker(moduleRelativePathOrURL: string | U
// adds worker dynamic import support:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1540913
const wasmBinaryPath = `${modulePrefix}.wasm`
const response = await axios.get(wasmBinaryPath, { responseType: 'arraybuffer' })
const wasmBinary = response.data
const response = await axios.get(`${wasmBinaryPath}.zstd`, { responseType: 'arraybuffer' })
if (!decoderInitialized) {
await decoder.init()
decoderInitialized = true
}
const decompressedArray = decoder.decode(new Uint8Array(response.data))
const wasmBinary = decompressedArray.buffer
const modulePath = `${modulePrefix}.umd.js`
importScripts(modulePath)
const moduleBaseName: string = camelCase(modulePrefix.replace(/.*\//, ''))
Expand Down

0 comments on commit 3456c69

Please sign in to comment.