@ -4,61 +4,61 @@ const assert = require('assert');
const fs = require ( 'fs' ) ;
const fs = require ( 'fs' ) ;
const http = require ( 'http' ) ;
const http = require ( 'http' ) ;
const path = require ( 'path' ) ;
const path = require ( 'path' ) ;
const spawn = require ( 'child_process' ) . spawn ;
const { spawn } = require ( 'child_process' ) ;
const url = require ( 'url' ) ;
const url = require ( 'url' ) ;
const _ MAINSCRIPT = path . join ( common . fixturesDir , 'loop.js' ) ;
const DEBUG = false ;
const DEBUG = false ;
const TIMEOUT = 15 * 1000 ;
const TIMEOUT = 15 * 1000 ;
const EXPECT_ALIVE_SYMBOL = Symbol ( 'isAlive' ) ;
const DONT_EXPECT_RESPONSE_SYMBOL = Symbol ( 'dontExpectResponse' ) ;
const mainScript = path . join ( common . fixturesDir , 'loop.js' ) ;
function send ( socket , message , id , callback ) {
function spawnChildProcess ( inspectorFlags , scriptContents , scriptFile ) {
const msg = JSON . parse ( JSON . stringify ( message ) ) ; // Clone!
const args = [ ] . concat ( inspectorFlags ) ;
msg [ 'id' ] = id ;
if ( scriptContents ) {
if ( DEBUG )
args . push ( '-e' , scriptContents ) ;
console . log ( '[sent]' , JSON . stringify ( msg ) ) ;
} else {
const messageBuf = Buffer . from ( JSON . stringify ( msg ) ) ;
args . push ( scriptFile ) ;
}
const child = spawn ( process . execPath , args ) ;
const wsHeaderBuf = Buffer . allocUnsafe ( 16 ) ;
const handler = tearDown . bind ( null , child ) ;
wsHeaderBuf . writeUInt8 ( 0x81 , 0 ) ;
process . on ( 'exit' , handler ) ;
let byte2 = 0x80 ;
process . on ( 'uncaughtException' , handler ) ;
const bodyLen = messageBuf . length ;
process . on ( 'unhandledRejection' , handler ) ;
process . on ( 'SIGINT' , handler ) ;
let maskOffset = 2 ;
return child ;
if ( bodyLen < 126 ) {
byte2 = 0x80 + bodyLen ;
} else if ( bodyLen < 65536 ) {
byte2 = 0xFE ;
wsHeaderBuf . writeUInt16BE ( bodyLen , 2 ) ;
maskOffset = 4 ;
} else {
byte2 = 0xFF ;
wsHeaderBuf . writeUInt32BE ( bodyLen , 2 ) ;
wsHeaderBuf . writeUInt32BE ( 0 , 6 ) ;
maskOffset = 10 ;
}
}
wsHeaderBuf . writeUInt8 ( byte2 , 1 ) ;
wsHeaderBuf . writeUInt32BE ( 0x01020408 , maskOffset ) ;
for ( let i = 0 ; i < messageBuf . length ; i ++ )
function makeBufferingDataCallback ( dataCallback ) {
messageBuf [ i ] = messageBuf [ i ] ^ ( 1 << ( i % 4 ) ) ;
let buffer = Buffer . alloc ( 0 ) ;
socket . write (
return ( data ) => {
Buffer . concat ( [ wsHeaderBuf . slice ( 0 , maskOffset + 4 ) , messageBuf ] ) ,
const newData = Buffer . concat ( [ buffer , data ] ) ;
callback ) ;
const str = newData . toString ( 'utf8' ) ;
const lines = str . replace ( /\r/g , '' ) . split ( '\n' ) ;
if ( str . endsWith ( '\n' ) )
buffer = Buffer . alloc ( 0 ) ;
else
buffer = Buffer . from ( lines . pop ( ) , 'utf8' ) ;
for ( const line of lines )
dataCallback ( line ) ;
} ;
}
}
function sendEnd ( socket ) {
function tearDown ( child , err ) {
socket . write ( Buffer . from ( [ 0x88 , 0x80 , 0x2D , 0x0E , 0x1E , 0xFA ] ) ) ;
child . kill ( ) ;
if ( err ) {
console . error ( err ) ;
process . exit ( 1 ) ;
}
}
}
function parseWSFrame ( buffer , handler ) {
function parseWSFrame ( buffer ) {
// Protocol described in https://tools.ietf.org/html/rfc6455#section-5
// Protocol described in https://tools.ietf.org/html/rfc6455#section-5
let message = null ;
if ( buffer . length < 2 )
if ( buffer . length < 2 )
return 0 ;
return { length : 0 , message } ;
if ( buffer [ 0 ] === 0x88 && buffer [ 1 ] === 0x00 ) {
if ( buffer [ 0 ] === 0x88 && buffer [ 1 ] === 0x00 ) {
handler ( null ) ;
return { length : 2 , message , closed : true } ;
return 2 ;
}
}
assert . strictEqual ( 0x81 , buffer [ 0 ] ) ;
assert . strictEqual ( 0x81 , buffer [ 0 ] ) ;
let dataLen = 0x7F & buffer [ 1 ] ;
let dataLen = 0x7F & buffer [ 1 ] ;
@ -74,10 +74,9 @@ function parseWSFrame(buffer, handler) {
bodyOffset = 10 ;
bodyOffset = 10 ;
}
}
if ( buffer . length < bodyOffset + dataLen )
if ( buffer . length < bodyOffset + dataLen )
return 0 ;
return { length : 0 , message } ;
const jsonPayload =
const jsonPayload =
buffer . slice ( bodyOffset , bodyOffset + dataLen ) . toString ( 'utf8' ) ;
buffer . slice ( bodyOffset , bodyOffset + dataLen ) . toString ( 'utf8' ) ;
let message ;
try {
try {
message = JSON . parse ( jsonPayload ) ;
message = JSON . parse ( jsonPayload ) ;
} catch ( e ) {
} catch ( e ) {
@ -86,368 +85,285 @@ function parseWSFrame(buffer, handler) {
}
}
if ( DEBUG )
if ( DEBUG )
console . log ( '[received]' , JSON . stringify ( message ) ) ;
console . log ( '[received]' , JSON . stringify ( message ) ) ;
handler ( message ) ;
return { length : bodyOffset + dataLen , message } ;
return bodyOffset + dataLen ;
}
}
function tearDown ( child , err ) {
function formatWSFrame ( message ) {
child . kill ( ) ;
const messageBuf = Buffer . from ( JSON . stringify ( message ) ) ;
if ( err instanceof Error ) {
console . error ( err . stack ) ;
process . exit ( 1 ) ;
}
}
function checkHttpResponse ( host , port , path , callback , errorcb ) {
const wsHeaderBuf = Buffer . allocUnsafe ( 16 ) ;
const req = http . get ( { host , port , path } , function ( res ) {
wsHeaderBuf . writeUInt8 ( 0x81 , 0 ) ;
let response = '' ;
let byte2 = 0x80 ;
res . setEncoding ( 'utf8' ) ;
const bodyLen = messageBuf . length ;
res
. on ( 'data' , ( data ) => response += data . toString ( ) )
. on ( 'end' , ( ) => {
let err = null ;
let json = undefined ;
try {
json = JSON . parse ( response ) ;
} catch ( e ) {
err = e ;
err . response = response ;
}
callback ( err , json ) ;
} ) ;
} ) ;
if ( errorcb )
req . on ( 'error' , errorcb ) ;
}
function makeBufferingDataCallback ( dataCallback ) {
let maskOffset = 2 ;
let buffer = Buffer . alloc ( 0 ) ;
if ( bodyLen < 126 ) {
return ( data ) => {
byte2 = 0x80 + bodyLen ;
const newData = Buffer . concat ( [ buffer , data ] ) ;
} else if ( bodyLen < 65536 ) {
const str = newData . toString ( 'utf8' ) ;
byte2 = 0xFE ;
const lines = str . split ( '\n' ) ;
wsHeaderBuf . writeUInt16BE ( bodyLen , 2 ) ;
if ( str . endsWith ( '\n' ) )
maskOffset = 4 ;
buffer = Buffer . alloc ( 0 ) ;
} else {
else
byte2 = 0xFF ;
buffer = Buffer . from ( lines . pop ( ) , 'utf8' ) ;
wsHeaderBuf . writeUInt32BE ( bodyLen , 2 ) ;
for ( const line of lines )
wsHeaderBuf . writeUInt32BE ( 0 , 6 ) ;
dataCallback ( line ) ;
maskOffset = 10 ;
} ;
}
}
wsHeaderBuf . writeUInt8 ( byte2 , 1 ) ;
wsHeaderBuf . writeUInt32BE ( 0x01020408 , maskOffset ) ;
function timeout ( message , multiplicator ) {
for ( let i = 0 ; i < messageBuf . length ; i ++ )
return setTimeout ( common . mustNotCall ( message ) ,
messageBuf [ i ] = messageBuf [ i ] ^ ( 1 << ( i % 4 ) ) ;
TIMEOUT * ( multiplicator || 1 ) ) ;
}
function TestSession ( socket , harness ) {
return Buffer . concat ( [ wsHeaderBuf . slice ( 0 , maskOffset + 4 ) , messageBuf ] ) ;
this . mainScriptPath = harness . mainScriptPath ;
}
this . mainScriptId = null ;
this . harness_ = harness ;
class InspectorSession {
this . socket_ = socket ;
constructor ( socket , instance ) {
this . expectClose_ = false ;
this . _ instance = instance ;
this . scripts_ = { } ;
this . _ socket = socket ;
this . messagefilter_ = null ;
this . _ nextId = 1 ;
this . responseCheckers_ = { } ;
this . _ commandResponsePromises = new Map ( ) ;
this . lastId_ = 0 ;
this . _ unprocessedNotifications = [ ] ;
this . messages_ = { } ;
this . _ notificationCallback = null ;
this . expectedId_ = 1 ;
this . _ scriptsIdsByUrl = new Map ( ) ;
this . lastMessageResponseCallback_ = null ;
this . closeCallback_ = null ;
let buffer = Buffer . alloc ( 0 ) ;
let buffer = Buffer . alloc ( 0 ) ;
socket . on ( 'data' , ( data ) => {
socket . on ( 'data' , ( data ) => {
buffer = Buffer . concat ( [ buffer , data ] ) ;
buffer = Buffer . concat ( [ buffer , data ] ) ;
let consumed ;
do {
do {
consumed = parseWSFrame ( buffer , this . processMessage_ . bind ( this ) ) ;
const { length , message , closed } = parseWSFrame ( buffer ) ;
if ( consumed )
if ( ! length )
buffer = buffer . slice ( consumed ) ;
break ;
} while ( consumed ) ;
} ) . on ( 'close' , ( ) => {
if ( closed ) {
assert ( this . expectClose_ , 'Socket closed prematurely' ) ;
socket . write ( Buffer . from ( [ 0x88 , 0x00 ] ) ) ; // WS close frame
this . closeCallback_ && this . closeCallback_ ( ) ;
}
buffer = buffer . slice ( length ) ;
if ( message )
this . _ onMessage ( message ) ;
} while ( true ) ;
} ) ;
this . _ terminationPromise = new Promise ( ( resolve ) => {
socket . once ( 'close' , resolve ) ;
} ) ;
} ) ;
}
}
TestSession . prototype . scriptUrlForId = function ( id ) {
waitForServerDisconnect ( ) {
return this . scripts_ [ id ] ;
return this . _ terminationPromise ;
} ;
}
TestSession . prototype . processMessage_ = function ( message ) {
disconnect ( ) {
if ( message === null ) {
this . _ socket . destroy ( ) ;
sendEnd ( this . socket_ ) ;
return ;
}
}
const method = message [ 'method' ] ;
_ onMessage ( message ) {
if ( method === 'Debugger.scriptParsed' ) {
if ( message . id ) {
const { resolve , reject } = this . _ commandResponsePromises . get ( message . id ) ;
this . _ commandResponsePromises . delete ( message . id ) ;
if ( message . result )
resolve ( message . result ) ;
else
reject ( message . error ) ;
} else {
if ( message . method === 'Debugger.scriptParsed' ) {
const script = message [ 'params' ] ;
const script = message [ 'params' ] ;
const scriptId = script [ 'scriptId' ] ;
const scriptId = script [ 'scriptId' ] ;
const url = script [ 'url' ] ;
const url = script [ 'url' ] ;
this . scripts_ [ scriptId ] = url ;
this . _ scriptsIdsByUrl . set ( scriptId , url ) ;
if ( url === mainScript )
if ( url === _ MAINSCRIPT )
this . mainScriptId = scriptId ;
this . mainScriptId = scriptId ;
}
}
this . messagefilter_ && this . messagefilter_ ( message ) ;
const id = message [ 'id' ] ;
if ( id ) {
this . expectedId_ ++ ;
if ( this . responseCheckers_ [ id ] ) {
const messageJSON = JSON . stringify ( message ) ;
const idJSON = JSON . stringify ( this . messages_ [ id ] ) ;
assert ( message [ 'result' ] , ` ${ messageJSON } (response to ${ idJSON } ) ` ) ;
this . responseCheckers_ [ id ] ( message [ 'result' ] ) ;
delete this . responseCheckers_ [ id ] ;
}
const messageJSON = JSON . stringify ( message ) ;
const idJSON = JSON . stringify ( this . messages_ [ id ] ) ;
assert ( ! message [ 'error' ] , ` ${ messageJSON } (replying to ${ idJSON } ) ` ) ;
delete this . messages_ [ id ] ;
if ( id === this . lastId_ ) {
this . lastMessageResponseCallback_ && this . lastMessageResponseCallback_ ( ) ;
this . lastMessageResponseCallback_ = null ;
}
}
} ;
TestSession . prototype . sendAll_ = function ( commands , callback ) {
if ( this . _ notificationCallback ) {
if ( ! commands . length ) {
// In case callback needs to install another
callback ( ) ;
const callback = this . _ notificationCallback ;
} else {
this . _ notificationCallback = null ;
let id = ++ this . lastId_ ;
callback ( message ) ;
let command = commands [ 0 ] ;
if ( command instanceof Array ) {
this . responseCheckers_ [ id ] = command [ 1 ] ;
command = command [ 0 ] ;
}
if ( command instanceof Function )
command = command ( ) ;
if ( ! command [ DONT_EXPECT_RESPONSE_SYMBOL ] ) {
this . messages_ [ id ] = command ;
} else {
} else {
id += 100000 ;
this . _ unprocessedNotifications . push ( message ) ;
this . lastId_ -- ;
}
}
send ( this . socket_ , command , id ,
( ) => this . sendAll_ ( commands . slice ( 1 ) , callback ) ) ;
}
}
} ;
TestSession . prototype . sendInspectorCommands = function ( commands ) {
if ( ! ( commands instanceof Array ) )
commands = [ commands ] ;
return this . enqueue ( ( callback ) => {
let timeoutId = null ;
this . lastMessageResponseCallback_ = ( ) => {
timeoutId && clearTimeout ( timeoutId ) ;
callback ( ) ;
} ;
this . sendAll_ ( commands , ( ) => {
timeoutId = setTimeout ( ( ) => {
assert . fail ( ` Messages without response: ${
Object . keys ( this . messages_ ) . join ( ', ' ) } ` );
} , TIMEOUT ) ;
} ) ;
} ) ;
} ;
TestSession . prototype . sendCommandsAndExpectClose = function ( commands ) {
if ( ! ( commands instanceof Array ) )
commands = [ commands ] ;
return this . enqueue ( ( callback ) => {
let timeoutId = null ;
let done = false ;
this . expectClose_ = true ;
this . closeCallback_ = function ( ) {
if ( timeoutId )
clearTimeout ( timeoutId ) ;
done = true ;
callback ( ) ;
} ;
this . sendAll_ ( commands , ( ) => {
if ( ! done ) {
timeoutId = timeout ( 'Session still open' ) ;
}
}
} ) ;
} ) ;
} ;
TestSession . prototype . createCallbackWithTimeout_ = function ( message ) {
_ sendMessage ( message ) {
const promise = new Promise ( ( resolve ) => {
const msg = JSON . parse ( JSON . stringify ( message ) ) ; // Clone!
this . enqueue ( ( callback ) => {
msg [ 'id' ] = this . _ nextId ++ ;
const timeoutId = timeout ( message ) ;
if ( DEBUG )
resolve ( ( ) => {
console . log ( '[sent]' , JSON . stringify ( msg ) ) ;
clearTimeout ( timeoutId ) ;
callback ( ) ;
} ) ;
} ) ;
} ) ;
return ( ) => promise . then ( ( callback ) => callback ( ) ) ;
} ;
TestSession . prototype . expectMessages = function ( expects ) {
const responsePromise = new Promise ( ( resolve , reject ) => {
if ( ! ( expects instanceof Array ) ) expects = [ expects ] ;
this . _ commandResponsePromises . set ( msg [ 'id' ] , { resolve , reject } ) ;
} ) ;
const callback = this . createCallbackWithTimeout_ (
return new Promise (
` Matching response was not received: \n ${ expects [ 0 ] } ` ) ;
( resolve ) => this . _ socket . write ( formatWSFrame ( msg ) , resolve ) )
this . messagefilter_ = ( message ) => {
. then ( ( ) => responsePromise ) ;
if ( expects [ 0 ] ( message ) )
expects . shift ( ) ;
if ( ! expects . length ) {
this . messagefilter_ = null ;
callback ( ) ;
}
}
} ;
return this ;
} ;
TestSession . prototype . expectStderrOutput = function ( regexp ) {
send ( commands ) {
this . harness_ . addStderrFilter (
if ( Array . isArray ( commands ) ) {
regexp ,
// Multiple commands means the response does not matter. There might even
this . createCallbackWithTimeout_ ( ` Timed out waiting for ${ regexp } ` ) ) ;
// never be a response.
return this ;
return Promise
} ;
. all ( commands . map ( ( command ) => this . _ sendMessage ( command ) ) )
. then ( ( ) => { } ) ;
} else {
return this . _ sendMessage ( commands ) ;
}
}
TestSession . prototype . runNext_ = function ( ) {
waitForNotification ( methodOrPredicate , description ) {
if ( this . task_ ) {
const desc = description || methodOrPredicate ;
setImmediate ( ( ) => {
const message = ` Timed out waiting for matching notification ( ${ desc } )) ` ;
this . task_ ( ( ) => {
return common . fires (
this . task_ = this . task_ . next_ ;
this . _ asyncWaitForNotification ( methodOrPredicate ) , message , TIMEOUT ) ;
this . runNext_ ( ) ;
} ) ;
} ) ;
}
}
} ;
TestSession . prototype . enqueue = function ( task ) {
async _ asyncWaitForNotification ( methodOrPredicate ) {
if ( ! this . task_ ) {
function matchMethod ( notification ) {
this . task_ = task ;
return notification . method === methodOrPredicate ;
this . runNext_ ( ) ;
}
const predicate =
typeof methodOrPredicate === 'string' ? matchMethod : methodOrPredicate ;
let notification = null ;
do {
if ( this . _ unprocessedNotifications . length ) {
notification = this . _ unprocessedNotifications . shift ( ) ;
} else {
} else {
let t = this . task_ ;
notification = await new Promise (
while ( t . next_ )
( resolve ) => this . _ notificationCallback = resolve ) ;
t = t . next_ ;
}
t . next_ = task ;
} while ( ! predicate ( notification ) ) ;
return notification ;
}
}
return this ;
} ;
TestSession . prototype . disconnect = function ( childDone ) {
return this . enqueue ( ( callback ) => {
this . expectClose_ = true ;
this . socket_ . destroy ( ) ;
console . log ( '[test]' , 'Connection terminated' ) ;
callback ( ) ;
} , childDone ) ;
} ;
TestSession . prototype . expectClose = function ( ) {
return this . enqueue ( ( callback ) => {
this . expectClose_ = true ;
callback ( ) ;
} ) ;
} ;
TestSession . prototype . assertClosed = function ( ) {
return this . enqueue ( ( callback ) => {
assert . strictEqual ( this . closed_ , true ) ;
callback ( ) ;
} ) ;
} ;
TestSession . prototype . testHttpResponse = function ( path , check ) {
return this . enqueue ( ( callback ) =>
checkHttpResponse ( null , this . harness_ . port , path , ( err , response ) => {
check . call ( this , err , response ) ;
callback ( ) ;
} ) ) ;
} ;
_ isBreakOnLineNotification ( message , line , url ) {
if ( 'Debugger.paused' === message [ 'method' ] ) {
const callFrame = message [ 'params' ] [ 'callFrames' ] [ 0 ] ;
const location = callFrame [ 'location' ] ;
assert . strictEqual ( url , this . _ scriptsIdsByUrl . get ( location [ 'scriptId' ] ) ) ;
assert . strictEqual ( line , location [ 'lineNumber' ] ) ;
return true ;
}
}
function Harness ( port , childProcess ) {
waitForBreakOnLine ( line , url ) {
this . port = port ;
return this
this . mainScriptPath = mainScript ;
. waitForNotification (
this . stderrFilters_ = [ ] ;
( notification ) =>
this . process_ = childProcess ;
this . _ isBreakOnLineNotification ( notification , line , url ) ,
this . result_ = { } ;
` break on ${ url } : ${ line } ` )
this . running_ = true ;
. then ( ( notification ) =>
notification . params . callFrames [ 0 ] . scopeChain [ 0 ] . object . objectId ) ;
}
childProcess . stdout . on ( 'data' , makeBufferingDataCallback (
_ matchesConsoleOutputNotification ( notification , type , values ) {
( line ) => console . log ( '[out]' , line ) ) ) ;
if ( ! Array . isArray ( values ) )
values = [ values ] ;
if ( 'Runtime.consoleAPICalled' === notification [ 'method' ] ) {
const params = notification [ 'params' ] ;
if ( params [ 'type' ] === type ) {
let i = 0 ;
for ( const value of params [ 'args' ] ) {
if ( value [ 'value' ] !== values [ i ++ ] )
return false ;
}
return i === values . length ;
}
}
}
waitForConsoleOutput ( type , values ) {
const desc = ` Console output matching ${ JSON . stringify ( values ) } ` ;
return this . waitForNotification (
( notification ) => this . _ matchesConsoleOutputNotification ( notification ,
type , values ) ,
desc ) ;
}
childProcess . stderr . on ( 'data' , makeBufferingDataCallback ( ( message ) => {
async runToCompletion ( ) {
const pending = [ ] ;
console . log ( '[test]' , 'Verify node waits for the frontend to disconnect' ) ;
console . log ( '[err]' , message ) ;
await this . send ( { 'method' : 'Debugger.resume' } ) ;
for ( const filter of this . stderrFilters_ )
await this . waitForNotification ( ( notification ) => {
if ( ! filter ( message ) ) pending . push ( filter ) ;
return notification . method === 'Runtime.executionContextDestroyed' &&
this . stderrFilters_ = pending ;
notification . params . executionContextId === 1 ;
} ) ) ;
childProcess . on ( 'exit' , ( code , signal ) => {
this . result_ = { code , signal } ;
this . running_ = false ;
} ) ;
} ) ;
while ( ( await this . _ instance . nextStderrString ( ) ) !==
'Waiting for the debugger to disconnect...' ) ;
await this . disconnect ( ) ;
}
}
Harness . prototype . addStderrFilter = function ( regexp , callback ) {
this . stderrFilters_ . push ( ( message ) => {
if ( message . match ( regexp ) ) {
callback ( ) ;
return true ;
}
}
} ) ;
} ;
Harness . prototype . assertStillAlive = function ( ) {
class NodeInstance {
assert . strictEqual ( this . running_ , true ,
constructor ( inspectorFlags = [ '--inspect-brk=0' ] ,
` Child died: ${ JSON . stringify ( this . result_ ) } ` ) ;
scriptContents = '' ,
} ;
scriptFile = _ MAINSCRIPT ) {
this . _ portCallback = null ;
this . portPromise = new Promise ( ( resolve ) => this . _ portCallback = resolve ) ;
this . _ process = spawnChildProcess ( inspectorFlags , scriptContents ,
scriptFile ) ;
this . _ running = true ;
this . _ stderrLineCallback = null ;
this . _ unprocessedStderrLines = [ ] ;
this . _ process . stdout . on ( 'data' , makeBufferingDataCallback (
( line ) => console . log ( '[out]' , line ) ) ) ;
Harness . prototype . run_ = function ( ) {
this . _ process . stderr . on ( 'data' , makeBufferingDataCallback (
setImmediate ( ( ) => {
( message ) => this . onStderrLine ( message ) ) ) ;
if ( ! this . task_ [ EXPECT_ALIVE_SYMBOL ] )
this . assertStillAlive ( ) ;
this . _ shutdownPromise = new Promise ( ( resolve ) => {
this . task_ ( ( ) => {
this . _ process . once ( 'exit' , ( exitCode , signal ) => {
this . task_ = this . task_ . next_ ;
resolve ( { exitCode , signal } ) ;
if ( this . task_ )
this . _ running = false ;
this . run_ ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ;
}
Harness . prototype . enqueue_ = function ( task , expectAlive ) {
onStderrLine ( line ) {
task [ EXPECT_ALIVE_SYMBOL ] = ! ! expectAlive ;
console . log ( '[err]' , line ) ;
if ( ! this . task_ ) {
if ( this . _ portCallback ) {
this . task_ = task ;
const matches = line . match ( /Debugger listening on ws:\/\/.+:(\d+)\/.+/ ) ;
this . run_ ( ) ;
if ( matches )
this . _ portCallback ( matches [ 1 ] ) ;
this . _ portCallback = null ;
}
if ( this . _ stderrLineCallback ) {
this . _ stderrLineCallback ( line ) ;
this . _ stderrLineCallback = null ;
} else {
} else {
let chain = this . task_ ;
this . _ unprocessedStderrLines . push ( line ) ;
while ( chain . next_ )
chain = chain . next_ ;
chain . next_ = task ;
}
}
return this ;
} ;
Harness . prototype . testHttpResponse = function ( host , path , check , errorcb ) {
return this . enqueue_ ( ( doneCallback ) => {
function wrap ( callback ) {
if ( callback ) {
return function ( ) {
callback ( ... arguments ) ;
doneCallback ( ) ;
} ;
}
}
httpGet ( host , path ) {
console . log ( '[test]' , ` Testing ${ path } ` ) ;
return this . portPromise . then ( ( port ) => new Promise ( ( resolve , reject ) => {
const req = http . get ( { host , port , path } , ( res ) => {
let response = '' ;
res . setEncoding ( 'utf8' ) ;
res
. on ( 'data' , ( data ) => response += data . toString ( ) )
. on ( 'end' , ( ) => {
resolve ( response ) ;
} ) ;
} ) ;
req . on ( 'error' , reject ) ;
} ) ) . then ( ( response ) => {
try {
return JSON . parse ( response ) ;
} catch ( e ) {
e . body = response ;
throw e ;
}
}
checkHttpResponse ( host , this . port , path , wrap ( check ) , wrap ( errorcb ) ) ;
} ) ;
} ) ;
} ;
}
Harness . prototype . wsHandshake = function ( devtoolsUrl , tests , readyCallback ) {
wsHandshake ( devtoolsUrl ) {
return this . portPromise . then ( ( port ) => new Promise ( ( resolve ) => {
http . get ( {
http . get ( {
port : this . port ,
port ,
path : url . parse ( devtoolsUrl ) . path ,
path : url . parse ( devtoolsUrl ) . path ,
headers : {
headers : {
'Connection' : 'Upgrade' ,
'Connection' : 'Upgrade' ,
@ -456,100 +372,39 @@ Harness.prototype.wsHandshake = function(devtoolsUrl, tests, readyCallback) {
'Sec-WebSocket-Key' : 'key=='
'Sec-WebSocket-Key' : 'key=='
}
}
} ) . on ( 'upgrade' , ( message , socket ) => {
} ) . on ( 'upgrade' , ( message , socket ) => {
const session = new TestSession ( socket , this ) ;
resolve ( new InspectorSession ( socket , this ) ) ;
if ( ! ( tests instanceof Array ) )
tests = [ tests ] ;
function enqueue ( tests ) {
session . enqueue ( ( sessionCb ) => {
if ( tests . length ) {
tests [ 0 ] ( session ) ;
session . enqueue ( ( cb2 ) => {
enqueue ( tests . slice ( 1 ) ) ;
cb2 ( ) ;
} ) ;
} else {
readyCallback ( ) ;
}
sessionCb ( ) ;
} ) ;
}
enqueue ( tests ) ;
} ) . on ( 'response' , common . mustNotCall ( 'Upgrade was not received' ) ) ;
} ) . on ( 'response' , common . mustNotCall ( 'Upgrade was not received' ) ) ;
} ;
} ) ) ;
Harness . prototype . runFrontendSession = function ( tests ) {
return this . enqueue_ ( ( callback ) => {
checkHttpResponse ( null , this . port , '/json/list' , ( err , response ) => {
assert . ifError ( err ) ;
this . wsHandshake ( response [ 0 ] [ 'webSocketDebuggerUrl' ] , tests , callback ) ;
} ) ;
} ) ;
} ;
Harness . prototype . expectShutDown = function ( errorCode ) {
this . enqueue_ ( ( callback ) => {
if ( this . running_ ) {
const timeoutId = timeout ( 'Have not terminated' ) ;
this . process_ . on ( 'exit' , ( code , signal ) => {
clearTimeout ( timeoutId ) ;
assert . strictEqual ( errorCode , code , JSON . stringify ( { code , signal } ) ) ;
callback ( ) ;
} ) ;
} else {
assert . strictEqual ( errorCode , this . result_ . code ) ;
callback ( ) ;
}
}
} , true ) ;
} ;
Harness . prototype . kill = function ( ) {
return this . enqueue_ ( ( callback ) => {
this . process_ . kill ( ) ;
callback ( ) ;
} ) ;
} ;
exports . startNodeForInspectorTest = function ( callback ,
async connectInspectorSession ( ) {
inspectorFlags = [ '--inspect-brk' ] ,
console . log ( '[test]' , 'Connecting to a child Node process' ) ;
scriptContents = '' ,
const response = await this . httpGet ( null , '/json/list' ) ;
scriptFile = mainScript ) {
const url = response [ 0 ] [ 'webSocketDebuggerUrl' ] ;
const args = [ ] . concat ( inspectorFlags ) ;
return await this . wsHandshake ( url ) ;
if ( scriptContents ) {
args . push ( '-e' , scriptContents ) ;
} else {
args . push ( scriptFile ) ;
}
}
const child = spawn ( process . execPath , args ) ;
expectShutdown ( ) {
return this . _ shutdownPromise ;
const timeoutId = timeout ( 'Child process did not start properly' , 4 ) ;
}
let found = false ;
const dataCallback = makeBufferingDataCallback ( ( text ) => {
clearTimeout ( timeoutId ) ;
console . log ( '[err]' , text ) ;
if ( found ) return ;
const match = text . match ( /Debugger listening on ws:\/\/.+:(\d+)\/.+/ ) ;
found = true ;
child . stderr . removeListener ( 'data' , dataCallback ) ;
assert . ok ( match , text ) ;
callback ( new Harness ( match [ 1 ] , child ) ) ;
} ) ;
child . stderr . on ( 'data' , dataCallback ) ;
const handler = tearDown . bind ( null , child ) ;
nextStderrString ( ) {
if ( this . _ unprocessedStderrLines . length )
return Promise . resolve ( this . _ unprocessedStderrLines . shift ( ) ) ;
return new Promise ( ( resolve ) => this . _ stderrLineCallback = resolve ) ;
}
process . on ( 'exit' , handler ) ;
kill ( ) {
process . on ( 'uncaughtException' , handler ) ;
this . _ process . kill ( ) ;
process . on ( 'SIGINT' , handler ) ;
}
} ;
}
exports . mainScriptSource = function ( ) {
function readMainScriptSource ( ) {
return fs . readFileSync ( mainScript , 'utf8' ) ;
return fs . readFileSync ( _ MAINSCRIPT , 'utf8' ) ;
} ;
}
exports . markMessageNoResponse = function ( message ) {
module . exports = {
message [ DONT_EXPECT_RESPONSE_SYMBOL ] = true ;
mainScriptPath : _ MAINSCRIPT ,
readMainScriptSource ,
NodeInstance
} ;
} ;