Skip to content

Commit ab6f070

Browse files
committed
v1.0.0
0 parents  commit ab6f070

22 files changed

+1486
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
.vscode/
61+
62+
.idea
63+
64+
.DS_Store

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Rolando Santamaria Maso
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# fast-proxy-lite
2+
Node.js framework agnostic library that enables you to forward an http request to another HTTP server.
3+
Supported proxy protocols: HTTP, HTTPS
4+
5+
> This library was initially forked from `fast-proxy`: https://github.com/fastify/fast-proxy
6+
7+
`fast-proxy-lite` powers: https://www.npmjs.com/package/fast-gateway 🚀
8+
## Install
9+
```
10+
npm i fast-proxy-lite
11+
```
12+
13+
## Usage
14+
The following examples describe how to use `fast-proxy-lite` with `restana`:
15+
16+
Gateway:
17+
```js
18+
const { proxy, close } = require('fast-proxy-lite')({
19+
base: 'http://127.0.0.1:3000'
20+
// options
21+
})
22+
const gateway = require('restana')()
23+
24+
gateway.all('/service/*', function (req, res) {
25+
proxy(req, res, req.url, {})
26+
})
27+
28+
gateway.start(8080)
29+
```
30+
31+
Remote service:
32+
```js
33+
const service = require('restana')()
34+
service.get('/service/hi', (req, res) => res.send('Hello World!'))
35+
36+
service.start(3000)
37+
```
38+
39+
Using imports:
40+
```ts
41+
import fastProxy from 'fast-proxy-lite'
42+
43+
const { proxy, close } = fastProxy({
44+
base: 'http://127.0.0.1:3000'
45+
})
46+
```
47+
48+
## Benchmarks
49+
Please see: https://github.com/jkyberneees/nodejs-proxy-benchmarks
50+
51+
## API
52+
53+
### Options
54+
#### `base`
55+
Set the base URL for all the forwarded requests. Will be required if `http2` is set to `true`
56+
Note that _path will be discarded_.
57+
58+
#### cacheURLs
59+
The number of parsed URLs that will be cached. Default: 100.
60+
> Use value = `0` to disable the caching mechanism
61+
62+
#### requests.http and requests.https
63+
Allows to optionally overwrite the internal `http` and `https` client agents implementation. Defaults: [`http`](https://nodejs.org/api/http.html#http_http) and [`https`](https://nodejs.org/api/https.html#https_https).
64+
65+
For example, this could be used to add support for following redirects, like so:
66+
67+
```js
68+
...
69+
requests: {
70+
http: require('follow-redirects/http'),
71+
https: require('follow-redirects/https')
72+
}
73+
...
74+
```
75+
76+
#### keepAliveMsecs
77+
Defaults to 1 minute, passed down to [`http.Agent`][http-agent] and
78+
[`https.Agent`][https-agent] instances.
79+
80+
#### maxSockets
81+
Defaults to 2048 sockets, passed down to [`http.Agent`][http-agent] and
82+
[`https.Agent`][https-agent] instances.
83+
84+
#### rejectUnauthorized
85+
Defaults to `true`, passed down to [`https.Agent`][https-agent] instances.
86+
This needs to be set to `false` to reply from https servers with
87+
self-signed certificates.
88+
89+
#### Extended configurations
90+
Other supported configurations in https://nodejs.org/api/http.html#http_new_agent_options can also be part of the `opts` object.
91+
92+
### close
93+
Optional _"on `close` resource release"_ strategy. You can link this to your application shutdown hook as an example.
94+
95+
### proxy(originReq, originRes, source, [opts])
96+
Enables you to forward an http request to another HTTP server.
97+
```js
98+
proxy(
99+
originReq, // http.IncomingMessage
100+
originRes, // http.ServerResponse
101+
req.url, // String -> remote URL + path or path if base was set
102+
{} // Options described below
103+
)
104+
```
105+
#### opts
106+
107+
##### base
108+
Optionally indicates the base URL for the current request proxy. When used, the global `base` config is overwriten.
109+
> This configuration value is ignored when using HTTP2.
110+
111+
##### onResponse(req, res, stream)
112+
Called when an http response is received from the source.
113+
The default behavior is `pump(stream, res)`, which will be disabled if the
114+
option is specified.
115+
116+
##### rewriteRequestHeaders(req, headers)
117+
Called to rewrite the headers of the request, before them being sent to the downstream server.
118+
It must return the new headers object.
119+
120+
##### rewriteHeaders(headers)
121+
Called to rewrite the headers of the response, before them being copied
122+
over to the outer response.
123+
It must return the new headers object.
124+
125+
##### request
126+
Extended options supported by `http[s].request` method (https://nodejs.org/api/http.html#http_http_request_options_callback)
127+
The following options are dynamically assigned: `method, port, path, hostname, headers, agent`.
128+
129+
##### queryString
130+
Replaces the original querystring of the request with what is specified.
131+
This will get passed to
132+
[`querystring.stringify`](https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options).
133+
134+
## Related topics
135+
- http-agent: https://nodejs.org/api/http.html#http_new_agent_options
136+
- https-agent: https://nodejs.org/api/https.html#https_class_https_agent
137+
138+
## License
139+
MIT

demos/gateway-typescript.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
import fastProxy from '..'
4+
import restana from 'restana'
5+
6+
const { proxy } = fastProxy({
7+
base: 'http://127.0.0.1:3000'
8+
})
9+
10+
const service = restana()
11+
service.all('/service/*', (req, res) => proxy(req, res, req.url, {}))
12+
13+
service.start(8080)

demos/gateway.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const { proxy } = require('../index')({
4+
base: 'http://127.0.0.1:3000'
5+
})
6+
7+
const service = require('restana')()
8+
service.all('/service/*', (req, res) => proxy(req, res, req.url, {}))
9+
10+
service.start(8080)

demos/service.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const bodyParser = require('body-parser')
4+
5+
const service = require('restana')()
6+
service.use(bodyParser.json())
7+
8+
service.get('/service/get', (req, res) => res.send('Hello World!'))
9+
10+
service.post('/service/post', (req, res) => res.send(req.body))
11+
12+
service.start(3000)

index.d.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as Http from 'http';
2+
import * as Https from 'https';
3+
import { Stream } from 'pump';
4+
5+
interface Options {
6+
base?: string;
7+
cacheURLs?: number;
8+
requests?: {
9+
http?: Http.Agent,
10+
https?: Https.Agent
11+
};
12+
keepAliveMsecs?: number;
13+
maxSockets?: number;
14+
rejectUnauthorized?: boolean;
15+
}
16+
17+
declare function fastProxy(options?: Options): {
18+
proxy(
19+
originReq: Http.IncomingMessage,
20+
originRes: Http.ServerResponse,
21+
source: string,
22+
opts?: {
23+
base?: string;
24+
onResponse?(req: Http.IncomingMessage, res: Http.ServerResponse, stream: Stream): void;
25+
rewriteRequestHeaders?(req: Http.IncomingMessage, headers: Http.IncomingHttpHeaders): Http.IncomingHttpHeaders;
26+
rewriteHeaders?(headers: Http.OutgoingHttpHeaders): Http.OutgoingHttpHeaders;
27+
request?: Http.RequestOptions;
28+
queryString?: string;
29+
}
30+
): void;
31+
close(): void;
32+
}
33+
34+
export default fastProxy

0 commit comments

Comments
 (0)