Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
import { createHash } from "node:crypto";
import { Video } from "@cap/web-domain";
export const DESKTOP_SEGMENTS_RECOVERY_MARKER_PREFIX =
"desktop-segments-recovery:v1:";
export type DesktopSegmentsRecoveryMarker = {
observedAtMs: number;
signature: string;
attempts: number;
};
export function getDesktopSegmentsManifestSignature(
manifest: Video.SegmentManifestType,
) {
const normalize = (segments: Video.SegmentManifestType["video_segments"]) =>
segments
.map(Video.normalizeSegmentEntry)
.toSorted((a, b) => a.index - b.index);
return createHash("sha256")
.update(
JSON.stringify({
videoInitUploaded: manifest.video_init_uploaded,
audioInitUploaded: manifest.audio_init_uploaded,
videoSegments: normalize(manifest.video_segments),
audioSegments: normalize(manifest.audio_segments),
isComplete: manifest.is_complete,
}),
)
.digest("hex")
.slice(0, 32);
}
export function buildDesktopSegmentsRecoveryMarker(
signature: string,
observedAtMs = Date.now(),
attempts = 0,
) {
return `${DESKTOP_SEGMENTS_RECOVERY_MARKER_PREFIX}${observedAtMs}:${signature}:${attempts}`;
}
export function parseDesktopSegmentsRecoveryMarker(
message: string | null | undefined,
): DesktopSegmentsRecoveryMarker | null {
if (!message?.startsWith(DESKTOP_SEGMENTS_RECOVERY_MARKER_PREFIX)) {
return null;
}
const payload = message.slice(DESKTOP_SEGMENTS_RECOVERY_MARKER_PREFIX.length);
const [observedAtRaw, signature, attemptsRaw] = payload.split(":");
const observedAtMs = Number(observedAtRaw);
if (!Number.isSafeInteger(observedAtMs) || !signature) {
return null;
}
const attemptsParsed = Number(attemptsRaw);
const attempts =
Number.isSafeInteger(attemptsParsed) && attemptsParsed >= 0
? attemptsParsed
: 0;
return { observedAtMs, signature, attempts };
}