1
1
import puppeteer from 'puppeteer-core' ;
2
2
import chromium from 'chrome-aws-lambda' ;
3
3
import middleware from './_common/middleware.js' ;
4
- import { exec } from 'child_process' ;
4
+ import { execFile } from 'child_process' ;
5
5
import { promises as fs } from 'fs' ;
6
6
import path from 'path' ;
7
7
import pkg from 'uuid' ;
@@ -20,32 +20,37 @@ const directChromiumScreenshot = async (url) => {
20
20
21
21
return new Promise ( ( resolve , reject ) => {
22
22
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 ( ' ' ) } ` ) ;
26
32
27
- exec ( command , async ( error , stdout , stderr ) => {
33
+ execFile ( chromePath , args , async ( error , stdout , stderr ) => {
28
34
if ( error ) {
29
- console . error ( `[DIRECT-SCREENSHOT] Error executing Chromium: ${ error . message } ` ) ;
35
+ console . error ( `[DIRECT-SCREENSHOT] Chromium error : ${ error . message } ` ) ;
30
36
return reject ( error ) ;
31
37
}
32
-
38
+
33
39
try {
34
- // Read screenshot
40
+ // Read the screenshot file
35
41
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 ` ) ;
37
43
38
- // Convert base64
44
+ // Convert to base64
39
45
const base64Data = screenshotData . toString ( 'base64' ) ;
40
-
41
- // Clean
42
- await fs . unlink ( screenshotPath ) . catch ( err =>
46
+
47
+ await fs . unlink ( screenshotPath ) . catch ( err =>
43
48
console . warn ( `[DIRECT-SCREENSHOT] Failed to delete temp file: ${ err . message } ` )
44
49
) ;
45
-
50
+
46
51
resolve ( base64Data ) ;
47
52
} catch ( readError ) {
48
- console . error ( `[DIRECT-SCREENSHOT] Error reading screenshot: ${ readError . message } ` ) ;
53
+ console . error ( `[DIRECT-SCREENSHOT] Failed reading screenshot: ${ readError . message } ` ) ;
49
54
reject ( readError ) ;
50
55
}
51
56
} ) ;
0 commit comments