UNPKG

@gullerya/just-test

Version:

JavaScript multiplatform tests runner

49 lines (41 loc) 1.5 kB
import fs from 'node:fs/promises'; import path from 'node:path'; import { STATUS_CODES } from 'node:http'; import Logger from '../logger/logger.js'; import { RequestHandlerBase } from './request-handler-base.js'; import { findMimeType, extensionsMap } from '../server-utils.js'; export default class RunnerLibsRequestHandler extends RequestHandlerBase { #logger = new Logger({ context: 'handler libs' }); constructor() { super(); this.#logger.info(`libs requests handler initialized; basePath: '${this.basePath}'`); } get basePath() { return 'libs'; } async handle(handlerRelativePath, req, res) { if (req.method !== 'GET') { this.#logger.warn(`sending 405 for '${req.method} ${this.basePath}/${handlerRelativePath}'`); res.writeHead(405).end(STATUS_CODES[405]); return; } try { const filePath = path.join('node_modules', handlerRelativePath); this.#logger.info(`serving '${filePath}' for '${handlerRelativePath}'`); const contentType = findMimeType(filePath, extensionsMap.js); const content = await fs.readFile(filePath, { encoding: 'utf-8' }); res.writeHead(200, { 'Content-Type': contentType, 'Cache-Control': 'public, max-age=604800' }).end(content); } catch (error) { if (error.code === 'ENOENT') { this.#logger.warn(`sending 404 for '${req.method} ${this.basePath}/${handlerRelativePath}'`); res.writeHead(404).end(); } else { throw error; } } return true; } }