1
+ // deno-lint-ignore-file require-await
2
+ import { runTest , type TestInput } from "@regexplanet/common" ;
3
+
1
4
async function serveStaticFile (
2
5
path : string ,
3
6
contentType : string ,
4
- ) : Promise < ( req : Request ) => Response > {
7
+ ) {
5
8
const data = await Deno . readFile ( path ) ;
6
- return ( _req : Request ) => {
9
+ return async ( _req : Request ) : Promise < Response > => {
7
10
return new Response ( data , {
8
11
headers : {
9
12
"content-type" : contentType ,
@@ -12,15 +15,21 @@ async function serveStaticFile(
12
15
} ;
13
16
}
14
17
15
- const routeMap = new Map ( [
16
- [ "/" , root ] ,
17
- [ "/robots.txt" , await serveStaticFile ( "static/robots.txt" , "text/css" ) ] ,
18
- [ "/favicon.ico" , await serveStaticFile ( "static/favicon.ico" , "image/x-icon" ) ] ,
19
- [
20
- "/favicon.svg" ,
21
- await serveStaticFile ( "static/favicon.svg" , "image/svg+xml" ) ,
22
- ] ,
23
- [ "/status.json" , status ] ,
18
+
19
+ const routeMap : Map < string , ( req : Request ) => Promise < Response > > = new Map ( [
20
+ [ "/" , root ] ,
21
+ [ "/robots.txt" , await serveStaticFile ( "static/robots.txt" , "text/css" ) ] ,
22
+ [
23
+ "/favicon.ico" ,
24
+ await serveStaticFile ( "static/favicon.ico" , "image/x-icon" ) ,
25
+ ] ,
26
+ [
27
+ "/favicon.svg" ,
28
+ await serveStaticFile ( "static/favicon.svg" , "image/svg+xml" ) ,
29
+ ] ,
30
+ [ "/status.json" , status ] ,
31
+
32
+ [ "/test.json" , testJson ] ,
24
33
] ) ;
25
34
26
35
function handleJsonp ( req : Request , data : object ) : Response {
@@ -39,22 +48,31 @@ function handleJsonp(req: Request, data: object): Response {
39
48
} ) ;
40
49
}
41
50
42
- function handler ( req : Request ) : Response {
51
+ async function handler ( req : Request ) {
43
52
const url = new URL ( req . url , `http://${ req . headers . get ( "host" ) } ` ) ;
44
53
const path = url . pathname ;
45
54
const fn = routeMap . get ( path ) ;
46
55
if ( fn ) {
47
56
return fn ( req ) ;
48
57
}
49
58
console . log ( `WARNING: Not Found: ${ path } ` ) ;
50
- return new Response ( "Not Found" , { status : 404 } ) ;
59
+ if ( path . endsWith ( ".json" ) ) {
60
+ return handleJsonp ( req , {
61
+ success : false ,
62
+ code : "ENOTFOUND" ,
63
+ message : "404 File not found" ,
64
+ statusCode : 404 ,
65
+ path,
66
+ } ) ;
67
+ }
68
+ return new Response ( `404: ${ path } not found` , { status : 404 } ) ;
51
69
}
52
70
53
- function root ( _req : Request ) : Response {
54
- return new Response ( " Running!" ) ;
71
+ async function root ( _req : Request ) : Promise < Response > {
72
+ return new Response ( ` Running Deno v ${ Deno . version . deno } ` ) ;
55
73
}
56
74
57
- function status ( req : Request ) : Response {
75
+ async function status ( req : Request ) : Promise < Response > {
58
76
const data = {
59
77
success : true ,
60
78
version : `${ Deno . version . deno } (v8 ${ Deno . version . v8 } )` ,
@@ -66,6 +84,45 @@ function status(req: Request): Response {
66
84
return handleJsonp ( req , data ) ;
67
85
}
68
86
87
+ async function testJson ( req : Request ) {
88
+ let testInput : TestInput ;
89
+
90
+ if ( req . method === "POST" ) {
91
+ if ( req . headers . get ( "content-type" ) === "application/json" ) {
92
+ testInput = await req . json ( ) ;
93
+ } else {
94
+ const data = await req . formData ( ) ;
95
+ console . log ( "formData" , data ) ;
96
+
97
+ testInput = {
98
+ engine : "bun" ,
99
+ regex : data . get ( "regex" ) as string ,
100
+ replacement : data . get ( "replacement" ) as string ,
101
+ option : data . getAll ( "option" ) as string [ ] ,
102
+ inputs : data . getAll ( "input" ) as string [ ] ,
103
+ } ;
104
+ }
105
+ } else {
106
+ const searchParams = new URL ( req . url ) . searchParams ;
107
+ testInput = {
108
+ engine : searchParams . get ( "engine" ) || "deno" ,
109
+ regex : searchParams . get ( "regex" ) || "" ,
110
+ replacement : searchParams . get ( "replacement" ) || "" ,
111
+ option : searchParams . getAll ( "option" ) as string [ ] ,
112
+ inputs : searchParams . getAll ( "input" ) as string [ ] ,
113
+ } ;
114
+ console . log ( "searchParams" , searchParams ) ;
115
+ }
116
+
117
+ console . log ( "testInput" , testInput ) ;
118
+
119
+ const retVal = runTest ( testInput ) ;
120
+
121
+ console . log ( "testOutput" , retVal ) ;
122
+
123
+ return handleJsonp ( req , retVal ) ;
124
+ }
125
+
69
126
function main ( ) {
70
127
const port = parseInt ( Deno . env . get ( "PORT" ) || "4000" ) ;
71
128
const hostname = Deno . env . get ( "HOSTNAME" ) || "localhost" ;
0 commit comments