Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Normalize multipart Content-Type; omit part header

Richie McIlroyfb75731fa0ac
diff --git a/apps/web/__tests__/unit/instant-recording-uploader.test.ts b/apps/web/__tests__/unit/instant-recording-uploader.test.ts
index 47b7a32b9..69eb92ba0 100644
--- a/apps/web/__tests__/unit/instant-recording-uploader.test.ts
+++ b/apps/web/__tests__/unit/instant-recording-uploader.test.ts
@@ -17,6 +17,7 @@ type UploadOutcome =
class MockXMLHttpRequest {
static outcomes: UploadOutcome[] = [];
static abortedCount = 0;
+ static recordedHeaders: Array<Map<string, string>> = [];
upload = {
onprogress: null as ((event: ProgressEvent<EventTarget>) => void) | null,
@@ -34,6 +35,7 @@ class MockXMLHttpRequest {
static setOutcomes(outcomes: UploadOutcome[]) {
MockXMLHttpRequest.outcomes = [...outcomes];
MockXMLHttpRequest.abortedCount = 0;
+ MockXMLHttpRequest.recordedHeaders = [];
}
open() {}
@@ -47,6 +49,8 @@ class MockXMLHttpRequest {
}
send(part: Blob) {
+ MockXMLHttpRequest.recordedHeaders.push(new Map(this.headers));
+
const outcome = MockXMLHttpRequest.outcomes.shift();
if (!outcome) {
throw new Error("Missing upload outcome");
@@ -191,6 +195,43 @@ describe("InstantRecordingUploader", () => {
status: "complete",
}),
]);
+ expect(MockXMLHttpRequest.recordedHeaders[0]?.has("content-type")).toBe(
+ false,
+ );
+ });
+
+ it("normalizes multipart initiate content type before creating the upload", async () => {
+ const fetchMock = vi.fn(
+ async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = input.toString();
+ const body = init?.body ? JSON.parse(init.body as string) : null;
+
+ if (url === "/api/upload/multipart/initiate") {
+ expect(body).toMatchObject({
+ videoId,
+ contentType: "video/webm",
+ subpath: "raw-upload.webm",
+ });
+ return makeJsonResponse({ uploadId: "upload-123" });
+ }
+
+ throw new Error(`Unexpected fetch call: ${url}`);
+ },
+ );
+
+ vi.stubGlobal("fetch", fetchMock);
+
+ const { initiateMultipartUpload } = await import(
+ "@/app/(org)/dashboard/caps/components/web-recorder-dialog/instant-mp4-uploader"
+ );
+
+ await expect(
+ initiateMultipartUpload({
+ videoId,
+ contentType: "video/webm;codecs=vp9,opus",
+ subpath: "raw-upload.webm",
+ }),
+ ).resolves.toBe("upload-123");
});
it("completes multipart uploads with parts ordered by part number", async () => {
diff --git a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/instant-mp4-uploader.ts b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/instant-mp4-uploader.ts
index 385728ec8..b9c356792 100644
--- a/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/instant-mp4-uploader.ts
+++ b/apps/web/app/(org)/dashboard/caps/components/web-recorder-dialog/instant-mp4-uploader.ts
@@ -81,6 +81,11 @@ const postJson = async <TResponse>(
return (await response.json()) as TResponse;
};
+const normalizeMultipartContentType = (mimeType: string) => {
+ const normalized = mimeType.split(";")[0]?.trim();
+ return normalized || "application/octet-stream";
+};
+
export const initiateMultipartUpload = async ({
videoId,
contentType,
@@ -92,7 +97,11 @@ export const initiateMultipartUpload = async ({
}) => {
const result = await postJson<{ uploadId: string }>(
"/api/upload/multipart/initiate",
- { videoId, contentType, subpath },
+ {
+ videoId,
+ contentType: normalizeMultipartContentType(contentType),
+ subpath,
+ },
);
if (!result.uploadId) {
@@ -573,9 +582,6 @@ export class InstantRecordingUploader {
xhr.open("PUT", url);
xhr.responseType = "text";
xhr.timeout = PART_UPLOAD_REQUEST_TIMEOUT_MS;
- if (this.mimeType) {
- xhr.setRequestHeader("Content-Type", this.mimeType);
- }
this.activeRequests.set(partNumber, xhr);
this.updateChunkState(partNumber, {