Skip to content

Commit

Permalink
Revert "write reponse to binary file"
Browse files Browse the repository at this point in the history
This reverts commit a88ced4.
  • Loading branch information
c3wenjiaowang committed Jun 27, 2023
1 parent a88ced4 commit 1c9d690
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 40 deletions.
29 changes: 10 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,7 @@ function readResponseFile(filePath) {
throw new Error("Response file waiting to read for > 30s in main thread in sync-rpc.");
}
}
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);
const res = fs.readFileSync(filePath);
fs.unlinkSync(filePath);
return res;
}
Expand Down Expand Up @@ -116,11 +108,10 @@ 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 @@ -157,7 +148,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 @@ -181,8 +172,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 @@ -194,10 +185,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 @@ -268,7 +259,7 @@ function sendMessage(input) {
debugMode && logToFile(`${channel} received response #${index[0]} at client.`, localClientLog);
}
try {
return spawnSyncCall ? JSON.parse(res.stdout.toString('utf8')) : res;
return JSON.parse(spawnSyncCall ? res.stdout.toString('utf8') : res.toString('utf8'));
} catch (ex) {
if (spawnSyncCall) {
if (res.error) {
Expand Down
21 changes: 1 addition & 20 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ 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 @@ -88,9 +74,6 @@ 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 @@ -100,9 +83,7 @@ const server = net.createServer({allowHalfOpen: true}, c => {
if (req.client != 'ready') {
respond({s: false, v: {code: err.code, message: err.message}});
} else {
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}));
writeResponseToFile(req.responseFilePath, JSON.stringify({s: false, v: {code: err.code, message: err.message}}));
}
}
);
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.6",
"version": "1.3.7",
"main": "lib/index.js",
"description": "Run asynchronous commands synchronously by putting them in a separate process",
"keywords": [],
Expand Down

0 comments on commit 1c9d690

Please sign in to comment.