Skip to content

Commit c48318c

Browse files
Streams bug fix, version 1.0.32
1 parent 68115fa commit c48318c

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uquik",
3-
"version": "1.0.31",
3+
"version": "1.0.32",
44
"description": "uQuik HTTP(S) framework",
55
"main": "index.js",
66
"scripts": {
@@ -44,4 +44,4 @@
4444
"eslint-plugin-promise": "^6.0.0",
4545
"nanobench": "^2.1.1"
4646
}
47-
}
47+
}

src/Response.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class Response extends Writable {
330330
// Attempt to write the body to the client and end the response
331331
if (!this.streaming && !body) {
332332
// Add Content-Length where it's needed
333-
if (!this.hasContentLength && this.wrapped_request.method !== 'HEAD') this.raw_response.writeHeader('Content-Length', '0')
333+
if (!this.chunked && !this.hasContentLength && this.wrapped_request.method !== 'HEAD') this.raw_response.writeHeader('Content-Length', '0')
334334
// Send the response with the uWS.HttpResponse.endWithoutBody(length, close_connection) method as we have no body data
335335
// NOTE: This method is completely undocumented by uWS but exists in the source code to solve the problem of no body being sent with a custom content-length
336336
this.raw_response.endWithoutBody()
@@ -417,22 +417,32 @@ class Response extends Writable {
417417
// Pause the readable stream to prevent any further data from being read
418418
stream.pause()
419419

420+
this.raw_response.stream_lastOffset = lastOffset
421+
this.raw_response.stream_chunk = chunk
422+
420423
// Bind a drain handler which will resume the once the backpressure is cleared
421424
this.drain((offset) => {
422-
// On failure the timeout will start
423-
const [ok, done] = this.raw_response.tryEnd(chunk.slice(offset - lastOffset), totalSize)
424-
if (done) {
425-
if (!stream.destroyed) stream.destroy()
426-
} else if (ok) {
427-
// We sent a chunk and it was not the last one, so let's resume reading.
428-
// Timeout is still disabled, so we can spend any amount of time waiting
429-
// for more chunks to send.
430-
if (stream.isPaused()) stream.resume()
425+
if (this.completed) return !stream.destroyed && stream.destroy()
426+
427+
if (totalSize) {
428+
const [ok, done] = this.raw_response.tryEnd(this.raw_response.stream_chunk.slice(offset - this.raw_response.stream_lastOffset), totalSize)
429+
if (done) {
430+
if (!stream.destroyed) stream.destroy()
431+
} else if (ok) {
432+
// We sent a chunk and it was not the last one, so let's resume reading.
433+
// Timeout is still disabled, so we can spend any amount of time waiting
434+
// for more chunks to send.
435+
if (stream.readable && stream.isPaused()) stream.resume()
436+
}
437+
438+
// We always have to return true/false in onWritable.
439+
// If you did not send anything, return true for success.
440+
return ok
441+
} else {
442+
if (stream.readable && stream.isPaused()) stream.resume()
443+
444+
return !stream.isPaused()
431445
}
432-
433-
// We always have to return true/false in onWritable.
434-
// If you did not send anything, return true for success.
435-
return ok
436446
})
437447
}
438448
}
@@ -452,6 +462,7 @@ class Response extends Writable {
452462

453463
// Do not allow streaming if response has already been aborted or completed
454464
if (!this.completed) {
465+
this.chunked = true
455466
// Bind an 'abort' event handler which will destroy the consumed stream if request is aborted
456467
this.on('abort', () => {
457468
if (!readable.destroyed) readable.destroy()
@@ -465,8 +476,7 @@ class Response extends Writable {
465476

466477
// Bind listeners to end request on stream closure if no total size was specified and thus we delivered with chunked transfer
467478
if (!totalSize) {
468-
const endRequest = () => this.send()
469-
readable.once('end', endRequest)
479+
readable.once('end', () => this.send())
470480
}
471481
}
472482
}

0 commit comments

Comments
 (0)