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,6 +69,16 @@ 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 = {}) {
return new DeltaChatOverJsonRpc(directory, options);
}
export class DeltaChatOverJsonRpc extends StdioDeltaChat {
/**
*
* @param {string} directory
* @param {Partial<import("./index").SearchOptions & import("./index").StartOptions>} options
*/
constructor(directory, options = {}) {
const pathToServerBinary = getRPCServerPath(options); const pathToServerBinary = getRPCServerPath(options);
const server = spawn(pathToServerBinary, { const server = spawn(pathToServerBinary, {
env: { env: {
@@ -79,7 +89,9 @@ export function startDeltaChat(directory, options = {}) {
}); });
server.on("error", (err) => { server.on("error", (err) => {
throw new Error(FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)); throw new Error(
FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)
);
}); });
let shouldClose = false; let shouldClose = false;
@@ -90,19 +102,15 @@ export function startDeltaChat(directory, options = {}) {
throw new Error("Server quit"); throw new Error("Server quit");
}); });
/** @type {import('./index').DeltaChatOverJsonRpcServer} */ super(server.stdin, server.stdout, true);
//@ts-expect-error
const dc = new StdioDeltaChat(server.stdin, server.stdout, true);
dc.close = () => { this.close = () => {
shouldClose = true; shouldClose = true;
if (!server.kill()) { if (!server.kill()) {
console.log("server termination failed"); console.log("server termination failed");
} }
}; };
//@ts-expect-error this.pathToServerBinary = pathToServerBinary;
dc.pathToServerBinary = pathToServerBinary; }
return dc;
} }