delay-proxy
Advanced tools
Comparing version 0.1.0 to 0.2.0
#!/usr/bin/env node | ||
const http = require("http"); | ||
const chalk = require("chalk"); | ||
const argv = require("minimist")(process.argv.slice(2)); | ||
const pkg = require("../package.json"); | ||
const request = require("request"); | ||
const myLocalIp = require("my-local-ip")(); | ||
const { makeServer } = require("../index"); | ||
const port = argv.p || argv.port || 8001; | ||
@@ -30,60 +31,2 @@ | ||
const makeServer = () => { | ||
return http.createServer((req, res) => { | ||
const match = req.url.match(/^\/delay\/(\d+)\/(.*)/); | ||
if (match) { | ||
let delay = parseInt(match[1], 10); | ||
const proxyUrl = match[2]; | ||
if (isNaN(delay) || delay < 0 || delay > 20000) { | ||
delay = 0; | ||
} | ||
if (proxyUrl) { | ||
// retrieve original request headers (traffic might be rejected for host mismatch) | ||
const { host, ...headers } = req.headers; | ||
var proxyReq = request( | ||
{ | ||
url: proxyUrl, | ||
method: req.method, | ||
headers: { | ||
...headers, | ||
"X-Forwarded-Host": host | ||
} | ||
}, | ||
error => { | ||
if (error) { | ||
console.error(error); | ||
} | ||
} | ||
); | ||
if (delay) { | ||
proxyReq.on("response", proxyRes => { | ||
proxyRes.pause(); | ||
setTimeout(() => { | ||
proxyRes.pipe(res); | ||
}, delay); | ||
}); | ||
} else { | ||
req.pipe(proxyReq).pipe(res); | ||
} | ||
} else { | ||
return res.end(`No url specified`); | ||
} | ||
} else { | ||
return res.end(` | ||
Use the /delay/:milliseconds/:url endpoint to delay a response. | ||
Examples of a call: | ||
* http://localhost:${port}/delay/1000/https://jsonplaceholder.typicode.com/posts/1/comments | ||
* http://${myLocalIp}:${port}/delay/1000/https://jsonplaceholder.typicode.com/posts/1/comments | ||
* http://localhost:${port}/delay/2000/https://via.placeholder.com/350x150/F00000/FFFFFF?text=Hello+world! | ||
* http://${myLocalIp}:${port}/delay/2000/https://via.placeholder.com/350x150/F00000/FFFFFF?text=Hello+world! | ||
`); | ||
} | ||
}); | ||
}; | ||
if (argv.h || argv.help) { | ||
@@ -100,3 +43,3 @@ help(); | ||
// create and launch server | ||
makeServer().listen(port, () => | ||
makeServer({ port, localIp: myLocalIp }).listen(port, () => | ||
console.log(chalk` | ||
@@ -103,0 +46,0 @@ {bold delay-proxy} server started |
{ | ||
"name": "delay-proxy", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Development server to simulate slow bandwidth", | ||
@@ -8,2 +8,3 @@ "scripts": { | ||
"lint": "npx eslint .", | ||
"generate-changelog": "./bin/generate-changelog.js", | ||
"test": "echo 'No Unit tests yet'", | ||
@@ -20,2 +21,3 @@ "test:precommit": "npm test", | ||
}, | ||
"main": "index.js", | ||
"repository": { | ||
@@ -44,2 +46,3 @@ "type": "git", | ||
"devDependencies": { | ||
"commander": "^2.17.1", | ||
"cross-env": "^5.2.0", | ||
@@ -50,2 +53,4 @@ "eslint": "^5.5.0", | ||
"eslint-plugin-prettier": "^2.6.2", | ||
"generate-changelog": "^1.7.1", | ||
"github-url-from-git": "^1.5.0", | ||
"husky": "^0.14.3", | ||
@@ -52,0 +57,0 @@ "lint-staged": "^7.2.2", |
@@ -22,4 +22,6 @@ # delay-proxy | ||
### Install globally | ||
### Command line | ||
#### Install globally | ||
```shell | ||
@@ -41,3 +43,3 @@ npm install -g delay-proxy | ||
### Install locally | ||
#### Install locally | ||
@@ -56,2 +58,21 @@ You might not like to install the package globally or wanna ship and use this package as a dev dependency in your project: | ||
### As a module | ||
`delay-proxy` also works as a required module. | ||
```js | ||
const PORT = 8001; | ||
const LOCAL_IP = require("my-local-ip")(); // optional | ||
const { makeServer } = require("delay-proxy"); | ||
makeServer({ port: PORT, localIp: LOCAL_IP }).listen(PORT, () => | ||
console.log( | ||
"delay-proxy now listening on", | ||
`http://localhost:${PORT}`, | ||
`${LOCAL_IP}:${PORT}` | ||
) | ||
); | ||
``` | ||
## Contributing | ||
@@ -77,3 +98,4 @@ | ||
- follow the [AngularJS git commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commits) for your commit messages | ||
- follow the [AngularJS git commit guidelines](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#commits) for your commit messages (it make it easier to generate changelog) | ||
- to generate changelog: `npm run generate-changelog -- v1.1.0 v1.2.0` | ||
- your code will automatically be: | ||
@@ -80,0 +102,0 @@ - linted by [eslint](https://eslint.org/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11178
6
153
104
12
2