File
Blob: src/node/os.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | // Exceedingly minimal implementation of the `node:os` module for Workers. |
| 6 | // Generally all stubs that return empty or default values since we don't |
| 7 | // ever expose any actual OS information to the user. |
| 8 | |
| 9 | import { validateNumber, validateObject } from 'node-internal:validators'; |
| 10 | import { Buffer } from 'node-internal:internal_buffer'; |
| 11 | import { ERR_INVALID_ARG_VALUE } from 'node-internal:internal_errors'; |
| 12 | import type { ValidEncoding } from 'node-internal:internal_fs_utils'; |
| 13 | import type { UserInfo, CpuInfo, NetworkInterfaceInfo } from 'node:os'; |
| 14 | |
| 15 | // We always assume POSIX in the workers environment |
| 16 | export const EOL = '\n'; |
| 17 | export const devNull = '/dev/null'; |
| 18 | |
| 19 | export function availableParallelism(): number { |
| 20 | return 1; |
| 21 | } |
| 22 | |
| 23 | // While Workers does support arm in production, that fact is not exposed |
| 24 | // to the user. Let's just always report x64. |
| 25 | export function arch(): string { |
| 26 | return 'x64'; |
| 27 | } |
| 28 | |
| 29 | // We do not expose CPU information to Workers. |
| 30 | export function cpus(): CpuInfo[] { |
| 31 | return []; |
| 32 | } |
| 33 | |
| 34 | export function endianness(): 'LE' | 'BE' { |
| 35 | return 'LE'; |
| 36 | } |
| 37 | |
| 38 | export function freemem(): number { |
| 39 | // We do not currently expose memory information to Workers. We might |
| 40 | // be able to in the future. |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | export function getPriority(pid?: number): number { |
| 45 | // Workers does not support process priority. |
| 46 | if (pid !== undefined) validateNumber(pid, 'pid'); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | export function homedir(): string { |
| 51 | // Workers really does not have a home directory. Return /tmp |
| 52 | // in preparation for the availability of node:fs in the future. |
| 53 | return '/tmp/'; |
| 54 | } |
| 55 | |
| 56 | export function hostname(): string { |
| 57 | // Workers does not have a hostname. Return 'localhost' for compatibility. |
| 58 | return 'localhost'; |
| 59 | } |
| 60 | |
| 61 | export function loadavg(): number[] { |
| 62 | // Workers does not expose load average information. |
| 63 | return [0, 0, 0]; |
| 64 | } |
| 65 | |
| 66 | export function machine(): string { |
| 67 | // Workers does not expose the machine architecture. |
| 68 | return 'x86_64'; |
| 69 | } |
| 70 | |
| 71 | export function networkInterfaces(): NetworkInterfaceInfo[] { |
| 72 | // Workers does not expose network interfaces. |
| 73 | // @ts-expect-error TS2740 We don't export properties here. |
| 74 | return {}; |
| 75 | } |
| 76 | |
| 77 | export function platform(): NodeJS.Platform { |
| 78 | // Workers only supports POSIX platforms. |
| 79 | return 'linux'; |
| 80 | } |
| 81 | |
| 82 | export function release(): string { |
| 83 | // Workers does not expose the OS release information. |
| 84 | return ''; |
| 85 | } |
| 86 | |
| 87 | export function setPriority(pid: number, priority: number): void { |
| 88 | // Workers does not support process priority. |
| 89 | // Just ignore. |
| 90 | validateNumber(pid, 'pid'); |
| 91 | validateNumber(priority, 'priority'); |
| 92 | } |
| 93 | |
| 94 | export function tmpdir(): string { |
| 95 | return '/tmp/'; |
| 96 | } |
| 97 | |
| 98 | export function totalmem(): number { |
| 99 | // We do not currently expose memory information to Workers. We might |
| 100 | // be able to in the future. |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | export function type(): string { |
| 105 | return 'Linux'; |
| 106 | } |
| 107 | |
| 108 | export function uptime(): number { |
| 109 | // For now, we do not report uptime in Workers as it is not clear |
| 110 | // exactly what it should be. We might be able to in the future |
| 111 | // but we'll need to define what it means first. |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | export function userInfo( |
| 116 | options: { encoding?: ValidEncoding | undefined } = {} |
| 117 | ): UserInfo<unknown> { |
| 118 | // We really do not have user information to share. |
| 119 | validateObject(options, 'options'); |
| 120 | const { encoding = null } = options; |
| 121 | if ( |
| 122 | encoding !== null && |
| 123 | encoding !== 'buffer' && |
| 124 | !Buffer.isEncoding(encoding) |
| 125 | ) { |
| 126 | throw new ERR_INVALID_ARG_VALUE( |
| 127 | 'options.encoding', |
| 128 | encoding, |
| 129 | 'must be a valid encoding' |
| 130 | ); |
| 131 | } |
| 132 | // @ts-expect-error TS2739 We don't export properties here. |
| 133 | return {}; |
| 134 | } |
| 135 | |
| 136 | export function version(): string { |
| 137 | // Workers does not expose the OS version. |
| 138 | return ''; |
| 139 | } |
| 140 | |
| 141 | export const constants = { |
| 142 | __proto__: null, |
| 143 | UV_UDP_REUSEADDR: 4, |
| 144 | dlopen: { |
| 145 | __proto__: null, |
| 146 | RTLD_LAZY: 1, |
| 147 | RTLD_NOW: 2, |
| 148 | RTLD_GLOBAL: 256, |
| 149 | RTLD_LOCAL: 0, |
| 150 | RTLD_DEEPBIND: 8, |
| 151 | }, |
| 152 | errno: { |
| 153 | __proto__: null, |
| 154 | E2BIG: 7, |
| 155 | EACCES: 13, |
| 156 | EADDRINUSE: 98, |
| 157 | EADDRNOTAVAIL: 99, |
| 158 | EAFNOSUPPORT: 97, |
| 159 | EAGAIN: 11, |
| 160 | EALREADY: 114, |
| 161 | EBADF: 9, |
| 162 | EBADMSG: 74, |
| 163 | EBUSY: 16, |
| 164 | ECANCELED: 125, |
| 165 | ECHILD: 10, |
| 166 | ECONNABORTED: 103, |
| 167 | ECONNREFUSED: 111, |
| 168 | ECONNRESET: 104, |
| 169 | EDEADLK: 35, |
| 170 | EDESTADDRREQ: 89, |
| 171 | EDOM: 33, |
| 172 | EDQUOT: 122, |
| 173 | EEXIST: 17, |
| 174 | EFAULT: 14, |
| 175 | EFBIG: 27, |
| 176 | EHOSTUNREACH: 113, |
| 177 | EIDRM: 43, |
| 178 | EILSEQ: 84, |
| 179 | EINPROGRESS: 115, |
| 180 | EINTR: 4, |
| 181 | EINVAL: 22, |
| 182 | EIO: 5, |
| 183 | EISCONN: 106, |
| 184 | EISDIR: 21, |
| 185 | ELOOP: 40, |
| 186 | EMFILE: 24, |
| 187 | EMLINK: 31, |
| 188 | EMSGSIZE: 90, |
| 189 | EMULTIHOP: 72, |
| 190 | ENAMETOOLONG: 36, |
| 191 | ENETDOWN: 100, |
| 192 | ENETRESET: 102, |
| 193 | ENETUNREACH: 101, |
| 194 | ENFILE: 23, |
| 195 | ENOBUFS: 105, |
| 196 | ENODATA: 61, |
| 197 | ENODEV: 19, |
| 198 | ENOENT: 2, |
| 199 | ENOEXEC: 8, |
| 200 | ENOLCK: 37, |
| 201 | ENOLINK: 67, |
| 202 | ENOMEM: 12, |
| 203 | ENOMSG: 42, |
| 204 | ENOPROTOOPT: 92, |
| 205 | ENOSPC: 28, |
| 206 | ENOSR: 63, |
| 207 | ENOSTR: 60, |
| 208 | ENOSYS: 38, |
| 209 | ENOTCONN: 107, |
| 210 | ENOTDIR: 20, |
| 211 | ENOTEMPTY: 39, |
| 212 | ENOTSOCK: 88, |
| 213 | ENOTSUP: 95, |
| 214 | ENOTTY: 25, |
| 215 | ENXIO: 6, |
| 216 | EOPNOTSUPP: 95, |
| 217 | EOVERFLOW: 75, |
| 218 | EPERM: 1, |
| 219 | EPIPE: 32, |
| 220 | EPROTO: 71, |
| 221 | EPROTONOSUPPORT: 93, |
| 222 | EPROTOTYPE: 91, |
| 223 | ERANGE: 34, |
| 224 | EROFS: 30, |
| 225 | ESPIPE: 29, |
| 226 | ESRCH: 3, |
| 227 | ESTALE: 116, |
| 228 | ETIME: 62, |
| 229 | ETIMEDOUT: 110, |
| 230 | ETXTBSY: 26, |
| 231 | EWOULDBLOCK: 11, |
| 232 | EXDEV: 18, |
| 233 | }, |
| 234 | signals: { |
| 235 | __proto__: null, |
| 236 | SIGHUP: 1, |
| 237 | SIGINT: 2, |
| 238 | SIGQUIT: 3, |
| 239 | SIGILL: 4, |
| 240 | SIGTRAP: 5, |
| 241 | SIGABRT: 6, |
| 242 | SIGIOT: 6, |
| 243 | SIGBUS: 7, |
| 244 | SIGFPE: 8, |
| 245 | SIGKILL: 9, |
| 246 | SIGUSR1: 10, |
| 247 | SIGSEGV: 11, |
| 248 | SIGUSR2: 12, |
| 249 | SIGPIPE: 13, |
| 250 | SIGALRM: 14, |
| 251 | SIGTERM: 15, |
| 252 | SIGCHLD: 17, |
| 253 | SIGSTKFLT: 16, |
| 254 | SIGCONT: 18, |
| 255 | SIGSTOP: 19, |
| 256 | SIGTSTP: 20, |
| 257 | SIGTTIN: 21, |
| 258 | SIGTTOU: 22, |
| 259 | SIGURG: 23, |
| 260 | SIGXCPU: 24, |
| 261 | SIGXFSZ: 25, |
| 262 | SIGVTALRM: 26, |
| 263 | SIGPROF: 27, |
| 264 | SIGWINCH: 28, |
| 265 | SIGIO: 29, |
| 266 | SIGPOLL: 29, |
| 267 | SIGPWR: 30, |
| 268 | SIGSYS: 31, |
| 269 | }, |
| 270 | priority: { |
| 271 | __proto__: null, |
| 272 | PRIORITY_LOW: 19, |
| 273 | PRIORITY_BELOW_NORMAL: 10, |
| 274 | PRIORITY_NORMAL: 0, |
| 275 | PRIORITY_ABOVE_NORMAL: -7, |
| 276 | PRIORITY_HIGH: -14, |
| 277 | PRIORITY_HIGHEST: -20, |
| 278 | }, |
| 279 | }; |
| 280 | Object.freeze(constants); |
| 281 | Object.freeze(constants.dlopen); |
| 282 | Object.freeze(constants.errno); |
| 283 | Object.freeze(constants.signals); |
| 284 | Object.freeze(constants.priority); |
| 285 | |
| 286 | export default { |
| 287 | EOL, |
| 288 | devNull, |
| 289 | constants, |
| 290 | availableParallelism, |
| 291 | arch, |
| 292 | cpus, |
| 293 | endianness, |
| 294 | freemem, |
| 295 | getPriority, |
| 296 | homedir, |
| 297 | hostname, |
| 298 | loadavg, |
| 299 | machine, |
| 300 | networkInterfaces, |
| 301 | platform, |
| 302 | release, |
| 303 | setPriority, |
| 304 | tmpdir, |
| 305 | totalmem, |
| 306 | type, |
| 307 | uptime, |
| 308 | userInfo, |
| 309 | version, |
| 310 | }; |