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

Fix frontend queuing to target secure WSS #1996

Merged
merged 3 commits into from
Aug 11, 2022
Merged
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
1 change: 1 addition & 0 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ def get_config_file(self):
"theme": self.theme,
"css": self.css,
"title": self.title or "Gradio",
"is_space": self.is_space,
"enable_queue": getattr(
self, "enable_queue", False
), # attribute set at launch
Expand Down
2 changes: 1 addition & 1 deletion gradio/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.4b3
3.1.4b4
20 changes: 14 additions & 6 deletions ui/packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ interface Payload {
declare let BUILD_MODE: string;
declare let BACKEND_URL: string;

const WS_ENDPOINT =
BUILD_MODE === "dev" || location.origin === "http://localhost:3000"
? `ws://${BACKEND_URL.replace("http://", "")}queue/join`
: `ws://${location.host}/queue/join`;

async function post_data<
Return extends Record<string, unknown> = Record<string, unknown>
>(url: string, body: unknown): Promise<Return> {
Expand Down Expand Up @@ -66,7 +61,7 @@ type Output = {
const ws_map = new Map();

export const fn =
(session_hash: string, api_endpoint: string) =>
(session_hash: string, api_endpoint: string, is_space: boolean) =>
async ({
action,
payload,
Expand Down Expand Up @@ -107,6 +102,19 @@ export const fn =
ws_map.get(fn).connection.send(JSON.stringify(data));
}

var ws_protocol = location.protocol === "https:" ? "wss:" : "ws:";
if (is_space) {
var ws_path = location.pathname.slice(6, -2); // remove 'embed' from start, '+' from end
var ws_host = "spaces.huggingface.tech";
} else {
var ws_path = location.pathname === "/" ? "" : location.pathname;
var ws_host =
BUILD_MODE === "dev" || location.origin === "http://localhost:3000"
? BACKEND_URL.replace("http://", "").slice(0, -1)
: location.host;
}
const WS_ENDPOINT = `${ws_protocol}//${ws_host}${ws_path}/queue/join`;

const websocket_data = {
connection: new WebSocket(WS_ENDPOINT),
hash: Math.random().toString(36).substring(2)
Expand Down
3 changes: 2 additions & 1 deletion ui/packages/app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface Config {
theme: string;
title: string;
version: string;
is_space: boolean;
// allow_flagging: string;
// allow_interpretation: boolean;
// article: string;
Expand Down Expand Up @@ -182,7 +183,7 @@ function mount_app(
});
} else {
let session_hash = Math.random().toString(36).substring(2);
config.fn = fn(session_hash, config.root + "api/");
config.fn = fn(session_hash, config.root + "api/", config.is_space);

new Blocks({
target: wrapper,
Expand Down