Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(recorder-core): add local recording backup utilities

Richie McIlroy9d86f5998f6f
diff --git a/packages/recorder-core/__tests__/local-recording-backup.test.ts b/packages/recorder-core/__tests__/local-recording-backup.test.ts
new file mode 100644
index 000000000..0eda90481
--- /dev/null
+++ b/packages/recorder-core/__tests__/local-recording-backup.test.ts
@@ -0,0 +1,80 @@
+import {
+ appendLocalRecordingChunk,
+ finalizeLocalRecording,
+ initialLocalRecordingState,
+} from "@cap/recorder-core/local-recording-backup";
+import { describe, expect, it } from "vitest";
+
+const makeBlob = (size: number, type = "video/webm;codecs=vp9,opus") =>
+ new Blob([new Uint8Array(size)], { type });
+
+describe("local recording backup", () => {
+ it("retains a full local copy when configured for capped streaming backup", () => {
+ const firstChunk = makeBlob(3);
+ const secondChunk = makeBlob(4);
+
+ const afterFirstChunk = appendLocalRecordingChunk(
+ initialLocalRecordingState(),
+ firstChunk,
+ { mode: "capped", maxBytes: 10 },
+ );
+ const afterSecondChunk = appendLocalRecordingChunk(
+ afterFirstChunk,
+ secondChunk,
+ { mode: "capped", maxBytes: 10 },
+ );
+
+ const blob = finalizeLocalRecording(afterSecondChunk);
+
+ expect(afterSecondChunk.overflowed).toBe(false);
+ expect(afterSecondChunk.retainedBytes).toBe(7);
+ expect(blob?.size).toBe(7);
+ expect(blob?.type).toBe("video/webm;codecs=vp9,opus");
+ });
+
+ it("drops the backup copy after the capped limit is exceeded", () => {
+ const firstChunk = makeBlob(6);
+ const secondChunk = makeBlob(5);
+ const thirdChunk = makeBlob(3);
+
+ const afterFirstChunk = appendLocalRecordingChunk(
+ initialLocalRecordingState(),
+ firstChunk,
+ { mode: "capped", maxBytes: 10 },
+ );
+ const afterSecondChunk = appendLocalRecordingChunk(
+ afterFirstChunk,
+ secondChunk,
+ { mode: "capped", maxBytes: 10 },
+ );
+ const afterThirdChunk = appendLocalRecordingChunk(
+ afterSecondChunk,
+ thirdChunk,
+ { mode: "capped", maxBytes: 10 },
+ );
+
+ const blob = finalizeLocalRecording(afterThirdChunk);
+
+ expect(afterSecondChunk.overflowed).toBe(true);
+ expect(afterSecondChunk.retainedBytes).toBe(0);
+ expect(afterSecondChunk.chunks).toHaveLength(0);
+ expect(afterThirdChunk.overflowed).toBe(true);
+ expect(afterThirdChunk.retainedBytes).toBe(0);
+ expect(afterThirdChunk.chunks).toHaveLength(0);
+ expect(blob).toBeNull();
+ });
+
+ it("keeps the uncapped buffered fallback intact", () => {
+ const state = appendLocalRecordingChunk(
+ initialLocalRecordingState(),
+ makeBlob(12, "video/mp4"),
+ { mode: "full" },
+ );
+
+ const blob = finalizeLocalRecording(state);
+
+ expect(state.overflowed).toBe(false);
+ expect(blob?.size).toBe(12);
+ expect(blob?.type).toBe("video/mp4");
+ });
+});
diff --git a/packages/recorder-core/src/local-recording-backup.ts b/packages/recorder-core/src/local-recording-backup.ts
new file mode 100644
index 000000000..13382ea64
--- /dev/null
+++ b/packages/recorder-core/src/local-recording-backup.ts
@@ -0,0 +1,66 @@
+export type LocalRecordingStrategy =
+ | { mode: "off" }
+ | { mode: "full" }
+ | { mode: "capped"; maxBytes: number };
+
+export type LocalRecordingState = {
+ chunks: Blob[];
+ retainedBytes: number;
+ overflowed: boolean;
+};
+
+export const initialLocalRecordingState = (): LocalRecordingState => ({
+ chunks: [],
+ retainedBytes: 0,
+ overflowed: false,
+});
+
+export const appendLocalRecordingChunk = (
+ state: LocalRecordingState,
+ chunk: Blob,
+ strategy: LocalRecordingStrategy,
+): LocalRecordingState => {
+ if (chunk.size === 0 || strategy.mode === "off") {
+ return state;
+ }
+
+ if (strategy.mode === "full") {
+ return {
+ chunks: [...state.chunks, chunk],
+ retainedBytes: state.retainedBytes + chunk.size,
+ overflowed: false,
+ };
+ }
+
+ if (state.overflowed) {
+ return state;
+ }
+
+ if (state.retainedBytes + chunk.size > strategy.maxBytes) {
+ return {
+ chunks: [],
+ retainedBytes: 0,
+ overflowed: true,
+ };
+ }
+
+ return {
+ chunks: [...state.chunks, chunk],
+ retainedBytes: state.retainedBytes + chunk.size,
+ overflowed: false,
+ };
+};
+
+export const finalizeLocalRecording = (
+ state: LocalRecordingState,
+ fallbackMimeType?: string,
+): Blob | null => {
+ if (state.overflowed || state.chunks.length === 0) {
+ return null;
+ }
+
+ return new Blob(state.chunks, {
+ type:
+ state.chunks[0]?.type ?? fallbackMimeType ?? "video/webm;codecs=vp8,opus",
+ });
+};