Skip to content

Commit 3d0fa2b

Browse files
author
DavidSM100
committed
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
1 parent 92e161c commit 3d0fa2b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

deltachat-rpc-server/npm-package/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ export interface StartOptions {
3535
*/
3636
export function startDeltaChat(directory: string, options?: Partial<SearchOptions & StartOptions> ): DeltaChatOverJsonRpcServer
3737

38+
export class DeltaChatOverJsonRpc extends StdioDeltaChat {
39+
constructor(
40+
directory: string,
41+
options?: Partial<SearchOptions & StartOptions>
42+
);
43+
readonly pathToServerBinary: string;
44+
}
3845

3946
export namespace FnTypes {
4047
export type getRPCServerPath = typeof getRPCServerPath

deltachat-rpc-server/npm-package/index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,46 @@ export function startDeltaChat(directory, options = {}) {
106106

107107
return dc;
108108
}
109+
110+
export class DeltaChatOverJsonRpc extends StdioDeltaChat {
111+
/**
112+
*
113+
* @param {string} directory
114+
* @param {Partial<import("./index").SearchOptions & import("./index").StartOptions>} options
115+
*/
116+
constructor(directory, options = {}) {
117+
const pathToServerBinary = getRPCServerPath(options);
118+
const server = spawn(pathToServerBinary, {
119+
env: {
120+
RUST_LOG: process.env.RUST_LOG,
121+
DC_ACCOUNTS_PATH: directory,
122+
},
123+
stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"],
124+
});
125+
126+
server.on("error", (err) => {
127+
throw new Error(
128+
FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)
129+
);
130+
});
131+
let shouldClose = false;
132+
133+
server.on("exit", () => {
134+
if (shouldClose) {
135+
return;
136+
}
137+
throw new Error("Server quit");
138+
});
139+
140+
super(server.stdin, server.stdout, true);
141+
142+
this.close = () => {
143+
shouldClose = true;
144+
if (!server.kill()) {
145+
console.log("server termination failed");
146+
}
147+
};
148+
149+
this.pathToServerBinary = pathToServerBinary;
150+
}
151+
}

0 commit comments

Comments
 (0)