mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
feat(demo): add rolling cross-platform imports
This commit is contained in:
1 parent
e9004ca71f
commit
9e7c76fb89
19 files changed
+859
-174
No files matched your search
@@ -0,0 +1,37 @@
|
||||
const DEMO_ORIGIN = 'https://chatlab.fun/assets/demo'
|
||||
const DEMO_LOCALES = new Set(['cn', 'en'])
|
||||
const DEMO_FILES = new Set([
|
||||
'demo-group.json',
|
||||
'demo-private-A-cuilan.json',
|
||||
'demo-private-B-wukong.json',
|
||||
'demo-private-C-spider.json',
|
||||
])
|
||||
|
||||
export async function proxyDemoRequest(context, fetchRemote = fetch) {
|
||||
const rawPath = Array.isArray(context.params.path) ? context.params.path.join('/') : String(context.params.path || '')
|
||||
const [locale, filename, ...extra] = rawPath.split('/')
|
||||
if (extra.length > 0 || !DEMO_LOCALES.has(locale) || !DEMO_FILES.has(filename)) {
|
||||
return new Response('Not found', { status: 404 })
|
||||
}
|
||||
|
||||
try {
|
||||
const upstream = await fetchRemote(`${DEMO_ORIGIN}/${locale}/${filename}`)
|
||||
if (!upstream.ok || !upstream.body) {
|
||||
return new Response('Demo asset unavailable', { status: upstream.status || 502 })
|
||||
}
|
||||
|
||||
return new Response(upstream.body, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': upstream.headers.get('content-type') || 'application/json',
|
||||
'Cache-Control': 'public, max-age=86400',
|
||||
},
|
||||
})
|
||||
} catch {
|
||||
return new Response('Demo asset unavailable', { status: 502 })
|
||||
}
|
||||
}
|
||||
|
||||
export function onRequestGet(context) {
|
||||
return proxyDemoRequest(context)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { describe, it } from 'node:test'
|
||||
import { proxyDemoRequest } from './[[path]].js'
|
||||
|
||||
describe('Web WASM demo asset proxy', () => {
|
||||
it('only proxies known localized demo files', async () => {
|
||||
const urls = []
|
||||
const response = await proxyDemoRequest({ params: { path: 'cn/demo-group.json' } }, async (url) => {
|
||||
urls.push(url)
|
||||
return new Response('demo content', {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
})
|
||||
})
|
||||
|
||||
assert.equal(response.status, 200)
|
||||
assert.equal(response.headers.get('content-type'), 'application/json')
|
||||
assert.equal(await response.text(), 'demo content')
|
||||
assert.deepEqual(urls, ['https://chatlab.fun/assets/demo/cn/demo-group.json'])
|
||||
})
|
||||
|
||||
it('rejects unknown paths before making a remote request', async () => {
|
||||
let requested = false
|
||||
const response = await proxyDemoRequest({ params: { path: 'cn/../../secret.json' } }, async () => {
|
||||
requested = true
|
||||
return new Response()
|
||||
})
|
||||
|
||||
assert.equal(response.status, 404)
|
||||
assert.equal(requested, false)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user