🎥 Make videos programmatically with React
`@remotion/vercel`: Fix nested bundle upload and surface sandbox API errors
JonnyBurger11f03eb9cf53
diff --git a/packages/vercel/src/add-bundle-to-sandbox.ts b/packages/vercel/src/add-bundle-to-sandbox.tsindex e88f01e349..5a7fa8c5ea 100644--- a/packages/vercel/src/add-bundle-to-sandbox.ts+++ b/packages/vercel/src/add-bundle-to-sandbox.ts@@ -1,12 +1,22 @@import {readdir, readFile} from 'fs/promises';import path from 'path';import type {Sandbox} from '@vercel/sandbox';-import {REMOTION_SANDBOX_BUNDLE_DIR} from './internals/add-bundle';+import {+ getAncestorDirectories,+ REMOTION_SANDBOX_BUNDLE_DIR,+ toPosixPath,+ toSandboxBundlePath,+} from './internals/add-bundle';+import {rethrowSandboxError} from './internals/format-sandbox-error';++const resolveBundleDirectory = (bundleDir: string): string => {+ return path.isAbsolute(bundleDir) ? bundleDir : path.resolve(bundleDir);+};async function getRemotionBundleFiles(bundleDir: string,): Promise<{path: string; content: Buffer}[]> {- const fullBundleDir = path.join(process.cwd(), bundleDir);+ const fullBundleDir = resolveBundleDirectory(bundleDir);const files: {path: string; content: Buffer}[] = [];@@ -19,7 +29,7 @@ async function getRemotionBundleFiles(await readDirRecursive(fullPath, relativePath);} else {const content = await readFile(fullPath);- files.push({path: relativePath, content});+ files.push({path: toPosixPath(relativePath), content});}}}@@ -28,6 +38,20 @@ async function getRemotionBundleFiles(return files;}+const collectBundleDirectories = (+ bundleFiles: {path: string; content: Buffer}[],+): string[] => {+ const dirs = new Set<string>();++ for (const file of bundleFiles) {+ for (const dir of getAncestorDirectories(file.path)) {+ dirs.add(dir);+ }+ }++ return Array.from(dirs).sort();+};+export async function addBundleToSandbox({sandbox,bundleDir,@@ -36,23 +60,32 @@ export async function addBundleToSandbox({bundleDir: string;}): Promise<void> {const bundleFiles = await getRemotionBundleFiles(bundleDir);+ const directories = collectBundleDirectories(bundleFiles);- const dirs = new Set<string>();- for (const file of bundleFiles) {- const dir = path.dirname(file.path);- if (dir && dir !== '.') {- dirs.add(dir);+ for (const dir of directories) {+ const sandboxPath = toSandboxBundlePath(dir);+ try {+ await sandbox.mkDir(sandboxPath);+ } catch (error) {+ rethrowSandboxError({+ error,+ operation: `create directory "${dir}"`,+ sandboxPath,+ });}}- for (const dir of Array.from(dirs).sort()) {- await sandbox.mkDir(REMOTION_SANDBOX_BUNDLE_DIR + '/' + dir);+ try {+ await sandbox.writeFiles(+ bundleFiles.map((file) => ({+ path: toSandboxBundlePath(file.path),+ content: file.content,+ })),+ );+ } catch (error) {+ rethrowSandboxError({+ error,+ operation: `upload ${bundleFiles.length} bundle file(s) to "${REMOTION_SANDBOX_BUNDLE_DIR}"`,+ });}-- await sandbox.writeFiles(- bundleFiles.map((file) => ({- path: REMOTION_SANDBOX_BUNDLE_DIR + '/' + file.path,- content: file.content,- })),- );}diff --git a/packages/vercel/src/internals/add-bundle.ts b/packages/vercel/src/internals/add-bundle.tsindex 789b0187b9..bb202078bd 100644--- a/packages/vercel/src/internals/add-bundle.ts+++ b/packages/vercel/src/internals/add-bundle.ts@@ -1 +1,21 @@export const REMOTION_SANDBOX_BUNDLE_DIR = 'remotion-bundle';++export const toPosixPath = (filePath: string): string => {+ return filePath.split(/[/\\]/).join('/');+};++export const getAncestorDirectories = (relativePath: string): string[] => {+ const normalized = toPosixPath(relativePath);+ const parts = normalized.split('/').filter(Boolean);+ const dirs: string[] = [];++ for (let i = 0; i < parts.length - 1; i++) {+ dirs.push(parts.slice(0, i + 1).join('/'));+ }++ return dirs;+};++export const toSandboxBundlePath = (relativePath: string): string => {+ return `${REMOTION_SANDBOX_BUNDLE_DIR}/${toPosixPath(relativePath)}`;+};diff --git a/packages/vercel/src/internals/format-sandbox-error.ts b/packages/vercel/src/internals/format-sandbox-error.tsnew file mode 100644index 0000000000..def28c5c0a--- /dev/null+++ b/packages/vercel/src/internals/format-sandbox-error.ts@@ -0,0 +1,36 @@+import {APIError} from '@vercel/sandbox';++export const formatSandboxError = (error: unknown): string => {+ if (error instanceof APIError) {+ const parts = [error.message];+ if (error.json !== undefined) {+ parts.push(`Response JSON: ${JSON.stringify(error.json)}`);+ } else if (error.text) {+ parts.push(`Response body: ${error.text}`);+ }++ return parts.join('\n');+ }++ if (error instanceof Error) {+ return error.message;+ }++ return String(error);+};++export const rethrowSandboxError = ({+ error,+ operation,+ sandboxPath,+}: {+ error: unknown;+ operation: string;+ sandboxPath?: string;+}): never => {+ const pathSuffix = sandboxPath ? ` at "${sandboxPath}"` : '';+ throw new Error(+ `Failed to ${operation} in Vercel sandbox${pathSuffix}: ${formatSandboxError(error)}`,+ {cause: error instanceof Error ? error : undefined},+ );+};