Skip to content

🎥 Make videos programmatically with React

3.3 KB
Raw Blame
import * as z from 'zod/mini';
import type {StudioElementPayload} from './element-payload';
import {parseStudioElementPayload} from './element-payload';

const browserStudioHashKey = 'remotion-browser-studio';
const defaultBrowserStudioEndpoint = 'https://www.remotion.dev/new';
const maxEncodedPayloadLength = 1_100_000;

const browserStudioEnvelopeSchema = z.object({
	type: z.literal('remotion-browser-studio'),
	version: z.literal(1),
	payload: z.unknown(),
});

const toBase64Url = (value: string) => {
	const bytes = new TextEncoder().encode(value);
	let binary = '';
	const chunkSize = 32_768;

	for (let offset = 0; offset < bytes.length; offset += chunkSize) {
		binary += String.fromCharCode(
			...bytes.subarray(offset, offset + chunkSize),
		);
	}

	return btoa(binary)
		.replaceAll('+', '-')
		.replaceAll('/', '_')
		.replace(/=+$/, '');
};

const fromBase64Url = (value: string) => {
	if (!/^[A-Za-z0-9_-]+$/.test(value)) {
		return null;
	}

	try {
		const base64 = value.replaceAll('-', '+').replaceAll('_', '/');

		const padded = base64.padEnd(
			base64.length + ((4 - (base64.length % 4)) % 4),
			'=',
		);

		const binary = atob(padded);
		const bytes = new Uint8Array(binary.length);

		for (let index = 0; index < binary.length; index++) {
			bytes[index] = binary.charCodeAt(index);
		}

		return new TextDecoder('utf-8', {fatal: true}).decode(bytes);
	} catch {
		return null;
	}
};

export const makeBrowserStudioUrl = ({
	endpoint,
	payload,
}: {
	readonly endpoint: string | null;
	readonly payload: StudioElementPayload;
}) => {
	if (parseStudioElementPayload(payload) === null) {
		throw new TypeError('Invalid Element payload');
	}

	const url = new URL(endpoint ?? defaultBrowserStudioEndpoint);

	if (url.protocol !== 'https:' && url.protocol !== 'http:') {
		throw new TypeError('Browser Studio endpoint must use HTTP or HTTPS');
	}

	const encoded = toBase64Url(
		JSON.stringify({
			payload,
			type: 'remotion-browser-studio',
			version: 1,
		}),
	);

	if (encoded.length > maxEncodedPayloadLength) {
		throw new TypeError('Browser Studio payload is too large');
	}

	url.hash = new URLSearchParams({[browserStudioHashKey]: encoded}).toString();

	return url.toString();
};

export const buildOpenInRemotionNewUrl = ({
	payload,
}: {
	readonly payload: StudioElementPayload;
}) => {
	return makeBrowserStudioUrl({endpoint: null, payload});
};

export const parseBrowserStudioHash = (
	hash: string,
): StudioElementPayload | null => {
	const encoded = new URLSearchParams(
		hash.startsWith('#') ? hash.slice(1) : hash,
	).get(browserStudioHashKey);

	if (
		encoded === null ||
		encoded.length === 0 ||
		encoded.length > maxEncodedPayloadLength
	) {
		return null;
	}

	const decoded = fromBase64Url(encoded);

	if (decoded === null) {
		return null;
	}

	try {
		const envelope = z.safeParse(
			browserStudioEnvelopeSchema,
			JSON.parse(decoded),
		);

		return envelope.success
			? parseStudioElementPayload(envelope.data.payload)
			: null;
	} catch {
		return null;
	}
};

export const openInBrowserStudio = ({
	endpoint,
	payload,
}: {
	readonly endpoint: string | null;
	readonly payload: StudioElementPayload;
}) => {
	if (typeof window === 'undefined') {
		throw new Error('Browser Studio can only be opened in a browser');
	}

	window.open(makeBrowserStudioUrl({endpoint, payload}), '_blank', 'noopener');
};