Files
image-workbench-desktop/functions/plugins/[[path]].js
T

75 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// /plugins/* → 从 KV(binding: PLUGINS)以**同源**方式提供插件文件。
// 同源脚本本就被页面 CSP(script-src 'self')允许 ⇒ 无需放宽 CSP、无需 unsafe-eval、无需重新部署 app.js。
// KV 键约定:plugins/index.json(清单)+ plugins/<file>(每个插件一个文件)。
//
// 缓存策略(按 basename 分流):
// - plugin-<id>.<hash12>.js(内容寻址,发布脚本生成)→ public, max-age=31536000, immutable(字节永不变 ⇒ 可永久缓存)
// - 其他 .js(历史遗留未哈希文件)→ no-cache + ETag(安全网:绝不许被永久缓存)
// - .json(清单)→ no-cache + ETag;if-none-match 命中 → 304 空 body
// - 其他扩展名 / 404 / 500 → no-store
// ETag 为 (value, basename) 的确定性纯函数:FNV-1a 32bit(同步、廉价),hex 后加双引号。
const JS_CT = 'application/javascript; charset=utf-8'
const JSON_CT = 'application/json; charset=utf-8'
// 安全头(2026-10-20 加,H1):Cloudflare Pages 的 `_headers` **不作用于 Functions 响应**
// (https://developers.cloudflare.com/pages/configuration/headers/)⇒ 静态资源有 CSP/nosniff,
// 而本函数原本**一个安全头都没有**。插件字节虽同源且有 SRI 校验,但缺 nosniff 意味着
// 万一将来 KV 里的内容类型异常,浏览器可能自行嗅探 ⇒ 补齐纵深防御。
const SECURITY_HEADERS = { 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer' }
const ERROR_HEADERS = Object.assign({ 'content-type': JSON_CT, 'cache-control': 'no-store' }, SECURITY_HEADERS)
const NO_STORE_HEADERS = Object.assign({ 'content-type': JS_CT, 'cache-control': 'no-store' }, SECURITY_HEADERS)
const IMMUTABLE_JS_RE = /^plugin-[a-z0-9-]+\.[0-9a-f]{12}\.js$/
// FNV-1a 32-bit(对 UTF-16 code unit 序列):同步、确定、够用于 ETag 指纹
const fnv1a32 = (s) => {
let h = 0x811c9dc5
for (let i = 0; i < s.length; i++) {
h ^= s.charCodeAt(i)
h = Math.imul(h, 0x01000193)
}
return (h >>> 0).toString(16).padStart(8, '0')
}
const etagOf = (value, base) => '"' + fnv1a32(base + '\n' + value) + '"'
export async function onRequestGet(context) {
const { params, env } = context
const parts = Array.isArray(params.path) ? params.path : [params.path].filter(Boolean)
const tail = parts.join('/') || 'index.json'
const key = 'plugins/' + tail
const base = tail.slice(tail.lastIndexOf('/') + 1)
if (!env || !env.PLUGINS) {
return new Response(JSON.stringify({ error: 'PLUGINS KV 未绑定' }), { status: 500, headers: ERROR_HEADERS })
}
const value = await env.PLUGINS.get(key)
if (value === null) return new Response('not found', { status: 404, headers: Object.assign({ 'cache-control': 'no-store' }, SECURITY_HEADERS) })
// A) 内容寻址的插件 JS:内容变 ⇒ 文件名变 ⇒ 可 immutable 永久缓存
if (IMMUTABLE_JS_RE.test(base)) {
return new Response(value, {
headers: Object.assign({ 'content-type': JS_CT, 'cache-control': 'public, max-age=31536000, immutable' }, SECURITY_HEADERS),
})
}
// B) 未哈希 .js 与 .json(清单):no-cache + ETag,每次回源校验,命中即 304
if (base.endsWith('.js') || base.endsWith('.json')) {
const etag = etagOf(value, base)
const headers = Object.assign({
'content-type': base.endsWith('.json') ? JSON_CT : JS_CT,
'cache-control': 'no-cache',
etag,
}, SECURITY_HEADERS)
const inm = context.request && context.request.headers && context.request.headers.get('if-none-match')
if (inm === etag) return new Response(null, { status: 304, headers })
return new Response(value, { headers })
}
// 其他扩展名:保持 no-store
return new Response(value, { headers: NO_STORE_HEADERS })
}