bare-subprocess
Native process spawning for JavaScript
bare-subprocess — Native process spawning for JavaScript. It is a native addon and requires Bare >=1.7.0.
npm i bare-subprocessUsage
const { spawn } = require('bare-subprocess')
const subprocess = spawn('echo', ['hello', 'world'], {
stdio: 'inherit'
})
subprocess.on('exit', () => console.log('done'))API
Subprocess
channel: SubprocessChannel
Source
The SubprocessChannel instance backing the IPC channel, or undefined when no channel exists. For serialization: 'binary', this is always undefined.
Subprocess.connected: boolean
Source
true while an IPC channel exists between parent and child.
Subprocess.disconnect(): void
Source
Close the IPC channel. A 'disconnect' event is emitted once the channel is fully closed.
exitCode: number | null
Source
The exit code of the child, or null if the child has not exited or was terminated by a signal.
kill(signum?: number): void
Source
Send a signal to the child. signum may be a signal number or a name (e.g. 'SIGTERM'). Defaults to SIGTERM.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
signum? | number | — | Signal to send, as a signal number or name (e.g. 'SIGTERM'); defaults to SIGTERM. |
Throws
UNKNOWN_SIGNAL— thrown ifsignumis a string that isn't a recognized signal name.
killed: boolean
Source
true if subprocess.kill() has been called, otherwise false.
pid: number
Source
The process ID of the child.
ref(): void
Source
Reference the subprocess and its stdio pipes against the event loop.
Subprocess.send(message: unknown, handle?: unknown, cb?: (err: Error | null) => void): boolean
Source
Send message to the child over the IPC channel. handle may be a bare-pipe Pipe or a bare-tcp Socket to transfer ownership of along with the message. callback is invoked with (err) after the message has been written.
Overloads:
send(message: unknown, handle?: unknown, cb?: (err: Error | null) => void): boolean
send(message: unknown, cb: (err: Error | null) => void): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
message | unknown | — | The value to send to the child over the IPC channel. |
handle? | unknown | — | A bare-pipe Pipe or bare-tcp Socket to transfer to the child along with message. |
cb? | (err: Error | null) => void | — | Called with (err) once message has been written, or with an error if there is no connected IPC channel. |
Returns boolean — false if the subprocess has no IPC channel or it has disconnected (cb, if given, is then invoked asynchronously with an error); otherwise the underlying pipe write result.
signalCode: string | null
Source
The name of the signal the child was terminated with, or null.
spawnargs: string[]
Source
The arguments the child was spawned with.
spawnfile: string
Source
The file that was spawned.
stderr: Pipe | null
Source
Convenience accessor for subprocess.stdio[2].
stdin: Pipe | null
Source
Convenience accessor for subprocess.stdio[0].
stdio: (Pipe | null)[]
Source
An array of bare-pipe instances corresponding to the configured stdio slots. Slots configured as 'inherit', 'ignore', or backed by an inherited fd are null.
stdout: Pipe | null
Source
Convenience accessor for subprocess.stdio[1].
unref(): void
Source
Unreference the subprocess and its stdio pipes against the event loop.
Functions
spawn(file: string, args?: string[] | null, opts?: SpawnOptions): Subprocess
Source
Spawn file as a new subprocess with the given args. Returns a Subprocess instance. args may be null or omitted to spawn with no arguments. If args is omitted, the second argument is treated as options.
Overloads:
spawn(file: string, args?: string[] | null, opts?: SpawnOptions): Subprocess
spawn(file: string, opts?: SpawnOptions): SubprocessSynchronous form: spawnSync(file: string, args?: string[] | null, opts?: SpawnSyncOptions): SpawnSyncResult
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
file | string | — | The executable to spawn; a string path or a file:// URL. |
args? | string[] | null | — | Arguments to pass to file; may be null or omitted to spawn with none. If omitted, the second argument is treated as opts. |
opts? | SpawnOptions | — | Options controlling the environment, stdio, and behavior of the new subprocess; see SpawnOptions. |
Throws
UNKNOWN_SERIALIZATION_MODE— thrown ifopts.serializationis not'json','advanced', or'binary'.IPC_CHANNEL_ALREADY_DEFINED— thrown ifopts.stdiorequests more than one'ipc'slot.
Types
SubprocessChannel
interface SubprocessChannel {
readonly connected: boolean
send(message: unknown, handle?: unknown, cb?: (err: Error | null) => void): boolean
send(message: unknown, cb: (err: Error | null) => void): boolean
disconnect(): void
ref(): this
unref(): this
}SubprocessEvents
interface SubprocessEvents extends EventMap {
exit: [code: number | null, signalCode: string | null]
message: [message: unknown, handle: unknown]
disconnect: []
error: [err: Error]
}IO
type IO = 'inherit' | 'pipe' | 'overlapped' | 'ignore' | 'ipc'SerializationMode
type SerializationMode = 'json' | 'advanced' | 'binary'SpawnOptions
interface SpawnOptions {
cwd?: string
stdio?: [stdin?: IO, stdout?: IO, stderr?: IO, ...fds: IO[]] | IO | null
shell?: boolean | string
detached?: boolean
uid?: number
gid?: number
env?: Record<string, string>
windowsHide?: boolean
windowsVerbatimArguments?: boolean
serialization?: SerializationMode
}SpawnSyncOptions
interface SpawnSyncOptions extends SpawnOptions {
input?: string | Buffer
maxBuffer?: number
}SpawnSyncResult
interface SpawnSyncResult {
output: (Buffer | null)[] | null
pid: number
signal: number
status: number
stdout: Buffer | null
stderr: Buffer | null
error?: Error
}Classes
errors
Source
class errors {
code: string
}bare-subprocess/errors
Classes
SubprocessError
Source
class SubprocessError {
code: string
}bare-subprocess/parent
SubprocessParentChannel
new SubprocessParentChannel()
Source
Throws
NO_IPC_CHANNEL— thrown if theBARE_CHANNEL_FDenvironment variable is not set.UNKNOWN_SERIALIZATION_MODE— thrown ifBARE_CHANNEL_SERIALIZATION_MODEis set to something other than'json'or'advanced'.
SubprocessParentChannel.connected: boolean
Source
true while an IPC channel exists between parent and child.
SubprocessParentChannel.disconnect(): void
Source
Close the IPC channel. A 'disconnect' event is emitted once the channel is fully closed.
ref(): this
Source
Reference the subprocess and its stdio pipes against the event loop.
SubprocessParentChannel.send(message: unknown, handle?: unknown, cb?: (err: Error | null) => void): boolean
Source
Send message to the child over the IPC channel. handle may be a bare-pipe Pipe or a bare-tcp Socket to transfer ownership of along with the message. callback is invoked with (err) after the message has been written.
Overloads:
send(message: unknown, handle?: unknown, cb?: (err: Error | null) => void): boolean
send(message: unknown, cb: (err: Error | null) => void): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
message | unknown | — | The value to send to the parent process over the IPC channel. |
handle? | unknown | — | A bare-pipe Pipe or bare-tcp Socket to transfer to the parent along with message. |
cb? | (err: Error | null) => void | — | Called with (err) once message has been written, or with an error if the channel is disconnected. |
Returns boolean — false if the channel is disconnected (cb, if given, is then invoked asynchronously with a CHANNEL_DISCONNECTED error); otherwise the underlying pipe write result.
unref(): this
Source
Unreference the subprocess and its stdio pipes against the event loop.
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.