(reply, payload)
| 575 | } |
| 576 | |
| 577 | function onSendEnd (reply, payload) { |
| 578 | const res = reply.raw |
| 579 | const req = reply.request |
| 580 | |
| 581 | // we check if we need to update the trailers header and set it |
| 582 | if (reply[kReplyTrailers] !== null) { |
| 583 | const trailerHeaders = Object.keys(reply[kReplyTrailers]) |
| 584 | let header = '' |
| 585 | for (const trailerName of trailerHeaders) { |
| 586 | if (typeof reply[kReplyTrailers][trailerName] !== 'function') continue |
| 587 | header += ' ' |
| 588 | header += trailerName |
| 589 | } |
| 590 | // it must be chunked for trailer to work |
| 591 | reply.header('Transfer-Encoding', 'chunked') |
| 592 | reply.header('Trailer', header.trim()) |
| 593 | } |
| 594 | |
| 595 | // since Response contain status code, headers and body, |
| 596 | // we need to update the status, add the headers and use it's body as payload |
| 597 | // before continuing |
| 598 | if (payload != null && typeof payload === 'object' && toString.call(payload) === '[object Response]') { |
| 599 | // https://developer.mozilla.org/en-US/docs/Web/API/Response/status |
| 600 | if (typeof payload.status === 'number') { |
| 601 | reply.code(payload.status) |
| 602 | } |
| 603 | |
| 604 | // https://developer.mozilla.org/en-US/docs/Web/API/Response/headers |
| 605 | if (typeof payload.headers === 'object' && typeof payload.headers.forEach === 'function') { |
| 606 | for (const [headerName, headerValue] of payload.headers) { |
| 607 | reply.header(headerName, headerValue) |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // https://developer.mozilla.org/en-US/docs/Web/API/Response/body |
| 612 | if (payload.body !== null) { |
| 613 | if (payload.bodyUsed) { |
| 614 | throw new FST_ERR_REP_RESPONSE_BODY_CONSUMED() |
| 615 | } |
| 616 | } |
| 617 | // Keep going, body is either null or ReadableStream |
| 618 | payload = payload.body |
| 619 | } |
| 620 | const statusCode = res.statusCode |
| 621 | |
| 622 | if (payload === undefined || payload === null) { |
| 623 | // according to https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2 |
| 624 | // we cannot send a content-length for 304 and 204, and all status code |
| 625 | // < 200 |
| 626 | // A sender MUST NOT send a Content-Length header field in any message |
| 627 | // that contains a Transfer-Encoding header field. |
| 628 | // For HEAD we don't overwrite the `content-length` |
| 629 | if (statusCode >= 200 && statusCode !== 204 && statusCode !== 304 && req.method !== 'HEAD' && reply[kReplyTrailers] === null) { |
| 630 | reply[kReplyHeaders]['content-length'] = '0' |
| 631 | } |
| 632 | |
| 633 | safeWriteHead(reply, statusCode) |
| 634 | sendTrailer(payload, res, reply) |
no test coverage detected