@fastify/reply-from
Advanced tools
Comparing version 12.0.0 to 12.0.1
@@ -9,3 +9,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
reply.send('hello world') | ||
@@ -23,3 +23,3 @@ }) | ||
proxy.get('/', (request, reply) => { | ||
proxy.get('/', (_request, reply) => { | ||
reply.from('/') | ||
@@ -26,0 +26,0 @@ }) |
14
index.js
@@ -152,3 +152,3 @@ 'use strict' | ||
if (res && res.headers['retry-after']) { | ||
if (res?.headers['retry-after']) { | ||
retryAfter = res.headers['retry-after'] | ||
@@ -168,5 +168,3 @@ } | ||
if (retryDelay) { | ||
requestImpl = createRequestRetry(request, this, (req, res, err, retries) => { | ||
return retryDelay({ err, req, res, attempt: retries, getDefaultDelay, retriesCount }) | ||
}) | ||
requestImpl = createRequestRetry(request, this, (req, res, err, retries) => retryDelay({ err, req, res, attempt: retries, getDefaultDelay, retriesCount })) | ||
} else { | ||
@@ -228,3 +226,3 @@ requestImpl = createRequestRetry(request, this, getDefaultDelay) | ||
fastify.onClose((fastify, next) => { | ||
fastify.onClose((_fastify, next) => { | ||
close() | ||
@@ -263,11 +261,11 @@ // let the event loop do a full run so that it can | ||
function headersNoOp (headers, originalReq) { | ||
function headersNoOp (headers, _originalReq) { | ||
return headers | ||
} | ||
function requestHeadersNoOp (originalReq, headers) { | ||
function requestHeadersNoOp (_originalReq, headers) { | ||
return headers | ||
} | ||
function upstreamNoOp (req, base) { | ||
function upstreamNoOp (_req, base) { | ||
return base | ||
@@ -274,0 +272,0 @@ } |
@@ -78,3 +78,3 @@ 'use strict' | ||
} else if (hasUndiciOptions) { | ||
if (opts.base && opts.base.startsWith('unix+')) { | ||
if (opts.base?.startsWith('unix+')) { | ||
const undiciOpts = getUndiciOptions(opts.undici) | ||
@@ -106,4 +106,4 @@ undiciOpts.socketPath = decodeURIComponent(new URL(opts.base).host) | ||
if (hasUndiciOptions) { | ||
undiciAgent && undiciAgent.destroy() | ||
undiciInstance && undiciInstance.destroy() | ||
undiciAgent?.destroy() | ||
undiciInstance?.destroy() | ||
} else if (!isHttp2) { | ||
@@ -309,3 +309,3 @@ agents['http:'].destroy() | ||
maxFreeSockets: 2048, | ||
...(opts.http && opts.http.agentOptions) | ||
...(opts.http?.agentOptions) | ||
} | ||
@@ -312,0 +312,0 @@ } |
{ | ||
"name": "@fastify/reply-from", | ||
"version": "12.0.0", | ||
"version": "12.0.1", | ||
"description": "forward your HTTP request to another server, for fastify", | ||
@@ -9,4 +9,4 @@ "main": "index.js", | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix | snazzy", | ||
"lint": "eslint", | ||
"lint:fix": "eslint --fix", | ||
"test": "npm run test:unit && npm run test:typescript", | ||
@@ -27,2 +27,21 @@ "test:unit": "tap", | ||
"author": "Matteo Collina <hello@matteocollina.com>", | ||
"contributors": [ | ||
{ | ||
"name": "James Sumners", | ||
"url": "https://james.sumners.info" | ||
}, | ||
{ | ||
"name": "Manuel Spigolon", | ||
"email": "behemoth89@gmail.com" | ||
}, | ||
{ | ||
"name": "Aras Abbasi", | ||
"email": "aras.abbasi@gmail.com" | ||
}, | ||
{ | ||
"name": "Frazer Smith", | ||
"email": "frazer.dev@icloud.com", | ||
"url": "https://github.com/fdawgs" | ||
} | ||
], | ||
"license": "MIT", | ||
@@ -33,2 +52,12 @@ "bugs": { | ||
"homepage": "https://github.com/fastify/fastify-reply-from#readme", | ||
"funding": [ | ||
{ | ||
"type": "github", | ||
"url": "https://github.com/sponsors/fastify" | ||
}, | ||
{ | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/fastify" | ||
} | ||
], | ||
"devDependencies": { | ||
@@ -38,5 +67,6 @@ "@fastify/formbody": "^8.0.0", | ||
"@fastify/pre-commit": "^2.1.0", | ||
"@sinonjs/fake-timers": "^13.0.1", | ||
"@sinonjs/fake-timers": "^14.0.0", | ||
"@types/node": "^22.0.0", | ||
"@types/tap": "^15.0.11", | ||
"@types/tap": "^18.0.0", | ||
"eslint": "^9.17.0", | ||
"fastify": "^5.0.0", | ||
@@ -46,11 +76,8 @@ "form-data": "^4.0.0", | ||
"h2url": "^0.2.0", | ||
"msgpack5": "^6.0.2", | ||
"neostandard": "^0.12.0", | ||
"nock": "^13.5.4", | ||
"proxy": "^2.1.1", | ||
"proxyquire": "^2.1.3", | ||
"semver": "^7.6.0", | ||
"simple-get": "^4.0.1", | ||
"snazzy": "^9.0.0", | ||
"split2": "^4.2.0", | ||
"standard": "^17.1.0", | ||
"tap": "^18.7.2", | ||
@@ -57,0 +84,0 @@ "tsd": "^0.31.0" |
# @fastify/reply-from | ||
[![CI](https://github.com/fastify/fastify-reply-from/workflows/CI/badge.svg)](https://github.com/fastify/fastify-reply-from/actions/workflows/ci.yml) | ||
[![CI](https://github.com/fastify/fastify-reply-from/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-reply-from/actions/workflows/ci.yml) | ||
[![NPM version](https://img.shields.io/npm/v/@fastify/reply-from.svg?style=flat)](https://www.npmjs.com/package/@fastify/reply-from) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) | ||
@@ -17,3 +17,3 @@ Fastify plugin to forward the current HTTP request to another server. | ||
## Compatibility with @fastify/multipart | ||
`@fastify/reply-from` and [`@fastify/multipart`](https://github.com/fastify/fastify-multipart) should not be registered as sibling plugins nor should they be registered in plugins which have a parent-child relationship.`<br>` The two plugins are incompatible, in the sense that the behavior of `@fastify/reply-from` might not be the expected one when the above-mentioned conditions are not respected.`<br>` This is due to the fact that `@fastify/multipart` consumes the multipart content by parsing it, hence this content is not forwarded to the target service by `@fastify/reply-from`.`<br>` | ||
`@fastify/reply-from` and [`@fastify/multipart`](https://github.com/fastify/fastify-multipart) should not be registered as sibling plugins nor should they be registered in plugins that have a parent-child relationship.`<br>` The two plugins are incompatible, in the sense that the behavior of `@fastify/reply-from` might not be the expected one when the above-mentioned conditions are not respected.`<br>` This is due to the fact that `@fastify/multipart` consumes the multipart content by parsing it, hence this content is not forwarded to the target service by `@fastify/reply-from`.`<br>` | ||
However, the two plugins may be used within the same fastify instance, at the condition that they belong to disjoint branches of the fastify plugins hierarchy tree. | ||
@@ -87,6 +87,6 @@ | ||
By default, [undici](https://github.com/nodejs/undici) will be used to perform the HTTP/1.1 | ||
requests. Enabling this flag should guarantee | ||
requests. Enabling this option should guarantee | ||
20-50% more throughput. | ||
This flag could controls the settings of the undici client, like so: | ||
This option controls the settings of the undici client, like so: | ||
@@ -172,3 +172,3 @@ ```js | ||
You can either set `http2` to `true` or set the settings object to connect to a HTTP/2 server. | ||
You can either set `http2` to `true` or set the settings object to connect to an HTTP/2 server. | ||
The `http2` settings object has the shape of: | ||
@@ -194,3 +194,3 @@ | ||
By default package will issue log messages when a request is received. By setting this option to true, these log messages will be disabled. | ||
By default, the package will issue log messages when a request is received. By setting this option to true, these log messages will be disabled. | ||
@@ -213,3 +213,3 @@ Default for `disableRequestLogging` will be `false`. To disable the log messages set `disableRequestLogging` to `true`. | ||
This option will disable the URL caching. | ||
This cache is dedicated to reduce the amount of URL object generation. | ||
This cache is dedicated to reducing the amount of URL object generation. | ||
Generating URLs is a main bottleneck of this module, please disable this cache with caution. | ||
@@ -239,3 +239,3 @@ | ||
Enables the possibility to explictly opt-in for global agents. | ||
Enables the possibility to explicitly opt-in for global agents. | ||
@@ -283,3 +283,3 @@ Usage for undici global agent: | ||
This option set the limit on how many times the plugin should retry the request, specifically for 503 errors. | ||
This option sets the limit on how many times the plugin should retry the request, specifically for 503 errors. | ||
@@ -303,3 +303,3 @@ By Default: 10 | ||
- `getDefaultRetry` refers to the default retry handler. If this callback returns not null and you wish to handle those case of errors simply invoke it as done below. | ||
- `retriesCount` refers to the retriesCount property a client passes to reply-from. Note if the client does not explicitly set this value it will default to 0. The objective value here is to avoid hard-coding and seeing the retriesCount set. It is your perogative to ensure that you ensure the value here is as you wish (and not `0` if not intended to be as a result of a lack of not setting it). | ||
- `retriesCount` refers to the retriesCount property a client passes to reply-from. Note if the client does not explicitly set this value it will default to 0. The objective value here is to avoid hard-coding and seeing the retriesCount set. It is your prerogative to ensure that you ensure the value here is as you wish (and not `0` if not intended to be as a result of a lack of not setting it). | ||
@@ -350,3 +350,3 @@ Given example | ||
instance with a `from` method, which will reply to the original request | ||
__from the desired source__. The options allows to override any part of | ||
__from the desired source__. The options allows overrides of any part of | ||
the request or response being sent or received to/from the source. | ||
@@ -358,3 +358,3 @@ | ||
Called when a HTTP response is received from the source. Passed the original source `request`, the in-progress reply to the source as `reply`, and the ongoing `response` from the upstream server. | ||
Called when an HTTP response is received from the source. Passed the original source `request`, the in-progress reply to the source as `reply`, and the ongoing `response` from the upstream server. | ||
@@ -380,6 +380,6 @@ The default behavior is `reply.send(response.stream)`, which will be disabled if the | ||
Called when a HTTP response is received with error from the source. | ||
Called when an HTTP response is received with error from the source. | ||
The default behavior is `reply.send(error)`, which will be disabled if the | ||
option is specified. | ||
It must reply the error. | ||
It must reply with the error. | ||
@@ -401,3 +401,3 @@ #### `rewriteHeaders(headers, request)` | ||
Called to get upstream destination, before the request is being sent. Useful when you want to decide which target server to call based on the request data. | ||
Called to get upstream destination, before the request is sent. Useful when you want to decide which target server to call based on the request data. | ||
Helpful for a gradual rollout of new services. | ||
@@ -404,0 +404,0 @@ Parameters are the Fastify request and the base string from the plugin options. |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/', async (request, reply) => { | ||
instance.get('/', async (_request, reply) => { | ||
const p = reply.from() | ||
@@ -27,0 +27,0 @@ t.equal(p, reply) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -27,0 +27,0 @@ }) |
@@ -13,3 +13,3 @@ 'use strict' | ||
.get('/ip') | ||
.reply(200, function (uri, requestBody) { | ||
.reply(200, function () { | ||
t.equal(this.req.headers.host, 'httpbin.org') | ||
@@ -22,3 +22,3 @@ return { origin: '127.0.0.1' } | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('http://httpbin.org/ip') | ||
@@ -25,0 +25,0 @@ }) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from() | ||
@@ -27,0 +27,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('/hello') | ||
@@ -28,0 +28,0 @@ }) |
@@ -34,3 +34,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -61,3 +61,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -64,0 +64,0 @@ }) |
@@ -41,3 +41,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -86,3 +86,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -130,3 +130,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -133,0 +133,0 @@ }) |
@@ -53,3 +53,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -86,3 +86,3 @@ }) | ||
body: form | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -89,0 +89,0 @@ t.same(JSON.parse(data), { something: 'else' }) |
'use strict' | ||
const t = require('tap') | ||
const { describe, after, it } = require('node:test') | ||
const fastify = require('fastify') | ||
const get = require('simple-get').concat | ||
const From = require('..') | ||
const fastifyProxyFrom = require('..') | ||
const { isIPv6 } = require('node:net') | ||
const upstream = fastify() | ||
t.teardown(upstream.close.bind(upstream)) | ||
t.plan(4) | ||
describe('GHSA-v2v2-hph8-q5xp', function () { | ||
it('should not parse the body if it is an object', async function (t) { | ||
t.plan(1) | ||
upstream.post('/test', async (request, reply) => { | ||
if (typeof request.body === 'object') { | ||
return 'not ok' | ||
} | ||
return 'ok' | ||
}) | ||
const upstream = fastify() | ||
upstream.listen({ port: 0 }, function (err) { | ||
t.error(err) | ||
upstream.post('/test', async (request) => { | ||
if (typeof request.body === 'object') { | ||
return 'not ok' | ||
} | ||
return 'ok' | ||
}) | ||
const app = fastify() | ||
app.register(From) | ||
t.teardown(app.close.bind(app)) | ||
await upstream.listen({ port: 0 }) | ||
app.post('/test', (request, reply) => { | ||
if (request.body.method === 'invalid_method') { | ||
return reply.code(400).send({ message: 'payload contains invalid method' }) | ||
let upstreamAdress = upstream.server.address().address | ||
if (isIPv6(upstreamAdress)) { | ||
upstreamAdress = `[${upstreamAdress}]` | ||
} | ||
reply.from(`http://127.0.0.1:${upstream.server.address().port}/test`) | ||
}) | ||
app.listen({ port: 0 }, function (err) { | ||
t.error(err) | ||
const app = fastify() | ||
app.register(fastifyProxyFrom) | ||
get({ | ||
url: `http://127.0.0.1:${app.server.address().port}/test`, | ||
headers: { 'content-type': 'application/json ; charset=utf-8' }, | ||
// eslint-disable-next-line no-useless-escape | ||
body: '"{\\\"method\\\":\\\"invalid_method\\\"}"', | ||
method: 'POST' | ||
}, (err, res, data) => { | ||
t.error(err) | ||
t.equal(data.toString(), 'ok') | ||
app.post('/test', (request, reply) => { | ||
if (request.body.method === 'invalid_method') { | ||
return reply.code(400).send({ message: 'payload contains invalid method' }) | ||
} | ||
reply.from(`http://${upstreamAdress}:${upstream.server.address().port}/test`) | ||
}) | ||
await app.listen({ port: 0 }) | ||
after(() => { | ||
upstream.close() | ||
app.close() | ||
}) | ||
let appAddress = app.server.address().address | ||
if (isIPv6(appAddress)) { | ||
appAddress = `[${appAddress}]` | ||
} | ||
const response = await fetch( | ||
`http://${appAddress}:${app.server.address().port}/test`, | ||
{ | ||
headers: { 'content-type': 'application/json ; charset=utf-8' }, | ||
// eslint-disable-next-line no-useless-escape | ||
body: '"{\\\"method\\\":\\\"invalid_method\\\"}"', | ||
method: 'POST' | ||
}) | ||
t.assert.strictEqual(await response.text(), 'ok') | ||
}) | ||
}) |
@@ -17,3 +17,3 @@ 'use strict' | ||
target.delete('/', (request, reply) => { | ||
target.delete('/', (_request, reply) => { | ||
t.pass('request proxied') | ||
@@ -25,3 +25,3 @@ reply.code(200).header('x-my-header', 'hello!').send({ | ||
instance.delete('/', (request, reply) => { | ||
instance.delete('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/hello`) | ||
@@ -28,0 +28,0 @@ }) |
@@ -32,3 +32,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`https://localhost:${target.address().port}`) | ||
@@ -35,0 +35,0 @@ }) |
@@ -32,3 +32,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -35,0 +35,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -17,3 +17,3 @@ 'use strict' | ||
instance.addContentTypeParser('application/octet-stream', function (req, payload, done) { | ||
instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) { | ||
done(null, payload) | ||
@@ -41,3 +41,3 @@ }) | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -63,3 +63,3 @@ }) | ||
}) | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -66,0 +66,0 @@ t.same(JSON.parse(data), { something: 'else' }) |
@@ -15,3 +15,3 @@ 'use strict' | ||
instance.addContentTypeParser('application/octet-stream', function (req, payload, done) { | ||
instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) { | ||
done(null, payload) | ||
@@ -39,3 +39,3 @@ }) | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -61,3 +61,3 @@ }) | ||
}) | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -64,0 +64,0 @@ t.same(JSON.parse(data), { something: 'else' }) |
@@ -32,3 +32,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -52,3 +52,3 @@ }) | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -55,0 +55,0 @@ t.same(data, { something: 'else' }) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world?a=b`, { | ||
@@ -27,0 +27,0 @@ queryString: { b: 'c' } |
@@ -12,3 +12,3 @@ 'use strict' | ||
instance.addHook('preHandler', (request, reply, done) => { | ||
instance.addHook('preHandler', (request, _reply, done) => { | ||
request.addedVal = 'test' | ||
@@ -31,5 +31,5 @@ done() | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world?a=b`, { | ||
queryString (search, reqUrl, request) { | ||
queryString (_search, _reqUrl, request) { | ||
return querystring.stringify({ q: request.addedVal }) | ||
@@ -36,0 +36,0 @@ } |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world?a=b`, { | ||
@@ -28,0 +28,0 @@ queryString () { |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world`, { | ||
@@ -27,0 +27,0 @@ queryString: { b: 'c' } |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world?b=c`) | ||
@@ -27,0 +27,0 @@ }) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world`) | ||
@@ -27,0 +27,0 @@ }) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/hello', (request, reply) => { | ||
instance.get('/hello', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/world`) | ||
@@ -27,0 +27,0 @@ }) |
@@ -5,11 +5,13 @@ 'use strict' | ||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const fastifyReplyFrom = require('..') | ||
const http = require('node:http') | ||
const get = require('simple-get').concat | ||
const msgpack = require('msgpack5')() | ||
const instance = Fastify() | ||
instance.register(From) | ||
instance.register(fastifyReplyFrom) | ||
t.plan(8) | ||
const payload = { hello: 'world' } | ||
const msgPackPayload = Buffer.from([0x81, 0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa5, 0x77, 0x6f, 0x72, 0x6c, 0x64]) | ||
t.plan(9) | ||
t.teardown(instance.close.bind(instance)) | ||
@@ -26,3 +28,3 @@ | ||
req.on('end', () => { | ||
t.same(msgpack.decode(Buffer.concat(data)), { hello: 'world' }) | ||
t.same(Buffer.concat(data), msgPackPayload) | ||
res.statusCode = 200 | ||
@@ -35,5 +37,6 @@ res.setHeader('content-type', 'application/json') | ||
instance.post('/', (request, reply) => { | ||
t.same(request.body, payload) | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
contentType: 'application/msgpack', | ||
body: msgpack.encode(request.body) | ||
body: msgPackPayload | ||
}) | ||
@@ -57,3 +60,3 @@ }) | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -60,0 +63,0 @@ t.same(data, { something: 'else' }) |
@@ -35,3 +35,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
@@ -59,3 +59,3 @@ body: { | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -62,0 +62,0 @@ t.same(data, { hello: 'fastify' }) |
@@ -35,3 +35,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
@@ -57,3 +57,3 @@ body: '' | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -60,0 +60,0 @@ t.same(data, { hello: 'fastify' }) |
@@ -35,3 +35,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
@@ -57,3 +57,3 @@ body: null | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -60,0 +60,0 @@ t.same(data, { hello: 'fastify' }) |
@@ -33,3 +33,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
@@ -57,3 +57,3 @@ body: { | ||
} | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -60,0 +60,0 @@ t.same(data, { hello: 'fastify' }) |
@@ -32,5 +32,5 @@ 'use strict' | ||
instance.get('/test', (request, reply) => { | ||
instance.get('/test', (_request, reply) => { | ||
reply.from('/test', { | ||
getUpstream: (req, base) => { | ||
getUpstream: (_req, base) => { | ||
t.pass('getUpstream called') | ||
@@ -42,3 +42,3 @@ return `${base}:${target.address().port}` | ||
instanceWithoutBase.get('/test2', (request, reply) => { | ||
instanceWithoutBase.get('/test2', (_request, reply) => { | ||
reply.from('/test2', { | ||
@@ -45,0 +45,0 @@ getUpstream: () => { |
@@ -23,3 +23,3 @@ 'use strict' | ||
instance.get('/test', (request, reply) => { | ||
instance.get('/test', (_request, reply) => { | ||
reply.from('/test', { | ||
@@ -26,0 +26,0 @@ getUpstream: () => { |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -8,9 +8,3 @@ 'use strict' | ||
const get = require('simple-get').concat | ||
const semver = require('semver') | ||
if (semver.gte(process.version, '20.2.0')) { | ||
t.comment('skip this test on node >= 20.2.0 as it is current broken') | ||
process.exit(0) | ||
} | ||
const instance = Fastify() | ||
@@ -33,3 +27,3 @@ | ||
instance.head('/', (request, reply) => { | ||
instance.head('/', (_request, reply) => { | ||
t.pass('head received') | ||
@@ -36,0 +30,0 @@ reply.from() |
@@ -15,3 +15,3 @@ 'use strict' | ||
.get('/ip') | ||
.reply(200, function (uri, requestBody) { | ||
.reply(200, function () { | ||
t.equal(this.req.headers.host, 'httpbin.org') | ||
@@ -21,3 +21,3 @@ return { origin: '127.0.0.1' } | ||
instance.get('*', (request, reply) => { | ||
instance.get('*', (_request, reply) => { | ||
reply.from(null, { | ||
@@ -53,3 +53,3 @@ rewriteRequestHeaders: (originalReq, headers) => { | ||
.get('/ip') | ||
.reply(200, function (uri, requestBody) { | ||
.reply(200, function () { | ||
t.equal(this.req.headers.host, 'httpbin.org:8080') | ||
@@ -64,3 +64,3 @@ return { origin: '127.0.0.1' } | ||
instance.get('*', (request, reply) => { | ||
instance.get('*', (_request, reply) => { | ||
reply.from() | ||
@@ -67,0 +67,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -15,3 +15,3 @@ 'use strict' | ||
t.teardown(instance.close.bind(instance)) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -18,0 +18,0 @@ }) |
@@ -17,3 +17,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request proxied') | ||
@@ -25,3 +25,3 @@ reply.code(404).header('x-my-header', 'hello!').send({ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -13,3 +13,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -16,0 +16,0 @@ }) |
@@ -32,3 +32,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -60,3 +60,3 @@ retriesCount: 1, | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -92,3 +92,3 @@ retriesCount: 0, | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -95,0 +95,0 @@ retriesCount: 1, |
@@ -15,3 +15,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request arrives') | ||
@@ -32,3 +32,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -61,3 +61,3 @@ }) | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request arrives') | ||
@@ -75,3 +75,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -78,0 +78,0 @@ }) |
@@ -26,3 +26,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request proxied') | ||
@@ -34,3 +34,3 @@ reply.code(404).header('x-my-header', 'hello!').send({ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -37,0 +37,0 @@ }) |
@@ -28,3 +28,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request proxied') | ||
@@ -36,3 +36,3 @@ reply.code(404).header('x-my-header', 'hello!').send({ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -39,0 +39,0 @@ }) |
@@ -13,3 +13,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -16,0 +16,0 @@ }) |
@@ -17,3 +17,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request proxied') | ||
@@ -25,3 +25,3 @@ reply.code(200).send({ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -13,3 +13,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -16,0 +16,0 @@ }) |
@@ -26,3 +26,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -63,3 +63,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -66,0 +66,0 @@ }) |
@@ -26,3 +26,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -71,3 +71,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -101,3 +101,3 @@ }) | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request arrives') | ||
@@ -117,3 +117,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`) | ||
@@ -120,0 +120,0 @@ }) |
@@ -34,3 +34,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -37,0 +37,0 @@ }) |
@@ -24,3 +24,3 @@ 'use strict' | ||
t.teardown(instance.close.bind(instance)) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -27,0 +27,0 @@ }) |
@@ -36,3 +36,3 @@ 'use strict' | ||
instance.patch('/', (request, reply) => { | ||
instance.patch('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { method: 'POST' }) | ||
@@ -46,3 +46,3 @@ }) | ||
instance.addContentTypeParser('application/json', function (req, payload, done) { | ||
instance.addContentTypeParser('application/json', function (_req, payload, done) { | ||
done(null, payload) | ||
@@ -66,3 +66,3 @@ }) | ||
body: bodyString | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -69,0 +69,0 @@ const parsed = JSON.parse(data) |
@@ -24,3 +24,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -27,0 +27,0 @@ }) |
@@ -14,3 +14,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
t.fail('this should never get called') | ||
@@ -20,3 +20,3 @@ res.end('hello world') | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
try { | ||
@@ -23,0 +23,0 @@ reply.from(null, { body: 'this is the new body' }) |
@@ -14,3 +14,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
t.fail('this should never get called') | ||
@@ -20,3 +20,3 @@ res.end('hello world') | ||
instance.head('/', (request, reply) => { | ||
instance.head('/', (_request, reply) => { | ||
try { | ||
@@ -23,0 +23,0 @@ reply.from(null, { body: 'this is the new body' }) |
@@ -16,3 +16,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
t.fail('the target server should never be called') | ||
@@ -22,3 +22,3 @@ res.end() | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
const body = new Readable({ | ||
@@ -25,0 +25,0 @@ read: function () { |
@@ -15,3 +15,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request arrives') | ||
@@ -32,3 +32,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.server.address().port}/`, | ||
@@ -35,0 +35,0 @@ { |
@@ -24,3 +24,3 @@ 'use strict' | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
onResponse: (_, reply, res) => { | ||
onResponse: (_, _reply, res) => { | ||
t.equal(res.statusCode, 888) | ||
@@ -27,0 +27,0 @@ } |
@@ -39,3 +39,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -59,3 +59,3 @@ }) | ||
body: bodyString | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -62,0 +62,0 @@ const parsed = JSON.parse(data) |
@@ -36,3 +36,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -39,0 +39,0 @@ }) |
@@ -33,3 +33,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -36,0 +36,0 @@ }) |
@@ -18,3 +18,3 @@ 'use strict' | ||
{ parseAs: 'buffer', bodyLimit: 1000 }, | ||
(req, body, done) => done(null, parse(body.toString())) | ||
(_req, body, done) => done(null, parse(body.toString())) | ||
) | ||
@@ -43,3 +43,3 @@ | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -46,0 +46,0 @@ }) |
@@ -19,3 +19,3 @@ 'use strict' | ||
{ parseAs: 'buffer', bodyLimit: 1000 }, | ||
(req, body, done) => done(null, parse(body.toString())) | ||
(_req, body, done) => done(null, parse(body.toString())) | ||
) | ||
@@ -44,3 +44,3 @@ | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -80,3 +80,3 @@ }) | ||
{ parseAs: 'buffer', bodyLimit: 1000 }, | ||
(req, body, done) => done(null, parse(body.toString())) | ||
(_req, body, done) => done(null, parse(body.toString())) | ||
) | ||
@@ -87,3 +87,3 @@ | ||
{ parseAs: 'buffer', bodyLimit: 1000 }, | ||
(req, body, done) => done(null, parse(body.toString())) | ||
(_req, body, done) => done(null, parse(body.toString())) | ||
) | ||
@@ -112,3 +112,3 @@ | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -115,0 +115,0 @@ }) |
@@ -11,3 +11,3 @@ 'use strict' | ||
let requestCount = 0 | ||
return http.createServer((req, res) => { | ||
return http.createServer((_req, res) => { | ||
if (requestCount++ < stopAfter) { | ||
@@ -39,3 +39,3 @@ res.statusCode = 503 | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -65,3 +65,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -91,3 +91,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -94,0 +94,0 @@ }) |
@@ -36,3 +36,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, fromOptions) | ||
@@ -63,3 +63,3 @@ }) | ||
test("a server 500's with a custom handler and should revive", async (t) => { | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => { | ||
const customRetryLogic = ({ req, res, getDefaultDelay }) => { | ||
const defaultDelay = getDefaultDelay() | ||
@@ -85,3 +85,3 @@ if (defaultDelay) return defaultDelay | ||
// the key here is our retryDelay doesn't register the deefault handler and as a result it doesn't work | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => { | ||
const customRetryLogic = ({ req, res }) => { | ||
if (res && res.statusCode === 500 && req.method === 'GET') { | ||
@@ -106,3 +106,3 @@ return 0 | ||
test('custom retry delay functions can invoke the default delay', async (t) => { | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => { | ||
const customRetryLogic = ({ req, res, getDefaultDelay }) => { | ||
// registering the default retry logic for non 500 errors if it occurs | ||
@@ -129,3 +129,3 @@ const defaultDelay = getDefaultDelay() | ||
test('custom retry delay function inspects the err paramater', async (t) => { | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => { | ||
const customRetryLogic = ({ err }) => { | ||
if (err && (err.code === 'UND_ERR_SOCKET' || err.code === 'ECONNRESET')) { | ||
@@ -149,3 +149,3 @@ return 0.1 | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay }) => { | ||
const customRetryLogic = ({ err, attempt }) => { | ||
attemptCounter.push(attempt) | ||
@@ -172,3 +172,3 @@ | ||
const attemptCounter = [] | ||
const customRetryLogic = ({ req, res, err, attempt, getDefaultDelay, retriesCount }) => { | ||
const customRetryLogic = ({ req, res, attempt, retriesCount }) => { | ||
if (retriesCount < attempt) { | ||
@@ -175,0 +175,0 @@ return null |
@@ -24,3 +24,3 @@ 'use strict' | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
rewriteHeaders: (headers, req) => { | ||
rewriteHeaders: (_headers, req) => { | ||
t.pass('rewriteHeaders called with correct request parameter') | ||
@@ -41,3 +41,3 @@ t.equal(req, request) | ||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
get(`http://localhost:${instance.server.address().port}`, (err, res) => { | ||
t.error(err) | ||
@@ -44,0 +44,0 @@ t.equal(res.statusCode, 205) |
@@ -24,5 +24,5 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
rewriteHeaders: (headers, req) => { | ||
rewriteHeaders: (headers) => { | ||
t.pass('rewriteHeaders called') | ||
@@ -45,3 +45,3 @@ return { | ||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
get(`http://localhost:${instance.server.address().port}`, (err, res) => { | ||
t.error(err) | ||
@@ -48,0 +48,0 @@ t.equal(res.headers['content-type'], 'text/plain') |
@@ -24,3 +24,3 @@ 'use strict' | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
rewriteRequestHeaders: (originalReq, headers) => { | ||
rewriteRequestHeaders: (originalReq) => { | ||
t.pass('rewriteRequestHeaders called with correct request parameter') | ||
@@ -41,3 +41,3 @@ t.equal(originalReq, request) | ||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
get(`http://localhost:${instance.server.address().port}`, (err, res) => { | ||
t.error(err) | ||
@@ -44,0 +44,0 @@ t.equal(res.statusCode, 205) |
@@ -23,5 +23,5 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
rewriteRequestHeaders: (originalReq, headers) => { | ||
rewriteRequestHeaders: (_originalReq, headers) => { | ||
t.pass('rewriteRequestHeaders called') | ||
@@ -28,0 +28,0 @@ return Object.assign(headers, { host: 'host-override' }) |
@@ -25,9 +25,9 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
onResponse: (request, reply, res) => { | ||
onResponse: (_request, reply, res) => { | ||
reply.send( | ||
res.stream.pipe( | ||
new Transform({ | ||
transform: function (chunk, enc, cb) { | ||
transform: function (chunk, _enc, cb) { | ||
this.push(chunk.toString().toUpperCase()) | ||
@@ -34,0 +34,0 @@ cb() |
@@ -16,3 +16,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
res.statusCode = 200 | ||
@@ -22,3 +22,3 @@ res.end('hello world') | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -25,0 +25,0 @@ }) |
@@ -36,3 +36,3 @@ 'use strict' | ||
instance.post('/', (request, reply) => { | ||
instance.post('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`) | ||
@@ -46,3 +46,3 @@ }) | ||
instance.addContentTypeParser('application/json', function (req, payload, done) { | ||
instance.addContentTypeParser('application/json', function (_req, payload, done) { | ||
done(null, payload) | ||
@@ -66,3 +66,3 @@ }) | ||
body: bodyString | ||
}, (err, res, data) => { | ||
}, (err, _res, data) => { | ||
t.error(err) | ||
@@ -69,0 +69,0 @@ const parsed = JSON.parse(data) |
@@ -19,3 +19,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.header('content-disposition', header).send('OK') | ||
@@ -29,3 +29,3 @@ }) | ||
}) | ||
proxy1.get('/', (request, reply) => { | ||
proxy1.get('/', (_request, reply) => { | ||
return reply.from(`http://localhost:${instance.server.address().port}`) | ||
@@ -39,3 +39,3 @@ }) | ||
}) | ||
proxy2.get('/', (request, reply) => { | ||
proxy2.get('/', (_request, reply) => { | ||
return reply.from(`http://localhost:${proxy1.server.address().port}`) | ||
@@ -42,0 +42,0 @@ }) |
@@ -16,3 +16,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer(() => { | ||
t.fail('target never called') | ||
@@ -35,3 +35,3 @@ }) | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -38,0 +38,0 @@ }) |
@@ -31,3 +31,3 @@ 'use strict' | ||
target.get('/', (req, reply) => { | ||
target.get('/', (_req, reply) => { | ||
t.pass('request proxied') | ||
@@ -57,3 +57,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('http://myserver.local') | ||
@@ -60,0 +60,0 @@ }) |
@@ -20,3 +20,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
res.statusCode = 200 | ||
@@ -26,3 +26,3 @@ res.end() | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -29,0 +29,0 @@ }) |
@@ -17,3 +17,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -20,0 +20,0 @@ }) |
@@ -16,3 +16,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
res.statusCode = 200 | ||
@@ -22,3 +22,3 @@ res.end('hello world') | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -25,0 +25,0 @@ }) |
'use strict' | ||
const { test } = require('tap') | ||
const { test, after } = require('node:test') | ||
const { createServer } = require('node:http') | ||
const Fastify = require('fastify') | ||
const get = require('simple-get').concat | ||
const { createProxy } = require('proxy') | ||
const From = require('..') | ||
const fastifyProxyFrom = require('..') | ||
const { isIPv6 } = require('node:net') | ||
@@ -18,16 +18,32 @@ const configFormat = { | ||
test(`use undici ProxyAgent to connect through proxy - configured via ${description}`, async (t) => { | ||
t.plan(5) | ||
t.plan(3) | ||
const target = await buildServer() | ||
const proxy = await buildProxy() | ||
t.teardown(target.close.bind(target)) | ||
t.teardown(proxy.close.bind(proxy)) | ||
const targetUrl = `http://localhost:${target.address().port}` | ||
const proxyUrl = `http://localhost:${proxy.address().port}` | ||
after(() => { | ||
target.close() | ||
proxy.close() | ||
}) | ||
let targetAddress = target.address().address | ||
if (isIPv6(targetAddress)) { | ||
targetAddress = `[${targetAddress}]` | ||
} | ||
let proxyAddress = proxy.address().address | ||
if (isIPv6(proxyAddress)) { | ||
proxyAddress = `[${proxyAddress}]` | ||
} | ||
const targetUrl = `http://${targetAddress}:${target.address().port}` | ||
const proxyUrl = `http://${proxyAddress}:${proxy.address().port}` | ||
proxy.on('connect', () => { | ||
t.ok(true, 'should connect to proxy') | ||
t.assert.ok(true, 'should connect to proxy') | ||
}) | ||
target.on('request', (req, res) => { | ||
target.on('request', (_req, res) => { | ||
res.setHeader('content-type', 'application/json') | ||
@@ -38,5 +54,8 @@ res.end(JSON.stringify({ hello: 'world' })) | ||
const instance = Fastify() | ||
t.teardown(instance.close.bind(instance)) | ||
instance.register(From, { | ||
after(() => { | ||
instance.close() | ||
}) | ||
instance.register(fastifyProxyFrom, { | ||
base: targetUrl, | ||
@@ -48,22 +67,22 @@ undici: { | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
}) | ||
const executionFlow = () => new Promise((resolve) => { | ||
instance.listen({ port: 0 }, err => { | ||
t.error(err) | ||
await instance.listen({ port: 0 }) | ||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
t.error(err) | ||
t.same(res.statusCode, 200) | ||
t.match(JSON.parse(data.toString()), { hello: 'world' }) | ||
resolve() | ||
instance.close() | ||
target.close() | ||
}) | ||
}) | ||
}) | ||
let instanceAddress = proxy.address().address | ||
await executionFlow() | ||
if (isIPv6(instanceAddress)) { | ||
if (instanceAddress === '::') { | ||
instanceAddress = '::1' | ||
} else { | ||
instanceAddress = `[${instanceAddress}]` | ||
} | ||
} | ||
const response = await fetch(`http://localhost:${instance.server.address().port}`) | ||
t.assert.strictEqual(response.status, 200) | ||
t.assert.deepStrictEqual(await response.json(), { hello: 'world' }) | ||
}) | ||
@@ -70,0 +89,0 @@ } |
@@ -32,3 +32,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -60,3 +60,3 @@ retriesCount: 1, | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -92,3 +92,3 @@ retriesCount: 0, | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`http://localhost:${target.address().port}/`, { | ||
@@ -95,0 +95,0 @@ retriesCount: 1, |
@@ -40,3 +40,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -43,0 +43,0 @@ }) |
@@ -38,3 +38,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -41,0 +41,0 @@ }) |
@@ -15,3 +15,3 @@ 'use strict' | ||
target.get('/', (request, reply) => { | ||
target.get('/', (_request, reply) => { | ||
t.pass('request arrives') | ||
@@ -37,3 +37,3 @@ | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -40,0 +40,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('/hello') | ||
@@ -28,0 +28,0 @@ }) |
@@ -25,3 +25,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
@@ -28,0 +28,0 @@ }) |
@@ -12,3 +12,3 @@ 'use strict' | ||
return { | ||
request: (opts, callback) => { callback(new Error('foo')) }, | ||
request: (_opts, callback) => { callback(new Error('foo')) }, | ||
close: () => {} | ||
@@ -24,3 +24,3 @@ } | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.code(201) | ||
@@ -27,0 +27,0 @@ reply.from() |
@@ -29,3 +29,3 @@ 'use strict' | ||
const target = http.createServer((req, res) => { | ||
const target = http.createServer((_req, res) => { | ||
t.fail('no response') | ||
@@ -35,3 +35,3 @@ res.end() | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`unix+http://${querystring.escape(socketPath)}/hello`) | ||
@@ -48,3 +48,3 @@ }) | ||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
get(`http://localhost:${instance.server.address().port}`, (err, res) => { | ||
t.error(err) | ||
@@ -51,0 +51,0 @@ t.equal(res.statusCode, 500) |
@@ -42,3 +42,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('hello') | ||
@@ -45,0 +45,0 @@ }) |
@@ -45,3 +45,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('hello') | ||
@@ -48,0 +48,0 @@ }) |
@@ -48,3 +48,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from('hello') | ||
@@ -51,0 +51,0 @@ }) |
@@ -48,3 +48,3 @@ 'use strict' | ||
instance.get('/', (request, reply) => { | ||
instance.get('/', (_request, reply) => { | ||
reply.from(`unix+https://${querystring.escape(socketPath)}/hello`) | ||
@@ -51,0 +51,0 @@ }) |
@@ -12,3 +12,3 @@ /// <reference types="node" /> | ||
RouteGenericInterface | ||
} from 'fastify'; | ||
} from 'fastify' | ||
@@ -20,3 +20,3 @@ import { | ||
RequestOptions, | ||
} from "http"; | ||
} from 'http' | ||
import { | ||
@@ -27,3 +27,3 @@ ClientSessionOptions, | ||
SecureClientSessionOptions, | ||
} from "http2"; | ||
} from 'http2' | ||
import { | ||
@@ -33,7 +33,6 @@ Agent as SecureAgent, | ||
RequestOptions as SecureRequestOptions | ||
} from "https"; | ||
import { Pool } from 'undici'; | ||
import { ProxyAgent } from 'undici'; | ||
} from 'https' | ||
import { Pool, ProxyAgent } from 'undici' | ||
declare module "fastify" { | ||
declare module 'fastify' { | ||
interface FastifyReply { | ||
@@ -53,3 +52,3 @@ from( | ||
request: FastifyRequest<RequestGenericInterface, RawServerBase> | ||
) => string; | ||
) => string | ||
@@ -113,3 +112,3 @@ export type RetryDetails = { | ||
http2?: Http2Options | boolean; | ||
undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options }; | ||
undici?: Pool.Options & { proxy?: string | URL | ProxyAgent.Options }; | ||
contentTypesToEncode?: string[]; | ||
@@ -124,6 +123,6 @@ retryMethods?: (HTTPMethods | 'TRACE')[]; | ||
export const fastifyReplyFrom: FastifyReplyFrom | ||
export { fastifyReplyFrom as default }; | ||
export { fastifyReplyFrom as default } | ||
} | ||
declare function fastifyReplyFrom(...params: Parameters<FastifyReplyFrom>): ReturnType<FastifyReplyFrom> | ||
declare function fastifyReplyFrom (...params: Parameters<FastifyReplyFrom>): ReturnType<FastifyReplyFrom> | ||
export = fastifyReplyFrom |
@@ -1,13 +0,13 @@ | ||
import fastify, { FastifyReply, FastifyRequest, RawReplyDefaultExpression, RawServerBase, RequestGenericInterface, RouteGenericInterface } from "fastify"; | ||
import * as http from 'http'; | ||
import { IncomingHttpHeaders } from "http2"; | ||
import * as https from 'https'; | ||
import { AddressInfo } from "net"; | ||
import { expectType } from 'tsd'; | ||
import replyFrom, { FastifyReplyFromOptions } from ".."; | ||
import fastify, { FastifyReply, FastifyRequest, RawReplyDefaultExpression, RawServerBase, RequestGenericInterface, RouteGenericInterface } from 'fastify' | ||
import * as http from 'node:http' | ||
import { IncomingHttpHeaders } from 'http2' | ||
import * as https from 'node:https' | ||
import { AddressInfo } from 'net' | ||
import { expectType } from 'tsd' | ||
import replyFrom, { FastifyReplyFromOptions } from '..' | ||
// @ts-ignore | ||
import tap from 'tap'; | ||
import tap from 'tap' | ||
const fullOptions: FastifyReplyFromOptions = { | ||
base: "http://example2.com", | ||
base: 'http://example2.com', | ||
http: { | ||
@@ -50,105 +50,106 @@ agentOptions: { | ||
destroyAgent: true | ||
}; | ||
} | ||
async function main() { | ||
const server = fastify(); | ||
async function main () { | ||
const server = fastify() | ||
server.register(replyFrom); | ||
server.register(replyFrom) | ||
server.register(replyFrom, {}); | ||
server.register(replyFrom, {}) | ||
server.register(replyFrom, {http2: true}); | ||
server.register(replyFrom, { http2: true }) | ||
server.register(replyFrom, fullOptions); | ||
server.register(replyFrom, fullOptions) | ||
server.register(replyFrom, { undici: { proxy: new URL('http://example2.com:8080') } }); | ||
server.register(replyFrom, { undici: { proxy: new URL('http://example2.com:8080') } }) | ||
server.register(replyFrom, { undici: { proxy: { uri: 'http://example2.com:8080' } } }); | ||
server.register(replyFrom, { undici: { proxy: { uri: 'http://example2.com:8080' } } }) | ||
server.get("/v1", (request, reply) => { | ||
expectType<FastifyReply>(reply.from()); | ||
}); | ||
server.get("/v3", (request, reply) => { | ||
reply.from("/v3", { | ||
body: {hello: "world"}, | ||
rewriteRequestHeaders(req, headers) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req); | ||
return headers; | ||
}, | ||
getUpstream(req, base) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req); | ||
return base; | ||
}, | ||
onResponse(request, reply, res) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request); | ||
expectType<FastifyReply<RouteGenericInterface, RawServerBase>>(reply); | ||
expectType<RawReplyDefaultExpression<RawServerBase>>(res); | ||
expectType<number>(res.statusCode); | ||
} | ||
}); | ||
}); | ||
server.get('/v1', (_request, reply) => { | ||
expectType<FastifyReply>(reply.from()) | ||
}) | ||
server.get('/v3', (_request, reply) => { | ||
reply.from('/v3', { | ||
body: { hello: 'world' }, | ||
rewriteRequestHeaders (req, headers) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req) | ||
return headers | ||
}, | ||
getUpstream (req, base) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(req) | ||
return base | ||
}, | ||
onResponse (request, reply, res) { | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request) | ||
expectType<FastifyReply<RouteGenericInterface, RawServerBase>>(reply) | ||
expectType<RawReplyDefaultExpression<RawServerBase>>(res) | ||
expectType<number>(res.statusCode) | ||
} | ||
}) | ||
}) | ||
// http2 | ||
const instance = fastify({http2: true}); | ||
// http2 | ||
const instance = fastify({ http2: true }) | ||
// @ts-ignore | ||
tap.tearDown(instance.close.bind(instance)); | ||
const target = fastify({http2: true}); | ||
tap.tearDown(instance.close.bind(instance)) | ||
const target = fastify({ http2: true }) | ||
// @ts-ignore | ||
tap.tearDown(target.close.bind(target)); | ||
instance.get("/", (request, reply) => { | ||
reply.from(); | ||
}); | ||
tap.tearDown(target.close.bind(target)) | ||
instance.get('/', (_request, reply) => { | ||
reply.from() | ||
}) | ||
instance.get("/http2", (request, reply) => { | ||
reply.from("/", { | ||
method: "POST", | ||
retryDelay: ({err, req, res, attempt, retriesCount, getDefaultDelay }) => { | ||
const defaultDelay = getDefaultDelay(); | ||
if (defaultDelay) return defaultDelay; | ||
instance.get('/http2', (_request, reply) => { | ||
reply.from('/', { | ||
method: 'POST', | ||
// eslint-disable-next-line n/handle-callback-err -- Not a real request, not handling errors | ||
retryDelay: ({ req, res, getDefaultDelay }) => { | ||
const defaultDelay = getDefaultDelay() | ||
if (defaultDelay) return defaultDelay | ||
if (res && res.statusCode === 500 && req.method === "GET") { | ||
return 300; | ||
} | ||
return null; | ||
}, | ||
rewriteHeaders(headers, req) { | ||
return headers; | ||
}, | ||
rewriteRequestHeaders(req, headers: IncomingHttpHeaders) { | ||
return headers; | ||
}, | ||
getUpstream(req, base) { | ||
return base; | ||
}, | ||
onError(reply: FastifyReply<RouteGenericInterface, RawServerBase>, error) { | ||
return reply.send(error.error); | ||
}, | ||
queryString(search, reqUrl, request) { | ||
expectType<string | undefined>(search); | ||
expectType<string>(reqUrl); | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request); | ||
return ''; | ||
}, | ||
}); | ||
}); | ||
if (res && res.statusCode === 500 && req.method === 'GET') { | ||
return 300 | ||
} | ||
return null | ||
}, | ||
rewriteHeaders (headers) { | ||
return headers | ||
}, | ||
rewriteRequestHeaders (_req, headers: IncomingHttpHeaders) { | ||
return headers | ||
}, | ||
getUpstream (_req, base) { | ||
return base | ||
}, | ||
onError (reply: FastifyReply<RouteGenericInterface, RawServerBase>, error) { | ||
return reply.send(error.error) | ||
}, | ||
queryString (search, reqUrl, request) { | ||
expectType<string | undefined>(search) | ||
expectType<string>(reqUrl) | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerBase>>(request) | ||
return '' | ||
}, | ||
}) | ||
}) | ||
await target.listen({ port: 0 }); | ||
const port = (target.server.address() as AddressInfo).port; | ||
await target.listen({ port: 0 }) | ||
const port = (target.server.address() as AddressInfo).port | ||
instance.register(replyFrom, { | ||
base: `http://localhost:${port}`, | ||
http2: { | ||
sessionOptions: { | ||
rejectUnauthorized: false, | ||
}, | ||
base: `http://localhost:${port}`, | ||
http2: { | ||
sessionOptions: { | ||
rejectUnauthorized: false, | ||
}, | ||
}); | ||
}, | ||
}) | ||
instance.register(replyFrom, { | ||
base: `http://localhost:${port}`, | ||
http2: true, | ||
}); | ||
await instance.listen({ port: 0 }); | ||
}) | ||
await instance.listen({ port: 0 }) | ||
const undiciInstance = fastify(); | ||
const undiciInstance = fastify() | ||
undiciInstance.register(replyFrom, { | ||
base: "http://example2.com", | ||
base: 'http://example2.com', | ||
undici: { | ||
@@ -158,9 +159,9 @@ pipelining: 10, | ||
} | ||
}); | ||
await undiciInstance.ready(); | ||
}) | ||
await undiciInstance.ready() | ||
tap.pass('done'); | ||
tap.end(); | ||
tap.pass('done') | ||
tap.end() | ||
} | ||
main(); | ||
main() |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
202244
19
115
5807
2