@@ -101,6 +101,8 @@ const HOST_RECONNECT_DELAY = "1 second";
101101const DESKTOP_ATTACH_TIMEOUT = "10 seconds" ;
102102const VIEWER_OUTPUT_LIMIT = 64 ;
103103const RECORDING_SCREENCAST = { format : "jpeg" , quality : 90 , everyNthFrame : 1 } as const ;
104+ const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024 ;
105+ const RECORDING_DOWNLOAD_TIMEOUT_MS = 5_000 ;
104106const decodeViewportSetting = Schema . decodeUnknownSync ( PreviewViewportSettingSchema ) ;
105107const AGENT_CURSOR_MOVE_MS = 160 ;
106108const AGENT_CURSOR_CLICK_LEAD_MS = 40 ;
@@ -294,6 +296,7 @@ interface EncoderWindow {
294296 frame ( data : string , width : number ) : Promise < boolean > ;
295297 stop ( ) : Promise < { mimeType : string | null ; count : number ; bytes : number } > ;
296298 chunk ( index : number ) : Promise < string > ;
299+ download ( ) : void ;
297300 } ;
298301}
299302
@@ -1920,8 +1923,6 @@ const make = Effect.gen(function* () {
19201923 message : "No recording is active for this tab." ,
19211924 } ) ;
19221925 }
1923- let mimeType : string | null ;
1924- const chunks : Array < Buffer > = [ ] ;
19251926 try {
19261927 await recording . session . send ( "Page.stopScreencast" ) . catch ( constVoid ) ;
19271928 await Promise . all ( recording . framesInFlight ) ;
@@ -1945,50 +1946,72 @@ const make = Effect.gen(function* () {
19451946 . catch ( constVoid ) ;
19461947 }
19471948 }
1948- const stopped = await recording . encoder . evaluate ( ( ) =>
1949+ const { mimeType , count , bytes } = await recording . encoder . evaluate ( ( ) =>
19491950 ( globalThis as unknown as EncoderWindow ) . __t3Recorder . stop ( ) ,
19501951 ) ;
1951- mimeType = stopped . mimeType ;
19521952 // Checked before the transfer so an oversized video never lands in this process.
1953- if ( stopped . bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES ) {
1953+ if ( bytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES ) {
19541954 throw new ServerBrowserPage . ServerBrowserOperationError ( {
19551955 tag : "PreviewAutomationRecordingTooLargeError" ,
19561956 message : "The recording is larger than the attachment limit." ,
19571957 } ) ;
19581958 }
1959- for ( let index = 0 ; index < stopped . count ; index += 1 ) {
1960- const chunk = await recording . encoder . evaluate (
1961- ( chunkIndex ) => ( globalThis as unknown as EncoderWindow ) . __t3Recorder . chunk ( chunkIndex ) ,
1962- index ,
1963- ) ;
1964- chunks . push ( Buffer . from ( chunk , "base64" ) ) ;
1959+ if ( ! mimeType || bytes === 0 ) {
1960+ throw new ServerBrowserPage . ServerBrowserOperationError ( {
1961+ tag : "PreviewAutomationExecutionError" ,
1962+ message : "The recording captured no frames." ,
1963+ } ) ;
19651964 }
1965+ const extension = mimeType . startsWith ( "video/mp4" ) ? "mp4" : "webm" ;
1966+ const pendingId = `${ PENDING_ATTACHMENT_THREAD_SEGMENT } -${ NodeCrypto . randomUUID ( ) } -${ extension } ` ;
1967+ const path = NodePath . join ( config . attachmentsDir , `${ pendingId } .${ extension } ` ) ;
1968+ await NodeFSP . mkdir ( config . attachmentsDir , { recursive : true } ) ;
1969+ if ( bytes >= RECORDING_BINARY_EXPORT_MIN_BYTES ) {
1970+ const stagedPath = `${ path } .partial` ;
1971+ try {
1972+ const [ download ] = await Promise . all ( [
1973+ recording . encoder . waitForEvent ( "download" , {
1974+ timeout : RECORDING_DOWNLOAD_TIMEOUT_MS ,
1975+ } ) ,
1976+ recording . encoder . evaluate ( ( ) =>
1977+ ( globalThis as unknown as EncoderWindow ) . __t3Recorder . download ( ) ,
1978+ ) ,
1979+ ] ) ;
1980+ await download . saveAs ( stagedPath ) ;
1981+ if ( ( await NodeFSP . stat ( stagedPath ) ) . size !== bytes ) {
1982+ throw new Error ( "The recording download is incomplete." ) ;
1983+ }
1984+ await NodeFSP . rename ( stagedPath , path ) ;
1985+ } finally {
1986+ await NodeFSP . rm ( stagedPath , { force : true } ) . catch ( constVoid ) ;
1987+ }
1988+ } else {
1989+ const chunks : Array < Buffer > = [ ] ;
1990+ for ( let index = 0 ; index < count ; index += 1 ) {
1991+ const chunk = await recording . encoder . evaluate (
1992+ ( chunkIndex ) =>
1993+ ( globalThis as unknown as EncoderWindow ) . __t3Recorder . chunk ( chunkIndex ) ,
1994+ index ,
1995+ ) ;
1996+ chunks . push ( Buffer . from ( chunk , "base64" ) ) ;
1997+ }
1998+ const data = Buffer . concat ( chunks ) ;
1999+ if ( data . byteLength !== bytes ) throw new Error ( "The recording transfer is incomplete." ) ;
2000+ await NodeFSP . writeFile ( path , data ) ;
2001+ }
2002+ return {
2003+ id : pendingId ,
2004+ tabId : tab . tabId ,
2005+ path,
2006+ mimeType : mimeType . split ( ";" ) [ 0 ] ! ,
2007+ sizeBytes : bytes ,
2008+ createdAt : new Date ( ) . toISOString ( ) ,
2009+ uploadedAttachmentId : pendingId ,
2010+ } ;
19662011 } finally {
19672012 await recording . encoder . close ( ) . catch ( constVoid ) ;
19682013 tab . recording = null ;
19692014 }
1970- const data = Buffer . concat ( chunks ) ;
1971- if ( ! mimeType || data . byteLength === 0 ) {
1972- throw new ServerBrowserPage . ServerBrowserOperationError ( {
1973- tag : "PreviewAutomationExecutionError" ,
1974- message : "The recording captured no frames." ,
1975- } ) ;
1976- }
1977-
1978- const extension = mimeType . startsWith ( "video/mp4" ) ? "mp4" : "webm" ;
1979- const pendingId = `${ PENDING_ATTACHMENT_THREAD_SEGMENT } -${ NodeCrypto . randomUUID ( ) } -${ extension } ` ;
1980- const path = NodePath . join ( config . attachmentsDir , `${ pendingId } .${ extension } ` ) ;
1981- await NodeFSP . mkdir ( config . attachmentsDir , { recursive : true } ) ;
1982- await NodeFSP . writeFile ( path , data ) ;
1983- return {
1984- id : pendingId ,
1985- tabId : tab . tabId ,
1986- path,
1987- mimeType : mimeType . split ( ";" ) [ 0 ] ! ,
1988- sizeBytes : data . byteLength ,
1989- createdAt : new Date ( ) . toISOString ( ) ,
1990- uploadedAttachmentId : pendingId ,
1991- } ;
19922015 } ) ;
19932016
19942017 let pointerSequence = 0 ;
0 commit comments