Skip to content

Commit 0136a97

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 0136a97

2 files changed

Lines changed: 45 additions & 30 deletions

File tree

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: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,48 @@ import { StdioDeltaChat } from "@deltachat/jsonrpc-client";
6969

7070
/** @type {import("./index").FnTypes.startDeltaChat} */
7171
export function startDeltaChat(directory, options = {}) {
72-
const pathToServerBinary = getRPCServerPath(options);
73-
const server = spawn(pathToServerBinary, {
74-
env: {
75-
RUST_LOG: process.env.RUST_LOG,
76-
DC_ACCOUNTS_PATH: directory,
77-
},
78-
stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"],
79-
});
72+
return new DeltaChatOverJsonRpc(directory, options);
73+
}
8074

81-
server.on("error", (err) => {
82-
throw new Error(FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err));
83-
});
84-
let shouldClose = false;
75+
export class DeltaChatOverJsonRpc extends StdioDeltaChat {
76+
/**
77+
*
78+
* @param {string} directory
79+
* @param {Partial<import("./index").SearchOptions & import("./index").StartOptions>} options
80+
*/
81+
constructor(directory, options = {}) {
82+
const pathToServerBinary = getRPCServerPath(options);
83+
const server = spawn(pathToServerBinary, {
84+
env: {
85+
RUST_LOG: process.env.RUST_LOG,
86+
DC_ACCOUNTS_PATH: directory,
87+
},
88+
stdio: ["pipe", "pipe", options.muteStdErr ? "ignore" : "inherit"],
89+
});
8590

86-
server.on("exit", () => {
87-
if (shouldClose) {
88-
return;
89-
}
90-
throw new Error("Server quit");
91-
});
91+
server.on("error", (err) => {
92+
throw new Error(
93+
FAILED_TO_START_SERVER_EXECUTABLE(pathToServerBinary, err)
94+
);
95+
});
96+
let shouldClose = false;
9297

93-
/** @type {import('./index').DeltaChatOverJsonRpcServer} */
94-
//@ts-expect-error
95-
const dc = new StdioDeltaChat(server.stdin, server.stdout, true);
98+
server.on("exit", () => {
99+
if (shouldClose) {
100+
return;
101+
}
102+
throw new Error("Server quit");
103+
});
96104

97-
dc.close = () => {
98-
shouldClose = true;
99-
if (!server.kill()) {
100-
console.log("server termination failed");
101-
}
102-
};
105+
super(server.stdin, server.stdout, true);
103106

104-
//@ts-expect-error
105-
dc.pathToServerBinary = pathToServerBinary;
107+
this.close = () => {
108+
shouldClose = true;
109+
if (!server.kill()) {
110+
console.log("server termination failed");
111+
}
112+
};
106113

107-
return dc;
114+
this.pathToServerBinary = pathToServerBinary;
115+
}
108116
}

0 commit comments

Comments
 (0)