network-overrides
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -16,3 +16,3 @@ const { spawn } = require('child_process'); | ||
function spawnBackendProcess(options = {}) { | ||
async function spawnBackendProcess(options = {}) { | ||
const backendStartPath = path.join(__dirname, 'backend', 'start.js'); | ||
@@ -22,25 +22,55 @@ | ||
const initializedServerProcessPromise = new Promise((resolve, reject) => { | ||
const serverProcess = spawn('node', [backendStartPath], { | ||
...restOptions, | ||
env: { | ||
...process.env, | ||
...envOptions, | ||
}, | ||
}); | ||
let serverIsAlreadyRunning = false; | ||
setTimeout(() => { | ||
if (serverProcess.exitCode) { | ||
const error = new Error('server initialization failed'); | ||
try { | ||
// check if the server is already running | ||
await getOverrides(); | ||
serverIsAlreadyRunning = true; | ||
} catch (e) {} | ||
error.exitCode = serverProcess.exitCode; | ||
if (serverIsAlreadyRunning) { | ||
const error = new Error('server is already running'); | ||
reject(error); | ||
} | ||
error.exitCode = EADDRINUSE.exitCode; | ||
resolve(serverProcess); | ||
}, 500); | ||
throw error; | ||
} | ||
// spawn the backend process | ||
const serverProcess = spawn('node', [backendStartPath], { | ||
...restOptions, | ||
env: { | ||
...process.env, | ||
...envOptions, | ||
}, | ||
}); | ||
return initializedServerProcessPromise; | ||
// wait a while for the server to get up, checking its status multiple times in between | ||
let attemptsLeft = 10; | ||
while (attemptsLeft-- > 0) { | ||
await sleep(100); | ||
// if the server has exited -> throw corresponding error | ||
if (serverProcess.exitCode) { | ||
const error = new Error('server initialization failed'); | ||
error.exitCode = serverProcess.exitCode; | ||
throw error; | ||
} | ||
try { | ||
// check if the server is ready | ||
await getOverrides(); | ||
return serverProcess; | ||
} catch (e) { | ||
// the server is not ready but hasn't exited | ||
// suppress the error and try again if there are any attempts left | ||
} | ||
} | ||
// exhausted attemps -> return serverProcess anyways | ||
return serverProcess; | ||
} | ||
@@ -90,5 +120,4 @@ | ||
) { | ||
const [subCommandName, ...subArgs] = commandToWrap.trim().split(/ +/g); | ||
if (ensureBackend) { | ||
// start the process in the background, continuing if already running | ||
try { | ||
@@ -106,2 +135,3 @@ await spawnBackendProcessInBackground(); | ||
// add overrides set | ||
try { | ||
@@ -117,2 +147,6 @@ await addOverrides(overrideSetId, overrides); | ||
// get command name and related args by splitting the command by spaces | ||
const [subCommandName, ...subArgs] = commandToWrap.trim().split(/ +/g); | ||
// start wrapped command | ||
const subProcess = spawn(subCommandName, subArgs, { | ||
@@ -122,4 +156,8 @@ stdio: 'inherit', | ||
//#region cleanup section | ||
let cleanupCalled = false; | ||
/** | ||
* remove overrides set, if we haven't yet | ||
*/ | ||
function cleanup() { | ||
@@ -158,4 +196,11 @@ if (cleanupCalled) { | ||
}); | ||
//#endregion cleanup section | ||
} | ||
function sleep(durationMs) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, durationMs); | ||
}); | ||
} | ||
module.exports = { | ||
@@ -162,0 +207,0 @@ startBackend, |
{ | ||
"name": "network-overrides", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "CLI and backend for the Network Overrides browser extension", | ||
@@ -5,0 +5,0 @@ "bin": { |
14657
380