|
1 | 1 | # Child Process
|
2 | 2 |
|
3 |
| -This module tries to replicate the `child_process` module from `NodeJS`. For an exhaustive list of implemented features, refer to the [child_process.d.ts](https://github.com/caido/dependency-llrt/blob/main/types/child_process.d.ts) file. |
| 3 | +Caido plugins offer a `child_process` module similiar to NodeJS's `child_process` module. This allows you to spawn child processes from your code, with some limitations. |
4 | 4 |
|
5 |
| -## Spawning a process |
| 5 | +This page will cover the differences between NodeJS's `child_process` module and Caido's implementation. |
6 | 6 |
|
7 |
| -To spawn a process, you can use the `spawn` function. |
| 7 | +::: info |
| 8 | +For an exhaustive list of implemented features, refer to the [child_process.d.ts](https://github.com/caido/dependency-llrt/blob/main/types/child_process.d.ts) file. |
| 9 | +::: |
8 | 10 |
|
9 |
| -```js |
10 |
| -import { spawn } from "child_process"; |
11 |
| - |
12 |
| -const child = spawn("echo", ["Hello, world!"]); |
13 |
| -``` |
14 |
| - |
15 |
| -### Reading stdout and stderr |
16 |
| - |
17 |
| -To handle the output and error streams of the child process, you can use the `stdout` and `stderr` properties of the child process. |
18 |
| - |
19 |
| -```js |
20 |
| -let output = ""; |
21 |
| -child.stdout.on("data", (data) => { |
22 |
| - output += data.toString(); |
23 |
| -}); |
24 |
| - |
25 |
| -let error = ""; |
26 |
| -child.stderr.on("data", (data) => { |
27 |
| - error += data.toString(); |
28 |
| -}); |
29 |
| -``` |
30 |
| - |
31 |
| -### Waiting for exit |
32 |
| - |
33 |
| -To wait for the child process to close and check the exit code, you can use the `close` event of the child process. |
34 |
| - |
35 |
| -```js |
36 |
| -const exitCode = await new Promise((resolve, reject) => { |
37 |
| - child.on("close", resolve); |
38 |
| -}); |
39 |
| - |
40 |
| -if (exitCode) { |
41 |
| - throw new Error(`subprocess error exit ${exitCode}, ${error}`); |
42 |
| -} |
43 |
| -``` |
44 |
| - |
45 |
| -### Driving a process to completion |
46 |
| - |
47 |
| -Combining the two methods, we can drive any child process to completion and get its output. |
48 |
| - |
49 |
| -```js |
50 |
| -async function driveChild(child) { |
51 |
| - let output = ""; |
52 |
| - child.stdout.on("data", (data) => { |
53 |
| - output += data.toString(); |
54 |
| - }); |
55 |
| - |
56 |
| - let error = ""; |
57 |
| - child.stderr.on("data", (data) => { |
58 |
| - error += data.toString(); |
59 |
| - }); |
60 |
| - |
61 |
| - const exitCode = await new Promise((resolve, reject) => { |
62 |
| - child.on("close", resolve); |
63 |
| - }); |
64 |
| - |
65 |
| - if (exitCode) { |
66 |
| - throw new Error(`subprocess error exit ${exitCode}, ${error}`); |
67 |
| - } |
68 |
| - |
69 |
| - return output; |
70 |
| -} |
71 |
| - |
72 |
| -export async function test() { |
73 |
| - const child = spawn("echo", ["Hello, world!"]); |
74 |
| - const result = await driveChild(child); |
75 |
| - return result; |
76 |
| -} |
77 |
| -``` |
78 |
| - |
79 |
| -## Executing within in a shell |
80 |
| - |
81 |
| -You can use the `shell: true` or `shell: '/shell/path'` options to execute the command in a shell. |
82 |
| - |
83 |
| -```js |
84 |
| -import { spawn } from "child_process"; |
85 |
| - |
86 |
| -export async function test() { |
87 |
| - const child = spawn("echo", ["Hello, world!"], { shell: true }); |
88 |
| - const result = await driveChild(child); |
89 |
| - return result; |
90 |
| -} |
91 |
| -``` |
92 |
| - |
93 |
| -## Limitations |
| 11 | +## Differences |
94 | 12 |
|
95 | 13 | - The function `exec` is not implemented, use `spawn` with the `shell` option instead.
|
96 | 14 | - The method `pipe` is not available to streams (like `stdout` and `stderr`).
|
0 commit comments