-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
151 lines (149 loc) · 4.57 KB
/
Copy pathworker.js
File metadata and controls
151 lines (149 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// src/worker.ts
import Module from "./niimath.js";
// src/workerImpl.ts
function outputCandidates(outName) {
return outName.endsWith(".gz") ? [outName] : [outName, `${outName}.gz`];
}
function setupWorker(ModuleFactory) {
let mod = null;
let runLog = [];
const captureModule = { print: (s) => runLog.push(s), printErr: (s) => runLog.push(s) };
ModuleFactory(captureModule).then((initializedMod) => {
mod = initializedMod;
const readyMsg = { type: "ready" };
self.postMessage(readyMsg);
});
self.onerror = function(event) {
const errorMsg = {
type: "error",
message: typeof event === "string" ? event : event.message ?? "Unknown error",
error: event.error?.stack ?? null
};
self.postMessage(errorMsg);
};
self.onunhandledrejection = function(event) {
const errorMsg = {
type: "error",
message: event.reason?.message ?? "Unhandled rejection",
error: event.reason?.stack ?? null
};
self.postMessage(errorMsg);
};
const handleMessage = (e) => {
try {
const file = e.data.blob;
const args = e.data.cmd;
const outName = e.data.outName ?? "out.nii";
if (!file || args.length < 1) {
throw new Error("Expected a file and at least one command");
}
const inName = file.name ?? "input.nii";
const extraFiles = e.data.extraFiles ?? [];
const fr = new FileReader();
fr.onerror = function() {
const errorMsg = {
type: "error",
message: `Failed to read input "${inName}": ${fr.error?.message ?? "unknown read error"}`,
error: fr.error?.stack ?? null
};
self.postMessage(errorMsg);
};
fr.readAsArrayBuffer(file);
fr.onload = async function() {
if (!mod) {
const errorMsg = {
type: "error",
message: "WASM module not loaded yet!",
error: null
};
self.postMessage(errorMsg);
return;
}
const data = new Uint8Array(fr.result);
let stagedInput = false;
const stagedExtras = [];
try {
if (!Array.isArray(args)) {
throw new Error("Expected args to be an array");
}
mod.FS_createDataFile(".", inName, data, true, true);
stagedInput = true;
for (const f of extraFiles) {
const bytes = new Uint8Array(await f.data.arrayBuffer());
mod.FS_createDataFile(".", f.name, bytes, true, true);
stagedExtras.push(f.name);
}
runLog = [];
const exitCode = mod.callMain(args);
if (exitCode !== 0) {
const detail = runLog.join("\n").trim();
throw new Error(`niimath exited with code ${exitCode}${detail ? `:
${detail}` : ""}`);
}
let actualOutName = outName;
let out_bin = null;
for (const candidate of outputCandidates(outName)) {
try {
out_bin = mod.FS_readFile(candidate);
actualOutName = candidate;
break;
} catch {
}
}
if (!out_bin) {
throw new Error(`niimath completed but output "${outName}" was not found`);
}
const exact = new Uint8Array(out_bin.byteLength);
exact.set(out_bin);
const outputFile = new Blob([exact.buffer], { type: "application/sla" });
const successMsg = {
blob: outputFile,
outName: actualOutName,
exitCode
};
self.postMessage(successMsg);
} catch (err) {
const error = err;
const errorMsg = {
type: "error",
message: error.message,
error: error.stack ?? null
};
self.postMessage(errorMsg);
} finally {
if (stagedInput) {
try {
mod.FS_unlink(inName);
} catch {
}
}
for (const name of stagedExtras) {
try {
mod.FS_unlink(name);
} catch {
}
}
for (const name of outputCandidates(outName)) {
if (inName !== name) {
try {
mod.FS_unlink(name);
} catch {
}
}
}
}
};
} catch (err) {
const error = err;
const errorMsg = {
type: "error",
message: error.message,
error: error.stack ?? null
};
self.postMessage(errorMsg);
}
};
self.addEventListener("message", handleMessage, false);
}
// src/worker.ts
setupWorker(Module);