fix: initialize DICOM decoder modules in browser workers
This commit is contained in:
1 parent
dd29258828
commit
7a1d18dfc6
8 files changed
+105
-41
No files matched your search
@@ -9,7 +9,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
"build": "pnpm run typecheck && vite build",
|
"build": "pnpm run typecheck && vite build && node scripts/check-dicom-worker.mjs",
|
||||||
"clean": "find ./dist -maxdepth 1 -mindepth 1 ! -name '.gitkeep' -exec rm -r {} +",
|
"clean": "find ./dist -maxdepth 1 -mindepth 1 ! -name '.gitkeep' -exec rm -r {} +",
|
||||||
"typecheck": "vue-tsc -p ./tsconfig.app.json --noEmit",
|
"typecheck": "vue-tsc -p ./tsconfig.app.json --noEmit",
|
||||||
"lint": "eslint src/",
|
"lint": "eslint src/",
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import vm from "node:vm";
|
||||||
|
import { fixture } from "../src/utils/__tests__/fixtures/dicom.ts";
|
||||||
|
|
||||||
|
// Exercise the shipped bundle without Node's require or module globals.
|
||||||
|
// Source-module tests alone miss broken CommonJS imports in browser workers.
|
||||||
|
const assets = path.resolve(import.meta.dirname, "../dist/assets");
|
||||||
|
const name = fs
|
||||||
|
.readdirSync(assets)
|
||||||
|
.find((n) => /^dicom\.worker-.*\.js$/.test(n));
|
||||||
|
assert.ok(name, "Production DICOM worker is missing");
|
||||||
|
const replies = [];
|
||||||
|
const self = { postMessage: (data) => replies.push(data) };
|
||||||
|
vm.runInNewContext(fs.readFileSync(path.join(assets, name), "utf8"), {
|
||||||
|
self,
|
||||||
|
console,
|
||||||
|
TextEncoder,
|
||||||
|
TextDecoder,
|
||||||
|
ArrayBuffer,
|
||||||
|
DataView,
|
||||||
|
Uint8Array,
|
||||||
|
Uint8ClampedArray,
|
||||||
|
Uint16Array,
|
||||||
|
Int16Array,
|
||||||
|
Int32Array,
|
||||||
|
Uint32Array,
|
||||||
|
Float32Array,
|
||||||
|
Float64Array,
|
||||||
|
setTimeout,
|
||||||
|
clearTimeout,
|
||||||
|
});
|
||||||
|
self.onmessage({ data: { id: 1, buffer: fixture() } });
|
||||||
|
assert.equal(replies[0]?.error, undefined);
|
||||||
|
assert.equal(replies[0]?.frames, 2);
|
||||||
|
assert.equal(replies[0]?.cols, 2);
|
||||||
|
assert.equal(replies[0]?.rows, 2);
|
||||||
|
assert.equal(replies[0]?.rgba[0], 0);
|
||||||
|
assert.equal(replies[0]?.rgba[12], 255);
|
||||||
|
self.onmessage({ data: { id: 2, frame: 1 } });
|
||||||
|
assert.equal(replies[1]?.error, undefined);
|
||||||
|
assert.equal(replies[1]?.rgba[0], 255);
|
||||||
|
console.log("Production DICOM worker: decode and frame switching passed");
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import type { Plugin } from "vite";
|
||||||
|
|
||||||
|
export function daikonCommonJS(): Plugin {
|
||||||
|
return {
|
||||||
|
name: "daikon-commonjs",
|
||||||
|
enforce: "pre",
|
||||||
|
transform(code, id) {
|
||||||
|
if (!/\/daikon\/(src|lib)\/.*\.js$/.test(id.replaceAll("\\", "/")))
|
||||||
|
return;
|
||||||
|
// Daikon guards static CommonJS imports with a runtime require check.
|
||||||
|
// Bundlers replace the calls, but leave the check false in browsers.
|
||||||
|
return code.replace(/typeof require\s*!==\s*['"]undefined['"]/g, "true");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,44 +1,7 @@
|
|||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { storedValue, windowPixel } from "../dicom-pixels";
|
import { storedValue, windowPixel } from "../dicom-pixels";
|
||||||
|
|
||||||
function fixture() {
|
import { fixture } from "./fixtures/dicom";
|
||||||
const data: number[] = [...new Uint8Array(128), 68, 73, 67, 77];
|
|
||||||
const add = (group: number, element: number, vr: string, bytes: number[]) => {
|
|
||||||
if (bytes.length % 2) bytes.push(vr === "UI" ? 0 : 32);
|
|
||||||
data.push(
|
|
||||||
group & 255,
|
|
||||||
group >> 8,
|
|
||||||
element & 255,
|
|
||||||
element >> 8,
|
|
||||||
vr.charCodeAt(0),
|
|
||||||
vr.charCodeAt(1)
|
|
||||||
);
|
|
||||||
if (["OB", "OW"].includes(vr))
|
|
||||||
data.push(0, 0, bytes.length & 255, bytes.length >> 8, 0, 0);
|
|
||||||
else data.push(bytes.length & 255, bytes.length >> 8);
|
|
||||||
data.push(...bytes);
|
|
||||||
};
|
|
||||||
const text = (s: string) => Array.from(new TextEncoder().encode(s));
|
|
||||||
add(2, 0x10, "UI", text("1.2.840.10008.1.2.1"));
|
|
||||||
add(0x28, 2, "US", [1, 0]);
|
|
||||||
add(0x28, 4, "CS", text("MONOCHROME2"));
|
|
||||||
add(0x28, 8, "IS", text("2"));
|
|
||||||
add(0x28, 0x10, "US", [2, 0]);
|
|
||||||
add(0x28, 0x11, "US", [2, 0]);
|
|
||||||
add(0x28, 0x100, "US", [16, 0]);
|
|
||||||
add(0x28, 0x101, "US", [12, 0]);
|
|
||||||
add(0x28, 0x102, "US", [11, 0]);
|
|
||||||
add(0x28, 0x103, "US", [1, 0]);
|
|
||||||
add(0x28, 0x1050, "DS", text("0"));
|
|
||||||
add(0x28, 0x1051, "DS", text("4096"));
|
|
||||||
add(
|
|
||||||
0x7fe0,
|
|
||||||
0x10,
|
|
||||||
"OW",
|
|
||||||
[0, 8, 255, 15, 0, 0, 255, 7, 255, 7, 0, 0, 255, 15, 0, 8]
|
|
||||||
);
|
|
||||||
return new Uint8Array(data).buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("DICOM rendering", () => {
|
describe("DICOM rendering", () => {
|
||||||
it("preserves signed 12-bit CT values", () => {
|
it("preserves signed 12-bit CT values", () => {
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
export function fixture() {
|
||||||
|
const data: number[] = [...new Uint8Array(128), 68, 73, 67, 77];
|
||||||
|
const add = (group: number, element: number, vr: string, bytes: number[]) => {
|
||||||
|
if (bytes.length % 2) bytes.push(vr === "UI" ? 0 : 32);
|
||||||
|
data.push(
|
||||||
|
group & 255,
|
||||||
|
group >> 8,
|
||||||
|
element & 255,
|
||||||
|
element >> 8,
|
||||||
|
vr.charCodeAt(0),
|
||||||
|
vr.charCodeAt(1)
|
||||||
|
);
|
||||||
|
if (["OB", "OW"].includes(vr))
|
||||||
|
data.push(0, 0, bytes.length & 255, bytes.length >> 8, 0, 0);
|
||||||
|
else data.push(bytes.length & 255, bytes.length >> 8);
|
||||||
|
data.push(...bytes);
|
||||||
|
};
|
||||||
|
const text = (s: string) => Array.from(new TextEncoder().encode(s));
|
||||||
|
add(2, 0x10, "UI", text("1.2.840.10008.1.2.1"));
|
||||||
|
add(0x28, 2, "US", [1, 0]);
|
||||||
|
add(0x28, 4, "CS", text("MONOCHROME2"));
|
||||||
|
add(0x28, 8, "IS", text("2"));
|
||||||
|
add(0x28, 0x10, "US", [2, 0]);
|
||||||
|
add(0x28, 0x11, "US", [2, 0]);
|
||||||
|
add(0x28, 0x100, "US", [16, 0]);
|
||||||
|
add(0x28, 0x101, "US", [12, 0]);
|
||||||
|
add(0x28, 0x102, "US", [11, 0]);
|
||||||
|
add(0x28, 0x103, "US", [1, 0]);
|
||||||
|
add(0x28, 0x1050, "DS", text("0"));
|
||||||
|
add(0x28, 0x1051, "DS", text("4096"));
|
||||||
|
add(
|
||||||
|
0x7fe0,
|
||||||
|
0x10,
|
||||||
|
"OW",
|
||||||
|
[0, 8, 255, 15, 0, 0, 255, 7, 255, 7, 0, 0, 255, 15, 0, 8]
|
||||||
|
);
|
||||||
|
return new Uint8Array(data).buffer;
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ import vue from "@vitejs/plugin-vue";
|
|||||||
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
|
import VueI18nPlugin from "@intlify/unplugin-vue-i18n/vite";
|
||||||
import legacy from "@vitejs/plugin-legacy";
|
import legacy from "@vitejs/plugin-legacy";
|
||||||
import { compression } from "vite-plugin-compression2";
|
import { compression } from "vite-plugin-compression2";
|
||||||
|
import { daikonCommonJS } from "./scripts/daikon-commonjs.ts";
|
||||||
|
|
||||||
function bundledAceAssets(): Plugin {
|
function bundledAceAssets(): Plugin {
|
||||||
return {
|
return {
|
||||||
@@ -65,6 +66,7 @@ function bundledPdfAssets(): Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const plugins = [
|
const plugins = [
|
||||||
|
daikonCommonJS(),
|
||||||
bundledPdfAssets(),
|
bundledPdfAssets(),
|
||||||
bundledAceAssets(),
|
bundledAceAssets(),
|
||||||
vue(),
|
vue(),
|
||||||
@@ -91,6 +93,7 @@ export default defineConfig(({ command }) => {
|
|||||||
return {
|
return {
|
||||||
plugins,
|
plugins,
|
||||||
resolve,
|
resolve,
|
||||||
|
worker: { plugins: () => [daikonCommonJS()] },
|
||||||
server: {
|
server: {
|
||||||
proxy: {
|
proxy: {
|
||||||
"/api/command": {
|
"/api/command": {
|
||||||
@@ -106,6 +109,7 @@ export default defineConfig(({ command }) => {
|
|||||||
return {
|
return {
|
||||||
plugins,
|
plugins,
|
||||||
resolve,
|
resolve,
|
||||||
|
worker: { plugins: () => [daikonCommonJS()] },
|
||||||
base: "",
|
base: "",
|
||||||
build: {
|
build: {
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
param([string]$Version = '3.0.0-sso.7')
|
param([string]$Version = '3.0.0-sso.8')
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
$repo = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
$repo = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
||||||
Push-Location $repo
|
Push-Location $repo
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -eu
|
set -eu
|
||||||
cd "$(dirname "$0")/.."
|
cd "$(dirname "$0")/.."
|
||||||
VERSION="${VERSION:-3.0.0-sso.7}"
|
VERSION="${VERSION:-3.0.0-sso.8}"
|
||||||
COMMIT=$(git rev-parse --short HEAD)
|
COMMIT=$(git rev-parse --short HEAD)
|
||||||
(cd frontend && pnpm install --frozen-lockfile && pnpm run build)
|
(cd frontend && pnpm install --frozen-lockfile && pnpm run build)
|
||||||
mkdir -p build/release/linux-amd64
|
mkdir -p build/release/linux-amd64
|
||||||
|
|||||||
Reference in new issue
Block a user