LogoPear Docs
ReferencesBareModules

bare-subprocess

Native process spawning for JavaScript

stable

bare-subprocess — Native process spawning for JavaScript. It is a native addon and requires Bare >=1.7.0.

npm i bare-subprocess

Usage

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

ParameterTypeDefaultDescription
signum?numberSignal to send, as a signal number or name (e.g. 'SIGTERM'); defaults to SIGTERM.

Throws

  • UNKNOWN_SIGNAL — thrown if signum is 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): boolean

Parameters

ParameterTypeDefaultDescription
messageunknownThe value to send to the child over the IPC channel.
handle?unknownA bare-pipe Pipe or bare-tcp Socket to transfer to the child along with message.
cb?(err: Error | null) => voidCalled with (err) once message has been written, or with an error if there is no connected IPC channel.

Returns booleanfalse 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): Subprocess

Synchronous form: spawnSync(file: string, args?: string[] | null, opts?: SpawnSyncOptions): SpawnSyncResult

Parameters

ParameterTypeDefaultDescription
filestringThe executable to spawn; a string path or a file:// URL.
args?string[] | nullArguments to pass to file; may be null or omitted to spawn with none. If omitted, the second argument is treated as opts.
opts?SpawnOptionsOptions controlling the environment, stdio, and behavior of the new subprocess; see SpawnOptions.

Throws

  • UNKNOWN_SERIALIZATION_MODE — thrown if opts.serialization is not 'json', 'advanced', or 'binary'.
  • IPC_CHANNEL_ALREADY_DEFINED — thrown if opts.stdio requests 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
}
Source

SubprocessEvents

interface SubprocessEvents extends EventMap {
  exit: [code: number | null, signalCode: string | null]
  message: [message: unknown, handle: unknown]
  disconnect: []
  error: [err: Error]
}
Source

IO

type IO = 'inherit' | 'pipe' | 'overlapped' | 'ignore' | 'ipc'
Source

SerializationMode

type SerializationMode = 'json' | 'advanced' | 'binary'
Source

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
}
Source

SpawnSyncOptions

interface SpawnSyncOptions extends SpawnOptions {
  input?: string | Buffer
  maxBuffer?: number
}
Source

SpawnSyncResult

interface SpawnSyncResult {
  output: (Buffer | null)[] | null
  pid: number
  signal: number
  status: number
  stdout: Buffer | null
  stderr: Buffer | null
  error?: Error
}
Source

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 the BARE_CHANNEL_FD environment variable is not set.
  • UNKNOWN_SERIALIZATION_MODE — thrown if BARE_CHANNEL_SERIALIZATION_MODE is 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): boolean

Parameters

ParameterTypeDefaultDescription
messageunknownThe value to send to the parent process over the IPC channel.
handle?unknownA bare-pipe Pipe or bare-tcp Socket to transfer to the parent along with message.
cb?(err: Error | null) => voidCalled with (err) once message has been written, or with an error if the channel is disconnected.

Returns booleanfalse 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

On this page