api(@deltachat/stdio-rpc-server): also export a class

This is convenient for bots and libs for bots, so they can extend from this class directly
This commit is contained in:
DavidSM100
2025-12-04 19:10:25 -05:00
committed by l
parent be920bf3bf
commit 4b81cd2fc8
2 changed files with 51 additions and 36 deletions

View File

@@ -35,6 +35,13 @@ export interface StartOptions {
*/ */
export function startDeltaChat(directory: string, options?: Partial<SearchOptions & StartOptions> ): DeltaChatOverJsonRpcServer export function startDeltaChat(directory: string, options?: Partial<SearchOptions & StartOptions> ): DeltaChatOverJsonRpcServer
export class DeltaChatOverJsonRpc extends StdioDeltaChat {
constructor(
directory: string,
options?: Partial<SearchOptions & StartOptions>
);
readonly pathToServerBinary: string;
}
export namespace FnTypes { export namespace FnTypes {
export type getRPCServerPath = typeof getRPCServerPath export type getRPCServerPath = typeof getRPCServerPath

View File

@@ -69,40 +69,48 @@ import { StdioDeltaChat } from "@deltachat/jsonrpc-client";
/** @type {import("./index").FnTypes.startDeltaChat} */ /** @type {import("./index").FnTypes.startDeltaChat} */
export function startDeltaChat(directory, options = {}) { export function startDeltaChat(directory, options = {}) {
const pathToServerBinary = getRPCServerPath(options); return new DeltaChatOverJsonRpc(directory, options);
const server = spawn(pathToServerBinary, { }
env: {
RUST_LOG: process.env.RUST_LOG, export class DeltaChatOverJsonRpc extends StdioDeltaChat {
DC_ACCOUNTS_PATH: directory, /**
}, *
stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"], * @param {string} directory
}); * @param {Partial<import("./index").SearchOptions & import("./index").StartOptions>} options
*/
server.on("error", (err) => { constructor(directory, options = {}) {
throw new Error(FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)); const pathToServerBinary = getRPCServerPath(options);
}); const server = spawn(pathToServerBinary, {
let shouldClose = false; env: {
RUST_LOG: process.env.RUST_LOG,
server.on("exit", () => { DC_ACCOUNTS_PATH: directory,
if (shouldClose) { },
return; stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"],
} });
throw new Error("Server quit");
}); server.on("error", (err) => {
throw new Error(
/** @type {import('./index').DeltaChatOverJsonRpcServer} */ FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)
//@ts-expect-error );
const dc = new StdioDeltaChat(server.stdin, server.stdout, true); });
let shouldClose = false;
dc.close = () => {
shouldClose = true; server.on("exit", () => {
if (!server.kill()) { if (shouldClose) {
console.log("server termination failed"); return;
} }
}; throw new Error("Server quit");
});
//@ts-expect-error
dc.pathToServerBinary = pathToServerBinary; super(server.stdin, server.stdout, true);
return dc; this.close = () => {
shouldClose = true;
if (!server.kill()) {
console.log("server termination failed");
}
};
this.pathToServerBinary = pathToServerBinary;
}
} }