forked from h3js/srvx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-helper.ts
More file actions
23 lines (18 loc) · 780 Bytes
/
worker-helper.ts
File metadata and controls
23 lines (18 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { WorkerOptions } from "node:worker_threads";
import { Worker } from "node:worker_threads";
const workerBootstrap = /* JavaScript */ `
import { createRequire } from "node:module";
import { workerData } from "node:worker_threads";
const filename = "${import.meta.url}";
const require = createRequire(filename);
const { createJiti } = require("jiti");
const jiti = createJiti(workerData.__ts_worker_filename);
jiti.import(workerData.__ts_worker_filename);
`;
export class TypeScriptWorker extends Worker {
constructor(filename: string | URL, options: WorkerOptions = {}) {
options.workerData ??= {};
options.workerData.__ts_worker_filename = filename.toString();
super(new URL(`data:text/javascript,${workerBootstrap}`), options);
}
}