MCPcopy
hub / github.com/fastify/fastify / sendStream

Function sendStream

lib/reply.js:813–866  ·  lib/reply.js::sendStream
(payload, res, reply)

Source from the content-addressed store, hash-verified

811}
812
813function sendStream (payload, res, reply) {
814 let sourceOpen = true
815 let errorLogged = false
816
817 class="cm">// set trailer when stream ended
818 sendStreamTrailer(payload, res, reply)
819
820 eos(payload, { readable: true, writable: false }, function (err) {
821 sourceOpen = false
822 if (err != null) {
823 if (res.headersSent || reply.request.raw.aborted === true) {
824 if (!errorLogged) {
825 errorLogged = true
826 logStreamError(reply.log, err, reply)
827 }
828 res.destroy()
829 } else {
830 onErrorHook(reply, err)
831 }
832 }
833 class="cm">// there is nothing to do if there is not an error
834 })
835
836 eos(res, function (err) {
837 if (sourceOpen) {
838 if (err != null && res.headersSent && !errorLogged) {
839 errorLogged = true
840 logStreamError(reply.log, err, res)
841 }
842 if (typeof payload.destroy === class="st">'function') {
843 payload.destroy()
844 } else if (typeof payload.close === class="st">'function') {
845 payload.close(noop)
846 } else if (typeof payload.abort === class="st">'function') {
847 payload.abort()
848 } else {
849 reply.log.warn(class="st">'stream payload does not end properly')
850 }
851 }
852 })
853
854 class="cm">// streams will error asynchronously, and we want to handle that error
855 class="cm">// appropriately, e.g. a 404 for a missing file. So we cannot use
856 class="cm">// writeHead, and we need to resort to setHeader, which will trigger
857 class="cm">// a writeHead when there is data to send.
858 if (!res.headersSent) {
859 for (const key in reply[kReplyHeaders]) {
860 res.setHeader(key, reply[kReplyHeaders][key])
861 }
862 } else {
863 reply.log.warn(class="st">'response will send, but you shouldn\'t use res.writeHead in stream mode')
864 }
865 payload.pipe(res)
866}
867
868function sendTrailer (payload, res, reply) {
869 if (reply[kReplyTrailers] === null) {

Callers 1

onSendEndFunction · 0.85

Calls 5

sendStreamTrailerFunction · 0.85
logStreamErrorFunction · 0.85
onErrorHookFunction · 0.85
closeMethod · 0.80
warnMethod · 0.80

Tested by

no test coverage detected