Debian Codegraph @

node/sync-child-process/dist/lib/worker.js

raw
1 "use strict";
2 // Copyright 2021 Google LLC. Use of this source code is governed by an
3 // MIT-style license that can be found in the LICENSE file or at
4 // https://opensource.org/licenses/MIT.
5 Object.defineProperty(exports, "__esModule", { value: true });
6 const worker_threads_1 = require("worker_threads");
7 const child_process_1 = require("child_process");
8 const assert_1 = require("assert");
9 const sync_message_port_1 = require("sync-message-port");
10 const port = new sync_message_port_1.SyncMessagePort(worker_threads_1.workerData.port);
11 /** A more type-safe way to call `port.postMesage()` */
12 function emit(event, transferList) {
13 port.postMessage(event, transferList);
14 }
15 const process = (0, child_process_1.spawn)(worker_threads_1.workerData.command, worker_threads_1.workerData.args, worker_threads_1.workerData.options);
16 port.on('message', message => {
17 if (message.type === 'stdin') {
18 process.stdin.write(message.data);
19 }
20 else if (message.type === 'stdinClosed') {
21 process.stdin.end();
22 }
23 else {
24 assert_1.strict.equal(message.type, 'kill');
25 process.kill(message.signal);
26 }
27 });
28 process.stdout.on('data', data => {
29 emit({ type: 'stdout', data }, [data.buffer]);
30 });
31 process.stderr.on('data', data => {
32 emit({ type: 'stderr', data }, [data.buffer]);
33 });
34 process.on('error', error => {
35 emit({ type: 'error', error });
36 process.kill();
37 worker_threads_1.parentPort.close();
38 port.close();
39 });
40 process.on('exit', (code, signal) => {
41 if (code !== null) {
42 emit({ type: 'exit', code });
43 }
44 else {
45 (0, assert_1.strict)(signal);
46 emit({ type: 'exit', signal });
47 }
48 worker_threads_1.parentPort.close();
49 port.close();
50 });
51 //# sourceMappingURL=worker.js.map