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
@@ -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");
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user