|
| 1 | +/** |
| 2 | + * Intrinsic: OS. |
| 3 | + * |
| 4 | + * Provides a shim which offers a `os` module implementation that is compatible with Node.js-style imports. |
| 5 | + */ |
1 | 6 |
|
2 |
| -export {}; |
| 7 | +function intrinsic(): any { |
| 8 | + // @ts-expect-error intrinsic symbol |
| 9 | + const api = __Elide_node_os__; |
| 10 | + if (!api) { |
| 11 | + throw new Error(`The 'os' module failed to load the intrinsic API.`); |
| 12 | + } |
| 13 | + return api || {}; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Resolves operating system constants for the current platform. |
| 18 | + */ |
| 19 | +export const constants = {}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Returns the operating system's end-of-line value. |
| 23 | + */ |
| 24 | +export const EOL = intrinsic().EOL; |
| 25 | + |
| 26 | +/** |
| 27 | + * Returns the operating system's null device path. |
| 28 | + */ |
| 29 | +export const devNull = intrinsic().devNull; |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns the number of logical CPUs available to the current process. |
| 33 | + * |
| 34 | + * @returns The number of logical CPUs available to the current process |
| 35 | + */ |
| 36 | +export function availableParallelism(): number { |
| 37 | + return intrinsic().availableParallelism() as number; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Returns the operating system CPU architecture for which the Elide binary was compiled. |
| 42 | + * |
| 43 | + * Possible values follow the Node API: |
| 44 | + * 'arm', 'arm64', 'ia32', 'loong64', 'mips', 'mipsel', 'ppc', 'ppc64', 'riscv64', 's390', 's390x', and 'x64'. |
| 45 | + * |
| 46 | + * @returns The operating system CPU architecture for which the Elide binary was compiled |
| 47 | + */ |
| 48 | +export function arch(): string { |
| 49 | + return intrinsic().arch() as string; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Describes CPU info as part of a return value from `cpus`. |
| 54 | + */ |
| 55 | +export type CpuInfo = { |
| 56 | + model: string; |
| 57 | + speed: number; |
| 58 | + times: { |
| 59 | + user: number; |
| 60 | + nice: number; |
| 61 | + sys: number; |
| 62 | + idle: number; |
| 63 | + irq: number; |
| 64 | + }; |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Returns an array of objects containing information about each logical CPU core. |
| 69 | + * |
| 70 | + * @returns An array of objects containing information about each logical CPU core |
| 71 | + */ |
| 72 | +export function cpus(): CpuInfo[] { |
| 73 | + return intrinsic().cpus() as CpuInfo[]; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Returns the endianness of the CPU for which the Elide binary was compiled. |
| 78 | + * |
| 79 | + * Possible values are 'BE' for big endian and 'LE' for little endian. |
| 80 | + * |
| 81 | + * @returns The endianness of the CPU for which the Elide binary was compiled |
| 82 | + */ |
| 83 | +export function endianness(): "BE" | "LE" { |
| 84 | + return intrinsic().endianness() as "BE" | "LE"; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Returns the amount of free system memory in bytes. |
| 89 | + * |
| 90 | + * @returns The amount of free system memory in bytes |
| 91 | + */ |
| 92 | +export function freemem(): number { |
| 93 | + return intrinsic().freemem() as number; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * Returns the priority value for the provided process ID, or the current process, if no ID is given. |
| 98 | + * |
| 99 | + * @param pid Process ID to get the priority for |
| 100 | + * @returns The priority value for the provided process ID |
| 101 | + */ |
| 102 | +export function getPriority(pid?: number): number { |
| 103 | + return intrinsic().getPriority(pid) as number; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Returns the home directory of the current user. |
| 108 | + * |
| 109 | + * @returns The home directory of the current user |
| 110 | + */ |
| 111 | +export function homedir(): string { |
| 112 | + return intrinsic().homedir() as string; |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Returns the hostname of the operating system. |
| 117 | + * |
| 118 | + * @returns The hostname of the operating system |
| 119 | + */ |
| 120 | +export function hostname(): string { |
| 121 | + return intrinsic().hostname() as string; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Returns the load average of the system. |
| 126 | + * |
| 127 | + * @returns Load averages |
| 128 | + */ |
| 129 | +export function loadavg(): number[] { |
| 130 | + return intrinsic().loadavg() as number[]; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Returns the machine type. |
| 135 | + * |
| 136 | + * @returns The machine type |
| 137 | + */ |
| 138 | +export function machine(): string { |
| 139 | + return intrinsic().machine() as string; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Object shape of a network interface info payload returned by `networkInterfaces`. |
| 144 | + */ |
| 145 | +export type NetworkInterfaceInfo = { |
| 146 | + address: string; |
| 147 | + netmask: string; |
| 148 | + family: string; |
| 149 | + mac: string; |
| 150 | + internal: boolean; |
| 151 | + cidr: string; |
| 152 | + scopeid?: number; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Returns network interfaces. |
| 157 | + * |
| 158 | + * @returns Network interfaces |
| 159 | + */ |
| 160 | +export function networkInterfaces(): { [key: string]: NetworkInterfaceInfo[] } { |
| 161 | + return intrinsic().networkInterfaces() as { [key: string]: NetworkInterfaceInfo[] }; |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Returns the operating system platform. |
| 166 | + * |
| 167 | + * Possible values are 'aix', 'android', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'. |
| 168 | + * |
| 169 | + * @returns The operating system platform |
| 170 | + */ |
| 171 | +export function platform(): string { |
| 172 | + return intrinsic().platform() as string; |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Returns the operating system as a string. |
| 177 | + * |
| 178 | + * @returns The operating system as a string |
| 179 | + */ |
| 180 | +export function release(): string { |
| 181 | + return intrinsic().release() as string; |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * Attempts to set the priority of the provided process ID, or the current process, if no ID is given. |
| 186 | + * |
| 187 | + * @param pidOrPriority Process ID to set the priority for, or the priority value to set if the value |
| 188 | + * should be set for the current process. Default is `0`. |
| 189 | + * @param priority Priority value to set for the provided process ID |
| 190 | + */ |
| 191 | +export function setPriority(pidOrPriority: number, priority?: number): void { |
| 192 | + intrinsic().setPriority(pidOrPriority, priority); |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * Returns the operating system's default directory for temporary files as a string. |
| 197 | + * |
| 198 | + * @returns The operating system's default directory for temporary files |
| 199 | + */ |
| 200 | +export function tmpdir(): string { |
| 201 | + return intrinsic().tmpdir() as string; |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + * Returns the total amount of system memory in bytes. |
| 206 | + * |
| 207 | + * @returns The total amount of system memory in bytes |
| 208 | + */ |
| 209 | +export function totalmem(): number { |
| 210 | + return intrinsic().totalmem() as number; |
| 211 | +} |
| 212 | + |
| 213 | +/** |
| 214 | + * Returns the operating system type as a string. |
| 215 | + * |
| 216 | + * @returns The operating system type as a string |
| 217 | + */ |
| 218 | +export function type(): string { |
| 219 | + return intrinsic().type() as string; |
| 220 | +} |
| 221 | + |
| 222 | +/** |
| 223 | + * Returns the system uptime in seconds. |
| 224 | + * |
| 225 | + * @returns The system uptime in seconds |
| 226 | + */ |
| 227 | +export function uptime(): number { |
| 228 | + return intrinsic().uptime() as number; |
| 229 | +} |
| 230 | + |
| 231 | +/** |
| 232 | + * Options which can be passed into the `userInfo` method. |
| 233 | + */ |
| 234 | +export type UserInfoOptions = { |
| 235 | + encoding?: BufferEncoding; |
| 236 | +} |
| 237 | + |
| 238 | +/** |
| 239 | + * Object shape of a user info payload returned by `userInfo`. |
| 240 | + */ |
| 241 | +export type UserInfo = { |
| 242 | + username: string; |
| 243 | + uid: number; |
| 244 | + gid: number; |
| 245 | + shell: string; |
| 246 | + homedir: string; |
| 247 | +} |
| 248 | + |
| 249 | +/** |
| 250 | + * Returns information about the currently effective user. |
| 251 | + * |
| 252 | + * @param options Options for retrieving user information |
| 253 | + * @returns Information about the currently effective user |
| 254 | + */ |
| 255 | +export function userInfo(options?: UserInfoOptions): UserInfo { |
| 256 | + return intrinsic().userInfo(options) as UserInfo; |
| 257 | +} |
| 258 | + |
| 259 | +/** |
| 260 | + * Returns the operating system version as a string. |
| 261 | + * |
| 262 | + * @returns The operating system version as a string |
| 263 | + */ |
| 264 | +export function version(): string { |
| 265 | + return intrinsic().version() as string; |
| 266 | +} |
0 commit comments