Skip to content

Commit

Permalink
inspector: use private fields instead of symbols
Browse files Browse the repository at this point in the history
PR-URL: #50776
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
  • Loading branch information
anonrig authored and RafaelGSS committed Nov 29, 2023
1 parent eacf4ba commit c3eee59
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
JSONParse,
JSONStringify,
SafeMap,
Symbol,
SymbolDispose,
} = primordials;

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

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 SafeMap();
}
#connection = null;
#nextId = 1;
#messageCallbacks = new SafeMap();

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

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

[onMessageSymbol](message) {
#onMessage(message) {
const parsed = JSONParse(message);
try {
if (parsed.id) {
const callback = this[messageCallbacksSymbol].get(parsed.id);
this[messageCallbacksSymbol].delete(parsed.id);
const callback = this.#messageCallbacks.get(parsed.id);
this.#messageCallbacks.delete(parsed.id);
if (callback) {
if (parsed.error) {
return callback(new ERR_INSPECTOR_COMMAND(parsed.error.code,
parsed.error.message));
return callback(
new ERR_INSPECTOR_COMMAND(parsed.error.code, parsed.error.message),
);
}

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

if (!this[connectionSymbol]) {
if (!this.#connection) {
throw new ERR_INSPECTOR_NOT_CONNECTED();
}
const id = this[nextIdSymbol]++;
const id = this.#nextId++;
const message = { 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;
const remainingCallbacks = this[messageCallbacksSymbol].values();
this.#connection.disconnect();
this.#connection = null;
const remainingCallbacks = this.#messageCallbacks.values();
for (const callback of remainingCallbacks) {
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
}
this[messageCallbacksSymbol].clear();
this[nextIdSymbol] = 1;
this.#messageCallbacks.clear();
this.#nextId = 1;
}
}

Expand Down

0 comments on commit c3eee59

Please sign in to comment.