| 59e6667 | | | 1 | import { log } from "@clack/prompts"; |
| 90d5eb8 | | | 2 | import { readFileSync } from "node:fs"; |
| 90d5eb8 | | | 3 | import { basename } from "node:path"; |
| e93a978 | | | 4 | import { getHub, getToken } from "./config.js"; |
| e93a978 | | | 5 | |
| e93a978 | | | 6 | export async function hubRequest<T>( |
| e93a978 | | | 7 | path: string, |
| e93a978 | | | 8 | options: RequestInit = {} |
| e93a978 | | | 9 | ): Promise<T> { |
| e93a978 | | | 10 | const hub = await getHub(); |
| e93a978 | | | 11 | const token = await getToken(); |
| e93a978 | | | 12 | |
| e93a978 | | | 13 | const url = `${hub}${path}`; |
| e93a978 | | | 14 | const res = await fetch(url, { |
| e93a978 | | | 15 | ...options, |
| e93a978 | | | 16 | headers: { |
| e93a978 | | | 17 | "Content-Type": "application/json", |
| e93a978 | | | 18 | Authorization: `Bearer ${token}`, |
| e93a978 | | | 19 | ...options.headers, |
| e93a978 | | | 20 | }, |
| e93a978 | | | 21 | }); |
| e93a978 | | | 22 | |
| e93a978 | | | 23 | if (!res.ok) { |
| e93a978 | | | 24 | const body = await res.text(); |
| e93a978 | | | 25 | let message: string; |
| e93a978 | | | 26 | try { |
| e93a978 | | | 27 | message = JSON.parse(body).error || body; |
| e93a978 | | | 28 | } catch { |
| e93a978 | | | 29 | message = body; |
| e93a978 | | | 30 | } |
| 59e6667 | | | 31 | log.error(`Error ${res.status}: ${message}`); |
| e93a978 | | | 32 | process.exit(1); |
| e93a978 | | | 33 | } |
| e93a978 | | | 34 | |
| e93a978 | | | 35 | return res.json() as Promise<T>; |
| e93a978 | | | 36 | } |
| 90d5eb8 | | | 37 | |
| 90d5eb8 | | | 38 | /** |
| 90d5eb8 | | | 39 | * Upload a file to a hub endpoint and stream SSE events back. |
| 90d5eb8 | | | 40 | * Used for import-bundle which streams gitimport progress. |
| 90d5eb8 | | | 41 | */ |
| 90d5eb8 | | | 42 | export async function hubUploadStream( |
| 90d5eb8 | | | 43 | path: string, |
| 90d5eb8 | | | 44 | filePath: string, |
| 90d5eb8 | | | 45 | onEvent: (event: string, data: any) => void, |
| 90d5eb8 | | | 46 | ): Promise<void> { |
| 90d5eb8 | | | 47 | const hub = await getHub(); |
| 90d5eb8 | | | 48 | const token = await getToken(); |
| 90d5eb8 | | | 49 | |
| 90d5eb8 | | | 50 | const fileData = readFileSync(filePath); |
| 90d5eb8 | | | 51 | const blob = new Blob([fileData], { type: "application/gzip" }); |
| 90d5eb8 | | | 52 | |
| 90d5eb8 | | | 53 | const form = new FormData(); |
| 90d5eb8 | | | 54 | form.append("file", blob, basename(filePath)); |
| 90d5eb8 | | | 55 | |
| 90d5eb8 | | | 56 | const res = await fetch(`${hub}${path}`, { |
| 90d5eb8 | | | 57 | method: "POST", |
| 90d5eb8 | | | 58 | headers: { |
| 90d5eb8 | | | 59 | Authorization: `Bearer ${token}`, |
| 90d5eb8 | | | 60 | }, |
| 90d5eb8 | | | 61 | body: form, |
| 90d5eb8 | | | 62 | }); |
| 90d5eb8 | | | 63 | |
| 90d5eb8 | | | 64 | if (!res.ok && !res.headers.get("content-type")?.includes("text/event-stream")) { |
| 90d5eb8 | | | 65 | const body = await res.text(); |
| 90d5eb8 | | | 66 | throw new Error(`Upload failed (${res.status}): ${body}`); |
| 90d5eb8 | | | 67 | } |
| 90d5eb8 | | | 68 | |
| 90d5eb8 | | | 69 | // Parse SSE stream |
| 90d5eb8 | | | 70 | const reader = res.body!.getReader(); |
| 90d5eb8 | | | 71 | const decoder = new TextDecoder(); |
| 90d5eb8 | | | 72 | let buffer = ""; |
| 90d5eb8 | | | 73 | |
| 90d5eb8 | | | 74 | while (true) { |
| 90d5eb8 | | | 75 | const { done, value } = await reader.read(); |
| 90d5eb8 | | | 76 | if (done) break; |
| 90d5eb8 | | | 77 | |
| 90d5eb8 | | | 78 | buffer += decoder.decode(value, { stream: true }); |
| 90d5eb8 | | | 79 | const lines = buffer.split("\n"); |
| 90d5eb8 | | | 80 | buffer = lines.pop()!; // keep incomplete line in buffer |
| 90d5eb8 | | | 81 | |
| 90d5eb8 | | | 82 | let currentEvent = ""; |
| 90d5eb8 | | | 83 | for (const line of lines) { |
| 90d5eb8 | | | 84 | if (line.startsWith("event: ")) { |
| 90d5eb8 | | | 85 | currentEvent = line.slice(7); |
| 90d5eb8 | | | 86 | } else if (line.startsWith("data: ")) { |
| 90d5eb8 | | | 87 | try { |
| 90d5eb8 | | | 88 | const data = JSON.parse(line.slice(6)); |
| 90d5eb8 | | | 89 | onEvent(currentEvent, data); |
| 90d5eb8 | | | 90 | if (currentEvent === "error") { |
| 90d5eb8 | | | 91 | throw new Error(data.message ?? "Import failed"); |
| 90d5eb8 | | | 92 | } |
| 90d5eb8 | | | 93 | } catch (e) { |
| 90d5eb8 | | | 94 | if (e instanceof SyntaxError) continue; // skip malformed JSON |
| 90d5eb8 | | | 95 | throw e; |
| 90d5eb8 | | | 96 | } |
| 90d5eb8 | | | 97 | } |
| 90d5eb8 | | | 98 | } |
| 90d5eb8 | | | 99 | } |
| 90d5eb8 | | | 100 | } |