diff --git a/lib/index.js b/lib/index.js index 2dcf47e..9baa60f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; } @@ -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, @@ -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(); @@ -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 ); } } @@ -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}`); } @@ -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) { diff --git a/lib/worker.js b/lib/worker.js index 06b086c..b2344af 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -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 = []; @@ -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.`); @@ -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})); } } ); diff --git a/package.json b/package.json index 83a71c7..e3a6d2d 100644 --- a/package.json +++ b/package.json @@ -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": [],