Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 11x 3x 11x 11x 11x 3x 3x 1x 1x 1x 3x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 4x 8x 2x 2x 1x 1x 1x 2x 1x 2x 1x 1x 1x 1x 3x 3x 1x 1x 4x 1x 1x | import { once } from 'node:events' import { IncomingMessage, ServerResponse } from 'node:http' import { CONTENT_TYPE, CONTROLLER_PAGE, HOME_PAGE, LOCATION } from './config.js' import Controller from './controller.js' import { logger } from './util.js' const controller = new Controller() /** * @param {IncomingMessage} request * @param {ServerResponse} response */ export function requestListener (request, response) { return routes(request, response) .catch(error => errorHandler(request, response, error)) } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ async function routes (request, response) { const method = request.method.toLowerCase() const route = Methods[method] || Responses.notFound return route(request, response) } /** * @param {IncomingMessage} request * @param {ServerResponse} response * @param {Error} error */ function errorHandler (request, response, error) { const codes = { 'BAD_REQUEST': Responses.badRequest, 'ENOENT': Responses.notFound, 'ERR_STREAM_PREMATURE_CLOSE': Responses.ok } const handler = codes[error.code] || function () { logger.error(error.message) logger.error(error.stack) return Responses.internalError(request, response) } return handler(request, response) } class Methods { /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static get (request, response) { const { url } = request const paths = { '/': () => { response.writeHead(302, { location: LOCATION.HOME }) return response.end() }, '/controller': async () => { const { stream } = await controller.getFileStream(CONTROLLER_PAGE) return stream.pipe(response) }, '/home': async () => { const { stream } = await controller.getFileStream(HOME_PAGE) return stream.pipe(response) }, '/stream': async () => { const { stream, onClose } = controller.createClientStream() request.once('close', onClose) response.writeHead(200, { 'Content-Type': 'audio/mpeg', 'Accpept-Ranges': 'bytes', 'Cache-Control': 'no-cache' }) return stream.pipe(response) } } const path = paths[url] || function () { return serveFile(request, response) } return path() } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static post (request, response) { const { url } = request const paths = { '/controller': async () => { const data = JSON.parse(await once(request, 'data')) await controller.handleCommand(data) return Responses.ok(request, response) } } const path = paths[url] || function () { return Responses.notFound(request, response) } return path() } } class Responses { /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static badRequest (request, response) { response.writeHead(400) return response.end() } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static internalError (request, response) { response.writeHead(500) return response.end() } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static notFound (request, response) { response.writeHead(404) return response.end() } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ static ok (request, response) { response.writeHead(200) return response.end() } } /** * @param {IncomingMessage} request * @param {ServerResponse} response */ async function serveFile (request, response) { const { stream, type } = await controller.getFileStream(request.url) response.writeHead(200, { 'Content-Type': CONTENT_TYPE[type] }) return stream.pipe(response) } |