-
Notifications
You must be signed in to change notification settings - Fork 644
/
ipc.ts
195 lines (155 loc) · 5.54 KB
/
ipc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { fork } from '@ionic/utils-subprocess';
import { ChildProcess } from 'child_process';
import * as Debug from 'debug';
import * as fs from 'fs';
import { ERROR_IPC_UNKNOWN_PROCEDURE, IPCError } from '../errors';
const debug = Debug('ionic:cli-framework:utils:ipc');
export interface RPCRequest<P extends string, A extends any[]> {
type: 'rpc-request';
id: string;
procedure: P;
args: A;
}
export interface RPCResponse<R extends RPCRequest<any, any>, D extends object> {
type: 'rpc-response';
id: string;
procedure: R['procedure'];
request: R;
err?: any;
data: D;
}
export type RPC<P extends string, A extends any[], D extends object> = RPCResponse<RPCRequest<P, A>, D>;
export interface RPCProcessOptions {
readonly name?: string;
readonly timeout?: number;
}
export class RPCProcess {
readonly name: string;
readonly timeout: number;
protected responseProcedures: Map<string, (args: any[]) => Promise<any>> = new Map();
protected proc?: ChildProcess;
constructor({ name = 'unnamed', timeout = 5000 }: RPCProcessOptions = {}) {
this.name = name;
this.timeout = timeout;
}
start(proc: ChildProcess | NodeJS.Process): void {
if (this.proc) {
throw new IPCError('RPC process already started.');
}
const p = proc as ChildProcess;
if (!p.send) {
throw new IPCError('Cannot use proc: `send()` undefined.');
}
this.proc = p;
p.on('message', async (msg: any) => {
if (isRPCRequest(msg)) {
debug('%s: Received RPC request: %O', this.name, msg);
const fn = this.responseProcedures.get(msg.procedure);
let err: any;
let data: any;
if (fn) {
try {
data = await fn(msg.args);
} catch (e) {
err = e;
}
} else {
err = new IPCError(`Unknown procedure: ${msg.procedure}`);
err.code = ERROR_IPC_UNKNOWN_PROCEDURE;
}
const response: RPCResponse<any, object> = { type: 'rpc-response', id: msg.id, procedure: msg.procedure, request: msg, err, data };
if (p.send) {
p.send(response);
debug('%s: Sent RPC response: %O', this.name, response);
} else {
throw new IPCError('Cannot use proc: `send()` undefined.');
}
}
});
p.on('error', err => {
debug('%s: Encountered error with proc: %O', this.name, err);
});
debug('%s: RPC process initiated (pid: %d)', this.name, p.pid);
}
register<R extends RPCResponse<any, object>>(procedure: R['procedure'], fn: (args: R['request']['args']) => Promise<R['data']>): void {
this.responseProcedures.set(procedure, fn);
}
async call<R extends RPCResponse<any, object>>(procedure: R['procedure'], args: R['request']['args']): Promise<R['data']> {
const p = this.proc;
if (!p) {
throw new IPCError('Cannot call procedure: no proc started.');
}
const id = Math.random().toString(16).substring(2, 8);
const request: RPCRequest<any, any> = { type: 'rpc-request', id, procedure, args };
return new Promise<any>((resolve, reject) => {
const timer = setTimeout(() => {
reject(new IPCError(`Timeout of ${this.timeout}ms reached.`));
}, this.timeout);
const messageHandler = (msg: any) => {
if (isRPCResponse(msg) && msg.id === id) {
debug('%s: Received RPC response: %O', this.name, msg);
if (msg.err) {
reject(msg.err);
} else {
resolve(msg.data);
}
p.removeListener('message', messageHandler);
p.removeListener('disconnect', disconnectHandler);
clearTimeout(timer);
}
};
const disconnectHandler = () => {
reject(new IPCError('Unexpected disconnect. Rejecting call!'));
clearTimeout(timer);
};
p.on('message', messageHandler);
p.on('disconnect', disconnectHandler);
if (p.send) {
p.send(request);
debug('%s: Sent RPC request: %O', this.name, request);
} else {
reject(new IPCError('Cannot use proc: `send()` undefined.'));
clearTimeout(timer);
}
});
}
end(): void {
if (!this.proc) {
throw new IPCError(`RPC process not started.`);
}
this.proc.disconnect();
debug('%s: Disconnected', this.name);
}
}
export class RPCHost {
protected rpc: RPCProcess;
constructor(readonly modulePath: string, readonly args: readonly string[]) {
this.rpc = new RPCProcess({ name: 'host' });
}
start(): void {
try {
fs.accessSync(this.modulePath, fs.constants.R_OK);
} catch (e) {
debug('Error during access check: %O', e);
throw new IPCError(`Module not accessible: ${this.modulePath}`);
}
const p = fork(this.modulePath, this.args, { stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });
debug('RPC subprocess forked %o', [this.modulePath, ...this.args]);
this.rpc.start(p);
}
register<R extends RPCResponse<any, object>>(procedure: R['procedure'], fn: (args: R['request']['args']) => Promise<R['data']>): void {
this.rpc.register(procedure, fn);
}
async call<R extends RPCResponse<any, object>>(procedure: R['procedure'], args: R['request']['args']): Promise<R['data']> {
return this.rpc.call(procedure, args);
}
end(): void {
this.rpc.end();
}
}
function isRPCRequest(msg: any): msg is RPCRequest<any, any> {
return msg && msg.type === 'rpc-request' && typeof msg.procedure === 'string';
}
function isRPCResponse(msg: any): msg is RPCResponse<any, object> {
return msg && msg.type === 'rpc-response' && typeof msg.procedure === 'string';
}