Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventsource polyfill for Node.js and browser environments #8118

Merged
merged 53 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
56e50b5
add msw setup and initialisation tests
hannahblair Apr 23, 2024
ad7e4e6
add changeset
gradio-pr-bot Apr 23, 2024
fb4bce0
Merge branch 'main' into js-client-tests
hannahblair Apr 23, 2024
705f9e1
add eventsource polyfill for node and browser envs
hannahblair Apr 24, 2024
9da7cfe
add changeset
gradio-pr-bot Apr 24, 2024
0f0320d
Merge branch 'main' into client-eventsource
hannahblair Apr 24, 2024
8d2ce5b
add changeset
gradio-pr-bot Apr 24, 2024
57d48c3
config tweak
hannahblair Apr 24, 2024
5ee640d
Merge branch 'client-eventsource' of https://github.com/gradio-app/gr…
hannahblair Apr 24, 2024
69e32a3
types
hannahblair Apr 24, 2024
7cd5a53
Merge branch 'main' into js-client-tests
hannahblair Apr 24, 2024
706d1dd
update eventsource usage
hannahblair Apr 24, 2024
2a2a151
add changeset
gradio-pr-bot Apr 24, 2024
d588e8e
add walk_and_store_blobs improvements and add tests
hannahblair Apr 25, 2024
f948e7a
add changeset
gradio-pr-bot Apr 25, 2024
a70b768
api_info tests
hannahblair Apr 25, 2024
2fafb73
Merge branch 'js-client-tests' of https://github.com/gradio-app/gradi…
hannahblair Apr 25, 2024
f0deb1b
add direct space URL link tests
hannahblair Apr 25, 2024
37248c7
Merge branch 'main' into js-client-tests
hannahblair Apr 25, 2024
f3fb49a
fix tests
hannahblair Apr 26, 2024
43f6dce
Merge branch 'js-client-tests' of https://github.com/gradio-app/gradi…
hannahblair Apr 26, 2024
c48766a
add view_api tests
hannahblair Apr 26, 2024
da34593
Merge branch 'main' into js-client-tests
hannahblair Apr 26, 2024
6943549
add post_message test
hannahblair Apr 26, 2024
17ee3fd
Merge branch 'js-client-tests' of https://github.com/gradio-app/gradi…
hannahblair Apr 26, 2024
1f91a72
tweak
hannahblair Apr 26, 2024
7a041ef
Merge branch 'main' into js-client-tests
hannahblair Apr 26, 2024
0463ba5
add spaces tests
hannahblair Apr 26, 2024
9dc5c0b
Merge branch 'js-client-tests' of https://github.com/gradio-app/gradi…
hannahblair Apr 26, 2024
732f788
jwt and protocol tests
hannahblair Apr 26, 2024
643e5bf
add post_data tests
hannahblair Apr 26, 2024
cd12651
test tweaks
hannahblair Apr 29, 2024
f939578
Merge branch 'main' into js-client-tests
hannahblair Apr 29, 2024
b465986
Merge branch 'main' into client-eventsource
hannahblair Apr 29, 2024
443898c
Merge branch 'js-client-tests' into client-eventsource
hannahblair Apr 29, 2024
0338ed1
dynamically import eventsource
hannahblair Apr 29, 2024
e2ca25a
revet eventsource imports
hannahblair Apr 29, 2024
480c847
Merge branch 'main' into client-eventsource
hannahblair Apr 30, 2024
b9a6834
add node test
hannahblair May 1, 2024
dc77a19
Merge branch 'main' into client-eventsource
hannahblair May 1, 2024
a78873a
lockfile
hannahblair May 1, 2024
02cfec5
add client test in root pkg file
hannahblair May 1, 2024
6c44d35
Merge branch 'main' into client-eventsource
hannahblair May 1, 2024
0ddf543
lcokfile
hannahblair May 1, 2024
d8d1c88
remove eventsource from js/app
hannahblair May 2, 2024
14a4273
Merge branch 'main' into client-eventsource
hannahblair May 2, 2024
3d7f8f0
add changeset
gradio-pr-bot May 2, 2024
b145987
remove ts ignore
hannahblair May 2, 2024
8d36bd8
move eventsource polyfill to eventsource factory
hannahblair May 2, 2024
1668324
add changeset
gradio-pr-bot May 2, 2024
1466abb
Merge branch 'main' into client-eventsource
hannahblair May 2, 2024
2cc42ac
tweak
hannahblair May 2, 2024
d3c2e57
Merge branch 'client-eventsource' of https://github.com/gradio-app/gr…
hannahblair May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/red-colts-burn.md
@@ -0,0 +1,6 @@
---
"@gradio/client": patch
"gradio": patch
---

fix:Add eventsource polyfill for Node.js and browser environments
7 changes: 6 additions & 1 deletion client/js/package.json
Expand Up @@ -13,7 +13,9 @@
"./package.json": "./package.json"
},
"dependencies": {
"@types/eventsource": "^1.1.15",
"bufferutil": "^4.0.7",
"eventsource": "^2.0.2",
"msw": "^2.2.1",
"semiver": "^1.1.0",
"typescript": "^5.0.0",
Expand All @@ -26,7 +28,10 @@
"scripts": {
"bundle": "vite build --ssr",
"generate_types": "tsc",
"build": "pnpm bundle && pnpm generate_types"
"build": "pnpm bundle && pnpm generate_types",
"test": "pnpm test:client && pnpm test:client:node",
"test:client": "vitest run -c vite.config.js",
"test:client:node": "TEST_MODE=node vitest run -c vite.config.js"
},
"engines": {
"node": ">=18.0.0"
Expand Down
12 changes: 12 additions & 0 deletions client/js/src/client.ts
Expand Up @@ -36,6 +36,16 @@ export class NodeBlob extends Blob {
}
}

if (typeof window === "undefined") {
import("eventsource")
.then((EventSourceModule) => {
global.EventSource = EventSourceModule.default as any;
})
.catch((error) =>
console.error("Failed to load EventSource module:", error)
);
}

hannahblair marked this conversation as resolved.
Show resolved Hide resolved
export class Client {
app_reference: string;
options: ClientOptions;
Expand Down Expand Up @@ -65,6 +75,8 @@ export class Client {
eventSource_factory(url: URL): EventSource {
if (typeof window !== undefined && typeof EventSource !== "undefined") {
return new EventSource(url.toString());
} else if (typeof global !== "undefined" && global.EventSource) {
return new global.EventSource(url.toString());
}
// @ts-ignore
hannahblair marked this conversation as resolved.
Show resolved Hide resolved
return null; // todo: polyfill eventsource for node envs
Expand Down
1 change: 1 addition & 0 deletions client/js/src/test/api_info.test.ts
@@ -1,4 +1,5 @@
import { QUEUE_FULL_MSG, SPACE_METADATA_ERROR_MSG } from "../constants";
import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";
import {
handle_message,
get_description,
Expand Down
2 changes: 1 addition & 1 deletion client/js/src/test/data.test.ts
@@ -1,4 +1,4 @@
import { describe, it, expect, vi } from "vitest";
import { describe, it, expect, vi, afterEach } from "vitest";
import {
update_object,
walk_and_store_blobs,
Expand Down
9 changes: 9 additions & 0 deletions client/js/src/test/handlers.ts
Expand Up @@ -425,5 +425,14 @@ export const handlers: RequestHandler[] = [
"Content-Type": "application/json"
}
});
}),
// heartbeat requests
http.get(`*/heartbeat/*`, () => {
return new HttpResponse(null, {
status: 200,
headers: {
"Content-Type": "application/json"
}
});
})
];
1 change: 1 addition & 0 deletions client/js/src/test/init_helpers.test.ts
Expand Up @@ -4,6 +4,7 @@ import {
determine_protocol
} from "../helpers/init_helpers";
import { initialise_server } from "./server";
import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";

const server = initialise_server();

Expand Down
1 change: 1 addition & 0 deletions client/js/src/test/post_data.test.ts
Expand Up @@ -3,6 +3,7 @@ import { Client } from "../client";
import { initialise_server } from "./server";
import { BROKEN_CONNECTION_MSG } from "../constants";
const server = initialise_server();
import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
Expand Down
1 change: 1 addition & 0 deletions client/js/src/test/spaces.test.ts
Expand Up @@ -5,6 +5,7 @@ import {
set_space_timeout,
check_space_status
} from "../helpers/spaces";
import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";

import { initialise_server } from "./server";
import { hardware_sleeptime_response } from "./test_data";
Expand Down
2 changes: 1 addition & 1 deletion client/js/src/test/upload_files.test.ts
@@ -1,4 +1,4 @@
import { describe, it, expect, afterEach } from "vitest";
import { describe, it, expect, afterEach, beforeAll, afterAll } from "vitest";

import { Client } from "..";
import { initialise_server } from "./server";
Expand Down
11 changes: 8 additions & 3 deletions client/js/src/utils/stream.ts
Expand Up @@ -28,7 +28,7 @@ export function open_stream(this: Client): void {
throw new Error("Cannot connect to sse endpoint: " + url.toString());
}

event_source.onmessage = async function (event) {
event_source.onmessage = async function (event: MessageEvent) {
let _data = JSON.parse(event.data);
if (_data.msg === "close_stream") {
close_stream(stream_status, event_source);
Expand All @@ -53,8 +53,13 @@ export function open_stream(this: Client): void {
close_stream(stream_status, event_source);
}
}
let fn = event_callbacks[event_id];
window.setTimeout(fn, 0, _data); // need to do this to put the event on the end of the event loop, so the browser can refresh between callbacks and not freeze in case of quick generations. See https://github.com/gradio-app/gradio/pull/7055
let fn: (data: any) => void = event_callbacks[event_id];

if (typeof window !== "undefined") {
window.setTimeout(fn, 0, _data); // need to do this to put the event on the end of the event loop, so the browser can refresh between callbacks and not freeze in case of quick generations. See https://github.com/gradio-app/gradio/pull/7055
} else {
setImmediate(fn, _data);
}
} else {
if (!pending_stream_messages[event_id]) {
pending_stream_messages[event_id] = [];
Expand Down
8 changes: 6 additions & 2 deletions client/js/src/utils/submit.ts
Expand Up @@ -376,7 +376,7 @@ export function submit(
);
}

event_source.onmessage = async function (event) {
event_source.onmessage = async function (event: MessageEvent) {
const _data = JSON.parse(event.data);
const { type, status, data } = handle_message(
_data,
Expand Down Expand Up @@ -476,7 +476,11 @@ export function submit(
fn_index,
time: new Date()
});
let hostname = window.location.hostname;
let hostname = "";
if (typeof window !== "undefined") {
hostname = window?.location?.hostname;
}

let hfhubdev = "dev.spaces.huggingface.tech";
const origin = hostname.includes(".dev.")
? `https://moon-${hostname.split(".")[1]}.${hfhubdev}`
Expand Down
7 changes: 7 additions & 0 deletions client/js/vite.config.js
@@ -1,6 +1,8 @@
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";

const TEST_MODE = process.env.TEST_MODE || "happy-dom";

export default defineConfig({
build: {
lib: {
Expand All @@ -17,6 +19,11 @@ export default defineConfig({
},
plugins: [svelte()],

mode: process.env.MODE || "development",
test: {
include: ["./src/test/*.test.*"],
environment: TEST_MODE
},
ssr: {
target: "node",
format: "esm",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
"ts:check": "svelte-check --tsconfig tsconfig.json --threshold error",
"test": "pnpm --filter @gradio/client build && vitest dev --config .config/vitest.config.ts",
"test:run": "pnpm --filter @gradio/client build && vitest run --config .config/vitest.config.ts --reporter=verbose",
"test:node": "TEST_MODE=node pnpm vitest run --config .config/vitest.config.ts",
"test:client": "pnpm --filter=@gradio/client test",
"test:browser": "pnpm --filter @gradio/app test:browser",
"test:browser:reload": "CUSTOM_TEST=1 pnpm --filter @gradio/app test:browser:reload",
"test:browser:full": "run-s build test:browser",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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