tons of code
This commit is contained in:
parent
3ef477922d
commit
f3a9abffe7
@ -10,14 +10,10 @@ import (
|
||||
kittenipc "efprojects.com/kitten-ipc"
|
||||
)
|
||||
|
||||
type callable interface {
|
||||
Call(method string, params ...any) (kittenipc.Vals, error)
|
||||
}
|
||||
|
||||
{{range $e := .Api.Endpoints}}
|
||||
|
||||
type {{.Name}} struct {
|
||||
Ipc callable
|
||||
Ipc kittenipc.Callable
|
||||
}
|
||||
|
||||
{{range $mtd := $e.Methods}}
|
||||
@ -26,10 +22,11 @@ func ({{$e.Name | receiver}} *{{$e.Name}}) {{$mtd.Name}}(
|
||||
) (
|
||||
{{range $mtd.Ret}}{{.Type | typedef}}, {{end}}error,
|
||||
) {
|
||||
results, err := {{$e.Name | receiver}}.Ipc.Call("{{$e.Name}}", "{{$mtd.Name}}"{{range $mtd.Params}}, {{.Name}}{{end}})
|
||||
results, err := {{$e.Name | receiver}}.Ipc.Call("{{$e.Name}}.{{$mtd.Name}}"{{range $mtd.Params}}, {{.Name}}{{end}})
|
||||
if err != nil {
|
||||
return {{range $mtd.Ret}}{{.Type | zerovalue}}, {{end}} fmt.Errorf("call to {{$e.Name}}.{{$mtd.Name}} failed: %w", err)
|
||||
}
|
||||
_ = results
|
||||
return {{range $idx, $ret := $mtd.Ret}}results[{{$idx}}].({{$ret.Type | typedef}}), {{end}}nil
|
||||
}
|
||||
{{end}}
|
||||
|
||||
@ -74,7 +74,7 @@ func (g *GoApiGenerator) Generate(apis *api.Api, destFile string) error {
|
||||
}
|
||||
|
||||
func (g *GoApiGenerator) writeDest(destFile string, bytes []byte) error {
|
||||
f, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
f, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open destination file: %w", err)
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func (g *TypescriptApiGenerator) Generate(apis *api.Api, destFile string) error
|
||||
}
|
||||
|
||||
func (g *TypescriptApiGenerator) writeDest(destFile string, bytes []byte) error {
|
||||
f, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE, 0644)
|
||||
f, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open destination file: %w", err)
|
||||
}
|
||||
|
||||
@ -104,6 +104,7 @@ func (t *TypescriptApiParser) Parse(sourceFilePath string) (*api.Api, error) {
|
||||
}
|
||||
apiMethod.Params = append(apiMethod.Params, apiPar)
|
||||
}
|
||||
if method.Type != nil {
|
||||
var apiRet api.Val
|
||||
switch method.Type.Kind {
|
||||
case ast.KindNumberKeyword:
|
||||
@ -117,6 +118,7 @@ func (t *TypescriptApiParser) Parse(sourceFilePath string) (*api.Api, error) {
|
||||
return false
|
||||
}
|
||||
apiMethod.Ret = []api.Val{apiRet}
|
||||
}
|
||||
endpoint.Methods = append(endpoint.Methods, apiMethod)
|
||||
}
|
||||
|
||||
|
||||
@ -39,6 +39,10 @@ type Message struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type Callable interface {
|
||||
Call(method string, params ...any) (Vals, error)
|
||||
}
|
||||
|
||||
type ipcCommon struct {
|
||||
localApi any
|
||||
socketPath string
|
||||
@ -279,7 +283,7 @@ func (p *ParentIPC) acceptConn() error {
|
||||
return fmt.Errorf("accept: %w", res.Error())
|
||||
}
|
||||
p.conn = res.MustGet()
|
||||
p.readConn()
|
||||
go p.readConn()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
8
lib/ts/dist/lib.d.ts
vendored
8
lib/ts/dist/lib.d.ts
vendored
@ -23,13 +23,13 @@ interface CallResult {
|
||||
error: Error | null;
|
||||
}
|
||||
declare abstract class IPCCommon {
|
||||
protected localApi: any;
|
||||
protected localApis: Record<string, any>;
|
||||
protected socketPath: string;
|
||||
protected conn: net.Socket | null;
|
||||
protected nextId: number;
|
||||
protected pendingCalls: Record<number, (result: CallResult) => void>;
|
||||
protected errors: QueuedEvent<Error>;
|
||||
protected constructor(localApi: any, socketPath: string);
|
||||
protected constructor(localApis: object[], socketPath: string);
|
||||
protected readConn(): void;
|
||||
protected processMsg(msg: Message): void;
|
||||
protected handleCall(msg: CallMessage): void;
|
||||
@ -43,13 +43,13 @@ export declare class ParentIPC extends IPCCommon {
|
||||
private readonly cmdArgs;
|
||||
private cmd;
|
||||
private readonly listener;
|
||||
constructor(cmdPath: string, cmdArgs: string[], localApi: any);
|
||||
constructor(cmdPath: string, cmdArgs: string[], ...localApis: object[]);
|
||||
start(): Promise<void>;
|
||||
private acceptConn;
|
||||
wait(): Promise<void>;
|
||||
}
|
||||
export declare class ChildIPC extends IPCCommon {
|
||||
constructor(localApi: any);
|
||||
constructor(...localApis: object[]);
|
||||
start(): Promise<void>;
|
||||
wait(): Promise<void>;
|
||||
}
|
||||
|
||||
2
lib/ts/dist/lib.d.ts.map
vendored
2
lib/ts/dist/lib.d.ts.map
vendored
@ -1 +1 @@
|
||||
{"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC;AAOhC,OAAO,EAAC,WAAW,EAAC,MAAM,WAAW,CAAC;AAItC,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,O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I,OAAO,CAAC,IAAI,CAAC;IAUtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAK9B"}
|
||||
2
lib/ts/dist/lib.js.map
vendored
2
lib/ts/dist/lib.js.map
vendored
File diff suppressed because one or more lines are too long
@ -7,7 +7,7 @@ import * as fs from 'node:fs';
|
||||
import * as util from 'node:util';
|
||||
import {QueuedEvent} from 'ts-events';
|
||||
|
||||
const IPC_SOCKET_ARG = '--ipc-socket';
|
||||
const IPC_SOCKET_ARG = 'ipc-socket';
|
||||
|
||||
enum MsgType {
|
||||
Call = 1,
|
||||
@ -38,17 +38,22 @@ interface CallResult {
|
||||
}
|
||||
|
||||
abstract class IPCCommon {
|
||||
protected localApi: any;
|
||||
protected localApis: Record<string, any>;
|
||||
protected socketPath: string;
|
||||
protected conn: net.Socket | null = null;
|
||||
protected nextId: number = 0;
|
||||
protected pendingCalls: Record<number, (result: CallResult) => void> = {};
|
||||
protected errors: QueuedEvent<Error>;
|
||||
|
||||
protected constructor(localApi: any, socketPath: string) {
|
||||
this.localApi = localApi;
|
||||
protected constructor(localApis: object[], socketPath: string) {
|
||||
this.socketPath = socketPath;
|
||||
this.errors = new QueuedEvent();
|
||||
|
||||
this.localApis = {};
|
||||
for (const localApi of localApis) {
|
||||
const className = (localApi as {name: string})['name'];
|
||||
this.localApis[className] = localApi;
|
||||
}
|
||||
}
|
||||
|
||||
protected readConn(): void {
|
||||
@ -89,12 +94,17 @@ abstract class IPCCommon {
|
||||
}
|
||||
|
||||
protected handleCall(msg: CallMessage): void {
|
||||
if (!this.localApi) {
|
||||
this.sendMsg({type: MsgType.Response, id: msg.id, error: 'remote side does not accept ipc calls'});
|
||||
const [endpointName, methodName] = msg.method.split('.');
|
||||
if(!endpointName || !methodName) {
|
||||
this.sendMsg({type: MsgType.Response, id: msg.id, error: `call malformed: ${msg.method}`});
|
||||
return;
|
||||
}
|
||||
|
||||
const method = this.localApi[msg.method];
|
||||
const endpoint = this.localApis[endpointName];
|
||||
if(!endpoint) {
|
||||
this.sendMsg({type: MsgType.Response, id: msg.id, error: `endpoint not found: ${endpointName}`});
|
||||
return;
|
||||
}
|
||||
const method = endpoint[methodName];
|
||||
if (!method || typeof method !== 'function') {
|
||||
this.sendMsg({type: MsgType.Response, id: msg.id, error: `method not found: ${msg.method}`});
|
||||
return;
|
||||
@ -111,7 +121,7 @@ abstract class IPCCommon {
|
||||
}
|
||||
|
||||
try {
|
||||
const result = method.apply(this.localApi, msg.params);
|
||||
const result = method.apply(this.localApis, msg.params);
|
||||
|
||||
if (result instanceof Promise) {
|
||||
result
|
||||
@ -185,13 +195,13 @@ export class ParentIPC extends IPCCommon {
|
||||
private cmd: ChildProcess | null = null;
|
||||
private readonly listener: net.Server;
|
||||
|
||||
constructor(cmdPath: string, cmdArgs: string[], localApi: any) {
|
||||
constructor(cmdPath: string, cmdArgs: string[], ...localApis: object[]) {
|
||||
const socketPath = path.join(os.tmpdir(), `kitten-ipc-${process.pid}.sock`);
|
||||
super(localApi, socketPath);
|
||||
super(localApis, socketPath);
|
||||
|
||||
this.cmdPath = cmdPath;
|
||||
if (cmdArgs.includes(IPC_SOCKET_ARG)) {
|
||||
throw new Error(`you should not use '${IPC_SOCKET_ARG}' argument in your command`);
|
||||
if (cmdArgs.includes(`--${IPC_SOCKET_ARG}`)) {
|
||||
throw new Error(`you should not use '--${IPC_SOCKET_ARG}' argument in your command`);
|
||||
}
|
||||
this.cmdArgs = cmdArgs;
|
||||
|
||||
@ -211,7 +221,7 @@ export class ParentIPC extends IPCCommon {
|
||||
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.on('error', (err) => {
|
||||
@ -260,8 +270,8 @@ export class ParentIPC extends IPCCommon {
|
||||
|
||||
|
||||
export class ChildIPC extends IPCCommon {
|
||||
constructor(localApi: any) {
|
||||
super(localApi, socketPathFromArgs());
|
||||
constructor(...localApis: object[]) {
|
||||
super(localApis, socketPathFromArgs());
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
@ -283,17 +293,19 @@ export class ChildIPC extends IPCCommon {
|
||||
|
||||
|
||||
function socketPathFromArgs(): string {
|
||||
const {values} = util.parseArgs({options: {
|
||||
IPC_SOCKET_ARG: {
|
||||
const {values} = util.parseArgs({
|
||||
options: {
|
||||
[IPC_SOCKET_ARG]: {
|
||||
type: 'string',
|
||||
}
|
||||
}});
|
||||
}
|
||||
});
|
||||
|
||||
if(!values.IPC_SOCKET_ARG) {
|
||||
if (!values[IPC_SOCKET_ARG]) {
|
||||
throw new Error('ipc socket path is missing');
|
||||
}
|
||||
|
||||
return values.IPC_SOCKET_ARG;
|
||||
return values[IPC_SOCKET_ARG];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user