11/* eslint-disable functional/no-expression-statements */
2- import { type APIRoute } from 'astro'
2+ import type { Handler } from 'aws-lambda'
3+ import type { APIGatewayEvent , APIGatewayProxyResult } from 'aws-lambda'
34import chromium from '@sparticuz/chromium-min'
4- import { Chromium } from '../../ libs/chromium.ts '
5+ import { Chromium } from './ libs/chromium.js '
56import type { PuppeteerLifeCycleEvent } from 'puppeteer-core'
67
78const exePath =
@@ -37,7 +38,10 @@ const puppeteerLifeCycleEvents = [
3738 'networkidle2' ,
3839] satisfies PuppeteerLifeCycleEvent [ ]
3940
40- export const GET : APIRoute = async ( { url } ) => {
41+ export const handler : Handler = async (
42+ event : APIGatewayEvent ,
43+ ) : Promise < APIGatewayProxyResult > => {
44+ const url = new URL ( event . path )
4145 const { isDev, height, width, cacheControl, gotoThenWaitUntil } = {
4246 isDev : url . searchParams . get ( 'dev' ) === 'true' ,
4347 height : url . searchParams . get ( 'h' ) ,
@@ -52,12 +56,12 @@ export const GET: APIRoute = async ({ url }) => {
5256 const targetUrl = url . searchParams . get ( 'src' )
5357
5458 if ( ! targetUrl ) {
55- return new Response ( 'URL query parameter is required' , { status : 400 } )
59+ return { statusCode : 404 , body : 'URL query parameter is required' }
5660 }
5761
5862 // Validate that the URL starts with http or https
5963 if ( ! targetUrl . startsWith ( 'http://' ) && ! targetUrl . startsWith ( 'https://' ) ) {
60- return new Response ( 'Invalid URL format' , { status : 400 } )
64+ return { statusCode : 400 , body : 'Invalid URL format' }
6165 }
6266
6367 const browser = await Chromium . getInstance ( options )
@@ -96,16 +100,20 @@ export const GET: APIRoute = async ({ url }) => {
96100 // await browser.close()
97101
98102 // Return the PNG image as the response
99- return new Response ( file , {
100- status : 200 ,
103+ return {
104+ statusCode : 200 ,
105+ body : Buffer . from ( file ) . toString ( 'base64' ) ,
101106 headers : {
102107 'Content-Type' : 'image/png' ,
103108 'access-control-allow-origin' : '*' ,
104109 'cache-control' : cacheControl ?? `public, max-age=31536000` ,
105110 } ,
106- } )
111+ } satisfies APIGatewayProxyResult
107112 } catch ( error ) {
108113 console . error ( 'Error capturing screenshot:' , error )
109- return new Response ( 'Internal Server Error' , { status : 500 } )
114+ return {
115+ statusCode : 500 ,
116+ body : 'Internal Server Error' ,
117+ }
110118 }
111119}
0 commit comments