1'use strict'; 2const common = require('../common'); 3 4common.skipIfInspectorDisabled(); 5 6const fixtures = require('../common/fixtures'); 7const startCLI = require('../common/debugger'); 8 9const assert = require('assert'); 10const { spawn } = require('child_process'); 11 12// NOTE(oyyd): We might want to import this regexp from "lib/_inspect.js"? 13const kDebuggerMsgReg = /Debugger listening on ws:\/\/\[?(.+?)\]?:(\d+)\//; 14 15function launchTarget(...args) { 16 const childProc = spawn(process.execPath, args); 17 return new Promise((resolve, reject) => { 18 const onExit = () => { 19 reject(new Error('Child process exits unexpectedly')); 20 }; 21 childProc.on('exit', onExit); 22 childProc.stderr.setEncoding('utf8'); 23 let data = ''; 24 childProc.stderr.on('data', (chunk) => { 25 data += chunk; 26 const ret = kDebuggerMsgReg.exec(data); 27 childProc.removeListener('exit', onExit); 28 if (ret) { 29 resolve({ 30 childProc, 31 host: ret[1], 32 port: ret[2], 33 }); 34 } 35 }); 36 }); 37} 38 39{ 40 const script = fixtures.path('debugger/alive.js'); 41 let cli = null; 42 let target = null; 43 44 function cleanup(error) { 45 if (cli) { 46 cli.quit(); 47 cli = null; 48 } 49 if (target) { 50 target.kill(); 51 target = null; 52 } 53 assert.ifError(error); 54 } 55 56 return launchTarget('--inspect=0', script) 57 .then(({ childProc, host, port }) => { 58 target = childProc; 59 cli = startCLI([`${host || '127.0.0.1'}:${port}`]); 60 return cli.waitForPrompt(); 61 }) 62 .then(() => cli.command('sb("alive.js", 3)')) 63 .then(() => cli.waitFor(/break/)) 64 .then(() => cli.waitForPrompt()) 65 .then(() => { 66 assert.match( 67 cli.output, 68 /> 3 \+\+x;/, 69 'marks the 3rd line'); 70 }) 71 .then(() => cleanup()) 72 .then(null, cleanup); 73} 74