Files
scriptc/tests/corpus/5818-constructor-function-emitter-defaults.cjs
T
Chris Tate 447f56e5c3 Share defaultMaxListeners with the typed EventEmitter, link instanceof alone, and clean up a pipe once
- EventEmitter.defaultMaxListeners on the untyped constructor is Node's
  accessor over the default the typed EventEmitter reads: assignments
  validate as validateNumber(arg, 'defaultMaxListeners', 0) does, and
  getMaxListeners() of emitters without their own limit answers the new
  value.
- dyn.builtinInstanceOf and dyn.functionInstanceOf link the
  constructor-function unit and the units it calls (dyn-invoke, inspect,
  symbol, emitter), so a program whose only use is `this instanceof F`
  links.
- The legacy pipe's cleanup runs once: when the destination emits
  'close' from end(), the cleanup still queued on the source's 'end'
  finds the listeners removed and emit('end') answers true.
2026-10-20 11:28:28 -05:00

54 lines
1.5 KiB
JavaScript

// EventEmitter.defaultMaxListeners read and written through the untyped
// EventEmitter constructor (Node's accessor and its validation), and a
// legacy pipe whose destination emits 'close' synchronously from end().
"use strict";
const util = require("util");
const EventEmitter = require("events");
const Stream = require("stream");
function Bus() {
EventEmitter.call(this);
}
util.inherits(Bus, EventEmitter);
console.log(EventEmitter.defaultMaxListeners);
console.log(new Bus().getMaxListeners());
EventEmitter.defaultMaxListeners = 3;
console.log(EventEmitter.defaultMaxListeners, new Bus().getMaxListeners());
for (const value of [-1, "4", Number.NaN]) {
try {
EventEmitter.defaultMaxListeners = value;
} catch (error) {
console.log(error.name, error.code, error.message);
}
}
console.log(EventEmitter.defaultMaxListeners);
EventEmitter.defaultMaxListeners = 10;
function Source() {
Stream.call(this);
this.readable = true;
}
util.inherits(Source, Stream);
function Sink() {
Stream.call(this);
this.writable = true;
}
util.inherits(Sink, Stream);
Sink.prototype.write = function (chunk) {
console.log("write", chunk);
return true;
};
Sink.prototype.end = function () {
console.log("sink end");
this.emit("close");
};
const source = new Source();
const sink = new Sink();
source.pipe(sink);
source.emit("data", "a");
console.log("emit end", source.emit("end"));
console.log("listeners", source.listenerCount("end"), source.listenerCount("data"), sink.listenerCount("close"));