Skip to content

Commit 6f9fcaa

Browse files
authored
feat: added verbose-proxy package (#15)
1 parent 40a55de commit 6f9fcaa

File tree

18 files changed

+1872
-26
lines changed

18 files changed

+1872
-26
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"introsort": "yarn workspace introsort",
1212
"fi": "yarn workspace fi",
1313
"node-index": "yarn workspace node-index",
14-
"request-handler": "yarn workspace request-handler"
14+
"request-handler": "yarn workspace request-handler",
15+
"verbose-proxy": "yarn workspace verbose-proxy"
1516
},
1617
"devDependencies": {
1718
"deepmerge": "^4.2.2",

pkg/verbose-proxy/.foreverignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src
2+
node_modules

pkg/verbose-proxy/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Verbose-proxy
2+
3+
Simple webserver that can act as a proxy as well, to just intercept and see what
4+
a request looks like, both in terms of request and response
5+
6+
By default it runs a little fake server that can return a customized response. See
7+
'dummy server' section below
8+
9+
## Usage / Installation
10+
11+
You don't actually have to install it, you can simply run it via:
12+
13+
```sh
14+
npx verbose-proxy
15+
```
16+
17+
Or you can install it globallyo
18+
19+
```sh
20+
npm i -g verbose-proxy
21+
```
22+
23+
## Help menu
24+
25+
```sh
26+
Usage: verbose-proxy [options]
27+
28+
Options:
29+
--version Show version number [boolean]
30+
--target proxy target [string] [default: "dummy"]
31+
--port which port to listen to [number] [default: "8889"]
32+
-h, --help Show help [boolean]
33+
34+
Examples:
35+
verbose-proxy --port=8901 Start verbose-proxy on port 8901
36+
```
37+
38+
## Dummy server
39+
40+
The dummy server is quite simple. You can query any url with any content type etc
41+
but if you want a specific response you can query eg. `curl http://localhost:8889/418/json`
42+
if you want to get a 418 response.
43+
44+
It's a bit dumb and won't actually change the response, other than return the status code.
45+
46+
it has two types of response bodies.. application/json and text/html, with a corresponding
47+
dummy return value
48+
49+
## TODO:
50+
51+
- adding some unit tests
52+
- add more functionality to the dummy server
53+
- add more options to customize the underlying http-proxy instance
54+
55+
## Contributions
56+
57+
Any contribution is welcome, pull request, issues, ideas etc.
58+
59+
## License
60+
61+
MIT

pkg/verbose-proxy/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
3+
}

pkg/verbose-proxy/bin/command

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require('../dist/index.js')

pkg/verbose-proxy/package.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "verbose-proxy",
3+
"version": "1.0.10",
4+
"description": "Debugging tool to just log out everything that comes in (optionally also proxy)",
5+
"private": false,
6+
"license": "MIT",
7+
"sideEffects": false,
8+
"files": [
9+
"dist"
10+
],
11+
"scripts": {
12+
"build": "rollup -c",
13+
"test": "jest",
14+
"start": "forever --watch --minUptime 1000 --spinSleepTime 1000 dist/index.js",
15+
"try": "./bin/command"
16+
},
17+
"bin": {
18+
"verbose-proxy": "./bin/command"
19+
},
20+
"devDependencies": {
21+
"@rollup/plugin-commonjs": "^21.0.1",
22+
"@rollup/plugin-node-resolve": "^13.0.6",
23+
"@types/http-proxy": "^1.17.7",
24+
"@types/jest": "*",
25+
"@types/node": "^16.11.11",
26+
"forever": "^4.0.1",
27+
"jest": "^27.3.1",
28+
"rollup": "^2.58.0",
29+
"rollup-plugin-ts": "^1.4.7",
30+
"ts-node": "*",
31+
"tslib": "^2.3.1",
32+
"typescript": "4.4.4"
33+
},
34+
"author": {
35+
"name": "Arnor Heidar Sigurdsson",
36+
"email": "arnorhs@gmail.com",
37+
"url": "https://arnorhs.dev"
38+
},
39+
"repository": {
40+
"type": "git",
41+
"url": "ssh://git@github.com/arnorhs/arnorhs-packages.git",
42+
"directory": "pkg/verbose-proxy"
43+
},
44+
"readmeFilename": "README.md",
45+
"publishConfig": {
46+
"access": "public"
47+
},
48+
"dependencies": {
49+
"http-proxy": "^1.18.1",
50+
"yargs": "^16.2.0"
51+
}
52+
}

pkg/verbose-proxy/rollup.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import typescript from 'rollup-plugin-ts'
2+
import resolve from '@rollup/plugin-node-resolve'
3+
import commonjs from '@rollup/plugin-commonjs'
4+
import packageJson from './package.json'
5+
6+
export default {
7+
input: './src/index.ts',
8+
output: [
9+
{
10+
file: 'dist/index.js',
11+
format: 'cjs',
12+
sourcemap: true,
13+
},
14+
],
15+
external: Object.keys(packageJson.dependencies ?? {}),
16+
plugins: [
17+
resolve({ preferBuiltins: true }),
18+
commonjs(),
19+
typescript({
20+
tsconfig: 'tsconfig.build.json',
21+
}),
22+
],
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('test', () => {
2+
it('works', () => {
3+
expect('true').toBeTruthy()
4+
})
5+
})

pkg/verbose-proxy/src/_old.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const http = require('http')
2+
3+
const p = (request, response) => {
4+
setTimeout(() => {
5+
//response.writeHead(500, { 'Content-Type': 'text/html' });
6+
response.writeHead(404, { 'Content-Type': 'application/json' })
7+
response.end(JSON.stringify({ ok: true }))
8+
}, 100)
9+
}
10+
11+
const server = http.createServer((request, response) => {
12+
const requestStart = Date.now()
13+
14+
let body = []
15+
let requestErrorMessage = null
16+
17+
const getChunk = (chunk) => body.push(chunk)
18+
const assembleBody = () => {
19+
body = Buffer.concat(body).toString()
20+
}
21+
const getError = (error) => {
22+
requestErrorMessage = error.message
23+
}
24+
25+
request.on('data', getChunk)
26+
request.on('end', assembleBody)
27+
request.on('error', getError)
28+
29+
const logClose = () => {
30+
removeHandlers()
31+
log(request, response, 'Client aborted.', requestStart, body)
32+
}
33+
const logError = (error) => {
34+
removeHandlers()
35+
log(request, response, error.message, requestStart, body)
36+
}
37+
const logFinish = () => {
38+
removeHandlers()
39+
log(request, response, requestErrorMessage, requestStart, body)
40+
}
41+
response.on('close', logClose)
42+
response.on('error', logError)
43+
response.on('finish', logFinish)
44+
45+
const removeHandlers = () => {
46+
request.off('data', getChunk)
47+
request.off('end', assembleBody)
48+
request.off('error', getError)
49+
response.off('close', logClose)
50+
response.off('error', logError)
51+
response.off('finish', logFinish)
52+
}
53+
54+
p(request, response)
55+
})
56+
57+
const log = (request, response, errorMessage, requestStart, body) => {
58+
const { rawHeaders, httpVersion, method, socket, url } = request
59+
const { remoteAddress, remoteFamily } = socket
60+
61+
const { statusCode, statusMessage } = response
62+
const processingTime = Date.now() - requestStart
63+
64+
console.log('==========================================')
65+
console.log(
66+
new Date().toISOString().substring(0, 19),
67+
`[${method}]`,
68+
url,
69+
`(${statusCode} ${statusMessage}, ${processingTime}ms)`,
70+
)
71+
72+
for (let i = 0; i < rawHeaders.length; i += 2) {
73+
console.log(` ${rawHeaders[i]}: ${rawHeaders[i + 1]}`)
74+
}
75+
76+
console.log('-- body start --')
77+
console.log(body)
78+
console.log('-- body end --')
79+
}
80+
81+
const listen = (port) => {
82+
server.listen(port)
83+
console.log(`server listening on port ${port}`)
84+
}
85+
86+
listen(process.env.PORT || 8889)

pkg/verbose-proxy/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { startProxy } from './lib/proxy'
2+
import { startDummyServer } from './lib/dummy-server'
3+
import { command } from './lib/command'
4+
5+
const DUMMYPORT = 8899
6+
7+
// something something check if server running
8+
const { port, target } = command()
9+
10+
if (target === 'dummy') {
11+
startDummyServer(DUMMYPORT)
12+
}
13+
14+
const url = target === 'dummy' ? `http://localhost:${DUMMYPORT}` : target
15+
16+
startProxy(parseInt(port, 10), url, {
17+
// expose options through command one day
18+
})

0 commit comments

Comments
 (0)