This commit is contained in:
Egor Aristov 2025-11-16 09:32:58 +03:00
parent 510e0a108d
commit 6647365a80
Signed by: egor3f
GPG Key ID: 40482A264AAEC85F
3 changed files with 26 additions and 5 deletions

View File

@ -22,8 +22,9 @@ async function main() {
console.log(`call result ts->go Div = ${await remoteApi.Div(10, 2)}`); console.log(`call result ts->go Div = ${await remoteApi.Div(10, 2)}`);
const data1 = new Uint8Array().fill(0b10101010, 0, 100); // todo check empty array
const data2 = new Uint8Array().fill(0b11110000, 0, 100); const data1 = new Uint8Array(100).fill(0b10101010, 0, 100);
const data2 = new Uint8Array(100).fill(0b11110000, 0, 100);
console.log(`call result ts->go XorData = ${await remoteApi.XorData(data1, data2)}`); console.log(`call result ts->go XorData = ${await remoteApi.XorData(data1, data2)}`);
await ipc.wait(); await ipc.wait();

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"log"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@ -66,7 +67,7 @@ func (ipc *ipcCommon) readConn() {
for scn.Scan() { for scn.Scan() {
var msg Message var msg Message
a := scn.Bytes() a := scn.Bytes()
_ = a log.Println(string(a))
if err := json.Unmarshal(scn.Bytes(), &msg); err != nil { if err := json.Unmarshal(scn.Bytes(), &msg); err != nil {
ipc.raiseErr(fmt.Errorf("unmarshal message: %w", err)) ipc.raiseErr(fmt.Errorf("unmarshal message: %w", err))
break break

View File

@ -9,6 +9,8 @@ import {AsyncQueue} from './asyncqueue.js';
const IPC_SOCKET_ARG = 'ipc-socket'; const IPC_SOCKET_ARG = 'ipc-socket';
type JSONSerializable = string | number | boolean;
enum MsgType { enum MsgType {
Call = 1, Call = 1,
Response = 2, Response = 2,
@ -169,7 +171,7 @@ abstract class IPCCommon {
callback({result: msg.result || [], error: err}); callback({result: msg.result || [], error: err});
} }
call(method: string, ...params: Vals): Promise<Vals> { call(method: string, ...args: Vals): Promise<Vals> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const id = this.nextId++; const id = this.nextId++;
@ -181,7 +183,7 @@ abstract class IPCCommon {
} }
}; };
try { try {
this.sendMsg({type: MsgType.Call, id, method, args: params}); this.sendMsg({type: MsgType.Call, id, method, args: args.map(this.convType)});
} catch (e) { } catch (e) {
delete this.pendingCalls[id]; delete this.pendingCalls[id];
reject(new Error(`send call: ${ e }`)); reject(new Error(`send call: ${ e }`));
@ -189,6 +191,23 @@ abstract class IPCCommon {
}); });
} }
private convType(arg: any): JSONSerializable {
// noinspection FallThroughInSwitchStatementJS
switch (typeof arg) {
case 'string':
case 'boolean':
case 'number':
return arg;
// @ts-expect-error TS7029
case 'object':
if(arg instanceof Uint8Array) {
return Buffer.from(arg).toString('base64');
}
default:
throw new Error(`arg type ${typeof arg} is not supported`);
}
}
stop() { stop() {
if (this.stopRequested) { if (this.stopRequested) {
throw new Error('close already requested'); throw new Error('close already requested');