All files / lib/readline promises.js

100% Statements 61/61
100% Branches 10/10
100% Functions 5/5
100% Lines 61/61

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 622x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 140x 140x 2x 14x 14x 14x 14x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 4x 4x 10x 10x 14x 14x 2x 2x 32x 32x 32x 2x 2x 2x 2x 2x 2x  
'use strict';
 
const {
  Promise,
} = primordials;
 
const {
  Readline,
} = require('internal/readline/promises');
 
const {
  Interface: _Interface,
  kQuestionCancel,
} = require('internal/readline/interface');
 
const {
  AbortError,
} = require('internal/errors');
const { validateAbortSignal } = require('internal/validators');
 
class Interface extends _Interface {
  // eslint-disable-next-line no-useless-constructor
  constructor(input, output, completer, terminal) {
    super(input, output, completer, terminal);
  }
  question(query, options = {}) {
    return new Promise((resolve, reject) => {
      let cb = resolve;
 
      if (options?.signal) {
        validateAbortSignal(options.signal, 'options.signal');
        if (options.signal.aborted) {
          return reject(
            new AbortError(undefined, { cause: options.signal.reason }));
        }
 
        const onAbort = () => {
          this[kQuestionCancel]();
          reject(new AbortError(undefined, { cause: options.signal.reason }));
        };
        options.signal.addEventListener('abort', onAbort, { once: true });
        cb = (answer) => {
          options.signal.removeEventListener('abort', onAbort);
          resolve(answer);
        };
      }
 
      super.question(query, cb);
    });
  }
}
 
function createInterface(input, output, completer, terminal) {
  return new Interface(input, output, completer, terminal);
}
 
module.exports = {
  Interface,
  Readline,
  createInterface,
};