Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(sdk-recorder): freeze duration when recording stops

Aditya Garudedc2c057154b
diff --git a/packages/sdk-recorder/__tests__/recorder-options.test.ts b/packages/sdk-recorder/__tests__/recorder-options.test.ts
index 4e575d8c7..9229ff35f 100644
--- a/packages/sdk-recorder/__tests__/recorder-options.test.ts
+++ b/packages/sdk-recorder/__tests__/recorder-options.test.ts
@@ -57,6 +57,7 @@ const originals = new Map(
"setTimeout",
"clearTimeout",
"fetch",
+ "Date",
].map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]),
);
@@ -77,6 +78,64 @@ beforeEach(() => {
}
});
+test.each(["recording", "paused"] as const)(
+ "uploads the duration at stop when %s",
+ async (phase) => {
+ let now = 0;
+ const durations: number[] = [];
+ const NativeDate = Date;
+ Object.defineProperty(globalThis, "Date", {
+ configurable: true,
+ value: class extends NativeDate {
+ static now() {
+ return now;
+ }
+ },
+ });
+ Object.defineProperty(globalThis, "fetch", {
+ configurable: true,
+ value: async (input: string, init?: RequestInit) => {
+ if (input.endsWith("/videos/create")) {
+ now = 5000;
+ return Response.json({
+ videoId: "test-video",
+ shareUrl: "https://cap.so/s/test-video",
+ embedUrl: "https://cap.so/embed/test-video",
+ });
+ }
+ if (input.endsWith("/initiate"))
+ return Response.json({ uploadId: "test-upload" });
+ if (input.endsWith("/presign-part"))
+ return Response.json({ presignedUrl: "https://upload.invalid/part" });
+ if (input === "https://upload.invalid/part")
+ return new Response(null, { headers: { ETag: "test-etag" } });
+ if (input.endsWith("/complete")) {
+ const body = JSON.parse(String(init?.body));
+ durations.push(body.durationInSecs);
+ return Response.json({ success: true });
+ }
+ throw new Error(`Unexpected request: ${input}`);
+ },
+ });
+ const recorder = new CapRecorder({
+ publicKey: "test-only",
+ mode: "camera",
+ microphone: { enabled: false },
+ });
+ try {
+ await recorder.start();
+ now = 1500;
+ if (phase === "paused") recorder.pause();
+ now = 2000;
+ await recorder.stop();
+ expect(recorder.durationMs).toBe(phase === "paused" ? 1500 : 2000);
+ expect(durations).toEqual([phase === "paused" ? 1.5 : 2]);
+ } finally {
+ recorder.destroy();
+ }
+ },
+);
+
afterEach(() => {
for (const [key, descriptor] of originals) {
if (descriptor) Object.defineProperty(globalThis, key, descriptor);
diff --git a/packages/sdk-recorder/src/index.ts b/packages/sdk-recorder/src/index.ts
index 5d10d99de..edd71d913 100644
--- a/packages/sdk-recorder/src/index.ts
+++ b/packages/sdk-recorder/src/index.ts
@@ -212,6 +212,9 @@ export class CapRecorder {
throw new Error(`Cannot stop from phase "${this._phase}"`);
}
+ this._durationMs =
+ this.accumulatedMs +
+ (this._phase === "recording" ? Date.now() - this.lastResumeTime : 0);
this.setPhase("stopping");
this.stopDurationTimer();
@@ -244,8 +247,6 @@ export class CapRecorder {
this._videoId = videoId;
- this._durationMs =
- this.accumulatedMs + (Date.now() - this.lastResumeTime);
await this.client.uploadBlob(videoId, blob, {
durationMs: this._durationMs,
});