-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
538 lines (503 loc) · 16.7 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//@ts-check
const fs = require("fs")
const http = require("http")
const path = require("path")
const mime = require("mime")
const cf = require("./util/common.js")
const pug = require("pug")
const sass = require("sass")
const watchAndCompile = require("./util/watch_compiler.js")
const WebSocket = require("ws")
const stream = require("stream")
const crypto = require("crypto")
const plugins = require("./plugins")
const {EventEmitter} = require("events")
const symbols = {
PUG_SOURCE_NOT_FOUND: Symbol("PUG_SOURCE_NOT_FOUND")
}
/**
* @typedef {Map<string, {web: (locals?) => any, client: (locals?) => any}>} PugCache
*/
/**
* @typedef Config
* @property {number} [port]
* @property {string} [filesDir]
* @property {any} [globalHeaders]
* @property {any} [basicCacheControl]
* @property {string} [ip]
* @property {string} [relativeRoot]
* @property {boolean} [ws]
* @property {string} [onionLocation]
*/
void 0
/** @type {Config} */
const defaultConfig = {
port: 8080,
filesDir: "html",
globalHeaders: {},
basicCacheControl: {
exts: [
"ttf", "svg", "gif", "webmanifest", "ico",
"png", "jpg", "jpeg",
"PNG", "JPG", "JPEG"
],
seconds: 604800
},
ip: "0.0.0.0",
relativeRoot: "",
ws: false,
onionLocation: null
}
function mimeType(type) {
const types = new Map([
["mjs", "text/javascript"],
["ttf", "application/font-sfnt"],
["ico", "image/x-icon"],
["sass", "text/css"]
])
return types[type.split(".")[1]] || mime.getType(type)
}
function toRange(length, headers, req) {
if (length === 0) return {statusCode: 200, start: undefined, end: undefined, length: 0}
let start = 0
let end = length-1
let statusCode = 200
if (req.headers.range) {
let match = req.headers.range.match(/^bytes=([0-9]*)-([0-9]*)$/)
if (match) {
if (match[1]) {
let value = +match[1]
if (!isNaN(value) && value >= start) {
start = value
statusCode = 206
}
}
if (match[2]) {
let value = +match[2]
if (!isNaN(value) && value <= end) {
end = value
statusCode = 206
}
}
}
}
if (start > end) {
start = 0
end = length-1
statusCode = 200
}
if (statusCode == 206) {
headers["Accept-Ranges"] = "bytes"
headers["Content-Range"] = "bytes "+start+"-"+end+"/"+length
}
return {statusCode, start, end, length: end - start + 1}
}
class Pinski {
/**
* @param {Config} config
*/
constructor(config) {
this.config = defaultConfig
Object.assign(this.config, config)
this.pugCache = new Map()
this.sassCache = new Map()
/** @type {Map<string, {hash: string, type: string}>} */
this.staticFileTable = new Map()
this.pugDefaultLocals = {
static: this.staticFileTable,
getStaticURL: plugins.getStaticURL
}
this.mutedLogs = []
this.api = {
handlers: [],
routeStore: new Map(),
cancelStore: new Map(),
dirs: []
}
/** pug/sass */
this.pageHandlers = []
this.notFoundTarget = null
this.wss = null
this.server = null
this.pendingCompiles = 0
this.pendingCompilesEmitter = new EventEmitter()
this.shutdownCallbacks = []
}
getExports() {
return {
instance: this,
server: this.server,
wss: this.wss,
pugCache: this.pugCache,
sassCache: this.sassCache,
staticFileTable: this.staticFileTable
}
}
addRoute(web, local, type = undefined, absolute = false) {
this.deleteRoute(web)
this.pageHandlers.push({web, local, type, absolute})
}
deleteRoute(web) {
this.pageHandlers = this.pageHandlers.filter(p => p.web !== web)
}
enableWS() {
this.wss = new WebSocket.Server({server: this.server})
}
startServer() {
this.server = http.createServer(this._handleRequest.bind(this)).listen(this.config.port, this.config.ip)
}
muteLogsStartingWith(string) {
this.mutedLogs.push(string)
}
setNotFoundTarget(target) {
this.notFoundTarget = target
}
_shouldLog(path) {
return !this.mutedLogs.some(muted => path.startsWith(muted))
}
waitForFirstCompile() {
if (this.pendingCompiles === 0) {
return Promise.resolve()
} else {
return new Promise(resolve => {
this.pendingCompilesEmitter.once("done", resolve)
})
}
}
addStaticHashTableDir(dir) {
const wac = watchAndCompile(dir, [], this.staticFileTable, fullPath => {
return new Promise((resolve, reject) => {
const hash = crypto.createHash("sha256")
stream.pipeline(
fs.createReadStream(fullPath),
hash,
err => {
if (err) return reject(err)
const digest = hash.digest("hex")
// console.log(fullPath, "→", digest)
return resolve({hash: digest, type: "static"})
}
)
})
})
this.shutdownCallbacks.push(wac.shutdown)
}
addPugDir(dir, includes = []) {
this.pendingCompiles++
const wac = watchAndCompile(dir, includes, this.pugCache, fullPath => {
try {
const web = pug.compileFile(fullPath, {doctype: "html"})
const client = pug.compileFileClient(fullPath, {doctype: "html", compileDebug: false})
return {web, client}
} catch (error) {
console.error(`Pug compilation of file ${fullPath} failed.`)
console.error(error.message)
return null
}
})
wac.compiler.then(() => {
this.pendingCompiles--
if (this.pendingCompiles === 0) this.pendingCompilesEmitter.emit("done")
})
this.shutdownCallbacks.push(wac.shutdown)
}
addSassDir(dir, includes = []) {
this.pendingCompiles++
const wac = watchAndCompile(dir, includes, this.sassCache, fullPath =>
fs.promises.readFile(fullPath, {encoding: "utf8"}).then(data => {
if (data) {
try {
const rendered = sass.renderSync({file: fullPath, indentedSyntax: !fullPath.endsWith("scss"), outputStyle: "compressed"}).css.toString()
const hash = crypto.createHash("sha256").update(rendered).digest("hex")
this.staticFileTable.set(fullPath, {type: "sass", hash})
cf.log("[CMP] Compiled "+fullPath, "spam")
return rendered
} catch (error) {
console.error(`Sass compilation of file ${fullPath} failed.`)
console.error(error)
return null
}
} else {
return null
}
})
)
wac.compiler.then(() => {
this.pendingCompiles--
if (this.pendingCompiles === 0) this.pendingCompilesEmitter.emit("done")
})
this.shutdownCallbacks.push(wac.shutdown)
}
addAPIDir(dir) {
return this.addAbsoluteAPIDir(path.join(this.config.relativeRoot, dir))
}
addAbsoluteAPIDir(dir) {
if (this.api.dirs.includes(dir)) return
this.api.dirs.push(dir)
const wac = watchAndCompile(dir, [], new Map(), async fullPath => {
if (this.api.cancelStore.has(fullPath)) {
const cancel = this.api.cancelStore.get(fullPath)
this.api.cancelStore.delete(fullPath)
cancel()
}
delete require.cache[require.resolve(fullPath)]
let routes = require(fullPath)
if (routes instanceof Array) {
routes.forEach(r => {
if (typeof r.priority === "undefined") r.priority = 0
})
routes = routes.filter(r => {
if (r.cancel) {
this.api.cancelStore.set(fullPath, r.code)
return false
} else {
return true
}
})
//console.log("Adding new handers", routes)
this.api.routeStore.set(fullPath, routes)
this.api.handlers = [].concat(...this.api.routeStore.values()).sort((a, b) => (b.priority - a.priority))
}
})
this.shutdownCallbacks.push(wac.shutdown)
}
_handleRequest(req, res) {
// change HEAD to GET
let isHead = req.method === "HEAD"
if (isHead) req.method = "GET"
// fill host
if (!req.headers.host) req.headers.host = "localhost"
// headers can be added to later
let headers = {}
// parse url
let url
try {
url = new URL(req.url, "http://localhost")
} catch (e) {
res.writeHead(400, {"Content-Type": "text/plain; charset=UTF-8"})
res.end("Malformed URI")
return
}
// remove trailing slash
if (url.pathname !== "/") url.pathname = url.pathname.replace(/\/+$/, "")
if (this._shouldLog(url.pathname)) cf.log(`[INC] ${url.pathname}`, "spam")
// manage cache control for file extension
let ext = url.pathname.split(".").slice(-1)[0]
if (this.config.basicCacheControl.exts.includes(ext)) headers["Cache-Control"] = `max-age=${this.config.basicCacheControl.seconds}, public`
// manage onion-location
if (this.config.onionLocation) headers["Onion-Location"] = this.config.onionLocation + req.url
// go through handlers
try {
this._handleWithAPI(req, res, url, headers, isHead)
|| this._handleWithPage(req, res, url, headers, isHead)
|| this._handleDirect(req, res, url, headers, isHead)
} catch (err) {
this._handleError(req, res, url, headers, isHead, err)
}
}
/**
* @param {import("http").IncomingMessage} req
* @param {import("http").ServerResponse} res
* @param {URL} url
*/
_handleWithAPI(req, res, url, headers, isHead) {
let i = 0
let handled = false
// try each handler
while (!handled && i < this.api.handlers.length) {
// prepare while loop for next
const handler = this.api.handlers[i]
i++
// see if it works
if (!handler.route || !handler.methods || (!handler.methods.includes(req.method) && !handler.methods.includes("*"))) continue
const re = new RegExp(`^${handler.route}$`)
const match = url.pathname.match(re)
if (!match) continue
// it works. don't loop any more
handled = true
// process the request
if (this._shouldLog(url.pathname)) cf.log(`[API] ${url.pathname} = ${handler.route}`, "spam")
new Promise((resolve, reject) => {
const fill = match.slice(1)
if (handler.upload && (req.method === "POST" || req.method === "PATCH")) {
const buffers = []
req.on("data", chunk => {
buffers.push(chunk)
})
req.on("end", () => {
const body = Buffer.concat(buffers)
if (handler.upload === "json") {
let data
try {
data = JSON.parse(body.toString())
} catch (e) {}
handler.code({req, url, res, fill, body, data}).then(resolve, reject)
} else {
handler.code({req, url, res, fill, body}).then(resolve, reject)
}
})
} else {
handler.code({req, url, res, fill}).then(resolve, reject)
}
}).then(result => {
// api is done, construct response from return value
if (result === undefined) {
cf.log(`${url.pathname} [API] returned undefined`, "error") // forgot to return?
return
}
if (result === null) {
if (this._shouldLog(url.pathname)) cf.log(`${url.pathname} [API] ignoring null`, "spam") // deliberate no response
return
}
// headers
if (!result.headers) result.headers = {}
let combinedHeaders = Object.assign({"Content-Type": result.contentType}, this.config.globalHeaders, headers, result.headers)
Object.entries(combinedHeaders).forEach(entry => {
if (entry[1] == null) delete combinedHeaders[entry[0]] // set header value null to delete. this can be used to override globals from api. this is also jank
})
// array
if (result.constructor.name === "Array") {
result = {statusCode: result[0], content: result[1]}
}
// write head
if (!result.statusCode) throw new Error("Missing result.statusCode :: "+url.pathname)
res.writeHead(result.statusCode, combinedHeaders)
if (isHead) return res.end()
// write body
if (result.stream) {
// stream
if (this._shouldLog(url.pathname)) cf.log(`${url.pathname} [API] using stream`, "spam")
// res.on("end", () => console.log("end"))
// res.on("finish", () => console.log("finish"))
// res.on("close", () => console.log("close"))
// result.stream.on("end", () => console.log("readable end"))
// result.stream.on("close", () => console.log("readable close"))
result.stream.pipe(res)
req.on("aborted", () => {
console.log("aborted")
result.stream.destroy()
})
} else {
// not stream
if (!result.contentType) result.contentType = (typeof result.content === "object" ? "application/json" : "text/plain")
if (typeof result.content === "number" || (typeof result.content === "object" && ["Object", "Array"].includes(result.content.constructor.name))) result.content = JSON.stringify(result.content)
headers["Content-Length"] = Buffer.byteLength(result.content)
res.write(result.content)
res.end()
}
}).catch(err => {
this._handleError(req, res, url, headers, isHead, err)
})
} // end of while loop
return handled
}
/**
* @param {import("http").IncomingMessage} req
* @param {import("http").ServerResponse} res
* @param {URL} url
*/
_handleWithPage(req, res, url, headers, isHead) {
let i = 0
let handled = false
// try each handler
while (!handled && i < this.pageHandlers.length) {
const handler = this.pageHandlers[i]
i++
const re = new RegExp(`^${handler.web}$`)
const match = url.pathname.match(re)
if (!match) continue
handled = true
new Promise((resolve, reject) => {
if (handler.type === "pug") {
if (this.pugCache.has(handler.local)) {
resolve(this.pugCache.get(handler.local).web(Object.assign({req, url}, this.pugDefaultLocals)))
} else {
reject(symbols.PUG_SOURCE_NOT_FOUND)
}
} else if (handler.type === "sass") {
resolve(this.sassCache.get(handler.local))
} else if (handler.absolute) {
resolve(fs.promises.readFile(handler.local, "utf8"))
} else {
resolve(fs.promises.readFile(path.join(this.config.relativeRoot, handler.local), "utf8"))
}
}).then(page => {
headers["Content-Length"] = Buffer.byteLength(page)
if (url.searchParams.has("statichash") && !headers["Cache-Control"]) headers["Cache-Control"] = `max-age=${30*24*60*60}, public`
if (this._shouldLog(url.pathname)) cf.log(`[PAG] ${url.pathname} = ${handler.web} -> ${handler.local}`, "spam")
let contentType = mimeType(handler.web) || mimeType(handler.local)
if (handler.type === "pug") contentType = "text/html; charset=UTF-8"
res.writeHead(200, Object.assign({"Content-Type": contentType}, headers, this.config.globalHeaders))
if (isHead) return res.end()
res.write(page)
res.end()
}).catch(error => {
if (error === symbols.PUG_SOURCE_NOT_FOUND) {
return this._handleError(req, res, url, headers, isHead, "Pug source file not found.")
} else {
return this._handleError(req, res, url, headers, isHead, error)
}
})
} // end of while loop
return handled
}
/**
* @param {import("http").IncomingMessage} req
* @param {import("http").ServerResponse} res
* @param {URL} url
*/
_handleDirect(req, res, url, headers, isHead) {
// If THAT fails, try reading the html directory for a matching file
let filename = path.join(this.config.relativeRoot, this.config.filesDir, url.pathname)
let inPublic = filename.startsWith(path.join(this.config.relativeRoot, this.config.filesDir))
if (!inPublic) cf.log("Non-public access attempt caught!", "warning")
new Promise((resolve, reject) => {
if (!inPublic) return reject()
else return resolve(fs.promises.stat(filename))
}).then(stats => {
if (stats.isDirectory()) return Promise.reject()
if (this._shouldLog(url.pathname)) cf.log(`[DIR] ${url.pathname}`, "spam")
if (url.searchParams.has("statichash") && !headers["Cache-Control"]) headers["Cache-Control"] = `max-age=${30*24*60*60}, public`
let ranged = toRange(stats.size, headers, req)
headers["Content-Length"] = ranged.length
res.writeHead(ranged.statusCode, Object.assign({"Content-Type": mimeType(url.pathname)}, headers, this.config.globalHeaders))
if (isHead) return res.end()
let stream = fs.createReadStream(filename, {start: ranged.start, end: ranged.end})
stream.pipe(res)
}).catch(() => {
if (this._shouldLog(url.pathname)) cf.log(`[404] ${url.pathname}`, "spam")
if (this.notFoundTarget) {
// rewrite request to be that url instead
const params = new URLSearchParams()
params.set("pathname", url.pathname)
req.url = this.notFoundTarget + "?" + params.toString()
return this._handleRequest(req, res)
} else {
res.writeHead(404, Object.assign({"Content-Type": "text/plain; charset=UTF-8"}, this.config.globalHeaders))
if (isHead) return res.end()
res.write("404 Not Found")
res.end()
}
})
}
_handleError(req, res, url, headers, isHead, err) {
res.writeHead(500, {"Content-Type": "text/plain; charset=UTF-8"})
if (isHead) return res.end()
res.write(
` ◸―――――――――――――――――――――――――――――――――――――◹`
+`\n ···< | 500. That's an error. | >···`
+`\n ◺―――――――――――――――――――――――――――――――――――――◿`
+`\n\n`
+`\n ╱ Are you visiting this website? Not sure what's going on? ╲`
+`\n ╲ You might want to come back later. ╱`
+`\n\n\n`
+`\n`+(err && err.stack ? err.stack : err && err.message ? err.message : err)
)
res.end()
}
shutdown() {
this.server.close()
this.shutdownCallbacks.forEach(c => c())
}
}
module.exports.Pinski = Pinski