fastify-http-proxy
Advanced tools
Comparing version 5.0.0 to 6.0.0
@@ -24,2 +24,3 @@ /// <reference types="node" /> | ||
wsServerOptions?: ServerOptions; | ||
httpMethods?: string[]; | ||
} | ||
@@ -26,0 +27,0 @@ |
@@ -170,3 +170,3 @@ 'use strict' | ||
url: '/', | ||
method: httpMethods, | ||
method: opts.httpMethods || httpMethods, | ||
preHandler, | ||
@@ -178,3 +178,3 @@ config: opts.config || {}, | ||
url: '/*', | ||
method: httpMethods, | ||
method: opts.httpMethods || httpMethods, | ||
preHandler, | ||
@@ -181,0 +181,0 @@ config: opts.config || {}, |
{ | ||
"name": "fastify-http-proxy", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "proxy http requests, for Fastify", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard | snazzy && tap test/*.js && npm run typescript", | ||
"lint:fix": "standard --fix", | ||
"lint:typescript": "standard --fix --parser @typescript-eslint/parser --plugin typescript test/types/*.ts", | ||
"test": "standard | snazzy && tap test/*.js && npm run typescript", | ||
"typescript": "tsd" | ||
@@ -28,3 +28,3 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^14.0.27", | ||
"@types/node": "^15.0.0", | ||
"@types/ws": "^7.4.0", | ||
@@ -35,4 +35,4 @@ "@typescript-eslint/parser": "^4.0.0", | ||
"express-http-proxy": "^1.6.2", | ||
"fast-proxy": "^1.7.0", | ||
"fastify": "^3.0.0", | ||
"fast-proxy": "^2.0.0", | ||
"fastify": "^3.12.0", | ||
"fastify-websocket": "^3.0.0", | ||
@@ -45,11 +45,11 @@ "got": "^11.5.1", | ||
"snazzy": "^9.0.0", | ||
"socket.io": "^3.0.4", | ||
"socket.io-client": "^3.0.4", | ||
"socket.io": "^4.0.0", | ||
"socket.io-client": "^4.0.0", | ||
"standard": "^16.0.3", | ||
"tap": "^14.10.8", | ||
"tsd": "^0.14.0", | ||
"tap": "^15.0.1", | ||
"tsd": "^0.17.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
"dependencies": { | ||
"fastify-reply-from": "^5.0.1", | ||
"fastify-reply-from": "^6.0.0", | ||
"ws": "^7.4.1" | ||
@@ -56,0 +56,0 @@ }, |
# fastify-http-proxy | ||
[![Build Status](https://travis-ci.org/fastify/fastify-http-proxy.svg?branch=master)](https://travis-ci.org/fastify/fastify-http-proxy) | ||
![CI](https://github.com/fastify/fastify-http-proxy/workflows/CI/badge.svg) | ||
[![NPM version](https://img.shields.io/npm/v/fastify-http-proxy.svg?style=flat)](https://www.npmjs.com/package/fastify-http-proxy) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-http-proxy/badge.svg)](https://snyk.io/test/github/fastify/fastify-http-proxy) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
Proxy your http requests to another server, with hooks. | ||
This [`fastify`](https://www.fastify.io) plugin forward all the request | ||
Proxy your HTTP requests to another server, with hooks. | ||
This [`fastify`](https://www.fastify.io) plugin forwards all requests | ||
received with a given prefix (or none) to an upstream. All Fastify hooks are still applied. | ||
`fastify-http-proxy` is built on top of | ||
[`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables | ||
you for single route proxying. | ||
[`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables single route proxying. | ||
@@ -42,3 +44,3 @@ This plugin can be used in a variety of circumstances, for example if you have to proxy an internal domain to an external domain (useful to avoid CORS problems) or to implement your own API gateway for a microservices architecture. | ||
If you want to have different proxies on different prefixes in you can register multiple instances of the plugin as shown in the following snippet: | ||
If you want to have different proxies on different prefixes you can register multiple instances of the plugin as shown in the following snippet: | ||
@@ -72,7 +74,7 @@ ```js | ||
For other examples, see `example.js`. | ||
For other examples, see [`example.js`](example.js). | ||
## Request tracking | ||
`fastify-http-proxy` can track and pipe the `request-id` across the upstreams. Using the [`hyperid`](https://www.npmjs.com/package/hyperid) module and the [`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) built in options a fairly simple example would look like this: | ||
`fastify-http-proxy` can track and pipe the `request-id` across the upstreams. Using the [`hyperid`](https://www.npmjs.com/package/hyperid) module and the [`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) built-in options a fairly simple example would look like this: | ||
@@ -149,2 +151,5 @@ ```js | ||
### httpMethods | ||
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`. | ||
## websocket | ||
@@ -166,13 +171,11 @@ | ||
The following benchmarks where generated on a Macbook 2018 with i5 and | ||
8GB of RAM: | ||
The following benchmarks where generated on a dedicated server with an Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and 64GB of RAM: | ||
| __Framework__ | req/sec | | ||
| :----------------- | :------------------------- | | ||
| `express-http-proxy` | 878.4 | | ||
| `http-proxy` | 3837 | | ||
| `fastify-http-proxy` | 4205 | | ||
| `fastify-http-proxy` (with [`undici`](https://github.com/mcollina/undici)) | 6235.6 | | ||
| `express-http-proxy` | 2557 | | ||
| `http-proxy` | 9519 | | ||
| `fastify-http-proxy` | 15919 | | ||
The results where gathered on the second run of `autocannon -c 100 -d 5 | ||
The results were gathered on the second run of `autocannon -c 100 -d 5 | ||
URL`. | ||
@@ -179,0 +182,0 @@ |
@@ -18,2 +18,3 @@ import fastify, { RawReplyDefaultExpression, RawRequestDefaultExpression } from "fastify"; | ||
replyOptions: { contentType: "application/json" }, | ||
httpMethods: ["DELETE", "GET", "HEAD", "PATCH", "POST", "PUT", "OPTIONS"], | ||
preHandler: (request, reply) => { | ||
@@ -20,0 +21,0 @@ expectType<RawRequestDefaultExpression>(request.raw); |
@@ -16,6 +16,6 @@ 'use strict' | ||
const srvUpstream = createServer() | ||
t.tearDown(srvUpstream.close.bind(srvUpstream)) | ||
t.teardown(srvUpstream.close.bind(srvUpstream)) | ||
const srvSocket = new ioServer.Server(srvUpstream) | ||
t.tearDown(srvSocket.close.bind(srvSocket)) | ||
t.teardown(srvSocket.close.bind(srvSocket)) | ||
@@ -25,3 +25,3 @@ await promisify(srvUpstream.listen.bind(srvUpstream))(0) | ||
const srvProxy = Fastify() | ||
t.tearDown(srvProxy.close.bind(srvProxy)) | ||
t.teardown(srvProxy.close.bind(srvProxy)) | ||
@@ -37,3 +37,3 @@ srvProxy.register(proxy, { | ||
socket.on('hello', data => { | ||
t.is(data, 'world') | ||
t.equal(data, 'world') | ||
socket.emit('hi', 'socket') | ||
@@ -44,3 +44,3 @@ }) | ||
const cliSocket = ioClient(`http://127.0.0.1:${srvProxy.server.address().port}`) | ||
t.tearDown(cliSocket.close.bind(cliSocket)) | ||
t.teardown(cliSocket.close.bind(cliSocket)) | ||
@@ -50,3 +50,3 @@ cliSocket.emit('hello', 'world') | ||
const out = await once(cliSocket, 'hi') | ||
t.is(out[0], 'socket') | ||
t.equal(out[0], 'socket') | ||
@@ -53,0 +53,0 @@ await Promise.all([ |
'use strict' | ||
const { tearDown, test } = require('tap') | ||
const { teardown, test } = require('tap') | ||
const Fastify = require('fastify') | ||
@@ -43,3 +43,3 @@ const proxy = require('../') | ||
tearDown(origin.close.bind(origin)) | ||
teardown(origin.close.bind(origin)) | ||
@@ -53,3 +53,3 @@ test('basic proxy', async t => { | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -74,3 +74,3 @@ const resultRoot = await got( | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -109,3 +109,3 @@ const { | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -135,3 +135,3 @@ const resultRoot = await got( | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -146,3 +146,3 @@ const resultRoot = await got( | ||
) | ||
t.deepEqual(resultRoot.body, { something: 'posted' }) | ||
t.same(resultRoot.body, { something: 'posted' }) | ||
}) | ||
@@ -156,3 +156,3 @@ | ||
preHandler (request, reply, next) { | ||
t.deepEqual(request.body, { hello: 'world' }) | ||
t.same(request.body, { hello: 'world' }) | ||
next() | ||
@@ -163,3 +163,3 @@ } | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -186,3 +186,3 @@ await got( | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -214,3 +214,3 @@ let errored = false | ||
async preHandler (request, reply) { | ||
t.deepEqual(reply.context.config, { | ||
t.same(reply.context.config, { | ||
foo: 'bar', | ||
@@ -233,3 +233,3 @@ url: '/', | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -271,3 +271,3 @@ let errored = false | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
origin2.close() | ||
@@ -301,3 +301,3 @@ proxyServer.close() | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
proxyServer.close() | ||
@@ -323,3 +323,3 @@ }) | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
proxyServer.close() | ||
@@ -344,3 +344,3 @@ }) | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
proxyServer.close() | ||
@@ -385,3 +385,3 @@ }) | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
proxyServer.close() | ||
@@ -407,3 +407,3 @@ }) | ||
t.tearDown(() => { | ||
t.teardown(() => { | ||
proxyServer.close() | ||
@@ -436,3 +436,3 @@ }) | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -451,4 +451,34 @@ try { | ||
}) | ||
test('settings of method types', async t => { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: `http://localhost:${origin.server.address().port}`, | ||
httpMethods: ['POST'] | ||
}) | ||
await server.listen(0) | ||
t.teardown(server.close.bind(server)) | ||
const resultRoot = await got( | ||
`http://localhost:${server.server.address().port}/this-has-data`, | ||
{ | ||
method: 'POST', | ||
json: { hello: 'world' }, | ||
responseType: 'json' | ||
} | ||
) | ||
t.same(resultRoot.body, { something: 'posted' }) | ||
let errored = false | ||
try { | ||
await await got(`http://localhost:${server.server.address().port}/a`) | ||
} catch (err) { | ||
t.equal(err.response.statusCode, 404) | ||
errored = true | ||
} | ||
t.ok(errored) | ||
}) | ||
} | ||
run() |
@@ -16,4 +16,4 @@ 'use strict' | ||
const wss = new WebSocket.Server({ server: origin }) | ||
t.tearDown(wss.close.bind(wss)) | ||
t.tearDown(origin.close.bind(origin)) | ||
t.teardown(wss.close.bind(wss)) | ||
t.teardown(origin.close.bind(origin)) | ||
@@ -37,3 +37,3 @@ wss.on('connection', (ws) => { | ||
await server.listen(0) | ||
t.tearDown(server.close.bind(server)) | ||
t.teardown(server.close.bind(server)) | ||
@@ -50,3 +50,3 @@ const ws = new WebSocket(`http://localhost:${server.server.address().port}`) | ||
t.is(buf.toString(), 'hello') | ||
t.equal(buf.toString(), 'hello') | ||
@@ -70,4 +70,4 @@ await Promise.all([ | ||
t.tearDown(app.close.bind(app)) | ||
t.tearDown(app2.close.bind(app2)) | ||
t.teardown(app.close.bind(app)) | ||
t.teardown(app2.close.bind(app2)) | ||
}) |
@@ -64,4 +64,4 @@ 'use strict' | ||
t.is(wsResult, expected) | ||
t.is(gotResult, expected) | ||
t.equal(wsResult, expected) | ||
t.equal(gotResult, expected) | ||
} | ||
@@ -68,0 +68,0 @@ |
@@ -63,4 +63,4 @@ 'use strict' | ||
t.is(wsResult, expected) | ||
t.is(gotResult, expected) | ||
t.equal(wsResult, expected) | ||
t.equal(gotResult, expected) | ||
} | ||
@@ -83,4 +83,2 @@ | ||
t.teardown(() => backend.close()) | ||
const backendURL = await backend.listen(0) | ||
@@ -90,3 +88,7 @@ | ||
t.teardown(() => frontend.close()) | ||
t.teardown(async () => { | ||
// Close the frontend before the backend to avoid timeouts | ||
await frontend.close() | ||
await backend.close() | ||
}) | ||
@@ -93,0 +95,0 @@ for (const path of paths) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
42508
22
1070
186
+ Addeddepd@2.0.0(transitive)
+ Addedfastify-reply-from@6.6.06.7.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addedprocess-warning@1.0.0(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtiny-lru@8.0.2(transitive)
+ Addedundici@4.16.0(transitive)
- Removeddepd@1.1.2(transitive)
- Removedfastify-reply-from@5.3.0(transitive)
- Removedhttp-errors@1.8.1(transitive)
- Removedstatuses@1.5.0(transitive)
- Removedtiny-lru@7.0.6(transitive)
- Removedundici@3.3.6(transitive)
Updatedfastify-reply-from@^6.0.0