From 4cd9f390662fad174b7e9411088db45d83ea8e62 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sat, 14 Nov 2020 13:46:29 +0200 Subject: [PATCH] lib: set abort-controller toStringTag PR-URL: https://github.com/nodejs/node/pull/36115 Backport-PR-URL: https://github.com/nodejs/node/pull/38386 Reviewed-By: Rich Trott Reviewed-By: Antoine du Hamel Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- lib/internal/abort_controller.js | 16 ++++++++++++++++ test/parallel/test-abortcontroller.js | 7 +++++++ .../test-eventtarget-memoryleakwarning.js | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index f047a878ffedd0..a834b76b459227 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -6,7 +6,9 @@ const { Object, ObjectSetPrototypeOf, + ObjectDefineProperty, Symbol, + SymbolToStringTag, TypeError, } = primordials; @@ -54,6 +56,13 @@ Object.defineProperties(AbortSignal.prototype, { aborted: { enumerable: true } }); +ObjectDefineProperty(AbortSignal.prototype, SymbolToStringTag, { + writable: false, + enumerable: false, + configurable: true, + value: 'AbortSignal', +}); + defineEventHandler(AbortSignal.prototype, 'abort'); function createAbortSignal() { @@ -97,6 +106,13 @@ Object.defineProperties(AbortController.prototype, { abort: { enumerable: true } }); +ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, { + writable: false, + enumerable: false, + configurable: true, + value: 'AbortController', +}); + module.exports = { AbortController, AbortSignal, diff --git a/test/parallel/test-abortcontroller.js b/test/parallel/test-abortcontroller.js index 3eba5fff051512..051b07ba389c2f 100644 --- a/test/parallel/test-abortcontroller.js +++ b/test/parallel/test-abortcontroller.js @@ -61,3 +61,10 @@ const { Event } = require('internal/event_target'); /^TypeError: Illegal constructor$/ ); } +{ + // Symbol.toStringTag + const toString = (o) => Object.prototype.toString.call(o); + const ac = new AbortController(); + strictEqual(toString(ac), '[object AbortController]'); + strictEqual(toString(ac.signal), '[object AbortSignal]'); +} diff --git a/test/parallel/test-eventtarget-memoryleakwarning.js b/test/parallel/test-eventtarget-memoryleakwarning.js index e6932320cf4b14..46c68b9979cfcb 100644 --- a/test/parallel/test-eventtarget-memoryleakwarning.js +++ b/test/parallel/test-eventtarget-memoryleakwarning.js @@ -23,7 +23,7 @@ common.expectWarning({ 'Use events.setMaxListeners() to increase ' + 'limit'], ['Possible EventTarget memory leak detected. 3 foo listeners added to ' + - '[AbortSignal [EventTarget]]. ' + + '[AbortSignal]. ' + 'Use events.setMaxListeners() to increase ' + 'limit'], ],