Created
August 14, 2019 23:41
-
-
Save issa-tseng/97f937978f98ce6c723ea47c945e5b0b to your computer and use it in GitHub Desktop.
a really dumb quick node.js proxy to introduce artificial latency to a local port
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
// node latent.js fromport toport delay | |
// where fromport is the port of the service and toport is the port to serve the proxy on | |
const { createServer, request } = require('http'); | |
const [ , , fromport, toport, delaystr = 1000 ] = process.argv; | |
const delay = parseInt(delaystr); | |
createServer((req, res) => { | |
setTimeout(() => { | |
const prox = request({ | |
hostname: 'localhost', | |
port: fromport, | |
path: req.url, | |
method: req.method, | |
headers: req.headers | |
}, (pres) => { | |
res.writeHead(pres.statusCode, pres.headers); | |
pres.pipe(res, { end: true }); | |
}); | |
req.pipe(prox, { end: true }); | |
}, delay); | |
}).listen(toport); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment