All files / src/util http.ts

100% Statements 12/12
60% Branches 3/5
100% Functions 6/6
100% Lines 9/9

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        44x       52x       52x 51x   51x     44x 48x 48x 48x    
import { Application } from 'express';
import { ListenOptions } from 'net';
import http from 'http';
 
export const listenPromise = (
  serverLike: http.Server | Application,
  listenOptions: ListenOptions = {}
): Promise<http.Server> =>
  new Promise((resolve, reject) => {
    // express 5's `app.listen` forwards bind errors to the listen callback (error-first),
    // whereas raw http.Server.listen signals them via the 'error' event — handle both so a
    // failed bind (e.g. EADDRINUSE) rejects instead of resolving with an unbound server.
    const server = serverLike.listen(listenOptions, (error?: Error) =>
      error ? reject(error) : resolve(server)
    ) as http.Server;
    server.on('error', reject);
  });
 
export const serverClosePromise = (server: http.Server): Promise<void> =>
  new Promise((resolve, reject) => {
    server.once('close', resolve);
    server.close((error) => (error ? reject(error) : null));
  });