-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const fork = require('child_process').fork | ||
const resolve = require('path').resolve | ||
|
||
module.exports = function forkRequest (address, delay = 100, cb) { | ||
const childProcess = fork( | ||
resolve(__dirname, 'request.js'), | ||
[address, delay], | ||
{ windowsHide: true } | ||
) | ||
|
||
childProcess.on('message', (payload) => { | ||
if (payload.error) { | ||
cb(new Error(payload.error), JSON.parse(payload.response), payload.body) | ||
return | ||
} | ||
cb(null, JSON.parse(payload.response), payload.body) | ||
}) | ||
childProcess.on('error', err => { | ||
cb(err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
const sget = require('simple-get').concat | ||
const promisify = require('util').promisify | ||
const wait = promisify(setTimeout) | ||
|
||
const address = process.argv[2] | ||
const delay = parseInt(process.argv[3]) | ||
|
||
// custom stringification to avoid circular reference breaking | ||
function stringifyResponse (response) { | ||
return JSON.stringify({ | ||
statusCode: response.statusCode, | ||
headers: response.headers | ||
}) | ||
} | ||
|
||
async function run () { | ||
await wait(delay) | ||
sget({ | ||
method: 'GET', | ||
url: address | ||
}, function (error, response, body) { | ||
if (error instanceof Error) { | ||
process.send({ | ||
error: error.message, | ||
response: stringifyResponse(response), | ||
body: body.toString() | ||
}) | ||
process.exit(1) | ||
} | ||
|
||
process.send({ | ||
error: null, | ||
response: stringifyResponse(response), | ||
body: body.toString() | ||
}) | ||
process.exit() | ||
}) | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.