@x-oasis/async-call-rpc-misc 0.1.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/async-call-rpc-misc.cjs.development.js +221 -0
- package/dist/async-call-rpc-misc.cjs.development.js.map +1 -0
- package/dist/async-call-rpc-misc.cjs.production.min.js +2 -0
- package/dist/async-call-rpc-misc.cjs.production.min.js.map +1 -0
- package/dist/async-call-rpc-misc.esm.js +216 -0
- package/dist/async-call-rpc-misc.esm.js.map +1 -0
- package/dist/buffer/DataBuffer.d.ts +2 -0
- package/dist/buffer/ReadBuffer.d.ts +12 -0
- package/dist/buffer/WriteBuffer.d.ts +3 -0
- package/dist/buffer/index.d.ts +3 -0
- package/dist/channel-protocol/AbstractChannelProtocol.d.ts +51 -0
- package/dist/channel-protocol/Channel.d.ts +16 -0
- package/dist/channel-protocol/DeferredMessageChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/IPCMainChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/IPCMainGlobalChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/IPCRendererChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/IPCRendererMessageChannelProtocol.d.ts +8 -0
- package/dist/channel-protocol/MessageChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/ProcessChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/ProcessClientChannelProtocol.d.ts +7 -0
- package/dist/channel-protocol/middlewares/buffer.d.ts +18 -0
- package/dist/channel-protocol/middlewares/handleDisconnectedRequest.d.ts +7 -0
- package/dist/channel-protocol/middlewares/handlePortRequest.d.ts +3 -0
- package/dist/channel-protocol/middlewares/handleRequest.d.ts +3 -0
- package/dist/channel-protocol/middlewares/handleRequestUtils.d.ts +10 -0
- package/dist/channel-protocol/middlewares/handleResponse.d.ts +3 -0
- package/dist/channel-protocol/middlewares/index.d.ts +10 -0
- package/dist/channel-protocol/middlewares/logger.d.ts +4 -0
- package/dist/channel-protocol/middlewares/normalize.d.ts +12 -0
- package/dist/channel-protocol/middlewares/prepareRequestData.d.ts +28 -0
- package/dist/channel-protocol/middlewares/sendRequest.d.ts +6 -0
- package/dist/channel-protocol/middlewares/updateSeqInfo.d.ts +6 -0
- package/dist/channel-protocol/middlewares/utils.d.ts +3 -0
- package/dist/common.d.ts +4 -0
- package/dist/error.d.ts +12 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +8 -0
- package/dist/rpc-service/ProxyRPCClient.d.ts +9 -0
- package/dist/rpc-service/RPCServiceHost.d.ts +11 -0
- package/dist/rpc-service/RPCServiceProvider.d.ts +9 -0
- package/dist/types/buffer.d.ts +4 -0
- package/dist/types/channel.d.ts +12 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/messageChannel.d.ts +76 -0
- package/dist/types/middleware.d.ts +44 -0
- package/dist/types/proxyChannel.d.ts +3 -0
- package/dist/types/proxyService.d.ts +10 -0
- package/dist/types/rpc.d.ts +31 -0
- package/dist/types/rpcProtocol.d.ts +22 -0
- package/package.json +38 -0
- package/src/buffer/DataBuffer.ts +1 -0
- package/src/buffer/ReadBuffer.ts +41 -0
- package/src/buffer/WriteBuffer.ts +5 -0
- package/src/buffer/index.ts +3 -0
- package/src/channel-protocol/AbstractChannelProtocol.ts +204 -0
- package/src/channel-protocol/Channel.ts +37 -0
- package/src/channel-protocol/DeferredMessageChannelProtocol.ts +85 -0
- package/src/channel-protocol/IPCMainChannelProtocol.ts +65 -0
- package/src/channel-protocol/IPCMainGlobalChannelProtocol.ts +66 -0
- package/src/channel-protocol/IPCRendererChannelProtocol.ts +81 -0
- package/src/channel-protocol/IPCRendererMessageChannelProtocol.ts +79 -0
- package/src/channel-protocol/MessageChannelProtocol.ts +63 -0
- package/src/channel-protocol/ProcessChannelProtocol.ts +60 -0
- package/src/channel-protocol/ProcessClientChannelProtocol.ts +59 -0
- package/src/channel-protocol/middlewares/buffer.ts +33 -0
- package/src/channel-protocol/middlewares/handleDisconnectedRequest.ts +30 -0
- package/src/channel-protocol/middlewares/handlePortRequest.ts +118 -0
- package/src/channel-protocol/middlewares/handleRequest.ts +163 -0
- package/src/channel-protocol/middlewares/handleRequestUtils.ts +42 -0
- package/src/channel-protocol/middlewares/handleResponse.ts +38 -0
- package/src/channel-protocol/middlewares/index.ts +11 -0
- package/src/channel-protocol/middlewares/logger.ts +22 -0
- package/src/channel-protocol/middlewares/normalize.ts +28 -0
- package/src/channel-protocol/middlewares/prepareRequestData.ts +137 -0
- package/src/channel-protocol/middlewares/sendRequest.ts +15 -0
- package/src/channel-protocol/middlewares/updateSeqInfo.ts +27 -0
- package/src/channel-protocol/middlewares/utils.ts +67 -0
- package/src/common.ts +20 -0
- package/src/error.ts +32 -0
- package/src/index.ts +5 -0
- package/src/rpc-service/ProxyRPCClient.ts +82 -0
- package/src/rpc-service/RPCServiceHost.ts +35 -0
- package/src/rpc-service/RPCServiceProvider.ts +22 -0
- package/src/types/buffer.ts +5 -0
- package/src/types/channel.ts +24 -0
- package/src/types/index.ts +6 -0
- package/src/types/messageChannel.ts +134 -0
- package/src/types/middleware.ts +53 -0
- package/src/types/proxyChannel.ts +3 -0
- package/src/types/proxyService.ts +16 -0
- package/src/types/rpc.ts +61 -0
- package/src/types/rpcProtocol.ts +24 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
2
|
+
import { MainPort, MessageChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
handleRequest,
|
|
7
|
+
handleResponse,
|
|
8
|
+
updateSeqInfo,
|
|
9
|
+
sendRequest,
|
|
10
|
+
prepareNormalData,
|
|
11
|
+
handleDisconnectedRequest,
|
|
12
|
+
normalizeMessageChannelRawMessage,
|
|
13
|
+
} from './middlewares';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
|
|
16
|
+
export default class MessageChannelProtocol extends AbstractChannelProtocol {
|
|
17
|
+
private readonly port: MainPort;
|
|
18
|
+
|
|
19
|
+
constructor(props: MessageChannelProtocolProps) {
|
|
20
|
+
super(props);
|
|
21
|
+
const { clientMiddlewares, senderMiddlewares, port } = props;
|
|
22
|
+
this.port = port;
|
|
23
|
+
|
|
24
|
+
this.applySendMiddleware(
|
|
25
|
+
[].concat(
|
|
26
|
+
prepareNormalData,
|
|
27
|
+
updateSeqInfo,
|
|
28
|
+
handleDisconnectedRequest,
|
|
29
|
+
serialize,
|
|
30
|
+
senderMiddlewares,
|
|
31
|
+
sendRequest
|
|
32
|
+
)
|
|
33
|
+
);
|
|
34
|
+
this.applyOnMessageMiddleware(
|
|
35
|
+
[].concat(
|
|
36
|
+
normalizeMessageChannelRawMessage,
|
|
37
|
+
deserialize,
|
|
38
|
+
clientMiddlewares,
|
|
39
|
+
handleRequest,
|
|
40
|
+
handleResponse
|
|
41
|
+
)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
this.channel = new Channel({
|
|
45
|
+
send: (...args) => this.port.postMessage(...args),
|
|
46
|
+
initListener: (emitter) => {
|
|
47
|
+
emitter.register('message');
|
|
48
|
+
this.port.on('message', (...args: any[]) => {
|
|
49
|
+
this.onMessage(...args);
|
|
50
|
+
emitter.getEvent('message').fire(...args);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
this.port.start();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
disconnect() {
|
|
59
|
+
this.dispose();
|
|
60
|
+
this.port.close();
|
|
61
|
+
super.disconnect();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { UtilityProcess } from 'electron';
|
|
2
|
+
|
|
3
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
4
|
+
import { ProcessChannelProtocolProps } from '../types';
|
|
5
|
+
import {
|
|
6
|
+
serialize,
|
|
7
|
+
deserialize,
|
|
8
|
+
handleRequest,
|
|
9
|
+
handleResponse,
|
|
10
|
+
updateSeqInfo,
|
|
11
|
+
sendRequest,
|
|
12
|
+
prepareHostPortData,
|
|
13
|
+
processClientRawMessage,
|
|
14
|
+
handleDisconnectedRequest,
|
|
15
|
+
} from './middlewares';
|
|
16
|
+
import { Channel } from './Channel';
|
|
17
|
+
|
|
18
|
+
export default class ProcessChannelProtocol extends AbstractChannelProtocol {
|
|
19
|
+
private readonly process: UtilityProcess;
|
|
20
|
+
|
|
21
|
+
constructor(props: ProcessChannelProtocolProps) {
|
|
22
|
+
super(props);
|
|
23
|
+
const { clientMiddlewares, senderMiddlewares, process } = props;
|
|
24
|
+
this.process = process;
|
|
25
|
+
|
|
26
|
+
this.applySendMiddleware(
|
|
27
|
+
[].concat(
|
|
28
|
+
prepareHostPortData,
|
|
29
|
+
updateSeqInfo,
|
|
30
|
+
handleDisconnectedRequest,
|
|
31
|
+
serialize,
|
|
32
|
+
senderMiddlewares,
|
|
33
|
+
sendRequest
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
this.applyOnMessageMiddleware(
|
|
37
|
+
[].concat(
|
|
38
|
+
processClientRawMessage,
|
|
39
|
+
deserialize,
|
|
40
|
+
clientMiddlewares,
|
|
41
|
+
handleRequest,
|
|
42
|
+
handleResponse
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
this.channel = new Channel({
|
|
47
|
+
send: (...args) => this.process.postMessage(...args),
|
|
48
|
+
initListener: (emitter) => {
|
|
49
|
+
this.process.on('message', (...args: any[]) => {
|
|
50
|
+
this.onMessage(...args);
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
disconnect() {
|
|
57
|
+
this.dispose();
|
|
58
|
+
super.disconnect();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import AbstractChannelProtocol from './AbstractChannelProtocol';
|
|
2
|
+
import { MainPort, MessageChannelProtocolProps } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
serialize,
|
|
5
|
+
deserialize,
|
|
6
|
+
handleRequest,
|
|
7
|
+
handleResponse,
|
|
8
|
+
updateSeqInfo,
|
|
9
|
+
sendRequest,
|
|
10
|
+
prepareNormalData,
|
|
11
|
+
handleDisconnectedRequest,
|
|
12
|
+
normalizeMessageChannelRawMessage,
|
|
13
|
+
} from './middlewares';
|
|
14
|
+
import { Channel } from './Channel';
|
|
15
|
+
|
|
16
|
+
export default class ProcessClientChannelProtocol extends AbstractChannelProtocol {
|
|
17
|
+
private readonly port: MainPort;
|
|
18
|
+
|
|
19
|
+
constructor(props: MessageChannelProtocolProps) {
|
|
20
|
+
super(props);
|
|
21
|
+
const { clientMiddlewares, senderMiddlewares, port } = props;
|
|
22
|
+
this.port = port;
|
|
23
|
+
this.applySendMiddleware(
|
|
24
|
+
[].concat(
|
|
25
|
+
prepareNormalData,
|
|
26
|
+
updateSeqInfo,
|
|
27
|
+
handleDisconnectedRequest,
|
|
28
|
+
serialize,
|
|
29
|
+
senderMiddlewares,
|
|
30
|
+
sendRequest
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
this.applyOnMessageMiddleware(
|
|
34
|
+
[].concat(
|
|
35
|
+
normalizeMessageChannelRawMessage,
|
|
36
|
+
deserialize,
|
|
37
|
+
clientMiddlewares,
|
|
38
|
+
handleRequest,
|
|
39
|
+
handleResponse
|
|
40
|
+
)
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
this.channel = new Channel({
|
|
44
|
+
send: (...args) => this.port.postMessage(...args),
|
|
45
|
+
initListener: (emitter) => {
|
|
46
|
+
emitter.register('message');
|
|
47
|
+
this.port.on('message', (...args: any[]) => {
|
|
48
|
+
this.onMessage(...args);
|
|
49
|
+
emitter.getEvent('message').fire(...args);
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
disconnect() {
|
|
56
|
+
this.dispose();
|
|
57
|
+
super.disconnect();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NormalizedRawMessageOutput,
|
|
3
|
+
SenderMiddlewareOutput,
|
|
4
|
+
SendMiddlewareLifecycle,
|
|
5
|
+
} from '../../types';
|
|
6
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
7
|
+
|
|
8
|
+
export const serialize = (channel: AbstractChannelProtocol) => {
|
|
9
|
+
const fn = (value: SenderMiddlewareOutput) => ({
|
|
10
|
+
...value,
|
|
11
|
+
data: channel.writeBuffer.encode(value.data),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
fn.lifecycle = SendMiddlewareLifecycle.DataOperation;
|
|
15
|
+
return fn;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const deserialize =
|
|
19
|
+
(channel: AbstractChannelProtocol) => (value: NormalizedRawMessageOutput) => {
|
|
20
|
+
const { data } = value;
|
|
21
|
+
let decoded = data;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
decoded = channel.readBuffer.decode(data);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error('[decode error]', data, err);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
...value,
|
|
31
|
+
data: decoded,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { SenderMiddlewareOutput, SendMiddlewareLifecycle } from '../../types';
|
|
2
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
3
|
+
|
|
4
|
+
export const handleDisconnectedRequest = (
|
|
5
|
+
protocol: AbstractChannelProtocol
|
|
6
|
+
) => {
|
|
7
|
+
const fn = (value: SenderMiddlewareOutput) => {
|
|
8
|
+
const isConnected = protocol.isConnected();
|
|
9
|
+
|
|
10
|
+
if (!isConnected && !value.isOptionsRequest) {
|
|
11
|
+
protocol.addPendingSendEntry({
|
|
12
|
+
fnName: fn.displayName,
|
|
13
|
+
lifecycle: SendMiddlewareLifecycle.Prepare,
|
|
14
|
+
...value,
|
|
15
|
+
middlewareContext: {
|
|
16
|
+
...value.middlewareContext,
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
value.middlewareContext.minLifecycle = SendMiddlewareLifecycle.Aborted;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
fn.displayName = 'handleDisconnectedRequest';
|
|
27
|
+
fn.lifecycle = SendMiddlewareLifecycle.Prepare;
|
|
28
|
+
|
|
29
|
+
return fn;
|
|
30
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import isPromise from '@x-oasis/is-promise';
|
|
2
|
+
import { ResponseType, DeserializedMessageOutput } from '../../types';
|
|
3
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
4
|
+
import { isEventMethod } from '../../common';
|
|
5
|
+
|
|
6
|
+
export const handlePortRequest =
|
|
7
|
+
(protocol: AbstractChannelProtocol) =>
|
|
8
|
+
(message: DeserializedMessageOutput) => {
|
|
9
|
+
const serviceHost = protocol.serviceHost;
|
|
10
|
+
|
|
11
|
+
const { data, event: messageEvent } = message;
|
|
12
|
+
const header = data[0];
|
|
13
|
+
const channelName = header[4];
|
|
14
|
+
|
|
15
|
+
const body = data[1];
|
|
16
|
+
const type = header[0] as any;
|
|
17
|
+
|
|
18
|
+
if (Object.values(ResponseType).includes(type)) {
|
|
19
|
+
return message;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const seqId = header[1];
|
|
23
|
+
const requestPath = header[2];
|
|
24
|
+
const fnName = header[3];
|
|
25
|
+
const args = body[0];
|
|
26
|
+
|
|
27
|
+
if (serviceHost) {
|
|
28
|
+
if (isEventMethod(fnName)) {
|
|
29
|
+
// const event = serviceHost.getHandler(requestPath, fnName)
|
|
30
|
+
// const fn = (...args: any[]) => {
|
|
31
|
+
// const responseHeader = [ResponseType.ReturnSuccess, seqId]
|
|
32
|
+
// let responseBody = []
|
|
33
|
+
// let sendData = null
|
|
34
|
+
// try {
|
|
35
|
+
// responseBody = args
|
|
36
|
+
// sendData = protocol.writeBuffer.encode([responseHeader, responseBody])
|
|
37
|
+
// } catch (err) {
|
|
38
|
+
// sendData = protocol.writeBuffer.encode([responseHeader, []])
|
|
39
|
+
// console.error(`[handleRequest sendReply encode error ] ${requestPath} ${fnName}`, err)
|
|
40
|
+
// }
|
|
41
|
+
|
|
42
|
+
// // TODO: temp; main <=> project renderer...
|
|
43
|
+
// if (messageEvent?.sender) {
|
|
44
|
+
// messageEvent.sender.send(channelName, sendData)
|
|
45
|
+
// return
|
|
46
|
+
// }
|
|
47
|
+
|
|
48
|
+
// protocol.sendReply(sendData)
|
|
49
|
+
// }
|
|
50
|
+
|
|
51
|
+
// event(fn)
|
|
52
|
+
return message;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const handler = serviceHost.getHandler(requestPath, fnName);
|
|
56
|
+
|
|
57
|
+
// todo
|
|
58
|
+
const result = Promise.resolve(handler?.(args));
|
|
59
|
+
|
|
60
|
+
if (handler && isPromise(result)) {
|
|
61
|
+
result.then(
|
|
62
|
+
(port: any) => {
|
|
63
|
+
const responseHeader = [ResponseType.PortSuccess, seqId];
|
|
64
|
+
let responseBody: any[] = [];
|
|
65
|
+
let sendData = null;
|
|
66
|
+
try {
|
|
67
|
+
responseBody = [];
|
|
68
|
+
sendData = protocol.writeBuffer.encode([
|
|
69
|
+
responseHeader,
|
|
70
|
+
responseBody,
|
|
71
|
+
]);
|
|
72
|
+
} catch (err) {
|
|
73
|
+
sendData = protocol.writeBuffer.encode([responseHeader, []]);
|
|
74
|
+
console.error(
|
|
75
|
+
`[handleRequest sendReply encode error ] ${requestPath} ${fnName}`,
|
|
76
|
+
err
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// TODO: temp; main <=> project renderer...
|
|
81
|
+
if (messageEvent?.sender) {
|
|
82
|
+
messageEvent.sender.postMessage(channelName, sendData, [port]);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protocol.sendReply(sendData);
|
|
87
|
+
},
|
|
88
|
+
(err: Error) => {
|
|
89
|
+
const responseHeader = [ResponseType.ReturnFail, seqId];
|
|
90
|
+
const responseBody = [
|
|
91
|
+
{
|
|
92
|
+
message: err.message,
|
|
93
|
+
name: err.name,
|
|
94
|
+
// eslint-disable-next-line
|
|
95
|
+
stack: err.stack
|
|
96
|
+
? err.stack.split
|
|
97
|
+
? err.stack.split('\n')
|
|
98
|
+
: err.stack
|
|
99
|
+
: undefined,
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
if (messageEvent?.sender) {
|
|
104
|
+
messageEvent.sender.send(
|
|
105
|
+
protocol.channelName,
|
|
106
|
+
protocol.writeBuffer.encode([responseHeader, responseBody])
|
|
107
|
+
);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protocol.sendReply(
|
|
112
|
+
protocol.writeBuffer.encode([responseHeader, responseBody])
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import isPromise from '@x-oasis/is-promise';
|
|
2
|
+
import { ResponseType, DeserializedMessageOutput } from '../../types';
|
|
3
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
4
|
+
import {
|
|
5
|
+
isEventMethod,
|
|
6
|
+
isAssignPassingPortMethod,
|
|
7
|
+
isAcquirePortMethod,
|
|
8
|
+
} from '../../common';
|
|
9
|
+
import { handleAcquirePort } from './handleRequestUtils';
|
|
10
|
+
|
|
11
|
+
export const handleRequest =
|
|
12
|
+
(protocol: AbstractChannelProtocol) =>
|
|
13
|
+
(message: DeserializedMessageOutput) => {
|
|
14
|
+
const serviceHost = protocol.serviceHost;
|
|
15
|
+
|
|
16
|
+
const { data, event: messageEvent, ports } = message;
|
|
17
|
+
const header = data[0];
|
|
18
|
+
|
|
19
|
+
// TODO: protocol.channelName will cause error, check `acquirePortMain.ts#171`
|
|
20
|
+
const channelName = header[4] || protocol.channelName;
|
|
21
|
+
|
|
22
|
+
const body = data[1];
|
|
23
|
+
const type = header[0] as any;
|
|
24
|
+
|
|
25
|
+
if (Object.values(ResponseType).includes(type)) {
|
|
26
|
+
return message;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const seqId = header[1];
|
|
30
|
+
const requestPath = header[2];
|
|
31
|
+
const fnName = header[3];
|
|
32
|
+
const args = body[0];
|
|
33
|
+
|
|
34
|
+
if (serviceHost) {
|
|
35
|
+
if (isEventMethod(fnName)) {
|
|
36
|
+
const event = serviceHost.getHandler(requestPath, fnName);
|
|
37
|
+
|
|
38
|
+
const fn = (...args: any[]) => {
|
|
39
|
+
const responseHeader = [ResponseType.ReturnSuccess, seqId];
|
|
40
|
+
let responseBody = [];
|
|
41
|
+
let sendData = null;
|
|
42
|
+
try {
|
|
43
|
+
responseBody = args;
|
|
44
|
+
sendData = protocol.writeBuffer.encode([
|
|
45
|
+
responseHeader,
|
|
46
|
+
responseBody,
|
|
47
|
+
]);
|
|
48
|
+
} catch (err) {
|
|
49
|
+
sendData = protocol.writeBuffer.encode([responseHeader, []]);
|
|
50
|
+
console.error(
|
|
51
|
+
`[handleRequest sendReply encode error ] ${requestPath} ${fnName}`,
|
|
52
|
+
err
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// TODO: temp; main <=> project renderer...
|
|
57
|
+
if (messageEvent?.sender) {
|
|
58
|
+
messageEvent.sender.send(channelName, sendData);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protocol.sendReply(sendData);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
event?.(fn);
|
|
66
|
+
return message;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* AssignPassingPort
|
|
71
|
+
*/
|
|
72
|
+
if (isAssignPassingPortMethod(fnName)) {
|
|
73
|
+
const handler = serviceHost.getHandler(requestPath, fnName);
|
|
74
|
+
|
|
75
|
+
if (handler) args ? handler(args, ports?.[0]) : handler(ports?.[0]);
|
|
76
|
+
// no need send reply
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (isAcquirePortMethod(fnName)) {
|
|
81
|
+
const result = handleAcquirePort({
|
|
82
|
+
protocol,
|
|
83
|
+
serviceHost,
|
|
84
|
+
fnName,
|
|
85
|
+
seqId,
|
|
86
|
+
args,
|
|
87
|
+
requestPath,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// 目前简单处理,只有port的时候才返回,其实是需要返回PortFail的;进入
|
|
91
|
+
// client的catch
|
|
92
|
+
if (result) {
|
|
93
|
+
protocol.sendReply(...result);
|
|
94
|
+
}
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const handler = serviceHost.getHandler(requestPath, fnName);
|
|
99
|
+
|
|
100
|
+
const _result = handler?.(args);
|
|
101
|
+
|
|
102
|
+
// todo
|
|
103
|
+
const result = Promise.resolve(_result);
|
|
104
|
+
|
|
105
|
+
if (isPromise(result)) {
|
|
106
|
+
result.then(
|
|
107
|
+
(response: any) => {
|
|
108
|
+
const responseHeader = [ResponseType.ReturnSuccess, seqId];
|
|
109
|
+
let responseBody = [];
|
|
110
|
+
let sendData = null;
|
|
111
|
+
try {
|
|
112
|
+
responseBody = [response];
|
|
113
|
+
sendData = protocol.writeBuffer.encode([
|
|
114
|
+
responseHeader,
|
|
115
|
+
responseBody,
|
|
116
|
+
]);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
sendData = protocol.writeBuffer.encode([responseHeader, []]);
|
|
119
|
+
console.error(
|
|
120
|
+
`[handleRequest sendReply encode error ] ${requestPath} ${fnName}`,
|
|
121
|
+
err
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// TODO: temp; main <=> project renderer...
|
|
126
|
+
if (messageEvent?.sender) {
|
|
127
|
+
messageEvent.sender.send(channelName, sendData);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
protocol.sendReply(sendData);
|
|
132
|
+
},
|
|
133
|
+
(err: Error) => {
|
|
134
|
+
const responseHeader = [ResponseType.ReturnFail, seqId];
|
|
135
|
+
const responseBody = [
|
|
136
|
+
{
|
|
137
|
+
message: err.message,
|
|
138
|
+
name: err.name,
|
|
139
|
+
// eslint-disable-next-line
|
|
140
|
+
stack: err.stack
|
|
141
|
+
? err.stack.split
|
|
142
|
+
? err.stack.split('\n')
|
|
143
|
+
: err.stack
|
|
144
|
+
: undefined,
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
if (messageEvent?.sender) {
|
|
149
|
+
messageEvent.sender.send(
|
|
150
|
+
protocol.channelName,
|
|
151
|
+
protocol.writeBuffer.encode([responseHeader, responseBody])
|
|
152
|
+
);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
protocol.sendReply(
|
|
157
|
+
protocol.writeBuffer.encode([responseHeader, responseBody])
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import RPCServiceHost from '../../rpc-service/RPCServiceHost';
|
|
2
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
3
|
+
import { ResponseType } from '../../types';
|
|
4
|
+
|
|
5
|
+
export const handleAcquirePort = (props: {
|
|
6
|
+
protocol: AbstractChannelProtocol;
|
|
7
|
+
serviceHost: RPCServiceHost;
|
|
8
|
+
requestPath: string;
|
|
9
|
+
fnName: string;
|
|
10
|
+
seqId: string;
|
|
11
|
+
args: any[];
|
|
12
|
+
}) => {
|
|
13
|
+
const { serviceHost, requestPath, fnName, protocol, seqId, args } = props;
|
|
14
|
+
|
|
15
|
+
const handler = serviceHost.getHandler(requestPath, fnName);
|
|
16
|
+
|
|
17
|
+
const port = handler?.(args);
|
|
18
|
+
|
|
19
|
+
// 比如port process它监听的是message;你用process创建一个ProcessChannelProtocol
|
|
20
|
+
// 都会触发监听,这个时候并不一定有值返回,其实正常是否考虑将on message这种
|
|
21
|
+
// emitter收敛了,即使你new了,但其实是一个实例
|
|
22
|
+
if (!port) {
|
|
23
|
+
// console.error('may trigger message listener', protocol)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let responseHeader = [ResponseType.PortSuccess, seqId];
|
|
27
|
+
let responseBody: any[] = [];
|
|
28
|
+
let sendData = null;
|
|
29
|
+
try {
|
|
30
|
+
responseBody = [];
|
|
31
|
+
sendData = protocol.writeBuffer.encode([responseHeader, responseBody]);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
responseHeader = [ResponseType.PortFail, seqId];
|
|
34
|
+
sendData = protocol.writeBuffer.encode([responseHeader, []]);
|
|
35
|
+
console.error(
|
|
36
|
+
`[handleRequest sendReply encode error ] ${requestPath} ${fnName}`,
|
|
37
|
+
err
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return port ? [sendData, [port]] : null;
|
|
42
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RequestType,
|
|
3
|
+
ResponseType,
|
|
4
|
+
DeserializedMessageOutput,
|
|
5
|
+
} from '../../types';
|
|
6
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
7
|
+
|
|
8
|
+
export const handleResponse =
|
|
9
|
+
(protocol: AbstractChannelProtocol) =>
|
|
10
|
+
(message: DeserializedMessageOutput) => {
|
|
11
|
+
if (!message) return message;
|
|
12
|
+
const { data } = message;
|
|
13
|
+
const header = data[0];
|
|
14
|
+
const body = data[1];
|
|
15
|
+
const type = header[0] as any;
|
|
16
|
+
|
|
17
|
+
if (Object.values(RequestType).includes(type)) {
|
|
18
|
+
return message;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const seqId = header[1];
|
|
22
|
+
|
|
23
|
+
const findDefer = protocol.ongoingRequests.get(`${seqId}`);
|
|
24
|
+
if (findDefer) {
|
|
25
|
+
protocol.ongoingRequests.delete(`${seqId}`);
|
|
26
|
+
if (type === ResponseType.PortSuccess) {
|
|
27
|
+
findDefer.resolve(message.ports[0]);
|
|
28
|
+
} else if (type === ResponseType.ReturnFail) {
|
|
29
|
+
findDefer.reject(body[0]);
|
|
30
|
+
} else findDefer.resolve(body[0]);
|
|
31
|
+
} else {
|
|
32
|
+
const findListener = protocol.requestEvents.get(`${seqId}`);
|
|
33
|
+
|
|
34
|
+
if (findListener) {
|
|
35
|
+
findListener(...body);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './buffer';
|
|
2
|
+
export * from './handleRequest';
|
|
3
|
+
export * from './normalize';
|
|
4
|
+
export * from './handleResponse';
|
|
5
|
+
export * from './prepareRequestData';
|
|
6
|
+
export * from './updateSeqInfo';
|
|
7
|
+
export * from './handlePortRequest';
|
|
8
|
+
export * from './sendRequest';
|
|
9
|
+
export * from './handleDisconnectedRequest';
|
|
10
|
+
|
|
11
|
+
export { runMiddlewares } from './utils';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import AbstractChannelProtocol from '../AbstractChannelProtocol';
|
|
2
|
+
import { NormalizedRawMessageOutput } from '../../types';
|
|
3
|
+
|
|
4
|
+
export const createSenderLogger =
|
|
5
|
+
(logService: any) =>
|
|
6
|
+
(channel: AbstractChannelProtocol) =>
|
|
7
|
+
(value: NormalizedRawMessageOutput) => {
|
|
8
|
+
logService.info(`${channel.masterProcessName} send message`, value.data);
|
|
9
|
+
return value;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const createClientLogger =
|
|
13
|
+
(logService: any) =>
|
|
14
|
+
(channel: AbstractChannelProtocol) =>
|
|
15
|
+
(value: NormalizedRawMessageOutput) => {
|
|
16
|
+
const { data } = value;
|
|
17
|
+
logService.info(
|
|
18
|
+
`${channel.masterProcessName} receive message from ${channel.description}`,
|
|
19
|
+
data
|
|
20
|
+
);
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NormalizedRawMessageOutput } from '../../types';
|
|
2
|
+
|
|
3
|
+
export const normalizeMessageChannelRawMessage =
|
|
4
|
+
() =>
|
|
5
|
+
(event: MessageEvent): NormalizedRawMessageOutput => {
|
|
6
|
+
return {
|
|
7
|
+
event,
|
|
8
|
+
data: event.data,
|
|
9
|
+
ports: event.ports || [],
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const normalizeIPCChannelRawMessage =
|
|
14
|
+
() => (event: MessageEvent, data: string) => {
|
|
15
|
+
return {
|
|
16
|
+
event,
|
|
17
|
+
data,
|
|
18
|
+
ports: event.ports || [],
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const processClientRawMessage = () => (data: string) => {
|
|
23
|
+
return {
|
|
24
|
+
event: null,
|
|
25
|
+
data,
|
|
26
|
+
ports: [],
|
|
27
|
+
};
|
|
28
|
+
};
|