From 4b81cd2fc8974822946ede183656797e4dc13eb2 Mon Sep 17 00:00:00 2001 From: DavidSM100 Date: Thu, 4 Dec 2025 19:10:25 -0500 Subject: [PATCH] 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 --- deltachat-rpc-server/npm-package/index.d.ts | 7 ++ deltachat-rpc-server/npm-package/index.js | 80 +++++++++++---------- 2 files changed, 51 insertions(+), 36 deletions(-) diff --git a/deltachat-rpc-server/npm-package/index.d.ts b/deltachat-rpc-server/npm-package/index.d.ts index 8bf76c90c..76ab5719d 100644 --- a/deltachat-rpc-server/npm-package/index.d.ts +++ b/deltachat-rpc-server/npm-package/index.d.ts @@ -35,6 +35,13 @@ export interface StartOptions { */ export function startDeltaChat(directory: string, options?: Partial ): DeltaChatOverJsonRpcServer +export class DeltaChatOverJsonRpc extends StdioDeltaChat { + constructor( + directory: string, + options?: Partial + ); + readonly pathToServerBinary: string; +} export namespace FnTypes { export type getRPCServerPath = typeof getRPCServerPath diff --git a/deltachat-rpc-server/npm-package/index.js b/deltachat-rpc-server/npm-package/index.js index 445323089..3519c08ce 100644 --- a/deltachat-rpc-server/npm-package/index.js +++ b/deltachat-rpc-server/npm-package/index.js @@ -69,40 +69,48 @@ import { StdioDeltaChat } from "@deltachat/jsonrpc-client"; /** @type {import("./index").FnTypes.startDeltaChat} */ export function startDeltaChat(directory, options = {}) { - const pathToServerBinary = getRPCServerPath(options); - const server = spawn(pathToServerBinary, { - env: { - RUST_LOG: process.env.RUST_LOG, - DC_ACCOUNTS_PATH: directory, - }, - stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"], - }); - - server.on("error", (err) => { - throw new Error(FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)); - }); - let shouldClose = false; - - server.on("exit", () => { - if (shouldClose) { - return; - } - throw new Error("Server quit"); - }); - - /** @type {import('./index').DeltaChatOverJsonRpcServer} */ - //@ts-expect-error - const dc = new StdioDeltaChat(server.stdin, server.stdout, true); - - dc.close = () => { - shouldClose = true; - if (!server.kill()) { - console.log("server termination failed"); - } - }; - - //@ts-expect-error - dc.pathToServerBinary = pathToServerBinary; - - return dc; + return new DeltaChatOverJsonRpc(directory, options); +} + +export class DeltaChatOverJsonRpc extends StdioDeltaChat { + /** + * + * @param {string} directory + * @param {Partial} options + */ + constructor(directory, options = {}) { + const pathToServerBinary = getRPCServerPath(options); + const server = spawn(pathToServerBinary, { + env: { + RUST_LOG: process.env.RUST_LOG, + DC_ACCOUNTS_PATH: directory, + }, + stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"], + }); + + server.on("error", (err) => { + throw new Error( + FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err) + ); + }); + let shouldClose = false; + + server.on("exit", () => { + if (shouldClose) { + return; + } + throw new Error("Server quit"); + }); + + super(server.stdin, server.stdout, true); + + this.close = () => { + shouldClose = true; + if (!server.kill()) { + console.log("server termination failed"); + } + }; + + this.pathToServerBinary = pathToServerBinary; + } }