small changes

This commit is contained in:
Egor Aristov 2025-10-29 11:29:24 +03:00
parent e47c8dca44
commit 8d0af5f95a
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
4 changed files with 41 additions and 49 deletions

View File

@ -35,7 +35,7 @@ declare abstract class IPCCommon {
protected readConn(): void; protected readConn(): void;
protected processMsg(msg: Message): void; protected processMsg(msg: Message): void;
protected sendMsg(msg: Message): void; protected sendMsg(msg: Message): void;
protected handleCall(msg: CallMessage): void; protected handleCall(msg: CallMessage): Promise<void>;
protected handleResponse(msg: ResponseMessage): void; protected handleResponse(msg: ResponseMessage): void;
stop(): void; stop(): void;
call(method: string, ...params: Vals): Promise<Vals>; call(method: string, ...params: Vals): Promise<Vals>;

View File

@ -1 +1 @@
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAUhC,aAAK,OAAO;IACR,IAAI,IAAI;IACR,QAAQ,IAAI;CACf;AAED,KAAK,IAAI,GAAG,GAAG,EAAE,CAAC;AAElB,UAAU,WAAW;IACjB,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,IAAI,CAAC;CAChB;AAED,UAAU,eAAe;IACrB,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,KAAK,OAAO,GAAG,WAAW,GAAG,eAAe,CAAC;AAE7C,UAAU,UAAU;IAChB,MAAM,EAAE,IAAI,CAAC;IACb,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;CACvB;AAED,uBAAe,SAAS;IACpB,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACzC,SAAS,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AGD,qBAAa,QAAS,SAAQ,SAAS;gBACvB,GAAG,SAAS,EAAE,MAAM,EAAE;IAI5B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAUtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAa9B"}

File diff suppressed because one or more lines are too long

View File

@ -66,20 +66,20 @@ abstract class IPCCommon {
this.conn.on('error', (e) => { this.conn.on('error', (e) => {
this.raiseErr(e); this.raiseErr(e);
}) });
this.conn.on('close', (hadError: boolean) => { this.conn.on('close', (hadError: boolean) => {
if (hadError) { if (hadError) {
this.raiseErr(new Error('connection closed due to error')); this.raiseErr(new Error('connection closed due to error'));
} }
}) });
rl.on('line', (line) => { rl.on('line', (line) => {
try { try {
const msg: Message = JSON.parse(line); const msg: Message = JSON.parse(line);
this.processMsg(msg); this.processMsg(msg);
} catch (e) { } catch (e) {
this.raiseErr(new Error(`${e}`)); this.raiseErr(new Error(`${ e }`));
} }
}); });
} }
@ -102,24 +102,24 @@ abstract class IPCCommon {
const data = JSON.stringify(msg) + '\n'; const data = JSON.stringify(msg) + '\n';
this.conn.write(data); this.conn.write(data);
} catch (e) { } catch (e) {
this.raiseErr(new Error(`send response for ${msg.id}: ${e}`)); this.raiseErr(new Error(`send response for ${ msg.id }: ${ e }`));
} }
} }
protected handleCall(msg: CallMessage): void { protected async handleCall(msg: CallMessage) {
const [endpointName, methodName] = msg.method.split('.'); const [endpointName, methodName] = msg.method.split('.');
if(!endpointName || !methodName) { if (!endpointName || !methodName) {
this.sendMsg({type: MsgType.Response, id: msg.id, error: `call malformed: ${msg.method}`}); this.sendMsg({type: MsgType.Response, id: msg.id, error: `call malformed: ${ msg.method }`});
return; return;
} }
const endpoint = this.localApis[endpointName]; const endpoint = this.localApis[endpointName];
if(!endpoint) { if (!endpoint) {
this.sendMsg({type: MsgType.Response, id: msg.id, error: `endpoint not found: ${endpointName}`}); this.sendMsg({type: MsgType.Response, id: msg.id, error: `endpoint not found: ${ endpointName }`});
return; return;
} }
const method = endpoint[methodName]; const method = endpoint[methodName];
if (!method || typeof method !== 'function') { if (!method || typeof method !== 'function') {
this.sendMsg({type: MsgType.Response, id: msg.id, error: `method not found: ${msg.method}`}); this.sendMsg({type: MsgType.Response, id: msg.id, error: `method not found: ${ msg.method }`});
return; return;
} }
@ -128,60 +128,51 @@ abstract class IPCCommon {
this.sendMsg({ this.sendMsg({
type: MsgType.Response, type: MsgType.Response,
id: msg.id, id: msg.id,
error: `argument count mismatch: expected ${argsCount}, got ${msg.params.length}` error: `argument count mismatch: expected ${ argsCount }, got ${ msg.params.length }`
}); });
return; return;
} }
try { try {
this.processingCalls++; this.processingCalls++;
let result = method.apply(endpoint, msg.params);
const result = method.apply(endpoint, msg.params);
if (result instanceof Promise) { if (result instanceof Promise) {
result result = await result;
.then((res) => {
this.sendMsg({type: MsgType.Response, id: msg.id, result: [res]});
})
.catch((err) => {
this.sendMsg({type: MsgType.Response, id: msg.id, error: `${err}`});
});
} else {
this.sendMsg({type: MsgType.Response, id: msg.id, result: [result]});
} }
this.sendMsg({type: MsgType.Response, id: msg.id, result: [result]});
} catch (err) { } catch (err) {
this.sendMsg({type: MsgType.Response, id: msg.id, error: `${err}`}); this.sendMsg({type: MsgType.Response, id: msg.id, error: `${ err }`});
} finally { } finally {
this.processingCalls--; this.processingCalls--;
} }
if(this.stopRequested) { if (this.stopRequested) {
if(this.onClose) this.onClose(); if (this.onClose) this.onClose();
} }
} }
protected handleResponse(msg: ResponseMessage): void { protected handleResponse(msg: ResponseMessage): void {
const callback = this.pendingCalls[msg.id]; const callback = this.pendingCalls[msg.id];
if (!callback) { if (!callback) {
this.raiseErr(new Error(`received response for unknown msgId: ${msg.id}`)); this.raiseErr(new Error(`received response for unknown msgId: ${ msg.id }`));
return; return;
} }
delete this.pendingCalls[msg.id]; delete this.pendingCalls[msg.id];
const err = msg.error ? new Error(`remote error: ${msg.error}`) : null; const err = msg.error ? new Error(`remote error: ${ msg.error }`) : null;
callback({ result: msg.result || [], error: err }); callback({result: msg.result || [], error: err});
} }
stop() { stop() {
if (this.stopRequested) { if (this.stopRequested) {
throw new Error('close already requested'); throw new Error('close already requested');
} }
if(!this.conn || this.conn.readyState === "closed") { if (!this.conn || this.conn.readyState === 'closed') {
throw new Error('connection already closed'); throw new Error('connection already closed');
} }
this.stopRequested = true; this.stopRequested = true;
if(this.onClose) this.onClose(); if (this.onClose) this.onClose();
} }
call(method: string, ...params: Vals): Promise<Vals> { call(method: string, ...params: Vals): Promise<Vals> {
@ -199,13 +190,13 @@ abstract class IPCCommon {
this.sendMsg({type: MsgType.Call, id, method, params}); this.sendMsg({type: MsgType.Call, id, method, params});
} catch (e) { } catch (e) {
delete this.pendingCalls[id]; delete this.pendingCalls[id];
reject(new Error(`send call: ${e}`)); reject(new Error(`send call: ${ e }`));
} }
}); });
} }
protected raiseErr(err: Error): void { protected raiseErr(err: Error): void {
if(this.onError) this.onError(err); if (this.onError) this.onError(err);
} }
} }
@ -217,12 +208,12 @@ export class ParentIPC extends IPCCommon {
private readonly listener: net.Server; private readonly listener: net.Server;
constructor(cmdPath: string, cmdArgs: string[], ...localApis: object[]) { constructor(cmdPath: string, cmdArgs: string[], ...localApis: object[]) {
const socketPath = path.join(os.tmpdir(), `kitten-ipc-${process.pid}.sock`); const socketPath = path.join(os.tmpdir(), `kitten-ipc-${ process.pid }.sock`);
super(localApis, socketPath); super(localApis, socketPath);
this.cmdPath = cmdPath; this.cmdPath = cmdPath;
if (cmdArgs.includes(`--${IPC_SOCKET_ARG}`)) { if (cmdArgs.includes(`--${ IPC_SOCKET_ARG }`)) {
throw new Error(`you should not use '--${IPC_SOCKET_ARG}' argument in your command`); throw new Error(`you should not use '--${ IPC_SOCKET_ARG }' argument in your command`);
} }
this.cmdArgs = cmdArgs; this.cmdArgs = cmdArgs;
@ -232,7 +223,8 @@ export class ParentIPC extends IPCCommon {
async start(): Promise<void> { async start(): Promise<void> {
try { try {
fs.unlinkSync(this.socketPath); fs.unlinkSync(this.socketPath);
} catch {} } catch {
}
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
@ -242,7 +234,7 @@ export class ParentIPC extends IPCCommon {
this.listener.on('error', reject); this.listener.on('error', reject);
}); });
const cmdArgs = [...this.cmdArgs, `--${IPC_SOCKET_ARG}`, this.socketPath]; const cmdArgs = [...this.cmdArgs, `--${ IPC_SOCKET_ARG }`, this.socketPath];
this.cmd = spawn(this.cmdPath, cmdArgs, {stdio: 'inherit'}); this.cmd = spawn(this.cmdPath, cmdArgs, {stdio: 'inherit'});
this.cmd.on('error', (err) => { this.cmd.on('error', (err) => {
@ -273,19 +265,19 @@ export class ParentIPC extends IPCCommon {
async wait(): Promise<void> { async wait(): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(!this.cmd) throw new Error('Command is not started yet'); if (!this.cmd) throw new Error('Command is not started yet');
this.cmd.addListener('close', (code, signal) => { this.cmd.addListener('close', (code, signal) => {
if(signal || code) { if (signal || code) {
if(signal) reject(new Error(`Process exited with signal ${signal}`)); if (signal) reject(new Error(`Process exited with signal ${ signal }`));
else reject(new Error(`Process exited with code ${code}`)); else reject(new Error(`Process exited with code ${ code }`));
} else { } else {
resolve(); resolve();
} }
}); });
this.onError = (err) => { this.onError = (err) => {
reject(err); reject(err);
} };
}) });
} }
} }
@ -309,13 +301,13 @@ export class ChildIPC extends IPCCommon {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.onError = (err) => { this.onError = (err) => {
reject(err); reject(err);
} };
this.onClose = () => { this.onClose = () => {
if (this.processingCalls === 0) { if (this.processingCalls === 0) {
this.conn?.destroy(); this.conn?.destroy();
resolve(); resolve();
} }
} };
}); });
} }
} }