Spaces:
Running on Zero
Running on Zero
Download frontend/src/engine.ts from fffiloni/idol-commons: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/fffiloni/idol-commons/resolve/main/frontend/src/engine.ts
- Command line
-
hf download hf://spaces/fffiloni/idol-commons/frontend/src/engine.ts
-
curl -L -o engine.ts https://huggingface.co/spaces/fffiloni/idol-commons/resolve/main/frontend/src/engine.ts
1.39 kB
| /** The same Hub handshake as Gradio 5.49.1's frontend, scoped to its parent. */ | |
| declare global {interface Window {supports_zerogpu_headers?:boolean}} | |
| const HUB='https://huggingface.co'; | |
| const SUPPORT='supports-zerogpu-headers'; | |
| let handshake:Promise<void>|undefined; | |
| export function initializeHubQuota(){ | |
| if(handshake)return handshake; | |
| if(window.parent===window||!location.hostname.endsWith('.hf.space'))return Promise.resolve(); | |
| handshake=new Promise(resolve=>{ | |
| const receive=(event:MessageEvent)=>{ | |
| if(event.source!==window.parent||event.origin!==HUB||event.data!==SUPPORT)return; | |
| window.supports_zerogpu_headers=true; | |
| window.removeEventListener('message',receive);clearTimeout(timer);resolve(); | |
| }; | |
| const timer=window.setTimeout(()=>{window.removeEventListener('message',receive);handshake=undefined;resolve();},2000); | |
| window.addEventListener('message',receive); | |
| window.parent.postMessage(SUPPORT,HUB); | |
| }); | |
| return handshake; | |
| } | |
| export async function connectEngine(csrf:string){ | |
| await initializeHubQuota(); | |
| const {Client}=await import('@gradio/client'); | |
| // The official client obtains fresh Hub-signed ZeroGPU headers for each submit. | |
| // These headers only select compute quota; they never grant contribution rights. | |
| return Client.connect(location.origin+'/engine/',{headers:{'X-CSRF-Token':csrf},events:['data','status']}); | |
| } | |