-
Notifications
You must be signed in to change notification settings - Fork 838
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
13 changed files
with
747 additions
and
81 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
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
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,16 +1,16 @@ | ||
{ | ||
"url": "https://api.dictionaryapi.dev/api/v2/entries/en/{{DATA}}", | ||
"method": "GET", | ||
"response": { | ||
"content": { | ||
"input_type": "json", | ||
"output_type": "html", | ||
"output": "{{#each word in .}}\n<b>{{word.word}}</b>{{#if word.phonetic}}<i>{{word.phonetic}}</i>{{/if}}\n{{#each:word meanings in word.meanings}}\n <b>+ {{meanings.partOfSpeech}}</b>\n {{#each:meanings definitions in meanings.definitions}}\n <i>{{definitions.definition}}</i>\n {{/each:meanings}}\n{{/each:word}}\n{{/each}}\n" | ||
}, | ||
"error": { | ||
"input_type": "json", | ||
"output_type": "text", | ||
"output": "Error: {{message}}" | ||
"url": "https://api.dictionaryapi.dev/api/v2/entries/en/{{DATA}}", | ||
"method": "GET", | ||
"response": { | ||
"content": { | ||
"input_type": "json", | ||
"output_type": "html", | ||
"output": "{{#each word in .}}\n<b>{{word.word}}</b>{{#if word.phonetic}}<i>{{word.phonetic}}</i>{{/if}}\n{{#each:word meanings in word.meanings}}\n <b>+ {{meanings.partOfSpeech}}</b>\n {{#each:meanings definitions in meanings.definitions}}\n <i>{{definitions.definition}}</i>\n {{/each:meanings}}\n{{/each:word}}\n{{/each}}\n" | ||
}, | ||
"error": { | ||
"input_type": "json", | ||
"output_type": "text", | ||
"output": "Error: {{message}}" | ||
} | ||
} | ||
} | ||
} |
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,26 +1,26 @@ | ||
{ | ||
"url": "https://cloudflare-dns.com/dns-query", | ||
"method": "GET", | ||
"headers": { | ||
"accept": "application/dns-json" | ||
}, | ||
"input": { | ||
"type": "space-separated" | ||
}, | ||
"query": { | ||
"type": "{{DATA[0]}}", | ||
"name": "{{DATA[1]}}" | ||
}, | ||
"response": { | ||
"content": { | ||
"input_type": "json", | ||
"output_type": "html", | ||
"output": "\n<b>DNS query: {{Question[0].name}}</b>\n<code>Status: {{#if TC}}TC,{{/if}}{{#if RD}}RD,{{/if}}{{#if RA}}RA,{{/if}}{{#if AD}}AD,{{/if}}{{#if CD}}CD,{{/if}}{{Status}}</code>\n\n<b>Answer</b>{{#each answer in Answer}}\n<code>{{answer.name}}, {{answer.type}}, (TTL: {{answer.TTL}}),{{answer.data}}</code>{{/each}}\n" | ||
"url": "https://cloudflare-dns.com/dns-query", | ||
"method": "GET", | ||
"headers": { | ||
"accept": "application/dns-json" | ||
}, | ||
"error": { | ||
"input_type": "json", | ||
"output_type": "text", | ||
"output": "Error: {{error}}" | ||
"input": { | ||
"type": "space-separated" | ||
}, | ||
"query": { | ||
"type": "{{DATA[0]}}", | ||
"name": "{{DATA[1]}}" | ||
}, | ||
"response": { | ||
"content": { | ||
"input_type": "json", | ||
"output_type": "html", | ||
"output": "\n<b>DNS query: {{Question[0].name}}</b>\n<code>Status: {{#if TC}}TC,{{/if}}{{#if RD}}RD,{{/if}}{{#if RA}}RA,{{/if}}{{#if AD}}AD,{{/if}}{{#if CD}}CD,{{/if}}{{Status}}</code>\n\n<b>Answer</b>{{#each answer in Answer}}\n<code>{{answer.name}}, {{answer.type}}, (TTL: {{answer.TTL}}),{{answer.data}}</code>{{/each}}\n" | ||
}, | ||
"error": { | ||
"input_type": "json", | ||
"output_type": "text", | ||
"output": "Error: {{error}}" | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fs from 'node:fs/promises'; | ||
import { execSync } from 'node:child_process'; | ||
import { parse } from 'toml'; | ||
|
||
export function createVercelPlugin(vercelPath: string, tomlPath: string) { | ||
return { | ||
name: 'vercel', | ||
async closeBundle() { | ||
const { vars } = parse(await fs.readFile(tomlPath, 'utf-8')); | ||
for (const [key, value] of Object.entries(vars)) { | ||
execSync(`${vercelPath} env add ${key} production --force`, { | ||
input: `${value}`, | ||
encoding: 'utf-8', | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as process from 'node:process'; | ||
import { createVercelPlugin } from './index'; | ||
|
||
const { | ||
VERCEL_PATH = 'vercel', | ||
TOML_PATH = 'wrangler.toml', | ||
} = process.env; | ||
|
||
const plugin = createVercelPlugin(VERCEL_PATH, TOML_PATH); | ||
plugin.closeBundle().catch(console.error); |
File renamed without changes.
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as process from 'node:process'; | ||
import { RedisCache } from 'cloudflare-worker-adapter/cache/redis'; | ||
import type { VercelRequest, VercelResponse } from '@vercel/node'; | ||
import { ENV } from '../../config/env'; | ||
import { createRouter } from '../../route'; | ||
|
||
export default async function (request: VercelRequest, response: VercelResponse) { | ||
let redis: RedisCache | null = null; | ||
try { | ||
const { | ||
VERCEL_DOMAIN, | ||
REDIS_URL, | ||
} = process.env; | ||
if (!VERCEL_DOMAIN) { | ||
response.status(500).send('VERCEL_DOMAIN is required'); | ||
return; | ||
} | ||
if (!REDIS_URL) { | ||
response.status(500).send('REDIS_URL is required'); | ||
return; | ||
} | ||
const cache = RedisCache.createFromUri(REDIS_URL); | ||
// edge function 使用 redis 作为数据库容易出现连接数过多的问题,此处仅作为演示,请自行实现 `Cache` 接口 | ||
redis = cache; | ||
ENV.merge({ | ||
...process.env, | ||
DATABASE: cache, | ||
}); | ||
const router = createRouter(); | ||
let body: BodyInit | null = null; | ||
if (request.body) { | ||
body = JSON.stringify(request.body); | ||
} | ||
const newReq = new Request(VERCEL_DOMAIN + request.url, { | ||
method: request.method, | ||
headers: Object.entries(request.headers).reduce((acc, [key, value]) => { | ||
if (value === undefined) { | ||
return acc; | ||
} | ||
if (Array.isArray(value)) { | ||
for (const v of value) { | ||
acc.append(key, v); | ||
} | ||
return acc; | ||
} | ||
acc.set(key, value); | ||
return acc; | ||
}, new Headers()), | ||
body, | ||
}); | ||
const res = await router.fetch(newReq); | ||
if (redis) { | ||
await redis.close().catch(console.error); | ||
redis = null; | ||
} | ||
response.status(res.status).send(await res.text()); | ||
} catch (e) { | ||
if (redis) { | ||
await redis.close(); | ||
} | ||
response.status(500).send(JSON.stringify({ | ||
message: (e as Error).message, | ||
stack: (e as Error).stack, | ||
}, null, 2)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"builds": [ | ||
{ "src": "dist/**", "use": "@vercel/node" } | ||
], | ||
"routes": [ | ||
{ "src": "/(.*)", "dest": "/dist" } | ||
] | ||
} |
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
Oops, something went wrong.