-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathzerogpu.ts
More file actions
40 lines (33 loc) · 1.05 KB
/
zerogpu.ts
File metadata and controls
40 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const ZEROGPU_HEADERS_MESSAGE = "supports-zerogpu-headers";
let zerogpu_handshake_initialized = false;
function supports_browser_handshake(): boolean {
return (
typeof window !== "undefined" &&
typeof document !== "undefined" &&
typeof window.addEventListener === "function"
);
}
export function get_zerogpu_origin(hostname: string): string | null {
if (hostname.includes(".dev.")) {
return `https://moon-${hostname.split(".")[1]}.dev.spaces.huggingface.tech`;
}
if (hostname.endsWith(".hf.space")) {
return "https://huggingface.co";
}
return null;
}
export function initialize_zerogpu_handshake(): void {
if (!supports_browser_handshake() || zerogpu_handshake_initialized) {
return;
}
window.addEventListener("message", (event) => {
if (event.data === ZEROGPU_HEADERS_MESSAGE) {
window.supports_zerogpu_headers = true;
}
});
zerogpu_handshake_initialized = true;
const origin = get_zerogpu_origin(window.location.hostname);
if (origin && window.parent !== window) {
window.parent.postMessage(ZEROGPU_HEADERS_MESSAGE, origin);
}
}