336 lines
9.8 KiB
TypeScript
336 lines
9.8 KiB
TypeScript
import { ReadableStream as Firefox52ReadableStream } from "web-streams-polyfill/es5";
|
|
|
|
type StreamingRequestInfo = RequestInfo | URL;
|
|
|
|
const STREAM_PATH_PATTERN = /(?:\/stream(?:[/?#]|$)|\/events(?:[/?#]|$))/i;
|
|
const LEGACY_STREAM_PATHS = [
|
|
"/api/multi-agent/init",
|
|
"/api/writing/rewrite",
|
|
];
|
|
|
|
function requestUrl(input: StreamingRequestInfo): string {
|
|
if (typeof input === "string") return input;
|
|
if (typeof URL !== "undefined" && input instanceof URL) return input.href;
|
|
return (input as Request).url ?? "";
|
|
}
|
|
|
|
function requestHeaders(input: StreamingRequestInfo, init?: RequestInit): Headers {
|
|
const headers = new Headers(
|
|
typeof Request !== "undefined" && input instanceof Request
|
|
? input.headers
|
|
: undefined,
|
|
);
|
|
if (init?.headers) {
|
|
new Headers(init.headers).forEach((value, name) => headers.set(name, value));
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
function bodyRequestsStreaming(body: BodyInit | null | undefined): boolean {
|
|
if (typeof body !== "string") return false;
|
|
// The older RAG endpoints expose streaming behind a JSON flag instead of a
|
|
// dedicated URL. Avoid parsing arbitrary large request bodies unless the
|
|
// marker is present.
|
|
if (!/["']stream["']\s*:/.test(body)) return false;
|
|
try {
|
|
const parsed = JSON.parse(body) as { stream?: unknown };
|
|
return parsed.stream === true;
|
|
} catch {
|
|
return /["']stream["']\s*:\s*true\b/i.test(body);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Identify requests whose callers consume `Response.body`. Firefox 52 ships
|
|
* fetch but exposes no readable response stream, so these requests must use
|
|
* the incremental XHR bridge before they are sent.
|
|
*/
|
|
export function isFirefox52StreamingRequest(
|
|
input: StreamingRequestInfo,
|
|
init?: RequestInit,
|
|
): boolean {
|
|
const url = requestUrl(input);
|
|
const headers = requestHeaders(input, init);
|
|
const accept = headers.get("Accept")?.toLowerCase() ?? "";
|
|
if (
|
|
accept.includes("text/event-stream") ||
|
|
accept.includes("application/x-ndjson")
|
|
) {
|
|
return true;
|
|
}
|
|
if (headers.has("Range")) return true;
|
|
if (STREAM_PATH_PATTERN.test(url)) return true;
|
|
if (LEGACY_STREAM_PATHS.some((path) => url.includes(path))) return true;
|
|
return bodyRequestsStreaming(init?.body);
|
|
}
|
|
|
|
export function parseXhrResponseHeaders(rawHeaders: string): Headers {
|
|
const headers = new Headers();
|
|
let lastName = "";
|
|
for (const line of rawHeaders.replace(/\r\n/g, "\n").split("\n")) {
|
|
if (!line) continue;
|
|
if (/^[\t ]/.test(line) && lastName) {
|
|
headers.set(lastName, `${headers.get(lastName) ?? ""} ${line.trim()}`);
|
|
continue;
|
|
}
|
|
const separator = line.indexOf(":");
|
|
if (separator <= 0) continue;
|
|
lastName = line.slice(0, separator).trim();
|
|
headers.append(lastName, line.slice(separator + 1).trim());
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
function abortError(): Error {
|
|
try {
|
|
return new DOMException("The operation was aborted.", "AbortError");
|
|
} catch {
|
|
const error = new Error("The operation was aborted.");
|
|
error.name = "AbortError";
|
|
return error;
|
|
}
|
|
}
|
|
|
|
function networkError(): TypeError {
|
|
return new TypeError("Network request failed");
|
|
}
|
|
|
|
export function hasUsableNativeResponseStream(
|
|
responsePrototype: object | null | undefined,
|
|
readableStreamPrototype: object | null | undefined,
|
|
): boolean {
|
|
return (
|
|
responsePrototype != null &&
|
|
"body" in responsePrototype &&
|
|
readableStreamPrototype != null &&
|
|
typeof Reflect.get(readableStreamPrototype, "pipeThrough") === "function"
|
|
);
|
|
}
|
|
|
|
function concatBytes(chunks: Uint8Array[]): Uint8Array {
|
|
let length = 0;
|
|
for (const chunk of chunks) length += chunk.byteLength;
|
|
const result = new Uint8Array(length);
|
|
let offset = 0;
|
|
for (const chunk of chunks) {
|
|
result.set(chunk, offset);
|
|
offset += chunk.byteLength;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function createStreamingResponse(
|
|
xhr: XMLHttpRequest,
|
|
body: ReadableStream<Uint8Array>,
|
|
): Response {
|
|
let used = false;
|
|
|
|
const consume = async (): Promise<Uint8Array> => {
|
|
if (used) throw new TypeError("Body has already been consumed.");
|
|
used = true;
|
|
const reader = body.getReader();
|
|
const chunks: Uint8Array[] = [];
|
|
while (true) {
|
|
const result = await reader.read();
|
|
if (result.done) return concatBytes(chunks);
|
|
if (result.value) chunks.push(result.value);
|
|
}
|
|
};
|
|
|
|
const response = {
|
|
get body() {
|
|
used = true;
|
|
return body;
|
|
},
|
|
get bodyUsed() {
|
|
return used;
|
|
},
|
|
headers: parseXhrResponseHeaders(xhr.getAllResponseHeaders()),
|
|
ok: xhr.status >= 200 && xhr.status < 300,
|
|
redirected: false,
|
|
status: xhr.status,
|
|
statusText: xhr.statusText,
|
|
type: "basic" as ResponseType,
|
|
url: xhr.responseURL || "",
|
|
async arrayBuffer() {
|
|
const bytes = await consume();
|
|
return bytes.buffer.slice(
|
|
bytes.byteOffset,
|
|
bytes.byteOffset + bytes.byteLength,
|
|
) as ArrayBuffer;
|
|
},
|
|
async blob() {
|
|
const bytes = await consume();
|
|
const data = bytes.buffer.slice(
|
|
bytes.byteOffset,
|
|
bytes.byteOffset + bytes.byteLength,
|
|
) as ArrayBuffer;
|
|
return new Blob([data], {
|
|
type: response.headers.get("Content-Type") ?? "",
|
|
});
|
|
},
|
|
async json() {
|
|
return JSON.parse(await response.text()) as unknown;
|
|
},
|
|
async text() {
|
|
return new TextDecoder().decode(await consume());
|
|
},
|
|
clone() {
|
|
throw new TypeError("A Firefox 52 streaming response cannot be cloned.");
|
|
},
|
|
async formData() {
|
|
throw new TypeError("Streaming form data is not supported in Firefox 52.");
|
|
},
|
|
bytes: consume,
|
|
};
|
|
|
|
return response as Response;
|
|
}
|
|
|
|
function requestMethod(input: StreamingRequestInfo, init?: RequestInit): string {
|
|
return (
|
|
init?.method ??
|
|
(typeof Request !== "undefined" && input instanceof Request
|
|
? input.method
|
|
: "GET")
|
|
).toUpperCase();
|
|
}
|
|
|
|
function xhrStreamingFetch(
|
|
input: StreamingRequestInfo,
|
|
init?: RequestInit,
|
|
): Promise<Response> {
|
|
return new Promise((resolve, reject) => {
|
|
const signal = init?.signal;
|
|
if (signal?.aborted) {
|
|
reject(abortError());
|
|
return;
|
|
}
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
const headers = requestHeaders(input, init);
|
|
const encoder = new TextEncoder();
|
|
let controller: ReadableStreamDefaultController<Uint8Array> | null = null;
|
|
let responseResolved = false;
|
|
let terminal = false;
|
|
let readIndex = 0;
|
|
let trailingHighSurrogate = "";
|
|
|
|
const body = new Firefox52ReadableStream<Uint8Array>({
|
|
start(nextController) {
|
|
controller = nextController as unknown as ReadableStreamDefaultController<Uint8Array>;
|
|
},
|
|
cancel() {
|
|
terminal = true;
|
|
xhr.abort();
|
|
},
|
|
}) as unknown as ReadableStream<Uint8Array>;
|
|
|
|
const settleResponse = () => {
|
|
if (responseResolved || xhr.readyState < XMLHttpRequest.HEADERS_RECEIVED) {
|
|
return;
|
|
}
|
|
responseResolved = true;
|
|
resolve(createStreamingResponse(xhr, body));
|
|
};
|
|
|
|
const pushAvailableText = (flush: boolean) => {
|
|
if (terminal || !controller) return;
|
|
let current = "";
|
|
try {
|
|
current = xhr.responseText || "";
|
|
} catch {
|
|
return;
|
|
}
|
|
let chunk = trailingHighSurrogate + current.slice(readIndex);
|
|
readIndex = current.length;
|
|
trailingHighSurrogate = "";
|
|
if (!flush && chunk.length > 0) {
|
|
const last = chunk.charCodeAt(chunk.length - 1);
|
|
if (last >= 0xd800 && last <= 0xdbff) {
|
|
trailingHighSurrogate = chunk.slice(-1);
|
|
chunk = chunk.slice(0, -1);
|
|
}
|
|
}
|
|
if (chunk) controller.enqueue(encoder.encode(chunk));
|
|
};
|
|
|
|
const fail = (error: Error) => {
|
|
if (terminal) return;
|
|
terminal = true;
|
|
if (responseResolved) controller?.error(error);
|
|
else reject(error);
|
|
};
|
|
|
|
xhr.onreadystatechange = settleResponse;
|
|
xhr.onprogress = () => {
|
|
settleResponse();
|
|
pushAvailableText(false);
|
|
};
|
|
xhr.onload = () => {
|
|
settleResponse();
|
|
if (terminal) return;
|
|
pushAvailableText(true);
|
|
terminal = true;
|
|
controller?.close();
|
|
};
|
|
xhr.onerror = () => fail(networkError());
|
|
xhr.ontimeout = () => fail(networkError());
|
|
xhr.onabort = () => {
|
|
if (!terminal) fail(abortError());
|
|
};
|
|
|
|
const onAbort = () => {
|
|
if (terminal) return;
|
|
xhr.abort();
|
|
fail(abortError());
|
|
};
|
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
|
|
try {
|
|
xhr.open(requestMethod(input, init), requestUrl(input), true);
|
|
xhr.responseType = "text";
|
|
xhr.withCredentials = init?.credentials === "include";
|
|
headers.forEach((value, name) => xhr.setRequestHeader(name, value));
|
|
const requestBody = init?.body;
|
|
if (
|
|
requestBody != null &&
|
|
typeof ReadableStream !== "undefined" &&
|
|
requestBody instanceof ReadableStream
|
|
) {
|
|
throw new TypeError("Streaming request bodies are not supported in Firefox 52.");
|
|
}
|
|
xhr.send((requestBody ?? null) as XMLHttpRequestBodyInit | null);
|
|
} catch (error) {
|
|
fail(error instanceof Error ? error : new Error(String(error)));
|
|
}
|
|
});
|
|
}
|
|
|
|
let installed = false;
|
|
|
|
/** Install the XHR response-stream bridge while retaining native fetch for ordinary calls. */
|
|
export function installFirefox52StreamingFetch(): boolean {
|
|
if (installed || typeof window === "undefined" || typeof window.fetch !== "function") {
|
|
return false;
|
|
}
|
|
if (
|
|
hasUsableNativeResponseStream(
|
|
typeof Response === "undefined" ? undefined : Response.prototype,
|
|
typeof ReadableStream === "undefined" ? undefined : ReadableStream.prototype,
|
|
)
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
const nativeFetch = window.fetch.bind(window);
|
|
window.fetch = ((input: StreamingRequestInfo, init?: RequestInit) => {
|
|
if (isFirefox52StreamingRequest(input, init)) {
|
|
return xhrStreamingFetch(input, init);
|
|
}
|
|
return nativeFetch(input, init);
|
|
}) as typeof window.fetch;
|
|
installed = true;
|
|
return true;
|
|
}
|