-
Notifications
You must be signed in to change notification settings - Fork 581
/
Benchmarker.cjs
51 lines (48 loc) · 1.08 KB
/
Benchmarker.cjs
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
/* Non-SSL is simply App() */
const uWS = require('uWebSockets.js');
uWS.App()
.get('/', new uWS.DeclarativeResponse().writeHeader('content-type', 'text/plain').end('Hi'))
.get('/id/:id', new uWS.DeclarativeResponse().writeHeader('content-type', 'text/plain')
.writeHeader('x-powered-by', 'benchmark')
.writeParameterValue("id")
.write(" ")
.writeQueryValue("name")
.end())
.post('/json', (res, req) => {
readJson(
res,
(obj) => {
res.writeHeader('content-type', 'application/json').end(
JSON.stringify(obj)
)
},
() => {
res.end('Ok')
}
)
})
.listen(3000, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 3000')
}
})
function readJson(res, cb, err) {
let buffer
res.onData((ab, isLast) => {
let chunk = Buffer.from(ab)
if (isLast) {
if (buffer) {
cb(JSON.parse(Buffer.concat([buffer, chunk])))
} else {
cb(JSON.parse(chunk))
}
} else {
if (buffer) {
buffer = Buffer.concat([buffer, chunk])
} else {
buffer = Buffer.concat([chunk])
}
}
})
res.onAborted(err)
}