Skip to content

🎥 Make videos programmatically with React

can now handle rendering errors for media

Matt McGillivrayefd89d4e8311
diff --git a/packages/cloudrun/src/api/render-media-on-cloudrun.ts b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
index 32a71bf36e..4f67c32952 100644
--- a/packages/cloudrun/src/api/render-media-on-cloudrun.ts
+++ b/packages/cloudrun/src/api/render-media-on-cloudrun.ts
@@ -210,16 +210,29 @@ export const renderMediaOnCloudrun = async ({
const stream: any = postResponse.data;
+ let accumulatedChunks = ''; // A buffer to accumulate chunks.
+
stream.on('data', (chunk: Buffer) => {
- const chunkResponse = JSON.parse(chunk.toString().trim());
- if (chunkResponse.response) {
- response = chunkResponse.response;
- } else if (chunkResponse.onProgress) {
- updateRenderProgress?.(chunkResponse.onProgress);
+ accumulatedChunks += chunk.toString(); // Add the new chunk to the buffer.
+ let parsedData;
+
+ try {
+ parsedData = JSON.parse(accumulatedChunks.trim());
+ accumulatedChunks = ''; // Clear the buffer after successful parsing.
+ } catch (e) {
+ // If parsing fails, it means we don't have a complete JSON string yet.
+ // We'll wait for more chunks.
+ return;
+ }
+
+ if (parsedData.response) {
+ response = parsedData.response;
+ } else if (parsedData.onProgress) {
+ updateRenderProgress?.(parsedData.onProgress);
}
- if (chunkResponse.type === 'error') {
- reject(chunkResponse);
+ if (parsedData.type === 'error') {
+ reject(parsedData);
}
});