summaryrefslogtreecommitdiff
path: root/std/node
diff options
context:
space:
mode:
authorSchwarzkopf Balázs <schwarzkopfb@icloud.com>2020-08-27 11:00:38 +0200
committerGitHub <noreply@github.com>2020-08-27 11:00:38 +0200
commite1564f385c770ac37c550f7d9e164d6a846c191e (patch)
tree1fbf2e00012793b64f07c8085d6e4d006a139a3b /std/node
parent7583fd0979ee35144c7df078d00aa2e78b510be9 (diff)
fix(std/node): "events" and "util" modules (#7170)
Diffstat (limited to 'std/node')
-rw-r--r--std/node/_utils.ts18
-rw-r--r--std/node/events.ts11
-rw-r--r--std/node/events_test.ts25
-rw-r--r--std/node/os.ts2
-rw-r--r--std/node/util.ts17
5 files changed, 53 insertions, 20 deletions
diff --git a/std/node/_utils.ts b/std/node/_utils.ts
index 0e8b26fb5..5bd4d1075 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 bc27731ca..d7c2275a0 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 adeae5b93..a3285f24b 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 c312ffe0c..6714f071c 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 e897931f2..ce1c06b7c 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 */