MCPcopy
hub / github.com/fastify/fastify / sendTrailer

Function sendTrailer

lib/reply.js:868–923  ·  view source on GitHub ↗
(payload, res, reply)

Source from the content-addressed store, hash-verified

866}
867
868function sendTrailer (payload, res, reply) {
869 if (reply[kReplyTrailers] === null) {
870 // when no trailer, we close the stream
871 res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
872 return
873 }
874 const trailerHeaders = Object.keys(reply[kReplyTrailers])
875 const trailers = {}
876 let handled = 0
877 let skipped = true
878 let sent = false
879 function send () {
880 // add trailers when all handler handled
881 /* istanbul ignore else */
882 if (handled === 0 && !sent) {
883 sent = true
884 res.addTrailers(trailers)
885 // we need to properly close the stream
886 // after trailers sent
887 res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
888 }
889 }
890
891 for (const trailerName of trailerHeaders) {
892 if (typeof reply[kReplyTrailers][trailerName] !== 'function') continue
893 skipped = false
894 handled--
895
896 let cbAlreadyCalled = false
897 function cb (err, value) {
898 if (cbAlreadyCalled) return
899 cbAlreadyCalled = true
900 handled++
901
902 // we can safely ignore error for trailer
903 // since it does affect the client
904 // we log in here only for debug usage
905 if (err) reply.log.debug(err)
906 else trailers[trailerName] = value
907
908 // we push the check to the end of event
909 // loop, so the registration continue to
910 // process.
911 process.nextTick(send)
912 }
913
914 const result = reply[kReplyTrailers][trailerName](reply, payload, cb)
915 if (typeof result === 'object' && typeof result.then === 'function') {
916 result.then((v) => cb(null, v), cb)
917 }
918 }
919
920 // when all trailers are skipped
921 // we need to close the stream
922 if (skipped) res.end(null, null, null) // avoid ArgumentsAdaptorTrampoline from V8
923}
924
925function sendStreamTrailer (payload, res, reply) {

Callers 4

onSendEndFunction · 0.85
writePayloadFunction · 0.85
onReadFunction · 0.85
sendStreamTrailerFunction · 0.85

Calls 2

cbFunction · 0.85
thenMethod · 0.80

Tested by

no test coverage detected