-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
172 lines (124 loc) · 3.67 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { fsRoutes, hydrationScript, type RoutesOptions } from "./src/filesystem.ts";
import { ws, upgrade } from "./src/websocket"
import { generateContext } from "./src/routes";
import { type Context, type Handler } from "./src/routes"
import { type WebSocket } from "./src/websocket"
import { type Server } from "bun"
import { Method } from "./src/method.ts"
import { fsStatic, type StaticOptions } from "./src/static.ts"
import * as Methods from "./src/method.ts"
import * as Log from "./src/logs.ts";
interface Config {
port: string | number,
hostname?: string,
production?: boolean,
logs?: boolean,
key?: string,
cert?: string,
}
export interface Nudol {
config: Config,
handlers: Map<Handler, (context: Context) => any>;
routes_path: string|null;
websocket: WebSocket | null;
temp_dir: boolean;
temp_path: string;
static_routes: any;
server: Server | undefined;
get: ( this: Nudol, path: string, fn: (request: Context) => void ) => void,
post: ( this: Nudol, path: string, fn: (request: Context) => void ) => void,
notfound: ( methods: Method[] , fn: (request: Context) => void ) => void;
ws: ( ws: WebSocket ) => void;
upgrade_function: (( server: Server, request: Request) => Promise<boolean>) | null;
upgrade: ( fn: ( server: Server, request: Request) => Promise<boolean> ) => void;
fsStatic: ( path: string, alias?: StaticOptions ) => Promise<void>;
fsRoutes( routes_directory_path: string, params?: RoutesOptions ): Promise<void>
hydrationScript( hydrationpath: string ): any
listen(): void,
}
export function Nudol( config: Partial<Config> = {} ): Nudol {
var instance: Nudol = {
config: {
port: 3000,
hostname: "0.0.0.0",
production: false,
logs: true,
key: undefined,
cert: undefined,
...config
} as Config,
handlers: new Map([]),
routes_path: null,
websocket: null,
temp_dir: false,
temp_path: ".temp",
static_routes: {},
server: undefined,
upgrade_function: null,
ws: ws,
get: Methods.get,
post: Methods.post,
notfound: Methods.notfound,
upgrade: upgrade,
hydrationScript: hydrationScript,
fsStatic: fsStatic,
fsRoutes: fsRoutes,
listen: listen
}
return instance
}
function listen( this: Nudol ) {
const self = this
if(this.config.logs) Log.start( this )
this.server = Bun.serve({
port: this.config.port,
hostname: this.config.hostname,
static: this.static_routes,
keyFile: this.config.key,
certFile: this.config.cert,
fetch( req: Request ) {
for(const [handler, handler_function] of self.handlers) {
if( req.method != handler.method ) continue;
let check = new URL(req.url).pathname.match( handler.regexp )
if( check ) {
return handler_function( generateContext( req, check.groups ) )
}
}
const notfound = [...self.handlers ].find(( [k, _] ) => (k.path == "404" && k.method == req.method) )
if( notfound ) return notfound[1]( generateContext( req, {}) )
// if(self.websocket != null) {
// if(self.websocket.path == new URL(req.url).pathname) {
// if(self.upgrade_function) {
// const success = await self.upgrade_function( this, req )
//
// if (success) {
// return undefined;
// }
//
// } else {
//
// const success = this.upgrade(req);
//
// if (success) {
// return undefined;
// }
//
// }
// }
// }
return new Response("404 Not found", { status: 404 } );
},
websocket: {
idleTimeout: self.websocket?.idleTimeout,
async open(ws) {
self.websocket!.onopen(ws)
},
async message(ws, message) {
self.websocket!.onmessage(ws, message)
},
async close(ws, code) {
self.websocket!.onclose(ws, code)
}
}
});
}