11import puppeteer from 'puppeteer-core' ;
22import chromium from 'chrome-aws-lambda' ;
33import middleware from './_common/middleware.js' ;
4- import { exec } from 'child_process' ;
4+ import { execFile } from 'child_process' ;
55import { promises as fs } from 'fs' ;
66import path from 'path' ;
77import pkg from 'uuid' ;
@@ -20,32 +20,37 @@ const directChromiumScreenshot = async (url) => {
2020
2121 return new Promise ( ( resolve , reject ) => {
2222 const chromePath = process . env . CHROME_PATH || '/usr/bin/chromium' ;
23- const command = `${ chromePath } --headless --disable-gpu --no-sandbox --screenshot=${ screenshotPath } "${ url } "` ;
24-
25- console . log ( `[DIRECT-SCREENSHOT] Executing command: ${ command } ` ) ;
23+ const args = [
24+ '--headless' ,
25+ '--disable-gpu' ,
26+ '--no-sandbox' ,
27+ `--screenshot=${ screenshotPath } ` ,
28+ url
29+ ] ;
30+
31+ console . log ( `[DIRECT-SCREENSHOT] Executing: ${ chromePath } ${ args . join ( ' ' ) } ` ) ;
2632
27- exec ( command , async ( error , stdout , stderr ) => {
33+ execFile ( chromePath , args , async ( error , stdout , stderr ) => {
2834 if ( error ) {
29- console . error ( `[DIRECT-SCREENSHOT] Error executing Chromium: ${ error . message } ` ) ;
35+ console . error ( `[DIRECT-SCREENSHOT] Chromium error : ${ error . message } ` ) ;
3036 return reject ( error ) ;
3137 }
32-
38+
3339 try {
34- // Read screenshot
40+ // Read the screenshot file
3541 const screenshotData = await fs . readFile ( screenshotPath ) ;
36- console . log ( `[DIRECT-SCREENSHOT] Read ${ screenshotData . length } bytes from screenshot file ` ) ;
42+ console . log ( `[DIRECT-SCREENSHOT] Screenshot read successfully ` ) ;
3743
38- // Convert base64
44+ // Convert to base64
3945 const base64Data = screenshotData . toString ( 'base64' ) ;
40-
41- // Clean
42- await fs . unlink ( screenshotPath ) . catch ( err =>
46+
47+ await fs . unlink ( screenshotPath ) . catch ( err =>
4348 console . warn ( `[DIRECT-SCREENSHOT] Failed to delete temp file: ${ err . message } ` )
4449 ) ;
45-
50+
4651 resolve ( base64Data ) ;
4752 } catch ( readError ) {
48- console . error ( `[DIRECT-SCREENSHOT] Error reading screenshot: ${ readError . message } ` ) ;
53+ console . error ( `[DIRECT-SCREENSHOT] Failed reading screenshot: ${ readError . message } ` ) ;
4954 reject ( readError ) ;
5055 }
5156 } ) ;
0 commit comments