Skip to content

🎥 Make videos programmatically with React

artifact type refactor

Jonny Burger208ba6104027
diff --git a/packages/core/src/Artifact.tsx b/packages/core/src/Artifact.tsx
index 673e19fb78..5846737f01 100644
--- a/packages/core/src/Artifact.tsx
+++ b/packages/core/src/Artifact.tsx
@@ -31,7 +31,7 @@ export const Artifact: React.FC<{
content: btoa(new TextDecoder('utf8').decode(content)),
filename,
frame,
- binary: true,
+ contentType: 'binary',
});
} else {
registerRenderAsset({
@@ -40,7 +40,7 @@ export const Artifact: React.FC<{
content,
filename,
frame,
- binary: false,
+ contentType: 'text',
});
}
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index 871eb643b1..8170e4239d 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -133,14 +133,25 @@ export type AudioOrVideoAsset = {
audioStartFrame: number;
};
+type DiscriminatedArtifact =
+ | {
+ contentType: 'binary';
+ content: string;
+ }
+ | {
+ contentType: 'text';
+ content: string;
+ }
+ | {
+ contentType: 'thumbnail';
+ };
+
export type ArtifactAsset = {
type: 'artifact';
id: string;
filename: string;
- content: string | Uint8Array;
frame: number;
- binary: boolean;
-};
+} & DiscriminatedArtifact;
export type TRenderAsset = AudioOrVideoAsset | ArtifactAsset;
diff --git a/packages/core/src/validation/validate-artifact.ts b/packages/core/src/validation/validate-artifact.ts
index f3f4232b29..b92b65acc3 100644
--- a/packages/core/src/validation/validate-artifact.ts
+++ b/packages/core/src/validation/validate-artifact.ts
@@ -37,5 +37,10 @@ export const validateRenderAsset = (artifact: TRenderAsset) => {
}
validateArtifactFilename(artifact.filename);
+
+ if (artifact.contentType === 'thumbnail') {
+ return;
+ }
+
validateContent(artifact.content);
};
diff --git a/packages/renderer/src/collect-assets.ts b/packages/renderer/src/collect-assets.ts
index 279eba2f6e..62e566ace3 100644
--- a/packages/renderer/src/collect-assets.ts
+++ b/packages/renderer/src/collect-assets.ts
@@ -26,20 +26,29 @@ export const collectAssets = async ({
return asset;
}
- if (typeof asset.content !== 'string') {
- throw new Error(
- `Expected string content for artifact ${asset.id}, but got ${asset.content}`,
- );
+ if (asset.contentType === 'binary' || asset.contentType === 'text') {
+ if (typeof asset.content !== 'string') {
+ throw new Error(
+ `Expected string content for artifact ${asset.id}, but got ${asset.content}`,
+ );
+ }
+
+ const stringOrUintArray =
+ asset.contentType === 'binary'
+ ? new TextEncoder().encode(atob(asset.content as string))
+ : asset.content;
+ return {
+ ...asset,
+ content: stringOrUintArray,
+ } as ArtifactAsset;
}
- const stringOrUintArray = asset.binary
- ? new TextEncoder().encode(atob(asset.content as string))
- : asset.content;
+ if (asset.contentType === 'thumbnail') {
+ // TODO: handle thumbnail
+ throw new Error('handle this');
+ }
- return {
- ...asset,
- content: stringOrUintArray,
- } as ArtifactAsset;
+ return asset satisfies never;
});
return fixedArtifacts;
diff --git a/packages/renderer/src/perf.ts b/packages/renderer/src/perf.ts
index ff320ed615..1f730da857 100644
--- a/packages/renderer/src/perf.ts
+++ b/packages/renderer/src/perf.ts
@@ -1,14 +1,7 @@
-type PerfId =
- | 'activate-target'
- | 'capture'
- | 'save'
- | 'extract-frame'
- | 'piping';
+type PerfId = 'capture' | 'extract-frame' | 'piping';
const perf: {[key in PerfId]: number[]} = {
- 'activate-target': [],
capture: [],
- save: [],
'extract-frame': [],
piping: [],
};
diff --git a/packages/renderer/src/test/artifacts.test.tsx b/packages/renderer/src/test/artifacts.test.tsx
index 15389810e0..607a8d392d 100644
--- a/packages/renderer/src/test/artifacts.test.tsx
+++ b/packages/renderer/src/test/artifacts.test.tsx
@@ -32,6 +32,10 @@ test('Should be able to collect artifacts', async () => {
throw new Error('Expected artifact');
}
+ if (sec.contentType !== 'text') {
+ throw new Error('Expected text');
+ }
+
expect(sec.filename).toEqual('hi.txt');
expect(sec.content).toEqual('hi there');
});