Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
39 changes: 23 additions & 16 deletions test/transform-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
)
})
})