From 8ac0eb7c98c60767b0fbf8a222110ad962b819e3 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Fri, 8 Feb 2019 09:01:48 -0700 Subject: [PATCH 1/2] Changes signature to include request, reply, and stream (not just stream). Updates existing test --- index.js | 13 ++++++++++--- test/transform-body.js | 39 +++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 55584370..f0d3549c 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,11 @@ const lru = require('tiny-lru') const querystring = require('querystring') const Stream = require('stream') const buildRequest = require('./lib/request') -const { filterPseudoHeaders, copyHeaders, stripHttp1ConnectionHeaders } = require('./lib/utils') +const { + filterPseudoHeaders, + copyHeaders, + stripHttp1ConnectionHeaders +} = require('./lib/utils') module.exports = fp(function from (fastify, opts, next) { const cache = lru(opts.cacheURLs || 100) @@ -94,13 +98,16 @@ module.exports = fp(function from (fastify, opts, next) { } this.request.log.info('response received') if (sourceHttp2) { - copyHeaders(rewriteHeaders(stripHttp1ConnectionHeaders(res.headers)), this) + copyHeaders( + rewriteHeaders(stripHttp1ConnectionHeaders(res.headers)), + this + ) } else { copyHeaders(rewriteHeaders(res.headers), this) } this.code(res.statusCode) if (onResponse) { - onResponse(res.stream) + onResponse(this.request.req, this, res.stream) } else { this.send(res.stream) } diff --git a/test/transform-body.js b/test/transform-body.js index 830561c2..6f9303eb 100644 --- a/test/transform-body.js +++ b/test/transform-body.js @@ -24,31 +24,38 @@ const target = http.createServer((req, res) => { instance.get('/', (request, reply) => { reply.from(`http://localhost:${target.address().port}`, { - onResponse: (res) => { - reply.send(res.pipe(new Transform({ - transform: function (chunk, enc, cb) { - this.push(chunk.toString().toUpperCase()) - cb() - } - }))) + onResponse: (request, reply, res) => { + reply.send( + res.pipe( + new Transform({ + transform: function (chunk, enc, cb) { + this.push(chunk.toString().toUpperCase()) + cb() + } + }) + ) + ) } }) }) t.tearDown(target.close.bind(target)) -instance.listen(0, (err) => { +instance.listen(0, err => { t.error(err) - target.listen(0, (err) => { + target.listen(0, err => { t.error(err) - get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { - t.error(err) - t.equal(res.headers['content-type'], 'text/plain') - t.equal(res.headers['x-my-header'], 'hello!') - t.equal(res.statusCode, 205) - t.equal(data.toString(), 'HELLO WORLD') - }) + get( + `http://localhost:${instance.server.address().port}`, + (err, res, data) => { + t.error(err) + t.equal(res.headers['content-type'], 'text/plain') + t.equal(res.headers['x-my-header'], 'hello!') + t.equal(res.statusCode, 205) + t.equal(data.toString(), 'HELLO WORLD') + } + ) }) }) From 6d1f2a701ff55fd7dc218cb82ebd6ce76ae9edd1 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Sat, 9 Feb 2019 15:22:42 -0700 Subject: [PATCH 2/2] Update README to show contract change for onResponse --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 66880143..49398eda 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ instance with a `from` method, which will reply to the original request __from the desired source__. The options allows to override any part of the request or response being sent or received to/from the source. -#### onResponse(res) +#### onResponse(request, reply, res) Called when an http response is received from the source. The default behavior is `reply.send(res)`, which will be disabled if the