Skip to content

Commit

Permalink
Use ETX control code rather than unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
bencmbrook committed Oct 22, 2023
1 parent db54619 commit b6ae566
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mux-web-streams",
"version": "1.1.0",
"version": "1.1.1",
"description": "Multiplex and demultiplex web streams.",
"author": "Ben Brook",
"license": "MIT",
Expand Down
18 changes: 5 additions & 13 deletions src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,9 @@ function serializeChunk({
id,
end,
value,
}: Omit<Header, 'dataIsRaw'> & { value: SerializableData }): Uint8Array | null {
}: Omit<Header, 'dataIsRaw'> & { value: SerializableData }): Uint8Array {
const { data, isRaw } = serializeData({ value });

// No data in this chunk; skip writing anything at all
if (data.length === 0) {
return null;
}

// Create the header
const header = headerToArray({
id,
Expand Down Expand Up @@ -179,11 +174,8 @@ export const muxer = (
value: result.value,
});

// If the byteChunk is not empty (sometimes streams have empty chunks)
if (byteChunk !== null) {
// Write it to the muxed output
controller.enqueue(byteChunk);
}
// Write it to the muxed output
controller.enqueue(byteChunk);
} else {
// This incoming stream is finished
// Mark this incoming stream as done, so we no longer attempt to read from it.
Expand All @@ -196,9 +188,9 @@ export const muxer = (
const byteChunk = serializeChunk({
id: currentReader.id,
end: true,
value: '\u0004', // arbitrarily chosen; value just needs to have length > 0 to distinguish cases
value: 0x03, // "end of text" control code
});
controller.enqueue(byteChunk!);
controller.enqueue(byteChunk);
}
})();
},
Expand Down
4 changes: 2 additions & 2 deletions test/mux-web-streams.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ const inputData: SerializableData[][] = [
[{ a: 1 }, { b: 2 }, { c: 3 }],
[1, 2],
['A'],
[[1, 2], 3, 'a', new Uint8Array([12, 1, 100, 255, 0])],
[[1, 2], 3, 'a', new Uint8Array([12, 1, 3, 100, 255, 0])],
[true, false],
[[1, 2], 3, null, 'a'],
[[1, 2], 3, 'a'],
[[1, 2], 3, false],
[[1, 2], 3, 0],
[[1, 2], 3, []],
[new Uint8Array([12, 1, 100, 255, 0])],
[new Uint8Array([12, 1, 3, 100, 255, 0])],
[null, null, null],
[[1, 2], 3, 'a', {}],
[[1, 2], 3, null, 'a'],
Expand Down

0 comments on commit b6ae566

Please sign in to comment.