2017-01-09 21:28:20 +00:00
|
|
|
|
2023-11-29 20:36:58 +00:00
|
|
|
import type { WorkerResult, WorkerMessage, WorkerError, SourceLine } from "../common/workertypes";
|
2021-08-15 15:10:01 +00:00
|
|
|
import { getBasePlatform, getRootBasePlatform } from "../common/util";
|
2023-11-29 20:36:58 +00:00
|
|
|
import { TOOL_PRELOADFS } from "./workertools";
|
|
|
|
import { store, builder, errorResult, getWorkFileAsString } from "./builder";
|
|
|
|
import { emglobal, fsMeta, loadFilesystem } from "./wasmutils";
|
2018-10-05 13:47:15 +00:00
|
|
|
|
2023-11-29 20:36:58 +00:00
|
|
|
declare function importScripts(path: string);
|
2018-10-05 13:47:15 +00:00
|
|
|
declare function postMessage(msg);
|
|
|
|
|
2021-08-12 18:51:10 +00:00
|
|
|
export function setupRequireFunction() {
|
2018-11-21 16:53:33 +00:00
|
|
|
var exports = {};
|
|
|
|
exports['jsdom'] = {
|
2023-11-29 20:36:58 +00:00
|
|
|
JSDOM: function (a, b) {
|
2018-11-21 16:53:33 +00:00
|
|
|
this.window = {};
|
|
|
|
}
|
|
|
|
};
|
2023-11-29 20:36:58 +00:00
|
|
|
emglobal['require'] = (modname: string) => {
|
|
|
|
console.log('require', modname, exports[modname] != null);
|
2018-11-21 16:53:33 +00:00
|
|
|
return exports[modname];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:10:59 +00:00
|
|
|
////////////////////////////
|
|
|
|
|
2021-08-14 16:06:49 +00:00
|
|
|
//const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); // for testing
|
2021-08-12 23:19:39 +00:00
|
|
|
|
2023-11-29 20:36:58 +00:00
|
|
|
async function handleMessage(data: WorkerMessage): Promise<WorkerResult> {
|
2018-06-25 14:43:15 +00:00
|
|
|
// preload file system
|
2018-03-03 02:16:25 +00:00
|
|
|
if (data.preload) {
|
|
|
|
var fs = TOOL_PRELOADFS[data.preload];
|
2020-10-17 19:34:08 +00:00
|
|
|
if (!fs && data.platform)
|
2023-11-29 20:36:58 +00:00
|
|
|
fs = TOOL_PRELOADFS[data.preload + '-' + getBasePlatform(data.platform)];
|
2018-06-20 07:09:37 +00:00
|
|
|
if (!fs && data.platform)
|
2023-11-29 20:36:58 +00:00
|
|
|
fs = TOOL_PRELOADFS[data.preload + '-' + getRootBasePlatform(data.platform)];
|
2018-06-20 07:09:37 +00:00
|
|
|
if (fs && !fsMeta[fs])
|
|
|
|
loadFilesystem(fs);
|
2017-01-23 21:21:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-06-25 21:51:07 +00:00
|
|
|
// clear filesystem? (TODO: buildkey)
|
|
|
|
if (data.reset) {
|
2021-07-30 23:21:50 +00:00
|
|
|
store.reset();
|
2018-06-25 21:51:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-30 23:21:50 +00:00
|
|
|
return builder.handleMessage(data);
|
2018-03-03 02:16:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 20:36:58 +00:00
|
|
|
const ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
|
2018-03-03 02:16:25 +00:00
|
|
|
if (ENVIRONMENT_IS_WORKER) {
|
2021-08-12 23:19:39 +00:00
|
|
|
var lastpromise = null;
|
2023-11-29 20:36:58 +00:00
|
|
|
onmessage = async function (e) {
|
2021-08-12 23:19:39 +00:00
|
|
|
await lastpromise; // wait for previous message to complete
|
|
|
|
lastpromise = handleMessage(e.data);
|
|
|
|
var result = await lastpromise;
|
|
|
|
lastpromise = null;
|
2018-03-03 02:16:25 +00:00
|
|
|
if (result) {
|
2021-08-15 15:10:01 +00:00
|
|
|
try {
|
|
|
|
postMessage(result);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
postMessage(errorResult(`${e}`));
|
|
|
|
}
|
2018-03-03 02:16:25 +00:00
|
|
|
}
|
2017-01-06 00:14:12 +00:00
|
|
|
}
|
2016-12-16 01:21:51 +00:00
|
|
|
}
|