mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
Second pass over the suite, driven by the inventory in the gaps document. Nine agents wrote suites in parallel against private databases, then a tenth read all of it adversarially and five of its findings were fixed. unit + component 2076 -> 2079 (+888 over the round) api 647 -> 1015 e2e 18 -> 29 What was closed: - lib/route-access.ts, the page-level authorization layer, went from zero tests to 48. Every API route was guarded and none of the pages were. - The five media proxy routes now have a real 2xx beside every 403. The blocker was the positive control, solved by stubbing r2Client.send() and leaving lib/r2-media-proxy.ts itself real. - Every remaining server-side lib module: invitations, email verification, the upload tokens, the logger, request origin, the whole R2 and Bunny lifecycle, notifications and admin stats. - Six video-page hooks, and the chunking arithmetic extracted out of lib/client/r2-video-upload.ts as a pure module. - Five end-to-end flows: workspace members, bulk operations, the admin area, player interaction and failure recovery. Three things about the harness itself turned out to be wrong: - Two @/lib/r2 stubs in tests/setup/api.ts had the wrong return shape, so every route reaching finalizeR2VideoUpload silently took the "not a valid video" branch and no test noticed. - The auth matrix asserted only "not 2xx", which two entries satisfied without their guard existing. It now requires 401 or 403, which makes both load-bearing, and all 60 routes pass the stricter form. - Both admin API routes had no positive control anywhere: replacing their guard with an unconditional refusal left the entire suite green. Found by the adversarial review, now covered. Process: - bun run test:mutation runs StrykerJS over the authorization and validation modules. Diagnostic, not a gate, weekly in CI rather than on a push. - playwright.config.ts gains an opt-in webkit project for the player spec. - AGENTS.md now requires a batch of new tests to be reviewed by somebody who did not write them. Only two production files change, both deliberate: lib/auth.ts loses a verbatim copy of its own permission formulas, and lib/client/r2-video-upload.ts calls the extracted arithmetic. No behaviour change in either.
329 lines
8.8 KiB
TypeScript
329 lines
8.8 KiB
TypeScript
import { captureVideoThumbnail } from '@/lib/client/video-thumbnail';
|
|
import {
|
|
getMultipartProgressPercent,
|
|
getPartByteRange,
|
|
getRetryDelayMs,
|
|
getUploadProgressPercent,
|
|
PART_RETRY_DELAYS_MS,
|
|
} from '@/lib/client/upload-chunking';
|
|
|
|
export type R2MultipartPart = { partNumber: number; url: string };
|
|
|
|
export type R2MultipartInit = {
|
|
uploadId: string;
|
|
partSizeBytes: number;
|
|
parts: R2MultipartPart[];
|
|
};
|
|
|
|
export type R2VideoInitResponse = {
|
|
presignedPutUrl: string;
|
|
objectKey: string;
|
|
proxyUrl: string;
|
|
uploadToken: string;
|
|
reservationId: string | null;
|
|
contentType: string;
|
|
thumbnailPresignedPutUrl: string;
|
|
thumbnailObjectKey: string;
|
|
thumbnailProxyUrl: string;
|
|
multipart: R2MultipartInit | null;
|
|
};
|
|
|
|
export type R2VideoUploadResult = R2VideoInitResponse & {
|
|
duration: number | null;
|
|
thumbnailUrl: string | null;
|
|
};
|
|
|
|
type UploadProgressHandler = (progress: number) => void;
|
|
|
|
function uploadBytesWithProgress(
|
|
url: string,
|
|
body: Blob | File,
|
|
contentType: string,
|
|
onProgress?: UploadProgressHandler
|
|
): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('PUT', url);
|
|
xhr.setRequestHeader('Content-Type', contentType);
|
|
|
|
xhr.upload.onprogress = (event) => {
|
|
if (!onProgress || !event.lengthComputable) return;
|
|
onProgress(getUploadProgressPercent(event.loaded, event.total));
|
|
};
|
|
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
resolve();
|
|
return;
|
|
}
|
|
reject(new Error(`Upload failed with status ${xhr.status}`));
|
|
};
|
|
|
|
xhr.onerror = () => {
|
|
reject(
|
|
new Error(
|
|
'Network error during upload. If you use direct S3/R2 uploads, configure bucket CORS to allow PUT from this site origin.'
|
|
)
|
|
);
|
|
};
|
|
xhr.onabort = () => reject(new Error('Upload aborted'));
|
|
|
|
xhr.send(body);
|
|
});
|
|
}
|
|
|
|
function uploadPartWithProgress(
|
|
url: string,
|
|
body: Blob,
|
|
onPartProgress?: (loadedBytes: number) => void
|
|
): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open('PUT', url);
|
|
// Intentionally no Content-Type header: it is not part of the presigned
|
|
// UploadPart signature, and the part body is raw bytes.
|
|
|
|
xhr.upload.onprogress = (event) => {
|
|
if (!onPartProgress || !event.lengthComputable) return;
|
|
onPartProgress(event.loaded);
|
|
};
|
|
|
|
xhr.onload = () => {
|
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
const etag = xhr.getResponseHeader('ETag');
|
|
if (!etag) {
|
|
reject(
|
|
new Error(
|
|
'Upload response missing ETag header. Configure bucket CORS to expose the ETag header.'
|
|
)
|
|
);
|
|
return;
|
|
}
|
|
resolve(etag);
|
|
return;
|
|
}
|
|
reject(new Error(`Chunk upload failed with status ${xhr.status}`));
|
|
};
|
|
|
|
xhr.onerror = () => {
|
|
reject(
|
|
new Error(
|
|
'Network error during upload. If you use direct S3/R2 uploads, configure bucket CORS to allow PUT from this site origin.'
|
|
)
|
|
);
|
|
};
|
|
xhr.onabort = () => reject(new Error('Upload aborted'));
|
|
|
|
xhr.send(body);
|
|
});
|
|
}
|
|
|
|
async function withRetry<T>(fn: () => Promise<T>, delays: number[]): Promise<T> {
|
|
let lastError: unknown;
|
|
for (let attempt = 0; attempt < delays.length; attempt += 1) {
|
|
if (attempt > 0) {
|
|
await new Promise((resolve) => setTimeout(resolve, getRetryDelayMs(attempt, delays)));
|
|
}
|
|
try {
|
|
return await fn();
|
|
} catch (error) {
|
|
lastError = error;
|
|
}
|
|
}
|
|
throw lastError instanceof Error ? lastError : new Error('Upload failed after retries');
|
|
}
|
|
|
|
async function completeMultipartUpload(
|
|
projectId: string,
|
|
objectKey: string,
|
|
uploadToken: string,
|
|
parts: Array<{ partNumber: number; etag: string }>
|
|
): Promise<void> {
|
|
const res = await fetch(`/api/projects/${projectId}/videos/r2-complete`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ objectKey, uploadToken, parts }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const payload = (await res.json().catch(() => null)) as { error?: string } | null;
|
|
throw new Error(payload?.error || 'Failed to complete multipart upload');
|
|
}
|
|
}
|
|
|
|
async function uploadVideoMultipart(
|
|
projectId: string,
|
|
file: File,
|
|
multipart: R2MultipartInit,
|
|
objectKey: string,
|
|
uploadToken: string,
|
|
onProgress?: UploadProgressHandler
|
|
): Promise<void> {
|
|
const totalBytes = file.size;
|
|
const partSize = multipart.partSizeBytes;
|
|
const loadedPerPart = new Array<number>(multipart.parts.length).fill(0);
|
|
|
|
const reportProgress = () => {
|
|
if (!onProgress) return;
|
|
onProgress(getMultipartProgressPercent(loadedPerPart, totalBytes));
|
|
};
|
|
|
|
const completedParts: Array<{ partNumber: number; etag: string }> = [];
|
|
|
|
for (let index = 0; index < multipart.parts.length; index += 1) {
|
|
const part = multipart.parts[index];
|
|
const { start, end } = getPartByteRange(part.partNumber, partSize, totalBytes);
|
|
const blob = file.slice(start, end);
|
|
|
|
const etag = await withRetry(
|
|
() =>
|
|
uploadPartWithProgress(part.url, blob, (loadedBytes) => {
|
|
loadedPerPart[index] = loadedBytes;
|
|
reportProgress();
|
|
}),
|
|
PART_RETRY_DELAYS_MS
|
|
);
|
|
|
|
loadedPerPart[index] = end - start;
|
|
reportProgress();
|
|
completedParts.push({ partNumber: part.partNumber, etag });
|
|
}
|
|
|
|
await completeMultipartUpload(projectId, objectKey, uploadToken, completedParts);
|
|
}
|
|
|
|
async function readVideoDuration(file: File): Promise<number | null> {
|
|
return new Promise((resolve) => {
|
|
const objectUrl = URL.createObjectURL(file);
|
|
const video = document.createElement('video');
|
|
video.preload = 'metadata';
|
|
|
|
const cleanup = () => {
|
|
video.removeAttribute('src');
|
|
video.load();
|
|
URL.revokeObjectURL(objectUrl);
|
|
};
|
|
|
|
video.onloadedmetadata = () => {
|
|
const duration =
|
|
Number.isFinite(video.duration) && video.duration > 0 ? Math.round(video.duration) : null;
|
|
cleanup();
|
|
resolve(duration);
|
|
};
|
|
|
|
video.onerror = () => {
|
|
cleanup();
|
|
resolve(null);
|
|
};
|
|
|
|
video.src = objectUrl;
|
|
});
|
|
}
|
|
|
|
export async function initR2VideoUpload(
|
|
projectId: string,
|
|
file: File
|
|
): Promise<R2VideoInitResponse> {
|
|
const initRes = await fetch(`/api/projects/${projectId}/videos/r2-init`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
fileName: file.name,
|
|
contentType: file.type,
|
|
sizeBytes: file.size,
|
|
}),
|
|
});
|
|
|
|
const initPayload = (await initRes.json().catch(() => null)) as {
|
|
data?: R2VideoInitResponse;
|
|
error?: string;
|
|
} | null;
|
|
if (!initRes.ok || !initPayload?.data) {
|
|
throw new Error(initPayload?.error || 'Failed to initialize video upload');
|
|
}
|
|
|
|
return initPayload.data;
|
|
}
|
|
|
|
export async function cleanupPendingR2VideoUpload(
|
|
projectId: string,
|
|
input: {
|
|
objectKey: string;
|
|
uploadToken: string;
|
|
reservationId: string | null;
|
|
thumbnailObjectKey?: string | null;
|
|
},
|
|
keepalive = false
|
|
): Promise<void> {
|
|
try {
|
|
await fetch(`/api/projects/${projectId}/videos/r2-init`, {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
objectKey: input.objectKey,
|
|
uploadToken: input.uploadToken,
|
|
reservationId: input.reservationId,
|
|
thumbnailObjectKey: input.thumbnailObjectKey ?? undefined,
|
|
}),
|
|
keepalive,
|
|
});
|
|
} catch (error) {
|
|
console.error('Failed to cleanup pending R2 video upload:', error);
|
|
}
|
|
}
|
|
|
|
export async function uploadVideoToR2(
|
|
projectId: string,
|
|
file: File,
|
|
options?: { onProgress?: UploadProgressHandler }
|
|
): Promise<R2VideoUploadResult> {
|
|
const init = await initR2VideoUpload(projectId, file);
|
|
|
|
const cleanupInput = {
|
|
objectKey: init.objectKey,
|
|
uploadToken: init.uploadToken,
|
|
reservationId: init.reservationId,
|
|
thumbnailObjectKey: init.thumbnailObjectKey,
|
|
};
|
|
|
|
try {
|
|
if (init.multipart) {
|
|
await uploadVideoMultipart(
|
|
projectId,
|
|
file,
|
|
init.multipart,
|
|
init.objectKey,
|
|
init.uploadToken,
|
|
options?.onProgress
|
|
);
|
|
} else {
|
|
await uploadBytesWithProgress(
|
|
init.presignedPutUrl,
|
|
file,
|
|
init.contentType,
|
|
options?.onProgress
|
|
);
|
|
}
|
|
} catch (error) {
|
|
await cleanupPendingR2VideoUpload(projectId, cleanupInput);
|
|
throw error;
|
|
}
|
|
|
|
const [duration, thumbnailBlob] = await Promise.all([
|
|
readVideoDuration(file),
|
|
captureVideoThumbnail(file),
|
|
]);
|
|
|
|
let thumbnailUrl: string | null = null;
|
|
if (thumbnailBlob) {
|
|
try {
|
|
await uploadBytesWithProgress(init.thumbnailPresignedPutUrl, thumbnailBlob, 'image/jpeg');
|
|
thumbnailUrl = init.thumbnailProxyUrl;
|
|
} catch (error) {
|
|
console.warn('Failed to upload video thumbnail:', error);
|
|
}
|
|
}
|
|
|
|
return { ...init, duration, thumbnailUrl };
|
|
}
|