From e1564f385c770ac37c550f7d9e164d6a846c191e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Schwarzkopf=20Bal=C3=A1zs?= Date: Thu, 27 Aug 2020 11:00:38 +0200 Subject: [PATCH] fix(std/node): "events" and "util" modules (#7170) --- std/node/_utils.ts | 18 ++++++++++++++++++ std/node/events.ts | 11 +++++++++-- std/node/events_test.ts | 25 +++++++++++++++++++++++++ std/node/os.ts | 2 +- std/node/util.ts | 17 ----------------- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/std/node/_utils.ts b/std/node/_utils.ts index 0e8b26fb54..5bd4d10750 100644 --- a/std/node/_utils.ts +++ b/std/node/_utils.ts @@ -111,3 +111,21 @@ function slowCases(enc: string): string | undefined { if (enc === "") return "utf8"; } } + +export function validateIntegerRange( + value: number, + name: string, + min = -2147483648, + max = 2147483647, +): void { + // The defaults for min and max correspond to the limits of 32-bit integers. + if (!Number.isInteger(value)) { + throw new Error(`${name} must be 'an integer' but was ${value}`); + } + + if (value < min || value > max) { + throw new Error( + `${name} must be >= ${min} && <= ${max}. Value was ${value}`, + ); + } +} diff --git a/std/node/events.ts b/std/node/events.ts index bc27731ca2..d7c2275a0a 100644 --- a/std/node/events.ts +++ b/std/node/events.ts @@ -21,7 +21,7 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -import { validateIntegerRange } from "./util.ts"; +import { validateIntegerRange } from "./_utils.ts"; import { assert } from "../_util/assert.ts"; // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -356,7 +356,14 @@ export default class EventEmitter { * Infinity (or 0) to indicate an unlimited number of listeners. */ public setMaxListeners(n: number): this { - validateIntegerRange(n, "maxListeners", 0); + if (n !== Infinity) { + if (n === 0) { + n = Infinity; + } else { + validateIntegerRange(n, "maxListeners", 0); + } + } + this.maxListeners = n; return this; } diff --git a/std/node/events_test.ts b/std/node/events_test.ts index adeae5b93f..a3285f24bc 100644 --- a/std/node/events_test.ts +++ b/std/node/events_test.ts @@ -61,6 +61,31 @@ Deno.test({ EventEmitter.defaultMaxListeners = 20; assertEquals(EventEmitter.defaultMaxListeners, 20); EventEmitter.defaultMaxListeners = 10; //reset back to original value + + assertThrows(() => { + new EventEmitter().setMaxListeners(-1); + }); + + const ee = new EventEmitter(); + const noop = (): void => {}; + const origWarn = console.warn; + + for (let i = 10; i--;) { + ee.on("test", noop); + } + + // there are only sync actions until it gets restored, + // so it's safe to overwrite this + console.warn = (): void => fail("Infinity listeners should be allowed"); + + ee.setMaxListeners(Infinity); + ee.on("test", noop); + + // 0 means that unlimited listeners are allowed + ee.setMaxListeners(0); + ee.on("test", noop); + + console.warn = origWarn; }, }); diff --git a/std/node/os.ts b/std/node/os.ts index c312ffe0c0..6714f071c4 100644 --- a/std/node/os.ts +++ b/std/node/os.ts @@ -19,7 +19,7 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. import { notImplemented } from "./_utils.ts"; -import { validateIntegerRange } from "./util.ts"; +import { validateIntegerRange } from "./_utils.ts"; import { EOL as fsEOL } from "../fs/eol.ts"; import process from "./process.ts"; diff --git a/std/node/util.ts b/std/node/util.ts index e897931f2e..ce1c06b7c3 100644 --- a/std/node/util.ts +++ b/std/node/util.ts @@ -68,23 +68,6 @@ export function isPrimitive(value: unknown): boolean { ); } -export function validateIntegerRange( - value: number, - name: string, - min = -2147483648, - max = 2147483647, -): void { - // The defaults for min and max correspond to the limits of 32-bit integers. - if (!Number.isInteger(value)) { - throw new Error(`${name} must be 'an integer' but was ${value}`); - } - if (value < min || value > max) { - throw new Error( - `${name} must be >= ${min} && <= ${max}. Value was ${value}`, - ); - } -} - import { _TextDecoder, _TextEncoder } from "./_utils.ts"; /** The global TextDecoder */