inspector: use private fields instead of symbols

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Yagiz Nizipli 2023-11-19 17:35:19 -05:00 committed by GitHub
parent 4db94c5023
commit 4cfc188f87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,6 @@ const {
JSONParse, JSONParse,
JSONStringify, JSONStringify,
SafeMap, SafeMap,
Symbol,
SymbolDispose, SymbolDispose,
} = primordials; } = primordials;
@ -45,28 +44,19 @@ const {
console, console,
} = internalBinding('inspector'); } = internalBinding('inspector');
const connectionSymbol = Symbol('connectionProperty');
const messageCallbacksSymbol = Symbol('messageCallbacks');
const nextIdSymbol = Symbol('nextId');
const onMessageSymbol = Symbol('onMessage');
class Session extends EventEmitter { class Session extends EventEmitter {
constructor() { #connection = null;
super(); #nextId = 1;
this[connectionSymbol] = null; #messageCallbacks = new SafeMap();
this[nextIdSymbol] = 1;
this[messageCallbacksSymbol] = new SafeMap();
}
/** /**
* Connects the session to the inspector back-end. * Connects the session to the inspector back-end.
* @returns {void} * @returns {void}
*/ */
connect() { connect() {
if (this[connectionSymbol]) if (this.#connection)
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session'); throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
this[connectionSymbol] = this.#connection = new Connection((message) => this.#onMessage(message));
new Connection((message) => this[onMessageSymbol](message));
} }
/** /**
@ -77,23 +67,24 @@ class Session extends EventEmitter {
connectToMainThread() { connectToMainThread() {
if (isMainThread) if (isMainThread)
throw new ERR_INSPECTOR_NOT_WORKER(); throw new ERR_INSPECTOR_NOT_WORKER();
if (this[connectionSymbol]) if (this.#connection)
throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session'); throw new ERR_INSPECTOR_ALREADY_CONNECTED('The inspector session');
this[connectionSymbol] = this.#connection =
new MainThreadConnection( new MainThreadConnection(
(message) => queueMicrotask(() => this[onMessageSymbol](message))); (message) => queueMicrotask(() => this.#onMessage(message)));
} }
[onMessageSymbol](message) { #onMessage(message) {
const parsed = JSONParse(message); const parsed = JSONParse(message);
try { try {
if (parsed.id) { if (parsed.id) {
const callback = this[messageCallbacksSymbol].get(parsed.id); const callback = this.#messageCallbacks.get(parsed.id);
this[messageCallbacksSymbol].delete(parsed.id); this.#messageCallbacks.delete(parsed.id);
if (callback) { if (callback) {
if (parsed.error) { if (parsed.error) {
return callback(new ERR_INSPECTOR_COMMAND(parsed.error.code, return callback(
parsed.error.message)); new ERR_INSPECTOR_COMMAND(parsed.error.code, parsed.error.message),
);
} }
callback(null, parsed.result); callback(null, parsed.result);
@ -127,18 +118,18 @@ class Session extends EventEmitter {
validateFunction(callback, 'callback'); validateFunction(callback, 'callback');
} }
if (!this[connectionSymbol]) { if (!this.#connection) {
throw new ERR_INSPECTOR_NOT_CONNECTED(); throw new ERR_INSPECTOR_NOT_CONNECTED();
} }
const id = this[nextIdSymbol]++; const id = this.#nextId++;
const message = { id, method }; const message = { id, method };
if (params) { if (params) {
message.params = params; message.params = params;
} }
if (callback) { if (callback) {
this[messageCallbacksSymbol].set(id, callback); this.#messageCallbacks.set(id, callback);
} }
this[connectionSymbol].dispatch(JSONStringify(message)); this.#connection.dispatch(JSONStringify(message));
} }
/** /**
@ -148,16 +139,16 @@ class Session extends EventEmitter {
* @returns {void} * @returns {void}
*/ */
disconnect() { disconnect() {
if (!this[connectionSymbol]) if (!this.#connection)
return; return;
this[connectionSymbol].disconnect(); this.#connection.disconnect();
this[connectionSymbol] = null; this.#connection = null;
const remainingCallbacks = this[messageCallbacksSymbol].values(); const remainingCallbacks = this.#messageCallbacks.values();
for (const callback of remainingCallbacks) { for (const callback of remainingCallbacks) {
process.nextTick(callback, new ERR_INSPECTOR_CLOSED()); process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
} }
this[messageCallbacksSymbol].clear(); this.#messageCallbacks.clear();
this[nextIdSymbol] = 1; this.#nextId = 1;
} }
} }