posix-node
Version:
Missing Posix Functions for Node.js (via a native module written in Zig)
102 lines • 3.48 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const logging_1 = require("./logging");
// Map from nodejs to zig descriptions:
const nodeToZig = {
arm64: "aarch64",
x64: "x86_64",
linux: "linux-gnu",
darwin: "macos",
};
const name = `${nodeToZig[process.arch]}-${nodeToZig[process.platform]}`;
const LINUX_ONLY = [
"getresuid",
"getresgid",
"setresgid",
"setresuid",
"fexecve",
"_fexecve",
];
let mod = {};
let mod1 = {};
try {
mod = require(`./${name}.node`);
if (process.platform != "linux") {
for (const name of LINUX_ONLY) {
delete mod[name];
}
}
mod.getpid = () => process.pid;
// provide some better public interfaces:
mod["getaddrinfo"] = (node, service, hints) => {
const f = mod["_getaddrinfo"];
if (f == null)
throw Error("getaddrinfo is not implemented");
return f(node, service, hints?.flags ?? 0, hints?.family ?? 0, hints?.socktype ?? 0, hints?.protocol ?? 0);
};
// I could do the JSON in the extension module, but is that really better?
mod["statvfs"] = (...args) => JSON.parse(mod["_statvfs"]?.(...args));
mod["fstatvfs"] = (...args) => JSON.parse(mod["_fstatvfs"]?.(...args));
mod["bind"] = (socket, sockaddr) => {
return mod["_bind"](socket, sockaddr.sa_len, sockaddr.sa_family, sockaddr.sa_data);
};
mod["connect"] = (socket, sockaddr) => {
return mod["_connect"](socket, sockaddr.sa_len, sockaddr.sa_family, sockaddr.sa_data);
};
for (const name of ["execve", "fexecve"]) {
const f = mod["_" + name];
if (f != null) {
mod[name] = (pathname, argv, env) => {
return f(pathname, argv, mapToStrings(env));
};
}
}
const { _posix_spawn } = mod;
if (_posix_spawn != null) {
for (const name of ["posix_spawn", "posix_spawnp"]) {
mod[name] = (path, fileActions, attrs, argv, env) => {
if (attrs == null) {
attrs = {};
}
else {
// from Set([...]) to [...], which is easier to work with from Zig via node api.
if (attrs.sigmask != null) {
attrs.sigmask = Array.from(attrs.sigmask);
}
if (attrs.sigdefault != null) {
attrs.sigdefault = Array.from(attrs.sigdefault);
}
}
return _posix_spawn(path, fileActions ?? [], attrs ?? {}, argv, mapToStrings(env), name.endsWith("spawnp"));
};
}
}
for (const name in mod) {
exports[name] = mod1[name] = (...args) => {
if (name != "chdir")
(0, logging_1.log)(name, args);
const res = mod[name](...args);
if (name != "chdir")
(0, logging_1.log)(name, "returned", res);
return res;
};
}
exports["constants"] = mod1.constants = mod["getConstants"]?.();
}
catch (_err) { }
exports.default = mod1;
function is_array(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
}
function mapToStrings(obj) {
if (is_array(obj)) {
// already array of strings...
return obj;
}
const v = [];
for (const key in obj) {
v.push(`${key}=${obj[key]}`);
}
return v;
}
//# sourceMappingURL=index.js.map
;