1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

fix(std/node): "events" and "util" modules (#7170)

This commit is contained in:
Schwarzkopf Balázs 2020-08-27 11:00:38 +02:00 committed by GitHub
parent 7583fd0979
commit e1564f385c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 20 deletions

View file

@ -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}`,
);
}
}

View file

@ -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;
}

View file

@ -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;
},
});

View file

@ -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";

View file

@ -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 */