Files
quixos-nix-helpers/socket-address-cli.mjs
T

45 lines
1.4 KiB
JavaScript

// Language bridge to the shared socket policy; identities contain no credentials.
import {
checkedSocketPath,
prepareSocketDirectory,
removeEmptySocketDirectory,
socketAddress,
} from "./socket-address.mjs";
const [operation, role = "bootstrap"] = process.argv.slice(2);
if (
!["check-path", "prepare", "prepare-directory", "address", "cleanup"].includes(operation) ||
process.argv.length > 4 ||
(process.argv.length === 4 && !["prepare", "address"].includes(operation))
)
throw Error("Select a socket operation and an optional address role");
const chunks = [];
let length = 0;
for await (const chunk of process.stdin) {
length += chunk.length;
if (length > 1024 * 1024) throw Error("Socket command input exceeds its bound");
chunks.push(chunk);
}
const identity = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(Buffer.concat(chunks)));
switch (operation) {
case "check-path":
console.log(checkedSocketPath(identity));
break;
case "prepare": {
// Validate the role before creating any directories.
const address = socketAddress(identity, role);
await prepareSocketDirectory(identity);
console.log(address);
break;
}
case "prepare-directory":
console.log(await prepareSocketDirectory(identity));
break;
case "address":
console.log(socketAddress(identity, role));
break;
case "cleanup":
await removeEmptySocketDirectory(identity);
break;
}