mirror of
https://github.com/chatmail/core.git
synced 2026-04-27 18:36:30 +03:00
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:
7
deltachat-rpc-server/npm-package/index.d.ts
vendored
7
deltachat-rpc-server/npm-package/index.d.ts
vendored
@@ -35,6 +35,13 @@ export interface StartOptions {
|
||||
*/
|
||||
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 type getRPCServerPath = typeof getRPCServerPath
|
||||
|
||||
@@ -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<import("./index").SearchOptions & import("./index").StartOptions>} 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user