-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure connectivity to private HF spaces with SSE protocol (#8181)
* add msw setup and initialisation tests * add changeset * add eventsource polyfill for node and browser envs * add changeset * add changeset * config tweak * types * update eventsource usage * add changeset * add walk_and_store_blobs improvements and add tests * add changeset * api_info tests * add direct space URL link tests * fix tests * add view_api tests * add post_message test * tweak * add spaces tests * jwt and protocol tests * add post_data tests * test tweaks * dynamically import eventsource * revet eventsource imports * add jwt param to sse requests * add stream test * add changeset * add changeset --------- Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
- Loading branch information
1 parent
1d7fc6f
commit c5277a3
Showing
8 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@gradio/app": patch | ||
"@gradio/client": patch | ||
"gradio": patch | ||
--- | ||
|
||
fix:Ensure connectivity to private HF spaces with SSE protocol |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { vi } from "vitest"; | ||
|
||
Object.defineProperty(window, "EventSource", { | ||
writable: true, | ||
value: vi.fn().mockImplementation(() => ({ | ||
close: vi.fn(() => {}), | ||
addEventListener: vi.fn(), | ||
onmessage: vi.fn((_event: MessageEvent) => {}), | ||
onerror: vi.fn((_event: Event) => {}) | ||
})) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { vi } from "vitest"; | ||
import { Client } from "../client"; | ||
import { initialise_server } from "./server"; | ||
|
||
import { describe, it, expect, afterEach } from "vitest"; | ||
import "./mock_eventsource.ts"; | ||
|
||
const server = initialise_server(); | ||
|
||
beforeAll(() => server.listen()); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
describe("open_stream", () => { | ||
let mock_eventsource: any; | ||
let app: any; | ||
|
||
beforeEach(async () => { | ||
app = await Client.connect("hmb/hello_world"); | ||
app.eventSource_factory = vi.fn().mockImplementation(() => { | ||
mock_eventsource = new EventSource(""); | ||
return mock_eventsource; | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it("should throw an error if config is not defined", () => { | ||
app.config = undefined; | ||
|
||
expect(() => { | ||
app.open_stream(); | ||
}).toThrow("Could not resolve app config"); | ||
}); | ||
|
||
it("should connect to the SSE endpoint and handle messages", async () => { | ||
app.open_stream(); | ||
|
||
const eventsource_mock_call = app.eventSource_factory.mock.calls[0][0]; | ||
|
||
expect(eventsource_mock_call.href).toMatch( | ||
/https:\/\/hmb-hello-world\.hf\.space\/queue\/data\?session_hash/ | ||
); | ||
|
||
expect(app.eventSource_factory).toHaveBeenCalledWith(eventsource_mock_call); | ||
|
||
const onMessageCallback = mock_eventsource.onmessage; | ||
const onErrorCallback = mock_eventsource.onerror; | ||
|
||
const message = { msg: "hello jerry" }; | ||
|
||
onMessageCallback({ data: JSON.stringify(message) }); | ||
expect(app.stream_status.open).toBe(true); | ||
|
||
expect(app.event_callbacks).toEqual({}); | ||
expect(app.pending_stream_messages).toEqual({}); | ||
|
||
const close_stream_message = { msg: "close_stream" }; | ||
onMessageCallback({ data: JSON.stringify(close_stream_message) }); | ||
expect(app.stream_status.open).toBe(false); | ||
|
||
onErrorCallback({ data: JSON.stringify("404") }); | ||
expect(app.stream_status.open).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.