2016-12-13 02:08:31 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
|
|
|
ERR_INSPECTOR_ALREADY_CONNECTED,
|
|
|
|
ERR_INSPECTOR_CLOSED,
|
|
|
|
ERR_INSPECTOR_NOT_AVAILABLE,
|
|
|
|
ERR_INSPECTOR_NOT_CONNECTED,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_CALLBACK
|
|
|
|
} = require('internal/errors').codes;
|
2018-12-26 12:53:36 +01:00
|
|
|
|
|
|
|
const { hasInspector } = internalBinding('config');
|
|
|
|
if (!hasInspector)
|
|
|
|
throw new ERR_INSPECTOR_NOT_AVAILABLE();
|
|
|
|
|
|
|
|
const EventEmitter = require('events');
|
2018-08-03 00:51:02 +02:00
|
|
|
const { validateString } = require('internal/validators');
|
2016-12-13 02:08:31 +01:00
|
|
|
const util = require('util');
|
2018-12-10 04:12:39 +01:00
|
|
|
const { Connection, open, url } = internalBinding('inspector');
|
2016-12-13 02:08:31 +01:00
|
|
|
|
|
|
|
const connectionSymbol = Symbol('connectionProperty');
|
|
|
|
const messageCallbacksSymbol = Symbol('messageCallbacks');
|
|
|
|
const nextIdSymbol = Symbol('nextId');
|
|
|
|
const onMessageSymbol = Symbol('onMessage');
|
|
|
|
|
|
|
|
class Session extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this[connectionSymbol] = null;
|
|
|
|
this[nextIdSymbol] = 1;
|
|
|
|
this[messageCallbacksSymbol] = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
if (this[connectionSymbol])
|
2018-03-16 09:17:58 +01:00
|
|
|
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
|
|
|
|
const connection =
|
|
|
|
new Connection((message) => this[onMessageSymbol](message));
|
|
|
|
if (connection.sessionAttached) {
|
|
|
|
throw new ERR_INSPECTOR_ALREADY_CONNECTED('Another inspector session');
|
|
|
|
}
|
|
|
|
this[connectionSymbol] = connection;
|
2016-12-13 02:08:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[onMessageSymbol](message) {
|
|
|
|
const parsed = JSON.parse(message);
|
2017-08-22 18:46:33 +02:00
|
|
|
try {
|
|
|
|
if (parsed.id) {
|
|
|
|
const callback = this[messageCallbacksSymbol].get(parsed.id);
|
|
|
|
this[messageCallbacksSymbol].delete(parsed.id);
|
|
|
|
if (callback)
|
|
|
|
callback(parsed.error || null, parsed.result || null);
|
|
|
|
} else {
|
|
|
|
this.emit(parsed.method, parsed);
|
|
|
|
this.emit('inspectorNotification', parsed);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
process.emitWarning(error);
|
2016-12-13 02:08:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
post(method, params, callback) {
|
2018-08-03 00:51:02 +02:00
|
|
|
validateString(method, 'method');
|
2016-12-13 02:08:31 +01:00
|
|
|
if (!callback && util.isFunction(params)) {
|
|
|
|
callback = params;
|
|
|
|
params = null;
|
|
|
|
}
|
2017-06-20 23:37:00 +02:00
|
|
|
if (params && typeof params !== 'object') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function') {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_CALLBACK();
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2016-12-13 02:08:31 +01:00
|
|
|
|
2017-06-20 23:37:00 +02:00
|
|
|
if (!this[connectionSymbol]) {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INSPECTOR_NOT_CONNECTED();
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2016-12-13 02:08:31 +01:00
|
|
|
const id = this[nextIdSymbol]++;
|
2017-07-11 02:55:21 +02:00
|
|
|
const message = { id, method };
|
2016-12-13 02:08:31 +01:00
|
|
|
if (params) {
|
2018-01-05 17:42:39 +01:00
|
|
|
message.params = params;
|
2016-12-13 02:08:31 +01:00
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
this[messageCallbacksSymbol].set(id, callback);
|
|
|
|
}
|
|
|
|
this[connectionSymbol].dispatch(JSON.stringify(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
if (!this[connectionSymbol])
|
|
|
|
return;
|
|
|
|
this[connectionSymbol].disconnect();
|
|
|
|
this[connectionSymbol] = null;
|
|
|
|
const remainingCallbacks = this[messageCallbacksSymbol].values();
|
|
|
|
for (const callback of remainingCallbacks) {
|
2018-02-27 14:55:32 +01:00
|
|
|
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
|
2016-12-13 02:08:31 +01:00
|
|
|
}
|
|
|
|
this[messageCallbacksSymbol].clear();
|
|
|
|
this[nextIdSymbol] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-05-26 04:00:24 +02:00
|
|
|
open: (port, host, wait) => open(port, host, !!wait),
|
|
|
|
close: process._debugEnd,
|
|
|
|
url: url,
|
2018-11-28 23:24:14 +01:00
|
|
|
// This is dynamically added during bootstrap,
|
|
|
|
// where the console from the VM is still available
|
|
|
|
console: require('internal/console/inspector').consoleFromVM,
|
2016-12-13 02:08:31 +01:00
|
|
|
Session
|
|
|
|
};
|