• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Tab completion sometimes uses a separate REPL instance under the hood.
2// That REPL instance has its own domain. Make sure domain errors trickle back
3// up to the main REPL.
4//
5// Ref: https://github.com/nodejs/node/issues/21586
6
7'use strict';
8
9const { Stream } = require('stream');
10function noop() {}
11
12// A stream to push an array into a REPL
13function ArrayStream() {
14  this.run = function(data) {
15    data.forEach((line) => {
16      this.emit('data', `${line}\n`);
17    });
18  };
19}
20
21Object.setPrototypeOf(ArrayStream.prototype, Stream.prototype);
22Object.setPrototypeOf(ArrayStream, Stream);
23ArrayStream.prototype.readable = true;
24ArrayStream.prototype.writable = true;
25ArrayStream.prototype.pause = noop;
26ArrayStream.prototype.resume = noop;
27ArrayStream.prototype.write = noop;
28
29const repl = require('repl');
30
31const putIn = new ArrayStream();
32const testMe = repl.start('', putIn);
33
34// Some errors are passed to the domain, but do not callback.
35testMe._domain.on('error', function(err) {
36  throw err;
37});
38
39// Nesting of structures causes REPL to use a nested REPL for completion.
40putIn.run([
41  'var top = function() {',
42  'r = function test (',
43  ' one, two) {',
44  'var inner = {',
45  ' one:1',
46  '};'
47]);
48
49// In Node.js 10.11.0, this next line will terminate the repl silently...
50testMe.complete('inner.o', () => { throw new Error('fhqwhgads'); });