-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbunzer.js
286 lines (227 loc) · 10.5 KB
/
bunzer.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// start the server listening on a port
export function serve({hostname='127.0.0.1', port=8080, public_folder=undefined}={}) {
return Bun.listen({hostname, port, socket: {
// message chunk received from the client
data(socket, data) {
// create a new request object. this is just an empty context for us to store information
// about this request to pass to the user callbacks
const req = new_request()
req.socket = socket
// parse the first line of the http request
// parsing a raw http request in javascript is very slow and dumb but i can't find a faster alternative
const raw_http_request = data.toString('ascii') // ascii seems faster than utf-8
req.raw_http_request = raw_http_request
const first_space = raw_http_request.indexOf(' ')
const second_space = raw_http_request.indexOf(' ', first_space+2)
req.method = raw_http_request.slice(0, first_space)
req.path = raw_http_request.slice(first_space+1, second_space)
// routing. match the path to its handler function
const handler = router_find(req)
if(handler) {
// this is a wet mess because async/await and Promise.resolve is too slow
const response = handler(req)
if(!response) return send_200(socket)
if(is_response(response)) return send(socket, response.content, response.options)
if(is_promise(response))
return response.then(response => {
if(!response) return send_200(socket)
if(is_response(response)) return send(socket, response.content, response.options)
if(is_obj(response)) return send_fast_json(socket, response)
return send_fast_text(socket, response.toString())
}).catch(e => {console.error(e); return send_500(socket)})
if(is_obj(response)) return send_fast_json(socket, response)
return send_fast_text(socket, response.toString())
}
// no handler exists for this route, try to serve this path as a file from the public folder
if(public_folder) {
if(req.path[req.path.length-1] === '/') req.path += 'index.html'
// @perf could check if the file exists using a bloom filter first to 10x the performance of 404s
// (i've already implemented and benched this, but left it out because meh, it's a lot of code)
return send_file(socket, `${public_folder}${req.path}`)
}
// i'd prefer to respond with Cannot GET / but meh, that creates garbage
// return send(socket, `Cannot ${req.method} ${req.path}`, {status: 404})
return send_404(socket)
},
// todo: when we write too much data to a socket, we have to wait
// for this drain thing to be called then write more ... this is so annoying
// currently sending large data that would require multiple writes breaks
// until this is implemented. this is a part of uws, the http server bun uses internally
// drain() {},
error(socket, error) { send_500(socket); console.error(error) },
}})
}
// request context stuff
// todo object pooling? making new requests for every request sounds slow
// fast-querystring is faster than node:querystring but i'd rather not have a dependency
// import querystring from 'fast-querystring'
const querystring = require('node:querystring')
class Request {
get ip() { return this.socket.remoteAddress }
get headers() {
const headers = {}
const raw_http_request = this.raw_http_request
const start_of_headers = raw_http_request.indexOf('\r', 12) + 2
let start_of_line = start_of_headers
while(start_of_line < raw_http_request.length) {
const end_of_line = raw_http_request.indexOf('\r', start_of_line)
if(end_of_line === start_of_line || end_of_line === -1) break // double newline is the end of headers
const colon_pos = raw_http_request.lastIndexOf(':', end_of_line)
const header_name = raw_http_request.slice(start_of_line, colon_pos)
const header_value = raw_http_request.slice(colon_pos+2, end_of_line)
headers[header_name] = header_value
start_of_line = end_of_line + 2
}
this.start_of_body = start_of_line+2
return headers
}
get body() {
if(!this.start_of_body) this.start_of_body = this.raw_http_request.indexOf('\r\n\r\n', 12)+4
return this.raw_http_request.slice(this.start_of_body)
}
get query() { return querystring.parse(this.path.slice(this.rawquery_index)) }
}
function new_request() {return new Request()}
// sending responses stuff
export function response(content, options) {return new UserResponse(content, options) }
class UserResponse {
constructor(content, options) {
this.options = options || {}
if(typeof content === 'object') {
this.content = JSON.stringify(content)
if(this.options.headers === undefined) this.options.headers = {}
this.options.headers['Content-Type'] = 'application/json'
} else {
this.content = content || ''
}
}
}
function send_200(socket) {socket.write('HTTP/1.1 200\r\nContent-Length: 0\r\n\r\n'); socket.flush()}
function send_400(socket) {socket.write('HTTP/1.1 400\r\nContent-Length: 0\r\n\r\n'); socket.flush()}
function send_404(socket) {socket.write('HTTP/1.1 404\r\nContent-Length: 0\r\n\r\n'); socket.flush()}
function send_500(socket) {socket.write('HTTP/1.1 500\r\nContent-Length: 0\r\n\r\n'); socket.flush()}
function send_fast_json(socket, obj) {
const content = JSON.stringify(obj)
const response = `HTTP/1.1 200\r\nContent-Length: ${content.length}\r\nContent-Type: application/json\r\n\r\n${content}`
socket.write(response); socket.flush()
}
function send_fast_text(socket, content) {
const response = `HTTP/1.1 200\r\nContent-Length: ${content.length}\r\n\r\n${content}`
socket.write(response); socket.flush()
}
function send_fast_headers(socket, content, headers) {
let headers_str = ''
for(const key in headers) headers_str += `${key}: ${headers[key]}\r\n`
const response = `HTTP/1.1 200\r\nContent-Length: ${content.length}\r\n${headers_str}\r\n${content}`
socket.write(response); socket.flush()
}
function send_fast_status(socket, content, status) {
const response = `HTTP/1.1 ${status}\r\nContent-Length: ${content.length}\r\n\r\n${content}`
socket.write(response); socket.flush()
}
function send(socket, content, {status=200, headers}={}) {
// headers obj to http response string
let headers_str = ''
if(headers) for(const key in headers) headers_str += `${key}: ${headers[key]}\r\n`
let response = `HTTP/1.1 ${status}\r\nContent-Length: ${content.length}\r\n${headers_str}\r\n${content}`
socket.write(response); socket.flush()
}
// send_file is slow. bun does not support quick sendfile... yet? (v1.0)
// using fs instead of Bun.file because Bun.file doesn't work https://github.com/oven-sh/bun/issues/1446
import fs from 'fs'
function send_file(socket, filepath) {
fs.readFile(filepath, (err, file_content) => {
if(err) return send_404(socket)
const file = Bun.file(filepath)
send(socket, file_content, {
headers: {
'Content-Type': file.type,
'Referrer-Policy': 'no-referrer',
'Cache-Control': 'public,max-age=604800,immutable',
}
})
})
}
// helpers
function is_response(x) { return x instanceof UserResponse }
function is_promise(x) { return x instanceof Promise }
function is_obj(x) { return typeof x === 'object' }
// router stuff
// custom methods aren't supported because i've never used them
export function any(path, handler) {router_add(0, path, handler)}
export function get(path, handler) {router_add(1, path, handler)}
export function post(path, handler) {router_add(2, path, handler)}
export function put(path, handler) {router_add(3, path, handler)}
export function del(path, handler) {router_add(4, path, handler)}
function method_to_method_id(method) {
// fastpath for GET because it's most common
// (not all methods can be distinguished from a single character switch)
if(method.charCodeAt(0) === 71/*G*/) return 1
switch(method.charCodeAt(1)) {
case 79/*O*/: return 2
case 85/*U*/: return 3
case 69/*E*/: return 4
default: return 0
}
}
const static_routes_by_method = [new Map(), new Map(), new Map(), new Map(), new Map()]
const static_routes = static_routes_by_method[0]
const dynamic_routes_by_method = [[], [], [], [], []]
const dynamic_routes = dynamic_routes_by_method[0]
function new_dyanmic_route(path, handler) {
const parts = path.slice(1).split('/')
const info = []
for(let i=0; i<parts.length; i++) {
const part = parts[i]
if(!is_named_part(part)) continue
info.push({name: parts[i].slice(1), index: i})
}
return {parts, handler, info}
}
function router_add(method_id, path, handler) {
if(path[0] != '/') path = `/${path}` // ensure leading slash
const parts = path.slice(1).split('/')
const contains_named_part = parts.some(is_named_part)
if(!contains_named_part) {
static_routes.set(path, handler)
if(method_id) static_routes_by_method[method_id].set(path, handler)
return
}
// it's a dynamic route
const dynamic_route_info = new_dyanmic_route(path, handler)
dynamic_routes.push(dynamic_route_info)
if(method_id) dynamic_routes_by_method[method_id].push(dynamic_route_info)
}
function router_find(req) {
let path = req.path
let method_id = method_to_method_id(req.method)
// search static routes
const the_static_routes = static_routes_by_method[method_id]
const handler = the_static_routes.get(path)
if(handler) return handler
// remove ? if it exists then search static routes again
const questionmark_i = path.indexOf('?')
if(questionmark_i !== -1) {
req.rawquery_index = questionmark_i+1
path = path.slice(0, questionmark_i)
const handler = the_static_routes.get(path)
if(handler) return handler
}
// search dynamic routes
const parts = path.slice(1).split('/')
const the_dyanmic_routes = dynamic_routes_by_method[method_id]
outer: for(let i=0; i<the_dyanmic_routes.length; i++) {
const route = the_dyanmic_routes[i]
if(route.parts.length !== parts.length) continue
for(let i=0; i<parts.length; i++) {
if(is_named_part(route.parts[i])) continue
if(route.parts[i] !== parts[i]) continue outer
}
// found a matching route
// set the request's params then return its handler
req.params = {}
for(const info of route.info) req.params[info.name] = parts[info.index]
return route.handler
}
}
function is_named_part(part) {return part.charCodeAt(0) === 58/*:*/}