From caf8e7a49d60ca4230e5ec4417c2ef2060c94ee3 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 2 Sep 2018 17:30:20 +0200 Subject: [PATCH] worker: display MessagePort status in util.inspect() PR-URL: https://github.com/nodejs/node/pull/22658 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Denys Otrishko Reviewed-By: Luigi Pinca Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/worker.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/internal/worker.js b/lib/internal/worker.js index a1abcff567d424..402fc30b591d2d 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -125,6 +125,27 @@ MessagePort.prototype.close = function(cb) { const drainMessagePort = MessagePort.prototype.drain; delete MessagePort.prototype.drain; +Object.defineProperty(MessagePort.prototype, util.inspect.custom, { + enumerable: false, + writable: false, + value: function inspect() { // eslint-disable-line func-name-matching + let ref; + try { + // This may throw when `this` does not refer to a native object, + // e.g. when accessing the prototype directly. + ref = this.hasRef(); + } catch { return this; } + return Object.assign(Object.create(MessagePort.prototype), + ref === undefined ? { + active: false, + } : { + active: true, + refed: ref + }, + this); + } +}); + function setupPortReferencing(port, eventEmitter, eventName) { // Keep track of whether there are any workerMessage listeners: // If there are some, ref() the channel so it keeps the event loop alive.