Skip to content

Commit 1daa82a

Browse files
addaleaxtargos
authored andcommitted
worker: display MessagePort status in util.inspect()
PR-URL: #22658 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 2c33dc3 commit 1daa82a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/internal/worker.js

+21
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ MessagePort.prototype.close = function(cb) {
125125
const drainMessagePort = MessagePort.prototype.drain;
126126
delete MessagePort.prototype.drain;
127127

128+
Object.defineProperty(MessagePort.prototype, util.inspect.custom, {
129+
enumerable: false,
130+
writable: false,
131+
value: function inspect() { // eslint-disable-line func-name-matching
132+
let ref;
133+
try {
134+
// This may throw when `this` does not refer to a native object,
135+
// e.g. when accessing the prototype directly.
136+
ref = this.hasRef();
137+
} catch { return this; }
138+
return Object.assign(Object.create(MessagePort.prototype),
139+
ref === undefined ? {
140+
active: false,
141+
} : {
142+
active: true,
143+
refed: ref
144+
},
145+
this);
146+
}
147+
});
148+
128149
function setupPortReferencing(port, eventEmitter, eventName) {
129150
// Keep track of whether there are any workerMessage listeners:
130151
// If there are some, ref() the channel so it keeps the event loop alive.

0 commit comments

Comments
 (0)