Skip to content

Commit d299a7d

Browse files
Stream fix
1 parent ea529f2 commit d299a7d

File tree

2 files changed

+38
-36
lines changed

2 files changed

+38
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uquik",
3-
"version": "1.0.42",
3+
"version": "1.0.43",
44
"description": "uQuik HTTP(S) framework",
55
"main": "index.js",
66
"scripts": {

src/Response.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -426,47 +426,49 @@ class Response extends Writable {
426426
return
427427
}
428428

429-
if (!sent) {
430-
// Remember the initial write offset for future backpressure sliced chunks
431-
const writeOffset = this.write_offset
432-
// Pause the readable stream to prevent any further data from being read as chunk was not fully sent
433-
if (stream.readable && !stream.isPaused()) stream.pause()
434-
435-
// Bind a drain handler to relieve backpressure
436-
// Note! This callback may be called as many times as neccessary to send a full chunk when using the tryEnd method
437-
this.drain((offset) => {
438-
// Check if the response has been completed / connection has been closed
439-
if (this.completed) {
440-
// Destroy the readable stream as no more writing will occur
441-
if (!stream.destroyed) stream.destroy()
429+
if (sent) return
442430

443-
// Return true to signify this was a no-op
444-
return true
445-
}
431+
// Pause the readable stream to prevent any further data from being read as chunk was not fully sent
432+
if (stream.readable && !stream.isPaused()) stream.pause()
446433

447-
// If we have a total size then we need to serve sliced chunks as uWS does not buffer under the hood
448-
if (totalSize) {
449-
// Slice the chunk to the correct offset and send it to the client
450-
const [flushed, ended] = this._uws_write_chunk(chunk.slice(offset - writeOffset), totalSize)
451-
if (ended) {
452-
// Destroy the readable stream as no more writing will occur
453-
if (!stream.destroyed) stream.destroy()
454-
} else if (flushed) {
455-
// Resume the readable stream to allow more data to be read
456-
if (stream.readable && stream.isPaused()) stream.resume()
457-
}
458-
459-
// Return the flushed boolean as that signifies whether this specific chunk was fully sent
460-
return flushed
461-
}
434+
// Remember the chunkk and initial write offset for future backpressure sliced chunks
435+
this.raw_response.chunk = chunk
436+
this.raw_response.writeOffset = this.write_offset
462437

463-
// Resume the readable stream to allow more data to be read
464-
if (stream.readable && stream.isPaused()) stream.resume()
438+
// Bind a drain handler to relieve backpressure
439+
// Note! This callback may be called as many times as neccessary to send a full chunk when using the tryEnd method
440+
this.drain((offset) => {
441+
// Check if the response has been completed / connection has been closed
442+
if (this.completed) {
443+
// Destroy the readable stream as no more writing will occur
444+
if (!stream.destroyed) stream.destroy()
465445

466446
// Return true to signify this was a no-op
467447
return true
468-
})
469-
}
448+
}
449+
450+
// If we have a total size then we need to serve sliced chunks as uWS does not buffer under the hood
451+
if (totalSize) {
452+
// Slice the chunk to the correct offset and send it to the client
453+
const [flushed, ended] = this._uws_write_chunk(this.raw_response.chunk.slice(offset - this.raw_response.writeOffset), totalSize)
454+
if (ended) {
455+
// Destroy the readable stream as no more writing will occur
456+
if (!stream.destroyed) stream.destroy()
457+
} else if (flushed) {
458+
// Resume the readable stream to allow more data to be read
459+
if (stream.readable && stream.isPaused()) stream.resume()
460+
}
461+
462+
// Return the flushed boolean as that signifies whether this specific chunk was fully sent
463+
return flushed
464+
}
465+
466+
// Resume the readable stream to allow more data to be read
467+
if (stream.readable && stream.isPaused()) stream.resume()
468+
469+
// Return true to signify this was a no-op
470+
return true
471+
})
470472
}
471473
}
472474

0 commit comments

Comments
 (0)