Skip to content
Open
Show file tree
Hide file tree
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
63 changes: 63 additions & 0 deletions apps/mobile/test/streaming-fetch-patch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from "node:assert/strict";
import { createRequire } from "node:module";
import test from "node:test";

const require = createRequire(import.meta.url);

test("streaming fetch polyfill does not fail a 200 response when readyState jumps from 1 to 4", async () => {
const { installStreamingFetch } =
require("../node_modules/@copilotkit/react-native/dist/streaming-fetch-BTESzLKX.cjs") as {
installStreamingFetch: () => void;
};
const originalFetch = globalThis.fetch;
const originalResponse = globalThis.Response;
const originalXhr = (globalThis as { XMLHttpRequest?: unknown }).XMLHttpRequest;
const originalDev = (globalThis as { __DEV__?: boolean }).__DEV__;

class FakeXHR {
readyState = 0;
status = 0;
statusText = "";
responseText = "";
responseType = "";
onreadystatechange: (() => void) | null = null;
onload: (() => void) | null = null;
onerror: (() => void) | null = null;
ontimeout: (() => void) | null = null;
onprogress: (() => void) | null = null;
open() {}
setRequestHeader() {}
abort() {}
getAllResponseHeaders() {
return "content-type: application/json\r\n";
}
send() {
this.readyState = 4;
this.status = 200;
this.statusText = "OK";
this.responseText = '{"ok":true}';
this.onreadystatechange?.();
setTimeout(() => this.onload?.(), 10);
}
}

try {
(globalThis as { __DEV__?: boolean }).__DEV__ = false;
(globalThis as { Response?: unknown }).Response = class {
body = null;
};
(globalThis as { XMLHttpRequest?: unknown }).XMLHttpRequest = FakeXHR;
(globalThis as { fetch: unknown }).fetch = () => {
throw new TypeError("no Response.body");
};
installStreamingFetch();
const res = await globalThis.fetch("https://example.test/api/workspace");
assert.equal(res.status, 200);
assert.equal(await res.text(), '{"ok":true}');
} finally {
globalThis.fetch = originalFetch;
globalThis.Response = originalResponse;
(globalThis as { XMLHttpRequest?: unknown }).XMLHttpRequest = originalXhr;
(globalThis as { __DEV__?: boolean }).__DEV__ = originalDev;
}
});
39 changes: 39 additions & 0 deletions patches/@copilotkit__react-native@1.70.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
diff --git a/dist/streaming-fetch-BTESzLKX.cjs b/dist/streaming-fetch-BTESzLKX.cjs
index 71a58ca403e3cbda813eb6e242e8e5c0b73cc021..9e06f7b3b8f08240fd392fec454ec1596a05bf82 100644
--- a/dist/streaming-fetch-BTESzLKX.cjs
+++ b/dist/streaming-fetch-BTESzLKX.cjs
@@ -135,7 +135,7 @@ function installStreamingFetch() {
const xhrStatusText = xhr.statusText;
const rawHeaders = xhr.getAllResponseHeaders() || "";
setTimeout(() => {
- if (readyState === 4 && !settled && !resp) {
+ if (readyState === 4 && !settled && !resp && xhrStatus === 0) {
fail(/* @__PURE__ */ new TypeError(`Network request to ${url} completed with status ${xhrStatus} but no response was produced. This may indicate a CORS failure, DNS error, or React Native networking issue.`));
return;
}
diff --git a/dist/streaming-fetch-BnQh3vBz.mjs b/dist/streaming-fetch-BnQh3vBz.mjs
index 51f19f91a590854b00b785cfbbc322ecf66cc18c..cc3b1d054b2ad2d778d0b70ed8e6a48ca46ee090 100644
--- a/dist/streaming-fetch-BnQh3vBz.mjs
+++ b/dist/streaming-fetch-BnQh3vBz.mjs
@@ -134,7 +134,7 @@ function installStreamingFetch() {
const xhrStatusText = xhr.statusText;
const rawHeaders = xhr.getAllResponseHeaders() || "";
setTimeout(() => {
- if (readyState === 4 && !settled && !resp) {
+ if (readyState === 4 && !settled && !resp && xhrStatus === 0) {
fail(/* @__PURE__ */ new TypeError(`Network request to ${url} completed with status ${xhrStatus} but no response was produced. This may indicate a CORS failure, DNS error, or React Native networking issue.`));
return;
}
diff --git a/src/streaming-fetch.ts b/src/streaming-fetch.ts
index df5e0beccc89d14c88a6b4e2bc32abee1332bb51..59f21a979dee810d83d6c5d07bb476acf550ac5a 100644
--- a/src/streaming-fetch.ts
+++ b/src/streaming-fetch.ts
@@ -266,7 +266,7 @@ export function installStreamingFetch(): void {
setTimeout(() => {
// Safety net: if XHR completed but we never resolved/rejected, fail explicitly.
// This can happen when status === 0 and onerror doesn't fire (some RN networking impls).
- if (readyState === 4 && !settled && !resp) {
+ if (readyState === 4 && !settled && !resp && xhrStatus === 0) {
fail(
new TypeError(
`Network request to ${url} completed with status ${xhrStatus} but no response was produced. ` +
5 changes: 3 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ overrides:
'image-size': '2.0.4'

patchedDependencies:
'@copilotkit/react-native@1.70.1': patches/@copilotkit__react-native@1.70.1.patch
metro@0.83.3: patches/metro@0.83.3.patch