Skip to content

Commit

Permalink
feat: migrate to edge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Nov 3, 2023
1 parent 17c3d83 commit f7b3c0e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 75 deletions.
4 changes: 4 additions & 0 deletions api/[vendor]/[app].ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const config = {
runtime: 'edge',
}

export { default } from './[app]/[version].js'
45 changes: 18 additions & 27 deletions api/[vendor]/[app]/[version].ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
import type { VercelRequest, VercelResponse } from '@vercel/node'
import { chromium } from 'playwright'

const BAD_REQUEST = 400

const INTERNAL_SERVER_ERROR = 500

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

export default async (req: VercelRequest, res: VercelResponse) => {
const { vendor, app, version } = req.query as {
vendor: string
app: string
version?: string
}
export const config = {
runtime: 'edge',
}

export default async (req: Request): Promise<Response> => {
const { vendor, app, version } = Object.fromEntries(
new URL(req.url).searchParams,
)

if (vendor !== 'gtimg') {
res.writeHead(BAD_REQUEST)
res.write('Unsupported vendor yet')
return res.end()
return new Response('Unsupported vendor yet', { status: BAD_REQUEST })

Check failure on line 18 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe return of an `any` typed value

Check failure on line 18 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe construction of an any type value
}

if (app !== 'sogou_mac') {
res.writeHead(BAD_REQUEST)
res.write('Unsupported app yet')
return res.end()
return new Response('Unsupported app yet', { status: BAD_REQUEST })

Check failure on line 22 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe return of an `any` typed value

Check failure on line 22 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe construction of an any type value
}

const browser = await chromium.launch()
const page = await browser.newPage()
const result = await page.goto('https://pinyin.sogou.com/mac/')
const text = await result!.text()
const res = await fetch('https://pinyin.sogou.com/mac/')

const text = await res.text()

const matched = text.match(DOWNLOAD_URL_REGEX)

if (!matched) {
res.writeHead(INTERNAL_SERVER_ERROR)
res.write(
return new Response(

Check failure on line 32 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe return of an `any` typed value

Check failure on line 32 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe construction of an any type value
'No download url found, please raise an issue to us: https://github.com/un-ts/rabbit-linker/issues/new',
{ status: INTERNAL_SERVER_ERROR },
)
return res.end()
}

const [url, timestamp, shortVersion] = matched

if (!version || [timestamp, shortVersion].includes(version)) {
return res.redirect(url)
return Response.redirect(url)

Check failure on line 41 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe return of an `any` typed value

Check failure on line 41 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe member access .redirect on an `any` value

Check failure on line 41 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe call of an `any` typed value
}

res.writeHead(BAD_REQUEST)
res.write(
return new Response(

Check failure on line 44 in api/[vendor]/[app]/[version].ts

View workflow job for this annotation

GitHub Actions / Lint and Test with Node.js 18

Unsafe return of an `any` typed value
`No matched version found, it could be not the latest version, maybe you want version \`${shortVersion}\` instead`,
{ status: BAD_REQUEST },
)
res.end()
}
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
"private": true,
"packageManager": "[email protected]",
"scripts": {
"build": "playwright install chromium && tsx scripts/build",
"build": "tsx scripts/build",
"lint": "run-p 'lint:*'",
"lint:es": "eslint . --cache",
"lint:tsc": "tsc --noEmit",
"prepare": "simple-git-hooks",
"start": "vc dev"
},
"dependencies": {
"playwright": "^1.39.0"
},
"devDependencies": {
"@1stg/common-config": "^9.0.0",
"@octokit/request": "^8.1.4",
Expand Down
44 changes: 0 additions & 44 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7422,16 +7422,6 @@ __metadata:
languageName: node
linkType: hard

"fsevents@npm:2.3.2":
version: 2.3.2
resolution: "fsevents@npm:2.3.2"
dependencies:
node-gyp: "npm:latest"
checksum: be78a3efa3e181cda3cf7a4637cb527bcebb0bd0ea0440105a3bb45b86f9245b307dc10a2507e8f4498a7d4ec349d1910f4d73e4d4495b16103106e07eee735b
conditions: os=darwin
languageName: node
linkType: hard

"fsevents@npm:~2.1.2":
version: 2.1.3
resolution: "fsevents@npm:2.1.3"
Expand All @@ -7452,15 +7442,6 @@ __metadata:
languageName: node
linkType: hard

"fsevents@patch:fsevents@npm%3A2.3.2#optional!builtin<compat/fsevents>":
version: 2.3.2
resolution: "fsevents@patch:fsevents@npm%3A2.3.2#optional!builtin<compat/fsevents>::version=2.3.2&hash=df0bf1"
dependencies:
node-gyp: "npm:latest"
conditions: os=darwin
languageName: node
linkType: hard

"fsevents@patch:fsevents@npm%3A~2.1.2#optional!builtin<compat/fsevents>":
version: 2.1.3
resolution: "fsevents@patch:fsevents@npm%3A2.1.3#optional!builtin<compat/fsevents>::version=2.1.3&hash=31d12a"
Expand Down Expand Up @@ -11276,30 +11257,6 @@ __metadata:
languageName: node
linkType: hard

"playwright-core@npm:1.39.0":
version: 1.39.0
resolution: "playwright-core@npm:1.39.0"
bin:
playwright-core: cli.js
checksum: dbd719ae77ae84a43f831beb89514ca5cca62840a2f0cce445645002ac045c256c19b5f4f3cf9a7aa205428a1571e9e8d946ff1937cc316033ea58090c549a76
languageName: node
linkType: hard

"playwright@npm:^1.39.0":
version: 1.39.0
resolution: "playwright@npm:1.39.0"
dependencies:
fsevents: "npm:2.3.2"
playwright-core: "npm:1.39.0"
dependenciesMeta:
fsevents:
optional: true
bin:
playwright: cli.js
checksum: b55adb3453a9c2a02fe61dbcbd5fcb0cbae1038ea6115158c7933d203ae5b62a13cb294905d6661836751a5825bc2cfdc71b67988082ec47ac6930ca744af724
languageName: node
linkType: hard

"pluralize@npm:^8.0.0":
version: 8.0.0
resolution: "pluralize@npm:8.0.0"
Expand Down Expand Up @@ -11588,7 +11545,6 @@ __metadata:
eslint: "npm:^8.52.0"
lint-staged: "npm:^13.3.0"
npm-run-all: "npm:^4.1.5"
playwright: "npm:^1.39.0"
simple-git-hooks: "npm:^2.9.0"
tsx: "npm:^3.14.0"
typescript: "npm:^5.2.2"
Expand Down

0 comments on commit f7b3c0e

Please sign in to comment.