Skip to content
This repository was archived by the owner on Nov 16, 2025. It is now read-only.

Commit 4fbd362

Browse files
committed
Describe atEnd, and actually do something with any pending stuff we had saved but did not have a \r\n to complete it
1 parent 3f558c4 commit 4fbd362

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ module.exports = {
124124

125125
this.push(data) // Just pass it on. for now.
126126
next(); // Ready for next chunk.
127+
}, function atEnd(next) {
128+
// through2 takes 2 functions: forEachChunk and atEnd -- this is the second one,
129+
// it gets called when the data incoming to through2 ends. Any clean-up or final
130+
// actions we need to take before we stop getting called happen here.
131+
132+
// there's still a stream 'end' event that has to happen, after this stream will
133+
// do no more. We don't want a race between our cleanup and the caller's last read
134+
// or the end event. They may assume they got it all if end was emitted before this,
135+
// but we had one more thing to do, so we have to tell through2 we're not just done,
136+
// but done-done.
137+
//
138+
// If we had no atEnd, it would just send 'end' when we're done. But we had one thing
139+
// pending.
140+
141+
// So if we had leftovers, let's put them in headers. This isn't correct, but
142+
// vanishing data in a closure variable is no good either.
143+
response.rawHeaders.push(partial);
144+
next();
127145
})).pipe(response);
128146
// Whatever comes out of our header-splitting stream parser must be the
129147
// body. Because that's what we designed, right?

0 commit comments

Comments
 (0)