@@ -100,6 +100,8 @@ const HOST_RECONNECT_DELAY = "1 second";
100100const DESKTOP_ATTACH_TIMEOUT = "10 seconds" ;
101101const VIEWER_OUTPUT_LIMIT = 64 ;
102102const RECORDING_SCREENCAST = { format : "jpeg" , quality : 90 , everyNthFrame : 1 } as const ;
103+ const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024 ;
104+ const RECORDING_DOWNLOAD_TIMEOUT_MS = 5_000 ;
103105const decodeViewportSetting = Schema . decodeUnknownSync ( PreviewViewportSettingSchema ) ;
104106const AGENT_CURSOR_MOVE_MS = 160 ;
105107const AGENT_CURSOR_CLICK_LEAD_MS = 40 ;
@@ -279,6 +281,7 @@ interface EncoderWindow {
279281 frame ( data : string , width : number ) : Promise < boolean > ;
280282 stop ( ) : Promise < { mimeType : string | null ; count : number ; bytes : number } > ;
281283 chunk ( index : number ) : Promise < string > ;
284+ download ( ) : void ;
282285 } ;
283286}
284287
@@ -1818,56 +1821,76 @@ const make = Effect.gen(function* () {
18181821 message : "No recording is active for this tab." ,
18191822 } ) ;
18201823 }
1821- let mimeType : string | null ;
1822- const chunks : Array < Buffer > = [ ] ;
18231824 try {
18241825 await recording . session . send ( "Page.stopScreencast" ) . catch ( constVoid ) ;
18251826 await Promise . all ( recording . framesInFlight ) ;
18261827 await recording . session . detach ( ) . catch ( constVoid ) ;
1827- const stopped = await recording . encoder . evaluate ( ( ) =>
1828+ const { mimeType , count , bytes } = await recording . encoder . evaluate ( ( ) =>
18281829 ( globalThis as unknown as EncoderWindow ) . __t3Recorder . stop ( ) ,
18291830 ) ;
1830- mimeType = stopped . mimeType ;
18311831 // Checked before the transfer so an oversized video never lands in this process.
1832- if ( stopped . bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES ) {
1832+ if ( bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES ) {
18331833 throw new ServerBrowserPage . ServerBrowserOperationError ( {
18341834 tag : "PreviewAutomationRecordingTooLargeError" ,
18351835 message : "The recording is larger than the attachment limit." ,
18361836 } ) ;
18371837 }
1838- for ( let index = 0 ; index < stopped . count ; index += 1 ) {
1839- const chunk = await recording . encoder . evaluate (
1840- ( chunkIndex ) => ( globalThis as unknown as EncoderWindow ) . __t3Recorder . chunk ( chunkIndex ) ,
1841- index ,
1842- ) ;
1843- chunks . push ( Buffer . from ( chunk , "base64" ) ) ;
1838+ if ( ! mimeType || bytes === 0 ) {
1839+ throw new ServerBrowserPage . ServerBrowserOperationError ( {
1840+ tag : "PreviewAutomationExecutionError" ,
1841+ message : "The recording captured no frames." ,
1842+ } ) ;
18441843 }
1844+ const extension = mimeType . startsWith ( "video/mp4" ) ? "mp4" : "webm" ;
1845+ const pendingId = `${ PENDING_ATTACHMENT_THREAD_SEGMENT } -${ NodeCrypto . randomUUID ( ) } -${ extension } ` ;
1846+ const path = NodePath . join ( config . attachmentsDir , `${ pendingId } .${ extension } ` ) ;
1847+ await NodeFSP . mkdir ( config . attachmentsDir , { recursive : true } ) ;
1848+ if ( bytes >= RECORDING_BINARY_EXPORT_MIN_BYTES ) {
1849+ const stagedPath = `${ path } .partial` ;
1850+ try {
1851+ const [ download ] = await Promise . all ( [
1852+ recording . encoder . waitForEvent ( "download" , {
1853+ timeout : RECORDING_DOWNLOAD_TIMEOUT_MS ,
1854+ } ) ,
1855+ recording . encoder . evaluate ( ( ) =>
1856+ ( globalThis as unknown as EncoderWindow ) . __t3Recorder . download ( ) ,
1857+ ) ,
1858+ ] ) ;
1859+ await download . saveAs ( stagedPath ) ;
1860+ if ( ( await NodeFSP . stat ( stagedPath ) ) . size !== bytes ) {
1861+ throw new Error ( "The recording download is incomplete." ) ;
1862+ }
1863+ await NodeFSP . rename ( stagedPath , path ) ;
1864+ } finally {
1865+ await NodeFSP . rm ( stagedPath , { force : true } ) . catch ( constVoid ) ;
1866+ }
1867+ } else {
1868+ const chunks : Array < Buffer > = [ ] ;
1869+ for ( let index = 0 ; index < count ; index += 1 ) {
1870+ const chunk = await recording . encoder . evaluate (
1871+ ( chunkIndex ) =>
1872+ ( globalThis as unknown as EncoderWindow ) . __t3Recorder . chunk ( chunkIndex ) ,
1873+ index ,
1874+ ) ;
1875+ chunks . push ( Buffer . from ( chunk , "base64" ) ) ;
1876+ }
1877+ const data = Buffer . concat ( chunks ) ;
1878+ if ( data . byteLength !== bytes ) throw new Error ( "The recording transfer is incomplete." ) ;
1879+ await NodeFSP . writeFile ( path , data ) ;
1880+ }
1881+ return {
1882+ id : pendingId ,
1883+ tabId : tab . tabId ,
1884+ path,
1885+ mimeType : mimeType . split ( ";" ) [ 0 ] ! ,
1886+ sizeBytes : bytes ,
1887+ createdAt : new Date ( ) . toISOString ( ) ,
1888+ uploadedAttachmentId : pendingId ,
1889+ } ;
18451890 } finally {
18461891 await recording . encoder . close ( ) . catch ( constVoid ) ;
18471892 tab . recording = null ;
18481893 }
1849- const data = Buffer . concat ( chunks ) ;
1850- if ( ! mimeType || data . byteLength === 0 ) {
1851- throw new ServerBrowserPage . ServerBrowserOperationError ( {
1852- tag : "PreviewAutomationExecutionError" ,
1853- message : "The recording captured no frames." ,
1854- } ) ;
1855- }
1856-
1857- const extension = mimeType . startsWith ( "video/mp4" ) ? "mp4" : "webm" ;
1858- const pendingId = `${ PENDING_ATTACHMENT_THREAD_SEGMENT } -${ NodeCrypto . randomUUID ( ) } -${ extension } ` ;
1859- const path = NodePath . join ( config . attachmentsDir , `${ pendingId } .${ extension } ` ) ;
1860- await NodeFSP . mkdir ( config . attachmentsDir , { recursive : true } ) ;
1861- await NodeFSP . writeFile ( path , data ) ;
1862- return {
1863- id : pendingId ,
1864- tabId : tab . tabId ,
1865- path,
1866- mimeType : mimeType . split ( ";" ) [ 0 ] ! ,
1867- sizeBytes : data . byteLength ,
1868- createdAt : new Date ( ) . toISOString ( ) ,
1869- uploadedAttachmentId : pendingId ,
1870- } ;
18711894 } ) ;
18721895
18731896 let pointerSequence = 0 ;
0 commit comments