Skip to content

Commit 8aa5366

Browse files
test2
1 parent a8c4908 commit 8aa5366

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
"dependencies": {
1919
"@astrojs/vercel": "^7.8.2",
2020
"@devprotocol/util-ts": "^4.0.0",
21+
"@sparticuz/chromium-min": "^130.0.0",
2122
"astro": "^4.16.3",
2223
"chrome-aws-lambda": "^10.1.0",
24+
"chrome-launcher": "^1.1.2",
2325
"puppeteer-core": "10.1.0",
2426
"ramda": "0.30.1"
2527
},

src/pages/api/capt.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* eslint-disable functional/no-expression-statements */
2+
import { type APIRoute } from 'astro'
3+
import puppeteer from 'puppeteer-core'
4+
import chromium from "@sparticuz/chromium-min";
5+
6+
const exePath =
7+
process.platform === 'win32'
8+
? 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
9+
: process.platform === 'linux'
10+
? '/usr/bin/google-chrome'
11+
: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
12+
13+
14+
15+
const chromiumPack = "https://github.com/Sparticuz/chromium/releases/download/v121.0.0/chromium-v121.0.0-pack.tar"
16+
17+
18+
const Localoptions = {
19+
args: [],
20+
executablePath: exePath,
21+
headless: true,
22+
}
23+
const serverOptions = {
24+
args: chromium.args,
25+
executablePath: await chromium.executablePath(chromiumPack),
26+
headless: true,
27+
}
28+
29+
export const POST: APIRoute = async ({ url }) => {
30+
const isDev = url.searchParams.get('dev') === 'true'
31+
const options = isDev ? Localoptions : serverOptions
32+
33+
try {
34+
// Extract the "target" query parameter (the URL to capture)
35+
const targetUrl = url.searchParams.get('src')
36+
37+
if (!targetUrl) {
38+
return new Response('URL query parameter is required', { status: 400 })
39+
}
40+
41+
// Validate that the URL starts with http or https
42+
if (!targetUrl.startsWith('http://') && !targetUrl.startsWith('https://')) {
43+
return new Response('Invalid URL format', { status: 400 })
44+
}
45+
46+
const browser = await puppeteer.launch(options)
47+
const page = await browser.newPage()
48+
49+
// set the viewport size
50+
await page.setViewport({
51+
width: 1920,
52+
height: 1080,
53+
deviceScaleFactor: 1,
54+
})
55+
56+
// tell the page to visit the url
57+
await page.goto(targetUrl)
58+
59+
// take a screenshot
60+
const file = await page.screenshot({
61+
type: 'png',
62+
})
63+
64+
// close the browser
65+
await browser.close()
66+
67+
// Return the PNG image as the response
68+
return new Response(file, {
69+
status: 200,
70+
headers: {
71+
'Content-Type': 'image/png',
72+
},
73+
})
74+
} catch (error) {
75+
console.error('Error capturing screenshot:', error)
76+
return new Response('Internal Server Error', { status: 500 })
77+
}
78+
}

yarn.lock

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,16 @@ __metadata:
11171117
languageName: node
11181118
linkType: hard
11191119

1120+
"@sparticuz/chromium-min@npm:^130.0.0":
1121+
version: 130.0.0
1122+
resolution: "@sparticuz/chromium-min@npm:130.0.0"
1123+
dependencies:
1124+
follow-redirects: "npm:^1.15.9"
1125+
tar-fs: "npm:^3.0.6"
1126+
checksum: 10c0/4d52a326b1cf57eef666f1f442fc57a759ce26b9e5198d2c924fd66c466fa6c0096f59cbbc694beee8c1c43057d43a9a5da9cfbd41b5f3c44ba24bc72b386ffa
1127+
languageName: node
1128+
linkType: hard
1129+
11201130
"@types/babel__core@npm:^7.20.5":
11211131
version: 7.20.5
11221132
resolution: "@types/babel__core@npm:7.20.5"
@@ -2291,6 +2301,20 @@ __metadata:
22912301
languageName: node
22922302
linkType: hard
22932303

2304+
"chrome-launcher@npm:^1.1.2":
2305+
version: 1.1.2
2306+
resolution: "chrome-launcher@npm:1.1.2"
2307+
dependencies:
2308+
"@types/node": "npm:*"
2309+
escape-string-regexp: "npm:^4.0.0"
2310+
is-wsl: "npm:^2.2.0"
2311+
lighthouse-logger: "npm:^2.0.1"
2312+
bin:
2313+
print-chrome-path: bin/print-chrome-path.js
2314+
checksum: 10c0/518a6cb846b7187a692c510cc9d3f4d2a87ad3e21cec5eaefb3dcb7ce72ac6ab8b5465cb90510480b9f0b077c8fc340f57e2e078e1d7719aff576595800470b2
2315+
languageName: node
2316+
linkType: hard
2317+
22942318
"ci-info@npm:^4.0.0":
22952319
version: 4.0.0
22962320
resolution: "ci-info@npm:4.0.0"
@@ -2482,6 +2506,15 @@ __metadata:
24822506
languageName: node
24832507
linkType: hard
24842508

2509+
"debug@npm:^2.6.9":
2510+
version: 2.6.9
2511+
resolution: "debug@npm:2.6.9"
2512+
dependencies:
2513+
ms: "npm:2.0.0"
2514+
checksum: 10c0/121908fb839f7801180b69a7e218a40b5a0b718813b886b7d6bdb82001b931c938e2941d1e4450f33a1b1df1da653f5f7a0440c197f29fbf8a6e9d45ff6ef589
2515+
languageName: node
2516+
linkType: hard
2517+
24852518
"decode-named-character-reference@npm:^1.0.0":
24862519
version: 1.0.2
24872520
resolution: "decode-named-character-reference@npm:1.0.2"
@@ -3148,6 +3181,16 @@ __metadata:
31483181
languageName: node
31493182
linkType: hard
31503183

3184+
"follow-redirects@npm:^1.15.9":
3185+
version: 1.15.9
3186+
resolution: "follow-redirects@npm:1.15.9"
3187+
peerDependenciesMeta:
3188+
debug:
3189+
optional: true
3190+
checksum: 10c0/5829165bd112c3c0e82be6c15b1a58fa9dcfaede3b3c54697a82fe4a62dd5ae5e8222956b448d2f98e331525f05d00404aba7d696de9e761ef6e42fdc780244f
3191+
languageName: node
3192+
linkType: hard
3193+
31513194
"foreground-child@npm:^3.1.0":
31523195
version: 3.3.0
31533196
resolution: "foreground-child@npm:3.3.0"
@@ -3683,6 +3726,15 @@ __metadata:
36833726
languageName: node
36843727
linkType: hard
36853728

3729+
"is-docker@npm:^2.0.0":
3730+
version: 2.2.1
3731+
resolution: "is-docker@npm:2.2.1"
3732+
bin:
3733+
is-docker: cli.js
3734+
checksum: 10c0/e828365958d155f90c409cdbe958f64051d99e8aedc2c8c4cd7c89dcf35329daed42f7b99346f7828df013e27deb8f721cf9408ba878c76eb9e8290235fbcdcc
3735+
languageName: node
3736+
linkType: hard
3737+
36863738
"is-docker@npm:^3.0.0":
36873739
version: 3.0.0
36883740
resolution: "is-docker@npm:3.0.0"
@@ -3789,6 +3841,15 @@ __metadata:
37893841
languageName: node
37903842
linkType: hard
37913843

3844+
"is-wsl@npm:^2.2.0":
3845+
version: 2.2.0
3846+
resolution: "is-wsl@npm:2.2.0"
3847+
dependencies:
3848+
is-docker: "npm:^2.0.0"
3849+
checksum: 10c0/a6fa2d370d21be487c0165c7a440d567274fbba1a817f2f0bfa41cc5e3af25041d84267baa22df66696956038a43973e72fca117918c91431920bdef490fa25e
3850+
languageName: node
3851+
linkType: hard
3852+
37923853
"is-wsl@npm:^3.0.0":
37933854
version: 3.1.0
37943855
resolution: "is-wsl@npm:3.1.0"
@@ -3961,6 +4022,16 @@ __metadata:
39614022
languageName: node
39624023
linkType: hard
39634024

4025+
"lighthouse-logger@npm:^2.0.1":
4026+
version: 2.0.1
4027+
resolution: "lighthouse-logger@npm:2.0.1"
4028+
dependencies:
4029+
debug: "npm:^2.6.9"
4030+
marky: "npm:^1.2.2"
4031+
checksum: 10c0/414743d9b1491ad127c78741edfe88bd1c2411b267274c973036b90f56a268c3b8c3e02498bce04b560083da34a149bc3f81d2c47b6c6ad592202354cf781c43
4032+
languageName: node
4033+
linkType: hard
4034+
39644035
"load-yaml-file@npm:^0.2.0":
39654036
version: 0.2.0
39664037
resolution: "load-yaml-file@npm:0.2.0"
@@ -4101,6 +4172,13 @@ __metadata:
41014172
languageName: node
41024173
linkType: hard
41034174

4175+
"marky@npm:^1.2.2":
4176+
version: 1.2.5
4177+
resolution: "marky@npm:1.2.5"
4178+
checksum: 10c0/ca8a011f287dab1ac3291df720fc32b366c4cd767347b63722966650405ce71ec6566f71d1e22e1768bf6461a7fd689b9038e7df0fcfb62eacf3a5a6dcac249e
4179+
languageName: node
4180+
linkType: hard
4181+
41044182
"mdast-util-definitions@npm:^6.0.0":
41054183
version: 6.0.0
41064184
resolution: "mdast-util-definitions@npm:6.0.0"
@@ -4771,6 +4849,13 @@ __metadata:
47714849
languageName: node
47724850
linkType: hard
47734851

4852+
"ms@npm:2.0.0":
4853+
version: 2.0.0
4854+
resolution: "ms@npm:2.0.0"
4855+
checksum: 10c0/f8fda810b39fd7255bbdc451c46286e549794fcc700dc9cd1d25658bbc4dc2563a5de6fe7c60f798a16a60c6ceb53f033cb353f493f0cf63e5199b702943159d
4856+
languageName: node
4857+
linkType: hard
4858+
47744859
"ms@npm:2.1.2":
47754860
version: 2.1.2
47764861
resolution: "ms@npm:2.1.2"
@@ -6054,7 +6139,7 @@ __metadata:
60546139
languageName: node
60556140
linkType: hard
60566141

6057-
"tar-fs@npm:*":
6142+
"tar-fs@npm:*, tar-fs@npm:^3.0.6":
60586143
version: 3.0.6
60596144
resolution: "tar-fs@npm:3.0.6"
60606145
dependencies:
@@ -6128,13 +6213,15 @@ __metadata:
61286213
"@astrojs/vercel": "npm:^7.8.2"
61296214
"@devprotocol/util-ts": "npm:^4.0.0"
61306215
"@eslint/js": "npm:^9.12.0"
6216+
"@sparticuz/chromium-min": "npm:^130.0.0"
61316217
"@types/dotenv": "npm:8.2.0"
61326218
"@types/eslint-config-prettier": "npm:^6.11.3"
61336219
"@types/eslint__js": "npm:^8.42.3"
61346220
"@types/node": "npm:22.7.8"
61356221
"@types/ramda": "npm:0.30.2"
61366222
astro: "npm:^4.16.3"
61376223
chrome-aws-lambda: "npm:^10.1.0"
6224+
chrome-launcher: "npm:^1.1.2"
61386225
dotenv: "npm:16.4.5"
61396226
eslint: "npm:^9.12.0"
61406227
eslint-config-prettier: "npm:^9.1.0"

0 commit comments

Comments
 (0)