LogoPear Docs
ReferencesBareModules

bare-os

Operating system utilities for Bare

stable

bare-os exposes operating-system information and process utilities for Bare, closely mirroring the Node.js os module so most Node code ports directly.

Mirrors the Node.js os module.

npm i bare-os

Usage

const os = require('bare-os')

console.log(os.platform()) // 'darwin', 'linux', 'win32', ...
console.log(os.arch()) // 'arm64', 'x64', ...
console.log(os.homedir())
console.log(os.tmpdir())
console.log(os.hostname())
console.log(os.networkInterfaces())

API

System and platform

platform(): 'android' | 'darwin' | 'ios' | 'linux' | 'win32'

Source

Returns the operating system platform as a string. Possible values include 'android', 'darwin', 'ios', 'linux', and 'win32'.

arch(): 'arm' | 'arm64' | 'ia32' | 'x64'

Source

Returns the CPU architecture as a string. Possible values include 'arm', 'arm64', 'ia32', and 'x64'.

type(): string

Source

Returns the operating system name as returned by uname(3).

version(): string

Source

Returns the operating system version.

release(): string

Source

Returns the operating system release.

machine(): string

Source

Returns the machine type as a string.

endianness(): 'LE' | 'BE'

Source

Returns 'LE' on little-endian systems and 'BE' on big-endian systems.

availableParallelism(): number

Source

Returns the number of logical CPU cores available to the process.

Process and scheduling

pid(): number

Source

Returns the process ID.

ppid(): number

Source

Returns the parent process ID.

cwd(): string

Source

Returns the current working directory.

chdir(dir: string): void

Source

Changes the current working directory to dir.

Parameters

ParameterTypeDefaultDescription
dirstringPath of the directory to make the new working directory.

Throws

  • Thrown with the underlying system error code (for example ENOENT) when dir does not exist or cannot be entered.

execPath(): string

Source

Returns the absolute path of the executable that started the process.

kill(pid: number, signal?: string | number): void

Source

Sends signal to the process identified by pid. signal can be a string or a number. Defaults to 'SIGTERM'.

Parameters

ParameterTypeDefaultDescription
pidnumberProcess id to signal.
signal?string | numberSignal name or number to send (default 'SIGTERM'); 0 probes for the process's existence without sending a signal.

Throws

  • UNKNOWN_SIGNAL — Thrown as an OSError when signal is a string that is not a recognized signal name.
  • Thrown with the underlying system error code (for example ESRCH) when pid does not identify a running process.

getProcessTitle(): string

Source

Returns the current process title.

setProcessTitle(title: unknown): void

Source

Sets the process title. title is coerced to a string and must be shorter than 256 characters.

Parameters

ParameterTypeDefaultDescription
titleunknownNew process title; coerced to a string if not already one.

Throws

  • TITLE_OVERFLOW — Thrown as an OSError when the process title is 256 characters or longer.

getPriority(pid?: number): number

Source

Returns the scheduling priority of the process specified by pid. Defaults to 0, meaning the current process.

Parameters

ParameterTypeDefaultDescription
pid?numberProcess id to query; defaults to 0 (the current process).

setPriority(priority: number): void

Source

Sets the scheduling priority of the process specified by pid. If pid is omitted, the priority of the current process is set.

Overloads:

setPriority(priority: number): void
setPriority(pid: number, priority: number): void

Parameters

ParameterTypeDefaultDescription
prioritynumberNice value to set.

Users and network

userInfo(uid?: number): UserInfo

Source

Returns information about a current user. The uid value defaults to the current effective uid.

Parameters

ParameterTypeDefaultDescription
uid?numberUser ID to look up; defaults to the current effective uid.

groupInfo(gid?: number): GroupInfo | null

Source

Returns information about a group. The gid value defaults to the effective group ID of the calling process.

Parameters

ParameterTypeDefaultDescription
gid?numberGroup ID to look up; defaults to the effective group ID of the calling process.

Returns GroupInfo | nullnull on platforms that do not support group lookups (for example, Windows).

hostname(): string

Source

Returns the hostname of the operating system.

networkInterfaces(): Record<string, NetworkInterface[]>

Source

Returns an object containing network interfaces that have been assigned a network address. Each key on the returned object identifies a network interface.

Memory and CPU

cpus

cpus(): {
  model: string
  speed: number
  times: {
    user: number
    nice: number
    sys: number
    idle: number
    irq: number
  }
}[]
Source

Returns an array of objects describing each logical CPU core.

cpuUsage(previous?: CpuUsage): CpuUsage

Source

Returns an object with user and system properties, each representing CPU time in microseconds. If previous is provided, the returned values are relative to it.

Parameters

ParameterTypeDefaultDescription
previous?CpuUsageA previous CpuUsage snapshot to compute a relative diff against.

threadCpuUsage(previous?: CpuUsage): CpuUsage

Source

Like os.cpuUsage() but for the current thread only.

Parameters

ParameterTypeDefaultDescription
previous?CpuUsageA previous CpuUsage snapshot (from threadCpuUsage()) to compute a relative diff against.

resourceUsage

resourceUsage(): {
  userCPUTime: number
  systemCPUTime: number
  maxRSS: number
  sharedMemorySize: number
  unsharedDataSize: number
  unsharedStackSize: number
  minorPageFault: number
  majorPageFault: number
  swappedOut: number
  fsRead: number
  fsWrite: number
  ipcSent: number
  ipcReceived: number
  signalsCount: number
  voluntaryContextSwitches: number
  involuntaryContextSwitches: number
}
Source

Returns an object describing the resource usage of the current process.

memoryUsage

memoryUsage(): {
  rss: number
  heapTotal: number
  heapUsed: number
  external: number
}
Source

Returns an object describing the memory usage of the process.

freemem(): number

Source

Returns the amount of free system memory in bytes.

totalmem(): number

Source

Returns the total amount of system memory in bytes.

availableMemory(): number

Source

Returns an estimate of the amount of memory available for the process in bytes.

constrainedMemory(): number

Source

Returns the amount of memory available to the process under resource constraints, such as cgroups.

uptime(): number

Source

Returns the system uptime in seconds.

loadavg(): ArrayLike<number>

Source

Returns an array containing the 1, 5, and 15 minute load averages.

Directories

tmpdir(): string

Source

Returns the operating system's default directory for temporary files.

homedir(): string

Source

Returns the home directory of the current user.

Environment variables

getEnvKeys(): string[]

Source

Returns an array of the names of all environment variables.

getEnv(name: string): string | undefined

Source

Returns the value of the environment variable name, or undefined if it is not set.

Parameters

ParameterTypeDefaultDescription
namestringName of the environment variable to read.

hasEnv(name: string): boolean

Source

Returns true if the environment variable name is set, otherwise false.

Parameters

ParameterTypeDefaultDescription
namestringName of the environment variable to check.

setEnv(name: string, value: string): void

Source

Sets the environment variable name to value.

Parameters

ParameterTypeDefaultDescription
namestringName of the environment variable to set.
valuestringValue to assign to the environment variable.

unsetEnv(name: string): void

Source

Removes the environment variable name.

Parameters

ParameterTypeDefaultDescription
namestringName of the environment variable to remove.

Constants

EOL: '\r\n' | '\n'

Source

The platform-specific end-of-line marker: '\r\n' on Windows, '\n' everywhere else.

devNull: '\\\\.\\nul' | '/dev/null'

Source

The platform-specific path to the null device: '\\.\nul' on Windows, '/dev/null' everywhere else.

constants

constants: {
  signals: Record<string, number>
  errnos: Record<string, number>
  priority: Record<string, number>
}
Source

An object of signal, error-number, and process-priority constants.

errors

Source
class errors {
  code: string
}

Types

NetworkInterface

interface NetworkInterface {
  address: string
  netmask: string
  family: 'IPv4' | 'IPv6'
  cidr: string
  mac: string
  internal: boolean
  scopeid?: number
}
Source

UserInfo

interface UserInfo {
  uid: number
  gid: number
  username: string
  homedir: string
  shell: string | null
}
Source

GroupInfo

interface GroupInfo {
  groupname: string
  gid: number
  members: string[]
}
Source

CpuUsage

interface CpuUsage {
  user: number
  system: number
}
Source

bare-os/constants

Constants and variables

constants.constants

constants: {
  signals: Record<string, number>
  errnos: Record<string, number>
  priority: Record<string, number>
}
Source

An object of signal, error-number, and process-priority constants.

bare-os/errors

Classes

OSError

Source
class OSError {
  code: string
}

See also

On this page