@@ -15,7 +15,7 @@ import { resolveConfig } from './utils/config.js';
15
15
import handlers from './routes/index.js' ;
16
16
17
17
/**
18
- * @param {Request } req
18
+ * @param {import("@cloudflare/workers-types/experimental"). Request } req
19
19
*/
20
20
async function parseData ( req ) {
21
21
if ( [ 'GET' , 'HEAD' , 'OPTIONS' ] . includes ( req . method ) ) {
@@ -40,22 +40,27 @@ async function parseData(req) {
40
40
}
41
41
42
42
/**
43
- * @param {import("@cloudflare/workers-types/experimental").ExecutionContext } pctx
44
- * @param {Request } req
43
+ * @param {import("@cloudflare/workers-types/experimental").ExecutionContext } eCtx
44
+ * @param {import("@cloudflare/workers-types/experimental"). Request } req
45
45
* @param {Env } env
46
46
* @returns {Promise<Context> }
47
47
*/
48
- export async function makeContext ( pctx , req , env ) {
48
+ export async function makeContext ( eCtx , req , env ) {
49
49
/** @type {Context } */
50
50
// @ts -ignore
51
- const ctx = pctx ;
51
+ const ctx = {
52
+ executionContext : eCtx ,
53
+ } ;
52
54
// @ts -ignore
53
55
ctx . attributes = { } ;
54
56
ctx . env = env ;
55
57
ctx . url = new URL ( req . url ) ;
56
58
ctx . log = console ;
59
+ const filename = ctx . url . pathname . split ( '/' ) . pop ( ) ?? '' ;
57
60
ctx . info = {
61
+ filename,
58
62
method : req . method . toUpperCase ( ) ,
63
+ extension : filename . split ( '.' ) . pop ( ) ,
59
64
headers : Object . fromEntries (
60
65
[ ...req . headers . entries ( ) ]
61
66
. map ( ( [ k , v ] ) => [ k . toLowerCase ( ) , v ] ) ,
@@ -67,24 +72,28 @@ export async function makeContext(pctx, req, env) {
67
72
68
73
export default {
69
74
/**
70
- * @param {Request } request
75
+ * @param {import("@cloudflare/workers-types/experimental"). Request } request
71
76
* @param {Env } env
72
- * @param {import("@cloudflare/workers-types/experimental").ExecutionContext } pctx
77
+ * @param {import("@cloudflare/workers-types/experimental").ExecutionContext } eCtx
73
78
* @returns {Promise<Response> }
74
79
*/
75
- async fetch ( request , env , pctx ) {
76
- const ctx = await makeContext ( pctx , request , env ) ;
80
+ async fetch ( request , env , eCtx ) {
81
+ const ctx = await makeContext ( eCtx , request , env ) ;
77
82
78
83
try {
79
84
const overrides = Object . fromEntries ( ctx . url . searchParams . entries ( ) ) ;
80
85
ctx . config = await resolveConfig ( ctx , overrides ) ;
81
86
82
- console . debug ( 'resolved config: ' , JSON . stringify ( ctx . config ) ) ;
87
+ console . debug ( 'resolved config: ' , JSON . stringify ( ctx . config , null , 2 ) ) ;
83
88
if ( ! ctx . config ) {
84
89
return errorResponse ( 404 , 'config not found' ) ;
85
90
}
86
91
87
- return await handlers [ ctx . config . route ] ( ctx , request ) ;
92
+ const fn = handlers [ ctx . config . route ] ;
93
+ if ( ! fn ) {
94
+ return errorResponse ( 404 , 'route not found' ) ;
95
+ }
96
+ return await fn ( ctx , request ) ;
88
97
} catch ( e ) {
89
98
if ( e . response ) {
90
99
return e . response ;
0 commit comments