All files / lib/readline promises.js

96.66% Statements 58/60
100% Branches 9/9
80% Functions 4/5
96.66% Lines 58/60

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 612x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 136x 136x 2x 10x 10x 10x 10x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 8x 8x 10x 10x 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());
        }
 
        const onAbort = () => {
          this[kQuestionCancel]();
          reject(new AbortError());
        };
        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,
};