All files / lib/readline promises.js

100% Statements 65/65
100% Branches 11/11
100% Functions 5/5
100% Lines 65/65

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 62 63 64 65 662x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 144x 144x 2x 20x 20x 20x 20x 10x 10x 4x 4x 4x 6x 6x 4x 4x 6x 6x 6x 4x 4x 6x 6x 16x 16x 20x 20x 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');
 
const {
  kEmptyObject,
} = require('internal/util');
 
class Interface extends _Interface {
  // eslint-disable-next-line no-useless-constructor
  constructor(input, output, completer, terminal) {
    super(input, output, completer, terminal);
  }
  question(query, options = kEmptyObject) {
    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,
};