/
sandbox.ts
39 lines (35 loc) · 1 KB
/
sandbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { RequestData, FetchEventResult } from '../types'
import { getModuleContext } from './context'
export async function run(params: {
name: string
env: string[]
onWarning: (warn: Error) => void
paths: string[]
request: RequestData
useCache: boolean
}): Promise<FetchEventResult> {
const { runInContext, context } = getModuleContext({
module: params.name,
onWarning: params.onWarning,
useCache: params.useCache !== false,
env: params.env,
})
for (const paramPath of params.paths) {
runInContext(paramPath)
}
const subreq = params.request.headers[`x-middleware-subrequest`]
const subrequests = typeof subreq === 'string' ? subreq.split(':') : []
if (subrequests.includes(params.name)) {
return {
waitUntil: Promise.resolve(),
response: new context.Response(null, {
headers: {
'x-middleware-next': '1',
},
}),
}
}
return context._ENTRIES[`middleware_${params.name}`].default({
request: params.request,
})
}