Skip to content

Commit 90afd5c

Browse files
authored
feat: add vercel support (#6)
* feat: add vercel support * . * feat: update Vercel build process and API handling * . * . * . * fix: return result as ArrayBuffer in synthesis API * docs: update README.md with Vercel deployment instructions and enhance api/index.mjs with entry file overview for Vercel Functions
1 parent e759e26 commit 90afd5c

File tree

9 files changed

+43
-4
lines changed

9 files changed

+43
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ dist-node
99
dist-prebuild
1010
dist-worker
1111

12-
.wrangler
12+
.wrangler
13+
.vercel
14+
.env*.local

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
TOKEN=YOUR_TOKEN # Optional
2323
```
2424

25+
### Vercel
26+
27+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fyy4382%2Fread-aloud&env=TOKEN&envDescription=%E7%B1%BB%E4%BC%BC%E4%BA%8E%E5%AF%86%E7%A0%81%EF%BC%8C%E9%98%B2%E6%AD%A2%E4%BB%96%E4%BA%BA%E6%BB%A5%E7%94%A8%E4%BD%A0%E7%9A%84%E6%9C%8D%E5%8A%A1&project-name=read-aloud&repository-name=read-aloud)
28+
29+
框架(Framework) 选择 Other,几个 Command 都保持不动(不要 Override)。
30+
31+
环境变量 TOKEN 设置为字母、数字、下划线和连字符的组合(其他特殊字符未被测试是否可用)。
32+
2533
### Node.js (Docker)
2634

2735
```bash
@@ -49,3 +57,11 @@ Swagger 文档:<https://ra.yfi.moe/api/ui>
4957
Deploy to Cloudflare 按钮默认使用 package.json 中 deploy 脚本进行部署。
5058

5159
流程:tsup 打包到 dist-prebuild 目录(`pnpm run prebuild:worker`)(由 wrangler.toml 中的 build 参数运行该命令),然后 wrangler deploy 将 `dist-prebuild/workerd.mjs`(由 wrangler.toml 中的 main 参数指定该文件)部署到 Cloudflare Workers。
60+
61+
### Vercel
62+
63+
使用了 [Custom build step for Node.js](https://vercel.com/docs/functions/runtimes/node-js/advanced-node-configuration#custom-build-step-for-node.js) 方式
64+
65+
通过 `package.json` 中的 `vercel-build` 脚本,Vercel CLI 会执行该命令进行构建,之后 `/api/index.mjs` 会导入构建结果。
66+
67+
同时,依赖 vercel.json 中的 rewrite 配置,将所有 /api 请求重写到 `api/index.mjs` 文件。

api/index.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @fileoverview 用于 Vercel Functions 的入口文件
3+
*
4+
* 使用 Vercel Functions 时,Vercel CLI 会执行 `package.json` 中的 `vercel-build` 脚本进行构建,
5+
* 当前文件会导入构建结果,并导出为 Vercel Functions 的入口文件。
6+
*
7+
* 同时,依赖 vercel.json 中的 rewrite 配置,将所有 /api 请求重写到这个文件。
8+
*/
9+
10+
import app from "../dist-prebuild/workerd.mjs";
11+
12+
export default app;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"dev:worker": "wrangler dev",
99
"deploy": "wrangler deploy --minify --outdir=dist-worker",
1010
"dev:node": "tsup-node --watch --onSuccess \"node dist-node/node.mjs\"",
11-
"build:node": "tsup-node"
11+
"build:node": "tsup-node",
12+
"build:vercel": "tsup --outDir=dist-prebuild entries/workerd.ts",
13+
"vercel-build": "pnpm run build:vercel"
1214
},
1315
"keywords": [],
1416
"author": "",

public/robots.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
User-agent: *
2+
Disallow: /

src/api/synthesis.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ synthesis.openapi(route, async (c) => {
113113
},
114114
);
115115
c.header("Content-Type", FORMAT_CONTENT_TYPE.get(format));
116-
return c.body(result);
116+
return c.body(result.buffer as ArrayBuffer);
117117
} catch (error) {
118118
if (error instanceof HTTPException) throw error;
119119
c.status(500);

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export default app;
1515

1616
app.route("/api", api);
1717

18-
app.get("/robots.txt", (c) => c.text("User-agent: *\nDisallow: /"));
1918
app.get("/api/ui", swaggerUI({ url: "/api/doc" }));
2019
app.doc("/api/doc", {
2120
openapi: "3.0.0",

src/utils/synthesis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ function buf2hex(buffer: ArrayBuffer) {
144144
.join("");
145145
}
146146

147+
// provided by and modified from @rexshao
148+
// https://github.com/yy4382/read-aloud/issues/4#issue-3048109976
147149
async function getURL() {
148150
const connectionId = randomUUID().toLowerCase();
149151
const TRUSTED_CLIENT_TOKEN = "6A5AA1D4EAFF4E9FB37E23D68491D6F4";

vercel.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"$schema": "https://openapi.vercel.sh/vercel.json",
3+
"rewrites": [{ "source": "/api/(.*)", "destination": "/api" }]
4+
}

0 commit comments

Comments
 (0)