Skip to content

Commit

Permalink
write reponse to binary file
Browse files Browse the repository at this point in the history
avoid json parsing the actual response content

fixing error

toggle debugMode to false
  • Loading branch information
c3wenjiaowang committed Jun 27, 2023
1 parent dd0b955 commit a88ced4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
29 changes: 19 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ function readResponseFile(filePath) {
throw new Error("Response file waiting to read for > 30s in main thread in sync-rpc.");
}
}
const res = fs.readFileSync(filePath);
const contentPath = filePath.split(".")[0];
const content = fs.readFileSync(contentPath);
const res = JSON.parse(fs.readFileSync(filePath));
if (res.s == true) {
res.v.b = content.toString('utf-8');
} else {
res.v = JSON.parse(content.toString('utf-8'));
}
fs.unlinkSync(contentPath);
fs.unlinkSync(filePath);
return res;
}
Expand Down Expand Up @@ -108,10 +116,11 @@ function start() {
if (!spawnSync) {
throw new Error(
'Sync-request requires node version 0.12 or later. If you need to use it with an older version of node\n' +
'you can `npm install sync-request@2.2.0`, which was the last version to support older versions of node.'
'you can `npm install sync-request@2.2.0`, which was the last version to support older versions of node.'
);
}
const port = findPort();
// '--inspect-brk',
const p = spawn(process.execPath, [require.resolve('./worker'), port, ...FLAGS], {
stdio: debugMode ? ['ignore', outputFile, errorFile] : 'inherit',
windowsHide: true,
Expand Down Expand Up @@ -148,7 +157,7 @@ function findPort() {
if (findPortResult.status !== 0) {
throw new Error(
findPortResult.stderr.toString() ||
'find port exited with code ' + findPortResult.status
'find port exited with code ' + findPortResult.status
);
}
const portString = findPortResult.stdout.toString('utf8').trim();
Expand All @@ -172,8 +181,8 @@ function waitForAlive(port, delay) {
if (response !== 'pong') {
throw new Error(
'Timed out waiting for sync-rpc server to start (it should respond with "pong" when sent "ping"):\n\n' +
'\n' +
response
'\n' +
response
);
}
}
Expand All @@ -185,10 +194,10 @@ function nativeNC(port, input) {
let res;
try {
res = spawnSync(`nc`, [host, port], {
input: input,
maxBuffer: Infinity,
windowsHide: true,
});
input: input,
maxBuffer: Infinity,
windowsHide: true,
});
} catch (e) {
debugMode && logToFile(`Received error: ${e.message}`);
}
Expand Down Expand Up @@ -259,7 +268,7 @@ function sendMessage(input) {
debugMode && logToFile(`${channel} received response #${index[0]} at client.`, localClientLog);
}
try {
return JSON.parse(spawnSyncCall ? res.stdout.toString('utf8') : res.toString('utf8'));
return spawnSyncCall ? JSON.parse(res.stdout.toString('utf8')) : res;
} catch (ex) {
if (spawnSyncCall) {
if (res.error) {
Expand Down
21 changes: 20 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ function writeResponseToFile(filePath, response) {
debugMode && console.log(`Changed response file to readonly.`);
}

function writeResponseContent(filePath, content) {
const startTime = Date.now();
while (!responseFileReadyForWriting(filePath)) {
if (timeLimitExceeded(startTime)) {
fs.unlinkSync(filePath);
writeResponseContent(filePath, JSON.stringify({s: false, v: {code: -1, message: "Response content file waiting to write for > 30s in worker thread in sync-rpc."}}));
}
}
fs.writeFileSync(filePath, content, {encoding: 'binary'});
debugMode && console.log(`Written content to file.`);
fs.chmodSync(filePath, '444');
debugMode && console.log(`Changed content file to readonly.`);
}

const INIT = 1;
const CALL = 0;
const modules = [];
Expand Down Expand Up @@ -74,6 +88,9 @@ const server = net.createServer({allowHalfOpen: true}, c => {
if (req.client != 'ready') {
respond({s: true, v: response});
} else {
const responseContentPath = req.responseFilePath.split('.')[0];
writeResponseContent(responseContentPath, response.b);
response.b = responseContentPath;
writeResponseToFile(req.responseFilePath, JSON.stringify({s: true, v: response}));
}
debugMode && console.log(`sent response #${index[0]} to local client.`);
Expand All @@ -83,7 +100,9 @@ const server = net.createServer({allowHalfOpen: true}, c => {
if (req.client != 'ready') {
respond({s: false, v: {code: err.code, message: err.message}});
} else {
writeResponseToFile(req.responseFilePath, JSON.stringify({s: false, v: {code: err.code, message: err.message}}));
const responseContentPath = req.responseFilePath.split('.')[0];
writeResponseContent(responseContentPath, JSON.stringify({code: err.code, message: err.message}));
writeResponseToFile(req.responseFilePath, JSON.stringify({s: false, v: responseContentPath}));
}
}
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sync-rpc",
"version": "1.3.7",
"version": "1.3.6",
"main": "lib/index.js",
"description": "Run asynchronous commands synchronously by putting them in a separate process",
"keywords": [],
Expand Down

0 comments on commit a88ced4

Please sign in to comment.