@@ -69,40 +69,48 @@ import { StdioDeltaChat } from "@deltachat/jsonrpc-client";
6969
7070/** @type {import("./index").FnTypes.startDeltaChat } */
7171export 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