Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: optimize stream sink for small messages #216

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,21 @@ export function createStream (options: Options): MplexStream {
const uint8ArrayList = new Uint8ArrayList()

for await (const data of source) {
uint8ArrayList.append(data)

while (uint8ArrayList.length !== 0) {
if (uint8ArrayList.length <= maxMsgSize) {
send({ id, type: Types.MESSAGE, data: uint8ArrayList.sublist() })
uint8ArrayList.consume(uint8ArrayList.length)
break
if (data.length <= maxMsgSize) {
send({ id, type: Types.MESSAGE, data: data instanceof Uint8ArrayList ? data : new Uint8ArrayList(data) })
} else {
uint8ArrayList.append(data)

while (uint8ArrayList.length !== 0) {
// eslint-disable-next-line max-depth
if (uint8ArrayList.length <= maxMsgSize) {
send({ id, type: Types.MESSAGE, data: uint8ArrayList.sublist() })
uint8ArrayList.consume(uint8ArrayList.length)
break
}
send({ id, type: Types.MESSAGE, data: uint8ArrayList.sublist(0, maxMsgSize) })
uint8ArrayList.consume(maxMsgSize)
}

send({ id, type: Types.MESSAGE, data: uint8ArrayList.sublist(0, maxMsgSize) })
uint8ArrayList.consume(maxMsgSize)
}
}
} catch (err: any) {
Expand Down