Repository navigation
Expand file tree
/
Copy pathworker.ts
More file actions
183 lines (164 loc) · 5.98 KB
/
Copy pathworker.ts
File metadata and controls
183 lines (164 loc) · 5.98 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
import { resolveObjectURL } from 'node:buffer';
import { Worker as NodeWorker } from 'node:worker_threads';
/**
* Web Worker for the terminal build, on a real thread.
*
* The webmail bundles its workers inline (`?worker&inline`), so each one
* arrives as a blob: or data: URL holding a self-contained script. Each runs
* in a Node worker thread behind the bootstrap in ./worker-thread.ts, which
* gives it a WorkerGlobalScope. Database, sync and search work therefore
* stays off the thread that draws the terminal, as it stays off a browser's
* main thread, and nothing has to be located on disk at runtime.
*
* Messages are structured-cloned in both directions, and transferred
* MessagePorts arrive as `event.ports`.
*/
type Listener = (event: unknown) => void;
export interface WorkerOptions {
href: () => string;
log: string | null;
}
let options: WorkerOptions = { href: () => 'https://mail.forwardemail.net/', log: null };
export function configureWorkers(next: WorkerOptions) {
options = next;
}
// Resolved synchronously: Vite's wrapper revokes the blob URL right after
// the constructor returns.
function loadScript(url: string): Promise<string> {
if (url.startsWith('blob:')) {
const blob = resolveObjectURL(url);
if (!blob) return Promise.reject(new Error(`Worker script not found: ${url}`));
return blob.text();
}
if (url.startsWith('data:')) {
const comma = url.indexOf(',');
const meta = url.slice(5, comma);
const body = url.slice(comma + 1);
return Promise.resolve(
meta.endsWith(';base64')
? Buffer.from(body, 'base64').toString('utf8')
: decodeURIComponent(body),
);
}
return Promise.reject(
new Error(`Only inline workers are supported in the terminal (got ${url})`),
);
}
function transferList(transfer?: unknown[] | { transfer?: unknown[] }) {
return (Array.isArray(transfer) ? transfer : (transfer?.transfer ?? [])) as unknown[];
}
export class ThreadWorker {
onmessage: Listener | null = null;
onmessageerror: Listener | null = null;
onerror: Listener | null = null;
#listeners = new Map<string, Set<Listener>>();
#wrapped = new WeakMap<object, Listener>();
#thread: NodeWorker | null = null;
#queue: Array<[unknown, unknown[]]> = [];
#terminated = false;
constructor(url: string | URL, init: { name?: string } = {}) {
const name = init.name ?? '';
loadScript(String(url)).then(
(code) => {
if (this.#terminated) return;
// The bootstrap runs in its own scope: its bundled modules declare
// top-level names (fake-indexeddb's Event) that would otherwise
// shadow the globals of the worker code that follows.
const source = `(function () {\n${__forwardemailWorkerBootstrap}\n})();\n${code}`;
const thread = new NodeWorker(source, {
eval: true,
name,
workerData: {
name,
href: options.href(),
log: options.log,
api: (globalThis as Record<string, unknown>).__FORWARDEMAIL_API_URL__,
},
stdout: true,
stderr: true,
});
thread.stdout.resume();
thread.stderr.resume();
thread.on('message', (message: { data: unknown; ports: MessagePort[] }) => {
this.#dispatch({
type: 'message',
data: message?.data,
ports: message?.ports ?? [],
target: this,
currentTarget: this,
});
});
thread.on('messageerror', (error) => {
this.#dispatch({ type: 'messageerror', data: error, target: this });
});
thread.on('error', (error) => this.#error(error));
thread.unref();
this.#thread = thread;
for (const [data, list] of this.#queue.splice(0)) this.#send(data, list);
},
(error) => this.#error(error),
);
}
postMessage(data: unknown, transfer?: unknown[] | { transfer?: unknown[] }): void {
if (this.#terminated) return;
const list = transferList(transfer);
if (!this.#thread) {
this.#queue.push([data, list]);
return;
}
this.#send(data, list);
}
#send(data: unknown, list: unknown[]) {
const ports = list.filter((item) => item instanceof MessagePort);
try {
this.#thread!.postMessage({ data, ports }, list as never);
} catch (error) {
this.#dispatch({ type: 'messageerror', data: error, target: this });
}
}
terminate(): void {
this.#terminated = true;
this.#queue = [];
void this.#thread?.terminate();
}
addEventListener(type: string, listener: Listener | { handleEvent: Listener } | null): void {
if (!listener) return;
let fn = listener as Listener;
if (typeof listener !== 'function') {
fn = this.#wrapped.get(listener) ?? ((event) => listener.handleEvent(event));
this.#wrapped.set(listener, fn);
}
if (!this.#listeners.has(type)) this.#listeners.set(type, new Set());
this.#listeners.get(type)!.add(fn);
}
removeEventListener(type: string, listener: Listener | { handleEvent: Listener } | null): void {
if (!listener) return;
const fn = typeof listener === 'function' ? listener : this.#wrapped.get(listener);
if (fn) this.#listeners.get(type)?.delete(fn);
}
dispatchEvent(event: { type: string }): boolean {
this.#dispatch(event as { type: string } & Record<string, unknown>);
return true;
}
#dispatch(event: { type: string } & Record<string, unknown>) {
const handler = (this as unknown as Record<string, unknown>)[`on${event.type}`];
if (typeof handler === 'function') handler.call(this, event);
for (const listener of [...(this.#listeners.get(event.type) ?? [])]) {
listener.call(this, event);
}
}
#error(error: unknown) {
const err = error instanceof Error ? error : new Error(String(error));
console.error('[worker]', err);
this.#dispatch({
type: 'error',
message: err.message,
error: err,
filename: '',
lineno: 0,
colno: 0,
target: this,
preventDefault() {},
});
}
}