Skip to content

Commit 828a23c

Browse files
committed
Split process spawning concept into guides/concept
1 parent 871dc0a commit 828a23c

File tree

4 files changed

+108
-89
lines changed

4 files changed

+108
-89
lines changed

.vitepress/sidebars/guides.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export const guidesSidebar: DefaultTheme.SidebarItem[] = [
3434
text: "Sending HTTP Requests",
3535
link: "/guides/components/request",
3636
},
37+
{
38+
text: "Spawning a Process",
39+
link: "/guides/components/spawning_process",
40+
},
3741
],
3842
},
3943
{

src/concepts/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Here you will find explanations of the core concepts that underpin Caido plugins
1010

1111
## Modules
1212

13-
- **[Child Process](./modules/child_process.md)** - spawn processes using the `child_process` module.
13+
- [Child Process](./modules/child_process.md) - Caido's implementation of NodeJS's `child_process` module.
Lines changed: 6 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,14 @@
11
# Child Process
22

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.
44

5-
## Spawning a process
5+
This page will cover the differences between NodeJS's `child_process` module and Caido's implementation.
66

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+
:::
810

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
9412

9513
- The function `exec` is not implemented, use `spawn` with the `shell` option instead.
9614
- The method `pipe` is not available to streams (like `stdout` and `stderr`).
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Spawning a process
2+
3+
You might want to spawn a process to interact with external tools or programs.
4+
5+
To do this, you can use the `spawn` function from the `child_process` module.
6+
7+
::: info
8+
This module is similar to NodeJS's `child_process` module, but with some differences. You can find more information in the [child_process](/concepts/modules/child_process.md) documentation.
9+
:::
10+
11+
## Launching a process
12+
13+
The code below spawns a process that echoes "Hello, world!" to the console.
14+
15+
```js
16+
import { spawn } from "child_process";
17+
18+
const child = spawn("echo", ["Hello, world!"]);
19+
```
20+
21+
## Reading STDOUT and STDERR
22+
23+
To handle the output and error streams of the child process, you can use the `stdout` and `stderr` properties of the child process.
24+
25+
```js
26+
let output = "";
27+
child.stdout.on("data", (data) => {
28+
output += data.toString();
29+
});
30+
31+
let error = "";
32+
child.stderr.on("data", (data) => {
33+
error += data.toString();
34+
});
35+
```
36+
37+
## Waiting for exit
38+
39+
To wait for the child process to close and check the exit code, you can use the `close` event of the child process.
40+
41+
```js
42+
const exitCode = await new Promise((resolve, reject) => {
43+
child.on("close", resolve);
44+
});
45+
46+
if (exitCode) {
47+
throw new Error(`subprocess error exit ${exitCode}, ${error}`);
48+
}
49+
```
50+
51+
## Driving a process to completion
52+
53+
Combining the two methods, we can drive any child process to completion and get its output.
54+
55+
```js
56+
async function driveChild(child) {
57+
let output = "";
58+
child.stdout.on("data", (data) => {
59+
output += data.toString();
60+
});
61+
62+
let error = "";
63+
child.stderr.on("data", (data) => {
64+
error += data.toString();
65+
});
66+
67+
const exitCode = await new Promise((resolve, reject) => {
68+
child.on("close", resolve);
69+
});
70+
71+
if (exitCode) {
72+
throw new Error(`subprocess error exit ${exitCode}, ${error}`);
73+
}
74+
75+
return output;
76+
}
77+
78+
export async function test() {
79+
const child = spawn("echo", ["Hello, world!"]);
80+
const result = await driveChild(child);
81+
return result;
82+
}
83+
```
84+
85+
## Executing within in a shell
86+
87+
You can use the `shell: true` or `shell: '/shell/path'` options to execute the command in a shell.
88+
89+
```js
90+
import { spawn } from "child_process";
91+
92+
export async function test() {
93+
const child = spawn("echo", ["Hello, world!"], { shell: true });
94+
const result = await driveChild(child);
95+
return result;
96+
}
97+
```

0 commit comments

Comments
 (0)