Current section
Files
Jump to
Current section
Files
src/tinman_ffi.mjs
import * as os from "node:os";
import * as fs from "node:fs";
import { execSync } from "node:child_process";
import { Result$Ok, Result$Error } from "./gleam.mjs";
import {
Endianness$BigEndian,
Endianness$LittleEndian,
parse_version,
} from "./tinman.mjs";
export function platform() {
return process.platform;
}
export function is_windows() {
return process.platform === "win32";
}
export function os_type() {
return os.type();
}
export function arch() {
return os.arch();
}
export function version() {
// Use tinman's parse_version function to parse os.release()
return parse_version(os.release());
}
export function hostname() {
return os.hostname();
}
export function endianness() {
switch (os.endianness()) {
case "BE":
return Endianness$BigEndian();
case "LE":
return Endianness$LittleEndian();
default:
throw new Error("endianess");
}
}
export function total_memory() {
const total = os.totalmem();
if (total > 0) {
return Result$Ok(total);
}
return Result$Error(undefined);
}
export function used_memory() {
return process.memoryUsage.rss();
}
export function free_memory() {
const free = process.constrainedMemory() || process.availableMemory();
if (free > 0) {
return Result$Ok(free);
}
return Result$Error(undefined);
}
export function cpu_count() {
const count = os.cpus().length;
if (count > 0) {
return Result$Ok(count);
}
return Result$Error(undefined);
}
export function available_parallelism() {
return os.availableParallelism();
}
export function runtime() {
if (typeof Deno !== "undefined") {
return "deno";
} else if (typeof Bun !== "undefined") {
return "bun";
} else if (
typeof process !== "undefined" &&
process.versions &&
process.versions.node
) {
return "node";
}
return "";
}
export function runtime_version() {
const rt = runtime();
if (rt === "deno") {
return Deno.version.deno;
} else if (rt === "bun") {
return Bun.version;
} else if (rt === "node") {
return process.version;
}
return "";
}
export function user_name() {
try {
return os.userInfo().username;
} catch (e) {
return "";
}
}
export function user_id() {
if (process.getuid) {
return Result$Ok(process.getuid());
} else {
return Result$Error(undefined);
}
}
export function group_id() {
if (process.getgid) {
return Result$Ok(process.getgid());
} else {
return Result$Error(undefined);
}
}
export function is_online() {
const interfaces = os.networkInterfaces();
for (const [_name, addresses] of Object.entries(interfaces)) {
for (const address of addresses) {
// Check if interface is not internal (loopback) and has a valid MAC address
if (
!address.internal &&
address.mac &&
address.mac !== "00:00:00:00:00:00"
) {
return true;
}
}
}
return false;
}
export function is_tty() {
return process.stdin.isTTY === true && process.stdout.isTTY === true;
}
export function is_test() {
const stack = new Error().stack || "";
const lines = stack
.split("\n")
.filter((line) => line.includes("file:///"))
.reverse();
// we cannot check for the real entrypoint here because of https://github.com/gleam-lang/gleam/issues/4835
return /at main \(.*?_test\.mjs:\d+:\d+\)$/.test(lines[1]);
}
// -------------------- Process Information --------------------
export function process_id() {
return process.pid;
}
export function read_file(filePath) {
try {
return Result$Ok(fs.readFileSync(filePath, "utf-8"));
} catch (e) {
return Result$Error(undefined);
}
}
export function file_exists(filePath) {
try {
return fs.existsSync(filePath);
} catch (e) {
return false;
}
}
export function env(name) {
return process.env[name] || "";
}
export function os_cmd(command) {
try {
const result = execSync(command, {
encoding: "utf8",
shell: true,
});
return result;
} catch (e) {
return "";
}
}