(payload, res, reply)
| 866 | } |
| 867 | |
| 868 | function 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 | |
| 925 | function sendStreamTrailer (payload, res, reply) { |
no test coverage detected