Download frontend/src/utility-functions/network.ts from openfree/graphite2: direct link, hf CLI and curl.
- Browser
- Download file 1.6 kB
-
https://huggingface.co/spaces/openfree/graphite2/resolve/main/frontend/src/utility-functions/network.ts
- Command line
-
hf download hf://spaces/openfree/graphite2/frontend/src/utility-functions/network.ts
-
curl -L -o network.ts https://huggingface.co/spaces/openfree/graphite2/resolve/main/frontend/src/utility-functions/network.ts
1.6 kB
| export type RequestResult = { body: string; status: number }; | |
| // Special implementation using the legacy XMLHttpRequest API that provides callbacks to get: | |
| // - Calls with the percent progress uploading the request to the server | |
| // - Calls when downloading the result from the server, after the server has begun streaming back the response data | |
| // It returns a tuple of the promise as well as the XHR which can be used to call the `.abort()` method on it. | |
| export function requestWithUploadDownloadProgress( | |
| url: string, | |
| method: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH", | |
| body: string, | |
| uploadProgress: (progress: number) => void, | |
| downloadOccurring: () => void, | |
| ): [Promise<RequestResult>, XMLHttpRequest | undefined] { | |
| let xhrValue: XMLHttpRequest | undefined; | |
| const promise = new Promise<RequestResult>((resolve, reject) => { | |
| const xhr = new XMLHttpRequest(); | |
| xhr.upload.addEventListener("progress", (e) => uploadProgress(e.loaded / e.total)); | |
| xhr.addEventListener("progress", () => downloadOccurring()); | |
| xhr.addEventListener("load", () => resolve({ status: xhr.status, body: xhr.responseText })); | |
| xhr.addEventListener("abort", () => resolve({ status: xhr.status, body: xhr.responseText })); | |
| xhr.addEventListener("error", () => reject(new Error("Request error"))); | |
| xhr.open(method, url, true); | |
| xhr.setRequestHeader("accept", "*/*"); | |
| xhr.setRequestHeader("accept-language", "en-US,en;q=0.9"); | |
| xhr.setRequestHeader("content-type", "application/json"); | |
| xhrValue = xhr; | |
| xhr.send(body); | |
| }); | |
| return [promise, xhrValue]; | |
| } | |