diff --git a/libs/sdk-js/src/utils/sse.ts b/libs/sdk-js/src/utils/sse.ts index 2df40a1df..c3c525113 100644 --- a/libs/sdk-js/src/utils/sse.ts +++ b/libs/sdk-js/src/utils/sse.ts @@ -1,10 +1,3 @@ -const mergeArrays = (a: ArrayLike, b: ArrayLike) => { - const mergedArray = new Uint8Array(a.length + b.length); - mergedArray.set(a); - mergedArray.set(b, a.length); - return mergedArray; -}; - const CR = "\r".charCodeAt(0); const LF = "\n".charCodeAt(0); const NULL = "\0".charCodeAt(0); @@ -30,7 +23,7 @@ export class BytesLineDecoder extends TransformStream { // Handle trailing CR from previous chunk if (trailingCr) { - text = mergeArrays([CR], text); + text = joinArrays([[CR], text]); trailingCr = false; } @@ -74,9 +67,7 @@ export class BytesLineDecoder extends TransformStream { if (buffer.length) { // Include existing buffer in first line buffer.push(lines[0]); - lines[0] = joinArrays(buffer); - buffer = []; } @@ -169,24 +160,17 @@ export class SSEDecoder extends TransformStream { } } -function decodeArraysToJson(decoder: TextDecoder, arrays: Uint8Array[]) { - const totalLength = arrays.reduce((acc, curr) => acc + curr.length, 0); +function joinArrays(data: ArrayLike[]) { + const totalLength = data.reduce((acc, curr) => acc + curr.length, 0); let merged = new Uint8Array(totalLength); let offset = 0; - for (const c of arrays) { - merged.set(c, offset); - offset += c.length; - } - return JSON.parse(decoder.decode(merged)); -} - -function joinArrays(arrays: Uint8Array[]) { - const totalLength = arrays.reduce((acc, curr) => acc + curr.length, 0); - let merged = new Uint8Array(totalLength); - let offset = 0; - for (const c of arrays) { + for (const c of data) { merged.set(c, offset); offset += c.length; } return merged; } + +function decodeArraysToJson(decoder: TextDecoder, data: ArrayLike[]) { + return JSON.parse(decoder.decode(joinArrays(data))); +}