-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
376 lines (297 loc) · 11 KB
/
server.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import { decodeFrames, pongFrame, prepareFrame, WSChangeset } from './src/websockets/websocketFraming';
import { LinkPeeps, LinkPeepLocus, PLink, PLinkLocus } from './src/paths/linkPeeping';
import { PukableEntrypoint } from './src/pukables/entrypoints';
import { MessageChannel } from 'node:worker_threads'
import { FSPeep, FSPeepRoot } from './src/paths/filePeeping'
import { createSecureServer } from 'node:http2'
import { PP } from './src/fmt/ppstuff.js';
import { readFileSync } from 'node:fs';
import { L } from './src/fmt/logging';
import { createServer } from 'net';
import { Socket } from 'node:net';
L.log(`Hi (${process.pid})\n`)
const WEBROOT = 'web'
const RELOADER_SCRIPT = '\n<script>\n' + readFileSync('./src/websockets/websocketReloading.js', 'utf-8') + '\n</script>\n'
let cannotPuke = null
const { port1: changeReceiver, port2: changeTransmitter } = new MessageChannel()
const obs = new PerformanceObserver((items) => {
L.log(`${items.getEntries()[0].name} in ${items.getEntries()[0].duration.toFixed(2)} ms\n`);
performance.clearMarks();
});
obs.observe({ type: 'measure' });
performance.mark('a')
let webrootEntry, locus: PLinkLocus, pLinks: LinkPeeps
let rebuildLinkLocator = () => {
// Start by scanning the folder again and building a link tree.
let _webrootEntry = FSPeepRoot({ entrypoint: WEBROOT })
if (!_webrootEntry) {
L.log(`Couldn't resolve root directory ${WEBROOT}.`)
return process.exit(1)
}
let _pLinks = LinkPeeps({ entrypoint: _webrootEntry })
if (!_pLinks) {
L.log(`Couldn't construct LinkPeeps from entrypoint.`)
return process.exit(1)
}
// Connect `change` messages from the new FSPeep to the message port.
_webrootEntry.connectWatcher(changeTransmitter)
let _locus = LinkPeepLocus(_pLinks)
webrootEntry = _webrootEntry
locus = _locus
pLinks = _pLinks
}
rebuildLinkLocator()
performance.mark('b')
performance.measure('Built file watch tree', 'a', 'b')
L.log(`Serving from: ${webrootEntry.relpath}\n`)
for (let l of webrootEntry.repr()) {
L.log(l)
}
const server = createSecureServer({
maxSessionMemory: 1000,
allowHTTP1: true,
key: readFileSync('localhost-key.pem'),
cert: readFileSync('localhost.pem'),
})
let buildTask = build(webrootEntry);
export type BuiltStuff = { built: PukableEntrypoint[], webuke: Webuke, relent: Relent }
type Relent = { [relpath: string]: PukableEntrypoint[] }
type Webuke = { [webpath: string]: PukableEntrypoint }
async function build(root: FSPeep, relpathToBuild?: string): Promise<BuiltStuff> {
let _relent: Relent = {}
const _webuke: Webuke = {}
rebuildLinkLocator()
const buildSet: PLink[] = relpathToBuild
? [pLinks.links.find(x =>
x.relpath === relpathToBuild)]
: pLinks.links.filter(x =>
x.ogPeep.path.name === 'index'
&& x.type == 'html')
if (!buildSet) {
console.error('Nothing to build.')
cannotPuke = 'Empty build set (no index files?)';
return { built: [], relent: {}, webuke: {} }
}
L.log(PP.styles.pink + `\n> Building ${PP.ar(buildSet.map((i: any) => i.relpath))}. \n` + PP.styles.none)
let _built = []
try {
performance.mark('a')
for (let iPeep of buildSet) {
const entrypoint = new PukableEntrypoint(
locus, iPeep.relpath, undefined, RELOADER_SCRIPT)
_webuke[entrypoint.ownLink.webpath] = entrypoint
for (let assoc of entrypoint.getAssociatedFilenames()) {
if (!_relent[assoc]) { _relent[assoc] = [] }
_relent[assoc].push(entrypoint)
}
_relent[entrypoint.ownLink.relpath] = [entrypoint]
_built.push(entrypoint)
}
performance.mark('b')
performance.measure('built in', 'a', 'b')
cannotPuke = null
} catch (e) {
console.error(e)
console.error("Ran into a problem building files.")
cannotPuke = e
}
return { built: _built, relent: _relent, webuke: _webuke }
}
let enqueueReplRepr = null;
changeReceiver.addEventListener("message", async (ev: MessageEvent) => {
let [mode, relpath] = ev.data.split(" ")
if (mode == "change") {
buildTask = build(webrootEntry, relpath)
buildTask.then(({ built, relent, webuke }) => {
enqueueReplRepr = built
changeTransmitter.postMessage(`repl ${relpath}`)
let webpaths = relent[relpath]?.map(x => x.ownLink.webpath) || []
for (let wp of webpaths) {
changeTransmitter.postMessage(`webChanged ${wp}`)
}
})
} else if (mode == 'repl' && (relpath === enqueueReplRepr)) {
let { relent } = await buildTask
let builtPoints = relent[relpath]
for (let devInfo of builtPoints.map(x => x.debugRepr())) {
L.log(devInfo)
}
enqueueReplRepr = null
}
})
server.on('stream', async (stream, headers) => {
if (headers[':method'] != 'GET') {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 501,
});
stream.write('Not implemented')
stream.end()
return
}
if (cannotPuke !== null) {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 503,
});
stream.write('Error building files:<br>')
stream.write(cannotPuke.toString())
stream.end()
return
}
let { webuke } = await buildTask
const path = headers[':path'] || '/'
const located = locus(WEBROOT)(path)
if (located.type === 'html' && located.result.type === 'okHtml') {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
let entrypoint = webuke[located.webpath]
for (let v of entrypoint.blowChunks()) {
if (!v) {
console.warn('Warning: undefined value in stream')
continue
}
stream.write(v)
}
stream.end()
return
}
else if (located.type == 'file') {
if (located.type !== 'file' || located.result.type !== 'okFile') {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 404,
});
L.warn(`Sending 404 for file request: ${path}.`);
stream.write('404 Not Found')
stream.end()
return
}
let charset = located.result.contentType === 'text/html' ? ';charset=text/utf-8' : ''
stream.respond({
'content-type': `${located.result.contentType}${charset}`,
':status': 200,
});
stream.write(located.result.data)
stream.end()
return
}
else {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 404,
});
L.warn(`Sending 404: Request: ${path} not known to any path.`);
stream.write('404 Not Found')
stream.end()
return
}
})
/**
*
* @param {string[]} lines - Each line of an HTTP header message
* @returns {function(keyReg: RegExp): ?string}
*/
function headerExtract(lines) {
/**
* @param {RegExp} keyReg - Regular expression literal matching all of a key (e.g. /^Sec-WebSocket-Key: /)
* @returns {?string} - The value, if found
*/
return (keyReg) => {
return lines
.slice(1)
.map(l => l.split(keyReg)?.[1])
.find(l => l)
}
}
const changeset = WSChangeset(changeReceiver)
const Enc = new TextEncoder()
const websocket = createServer({
keepAlive: true,
keepAliveInitialDelay: 1000,
}, async (socket: Socket & { id?: number }) => {
let accept;
changeset.addSocket(socket)
socket.addListener("error", (e) => {
changeReceiver.removeListener("message", changeset.listeners[socket.id].cb)
L.log('------------\n')
L.error(e.message + '\n')
L.error(e.cause + '\n')
L.log('------------\n')
})
socket.addListener("close", () => {
L.log(`(Sock#${socket.id}) | Socket closing.\n`)
changeReceiver.removeListener("message", changeset.listeners[socket.id].cb)
socket.end()
socket.destroy()
})
socket.addListener("data", async (data) => {
if (accept !== undefined) {
let res = decodeFrames(data)
if (res.error == true) {
socket.end()
socket.destroy(new Error(res.reason))
return
} if (res.opcode === 'clos') {
socket.end()
} else if (res.opcode === 'ping') {
console.info(`Received ping from ${socket.id}. Responding with pong...`)
socket.write(pongFrame(res.data as number[]))
} else if (res.opcode === 'pong') {
changeset.keepalive(socket.id, res.data as number[])
} else if (res.opcode === 'dtxt') {
console.warn(`${socket.id} | Got some text.`)
console.warn(res.data)
} else {
console.warn(`${socket.id} | Got a bag of data.`)
console.warn(res.data)
}
}
else {
const lines = data.toString().split('\r\n')
const hval = headerExtract(lines)
if (lines?.[0].endsWith('HTTP/1.1')) {
if (!(hval(/^Upgrade: /) === 'websocket')) {
socket.write(Enc.encode([
'HTTP/1.1 426 Upgrade Required',
'Upgrade: websocket',
'Connection: Upgrade',
'',
''
].join('\r\n')))
return
}
const swk = hval(/^Sec-WebSocket-Key: /)
const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
const inKey = swk + magic
accept = await crypto.subtle.digest('sha-1', Enc.encode(inKey))
const b64accept = btoa(String.fromCharCode(...new Uint8Array(accept)))
const rb = Enc.encode([
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${b64accept}`,
'',
''
].join('\r\n'))
socket.write(rb)
socket.write(prepareFrame(`hi ${socket.id}`))
L.log(`(Sock#${socket.id}) Updater connected. Starting pings. \n`)
changeset.startPings(socket.id)
return
}
}
})
})
process.on('SIGTERM', onSIGTERM);
function onSIGTERM() {
server.close()
websocket.close()
process.exit(0);
}
websocket.listen(80)
L.log(PP.styles.blue + "websocket | Listening on port 80...\n" + PP.styles.none)
server.listen(3003)
L.log(PP.styles.blue + "https | Listening on port 3003...\n" + PP.styles.none)