File size: 7,483 Bytes
c2b7eb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# bare-os

Operating system utilities for Bare. The API closely follows that of the Node.js `os` module.

```
npm i bare-os
```

## Usage

```js
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

#### `os.constants`

An object containing the following properties:

- `signals` - Signal constants such as `SIGTERM` and `SIGKILL`.
- `errnos` - Error number constants.
- `priority` - Process priority constants.

These are also available as a separate module:

```js
const constants = require('bare-os/constants')
```

#### `os.EOL`

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

#### `os.devNull`

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

#### `const p = os.platform()`

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

#### `const a = os.arch()`

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

#### `const t = os.type()`

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

#### `const v = os.version()`

Returns the operating system version.

#### `const r = os.release()`

Returns the operating system release.

#### `const m = os.machine()`

Returns the machine type as a string.

#### `const p = os.execPath()`

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

#### `const id = os.pid()`

Returns the process ID.

#### `const id = os.ppid()`

Returns the parent process ID.

#### `const dir = os.cwd()`

Returns the current working directory.

#### `os.chdir(dir)`

Changes the current working directory to `dir`.

#### `const dir = os.tmpdir()`

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

#### `const dir = os.homedir()`

Returns the home directory of the current user.

#### `const name = os.hostname()`

Returns the hostname of the operating system.

#### `const interfaces = os.networkInterfaces()`

Returns an object containing network interfaces that have been assigned a network address. Each key on the returned object identifies a network interface. The associated value is an array of objects with the following properties:

- `address` - The assigned IPv4 or IPv6 address.
- `netmask` - The IPv4 or IPv6 network mask.
- `family` - Either `'IPv4'` or `'IPv6'`.
- `cidr` - The assigned IPv4 or IPv6 address with the routing prefix in CIDR notation.
- `mac` - The MAC address of the network interface.
- `internal` - `true` if the network interface is a loopback or similar interface that is not remotely accessible; otherwise `false`.
- `scopeid` - The numeric IPv6 scope ID. Only specified when `family` is `'IPv6'`.

#### `os.kill(pid[, signal])`

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

#### `const info = os.userInfo([uid])`

Returns information about a current user. The `uid` value defaults to the current effective uid. The returned object has the following properties:

- `uid` - The user ID.
- `gid` - The group ID.
- `username` - The username.
- `homedir` - The home directory.
- `shell` - The shell, or `null` if unavailable.

#### `const info = os.groupInfo([gid])`

Returns information about a group. The `gid` value defaults to the effective group ID of the calling process. The returned object has the following properties:

- `groupname` - The group name.
- `gid` - The group ID.
- `members` - List with the names of group members.

#### `const e = os.endianness()`

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

#### `const n = os.availableParallelism()`

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

#### `const usage = os.cpuUsage([previous])`

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.

#### `const usage = os.threadCpuUsage([previous])`

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

#### `const usage = os.resourceUsage()`

Returns an object describing the resource usage of the current process. The returned object has the following properties:

- `userCPUTime` - User CPU time in microseconds.
- `systemCPUTime` - System CPU time in microseconds.
- `maxRSS` - Maximum resident set size in bytes.
- `sharedMemorySize` - Shared memory size.
- `unsharedDataSize` - Unshared data size.
- `unsharedStackSize` - Unshared stack size.
- `minorPageFault` - Minor page faults.
- `majorPageFault` - Major page faults.
- `swappedOut` - Swap count.
- `fsRead` - File system reads.
- `fsWrite` - File system writes.
- `ipcSent` - IPC messages sent.
- `ipcReceived` - IPC messages received.
- `signalsCount` - Signals received.
- `voluntaryContextSwitches` - Voluntary context switches.
- `involuntaryContextSwitches` - Involuntary context switches.

#### `const usage = os.memoryUsage()`

Returns an object describing the memory usage of the process. The returned object has the following properties:

- `rss` - Resident set size in bytes.
- `heapTotal` - Total heap size in bytes.
- `heapUsed` - Used heap size in bytes.
- `external` - Memory usage of C++ objects bound to JavaScript objects.

#### `const bytes = os.freemem()`

Returns the amount of free system memory in bytes.

#### `const bytes = os.totalmem()`

Returns the total amount of system memory in bytes.

#### `const bytes = os.availableMemory()`

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

#### `const bytes = os.constrainedMemory()`

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

#### `const seconds = os.uptime()`

Returns the system uptime in seconds.

#### `const avgs = os.loadavg()`

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

#### `const list = os.cpus()`

Returns an array of objects describing each logical CPU core. Each object has the following properties:

- `model` - The CPU model.
- `speed` - The CPU speed in MHz.
- `times` - An object with `user`, `nice`, `sys`, `idle`, and `irq` CPU time values in milliseconds.

#### `const title = os.getProcessTitle()`

Returns the current process title.

#### `os.setProcessTitle(title)`

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

#### `const priority = os.getPriority([pid])`

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

#### `os.setPriority([pid, ]priority)`

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

#### `const keys = os.getEnvKeys()`

Returns an array of the names of all environment variables.

#### `const value = os.getEnv(name)`

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

#### `const exists = os.hasEnv(name)`

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

#### `os.setEnv(name, value)`

Sets the environment variable `name` to `value`.

#### `os.unsetEnv(name)`

Removes the environment variable `name`.

## License

Apache-2.0