From e50a42b1760d5f04331dfa09d22c92d2dc11d0ea Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 30 Jul 2018 22:37:27 -0400 Subject: [PATCH] Added support for undici --- README.md | 9 ++++----- benchmarks/fastify-proxy.js | 12 +++++++++++- index.js | 9 +++++---- package.json | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 47d872a..c553ec1 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,8 @@ For other examples, see `example.js`. ## Options -This `fastify` plugin supports the following options. +This `fastify` plugin supports _all_ the options of +[`fastify-reply-from`](https://github.com/fastify/fastify-reply-from) plus the following. *Note that this plugin is fully encapsulated, and non-JSON payloads will be streamed directly to the destination.* @@ -82,10 +83,6 @@ The prefix to mount this plugin on. All the requests to the current server start A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication). -### http2 - -A boolean value that indicates whether the proxy should support http2. - ## Benchmarks The following benchmarks where generated on a Macbook 2018 with i5 and @@ -95,6 +92,8 @@ The following benchmarks where generated on a Macbook 2018 with i5 and | `express-http-proxy` | 878.4 | | `http-proxy` | 3837 | | `fastify-http-proxy` | 4205 | +| `fastify-http-proxy` (with +[`undici`](https://github.com/mcollina/undici)) | 6235.6 | The results where gathered on the second run of `autocannon -c 100 -d 5 URL`. diff --git a/benchmarks/fastify-proxy.js b/benchmarks/fastify-proxy.js index 63cf90e..42689a8 100644 --- a/benchmarks/fastify-proxy.js +++ b/benchmarks/fastify-proxy.js @@ -5,9 +5,19 @@ const proxy = require('..') async function startProxy (upstream) { const server = Fastify() + let undici = false + + if (process.env.UNDICI) { + undici = { + connections: 100, + pipelining: 10 + } + } + server.register(proxy, { upstream, - http2: !!process.env.HTTP2 + http2: !!process.env.HTTP2, + undici }) await server.listen(3000) diff --git a/index.js b/index.js index dff0403..85ca652 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,11 @@ module.exports = async function (fastify, opts) { const beforeHandler = opts.beforeHandler - fastify.register(From, { - base: opts.upstream, - http2: opts.http2 - }) + const fromOpts = Object.assign({}, opts) + fromOpts.base = opts.upstream + fromOpts.prefix = undefined + + fastify.register(From, fromOpts) fastify.addContentTypeParser('application/json', bodyParser) fastify.addContentTypeParser('*', bodyParser) diff --git a/package.json b/package.json index fa498b8..e9ba349 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,6 @@ "tap": "^12.0.0" }, "dependencies": { - "fastify-reply-from": "^0.4.5" + "fastify-reply-from": "^0.5.0" } }