|
| 1 | +import { basename, join, relative } from "node:path"; |
| 2 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 3 | +import AdmZip from "adm-zip"; |
| 4 | + |
| 5 | +const IGNORED_DIRS = new Set([".git", "node_modules", "dist", ".next", "coverage"]); |
| 6 | +const IGNORED_FILES = new Set([".DS_Store", "Thumbs.db"]); |
| 7 | + |
| 8 | +export interface PublishArchiveResult { |
| 9 | + buffer: Buffer; |
| 10 | + fileCount: number; |
| 11 | +} |
| 12 | + |
| 13 | +export interface PublishedProjectResponse { |
| 14 | + projectId: string; |
| 15 | + title: string; |
| 16 | + fileCount: number; |
| 17 | + url: string; |
| 18 | + claimToken: string; |
| 19 | +} |
| 20 | + |
| 21 | +function shouldIgnoreSegment(segment: string): boolean { |
| 22 | + return segment.startsWith(".") || IGNORED_DIRS.has(segment) || IGNORED_FILES.has(segment); |
| 23 | +} |
| 24 | + |
| 25 | +function collectProjectFiles(rootDir: string, currentDir: string, paths: string[]): void { |
| 26 | + for (const entry of readdirSync(currentDir, { withFileTypes: true })) { |
| 27 | + if (shouldIgnoreSegment(entry.name)) continue; |
| 28 | + const absolutePath = join(currentDir, entry.name); |
| 29 | + const relativePath = relative(rootDir, absolutePath).replaceAll("\\", "/"); |
| 30 | + if (!relativePath) continue; |
| 31 | + |
| 32 | + if (entry.isDirectory()) { |
| 33 | + collectProjectFiles(rootDir, absolutePath, paths); |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + if (!statSync(absolutePath).isFile()) continue; |
| 38 | + paths.push(relativePath); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +export function createPublishArchive(projectDir: string): PublishArchiveResult { |
| 43 | + const filePaths: string[] = []; |
| 44 | + collectProjectFiles(projectDir, projectDir, filePaths); |
| 45 | + if (!filePaths.includes("index.html")) { |
| 46 | + throw new Error("Project must include an index.html file at the root before publish."); |
| 47 | + } |
| 48 | + |
| 49 | + const archive = new AdmZip(); |
| 50 | + for (const filePath of filePaths) { |
| 51 | + archive.addFile(filePath, readFileSync(join(projectDir, filePath))); |
| 52 | + } |
| 53 | + |
| 54 | + return { |
| 55 | + buffer: archive.toBuffer(), |
| 56 | + fileCount: filePaths.length, |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export function getPublishApiBaseUrl(): string { |
| 61 | + return ( |
| 62 | + process.env["HYPERFRAMES_PUBLISHED_PROJECTS_API_URL"] || |
| 63 | + process.env["HEYGEN_API_URL"] || |
| 64 | + "https://api2.heygen.com" |
| 65 | + ).replace(/\/$/, ""); |
| 66 | +} |
| 67 | + |
| 68 | +export async function publishProjectArchive(projectDir: string): Promise<PublishedProjectResponse> { |
| 69 | + const title = basename(projectDir); |
| 70 | + const archive = createPublishArchive(projectDir); |
| 71 | + const archiveBytes = new Uint8Array(archive.buffer.byteLength); |
| 72 | + archiveBytes.set(archive.buffer); |
| 73 | + const body = new FormData(); |
| 74 | + body.set("title", title); |
| 75 | + body.set("file", new File([archiveBytes], `${title}.zip`, { type: "application/zip" })); |
| 76 | + const headers: Record<string, string> = { |
| 77 | + heygen_route: "canary", |
| 78 | + }; |
| 79 | + |
| 80 | + const response = await fetch(`${getPublishApiBaseUrl()}/v1/hyperframes/projects/publish`, { |
| 81 | + method: "POST", |
| 82 | + body, |
| 83 | + headers, |
| 84 | + signal: AbortSignal.timeout(30_000), |
| 85 | + }); |
| 86 | + |
| 87 | + const payload = await response.json().catch(() => null); |
| 88 | + const message = |
| 89 | + typeof payload?.message === "string" ? payload.message : "Failed to publish project"; |
| 90 | + if (!response.ok || !payload?.data) { |
| 91 | + throw new Error(message); |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + projectId: String(payload.data.project_id), |
| 96 | + title: String(payload.data.title), |
| 97 | + fileCount: Number(payload.data.file_count), |
| 98 | + url: String(payload.data.url), |
| 99 | + claimToken: String(payload.data.claim_token), |
| 100 | + }; |
| 101 | +} |
0 commit comments