Skip to content

Commit

Permalink
build(packaging): ci build
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinse12 authored and github-actions[bot] committed Mar 23, 2024
1 parent 241d323 commit d7e4f88
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
129 changes: 66 additions & 63 deletions dist/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,8 @@ class Daemon extends core_1.Command {
required: true,
default: defaults_js_1.default.PLEBBIT_DATA_PATH
}),
// seed: Flags.boolean({
// description:
// "Seeding flag. Seeding helps subplebbits distribute their publications and latest updates, as well as receiving new publications",
// required: false,
// default: false
// }),
// seedSubs: Flags.string({
// description: "Subplebbits to seed. If --seed is used and no subs was provided, it will default to seeding default subs",
// required: false,
// multiple: true,
// default: []
// }),
plebbitRpcPort: core_1.Flags.integer({
description: "Specify Plebbit RPC API port to listen on",
description: "Specify Plebbit RPC port to listen on",
required: true,
default: defaults_js_1.default.PLEBBIT_RPC_API_PORT
}),
Expand Down Expand Up @@ -61,81 +49,96 @@ class Daemon extends core_1.Command {
}
catch (e) {
plebbitRpcAuthKey = (0, crypto_1.randomBytes)(32).toString("base64").replace(/[/+=]/g, "").substring(0, 40);
await promises_1.default.writeFile(plebbitRpcAuthKey, plebbitRpcAuthKey, { flag: "wx" });
await promises_1.default.writeFile(plebbitRpcAuthKeyPath, plebbitRpcAuthKey, { flag: "wx" });
}
return plebbitRpcAuthKey;
}
async run() {
const { flags } = await this.parse(Daemon);
const log = (await (0, util_js_1.getPlebbitLogger)())("plebbit-cli:daemon");
log(`flags: `, flags);
const ipfsApiEndpoint = `http://localhost:${flags.ipfsApiPort}/api/v0`;
const ipfsGatewayEndpoint = `http://localhost:${flags.ipfsGatewayPort}`;
let mainProcessExited = false;
let isIpfsNodeAlreadyRunningByAnotherProgram = false;
// Ipfs Node may fail randomly, we need to set a listener so when it exits because of an error we restart it
let ipfsProcess;
const keepIpfsUp = async () => {
isIpfsNodeAlreadyRunningByAnotherProgram = await tcp_port_used_1.default.check(flags.ipfsApiPort, "127.0.0.1");
if (isIpfsNodeAlreadyRunningByAnotherProgram) {
if (ipfsProcess || usingDifferentProcessRpc)
return; // already started, no need to intervene
const isIpfsApiPortTaken = await tcp_port_used_1.default.check(flags.ipfsApiPort, "127.0.0.1");
if (isIpfsApiPortTaken) {
log(`Ipfs API already running on port (${flags.ipfsApiPort}) by another program. Plebbit-cli will use the running ipfs daemon instead of starting a new one`);
return;
}
ipfsProcess = await (0, startIpfs_js_1.startIpfsNode)(flags.ipfsApiPort, flags.ipfsGatewayPort);
log(`Started ipfs process with pid (${ipfsProcess.pid})`);
console.log(`IPFS API listening on: ${ipfsApiEndpoint}`);
console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`);
ipfsProcess.on("exit", async () => {
// Restart IPFS process because it failed
log(`Ipfs node with pid (${ipfsProcess.pid}) exited`);
if (!mainProcessExited)
log(`Ipfs node with pid (${ipfsProcess?.pid}) exited`);
if (!mainProcessExited) {
ipfsProcess = undefined;
await keepIpfsUp();
}
else
ipfsProcess.removeAllListeners();
});
};
let subsToSeed;
// if (flags.seed) {
// if (lodash.isEmpty(flags.seedSubs)) {
// // load default subs here
// const res = await fetch("https://raw.githubusercontent.com/plebbit/temporary-default-subplebbits/master/subplebbits.json");
// const subs: { title: string; address: string }[] = await res.json();
// subsToSeed = subs.map((sub) => sub.address);
// } else subsToSeed = flags.seedSubs;
// }
log.trace(`subs to seed:`, subsToSeed);
let startedOwnRpc = false;
let usingDifferentProcessRpc = false;
const createOrConnectRpc = async () => {
if (startedOwnRpc)
return;
const isRpcPortTaken = await tcp_port_used_1.default.check(flags.plebbitRpcPort, "127.0.0.1");
if (isRpcPortTaken && usingDifferentProcessRpc)
return;
if (isRpcPortTaken) {
log(`Plebbit RPC is already running on port (${flags.plebbitRpcPort}) by another program. Plebbit-cli will use the running RPC server, and if shuts down, plebbit-cli will start a new RPC instance`);
const plebbitRpcApiUrl = `ws://localhost:${flags.plebbitRpcPort}`;
console.log("Using the already started RPC server at:", plebbitRpcApiUrl);
console.log("plebbit-cli daemon will monitor the plebbit RPC and ipfs API to make sure they're always up");
const Plebbit = await import("@plebbit/plebbit-js");
const plebbit = await Plebbit.default({ plebbitRpcClientsOptions: [plebbitRpcApiUrl] });
plebbit.on("error", () => { });
console.log(`Subplebbits in data path: `, await plebbit.listSubplebbits());
usingDifferentProcessRpc = true;
return;
}
const rpcAuthKey = await this._generateRpcAuthKeyIfNotExisting(flags.plebbitDataPath);
const PlebbitWsServer = await import("@plebbit/plebbit-js/dist/node/rpc/src/index.js");
const rpcServer = await PlebbitWsServer.default.PlebbitWsServer({
port: flags.plebbitRpcPort,
plebbitOptions: {
ipfsHttpClientsOptions: [ipfsApiEndpoint],
dataPath: flags.plebbitDataPath
},
authKey: rpcAuthKey
});
usingDifferentProcessRpc = false;
startedOwnRpc = true;
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort}/${rpcAuthKey} (secret auth key for remote connections)`);
console.log(`Plebbit data path: ${path_1.default.resolve(rpcServer.plebbit.dataPath)}`);
console.log(`Subplebbits in data path: `, await rpcServer.plebbit.listSubplebbits());
const handlRpcExit = async (signal) => {
log(`in handle exit (${signal})`);
await rpcServer.destroy();
process.exit();
};
["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handlRpcExit));
};
await keepIpfsUp();
process.on("exit", () => (mainProcessExited = true) && !isIpfsNodeAlreadyRunningByAnotherProgram && process.kill(ipfsProcess.pid));
const ipfsApiEndpoint = `http://localhost:${flags.ipfsApiPort}/api/v0`;
const ipfsGatewayEndpoint = `http://localhost:${flags.ipfsGatewayPort}`;
const rpcAuthKey = await this._generateRpcAuthKeyIfNotExisting(flags.plebbitDataPath);
//@ts-expect-error
const PlebbitWsServer = await import("@plebbit/plebbit-js/dist/node/rpc/src/index.js?");
const rpcServer = await PlebbitWsServer.default.PlebbitWsServer({
port: flags.plebbitRpcPort,
plebbitOptions: {
ipfsHttpClientsOptions: [ipfsApiEndpoint],
dataPath: flags.plebbitDataPath
},
authKey: rpcAuthKey
await createOrConnectRpc();
setInterval(async () => {
await keepIpfsUp();
await createOrConnectRpc();
}, 5000);
process.on("exit", () => {
mainProcessExited = true;
if (typeof ipfsProcess?.pid === "number" && !ipfsProcess.killed)
process.kill(ipfsProcess.pid);
});
const handleExit = async (signal) => {
log(`in handle exit (${signal})`);
await rpcServer.destroy();
process.exit();
};
const subs = await rpcServer.plebbit.listSubplebbits();
["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handleExit));
console.log(`IPFS API listening on: ${ipfsApiEndpoint}`);
console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort} (local connections only)`);
console.log(`plebbit rpc: listening on ws://localhost:${flags.plebbitRpcPort}/${rpcAuthKey} (secret auth key for remote connections)`);
console.log(`Plebbit data path: ${path_1.default.resolve(rpcServer.plebbit.dataPath)}`);
console.log(`Subplebbits in data path: `, subs);
// if (Array.isArray(subsToSeed)) {
// const seedSubsLoop = () => {
// // I think calling setTimeout constantly here will overflow memory. Need to check later
// seedSubplebbits(<string[]>subsToSeed, rpcServer.plebbit).then(() => setTimeout(seedSubsLoop, 600000)); // Seed subs every 10 minutes
// };
// console.log(`Seeding subplebbits:`, subsToSeed);
// seedSubsLoop();
// }
}
}
exports.default = Daemon;
2 changes: 1 addition & 1 deletion dist/cli/commands/subplebbit/start.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export default class Start extends BaseCommand {
static args: {
addresses: import("@oclif/core/lib/interfaces/parser.js").Arg<string, Record<string, unknown>>;
};
static examples: never[];
static examples: string[];
run(): Promise<void>;
}
5 changes: 4 additions & 1 deletion dist/cli/commands/subplebbit/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class Start extends base_command_js_1.BaseCommand {
description: "Addresses of subplebbits to start. Separated by space"
})
};
static examples = [];
static examples = [
"plebbit subplebbit start plebbit.eth",
"plebbit subplebbit start 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu"
];
async run() {
const { argv, flags } = await this.parse(Start);
const addresses = argv;
Expand Down
2 changes: 1 addition & 1 deletion dist/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit d7e4f88

Please sign in to comment.