Skip to content

Commit

Permalink
Merge pull request #240 from pkaminski/patch-2
Browse files Browse the repository at this point in the history
Correctly send single-fragment websocket streams
  • Loading branch information
kartikk221 authored Mar 25, 2024
2 parents fba35c7 + 5d09f95 commit e4a5c7c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/components/ws/Websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,16 @@ class Websocket extends EventEmitter {

// Create a callback for ending the readable consumption
const end_stream = () => {
// Retrieve the last buffered fragment to send as last chunk
// Retrieve the last buffered fragment to send as last or only chunk
const fragment = scope._buffer_fragment();

// Stream the final chunk as last fragment and cleanup the readable
scope._stream_chunk(scope.#stream, FRAGMENTS.LAST, fragment, is_binary);
if (is_first) {
scope.#ws.send(fragment, is_binary);
} else {
// Stream the final chunk as last fragment
scope._stream_chunk(scope.#stream, FRAGMENTS.LAST, fragment, is_binary);
}
// Clean up the readable
scope.#stream = undefined;
resolve();
};
Expand Down Expand Up @@ -394,14 +399,19 @@ class Websocket extends EventEmitter {

// Create a callback for ending the writable usage
const end_stream = () => {
// Retrieve the last buffered fragment to write as last chunk
// Retrieve the last buffered fragment to write as last or only chunk
const fragment = scope._buffer_fragment();

// Write the final empty chunk as last fragment and cleanup the writable
scope._write(FRAGMENTS.LAST, fragment, true, false, () => (scope.#stream = undefined));
if (is_first) {
scope.#ws.send(fragment, true, false);
scope.#ws.stream = undefined;
} else {
// Write the final empty chunk as last fragment and cleanup the writable
scope._write(FRAGMENTS.LAST, fragment, true, false, () => (scope.#stream = undefined));
}
};

// Bind listeners to end the framented write procedure
// Bind listeners to end the fragmented write procedure
this.#stream.on('finish', end_stream);

// Return the writable stream
Expand Down

0 comments on commit e4a5c7c

Please sign in to comment.