|
11 | 11 | * where `siteId` is derived from a SHA-256 over the project files. |
12 | 12 | */ |
13 | 13 |
|
14 | | -import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 14 | +import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; |
15 | 15 | import { tmpdir } from "node:os"; |
16 | 16 | import { join } from "node:path"; |
17 | 17 | import { S3Client } from "@aws-sdk/client-s3"; |
| 18 | +import { CONTENT_STORE_RETENTION_DAYS } from "../contentStore.js"; |
18 | 19 | import { hashProjectDir, listSiteFiles } from "@hyperframes/producer/distributed"; |
19 | 20 | import { |
| 21 | + downloadS3ObjectToFile, |
| 22 | + ensureContentStoreObject, |
20 | 23 | mapConcurrent, |
| 24 | + parseS3Uri, |
21 | 25 | putContentStoreFile, |
22 | 26 | sha256File, |
23 | 27 | uploadContentAddressedFileToS3, |
24 | 28 | } from "../s3Transport.js"; |
25 | | -import { serializeSiteManifest, siteManifestUri, type SiteFile } from "../siteManifest.js"; |
| 29 | +import { |
| 30 | + isSiteManifestUri, |
| 31 | + parseSiteManifest, |
| 32 | + serializeSiteManifest, |
| 33 | + siteManifestUri, |
| 34 | + type SiteFile, |
| 35 | +} from "../siteManifest.js"; |
26 | 36 |
|
27 | 37 | /** Options for {@link deploySite}. */ |
28 | 38 | export interface DeploySiteOptions { |
@@ -64,6 +74,43 @@ export function siteHandleFromId(bucketName: string, siteId: string): SiteHandle |
64 | 74 | return { siteId, bucketName, projectS3Uri, bytes: 0, uploadedAt: "", uploaded: false }; |
65 | 75 | } |
66 | 76 |
|
| 77 | +/** Before a render reuses `site`, re-store any file the content store expired, from `projectDir`. */ |
| 78 | +export async function ensureSiteStored( |
| 79 | + site: SiteHandle, |
| 80 | + s3: S3Client, |
| 81 | + projectDir?: string, |
| 82 | +): Promise<void> { |
| 83 | + if (!isSiteManifestUri(site.projectS3Uri)) return; |
| 84 | + const { bucket } = parseS3Uri(site.projectS3Uri); |
| 85 | + const workdir = mkdtempSync(join(tmpdir(), "hf-site-check-")); |
| 86 | + try { |
| 87 | + const manifestPath = join(workdir, "files.json"); |
| 88 | + await downloadS3ObjectToFile(s3, site.projectS3Uri, manifestPath); |
| 89 | + const files = parseSiteManifest(readFileSync(manifestPath, "utf-8")); |
| 90 | + const missing: string[] = []; |
| 91 | + await mapConcurrent(files, 16, async ({ path, sha256, sizeBytes }) => { |
| 92 | + try { |
| 93 | + await ensureContentStoreObject(s3, bucket, sha256, sizeBytes); |
| 94 | + } catch (error) { |
| 95 | + if ((error as Error).name !== "PLAN_ARTIFACT_MISSING") throw error; |
| 96 | + if (!projectDir) return void missing.push(path); |
| 97 | + await putContentStoreFile(s3, join(projectDir, path), bucket, sha256); |
| 98 | + } |
| 99 | + }); |
| 100 | + if (missing.length > 0) { |
| 101 | + const error = new Error( |
| 102 | + `[deploySite] SITE_FILES_MISSING: ${missing.length} of ${files.length} files of site ` + |
| 103 | + `${site.siteId} are no longer stored (unused for ${CONTENT_STORE_RETENTION_DAYS} days). ` + |
| 104 | + "Run `hyperframes lambda sites create`, or pass projectDir to re-upload them.", |
| 105 | + ); |
| 106 | + error.name = "SITE_FILES_MISSING"; |
| 107 | + throw error; |
| 108 | + } |
| 109 | + } finally { |
| 110 | + rmSync(workdir, { recursive: true, force: true }); |
| 111 | + } |
| 112 | +} |
| 113 | + |
67 | 114 | /** Upload `projectDir`'s files to the content store and write the site manifest. */ |
68 | 115 | export async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> { |
69 | 116 | const s3 = opts.s3 ?? new S3Client({ region: opts.region }); |
|
0 commit comments