Skip to content

Commit e95fc30

Browse files
author
ionut.stan
committed
Added JSONRPC.Plugins.Client.ProcessStdIOTransport
1 parent 2b81adc commit e95fc30

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const JSONRPC = {};
2+
JSONRPC.ClientPluginBase = require("../../ClientPluginBase");
3+
JSONRPC.Utils = require("../../Utils");
4+
5+
const assert = require("assert");
6+
7+
const ChildProcess = require("child_process");
8+
9+
module.exports =
10+
class ProcessStdIOTransport extends JSONRPC.ClientPluginBase
11+
{
12+
/**
13+
* @param {string} strEndpointCommand
14+
* @param {string} strWorkingDirectoryPath
15+
*/
16+
constructor(strEndpointCommand, strWorkingDirectoryPath)
17+
{
18+
super();
19+
20+
this._strEndpointCommand = strEndpointCommand;
21+
this._strWorkingDirectoryPath = strWorkingDirectoryPath;
22+
}
23+
24+
25+
/**
26+
* Populates the OutgoingRequest class instance (outgoingRequest) with the RAW JSON response and the JSON parsed response object.
27+
*
28+
* @param {JSONRPC.OutgoingRequest} outgoingRequest
29+
*
30+
* @returns {Promise.<null>}
31+
*/
32+
async makeRequest(outgoingRequest)
33+
{
34+
if(outgoingRequest.isMethodCalled)
35+
{
36+
return;
37+
}
38+
39+
outgoingRequest.isMethodCalled = true;
40+
41+
// Without new lines or indent.
42+
outgoingRequest.requestBody = JSON.stringify(outgoingRequest.requestObject);
43+
44+
const objExecOptions = {
45+
cwd: this._strWorkingDirectoryPath,
46+
maxBuffer: 10 * 1024 * 1024
47+
};
48+
49+
const strExePath = this._strEndpointCommand.trim().split(/[\s]+/, 1)[0];
50+
const strArguments = this._strEndpointCommand.substr(strExePath.length).trim();
51+
52+
const child = ChildProcess.spawn(strExePath, [strArguments], objExecOptions);
53+
54+
await new Promise((fnResolve, fnReject) => {
55+
child.on(
56+
"close",
57+
(code) => {
58+
child.stdin.end();
59+
60+
fnResolve(null);
61+
}
62+
);
63+
64+
outgoingRequest.responseBody = "";
65+
child.stdout.on(
66+
"data",
67+
(data) => {
68+
outgoingRequest.responseBody += data;
69+
}
70+
);
71+
72+
child.on(
73+
"error",
74+
(error) => {
75+
fnReject(error);
76+
}
77+
);
78+
79+
child.stdin.setEncoding("utf-8");
80+
child.stdin.write(outgoingRequest.requestBody);
81+
});
82+
}
83+
};
84+

0 commit comments

Comments
 (0)