|
| 1 | +import http from 'http'; |
| 2 | +import { AddressInfo } from 'net'; |
| 3 | + |
| 4 | +import { serializeError } from 'serialize-error'; |
| 5 | + |
| 6 | +import { log } from '../utils/logging'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Create an HTTP request listener based on the supplied function. |
| 10 | + * |
| 11 | + * - The request body is JSON parsed and passed into the function as parameters. |
| 12 | + * - The function's return value is JSON stringified into the response body. |
| 13 | + */ |
| 14 | +export const createRequestListenerFromFunction = ( |
| 15 | + fn: (...args: unknown[]) => unknown | Promise<unknown>, |
| 16 | +): http.RequestListener => async (req, res) => { |
| 17 | + const writeJsonResponse = (statusCode: number, jsonResponse: unknown) => |
| 18 | + new Promise<void>((resolve) => |
| 19 | + res |
| 20 | + .writeHead(statusCode, { 'Content-Type': 'application/json' }) |
| 21 | + .end(JSON.stringify(jsonResponse, null, 2), 'utf8', resolve), |
| 22 | + ); |
| 23 | + |
| 24 | + try { |
| 25 | + const requestBody = await new Promise<string>((resolve, reject) => { |
| 26 | + let data = ''; |
| 27 | + |
| 28 | + req |
| 29 | + .on('data', (chunk) => (data += chunk)) |
| 30 | + .on('end', () => resolve(data)) |
| 31 | + .on('error', (err) => reject(err)); |
| 32 | + }); |
| 33 | + |
| 34 | + const jsonRequest: unknown = JSON.parse(requestBody); |
| 35 | + |
| 36 | + // Pass a non-array request body as the first parameter |
| 37 | + const args = Array.isArray(jsonRequest) ? jsonRequest : [jsonRequest]; |
| 38 | + |
| 39 | + const response: unknown = await fn(...args); |
| 40 | + |
| 41 | + await writeJsonResponse(200, response); |
| 42 | + } catch (err: unknown) { |
| 43 | + await writeJsonResponse(500, serializeError(err)); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * Create a HTTP server based on the supplied `http.RequestListener`. |
| 49 | + * |
| 50 | + * This function resolves when the server is closed. |
| 51 | + */ |
| 52 | +export const serveRequestListener = ( |
| 53 | + requestListener: http.RequestListener, |
| 54 | + port?: number, |
| 55 | +) => { |
| 56 | + const server = http.createServer(requestListener); |
| 57 | + |
| 58 | + return new Promise<void>((resolve, reject) => |
| 59 | + server |
| 60 | + .listen(port) |
| 61 | + .on('close', resolve) |
| 62 | + .on('error', reject) |
| 63 | + .on('listening', () => { |
| 64 | + const address = server.address() as AddressInfo; |
| 65 | + |
| 66 | + log.ok('listening on port', log.bold(address.port)); |
| 67 | + }), |
| 68 | + ); |
| 69 | +}; |
0 commit comments