-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
23 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export { default } from './[app]/[version].js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Lint and Test with Node.js 18
|
||
} | ||
|
||
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 GitHub Actions / Lint and Test with Node.js 18
|
||
} | ||
|
||
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 GitHub Actions / Lint and Test with Node.js 18
|
||
'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 GitHub Actions / Lint and Test with Node.js 18
Check failure on line 41 in api/[vendor]/[app]/[version].ts GitHub Actions / Lint and Test with Node.js 18
|
||
} | ||
|
||
res.writeHead(BAD_REQUEST) | ||
res.write( | ||
return new Response( | ||
`No matched version found, it could be not the latest version, maybe you want version \`${shortVersion}\` instead`, | ||
{ status: BAD_REQUEST }, | ||
) | ||
res.end() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters