@sap-ai-sdk/core
Version:
**This package is designed for internal usage and should not be consumed directly.**
207 lines • 6.89 kB
JavaScript
import { ErrorWithCause } from '@sap-cloud-sdk/util';
import { LineDecoder } from './line-decoder.js';
import { SSEDecoder } from './sse-decoder.js';
/**
* Stream implemented as an async iterable.
*/
export class SseStream {
iterator;
static transformToSseStream(response, controller) {
let consumed = false;
async function* iterator() {
if (consumed) {
throw new Error('Cannot iterate over a consumed stream.');
}
consumed = true;
let done = false;
try {
for await (const sse of _iterSseMessages(response, controller)) {
if (done) {
continue;
}
if (sse.data.startsWith('[DONE]')) {
done = true;
continue;
}
let data;
try {
data = JSON.parse(sse.data);
}
catch (e) {
throw new ErrorWithCause(`Server sent an unexpected non-JSON response: ${sse.data.length > 256 ? sse.data.slice(0, 256) + '...' : sse.data}`, e);
}
if (data?.error) {
throw new Error(`Error received from the server.\n${JSON.stringify(data.error)}`);
}
// Yield also the event if it exists, otherwise just the data
yield sse.event === null ? data : { event: sse.event, data };
}
done = true;
}
catch (e) {
// Ignore the error if controller was aborted
if (e instanceof Error && e.name === 'CanceledError') {
return;
}
throw new ErrorWithCause('Error while iterating over SSE stream.', e);
}
finally {
// Make sure that the controller is aborted if the stream was not fully consumed
if (!done) {
controller.abort();
}
}
}
return new SseStream(iterator, controller);
}
controller;
constructor(iterator, controller) {
this.iterator = iterator;
this.controller = controller;
}
[Symbol.asyncIterator]() {
return this.iterator();
}
/**
* Converts this stream to a newline-delimited JSON ReadableStream.
* @returns A ReadableStream of UTF-8 encoded NDJSON bytes.
*/
toReadableStream() {
let iter;
const encoder = new TextEncoder();
const { controller: abortController } = this;
const getIterator = () => this[Symbol.asyncIterator]();
async function cleanup() {
try {
await iter.return?.();
}
catch {
// Best-effort: ignore errors during iterator teardown.
}
finally {
abortController.abort();
}
}
return new ReadableStream({
async start() {
iter = getIterator();
},
async pull(controller) {
try {
const { value, done } = await iter.next();
if (done) {
controller.close();
return;
}
controller.enqueue(encoder.encode(JSON.stringify(value) + '\n'));
}
catch (err) {
await cleanup();
controller.error(err);
}
},
async cancel() {
await cleanup();
}
});
}
}
/**
* @internal
*/
export async function* _iterSseMessages(response, controller) {
if (!response.data) {
controller.abort();
throw new Error('Attempted to iterate over a response with no body');
}
const sseDecoder = new SSEDecoder();
const lineDecoder = new LineDecoder();
const iter = response.data;
for await (const sseChunk of iterSseChunks(iter)) {
for (const line of lineDecoder.decode(sseChunk)) {
const sse = sseDecoder.decode(line);
if (sse) {
yield sse;
}
}
}
for (const line of lineDecoder.flush()) {
const sse = sseDecoder.decode(line);
if (sse) {
yield sse;
}
}
}
/**
* Given an async iterable iterator, iterates over it and yields full
* SSE chunks, i.e. yields when a double new-line is encountered.
* @param iterator - Async iterable iterator.
* @returns Async generator of Uint8Array.
* @internal
*/
async function* iterSseChunks(iterator) {
let data = new Uint8Array();
for await (const chunk of iterator) {
if (chunk == null) {
continue;
}
const binaryChunk = chunk instanceof ArrayBuffer
? new Uint8Array(chunk)
: typeof chunk === 'string'
? new TextEncoder().encode(chunk)
: chunk;
const newData = new Uint8Array(data.length + binaryChunk.length);
newData.set(data);
newData.set(binaryChunk, data.length);
data = newData;
let patternIndex;
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
}
}
if (data.length) {
yield data;
}
}
function findDoubleNewlineIndex(buffer) {
// This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
// and returns the index right after the first occurrence of any pattern,
// or -1 if none of the patterns are found.
const newline = 0x0a; // \n
const carriage = 0x0d; // \r
for (let i = 0; i < buffer.length - 2; i++) {
if (buffer[i] === newline && buffer[i + 1] === newline) {
// \n\n
return i + 2;
}
if (buffer[i] === carriage && buffer[i + 1] === carriage) {
// \r\r
return i + 2;
}
if (buffer[i] === carriage &&
buffer[i + 1] === newline &&
i + 3 < buffer.length &&
buffer[i + 2] === carriage &&
buffer[i + 3] === newline) {
// \r\n\r\n
return i + 4;
}
}
return -1;
}
/**
* This is an internal helper function that's just used for testing.
* @param chunks - The chunks to decode.
* @returns The decoded lines.
* @internal
*/
export function _decodeChunks(chunks) {
const decoder = new LineDecoder();
const lines = [];
for (const chunk of chunks) {
lines.push(...decoder.decode(chunk));
}
return lines;
}
//# sourceMappingURL=sse-stream.js.map