-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
212 lines (195 loc) · 6.09 KB
/
index.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
import CONFIG from './conf.js'
import {
addSocket as _addSocket,
closeSub,
delSocket as _delSocket,
forEachSub,
forEachSubId,
openSub as _openSub,
sqliteInit,
} from './sqlite.js'
import { pgForEachEvent, pgInit, pgListen, pgStoreNotify } from './pg.js'
import { plugsAction, plugsInit } from './plugs.js'
import migrations from './migrations/index.js'
console.log(`\x1b[32m
▄▄▄▄ ▒█████ ▒█████ ▄████ ▓█████ ██▀███
▓█████▄ ▒██▒ ██▒▒██▒ ██▒ ██▒ ▀█▒▓█ ▀ ▓██ ▒ ██▒
▒██▒ ▄██▒██░ ██▒▒██░ ██▒▒██░▄▄▄░▒███ ▓██ ░▄█ ▒ \x1b[0m booger ${CONFIG.version} \x1b[32m
▒██░█▀ ▒██ ██░▒██ ██░░▓█ ██▓▒▓█ ▄ ▒██▀▀█▄ \x1b[0m deno ${Deno.version.deno} \x1b[32m
░▓█ ▀█▓░ ████▓▒░░ ████▓▒░░▒▓███▀▒░▒████▒░██▓ ▒██▒
░▒▓███▀▒░ ▒░▒░▒░ ░ ▒░▒░▒░ ░▒ ▒ ░░ ▒░ ░░ ▒▓ ░▒▓░ \x1b[0m port ${CONFIG.port} \x1b[32m
▒░▒ ░ ░ ▒ ▒░ ░ ▒ ▒░ ░ ░ ░ ░ ░ ░▒ ░ ▒░ \x1b[0m pid ${Deno.pid} \x1b[32m
░ ░ ░ ░ ░ ▒ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░
░
\x1b[0m`)
const sockets = new Map() // map[socket id][websocket]
let pg
try {
sqliteInit()
await plugsInit()
pg = await pgInit(CONFIG.db, migrations)
await pgListen(pg, (e) => {
forEachSub(JSON.parse(e), (id, subId) => {
const ws = sockets.get(id)
if (!ws) return
try {
ws.send(`["EVENT", "${subId}", ${e}]`)
} catch (e) {
handleError(ws, e)
}
})
})
Deno.serve({
port: CONFIG.port,
hostname: CONFIG.hostname,
reusePort: true,
}, async (req) => {
if (req.headers.get('upgrade')?.toLowerCase() !== 'websocket') {
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': '*',
}
// nip 11
if (req.headers.get('accept') === 'application/nostr+json') {
return Response.json(CONFIG.nip11, { status: 200, headers })
}
// health check
if (new URL(req.url).pathname === '/health') {
return new Response(null, { status: 200 })
}
// respond with homepage
let file
try {
file = await Deno.open('./index.html', { read: true })
return new Response(file.readable, { status: 200 })
} catch {
return new Response(null, { status: 404 })
}
}
const id = crypto.randomUUID()
const headers = Object.fromEntries(req.headers.entries())
try {
await plugsAction('connect', { id, headers })
} catch (e) {
return new Response(e.message, { status: 403 })
}
const { socket: ws, response: res } = Deno.upgradeWebSocket(req, {
idleTimeout: 30,
})
ws.booger = { id, headers }
ws.onopen = () => {
try {
addSocket(ws)
} catch (e) {
sendNotice(ws, e.message)
delSocket(ws)
ws.close()
}
}
ws.onmessage = async ({ data }) => {
try {
const m = JSON.parse(data)
switch (m[0]) {
case 'EVENT':
return await store(ws, m[1])
case 'REQ':
return await openSub(ws, ...m.splice(1))
case 'CLOSE':
plugsAction('unsub', ws.booger, { subId: m[1] })
.catch(console.error)
return closeSub(ws.booger.id, m[1])
default:
sendNotice(ws, `invalid request type ${m[0]}`)
}
} catch (e) {
handleError(ws, e)
}
}
ws.onerror = (e) => handleError(ws, e)
ws.onclose = () => {
plugsAction('disconnect', ws.booger).catch(console.error)
forEachSubId(
ws.booger.id,
(subId) =>
plugsAction('unsub', ws.booger, { subId }).catch(console.error),
)
delSocket(ws)
}
return res
})
} catch (e) {
console.error('booger failed to start')
console.error(e)
Deno.exit(1)
}
async function store(ws, event) {
try {
await plugsAction('event', ws.booger, { event })
await pgStoreNotify(pg, event)
ws.send(JSON.stringify(['OK', event.id, true, '']))
} catch (e) {
ws.send(JSON.stringify(['OK', event.id, false, e.message]))
}
}
async function openSub(ws, subId, ...filters) {
try {
await plugsAction('sub', ws.booger, { subId, filters })
_openSub(ws.booger.id, subId, filters)
let count = 0
await pgForEachEvent(
pg,
filters,
(e) => {
try {
ws.send(`["EVENT", "${subId}", ${e}]`)
count++
} catch (e) {
handleError(ws, e)
throw e
}
},
)
ws.send(JSON.stringify(['EOSE', subId]))
plugsAction('eose', ws.booger, { subId, count })
.catch(console.error)
} catch (e) {
sendNotice(ws, e.message)
}
}
function addSocket(ws) {
_addSocket(ws.booger.id)
sockets.set(ws.booger.id, ws)
}
function delSocket(ws) {
_delSocket(ws.booger.id)
sockets.delete(ws.booger.id)
}
function handleError(ws, error) {
// if socket closed, assume that's the source of the error
if (ws.readyState !== 1) return
console.error(error)
plugsAction('error', ws.booger, {
error: {
name: error.name,
message: error.message,
stack: error.stack?.split('\n'),
code: error.code,
errno: error.errno,
syscall: error.syscall,
},
}).catch(console.error)
}
function sendNotice(ws, notice) {
// if socket closed, assume that's the source of the error
if (ws.readyState !== 1) return
console.info('notice', notice)
plugsAction('notice', ws.booger, { notice })
.catch(console.error)
try {
ws.send(JSON.stringify(['NOTICE', notice]))
} catch (e) {
handleError(ws, e)
}
}