|
1 | 1 | /* eslint-disable functional/no-expression-statements */ |
2 | 2 | import type { Handler } from 'aws-lambda' |
3 | | -import type { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda' |
| 3 | +import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda' |
4 | 4 | import chromium from '@sparticuz/chromium' |
5 | 5 | import { Chromium } from './libs/chromium.js' |
6 | 6 | import type { PuppeteerLifeCycleEvent } from 'puppeteer-core' |
@@ -30,23 +30,49 @@ const puppeteerLifeCycleEvents = [ |
30 | 30 | 'networkidle2', |
31 | 31 | ] satisfies PuppeteerLifeCycleEvent[] |
32 | 32 |
|
| 33 | +type APIGatewayEvent = Omit<APIGatewayProxyEvent, 'requestContext'> & { |
| 34 | + requestContext: APIGatewayProxyEvent['requestContext'] & { |
| 35 | + http?: { path?: string } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +const U = undefined |
| 40 | +const EXP = /\/.+\/with\/([0-9]+)\/([0-9]+)\/(.+)/ |
| 41 | + |
33 | 42 | export const handler: Handler = async ({ |
34 | 43 | queryStringParameters, |
| 44 | + requestContext, |
35 | 45 | }: APIGatewayEvent): Promise<APIGatewayProxyResult> => { |
36 | 46 | console.log('queryStringParameters', queryStringParameters) |
| 47 | + const path = requestContext.http?.path |
| 48 | + const pathParams = path |
| 49 | + ? ((arg) => |
| 50 | + arg |
| 51 | + ? { |
| 52 | + w: arg[1], |
| 53 | + h: arg[2], |
| 54 | + src: decodeURIComponent(arg[3]).replace( |
| 55 | + /^(http)(s?):\/\/?/, |
| 56 | + '$1$2://', |
| 57 | + ), |
| 58 | + } |
| 59 | + : U)(path.match(EXP)) |
| 60 | + : U |
| 61 | + console.log('pathParams', pathParams) |
37 | 62 | const params = { ...queryStringParameters } |
38 | | - const { isDev, height, width, cacheControl, gotoThenWaitUntil } = { |
| 63 | + const { isDev, height, width, src, cacheControl, gotoThenWaitUntil } = { |
39 | 64 | isDev: params['dev'] === 'true', |
40 | | - height: params['h'], |
41 | | - width: params['w'], |
| 65 | + height: pathParams?.h ?? params['h'], |
| 66 | + width: pathParams?.w ?? params['w'], |
| 67 | + src: pathParams?.src ?? params['src'], |
42 | 68 | cacheControl: params['cache-control'], |
43 | 69 | gotoThenWaitUntil: params['goto-then-waituntil'], |
44 | 70 | } |
45 | 71 | const options = isDev ? Localoptions : serverOptions |
46 | 72 |
|
47 | 73 | try { |
48 | 74 | // Extract the "target" query parameter (the URL to capture) |
49 | | - const targetUrl = params['src'] |
| 75 | + const targetUrl = src |
50 | 76 |
|
51 | 77 | if (!targetUrl) { |
52 | 78 | return { statusCode: 404, body: 'URL query parameter is required' } |
|
0 commit comments