diff --git a/example/ts/src/index.ts b/example/ts/src/index.ts index 4352d89..8603660 100644 --- a/example/ts/src/index.ts +++ b/example/ts/src/index.ts @@ -22,8 +22,9 @@ async function main() { console.log(`call result ts->go Div = ${await remoteApi.Div(10, 2)}`); - const data1 = new Uint8Array().fill(0b10101010, 0, 100); - const data2 = new Uint8Array().fill(0b11110000, 0, 100); + // todo check empty array + 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)}`); await ipc.wait(); diff --git a/lib/golang/lib.go b/lib/golang/lib.go index 1a97cf9..e849632 100644 --- a/lib/golang/lib.go +++ b/lib/golang/lib.go @@ -6,6 +6,7 @@ import ( "errors" "flag" "fmt" + "log" "net" "os" "os/exec" @@ -66,7 +67,7 @@ func (ipc *ipcCommon) readConn() { for scn.Scan() { var msg Message a := scn.Bytes() - _ = a + log.Println(string(a)) if err := json.Unmarshal(scn.Bytes(), &msg); err != nil { ipc.raiseErr(fmt.Errorf("unmarshal message: %w", err)) break diff --git a/lib/ts/src/lib.ts b/lib/ts/src/lib.ts index 8d49ad1..e6d53b1 100644 --- a/lib/ts/src/lib.ts +++ b/lib/ts/src/lib.ts @@ -9,6 +9,8 @@ import {AsyncQueue} from './asyncqueue.js'; const IPC_SOCKET_ARG = 'ipc-socket'; +type JSONSerializable = string | number | boolean; + enum MsgType { Call = 1, Response = 2, @@ -169,7 +171,7 @@ abstract class IPCCommon { callback({result: msg.result || [], error: err}); } - call(method: string, ...params: Vals): Promise { + call(method: string, ...args: Vals): Promise { return new Promise((resolve, reject) => { const id = this.nextId++; @@ -181,7 +183,7 @@ abstract class IPCCommon { } }; try { - this.sendMsg({type: MsgType.Call, id, method, args: params}); + this.sendMsg({type: MsgType.Call, id, method, args: args.map(this.convType)}); } catch (e) { delete this.pendingCalls[id]; 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() { if (this.stopRequested) { throw new Error('close already requested');