2016-12-13 02:08:31 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const EventEmitter = require('events');
|
2017-09-26 02:11:50 +02:00
|
|
|
const errors = require('internal/errors');
|
2016-12-13 02:08:31 +01:00
|
|
|
const util = require('util');
|
2017-07-13 06:53:01 +02:00
|
|
|
const { Connection, open, url } = process.binding('inspector');
|
2016-12-13 02:08:31 +01:00
|
|
|
|
2017-07-13 06:53:01 +02:00
|
|
|
if (!Connection)
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.Error('ERR_INSPECTOR_NOT_AVAILABLE');
|
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])
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.Error('ERR_INSPECTOR_ALREADY_CONNECTED');
|
2016-12-13 02:08:31 +01:00
|
|
|
this[connectionSymbol] =
|
2017-07-13 06:53:01 +02:00
|
|
|
new Connection((message) => this[onMessageSymbol](message));
|
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) {
|
2017-06-20 23:37:00 +02:00
|
|
|
if (typeof method !== 'string') {
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
|
|
|
'method', 'string', method);
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
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') {
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
|
2017-10-28 11:39:55 +02:00
|
|
|
'params', 'Object', params);
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
|
|
|
if (callback && typeof callback !== 'function') {
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.TypeError('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]) {
|
2017-09-26 02:11:50 +02:00
|
|
|
throw new errors.Error('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) {
|
2017-09-26 02:11:50 +02:00
|
|
|
process.nextTick(callback, new errors.Error('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,
|
2016-12-13 02:08:31 +01:00
|
|
|
Session
|
|
|
|
};
|