All files / lib readline.js

97.13% Statements 508/523
96.38% Branches 80/83
72.54% Functions 37/51
97.13% Lines 508/523

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 5242x 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 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 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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 426x 426x 1x 1x 425x 426x 426x 8x 8x 426x 1x 1x 1x 425x 425x 425x 425x 426x 8x 8x 426x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 30x 30x 14x 14x 30x 30x 10x 10x 2x 2x 8x 8x 4x 8x 8x 8x 4x 8x 8x 8x 4x 4x 10x 10x 28x 28x 28x 28x 2x 2x 12x 6x 6x 12x 12x 2x 2x 2x 10x 10x 10x 10x 10x 4x 2x 4x 4x 4x 2x 2x 4x 4x 10x 10x 10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 93x 93x 93x 2x 2x 2x 2x 2x 2x 2x 312x 312x 2x 2x 2x 2x 1206x 1206x 2x 2x 2x 2x 9292x 9292x 2x 2x 2x 2x 330x 330x 2x 2x 2x 2x 449x 449x 2x 2x 2x 2x 988x 988x 2x 2x 2x 2x 7434x 7434x 2x 2x 2x 2x 25x 25x 2x 2x 2x 2x 25x 25x 2x 2x 2x 2x 12x 12x 2x 2x 2x 2x 24x 24x 2x 2x 2x 2x 5x 5x 2x 2x 2x 2x 9x 9x 2x 2x 2x 2x 13x 13x 2x 2x 2x 2x 15x 15x 2x 2x 2x 2x 7x 7x 2x 2x 2x 2x 321x 321x 2x 2x 2x 2x 27x 27x 2x 2x 2x 2x 73x 73x 2x 2x 2x 2x 16884x 16884x 2x 2x 2x 2x 503x 503x 2x 2x 2x 2x 8756x 8756x 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 2x 2x   2x 2x 1x 2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 56x 2x 2x   2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 1x 24x 24x 25x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 81x 81x 81x 81x 80x 81x 81x 80x 81x 5x 1x   1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 5x 78x 78x 81x 1x 1x 1x 81x 81x 8x 8x 8x 8x 8x 8x 8x 81x 81x 69x 65x 65x 65x 65x 81x 81x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
 
'use strict';
 
const {
  DateNow,
  FunctionPrototypeBind,
  FunctionPrototypeCall,
  ObjectDefineProperties,
  ObjectSetPrototypeOf,
  Promise,
  PromiseReject,
  StringPrototypeSlice,
} = primordials;
 
const {
  clearLine,
  clearScreenDown,
  cursorTo,
  moveCursor,
} = require('internal/readline/callbacks');
const emitKeypressEvents = require('internal/readline/emitKeypressEvents');
const promises = require('readline/promises');
 
const {
  AbortError,
} = require('internal/errors');
const {
  inspect,
} = require('internal/util/inspect');
const {
  kEmptyObject,
  promisify,
} = require('internal/util');
const { validateAbortSignal } = require('internal/validators');
 
/**
 * @typedef {import('./stream.js').Readable} Readable
 * @typedef {import('./stream.js').Writable} Writable
 */
 
const {
  Interface: _Interface,
  InterfaceConstructor,
  kAddHistory,
  kDecoder,
  kDeleteLeft,
  kDeleteLineLeft,
  kDeleteLineRight,
  kDeleteRight,
  kDeleteWordLeft,
  kDeleteWordRight,
  kGetDisplayPos,
  kHistoryNext,
  kHistoryPrev,
  kInsertString,
  kLine,
  kLine_buffer,
  kMoveCursor,
  kNormalWrite,
  kOldPrompt,
  kOnLine,
  kPreviousKey,
  kPrompt,
  kQuestion,
  kQuestionCallback,
  kQuestionCancel,
  kRefreshLine,
  kSawKeyPress,
  kSawReturnAt,
  kSetRawMode,
  kTabComplete,
  kTabCompleter,
  kTtyWrite,
  kWordLeft,
  kWordRight,
  kWriteToOutput,
} = require('internal/readline/interface');
 
function Interface(input, output, completer, terminal) {
  if (!(this instanceof Interface)) {
    return new Interface(input, output, completer, terminal);
  }
 
  if (input?.input &&
      typeof input.completer === 'function' && input.completer.length !== 2) {
    const { completer } = input;
    input.completer = (v, cb) => cb(null, completer(v));
  } else if (typeof completer === 'function' && completer.length !== 2) {
    const realCompleter = completer;
    completer = (v, cb) => cb(null, realCompleter(v));
  }
 
  FunctionPrototypeCall(InterfaceConstructor, this,
                        input, output, completer, terminal);
 
  if (process.env.TERM === 'dumb') {
    this._ttyWrite = FunctionPrototypeBind(_ttyWriteDumb, this);
  }
}
 
ObjectSetPrototypeOf(Interface.prototype, _Interface.prototype);
ObjectSetPrototypeOf(Interface, _Interface);
 
/**
 * Displays `query` by writing it to the `output`.
 * @param {string} query
 * @param {{ signal?: AbortSignal; }} [options]
 * @param {Function} cb
 * @returns {void}
 */
Interface.prototype.question = function question(query, options, cb) {
  cb = typeof options === 'function' ? options : cb;
  if (options === null || typeof options !== 'object') {
    options = kEmptyObject;
  }
 
  if (options.signal) {
    validateAbortSignal(options.signal, 'options.signal');
    if (options.signal.aborted) {
      return;
    }
 
    const onAbort = () => {
      this[kQuestionCancel]();
    };
    options.signal.addEventListener('abort', onAbort, { once: true });
    const cleanup = () => {
      options.signal.removeEventListener(onAbort);
    };
    const originalCb = cb;
    cb = typeof cb === 'function' ? (answer) => {
      cleanup();
      return originalCb(answer);
    } : cleanup;
  }
 
  if (typeof cb === 'function') {
    this[kQuestion](query, cb);
  }
};
Interface.prototype.question[promisify.custom] = function question(query, options) {
  if (options === null || typeof options !== 'object') {
    options = kEmptyObject;
  }
 
  if (options.signal && options.signal.aborted) {
    return PromiseReject(
      new AbortError(undefined, { cause: options.signal.reason }));
  }
 
  return new Promise((resolve, reject) => {
    let cb = resolve;
 
    if (options.signal) {
      const onAbort = () => {
        reject(new AbortError(undefined, { cause: options.signal.reason }));
      };
      options.signal.addEventListener('abort', onAbort, { once: true });
      cb = (answer) => {
        options.signal.removeEventListener('abort', onAbort);
        resolve(answer);
      };
    }
 
    this.question(query, options, cb);
  });
};
 
/**
 * Creates a new `readline.Interface` instance.
 * @param {Readable | {
 *   input: Readable;
 *   output: Writable;
 *   completer?: Function;
 *   terminal?: boolean;
 *   history?: string[];
 *   historySize?: number;
 *   removeHistoryDuplicates?: boolean;
 *   prompt?: string;
 *   crlfDelay?: number;
 *   escapeCodeTimeout?: number;
 *   tabSize?: number;
 *   signal?: AbortSignal;
 *   }} input
 * @param {Writable} [output]
 * @param {Function} [completer]
 * @param {boolean} [terminal]
 * @returns {Interface}
 */
function createInterface(input, output, completer, terminal) {
  return new Interface(input, output, completer, terminal);
}
 
ObjectDefineProperties(Interface.prototype, {
  // Redirect internal prototype methods to the underscore notation for backward
  // compatibility.
  [kSetRawMode]: {
    __proto__: null,
    get() {
      return this._setRawMode;
    }
  },
  [kOnLine]: {
    __proto__: null,
    get() {
      return this._onLine;
    }
  },
  [kWriteToOutput]: {
    __proto__: null,
    get() {
      return this._writeToOutput;
    }
  },
  [kAddHistory]: {
    __proto__: null,
    get() {
      return this._addHistory;
    }
  },
  [kRefreshLine]: {
    __proto__: null,
    get() {
      return this._refreshLine;
    }
  },
  [kNormalWrite]: {
    __proto__: null,
    get() {
      return this._normalWrite;
    }
  },
  [kInsertString]: {
    __proto__: null,
    get() {
      return this._insertString;
    }
  },
  [kTabComplete]: {
    __proto__: null,
    get() {
      return this._tabComplete;
    }
  },
  [kWordLeft]: {
    __proto__: null,
    get() {
      return this._wordLeft;
    }
  },
  [kWordRight]: {
    __proto__: null,
    get() {
      return this._wordRight;
    }
  },
  [kDeleteLeft]: {
    __proto__: null,
    get() {
      return this._deleteLeft;
    }
  },
  [kDeleteRight]: {
    __proto__: null,
    get() {
      return this._deleteRight;
    }
  },
  [kDeleteWordLeft]: {
    __proto__: null,
    get() {
      return this._deleteWordLeft;
    }
  },
  [kDeleteWordRight]: {
    __proto__: null,
    get() {
      return this._deleteWordRight;
    }
  },
  [kDeleteLineLeft]: {
    __proto__: null,
    get() {
      return this._deleteLineLeft;
    }
  },
  [kDeleteLineRight]: {
    __proto__: null,
    get() {
      return this._deleteLineRight;
    }
  },
  [kLine]: {
    __proto__: null,
    get() {
      return this._line;
    }
  },
  [kHistoryNext]: {
    __proto__: null,
    get() {
      return this._historyNext;
    }
  },
  [kHistoryPrev]: {
    __proto__: null,
    get() {
      return this._historyPrev;
    }
  },
  [kGetDisplayPos]: {
    __proto__: null,
    get() {
      return this._getDisplayPos;
    }
  },
  [kMoveCursor]: {
    __proto__: null,
    get() {
      return this._moveCursor;
    }
  },
  [kTtyWrite]: {
    __proto__: null,
    get() {
      return this._ttyWrite;
    }
  },
 
  // Defining proxies for the internal instance properties for backward
  // compatibility.
  _decoder: {
    __proto__: null,
    get() {
      return this[kDecoder];
    },
    set(value) {
      this[kDecoder] = value;
    },
  },
  _line_buffer: {
    __proto__: null,
    get() {
      return this[kLine_buffer];
    },
    set(value) {
      this[kLine_buffer] = value;
    },
  },
  _oldPrompt: {
    __proto__: null,
    get() {
      return this[kOldPrompt];
    },
    set(value) {
      this[kOldPrompt] = value;
    },
  },
  _previousKey: {
    __proto__: null,
    get() {
      return this[kPreviousKey];
    },
    set(value) {
      this[kPreviousKey] = value;
    },
  },
  _prompt: {
    __proto__: null,
    get() {
      return this[kPrompt];
    },
    set(value) {
      this[kPrompt] = value;
    },
  },
  _questionCallback: {
    __proto__: null,
    get() {
      return this[kQuestionCallback];
    },
    set(value) {
      this[kQuestionCallback] = value;
    },
  },
  _sawKeyPress: {
    __proto__: null,
    get() {
      return this[kSawKeyPress];
    },
    set(value) {
      this[kSawKeyPress] = value;
    },
  },
  _sawReturnAt: {
    __proto__: null,
    get() {
      return this[kSawReturnAt];
    },
    set(value) {
      this[kSawReturnAt] = value;
    },
  },
});
 
// Make internal methods public for backward compatibility.
Interface.prototype._setRawMode = _Interface.prototype[kSetRawMode];
Interface.prototype._onLine = _Interface.prototype[kOnLine];
Interface.prototype._writeToOutput = _Interface.prototype[kWriteToOutput];
Interface.prototype._addHistory = _Interface.prototype[kAddHistory];
Interface.prototype._refreshLine = _Interface.prototype[kRefreshLine];
Interface.prototype._normalWrite = _Interface.prototype[kNormalWrite];
Interface.prototype._insertString = _Interface.prototype[kInsertString];
Interface.prototype._tabComplete = function(lastKeypressWasTab) {
  // Overriding parent method because `this.completer` in the legacy
  // implementation takes a callback instead of being an async function.
  this.pause();
  const string = StringPrototypeSlice(this.line, 0, this.cursor);
  this.completer(string, (err, value) => {
    this.resume();
 
    if (err) {
      this._writeToOutput(`Tab completion error: ${inspect(err)}`);
      return;
    }
 
    this[kTabCompleter](lastKeypressWasTab, value);
  });
};
Interface.prototype._wordLeft = _Interface.prototype[kWordLeft];
Interface.prototype._wordRight = _Interface.prototype[kWordRight];
Interface.prototype._deleteLeft = _Interface.prototype[kDeleteLeft];
Interface.prototype._deleteRight = _Interface.prototype[kDeleteRight];
Interface.prototype._deleteWordLeft = _Interface.prototype[kDeleteWordLeft];
Interface.prototype._deleteWordRight = _Interface.prototype[kDeleteWordRight];
Interface.prototype._deleteLineLeft = _Interface.prototype[kDeleteLineLeft];
Interface.prototype._deleteLineRight = _Interface.prototype[kDeleteLineRight];
Interface.prototype._line = _Interface.prototype[kLine];
Interface.prototype._historyNext = _Interface.prototype[kHistoryNext];
Interface.prototype._historyPrev = _Interface.prototype[kHistoryPrev];
Interface.prototype._getDisplayPos = _Interface.prototype[kGetDisplayPos];
Interface.prototype._getCursorPos = _Interface.prototype.getCursorPos;
Interface.prototype._moveCursor = _Interface.prototype[kMoveCursor];
Interface.prototype._ttyWrite = _Interface.prototype[kTtyWrite];
 
function _ttyWriteDumb(s, key) {
  key = key || kEmptyObject;
 
  if (key.name === 'escape') return;
 
  if (this[kSawReturnAt] && key.name !== 'enter')
    this[kSawReturnAt] = 0;
 
  if (key.ctrl) {
    if (key.name === 'c') {
      if (this.listenerCount('SIGINT') > 0) {
        this.emit('SIGINT');
      } else {
        // This readline instance is finished
        this.close();
      }
 
      return;
    } else if (key.name === 'd') {
      this.close();
      return;
    }
  }
 
  switch (key.name) {
    case 'return':  // Carriage return, i.e. \r
      this[kSawReturnAt] = DateNow();
      this._line();
      break;
 
    case 'enter':
      // When key interval > crlfDelay
      if (this[kSawReturnAt] === 0 ||
          DateNow() - this[kSawReturnAt] > this.crlfDelay) {
        this._line();
      }
      this[kSawReturnAt] = 0;
      break;
 
    default:
      if (typeof s === 'string' && s) {
        this.line += s;
        this.cursor += s.length;
        this._writeToOutput(s);
      }
  }
}
 
module.exports = {
  Interface,
  clearLine,
  clearScreenDown,
  createInterface,
  cursorTo,
  emitKeypressEvents,
  moveCursor,
  promises,
};