Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/licensing`: Add automatic idempotency keys to `registerUsageEvent()`

Jonny Burger2aab7e80951f
diff --git a/packages/docs/docs/licensing/register-usage-event.mdx b/packages/docs/docs/licensing/register-usage-event.mdx
index 6f5ddb479e..ac29689be7 100644
--- a/packages/docs/docs/licensing/register-usage-event.mdx
+++ b/packages/docs/docs/licensing/register-usage-event.mdx
@@ -51,6 +51,14 @@ If the host is `localhost` or similar, it will be registered as non-billable.
Whether the event was successful or not.
If the event was not successful, it will be registered as a non-billable event.
+### `idempotencyKey`<AvailableFrom v="4.0.533"/>
+
+Optional, type: `string` (1–255 characters).
+
+A key is generated automatically for each call and reused across network retries to prevent duplicate usage points. You do not need to supply one.
+
+If you call `registerUsageEvent()` more than once for the same event, pass the same key to each call. Keys are scoped to your project; a repeated key returns the original result without recording another usage point. Use a different key for each new event.
+
## Return value
A promise that resolves when the event was successfully registered.
diff --git a/packages/licensing/src/register-usage-event.ts b/packages/licensing/src/register-usage-event.ts
index fbf03bdf63..445175ae01 100644
--- a/packages/licensing/src/register-usage-event.ts
+++ b/packages/licensing/src/register-usage-event.ts
@@ -65,10 +65,15 @@ type RegisterUsageEventMandatoryOptions = {
type OptionalRegisterUsageEventOptional = {
isStill: boolean;
isProduction: boolean;
+ /** Automatically generated when omitted and reused across request retries. */
+ idempotencyKey: string;
};
type InternalRegisterUsageEventOptions = RegisterUsageEventMandatoryOptions &
- OptionalRegisterUsageEventOptional & {licenseKey: string | null};
+ Omit<OptionalRegisterUsageEventOptional, 'idempotencyKey'> & {
+ licenseKey: string | null;
+ idempotencyKey?: string | null;
+ };
type RegisterUsageEventOptions = RegisterUsageEventMandatoryOptions &
EitherApiKeyOrLicenseKey &
@@ -81,7 +86,15 @@ export const internalRegisterUsageEvent = async ({
isStill,
isProduction,
licenseKey,
+ idempotencyKey,
}: InternalRegisterUsageEventOptions): Promise<RegisterUsageEventResponse> => {
+ // Reuse the same key for every retry, including requests whose response
+ // was lost after the server recorded the event. randomUUID is unavailable
+ // in browsers on insecure origins, so keep those environments working too.
+ const eventIdempotencyKey =
+ idempotencyKey ??
+ globalThis.crypto?.randomUUID?.() ??
+ `${Date.now()}-${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}`;
let lastError: Error | undefined;
const totalAttempts = DEFAULT_MAX_RETRIES + 1;
@@ -101,6 +114,7 @@ export const internalRegisterUsageEvent = async ({
succeeded,
isStill,
isProduction,
+ idempotencyKey: eventIdempotencyKey,
}),
headers: {
'Content-Type': 'application/json',
diff --git a/packages/serverless/src/handlers/launch.ts b/packages/serverless/src/handlers/launch.ts
index 9d709c3bd4..edc1c456d9 100644
--- a/packages/serverless/src/handlers/launch.ts
+++ b/packages/serverless/src/handlers/launch.ts
@@ -866,6 +866,7 @@ export const launchHandler = async <Provider extends CloudProvider>({
logLevel: params.logLevel,
isStill: false,
isProduction: params.isProduction ?? true,
+ idempotencyKey: `cloud-render:${params.bucketName}:${params.renderId}`,
});
if (!params.webhook || webhookInvoked) {
diff --git a/packages/serverless/src/handlers/send-telemetry-event.ts b/packages/serverless/src/handlers/send-telemetry-event.ts
index dba42b29dd..e2d20b8b46 100644
--- a/packages/serverless/src/handlers/send-telemetry-event.ts
+++ b/packages/serverless/src/handlers/send-telemetry-event.ts
@@ -7,11 +7,13 @@ export const sendTelemetryEvent = async ({
logLevel,
isStill,
isProduction,
+ idempotencyKey,
}: {
licenseKey: string | null;
logLevel: LogLevel;
isStill: boolean;
isProduction: boolean;
+ idempotencyKey: string;
}) => {
if (licenseKey === null) {
return Promise.resolve();
@@ -26,6 +28,7 @@ export const sendTelemetryEvent = async ({
succeeded: true,
isStill,
isProduction,
+ idempotencyKey,
});
RenderInternals.Log.info({indent: false, logLevel}, 'Telemetry event sent');
} catch (err) {
diff --git a/packages/serverless/src/handlers/still.ts b/packages/serverless/src/handlers/still.ts
index ecb6abc7c5..ea8cecfe80 100644
--- a/packages/serverless/src/handlers/still.ts
+++ b/packages/serverless/src/handlers/still.ts
@@ -371,6 +371,7 @@ const innerStillHandler = async <Provider extends CloudProvider>(
logLevel: params.logLevel,
isStill: true,
isProduction: params.isProduction ?? true,
+ idempotencyKey: `cloud-still:${bucketName}:${renderId}`,
}),
]);