diff --git a/actions/build/scripts/constants.mjs b/actions/build/scripts/constants.mjs
index 2617710b2..e3a01e559 100644
--- a/actions/build/scripts/constants.mjs
+++ b/actions/build/scripts/constants.mjs
@@ -9,7 +9,7 @@ export const CONTENT_TYPES = {
".wasm": "application/wasm",
}
export const MAX_ENTRIES = 2_000
-export const MAX_FILE_BYTES = 25 * 1024 * 1024
+export const MAX_FILE_BYTES = 100 * 1024 * 1024
export const MAX_PATH_DEPTH = 20
export const MAX_SCREENSHOT_BYTES = 25 * 1024 * 1024
export const MAX_SCREENSHOT_HEIGHT = 16_384
diff --git a/actions/build/scripts/validate-build-output/index.test.mjs b/actions/build/scripts/validate-build-output/index.test.mjs
index 101aaa77b..16736af19 100644
--- a/actions/build/scripts/validate-build-output/index.test.mjs
+++ b/actions/build/scripts/validate-build-output/index.test.mjs
@@ -1,5 +1,5 @@
import assert from "node:assert/strict"
-import { readdir, symlink, writeFile } from "node:fs/promises"
+import { readdir, symlink, truncate, writeFile } from "node:fs/promises"
import path from "node:path"
import { describe, it } from "node:test"
@@ -53,6 +53,38 @@ describe("validateBuildOutput()", () => {
})
})
+ it("accepts a 100 MiB index and rejects one additional byte", async () => {
+ const root = await buildRoot()
+ const index = path.join(root, "dist", "index.html")
+ const maximumBytes = 100 * 1024 * 1024
+ await truncate(index, maximumBytes)
+
+ assert.deepEqual(await validateBuildOutput(root), {
+ bytes: maximumBytes,
+ files: [{ path: "index.html", bytes: maximumBytes }],
+ })
+
+ await truncate(index, maximumBytes + 1)
+
+ await assert.rejects(
+ validateBuildOutput(root),
+ /Build output file must be no larger than 104857600 bytes: index\.html has 104857601 bytes\./,
+ )
+ })
+
+ it("retains the 100 MiB aggregate limit for individually allowed files", async () => {
+ const root = await buildRoot()
+ await writeFile(path.join(root, "dist", "index.html"), "hi")
+ const asset = path.join(root, "dist", "data.bin")
+ await writeFile(asset, "")
+ await truncate(asset, 100 * 1024 * 1024 - 1)
+
+ await assert.rejects(
+ validateBuildOutput(root),
+ /Build output files must total no more than 104857600 bytes; found 104857601\./,
+ )
+ })
+
it("rejects symbolic links", async () => {
const root = await buildRoot()
await symlink("index.html", path.join(root, "dist", "linked.html"))
diff --git a/hub/docs/artifact-storage.md b/hub/docs/artifact-storage.md
index ee8abf96e..00f818030 100644
--- a/hub/docs/artifact-storage.md
+++ b/hub/docs/artifact-storage.md
@@ -47,7 +47,7 @@ The API's `/api/ready` includes configured storage. `isReady()` has a two-second
Keys are case-sensitive opaque strings of 1–128 ASCII letters, digits, underscores, or hyphens, beginning with a letter or digit. They are not paths or URLs. The domain rejects traversal, separators, percent encoding, and other characters before calling a provider.
-Writes accept a `Uint8Array` of at most 32 MiB, including empty bytes, and never overwrite an existing key. Reads return a bounded `Uint8Array`. A successful operation returns `{ status: "ok" }`; a successful read also includes `bytes`. Each operation has its own result type: reads may fail with `invalid-key`, `too-large`, `not-found`, or `unavailable`; writes with `invalid-key`, `too-large`, `already-exists`, or `unavailable`; deletes with `invalid-key`, `not-found`, or `unavailable`. Missing reads and deletes are explicit `not-found` results, not empty bytes or silent success. Provider messages and credentials are not included in results.
+Writes accept a `Uint8Array` of at most 100 MiB (104,857,600 bytes), including empty bytes, and never overwrite an existing key. Reads return a `Uint8Array` bounded by the same limit. A successful operation returns `{ status: "ok" }`; a successful read also includes `bytes`. Each operation has its own result type: reads may fail with `invalid-key`, `too-large`, `not-found`, or `unavailable`; writes with `invalid-key`, `too-large`, `already-exists`, or `unavailable`; deletes with `invalid-key`, `not-found`, or `unavailable`. Missing reads and deletes are explicit `not-found` results, not empty bytes or silent success. Provider messages and credentials are not included in results.
Immutability prevents overwriting bytes while a key exists. Deleting a key permits later reuse. Callers own authorization, key allocation, retention, and coordination between deletion and other work. Build integrations use opaque deterministic keys and persist `BuildArtifact.kind`, package-relative `path`, `storageKey`, and `byteLength`; preview serving derives the response content type from the checked package path.
diff --git a/hub/docs/build-previews.md b/hub/docs/build-previews.md
index cd7e040c5..afca860aa 100644
--- a/hub/docs/build-previews.md
+++ b/hub/docs/build-previews.md
@@ -22,6 +22,10 @@ Package paths remain relative and may not contain empty, dot, dot-dot, control,
Content types come from the checked package path, with unknown extensions served as `application/octet-stream`. HTML is private and revalidated. Immutable assets use a private one-year cache. Desktop and narrow screenshots are served from their package paths through the same Build authorization.
+Preview reads share a 100 MiB source-byte allowance per API process across Build origins, private previews, template previews, and public links. Hub reserves each file's recorded size before reading storage and keeps that reservation until the read settles and the HTTP response finishes or disconnects. A request returns 503 without reading the file when the allowance is full. Small and empty files reserve at least 64 KiB each. Temporary template-manifest reads release their reservation before reading the selected HTML file.
+
+This allowance bounds concurrent buffered preview files, not total process memory. Storage downloads can hold chunks and their combined buffer at once, and HTML bridge injection needs additional strings and encoded bytes. Imports and other runtime work need separate headroom within the deployment's memory allocation.
+
HTML responses inject the generated hosted preview bridge. The bridge installs the shared Controls engine and bindings, which load that Build's `/controls.replica.json`, and reports review element or area selections to the authenticated parent page. Review records store the anchor and Build identity separately; the bridge does not write artifacts or receive Hub session cookies. The generated bridge is checked by `npm run controls:assets:check`.
## Browser boundary
diff --git a/hub/docs/github-actions-builds.md b/hub/docs/github-actions-builds.md
index b6c435c11..e1f2a701c 100644
--- a/hub/docs/github-actions-builds.md
+++ b/hub/docs/github-actions-builds.md
@@ -175,7 +175,21 @@ Deletion events are still recorded when received, rather than confirmed synchron
`replica-screenshots-v1` contains `screenshots/**` and `screenshots.json`, `{ "version": 1, "distDigest": "sha256:…" }`. `distDigest` is the SHA-256 of the sorted `path\0sha256\n` lines for every dist file and `templates.json`.
-Both packages use the same path, size, and entry-count limits as local Builds.
+Both packages use the same path, size, and entry-count limits as local Builds:
+
+| Content | Limit |
+| ----------------------------- | ------------------------- |
+| Each static file | 100 MiB (104,857,600 bytes) |
+| All uncompressed `dist/` files | 100 MiB |
+| Each screenshot | 25 MiB |
+| All extracted package files | 150 MiB |
+| Downloaded ZIP | 160 MiB |
+
+A separate data asset must leave room for `index.html` and the rest of the app within the total `dist/` budget. Package validation also enforces path, entry-count, and archive protections.
+
+Hub buffers the downloaded ZIP and extracted files in memory; storage writes also copy the current file to preserve its bytes during the write. Keep the default on-demand worker concurrency of one for the 1 GiB Azure runtime. Additional concurrent imports need memory headroom. The [preview layer](build-previews.md#file-behavior) bounds buffered responses across concurrent HTTP requests independently of worker concurrency.
+
+To publish larger files, run Hub with this limit and update every selected workflow's Replica publish, screenshots, and optional build actions to the same reviewed revision containing it. Older action pins retain their earlier limits. Updating Replica alone neither deploys Hub nor changes downstream workflow references.
## Screenshots
diff --git a/hub/server/domains/artifacts/create-artifact-storage/index.test.ts b/hub/server/domains/artifacts/create-artifact-storage/index.test.ts
index 1c3466f3b..d1748fcb7 100644
--- a/hub/server/domains/artifacts/create-artifact-storage/index.test.ts
+++ b/hub/server/domains/artifacts/create-artifact-storage/index.test.ts
@@ -1,11 +1,11 @@
import { randomUUID } from "node:crypto"
-import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises"
+import { mkdir, readFile, rm, symlink, truncate, writeFile } from "node:fs/promises"
import { resolve } from "node:path"
import { describe, expect, it, onTestFinished, vi } from "vitest"
import { createAzureBlobArtifactStorage } from "../../../integrations/azure-blob/create-azure-blob-artifact-storage"
-import { artifactMaxBytes, createArtifactStorage } from "."
+import { createArtifactStorage } from "."
vi.mock("../../../integrations/azure-blob/create-azure-blob-artifact-storage", () => ({
createAzureBlobArtifactStorage: vi.fn(),
@@ -117,15 +117,17 @@ describe("createArtifactStorage()", () => {
await mkdir(directory)
onTestFinished(() => rm(directory, { recursive: true, force: true }))
const storage = await createArtifactStorage({ provider: "filesystem", directory })
+ const maximumBytes = 100 * 1024 * 1024
- expect(await storage.write("limit", new Uint8Array(artifactMaxBytes))).toEqual({ status: "ok" })
+ expect(await storage.write("limit", new Uint8Array(maximumBytes))).toEqual({ status: "ok" })
const result = await storage.read("limit")
expect(result.status).toBe("ok")
- if (result.status === "ok") expect(result.bytes.length).toBe(artifactMaxBytes)
- expect(await storage.write("large", new Uint8Array(artifactMaxBytes + 1))).toEqual({
+ if (result.status === "ok") expect(result.bytes.length).toBe(maximumBytes)
+ expect(await storage.write("large", new Uint8Array(maximumBytes + 1))).toEqual({
status: "too-large",
})
- await writeFile(`${directory}/large`, new Uint8Array(artifactMaxBytes + 1))
+ await writeFile(`${directory}/large`, "")
+ await truncate(`${directory}/large`, maximumBytes + 1)
expect(await storage.read("large")).toEqual({ status: "too-large" })
})
diff --git a/hub/server/domains/artifacts/create-artifact-storage/index.ts b/hub/server/domains/artifacts/create-artifact-storage/index.ts
index 6841cbac9..e6464d81c 100644
--- a/hub/server/domains/artifacts/create-artifact-storage/index.ts
+++ b/hub/server/domains/artifacts/create-artifact-storage/index.ts
@@ -4,7 +4,7 @@ import { log } from "../../../integrations/pino/log/index.ts"
import { parseArtifactStorageSettings } from "../parse-artifact-storage-settings/index.ts"
import type { ArtifactStorage, ArtifactStorageSettings } from "./types.ts"
-export const artifactMaxBytes = 32 * 1024 * 1024
+export const artifactMaxBytes = 100 * 1024 * 1024
const keyPattern = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/
export async function createArtifactStorage(
diff --git a/hub/server/domains/build-artifacts/constants.ts b/hub/server/domains/build-artifacts/constants.ts
index 7d571b66e..c1101a376 100644
--- a/hub/server/domains/build-artifacts/constants.ts
+++ b/hub/server/domains/build-artifacts/constants.ts
@@ -1,6 +1,6 @@
export const maximumBuildPackageEntries = 2_090
export const maximumDistFiles = 2_000
-export const maximumDistFileBytes = 25 * 1024 * 1024
+export const maximumDistFileBytes = 100 * 1024 * 1024
export const maximumDistPathDepth = 20
export const maximumScreenshotBytes = 25 * 1024 * 1024
// Matches the build action: dist output is capped separately from the whole package.
diff --git a/hub/server/domains/build-artifacts/extract-dist-package/index.test.ts b/hub/server/domains/build-artifacts/extract-dist-package/index.test.ts
index 18b9f238b..193dfd428 100644
--- a/hub/server/domains/build-artifacts/extract-dist-package/index.test.ts
+++ b/hub/server/domains/build-artifacts/extract-dist-package/index.test.ts
@@ -88,6 +88,14 @@ describe("extractDistPackage", () => {
}
}
+ it("rejects a static file one byte larger than 100 MiB", () => {
+ const archive = createZipArchive([
+ { name: "dist/index.html", bytes: Buffer.alloc(100 * 1024 * 1024 + 1) },
+ ])
+
+ expect(() => extractDistPackage(archive)).toThrow("dist/index.html exceeds its size limit.")
+ })
+
it("accepts a legacy package with full dist output and screenshots", () => {
const archive = createZipArchive([
...dist(),
diff --git a/hub/server/domains/build-preview/read-build-preview-file/index.test.ts b/hub/server/domains/build-preview/read-build-preview-file/index.test.ts
index 505bc57d7..fd31f307e 100644
--- a/hub/server/domains/build-preview/read-build-preview-file/index.test.ts
+++ b/hub/server/domains/build-preview/read-build-preview-file/index.test.ts
@@ -4,11 +4,12 @@ import { readBuildPreviewFile } from "."
describe("readBuildPreviewFile()", () => {
it("serves directory-style routes through the application entry point", async () => {
+ const bytes = new TextEncoder().encode("
Checkout
")
const findArtifact = vi.fn().mockImplementation((_buildId: string, path: string) => {
if (path === "dist/index.html") {
return Promise.resolve({
storageKey: "index-key",
- byteLength: 15,
+ byteLength: bytes.byteLength,
})
}
return Promise.resolve(null)
@@ -26,7 +27,7 @@ describe("readBuildPreviewFile()", () => {
isReady: vi.fn(),
read: vi.fn().mockResolvedValue({
status: "ok",
- bytes: new TextEncoder().encode("Checkout
"),
+ bytes,
}),
write: vi.fn(),
delete: vi.fn(),
@@ -46,13 +47,6 @@ describe("readBuildPreviewFile()", () => {
})
it("serves dotted routes declared by the template manifest", async () => {
- const findArtifact = vi.fn().mockImplementation((_buildId: string, path: string) => {
- const artifacts = {
- "templates.json": { storageKey: "manifest-key", byteLength: 166 },
- "dist/index.html": { storageKey: "index-key", byteLength: 15 },
- }
- return Promise.resolve(artifacts[path as keyof typeof artifacts] ?? null)
- })
const manifest = {
version: 1,
templates: [
@@ -65,6 +59,15 @@ describe("readBuildPreviewFile()", () => {
},
],
}
+ const manifestBytes = new TextEncoder().encode(JSON.stringify(manifest))
+ const htmlBytes = new TextEncoder().encode("Release notes
")
+ const findArtifact = vi.fn().mockImplementation((_buildId: string, path: string) => {
+ const artifacts = {
+ "templates.json": { storageKey: "manifest-key", byteLength: manifestBytes.byteLength },
+ "dist/index.html": { storageKey: "index-key", byteLength: htmlBytes.byteLength },
+ }
+ return Promise.resolve(artifacts[path as keyof typeof artifacts] ?? null)
+ })
const result = await readBuildPreviewFile(
{
@@ -81,11 +84,11 @@ describe("readBuildPreviewFile()", () => {
storageKey === "manifest-key"
? {
status: "ok",
- bytes: new TextEncoder().encode(JSON.stringify(manifest)),
+ bytes: manifestBytes,
}
: {
status: "ok",
- bytes: new TextEncoder().encode("Release notes
"),
+ bytes: htmlBytes,
},
),
),
@@ -129,6 +132,44 @@ describe("readBuildPreviewFile()", () => {
)
})
+ it.each(["asset", "template manifest"] as const)(
+ "rejects %s bytes that exceed the saved artifact size",
+ async (artifactKind) => {
+ const read = vi.fn().mockResolvedValue({
+ status: "ok",
+ bytes: new TextEncoder().encode(
+ artifactKind === "asset"
+ ? "asset bytes"
+ : JSON.stringify({ templates: [{ path: "/release/v2.0" }] }),
+ ),
+ })
+ const findArtifact = vi
+ .fn()
+ .mockImplementation((_buildId: string, artifactPath: string) =>
+ Promise.resolve(
+ artifactKind === "asset" || artifactPath === "templates.json"
+ ? { storageKey: "underreported-key", byteLength: 1 }
+ : null,
+ ),
+ )
+
+ const result = await readBuildPreviewFile(
+ {
+ acceptsHtml: artifactKind === "template manifest",
+ buildId: "00000000-0000-4000-8000-000000000001",
+ requestPath: artifactKind === "asset" ? "/asset.bin" : "/release/v2.0",
+ },
+ {
+ findArtifact,
+ storage: { isReady: vi.fn(), read, write: vi.fn(), delete: vi.fn() },
+ },
+ )
+
+ expect(result).toEqual({ status: "unavailable" })
+ expect(read).toHaveBeenCalledOnce()
+ },
+ )
+
it("decodes one URL path layer before artifact lookup", async () => {
const findArtifact = vi.fn().mockResolvedValue({
storageKey: "banner-key",
diff --git a/hub/server/domains/build-preview/read-build-preview-file/index.ts b/hub/server/domains/build-preview/read-build-preview-file/index.ts
index bda22bc9f..f2e42f2f4 100644
--- a/hub/server/domains/build-preview/read-build-preview-file/index.ts
+++ b/hub/server/domains/build-preview/read-build-preview-file/index.ts
@@ -65,13 +65,22 @@ export async function readBuildPreviewFile(
if (!artifact && input.acceptsHtml && !useSpaFallback) {
const manifest = await dependencies.findArtifact(input.buildId, "templates.json")
if (manifest) {
- const storedManifest = await dependencies.storage.read(manifest.storageKey)
- if (storedManifest.status !== "ok") {
- return {
- status: storedManifest.status === "not-found" ? "not-found" : "unavailable",
+ const release = dependencies.reserveBytes?.(manifest.byteLength)
+ if (release === null) return { status: "unavailable" }
+ try {
+ const storedManifest = await dependencies.storage.read(manifest.storageKey)
+ if (storedManifest.status !== "ok") {
+ return {
+ status: storedManifest.status === "not-found" ? "not-found" : "unavailable",
+ }
+ }
+ if (storedManifest.bytes.byteLength > manifest.byteLength) {
+ return { status: "unavailable" }
}
+ useSpaFallback = readDeclaredRoutePathnames(storedManifest.bytes).includes(requestPath)
+ } finally {
+ release?.()
}
- useSpaFallback = readDeclaredRoutePathnames(storedManifest.bytes).includes(requestPath)
}
}
if (!artifact && useSpaFallback) {
@@ -80,10 +89,14 @@ export async function readBuildPreviewFile(
}
if (!artifact) return { status: "not-found" }
+ if (dependencies.reserveBytes?.(artifact.byteLength) === null) {
+ return { status: "unavailable" }
+ }
const stored = await dependencies.storage.read(artifact.storageKey)
if (stored.status !== "ok") {
return { status: stored.status === "not-found" ? "not-found" : "unavailable" }
}
+ if (stored.bytes.byteLength > artifact.byteLength) return { status: "unavailable" }
const contentType = contentTypeForBuildPath(path)
const html = contentType.startsWith("text/html")
diff --git a/hub/server/domains/build-preview/read-build-preview-file/types.ts b/hub/server/domains/build-preview/read-build-preview-file/types.ts
index 99ed33fb0..4d8eace85 100644
--- a/hub/server/domains/build-preview/read-build-preview-file/types.ts
+++ b/hub/server/domains/build-preview/read-build-preview-file/types.ts
@@ -9,6 +9,7 @@ export interface ReadBuildPreviewFileInput {
export interface ReadBuildPreviewFileDependencies {
storage: ArtifactStorage
+ reserveBytes?(byteLength: number): (() => void) | null
findArtifact(
buildId: string,
path: string,
diff --git a/hub/server/domains/build-preview/reserve-build-preview-bytes/index.test.ts b/hub/server/domains/build-preview/reserve-build-preview-bytes/index.test.ts
new file mode 100644
index 000000000..7fe2ee064
--- /dev/null
+++ b/hub/server/domains/build-preview/reserve-build-preview-bytes/index.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from "vitest"
+
+import { reserveBuildPreviewBytes } from "."
+
+describe("reserveBuildPreviewBytes()", () => {
+ it("bounds empty and small reads without making released capacity available twice", () => {
+ const remaining = reserveBuildPreviewBytes(100 * 1024 * 1024 - 64 * 1024)
+ const empty = reserveBuildPreviewBytes(0)
+ let small: (() => void) | null = null
+
+ try {
+ expect(remaining).toBeTypeOf("function")
+ expect(empty).toBeTypeOf("function")
+ expect(reserveBuildPreviewBytes(1)).toBeNull()
+
+ empty?.()
+ empty?.()
+ small = reserveBuildPreviewBytes(1)
+
+ expect(small).toBeTypeOf("function")
+ expect(reserveBuildPreviewBytes(1)).toBeNull()
+ } finally {
+ remaining?.()
+ empty?.()
+ small?.()
+ }
+ })
+})
diff --git a/hub/server/domains/build-preview/reserve-build-preview-bytes/index.ts b/hub/server/domains/build-preview/reserve-build-preview-bytes/index.ts
new file mode 100644
index 000000000..5b2646aca
--- /dev/null
+++ b/hub/server/domains/build-preview/reserve-build-preview-bytes/index.ts
@@ -0,0 +1,17 @@
+const maximumReservedBytes = 100 * 1024 * 1024
+const minimumReservationBytes = 64 * 1024
+let reservedBytes = 0
+
+/** Shares the source-byte allowance across preview requests in this process. */
+export function reserveBuildPreviewBytes(byteLength: number): (() => void) | null {
+ if (!Number.isSafeInteger(byteLength) || byteLength < 0) return null
+ const bytes = Math.max(byteLength, minimumReservationBytes)
+ if (bytes > maximumReservedBytes - reservedBytes) return null
+ reservedBytes += bytes
+ let released = false
+ return () => {
+ if (released) return
+ released = true
+ reservedBytes -= bytes
+ }
+}
diff --git a/hub/server/domains/github-builds/store-github-build-files/index.ts b/hub/server/domains/github-builds/store-github-build-files/index.ts
index fe5572f08..da2ab4f28 100644
--- a/hub/server/domains/github-builds/store-github-build-files/index.ts
+++ b/hub/server/domains/github-builds/store-github-build-files/index.ts
@@ -24,7 +24,7 @@ export async function storeGitHubBuildFiles(
const write = await storage.write(storageKey, file.bytes)
if (write.status === "already-exists") {
const existing = await storage.read(storageKey)
- if (existing.status !== "ok" || !Buffer.from(existing.bytes).equals(file.bytes)) {
+ if (existing.status !== "ok" || !file.bytes.equals(existing.bytes)) {
throw new Error(`Artifact storage contains different bytes for ${file.path}.`)
}
} else if (write.status !== "ok") {
diff --git a/hub/server/integrations/filesystem/create-filesystem-artifact-storage/index.ts b/hub/server/integrations/filesystem/create-filesystem-artifact-storage/index.ts
index 5c602aa9a..39db2850c 100644
--- a/hub/server/integrations/filesystem/create-filesystem-artifact-storage/index.ts
+++ b/hub/server/integrations/filesystem/create-filesystem-artifact-storage/index.ts
@@ -45,7 +45,7 @@ export async function createFilesystemArtifactStorage(
length += bytesRead
}
if (length > stat.size) return { status: "unavailable" } as const
- return { status: "ok", bytes: bytes.slice(0, length) } as const
+ return { status: "ok", bytes: bytes.subarray(0, length) } as const
} finally {
await handle.close()
}
diff --git a/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts b/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts
index 0efc28043..f80e6c1f5 100644
--- a/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts
+++ b/hub/server/routes/api/public-sharing/hosted-sharing.db.test.ts
@@ -26,6 +26,7 @@ const environment = {
}
const enabledEnvironment = { ...environment, HUB_HOSTED_PUBLIC_SHARING_ENABLED: "true" }
const linkForm = { buildNumber: 1, expiration: "seven_days", label: "Preview for review" }
+const previewHtml = "Saved Build"
type Viewer = NonNullable>>
type Permission = NonNullable
@@ -81,7 +82,7 @@ function createBuild(prototypeId: string, number: number, subject: string, succe
artifacts: {
create: [
{
- byteLength: 23,
+ byteLength: Buffer.byteLength(previewHtml),
storageKey: `${prototypeId}/${number}/dist`,
path: "dist/index.html",
kind: "dist",
@@ -110,7 +111,7 @@ async function startHub(
read: (key) =>
Promise.resolve(
key.endsWith("/dist")
- ? { status: "ok", bytes: Buffer.from("Saved Build") }
+ ? { status: "ok", bytes: Buffer.from(previewHtml) }
: key.endsWith("/screenshot")
? { status: "ok", bytes: Buffer.from("screenshot") }
: { status: "not-found" },
diff --git a/hub/server/routes/build-preview/serve/get/index.test.ts b/hub/server/routes/build-preview/serve/get/index.test.ts
index 20231165d..d21d59b72 100644
--- a/hub/server/routes/build-preview/serve/get/index.test.ts
+++ b/hub/server/routes/build-preview/serve/get/index.test.ts
@@ -1,12 +1,23 @@
-import { randomBytes, randomUUID } from "node:crypto"
+import { createHash, randomBytes, randomUUID } from "node:crypto"
import { once } from "node:events"
-import { createServer, request as httpRequest, type IncomingHttpHeaders } from "node:http"
+import { createReadStream } from "node:fs"
+import { mkdir, mkdtemp, open, readFile, rm, writeFile } from "node:fs/promises"
+import {
+ createServer,
+ request as httpRequest,
+ type IncomingHttpHeaders,
+ type ServerResponse,
+} from "node:http"
import type { AddressInfo } from "node:net"
-import express from "express"
-import { describe, expect, it, vi, type Mock } from "vitest"
+import path from "node:path"
+import { Readable } from "node:stream"
+import express, { type ErrorRequestHandler } from "express"
+import { describe, expect, it, onTestFinished, vi, type Mock } from "vitest"
+import { extractDistPackage } from "../../../../domains/build-artifacts/extract-dist-package"
import { createPreviewSession } from "../../../../domains/build-preview/create-preview-session"
import { createSharePreviewSession } from "../../../../domains/public-sharing/create-share-preview-session"
+import { createZipArchive } from "../../../../test/create-zip-archive"
import type { BuildPreviewRouteDependencies } from "../../types"
import { get } from "."
@@ -16,10 +27,20 @@ interface RouteResponse {
body: Buffer
}
+function deferred() {
+ let resolve!: (value: T) => void
+ const promise = new Promise((settle) => {
+ resolve = settle
+ })
+ return { promise, resolve }
+}
+
async function request(
origin: string,
path: string,
headers: Record,
+ consume?: (chunk: Buffer) => void,
+ signal?: AbortSignal,
): Promise {
return new Promise((resolve, reject) => {
const url = new URL(origin)
@@ -30,10 +51,14 @@ async function request(
path,
method: "GET",
headers,
+ signal,
},
(response) => {
const chunks: Buffer[] = []
- response.on("data", (chunk: Buffer) => chunks.push(chunk))
+ response.on("data", (chunk: Buffer) => {
+ if (consume) consume(chunk)
+ else chunks.push(chunk)
+ })
response.on("end", () =>
resolve({
status: response.statusCode ?? 0,
@@ -61,7 +86,9 @@ async function withServeRoute(
>
restoreSession: Mock>
shareCookie: string
+ templateCookie: string
}) => Promise,
+ observeResponse?: (response: ServerResponse, requestPath: string) => void,
) {
const key = randomBytes(32)
const buildId = randomUUID()
@@ -99,12 +126,33 @@ async function withServeRoute(
buildId,
startPage: "/",
artifactPath: "dist/index.html",
+ templateRepository: { id: randomUUID(), local: true },
}),
findArtifact,
readAvailableShareLink,
restoreSession,
}
- app.get("/{*path}", get(dependencies))
+ app.use((request, response, next) => {
+ observeResponse?.(response, request.path)
+ next()
+ })
+ app.get(
+ "/{*path}",
+ get(dependencies, () => Promise.resolve(true)),
+ )
+ function handleError(
+ error: unknown,
+ _request: Parameters[1],
+ response: Parameters[2],
+ next: Parameters[3],
+ ) {
+ if (response.headersSent) {
+ next(error)
+ return
+ }
+ response.status(500).end()
+ }
+ app.use(handleError)
const session = createPreviewSession(
buildId,
"a".repeat(64),
@@ -119,6 +167,13 @@ async function withServeRoute(
},
key,
)
+ const templateSession = createPreviewSession(
+ buildId,
+ "b".repeat(64),
+ new Date(Date.now() + 15 * 60_000),
+ key,
+ true,
+ )
try {
await operation({
origin: `http://127.0.0.1:${port}`,
@@ -130,6 +185,7 @@ async function withServeRoute(
readAvailableShareLink,
restoreSession,
shareCookie: `replica_share_preview=${encodeURIComponent(shareSession)}`,
+ templateCookie: `replica_preview=${encodeURIComponent(templateSession)}`,
})
} finally {
await new Promise((resolve, reject) => {
@@ -248,6 +304,326 @@ describe("get()", () => {
})
})
+ it("serves a packaged 100 MiB asset through ZIP import and immutable Azure artifact storage", async () => {
+ const root = await mkdtemp(path.resolve(".preview-static-file-test-"))
+ onTestFinished(() => rm(root, { recursive: true, force: true }))
+ const maximumBytes = 100 * 1024 * 1024
+ await mkdir(path.join(root, "dist/assets"), { recursive: true })
+ await writeFile(path.join(root, "dist/index.html"), "")
+ const assetPath = path.join(root, "dist/assets/data.bin")
+ const asset = await open(assetPath, "w")
+ try {
+ await asset.truncate(maximumBytes)
+ await asset.write(Buffer.from("first"), 0, 5, 0)
+ await asset.write(Buffer.from("last"), 0, 4, maximumBytes - 4)
+ } finally {
+ await asset.close()
+ }
+ const sourceDigest = createHash("sha256")
+ const source = createReadStream(assetPath) as AsyncIterable
+ for await (const chunk of source) sourceDigest.update(chunk)
+ const expectedDigest = sourceDigest.digest("hex")
+ const packageModule = new URL(
+ "../../../../../../actions/build/scripts/create-dist-package/index.mjs",
+ import.meta.url,
+ ).href
+ const { createDistPackage } = (await import(packageModule)) as {
+ createDistPackage: (root: string, configRoot: string, output: string) => Promise
+ }
+ const output = path.join(root, "package")
+ await createDistPackage(root, root, output)
+ const archive = createZipArchive(
+ await Promise.all(
+ ["dist/index.html", "dist/assets/data.bin"].map(async (name) => ({
+ name,
+ bytes: await readFile(path.join(output, name)),
+ })),
+ ),
+ )
+ const imported = extractDistPackage(archive)
+ const importedAsset = imported.files.find((file) => file.path === "dist/assets/data.bin")!
+ const blobs = new Map()
+ vi.doMock("@azure/identity", () => ({ ManagedIdentityCredential: class {} }))
+ vi.doMock("@azure/storage-blob", () => ({
+ BlobServiceClient: class {
+ getContainerClient() {
+ return {
+ getBlockBlobClient(key: string) {
+ return {
+ uploadData(bytes: Uint8Array, options: { conditions: { ifNoneMatch: string } }) {
+ if (options.conditions.ifNoneMatch === "*" && blobs.has(key)) {
+ return Promise.reject(
+ Object.assign(new Error("The immutable blob already exists."), {
+ details: { errorCode: "ConditionNotMet" },
+ }),
+ )
+ }
+ blobs.set(key, bytes)
+ return Promise.resolve()
+ },
+ download() {
+ const bytes = blobs.get(key)!
+ return Promise.resolve({
+ contentLength: bytes.byteLength,
+ readableStreamBody: Readable.from(
+ (function* () {
+ for (let offset = 0; offset < bytes.byteLength; offset += 1024 * 1024) {
+ yield bytes.subarray(offset, offset + 1024 * 1024)
+ }
+ })(),
+ ),
+ })
+ },
+ }
+ },
+ }
+ }
+ },
+ }))
+ onTestFinished(() => {
+ vi.doUnmock("@azure/identity")
+ vi.doUnmock("@azure/storage-blob")
+ })
+ const { createArtifactStorage } =
+ await import("../../../../domains/artifacts/create-artifact-storage")
+ const storage = await createArtifactStorage({
+ provider: "azure-blob",
+ accountUrl: "https://example.blob.core.windows.net",
+ container: "artifacts",
+ })
+ expect(await storage.write("asset-key", importedAsset.bytes)).toEqual({ status: "ok" })
+ expect(await storage.write("asset-key", Uint8Array.of(9))).toEqual({
+ status: "already-exists",
+ })
+
+ await withServeRoute(async ({ origin, buildId, cookie, dependencies, findArtifact }) => {
+ dependencies.storage = storage
+ findArtifact.mockImplementation((_buildId, artifactPath) =>
+ Promise.resolve(
+ artifactPath === "dist/assets/data.bin"
+ ? { storageKey: "asset-key", byteLength: importedAsset.bytes.byteLength }
+ : null,
+ ),
+ )
+ let receivedBytes = 0
+ const digest = createHash("sha256")
+
+ const response = await request(
+ origin,
+ "/assets/data.bin",
+ requestHeaders(buildId, origin, cookie),
+ (chunk) => {
+ receivedBytes += chunk.byteLength
+ digest.update(chunk)
+ },
+ )
+
+ expect(response.status).toBe(200)
+ expect(response.headers["content-length"]).toBe(String(maximumBytes))
+ expect(response.headers["content-type"]).toBe("application/octet-stream")
+ expect(response.headers["cache-control"]).toBe("private, max-age=31536000, immutable")
+ expect(receivedBytes).toBe(maximumBytes)
+ expect(digest.digest("hex")).toBe(expectedDigest)
+ })
+ }, 15_000)
+
+ it("shares the source-byte limit across private, public, and template requests on different Build servers", async () => {
+ await withServeRoute(async (privateRoute) => {
+ await withServeRoute(async (publicRoute) => {
+ const readStarted = deferred()
+ const stored =
+ deferred>>()
+ privateRoute.findArtifact.mockResolvedValue({
+ storageKey: "private-asset",
+ byteLength: 100 * 1024 * 1024,
+ })
+ privateRoute.read.mockImplementation(() => {
+ readStarted.resolve()
+ return stored.promise
+ })
+ publicRoute.findArtifact.mockImplementation((_buildId, artifactPath) =>
+ Promise.resolve(
+ artifactPath === "dist/asset.bin" || artifactPath === "templates.json"
+ ? { storageKey: "public-asset", byteLength: 1 }
+ : null,
+ ),
+ )
+ publicRoute.read.mockResolvedValue({ status: "ok", bytes: Uint8Array.of(7) })
+ const first = request(
+ privateRoute.origin,
+ "/asset.bin",
+ requestHeaders(privateRoute.buildId, privateRoute.origin, privateRoute.cookie),
+ )
+
+ try {
+ await readStarted.promise
+ const blocked = await request(
+ publicRoute.origin,
+ "/asset.bin",
+ requestHeaders(publicRoute.buildId, publicRoute.origin, publicRoute.shareCookie),
+ )
+ const blockedTemplate = await request(publicRoute.origin, "/release/v2.0", {
+ ...requestHeaders(publicRoute.buildId, publicRoute.origin, publicRoute.templateCookie),
+ accept: "text/html",
+ })
+
+ expect(blocked.status).toBe(503)
+ expect(blockedTemplate.status).toBe(503)
+ expect(publicRoute.read).not.toHaveBeenCalled()
+ } finally {
+ stored.resolve({ status: "ok", bytes: Uint8Array.of(1) })
+ expect((await first).status).toBe(200)
+ }
+
+ const recovered = await request(
+ publicRoute.origin,
+ "/asset.bin",
+ requestHeaders(publicRoute.buildId, publicRoute.origin, publicRoute.shareCookie),
+ )
+ expect(recovered.status).toBe(200)
+ expect(publicRoute.read).toHaveBeenCalledOnce()
+ })
+ })
+ })
+
+ it.each(["finish", "close"] as const)(
+ "holds source bytes after reading until the response emits %s",
+ async (completion) => {
+ const responsePending = deferred<{
+ response: ServerResponse
+ finish: () => void
+ }>()
+ await withServeRoute(
+ async ({ origin, buildId, cookie, findArtifact, read }) => {
+ findArtifact.mockResolvedValue({
+ storageKey: "asset-key",
+ byteLength: 100 * 1024 * 1024,
+ })
+ read.mockResolvedValue({ status: "ok", bytes: Uint8Array.of(7) })
+ const headers = requestHeaders(buildId, origin, cookie)
+ const first = request(origin, "/slow.bin", headers).catch((error: unknown) => error)
+ const pending = await responsePending.promise
+
+ try {
+ const blocked = await request(origin, "/asset.bin", headers)
+
+ expect(blocked.status).toBe(503)
+ expect(read).toHaveBeenCalledOnce()
+ } finally {
+ const completed = once(pending.response, completion)
+ if (completion === "finish") pending.finish()
+ else pending.response.destroy()
+ await completed
+ await first
+ }
+
+ expect((await request(origin, "/asset.bin", headers)).status).toBe(200)
+ expect(read).toHaveBeenCalledTimes(2)
+ },
+ (response, requestPath) => {
+ if (requestPath !== "/slow.bin") return
+ const end = response.end.bind(response)
+ vi.spyOn(response, "end").mockImplementation(() => {
+ responsePending.resolve({ response, finish: () => end(Uint8Array.of(7)) })
+ return response
+ })
+ },
+ )
+ },
+ )
+
+ it.each(["storage read", "authorization recheck"] as const)(
+ "keeps a disconnected request reserved until its pending %s settles",
+ async (pendingOperation) => {
+ const pendingStarted = deferred()
+ const proceed = deferred()
+ const disconnected = deferred()
+ await withServeRoute(
+ async ({ origin, buildId, cookie, findArtifact, read, restoreSession }) => {
+ findArtifact.mockResolvedValue({
+ storageKey: "asset-key",
+ byteLength: 100 * 1024 * 1024,
+ })
+ read.mockResolvedValue({ status: "ok", bytes: Uint8Array.of(7) })
+ if (pendingOperation === "storage read") {
+ read.mockImplementationOnce(async () => {
+ pendingStarted.resolve()
+ await proceed.promise
+ return { status: "ok", bytes: Uint8Array.of(7) }
+ })
+ }
+ if (pendingOperation === "authorization recheck") {
+ restoreSession
+ .mockResolvedValueOnce({ userId: randomUUID() })
+ .mockImplementationOnce(async () => {
+ pendingStarted.resolve()
+ await proceed.promise
+ return { userId: randomUUID() }
+ })
+ }
+ const headers = requestHeaders(buildId, origin, cookie)
+ const abort = new AbortController()
+ const first = request(origin, "/pending.bin", headers, undefined, abort.signal).catch(
+ (error: unknown) => error,
+ )
+
+ try {
+ await pendingStarted.promise
+ abort.abort()
+ await disconnected.promise
+ const blocked = await request(origin, "/asset.bin", headers)
+
+ expect(blocked.status).toBe(503)
+ expect(read).toHaveBeenCalledOnce()
+ } finally {
+ proceed.resolve()
+ abort.abort()
+ await first
+ }
+
+ expect((await request(origin, "/asset.bin", headers)).status).toBe(200)
+ expect(read).toHaveBeenCalledTimes(2)
+ },
+ (response, requestPath) => {
+ if (requestPath === "/pending.bin") {
+ response.once("close", () => disconnected.resolve())
+ }
+ },
+ )
+ },
+ )
+
+ it("releases the template manifest reservation before reading the SPA entry point", async () => {
+ await withServeRoute(async ({ origin, buildId, cookie, findArtifact, read }) => {
+ const manifest = new TextEncoder().encode(
+ JSON.stringify({ templates: [{ path: "/release/v2.0" }] }),
+ )
+ const html = new TextEncoder().encode("Release notes")
+ findArtifact.mockImplementation((_buildId, artifactPath) =>
+ Promise.resolve(
+ artifactPath === "templates.json"
+ ? { storageKey: "manifest-key", byteLength: 100 * 1024 * 1024 }
+ : artifactPath === "dist/index.html"
+ ? { storageKey: "index-key", byteLength: html.byteLength }
+ : null,
+ ),
+ )
+ read.mockImplementation((key) =>
+ Promise.resolve({ status: "ok", bytes: key === "manifest-key" ? manifest : html }),
+ )
+
+ const response = await request(origin, "/release/v2.0", {
+ ...requestHeaders(buildId, origin, cookie),
+ accept: "text/html",
+ })
+
+ expect(response.status).toBe(200)
+ expect(response.body.toString()).toContain("Release notes")
+ expect(read).toHaveBeenNthCalledWith(1, "manifest-key")
+ expect(read).toHaveBeenNthCalledWith(2, "index-key")
+ })
+ })
+
it("serves the generated hosted bridge only to an authorized preview session", async () => {
await withServeRoute(async ({ origin, buildId, cookie, dependencies, findArtifact, read }) => {
const response = await request(
@@ -400,19 +776,26 @@ describe("get()", () => {
)
})
- it("distinguishes missing artifact bytes from unavailable storage", async () => {
- await withServeRoute(async ({ origin, buildId, cookie, findArtifact, read }) => {
- findArtifact.mockResolvedValue({
- storageKey: "asset-key",
- byteLength: 1,
- })
- read
- .mockResolvedValueOnce({ status: "not-found" })
- .mockResolvedValueOnce({ status: "unavailable" })
- const headers = requestHeaders(buildId, origin, cookie)
+ it.each(["not-found", "unavailable", "error"] as const)(
+ "releases the source-byte reservation after storage returns %s",
+ async (failure) => {
+ await withServeRoute(async ({ origin, buildId, cookie, findArtifact, read }) => {
+ findArtifact.mockResolvedValue({
+ storageKey: "asset-key",
+ byteLength: 100 * 1024 * 1024,
+ })
+ read.mockResolvedValue({ status: "ok", bytes: Uint8Array.of(7) })
+ if (failure === "error") read.mockRejectedValueOnce(new Error("Storage read failed"))
+ else read.mockResolvedValueOnce({ status: failure })
+ const headers = requestHeaders(buildId, origin, cookie)
- expect((await request(origin, "/asset.bin", headers)).status).toBe(404)
- expect((await request(origin, "/asset.bin", headers)).status).toBe(503)
- })
- })
+ const failed = await request(origin, "/asset.bin", headers)
+ const recovered = await request(origin, "/asset.bin", headers)
+
+ expect(failed.status).toBe(failure === "not-found" ? 404 : failure === "error" ? 500 : 503)
+ expect(recovered.status).toBe(200)
+ expect(read).toHaveBeenCalledTimes(2)
+ })
+ },
+ )
})
diff --git a/hub/server/routes/build-preview/serve/get/index.ts b/hub/server/routes/build-preview/serve/get/index.ts
index 4e8990e8e..41555e185 100644
--- a/hub/server/routes/build-preview/serve/get/index.ts
+++ b/hub/server/routes/build-preview/serve/get/index.ts
@@ -4,6 +4,7 @@ import { readBuildPreviewFile } from "../../../../domains/build-preview/read-bui
import { readPreviewBuildId } from "../../../../domains/build-preview/read-preview-build-id/index.ts"
import { readPreviewSession } from "../../../../domains/build-preview/read-preview-session/index.ts"
import { readTemplateScreenshotPath } from "../../../../domains/build-preview/template-screenshot-path/index.ts"
+import { reserveBuildPreviewBytes } from "../../../../domains/build-preview/reserve-build-preview-bytes/index.ts"
import { readSharePreviewSession } from "../../../../domains/public-sharing/read-share-preview-session/index.ts"
import { readCookie } from "../../../../integrations/express/read-cookie/index.ts"
import type { BuildPreviewRouteDependencies } from "../../types.ts"
@@ -58,6 +59,21 @@ export function get(
}
return async (request, response, next) => {
+ const reservations = new Set<() => void>()
+ let handlingRequest = true
+ let responseEnded = response.destroyed || response.writableFinished
+ function releaseReservations() {
+ if (handlingRequest || !responseEnded) return
+ response.off("finish", onResponseEnd)
+ response.off("close", onResponseEnd)
+ for (const release of reservations) release()
+ }
+ function onResponseEnd() {
+ responseEnded = true
+ releaseReservations()
+ }
+ response.once("finish", onResponseEnd)
+ response.once("close", onResponseEnd)
try {
const buildId = readPreviewBuildId(request.get("host"), dependencies.previewBaseOrigin)
if (!buildId) {
@@ -121,8 +137,23 @@ export function get(
distOnly: publicLink?.hosted,
requestPath: request.path,
},
- dependencies,
+ {
+ ...dependencies,
+ reserveBytes(byteLength) {
+ if (responseEnded) return null
+ const reservation = reserveBuildPreviewBytes(byteLength)
+ if (!reservation) return null
+ const release = reservation
+ function trackedRelease() {
+ reservations.delete(trackedRelease)
+ release()
+ }
+ reservations.add(trackedRelease)
+ return trackedRelease
+ },
+ },
)
+ if (response.destroyed) return
if (file.status !== "ok") {
response.status(file.status === "not-found" ? 404 : 503).end()
return
@@ -156,6 +187,9 @@ export function get(
response.end(file.bytes)
} catch (error) {
next(error)
+ } finally {
+ handlingRequest = false
+ releaseReservations()
}
}
}
diff --git a/hub/server/test/create-zip-archive/index.ts b/hub/server/test/create-zip-archive/index.ts
index 1fa1c9687..f452bb24f 100644
--- a/hub/server/test/create-zip-archive/index.ts
+++ b/hub/server/test/create-zip-archive/index.ts
@@ -8,14 +8,14 @@ export interface TestZipEntry {
mode?: number
}
-/** Builds a small ZIP archive for tests, with control over unsafe header fields. */
+/** Builds a ZIP archive for tests, with control over unsafe header fields. */
export function createZipArchive(entries: TestZipEntry[]): Buffer {
const locals: Buffer[] = []
const centrals: Buffer[] = []
let offset = 0
for (const entry of entries) {
const name = Buffer.from(entry.name)
- const bytes = Buffer.from(entry.bytes ?? "")
+ const bytes = Buffer.isBuffer(entry.bytes) ? entry.bytes : Buffer.from(entry.bytes ?? "")
const method = entry.method ?? 8
const data = method === 8 ? deflateRawSync(bytes) : bytes
const checksum = crc32(bytes)