refactor(video): derive the JPEG frame pattern from the exported prefix

This commit is contained in:
diegosouzapw
2026-09-01 08:01:00 -03:00
parent 630dc172a2
commit d7fc935514

View File

@@ -1,6 +1,10 @@
export const JPEG_FRAME_DATA_URI_PREFIX = "data:image/jpeg;base64,";
const JPEG_FRAME_DATA_URI_PATTERN = /^data:image\/jpeg;base64,([A-Za-z0-9+/=]+)$/i;
// Derived from the exported prefix so the two can never drift apart.
const JPEG_FRAME_DATA_URI_PATTERN = new RegExp(
`^${JPEG_FRAME_DATA_URI_PREFIX.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&")}([A-Za-z0-9+/=]+)$`,
"i"
);
function matchJpegFrame(dataUri: string): string {
const match = JPEG_FRAME_DATA_URI_PATTERN.exec(dataUri);
@@ -8,7 +12,10 @@ function matchJpegFrame(dataUri: string): string {
return match[1];
}
/** Throws on any non-JPEG or base64-invalid input; the single frame decode used by every video module. */
/**
* Throws on any non-JPEG or base64-invalid input; the single frame decode used by every
* video module.
*/
export function decodeJpegFrameDataUri(dataUri: string): Buffer {
return Buffer.from(matchJpegFrame(dataUri), "base64");
}