Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
136 lines (104 loc) · 3.69 KB
/
Copy pathapp.js
File metadata and controls
136 lines (104 loc) · 3.69 KB
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
import { Server } from 'hyper-express'
import { LRUCache } from 'lru-cache'
import cluster, { isWorker } from 'node:cluster'
import { maxQuery, maxRows } from './config.js'
const { DATABASE } = process.env
const db = DATABASE ? await import(`./database/${DATABASE}.js`) : null
const generateRandomNumber = () => Math.floor(Math.random() * maxRows) + 1
const parseQueries = (i) => Math.min(parseInt(i) || 1, maxQuery)
const escapeHTMLRules = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/' }
const unsafeHTMLMatcher = /[&<>"'\/]/g
const escapeHTMLCode = (text) => unsafeHTMLMatcher.test(text) ? text.replace(unsafeHTMLMatcher, function (m) { return escapeHTMLRules[m] || m; }) : text
const cache = new LRUCache({
max: maxRows
})
const app = new Server()
// use middleware to add `Server` into response header
app.use((_request, response, next) => {
response.header('Server', 'hyperexpress')
return next()
})
app.get('/plaintext', (_request, response) => {
response.type('text').send('Hello, World!')
})
app.get('/json', (_request, response) => {
response.json({ message: 'Hello, World!' })
})
if (db) {
app.get('/db', async (_request, response) => {
try {
const world = await db.find(generateRandomNumber())
response.json(world)
} catch (error) {
throw error
}
})
app.get('/queries', async (request, response) => {
try {
const queries = parseQueries(request.query.queries)
const worldPromises = new Array(queries)
for (let i = 0; i < queries; i++) {
worldPromises[i] = db.find(generateRandomNumber())
}
const worlds = await Promise.all(worldPromises);
response.json(worlds)
} catch (error) {
throw error
}
})
app.get('/updates', async (request, response) => {
try {
const queries = parseQueries(request.query.queries)
const worldPromises = new Array(queries)
for (let i = 0; i < queries; i++) {
worldPromises[i] = db.find(generateRandomNumber())
}
const worlds = await Promise.all(worldPromises)
for (let i = 0; i < queries; i++) {
worlds[i].randomNumber = generateRandomNumber()
}
await db.bulkUpdate(worlds)
response.json(worlds)
} catch (error) {
throw error
}
})
app.get('/fortunes', async (_request, response) => {
try {
const fortunes = await db.fortunes()
fortunes.push({ id: 0, message: 'Additional fortune added at request time.' })
fortunes.sort((a, b) => (a.message < b.message) ? -1 : 1)
const n = fortunes.length
let i = 0, html = ''
for (; i < n; i++) html += `<tr><td>${fortunes[i].id}</td><td>${escapeHTMLCode(fortunes[i].message)}</td></tr>`
response
.header('Content-Type', 'text/html; charset=utf-8')
.send(`<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>${html}</table></body></html>`)
} catch (error) {
throw error
}
})
let isCachePopulated = false
app.get('/cached-worlds', async (request, response) => {
try {
if (!isCachePopulated) {
const worlds = await db.getAllWorlds()
for (let i = 0; i < worlds.length; i++) {
cache.set(worlds[i].id, worlds[i])
}
isCachePopulated = true
}
const count = parseQueries(request.query.count)
const worlds = new Array(count)
for (let i = 0; i < count; i++) {
worlds[i] = cache.get(generateRandomNumber())
}
response.json(worlds)
} catch (error) {
throw error
}
})
}
app.listen(8080).then(() => {
console.log(`${isWorker ? `${cluster.worker.id}: ` : ''}Successfully bound to http://0.0.0.0:8080`)
})