加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
endpoint.worker.js 735 Bytes
一键复制 编辑 原始数据 按行查看 历史
nondanee 提交于 2020-05-27 01:25 . provide serverless endpoint script
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const pattern = /^\/package\/([0-9a-zA-Z_\-=]+)\/(\w+\.\w+)$/
const handleRequest = async request => {
const notFound = new Response(null, { status: 404 })
const path = new URL(request.url).pathname
const [matched, base64Url, fileName] = pattern.exec(path || '') || []
if (!matched) return notFound
let url = base64Url.replace(/-/g, '+').replace(/_/g, '/')
try { url = new URL(atob(url)) } catch(_) { url = null }
if (!url) return notFound
const headers = new Headers(request.headers)
headers.set('host', url.host)
headers.delete('cookie')
const { method, body } = request
return fetch(url, { method, headers, body })
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化