Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions lib/inspector.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,6 @@ const {
JSONParse,
JSONStringify,
SafeMap,
Symbol,
SymbolDispose,
}=primordials;

Expand DownExpand Up@@ -45,28 +44,19 @@ const {
console,
}=internalBinding('inspector');

constconnectionSymbol=Symbol('connectionProperty');
constmessageCallbacksSymbol=Symbol('messageCallbacks');
constnextIdSymbol=Symbol('nextId');
constonMessageSymbol=Symbol('onMessage');

classSessionextendsEventEmitter{
constructor(){
super();
this[connectionSymbol]=null;
this[nextIdSymbol]=1;
this[messageCallbacksSymbol]=newSafeMap();
}
#connection =null;
#nextId =1;
#messageCallbacks =newSafeMap();

/**
* Connects the session to the inspector back-end.
* @returns {void}
*/
connect(){
if(this[connectionSymbol])
if(this.#connection)
thrownewERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
this[connectionSymbol]=
newConnection((message)=>this[onMessageSymbol](message));
this.#connection =newConnection((message)=>this.#onMessage(message));
}

/**
Expand All@@ -77,23 +67,24 @@ class Session extends EventEmitter {
connectToMainThread(){
if(isMainThread)
thrownewERR_INSPECTOR_NOT_WORKER();
if(this[connectionSymbol])
if(this.#connection)
thrownewERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
this[connectionSymbol]=
this.#connection=
newMainThreadConnection(
(message)=>queueMicrotask(()=>this[onMessageSymbol](message)));
(message)=>queueMicrotask(()=>this.#onMessage(message)));
}

[onMessageSymbol](message){
#onMessage(message){
constparsed=JSONParse(message);
try{
if(parsed.id){
constcallback=this[messageCallbacksSymbol].get(parsed.id);
this[messageCallbacksSymbol].delete(parsed.id);
constcallback=this.#messageCallbacks.get(parsed.id);
this.#messageCallbacks.delete(parsed.id);
if(callback){
if(parsed.error){
returncallback(newERR_INSPECTOR_COMMAND(parsed.error.code,
parsed.error.message));
returncallback(
newERR_INSPECTOR_COMMAND(parsed.error.code,parsed.error.message),
);
}

callback(null,parsed.result);
Expand DownExpand Up@@ -127,18 +118,18 @@ class Session extends EventEmitter {
validateFunction(callback,'callback');
}

if(!this[connectionSymbol]){
if(!this.#connection){
thrownewERR_INSPECTOR_NOT_CONNECTED();
}
constid=this[nextIdSymbol]++;
constid=this.#nextId++;
constmessage={ id, method };
if(params){
message.params=params;
}
if(callback){
this[messageCallbacksSymbol].set(id,callback);
this.#messageCallbacks.set(id,callback);
}
this[connectionSymbol].dispatch(JSONStringify(message));
this.#connection.dispatch(JSONStringify(message));
}

/**
Expand All@@ -148,16 +139,16 @@ class Session extends EventEmitter {
* @returns {void}
*/
disconnect(){
if(!this[connectionSymbol])
if(!this.#connection)
return;
this[connectionSymbol].disconnect();
this[connectionSymbol]=null;
constremainingCallbacks=this[messageCallbacksSymbol].values();
this.#connection.disconnect();
this.#connection=null;
constremainingCallbacks=this.#messageCallbacks.values();
for(constcallbackofremainingCallbacks){
process.nextTick(callback,newERR_INSPECTOR_CLOSED());
}
this[messageCallbacksSymbol].clear();
this[nextIdSymbol]=1;
this.#messageCallbacks.clear();
this.#nextId=1;
}
}

Expand Down