Skip to content

Commit b8c7e24

Browse files
committed
feat: migrate to edge functions
1 parent 17c3d83 commit b8c7e24

File tree

5 files changed

+26
-75
lines changed

5 files changed

+26
-75
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
node-version: ${{ matrix.node }}
2424
cache: yarn
2525

26+
- name: Enable Corepack
27+
run: corepack enable
28+
2629
- name: Install Dependencies
2730
run: yarn --immutable
2831

api/[vendor]/[app].ts

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
export const config = {
2+
runtime: 'edge',
3+
}
4+
15
export { default } from './[app]/[version].js'

api/[vendor]/[app]/[version].ts

+18-27
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
1-
import type { VercelRequest, VercelResponse } from '@vercel/node'
2-
import { chromium } from 'playwright'
3-
41
const BAD_REQUEST = 400
52

63
const INTERNAL_SERVER_ERROR = 500
74

8-
// https://ime-sec.gtimg.com/202311032255/c2d34d57b6d7f9fc6b7869423e8576c2/pc/dl/gzindex/1688992049/sogou_mac_614d.zip
95
const DOWNLOAD_URL_REGEX =
106
/https:\/\/ime-sec\.gtimg.com\/\d+\/\w+\/pc\/dl\/gzindex\/(\d+)\/sogou_mac_(\w+)\.zip/
117

12-
export default async (req: VercelRequest, res: VercelResponse) => {
13-
const { vendor, app, version } = req.query as {
14-
vendor: string
15-
app: string
16-
version?: string
17-
}
8+
export const config = {
9+
runtime: 'edge',
10+
}
11+
12+
export default async (req: Request): Promise<Response> => {
13+
const { vendor, app, version } = Object.fromEntries(
14+
new URL(req.url).searchParams,
15+
)
1816

1917
if (vendor !== 'gtimg') {
20-
res.writeHead(BAD_REQUEST)
21-
res.write('Unsupported vendor yet')
22-
return res.end()
18+
return new Response('Unsupported vendor yet', { status: BAD_REQUEST })
2319
}
2420

2521
if (app !== 'sogou_mac') {
26-
res.writeHead(BAD_REQUEST)
27-
res.write('Unsupported app yet')
28-
return res.end()
22+
return new Response('Unsupported app yet', { status: BAD_REQUEST })
2923
}
3024

31-
const browser = await chromium.launch()
32-
const page = await browser.newPage()
33-
const result = await page.goto('https://pinyin.sogou.com/mac/')
34-
const text = await result!.text()
25+
const res = await fetch('https://pinyin.sogou.com/mac/')
26+
27+
const text = await res.text()
3528

3629
const matched = text.match(DOWNLOAD_URL_REGEX)
3730

3831
if (!matched) {
39-
res.writeHead(INTERNAL_SERVER_ERROR)
40-
res.write(
32+
return new Response(
4133
'No download url found, please raise an issue to us: https://github.com/un-ts/rabbit-linker/issues/new',
34+
{ status: INTERNAL_SERVER_ERROR },
4235
)
43-
return res.end()
4436
}
4537

4638
const [url, timestamp, shortVersion] = matched
4739

4840
if (!version || [timestamp, shortVersion].includes(version)) {
49-
return res.redirect(url)
41+
return Response.redirect(url)
5042
}
5143

52-
res.writeHead(BAD_REQUEST)
53-
res.write(
44+
return new Response(
5445
`No matched version found, it could be not the latest version, maybe you want version \`${shortVersion}\` instead`,
46+
{ status: BAD_REQUEST },
5547
)
56-
res.end()
5748
}

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
"private": true,
1010
"packageManager": "[email protected]",
1111
"scripts": {
12-
"build": "playwright install chromium && tsx scripts/build",
12+
"build": "tsx scripts/build",
1313
"lint": "run-p 'lint:*'",
1414
"lint:es": "eslint . --cache",
1515
"lint:tsc": "tsc --noEmit",
1616
"prepare": "simple-git-hooks",
1717
"start": "vc dev"
1818
},
19-
"dependencies": {
20-
"playwright": "^1.39.0"
21-
},
2219
"devDependencies": {
2320
"@1stg/common-config": "^9.0.0",
2421
"@octokit/request": "^8.1.4",

yarn.lock

-44
Original file line numberDiff line numberDiff line change
@@ -7422,16 +7422,6 @@ __metadata:
74227422
languageName: node
74237423
linkType: hard
74247424

7425-
"fsevents@npm:2.3.2":
7426-
version: 2.3.2
7427-
resolution: "fsevents@npm:2.3.2"
7428-
dependencies:
7429-
node-gyp: "npm:latest"
7430-
checksum: be78a3efa3e181cda3cf7a4637cb527bcebb0bd0ea0440105a3bb45b86f9245b307dc10a2507e8f4498a7d4ec349d1910f4d73e4d4495b16103106e07eee735b
7431-
conditions: os=darwin
7432-
languageName: node
7433-
linkType: hard
7434-
74357425
"fsevents@npm:~2.1.2":
74367426
version: 2.1.3
74377427
resolution: "fsevents@npm:2.1.3"
@@ -7452,15 +7442,6 @@ __metadata:
74527442
languageName: node
74537443
linkType: hard
74547444

7455-
"fsevents@patch:fsevents@npm%3A2.3.2#optional!builtin<compat/fsevents>":
7456-
version: 2.3.2
7457-
resolution: "fsevents@patch:fsevents@npm%3A2.3.2#optional!builtin<compat/fsevents>::version=2.3.2&hash=df0bf1"
7458-
dependencies:
7459-
node-gyp: "npm:latest"
7460-
conditions: os=darwin
7461-
languageName: node
7462-
linkType: hard
7463-
74647445
"fsevents@patch:fsevents@npm%3A~2.1.2#optional!builtin<compat/fsevents>":
74657446
version: 2.1.3
74667447
resolution: "fsevents@patch:fsevents@npm%3A2.1.3#optional!builtin<compat/fsevents>::version=2.1.3&hash=31d12a"
@@ -11276,30 +11257,6 @@ __metadata:
1127611257
languageName: node
1127711258
linkType: hard
1127811259

11279-
"playwright-core@npm:1.39.0":
11280-
version: 1.39.0
11281-
resolution: "playwright-core@npm:1.39.0"
11282-
bin:
11283-
playwright-core: cli.js
11284-
checksum: dbd719ae77ae84a43f831beb89514ca5cca62840a2f0cce445645002ac045c256c19b5f4f3cf9a7aa205428a1571e9e8d946ff1937cc316033ea58090c549a76
11285-
languageName: node
11286-
linkType: hard
11287-
11288-
"playwright@npm:^1.39.0":
11289-
version: 1.39.0
11290-
resolution: "playwright@npm:1.39.0"
11291-
dependencies:
11292-
fsevents: "npm:2.3.2"
11293-
playwright-core: "npm:1.39.0"
11294-
dependenciesMeta:
11295-
fsevents:
11296-
optional: true
11297-
bin:
11298-
playwright: cli.js
11299-
checksum: b55adb3453a9c2a02fe61dbcbd5fcb0cbae1038ea6115158c7933d203ae5b62a13cb294905d6661836751a5825bc2cfdc71b67988082ec47ac6930ca744af724
11300-
languageName: node
11301-
linkType: hard
11302-
1130311260
"pluralize@npm:^8.0.0":
1130411261
version: 8.0.0
1130511262
resolution: "pluralize@npm:8.0.0"
@@ -11588,7 +11545,6 @@ __metadata:
1158811545
eslint: "npm:^8.52.0"
1158911546
lint-staged: "npm:^13.3.0"
1159011547
npm-run-all: "npm:^4.1.5"
11591-
playwright: "npm:^1.39.0"
1159211548
simple-git-hooks: "npm:^2.9.0"
1159311549
tsx: "npm:^3.14.0"
1159411550
typescript: "npm:^5.2.2"

0 commit comments

Comments
 (0)