Debian Codegraph @

eslint/babel-eslint-parser/src/client.cjs

raw symbols SCIP
1 const path = require("path");
2
3 const ACTIONS = {
4 GET_VERSION: "GET_VERSION",
5 GET_TYPES_INFO: "GET_TYPES_INFO",
6 GET_VISITOR_KEYS: "GET_VISITOR_KEYS",
7 GET_TOKEN_LABELS: "GET_TOKEN_LABELS",
8 MAYBE_PARSE: "MAYBE_PARSE",
9 MAYBE_PARSE_SYNC: "MAYBE_PARSE_SYNC",
10 };
11
12 class Client {
13 #send;
14
15 constructor(send) {
16 this.#send = send;
17 }
18
19 #vCache;
20 getVersion() {
21 return (this.#vCache ??= this.#send(ACTIONS.GET_VERSION, undefined));
22 }
23
24 #tiCache;
25 getTypesInfo() {
26 return (this.#tiCache ??= this.#send(ACTIONS.GET_TYPES_INFO, undefined));
27 }
28
29 #vkCache;
30 getVisitorKeys() {
31 return (this.#vkCache ??= this.#send(ACTIONS.GET_VISITOR_KEYS, undefined));
32 }
33
34 #tlCache;
35 getTokLabels() {
36 return (this.#tlCache ??= this.#send(ACTIONS.GET_TOKEN_LABELS, undefined));
37 }
38
39 maybeParse(code, options) {
40 return this.#send(ACTIONS.MAYBE_PARSE, { code, options });
41 }
42 }
43
44 // We need to run Babel in a worker for two reasons:
45 // 1. ESLint workers must be CJS files, and this is a problem
46 // since Babel 8+ uses native ESM
47 // 2. ESLint parsers must run synchronously, but many steps
48 // of Babel's config loading (which is done for each file)
49 // can be asynchronous
50 // If ESLint starts supporting async parsers, we can move
51 // everything back to the main thread.
52 exports.WorkerClient = class WorkerClient extends Client {
53 static #worker_threads_cache;
54 static get #worker_threads() {
55 return (WorkerClient.#worker_threads_cache ??= require("worker_threads"));
56 }
57
58 #worker = new WorkerClient.#worker_threads.Worker(
59 path.resolve(__dirname, "../lib/worker/index.cjs"),
60 { env: WorkerClient.#worker_threads.SHARE_ENV },
61 );
62
63 constructor() {
64 super((action, payload) => {
65 // We create a new SharedArrayBuffer every time rather than reusing
66 // the same one, otherwise sometimes its contents get corrupted and
67 // Atomics.wait wakes up too early.
68 // https://github.com/babel/babel/pull/14541
69 const signal = new Int32Array(new SharedArrayBuffer(8));
70
71 const subChannel = new WorkerClient.#worker_threads.MessageChannel();
72
73 this.#worker.postMessage(
74 { signal, port: subChannel.port1, action, payload },
75 [subChannel.port1],
76 );
77
78 Atomics.wait(signal, 0, 0);
79 const { message } = WorkerClient.#worker_threads.receiveMessageOnPort(
80 subChannel.port2,
81 );
82
83 if (message.error) throw Object.assign(message.error, message.errorData);
84 else return message.result;
85 });
86
87 // The worker will never exit by itself. Prevent it from keeping
88 // the main process alive.
89 this.#worker.unref();
90 }
91 };
92
93 if (!USE_ESM) {
94 exports.LocalClient = class LocalClient extends Client {
95 static #handleMessage;
96
97 constructor() {
98 LocalClient.#handleMessage ??= require("./worker/handle-message.cjs");
99
100 super((action, payload) => {
101 return LocalClient.#handleMessage(
102 action === ACTIONS.MAYBE_PARSE ? ACTIONS.MAYBE_PARSE_SYNC : action,
103 payload,
104 );
105 });
106 }
107 };
108 }