diff options
author | Eugene Ostroukhov <eostroukhov@chromium.org> | 2016-12-12 17:08:31 -0800 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-05-23 19:45:48 +0200 |
commit | 60390cd7fb674865d9c8af89d5a2fecdae1c7ec0 (patch) | |
tree | 909dbc1791f997bb0bbd4ecb42e11233cb54fdcb /lib | |
parent | c0d858f8bb8ba5212548da2fba6a7bc02db0462b (diff) | |
download | android-node-v8-60390cd7fb674865d9c8af89d5a2fecdae1c7ec0.tar.gz android-node-v8-60390cd7fb674865d9c8af89d5a2fecdae1c7ec0.tar.bz2 android-node-v8-60390cd7fb674865d9c8af89d5a2fecdae1c7ec0.zip |
inspector: JavaScript bindings for the inspector
PR-URL: https://github.com/nodejs/node/pull/12263
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Josh Gavant <josh.gavant@outlook.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/inspector.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/inspector.js b/lib/inspector.js new file mode 100644 index 0000000000..1edc9fc3be --- /dev/null +++ b/lib/inspector.js @@ -0,0 +1,87 @@ +'use strict'; + +const connect = process.binding('inspector').connect; +const EventEmitter = require('events'); +const util = require('util'); + +if (!connect) + throw new Error('Inspector is not available'); + +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]) + throw new Error('Already connected'); + this[connectionSymbol] = + connect((message) => this[onMessageSymbol](message)); + } + + [onMessageSymbol](message) { + const parsed = JSON.parse(message); + 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); + } + } + + post(method, params, callback) { + if (typeof method !== 'string') + throw new TypeError( + `"method" must be a string, got ${typeof method} instead`); + if (!callback && util.isFunction(params)) { + callback = params; + params = null; + } + if (params && typeof params !== 'object') + throw new TypeError( + `"params" must be an object, got ${typeof params} instead`); + if (callback && typeof callback !== 'function') + throw new TypeError( + `"callback" must be a function, got ${typeof callback} instead`); + + if (!this[connectionSymbol]) + throw new Error('Session is not connected'); + const id = this[nextIdSymbol]++; + const message = {id, method}; + if (params) { + message['params'] = params; + } + 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) { + process.nextTick(callback, new Error('Session was closed')); + } + this[messageCallbacksSymbol].clear(); + this[nextIdSymbol] = 1; + } +} + +module.exports = { + Session +}; |