-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathworker.js
More file actions
187 lines (162 loc) · 7.09 KB
/
worker.js
File metadata and controls
187 lines (162 loc) · 7.09 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const fs = require("fs");
const path = require("path");
const esbuild = require("esbuild");
exports.WorkerPlugin = function WorkerPlugin(inline) {
function setup(build) {
build.onResolve({filter: /worker.js$/}, (args) => {
if (args.namespace === "worker-stub") {
const outfile = `build/worker/` + path.basename(args.path);
const subbuild = esbuild.build({
target: ["es2021"],
entryPoints: [require.resolve(args.path)],
outfile,
define: {
global: "self",
},
entryNames: "[name]",
chunkNames: "[name]",
assetNames: "[name]",
minify: !process.env.PSP_DEBUG,
bundle: true,
sourcemap: true,
});
return {
path: args.path,
namespace: "worker",
pluginData: {
outfile,
subbuild,
},
};
}
return {
path: args.path,
namespace: "worker-stub",
};
});
build.onLoad({filter: /.*/, namespace: "worker-stub"}, async (args) => {
if (inline) {
return {
contents: `
import worker from ${JSON.stringify(args.path)};
function make_host(a, b) {
function addEventListener(type, callback) {
a.push(callback);
}
function postMessage(msg) {
if (Object.keys(msg).length > 0) {
for (const listener of b) {
listener({data: msg});
}
}
}
return {
addEventListener,
postMessage,
location: {href: ""}
}
}
function run_single_threaded(code, e) {
console.error("Running perspective in single-threaded mode due to error initializing Web Worker:", e);
let f = Function("const self = arguments[0];" + code);
const workers = [];
const mains = [];
f(make_host(workers, mains));
return make_host(mains, workers);
}
export const initialize = async function () {
if (window.location.protocol.startsWith("file")) {
return run_single_threaded(worker, "file:// protocol does not support Web Workers");
}
try {
const blob = new Blob([worker], {type: 'application/javascript'});
const url = URL.createObjectURL(blob);
return new Worker(url, {type: "module"});
} catch (e) {
return run_single_threaded(worker, e);
}
};
export default initialize;
`,
};
}
return {
contents: `
import worker from ${JSON.stringify(args.path)};
async function get_worker_code() {
const url = new URL(worker, import.meta.url);
const req = await fetch(url);
const code = await req.text();
return code;
};
function make_host(a, b) {
function addEventListener(type, callback) {
a.push(callback);
}
function postMessage(msg) {
if (Object.keys(msg).length > 0) {
for (const listener of b) {
listener({data: msg});
}
}
}
return {
addEventListener,
postMessage,
location: {href: ""}
}
}
function run_single_threaded(code, e) {
console.error("Running perspective in single-threaded mode due to error initializing Web Worker:", e);
let f = Function("const self = arguments[0];" + code);
const workers = [];
const mains = [];
f(make_host(workers, mains));
return make_host(mains, workers);
}
const code_promise = get_worker_code();
export const initialize = async function () {
const code = await code_promise;
if (window.location.protocol.startsWith("file")) {
return run_single_threaded(code, "file:// protocol does not support Web Workers");
}
try {
const blob = new Blob([code], {type: 'application/javascript'});
const url = URL.createObjectURL(blob);
return new Worker(url, {type: "module"});
} catch (e) {
return run_single_threaded(code, e);
}
};
export default initialize;
`,
};
});
build.onLoad({filter: /.*/, namespace: "worker"}, async (args) => {
// Get the subbuild output and delete the temp file
await args.pluginData.subbuild;
contents = await fs.promises.readFile(args.pluginData.outfile);
// Copy the sourcemaps also
const mapfile = args.pluginData.outfile + ".map";
sourcemap = await fs.promises.readFile(mapfile);
const outpath =
build.initialOptions.outdir ||
path.dirname(build.initialOptions.outfile);
if (!fs.existsSync(outpath)) {
fs.mkdirSync(outpath, {recursive: true});
}
await fs.promises.writeFile(
path.join(outpath, path.basename(args.path) + ".map"),
sourcemap
);
return {
contents,
loader: inline ? "text" : "file",
};
});
}
return {
name: "webworker",
setup,
};
};