Skip to content

Commit

Permalink
fix: reorder handling of Response replies (#5612) (#5629)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Aug 17, 2024
1 parent 74be1a3 commit 22affe7
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 24 deletions.
44 changes: 20 additions & 24 deletions lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,30 @@ function onSendEnd (reply, payload) {
reply.header('Trailer', header.trim())
}

// since Response contain status code, we need to update before
// any action that used statusCode
const isResponse = toString.call(payload) === '[object Response]'
if (isResponse) {
// since Response contain status code, headers and body,
// we need to update the status, add the headers and use it's body as payload
// before continuing
if (toString.call(payload) === '[object Response]') {
// https://developer.mozilla.org/en-US/docs/Web/API/Response/status
if (typeof payload.status === 'number') {
reply.code(payload.status)
}

// https://developer.mozilla.org/en-US/docs/Web/API/Response/headers
if (typeof payload.headers === 'object' && typeof payload.headers.forEach === 'function') {
for (const [headerName, headerValue] of payload.headers) {
reply.header(headerName, headerValue)
}
}

// https://developer.mozilla.org/en-US/docs/Web/API/Response/body
if (payload.body !== null) {
if (payload.bodyUsed) {
throw new FST_ERR_REP_RESPONSE_BODY_CONSUMED()
}
}
// Keep going, body is either null or ReadableStream
payload = payload.body
}
const statusCode = res.statusCode

Expand Down Expand Up @@ -656,26 +672,6 @@ function onSendEnd (reply, payload) {
return
}

// Response
if (isResponse) {
// https://developer.mozilla.org/en-US/docs/Web/API/Response/headers
if (typeof payload.headers === 'object' && typeof payload.headers.forEach === 'function') {
for (const [headerName, headerValue] of payload.headers) {
reply.header(headerName, headerValue)
}
}

// https://developer.mozilla.org/en-US/docs/Web/API/Response/body
if (payload.body != null) {
if (payload.bodyUsed) {
throw new FST_ERR_REP_RESPONSE_BODY_CONSUMED()
}
// Response.body always a ReadableStream
sendWebStream(payload.body, res, reply)
}
return
}

if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {
throw new FST_ERR_REP_INVALID_PAYLOAD_TYPE(typeof payload)
}
Expand Down
75 changes: 75 additions & 0 deletions test/web-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,81 @@ test('should response with a Response', async (t) => {
t.equal(headers.hello, 'world')
})

test('should response with a Response 204', async (t) => {
t.plan(3)

const fastify = Fastify()

fastify.get('/', function (request, reply) {
reply.send(new Response(null, {
status: 204,
headers: {
hello: 'world'
}
}))
})

const {
statusCode,
headers,
body
} = await fastify.inject({ method: 'GET', path: '/' })

t.equal(statusCode, 204)
t.equal(body, '')
t.equal(headers.hello, 'world')
})

test('should response with a Response 304', async (t) => {
t.plan(3)

const fastify = Fastify()

fastify.get('/', function (request, reply) {
reply.send(new Response(null, {
status: 304,
headers: {
hello: 'world'
}
}))
})

const {
statusCode,
headers,
body
} = await fastify.inject({ method: 'GET', path: '/' })

t.equal(statusCode, 304)
t.equal(body, '')
t.equal(headers.hello, 'world')
})

test('should response with a Response without body', async (t) => {
t.plan(3)

const fastify = Fastify()

fastify.get('/', function (request, reply) {
reply.send(new Response(null, {
status: 200,
headers: {
hello: 'world'
}
}))
})

const {
statusCode,
headers,
body
} = await fastify.inject({ method: 'GET', path: '/' })

t.equal(statusCode, 200)
t.equal(body, '')
t.equal(headers.hello, 'world')
})

test('able to use in onSend hook - ReadableStream', async (t) => {
t.plan(4)

Expand Down

0 comments on commit 22affe7

Please sign in to comment.