vercel 31.0.4 → 31.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/events.js DELETED
@@ -1,977 +0,0 @@
1
- // Copyright Joyent, Inc. and other Node contributors.
2
- //
3
- // Permission is hereby granted, free of charge, to any person obtaining a
4
- // copy of this software and associated documentation files (the
5
- // "Software"), to deal in the Software without restriction, including
6
- // without limitation the rights to use, copy, modify, merge, publish,
7
- // distribute, sublicense, and/or sell copies of the Software, and to permit
8
- // persons to whom the Software is furnished to do so, subject to the
9
- // following conditions:
10
- //
11
- // The above copyright notice and this permission notice shall be included
12
- // in all copies or substantial portions of the Software.
13
- //
14
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
- // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
- // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
- // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
- // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
- // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- // Modified by the vm2 team to make this a standalone module to be loaded into the sandbox.
23
-
24
- 'use strict';
25
-
26
- const host = fromhost;
27
-
28
- const {
29
- Boolean,
30
- Error,
31
- String,
32
- Symbol
33
- } = globalThis;
34
-
35
- const ReflectApply = Reflect.apply;
36
- const ReflectOwnKeys = Reflect.ownKeys;
37
-
38
- const ErrorCaptureStackTrace = Error.captureStackTrace;
39
-
40
- const NumberIsNaN = Number.isNaN;
41
-
42
- const ObjectCreate = Object.create;
43
- const ObjectDefineProperty = Object.defineProperty;
44
- const ObjectDefineProperties = Object.defineProperties;
45
- const ObjectGetPrototypeOf = Object.getPrototypeOf;
46
-
47
- const SymbolFor = Symbol.for;
48
-
49
- function uncurryThis(func) {
50
- return (thiz, ...args) => ReflectApply(func, thiz, args);
51
- }
52
-
53
- const ArrayPrototypeIndexOf = uncurryThis(Array.prototype.indexOf);
54
- const ArrayPrototypeJoin = uncurryThis(Array.prototype.join);
55
- const ArrayPrototypeSlice = uncurryThis(Array.prototype.slice);
56
- const ArrayPrototypeSplice = uncurryThis(Array.prototype.splice);
57
- const ArrayPrototypeUnshift = uncurryThis(Array.prototype.unshift);
58
-
59
- const kRejection = SymbolFor('nodejs.rejection');
60
-
61
- function inspect(obj) {
62
- return typeof obj === 'symbol' ? obj.toString() : `${obj}`;
63
- }
64
-
65
- function spliceOne(list, index) {
66
- for (; index + 1 < list.length; index++)
67
- list[index] = list[index + 1];
68
- list.pop();
69
- }
70
-
71
- function assert(what, message) {
72
- if (!what) throw new Error(message);
73
- }
74
-
75
- function E(key, msg, Base) {
76
- return function NodeError(...args) {
77
- const error = new Base();
78
- const message = ReflectApply(msg, error, args);
79
- ObjectDefineProperties(error, {
80
- message: {
81
- value: message,
82
- enumerable: false,
83
- writable: true,
84
- configurable: true,
85
- },
86
- toString: {
87
- value() {
88
- return `${this.name} [${key}]: ${this.message}`;
89
- },
90
- enumerable: false,
91
- writable: true,
92
- configurable: true,
93
- },
94
- });
95
- error.code = key;
96
- return error;
97
- };
98
- }
99
-
100
-
101
- const ERR_INVALID_ARG_TYPE = E('ERR_INVALID_ARG_TYPE',
102
- (name, expected, actual) => {
103
- assert(typeof name === 'string', "'name' must be a string");
104
- if (!ArrayIsArray(expected)) {
105
- expected = [expected];
106
- }
107
-
108
- let msg = 'The ';
109
- if (StringPrototypeEndsWith(name, ' argument')) {
110
- // For cases like 'first argument'
111
- msg += `${name} `;
112
- } else {
113
- const type = StringPrototypeIncludes(name, '.') ? 'property' : 'argument';
114
- msg += `"${name}" ${type} `;
115
- }
116
- msg += 'must be ';
117
-
118
- const types = [];
119
- const instances = [];
120
- const other = [];
121
-
122
- for (const value of expected) {
123
- assert(typeof value === 'string',
124
- 'All expected entries have to be of type string');
125
- if (ArrayPrototypeIncludes(kTypes, value)) {
126
- ArrayPrototypePush(types, StringPrototypeToLowerCase(value));
127
- } else if (RegExpPrototypeTest(classRegExp, value)) {
128
- ArrayPrototypePush(instances, value);
129
- } else {
130
- assert(value !== 'object',
131
- 'The value "object" should be written as "Object"');
132
- ArrayPrototypePush(other, value);
133
- }
134
- }
135
-
136
- // Special handle `object` in case other instances are allowed to outline
137
- // the differences between each other.
138
- if (instances.length > 0) {
139
- const pos = ArrayPrototypeIndexOf(types, 'object');
140
- if (pos !== -1) {
141
- ArrayPrototypeSplice(types, pos, 1);
142
- ArrayPrototypePush(instances, 'Object');
143
- }
144
- }
145
-
146
- if (types.length > 0) {
147
- if (types.length > 2) {
148
- const last = ArrayPrototypePop(types);
149
- msg += `one of type ${ArrayPrototypeJoin(types, ', ')}, or ${last}`;
150
- } else if (types.length === 2) {
151
- msg += `one of type ${types[0]} or ${types[1]}`;
152
- } else {
153
- msg += `of type ${types[0]}`;
154
- }
155
- if (instances.length > 0 || other.length > 0)
156
- msg += ' or ';
157
- }
158
-
159
- if (instances.length > 0) {
160
- if (instances.length > 2) {
161
- const last = ArrayPrototypePop(instances);
162
- msg +=
163
- `an instance of ${ArrayPrototypeJoin(instances, ', ')}, or ${last}`;
164
- } else {
165
- msg += `an instance of ${instances[0]}`;
166
- if (instances.length === 2) {
167
- msg += ` or ${instances[1]}`;
168
- }
169
- }
170
- if (other.length > 0)
171
- msg += ' or ';
172
- }
173
-
174
- if (other.length > 0) {
175
- if (other.length > 2) {
176
- const last = ArrayPrototypePop(other);
177
- msg += `one of ${ArrayPrototypeJoin(other, ', ')}, or ${last}`;
178
- } else if (other.length === 2) {
179
- msg += `one of ${other[0]} or ${other[1]}`;
180
- } else {
181
- if (StringPrototypeToLowerCase(other[0]) !== other[0])
182
- msg += 'an ';
183
- msg += `${other[0]}`;
184
- }
185
- }
186
-
187
- if (actual == null) {
188
- msg += `. Received ${actual}`;
189
- } else if (typeof actual === 'function' && actual.name) {
190
- msg += `. Received function ${actual.name}`;
191
- } else if (typeof actual === 'object') {
192
- if (actual.constructor && actual.constructor.name) {
193
- msg += `. Received an instance of ${actual.constructor.name}`;
194
- } else {
195
- const inspected = inspect(actual, { depth: -1 });
196
- msg += `. Received ${inspected}`;
197
- }
198
- } else {
199
- let inspected = inspect(actual, { colors: false });
200
- if (inspected.length > 25)
201
- inspected = `${StringPrototypeSlice(inspected, 0, 25)}...`;
202
- msg += `. Received type ${typeof actual} (${inspected})`;
203
- }
204
- return msg;
205
- }, TypeError);
206
-
207
- const ERR_INVALID_THIS = E('ERR_INVALID_THIS', s => `Value of "this" must be of type ${s}`, TypeError);
208
-
209
- const ERR_OUT_OF_RANGE = E('ERR_OUT_OF_RANGE',
210
- (str, range, input, replaceDefaultBoolean = false) => {
211
- assert(range, 'Missing "range" argument');
212
- let msg = replaceDefaultBoolean ? str :
213
- `The value of "${str}" is out of range.`;
214
- const received = inspect(input);
215
- msg += ` It must be ${range}. Received ${received}`;
216
- return msg;
217
- }, RangeError);
218
-
219
- const ERR_UNHANDLED_ERROR = E('ERR_UNHANDLED_ERROR',
220
- err => {
221
- const msg = 'Unhandled error.';
222
- if (err === undefined) return msg;
223
- return `${msg} (${err})`;
224
- }, Error);
225
-
226
- function validateBoolean(value, name) {
227
- if (typeof value !== 'boolean')
228
- throw new ERR_INVALID_ARG_TYPE(name, 'boolean', value);
229
- }
230
-
231
- function validateFunction(value, name) {
232
- if (typeof value !== 'function')
233
- throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);
234
- }
235
-
236
- function validateString(value, name) {
237
- if (typeof value !== 'string')
238
- throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
239
- }
240
-
241
- function nc(cond, e) {
242
- return cond === undefined || cond === null ? e : cond;
243
- }
244
-
245
- function oc(base, key) {
246
- return base === undefined || base === null ? undefined : base[key];
247
- }
248
-
249
- const kCapture = Symbol('kCapture');
250
- const kErrorMonitor = host.kErrorMonitor || Symbol('events.errorMonitor');
251
- const kMaxEventTargetListeners = Symbol('events.maxEventTargetListeners');
252
- const kMaxEventTargetListenersWarned =
253
- Symbol('events.maxEventTargetListenersWarned');
254
-
255
- const kIsEventTarget = SymbolFor('nodejs.event_target');
256
-
257
- function isEventTarget(obj) {
258
- return oc(oc(obj, 'constructor'), kIsEventTarget);
259
- }
260
-
261
- /**
262
- * Creates a new `EventEmitter` instance.
263
- * @param {{ captureRejections?: boolean; }} [opts]
264
- * @constructs {EventEmitter}
265
- */
266
- function EventEmitter(opts) {
267
- EventEmitter.init.call(this, opts);
268
- }
269
- module.exports = EventEmitter;
270
- if (host.once) module.exports.once = host.once;
271
- if (host.on) module.exports.on = host.on;
272
- if (host.getEventListeners) module.exports.getEventListeners = host.getEventListeners;
273
- // Backwards-compat with node 0.10.x
274
- EventEmitter.EventEmitter = EventEmitter;
275
-
276
- EventEmitter.usingDomains = false;
277
-
278
- EventEmitter.captureRejectionSymbol = kRejection;
279
- ObjectDefineProperty(EventEmitter, 'captureRejections', {
280
- get() {
281
- return EventEmitter.prototype[kCapture];
282
- },
283
- set(value) {
284
- validateBoolean(value, 'EventEmitter.captureRejections');
285
-
286
- EventEmitter.prototype[kCapture] = value;
287
- },
288
- enumerable: true
289
- });
290
-
291
- if (host.EventEmitterReferencingAsyncResource) {
292
- const kAsyncResource = Symbol('kAsyncResource');
293
- const EventEmitterReferencingAsyncResource = host.EventEmitterReferencingAsyncResource;
294
-
295
- class EventEmitterAsyncResource extends EventEmitter {
296
- /**
297
- * @param {{
298
- * name?: string,
299
- * triggerAsyncId?: number,
300
- * requireManualDestroy?: boolean,
301
- * }} [options]
302
- */
303
- constructor(options = undefined) {
304
- let name;
305
- if (typeof options === 'string') {
306
- name = options;
307
- options = undefined;
308
- } else {
309
- if (new.target === EventEmitterAsyncResource) {
310
- validateString(oc(options, 'name'), 'options.name');
311
- }
312
- name = oc(options, 'name') || new.target.name;
313
- }
314
- super(options);
315
-
316
- this[kAsyncResource] =
317
- new EventEmitterReferencingAsyncResource(this, name, options);
318
- }
319
-
320
- /**
321
- * @param {symbol,string} event
322
- * @param {...any} args
323
- * @returns {boolean}
324
- */
325
- emit(event, ...args) {
326
- if (this[kAsyncResource] === undefined)
327
- throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
328
- const { asyncResource } = this;
329
- ArrayPrototypeUnshift(args, super.emit, this, event);
330
- return ReflectApply(asyncResource.runInAsyncScope, asyncResource,
331
- args);
332
- }
333
-
334
- /**
335
- * @returns {void}
336
- */
337
- emitDestroy() {
338
- if (this[kAsyncResource] === undefined)
339
- throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
340
- this.asyncResource.emitDestroy();
341
- }
342
-
343
- /**
344
- * @type {number}
345
- */
346
- get asyncId() {
347
- if (this[kAsyncResource] === undefined)
348
- throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
349
- return this.asyncResource.asyncId();
350
- }
351
-
352
- /**
353
- * @type {number}
354
- */
355
- get triggerAsyncId() {
356
- if (this[kAsyncResource] === undefined)
357
- throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
358
- return this.asyncResource.triggerAsyncId();
359
- }
360
-
361
- /**
362
- * @type {EventEmitterReferencingAsyncResource}
363
- */
364
- get asyncResource() {
365
- if (this[kAsyncResource] === undefined)
366
- throw new ERR_INVALID_THIS('EventEmitterAsyncResource');
367
- return this[kAsyncResource];
368
- }
369
- }
370
- EventEmitter.EventEmitterAsyncResource = EventEmitterAsyncResource;
371
- }
372
-
373
- EventEmitter.errorMonitor = kErrorMonitor;
374
-
375
- // The default for captureRejections is false
376
- ObjectDefineProperty(EventEmitter.prototype, kCapture, {
377
- value: false,
378
- writable: true,
379
- enumerable: false
380
- });
381
-
382
- EventEmitter.prototype._events = undefined;
383
- EventEmitter.prototype._eventsCount = 0;
384
- EventEmitter.prototype._maxListeners = undefined;
385
-
386
- // By default EventEmitters will print a warning if more than 10 listeners are
387
- // added to it. This is a useful default which helps finding memory leaks.
388
- let defaultMaxListeners = 10;
389
-
390
- function checkListener(listener) {
391
- validateFunction(listener, 'listener');
392
- }
393
-
394
- ObjectDefineProperty(EventEmitter, 'defaultMaxListeners', {
395
- enumerable: true,
396
- get: function() {
397
- return defaultMaxListeners;
398
- },
399
- set: function(arg) {
400
- if (typeof arg !== 'number' || arg < 0 || NumberIsNaN(arg)) {
401
- throw new ERR_OUT_OF_RANGE('defaultMaxListeners',
402
- 'a non-negative number',
403
- arg);
404
- }
405
- defaultMaxListeners = arg;
406
- }
407
- });
408
-
409
- ObjectDefineProperties(EventEmitter, {
410
- kMaxEventTargetListeners: {
411
- value: kMaxEventTargetListeners,
412
- enumerable: false,
413
- configurable: false,
414
- writable: false,
415
- },
416
- kMaxEventTargetListenersWarned: {
417
- value: kMaxEventTargetListenersWarned,
418
- enumerable: false,
419
- configurable: false,
420
- writable: false,
421
- }
422
- });
423
-
424
- /**
425
- * Sets the max listeners.
426
- * @param {number} n
427
- * @param {EventTarget[] | EventEmitter[]} [eventTargets]
428
- * @returns {void}
429
- */
430
- EventEmitter.setMaxListeners =
431
- function(n = defaultMaxListeners, ...eventTargets) {
432
- if (typeof n !== 'number' || n < 0 || NumberIsNaN(n))
433
- throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
434
- if (eventTargets.length === 0) {
435
- defaultMaxListeners = n;
436
- } else {
437
- for (let i = 0; i < eventTargets.length; i++) {
438
- const target = eventTargets[i];
439
- if (isEventTarget(target)) {
440
- target[kMaxEventTargetListeners] = n;
441
- target[kMaxEventTargetListenersWarned] = false;
442
- } else if (typeof target.setMaxListeners === 'function') {
443
- target.setMaxListeners(n);
444
- } else {
445
- throw new ERR_INVALID_ARG_TYPE(
446
- 'eventTargets',
447
- ['EventEmitter', 'EventTarget'],
448
- target);
449
- }
450
- }
451
- }
452
- };
453
-
454
- // If you're updating this function definition, please also update any
455
- // re-definitions, such as the one in the Domain module (lib/domain.js).
456
- EventEmitter.init = function(opts) {
457
-
458
- if (this._events === undefined ||
459
- this._events === ObjectGetPrototypeOf(this)._events) {
460
- this._events = ObjectCreate(null);
461
- this._eventsCount = 0;
462
- }
463
-
464
- this._maxListeners = this._maxListeners || undefined;
465
-
466
-
467
- if (oc(opts, 'captureRejections')) {
468
- validateBoolean(opts.captureRejections, 'options.captureRejections');
469
- this[kCapture] = Boolean(opts.captureRejections);
470
- } else {
471
- // Assigning the kCapture property directly saves an expensive
472
- // prototype lookup in a very sensitive hot path.
473
- this[kCapture] = EventEmitter.prototype[kCapture];
474
- }
475
- };
476
-
477
- function addCatch(that, promise, type, args) {
478
- if (!that[kCapture]) {
479
- return;
480
- }
481
-
482
- // Handle Promises/A+ spec, then could be a getter
483
- // that throws on second use.
484
- try {
485
- const then = promise.then;
486
-
487
- if (typeof then === 'function') {
488
- then.call(promise, undefined, function(err) {
489
- // The callback is called with nextTick to avoid a follow-up
490
- // rejection from this promise.
491
- process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
492
- });
493
- }
494
- } catch (err) {
495
- that.emit('error', err);
496
- }
497
- }
498
-
499
- function emitUnhandledRejectionOrErr(ee, err, type, args) {
500
- if (typeof ee[kRejection] === 'function') {
501
- ee[kRejection](err, type, ...args);
502
- } else {
503
- // We have to disable the capture rejections mechanism, otherwise
504
- // we might end up in an infinite loop.
505
- const prev = ee[kCapture];
506
-
507
- // If the error handler throws, it is not catchable and it
508
- // will end up in 'uncaughtException'. We restore the previous
509
- // value of kCapture in case the uncaughtException is present
510
- // and the exception is handled.
511
- try {
512
- ee[kCapture] = false;
513
- ee.emit('error', err);
514
- } finally {
515
- ee[kCapture] = prev;
516
- }
517
- }
518
- }
519
-
520
- /**
521
- * Increases the max listeners of the event emitter.
522
- * @param {number} n
523
- * @returns {EventEmitter}
524
- */
525
- EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
526
- if (typeof n !== 'number' || n < 0 || NumberIsNaN(n)) {
527
- throw new ERR_OUT_OF_RANGE('n', 'a non-negative number', n);
528
- }
529
- this._maxListeners = n;
530
- return this;
531
- };
532
-
533
- function _getMaxListeners(that) {
534
- if (that._maxListeners === undefined)
535
- return EventEmitter.defaultMaxListeners;
536
- return that._maxListeners;
537
- }
538
-
539
- /**
540
- * Returns the current max listener value for the event emitter.
541
- * @returns {number}
542
- */
543
- EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
544
- return _getMaxListeners(this);
545
- };
546
-
547
- /**
548
- * Synchronously calls each of the listeners registered
549
- * for the event.
550
- * @param {string | symbol} type
551
- * @param {...any} [args]
552
- * @returns {boolean}
553
- */
554
- EventEmitter.prototype.emit = function emit(type, ...args) {
555
- let doError = (type === 'error');
556
-
557
- const events = this._events;
558
- if (events !== undefined) {
559
- if (doError && events[kErrorMonitor] !== undefined)
560
- this.emit(kErrorMonitor, ...args);
561
- doError = (doError && events.error === undefined);
562
- } else if (!doError)
563
- return false;
564
-
565
- // If there is no 'error' event listener then throw.
566
- if (doError) {
567
- let er;
568
- if (args.length > 0)
569
- er = args[0];
570
- if (er instanceof Error) {
571
- try {
572
- const capture = {};
573
- ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
574
- } catch (e) {}
575
-
576
- // Note: The comments on the `throw` lines are intentional, they show
577
- // up in Node's output if this results in an unhandled exception.
578
- throw er; // Unhandled 'error' event
579
- }
580
-
581
- let stringifiedEr;
582
- try {
583
- stringifiedEr = inspect(er);
584
- } catch (e) {
585
- stringifiedEr = er;
586
- }
587
-
588
- // At least give some kind of context to the user
589
- const err = new ERR_UNHANDLED_ERROR(stringifiedEr);
590
- err.context = er;
591
- throw err; // Unhandled 'error' event
592
- }
593
-
594
- const handler = events[type];
595
-
596
- if (handler === undefined)
597
- return false;
598
-
599
- if (typeof handler === 'function') {
600
- const result = handler.apply(this, args);
601
-
602
- // We check if result is undefined first because that
603
- // is the most common case so we do not pay any perf
604
- // penalty
605
- if (result !== undefined && result !== null) {
606
- addCatch(this, result, type, args);
607
- }
608
- } else {
609
- const len = handler.length;
610
- const listeners = arrayClone(handler);
611
- for (let i = 0; i < len; ++i) {
612
- const result = listeners[i].apply(this, args);
613
-
614
- // We check if result is undefined first because that
615
- // is the most common case so we do not pay any perf
616
- // penalty.
617
- // This code is duplicated because extracting it away
618
- // would make it non-inlineable.
619
- if (result !== undefined && result !== null) {
620
- addCatch(this, result, type, args);
621
- }
622
- }
623
- }
624
-
625
- return true;
626
- };
627
-
628
- function _addListener(target, type, listener, prepend) {
629
- let m;
630
- let events;
631
- let existing;
632
-
633
- checkListener(listener);
634
-
635
- events = target._events;
636
- if (events === undefined) {
637
- events = target._events = ObjectCreate(null);
638
- target._eventsCount = 0;
639
- } else {
640
- // To avoid recursion in the case that type === "newListener"! Before
641
- // adding it to the listeners, first emit "newListener".
642
- if (events.newListener !== undefined) {
643
- target.emit('newListener', type,
644
- nc(listener.listener, listener));
645
-
646
- // Re-assign `events` because a newListener handler could have caused the
647
- // this._events to be assigned to a new object
648
- events = target._events;
649
- }
650
- existing = events[type];
651
- }
652
-
653
- if (existing === undefined) {
654
- // Optimize the case of one listener. Don't need the extra array object.
655
- events[type] = listener;
656
- ++target._eventsCount;
657
- } else {
658
- if (typeof existing === 'function') {
659
- // Adding the second element, need to change to array.
660
- existing = events[type] =
661
- prepend ? [listener, existing] : [existing, listener];
662
- // If we've already got an array, just append.
663
- } else if (prepend) {
664
- existing.unshift(listener);
665
- } else {
666
- existing.push(listener);
667
- }
668
-
669
- // Check for listener leak
670
- m = _getMaxListeners(target);
671
- if (m > 0 && existing.length > m && !existing.warned) {
672
- existing.warned = true;
673
- // No error code for this since it is a Warning
674
- // eslint-disable-next-line no-restricted-syntax
675
- const w = new Error('Possible EventEmitter memory leak detected. ' +
676
- `${existing.length} ${String(type)} listeners ` +
677
- `added to ${inspect(target, { depth: -1 })}. Use ` +
678
- 'emitter.setMaxListeners() to increase limit');
679
- w.name = 'MaxListenersExceededWarning';
680
- w.emitter = target;
681
- w.type = type;
682
- w.count = existing.length;
683
- process.emitWarning(w);
684
- }
685
- }
686
-
687
- return target;
688
- }
689
-
690
- /**
691
- * Adds a listener to the event emitter.
692
- * @param {string | symbol} type
693
- * @param {Function} listener
694
- * @returns {EventEmitter}
695
- */
696
- EventEmitter.prototype.addListener = function addListener(type, listener) {
697
- return _addListener(this, type, listener, false);
698
- };
699
-
700
- EventEmitter.prototype.on = EventEmitter.prototype.addListener;
701
-
702
- /**
703
- * Adds the `listener` function to the beginning of
704
- * the listeners array.
705
- * @param {string | symbol} type
706
- * @param {Function} listener
707
- * @returns {EventEmitter}
708
- */
709
- EventEmitter.prototype.prependListener =
710
- function prependListener(type, listener) {
711
- return _addListener(this, type, listener, true);
712
- };
713
-
714
- function onceWrapper() {
715
- if (!this.fired) {
716
- this.target.removeListener(this.type, this.wrapFn);
717
- this.fired = true;
718
- if (arguments.length === 0)
719
- return this.listener.call(this.target);
720
- return this.listener.apply(this.target, arguments);
721
- }
722
- }
723
-
724
- function _onceWrap(target, type, listener) {
725
- const state = { fired: false, wrapFn: undefined, target, type, listener };
726
- const wrapped = onceWrapper.bind(state);
727
- wrapped.listener = listener;
728
- state.wrapFn = wrapped;
729
- return wrapped;
730
- }
731
-
732
- /**
733
- * Adds a one-time `listener` function to the event emitter.
734
- * @param {string | symbol} type
735
- * @param {Function} listener
736
- * @returns {EventEmitter}
737
- */
738
- EventEmitter.prototype.once = function once(type, listener) {
739
- checkListener(listener);
740
-
741
- this.on(type, _onceWrap(this, type, listener));
742
- return this;
743
- };
744
-
745
- /**
746
- * Adds a one-time `listener` function to the beginning of
747
- * the listeners array.
748
- * @param {string | symbol} type
749
- * @param {Function} listener
750
- * @returns {EventEmitter}
751
- */
752
- EventEmitter.prototype.prependOnceListener =
753
- function prependOnceListener(type, listener) {
754
- checkListener(listener);
755
-
756
- this.prependListener(type, _onceWrap(this, type, listener));
757
- return this;
758
- };
759
-
760
-
761
- /**
762
- * Removes the specified `listener` from the listeners array.
763
- * @param {string | symbol} type
764
- * @param {Function} listener
765
- * @returns {EventEmitter}
766
- */
767
- EventEmitter.prototype.removeListener =
768
- function removeListener(type, listener) {
769
- checkListener(listener);
770
-
771
- const events = this._events;
772
- if (events === undefined)
773
- return this;
774
-
775
- const list = events[type];
776
- if (list === undefined)
777
- return this;
778
-
779
- if (list === listener || list.listener === listener) {
780
- if (--this._eventsCount === 0)
781
- this._events = ObjectCreate(null);
782
- else {
783
- delete events[type];
784
- if (events.removeListener)
785
- this.emit('removeListener', type, list.listener || listener);
786
- }
787
- } else if (typeof list !== 'function') {
788
- let position = -1;
789
-
790
- for (let i = list.length - 1; i >= 0; i--) {
791
- if (list[i] === listener || list[i].listener === listener) {
792
- position = i;
793
- break;
794
- }
795
- }
796
-
797
- if (position < 0)
798
- return this;
799
-
800
- if (position === 0)
801
- list.shift();
802
- else {
803
- spliceOne(list, position);
804
- }
805
-
806
- if (list.length === 1)
807
- events[type] = list[0];
808
-
809
- if (events.removeListener !== undefined)
810
- this.emit('removeListener', type, listener);
811
- }
812
-
813
- return this;
814
- };
815
-
816
- EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
817
-
818
- /**
819
- * Removes all listeners from the event emitter. (Only
820
- * removes listeners for a specific event name if specified
821
- * as `type`).
822
- * @param {string | symbol} [type]
823
- * @returns {EventEmitter}
824
- */
825
- EventEmitter.prototype.removeAllListeners =
826
- function removeAllListeners(type) {
827
- const events = this._events;
828
- if (events === undefined)
829
- return this;
830
-
831
- // Not listening for removeListener, no need to emit
832
- if (events.removeListener === undefined) {
833
- if (arguments.length === 0) {
834
- this._events = ObjectCreate(null);
835
- this._eventsCount = 0;
836
- } else if (events[type] !== undefined) {
837
- if (--this._eventsCount === 0)
838
- this._events = ObjectCreate(null);
839
- else
840
- delete events[type];
841
- }
842
- return this;
843
- }
844
-
845
- // Emit removeListener for all listeners on all events
846
- if (arguments.length === 0) {
847
- for (const key of ReflectOwnKeys(events)) {
848
- if (key === 'removeListener') continue;
849
- this.removeAllListeners(key);
850
- }
851
- this.removeAllListeners('removeListener');
852
- this._events = ObjectCreate(null);
853
- this._eventsCount = 0;
854
- return this;
855
- }
856
-
857
- const listeners = events[type];
858
-
859
- if (typeof listeners === 'function') {
860
- this.removeListener(type, listeners);
861
- } else if (listeners !== undefined) {
862
- // LIFO order
863
- for (let i = listeners.length - 1; i >= 0; i--) {
864
- this.removeListener(type, listeners[i]);
865
- }
866
- }
867
-
868
- return this;
869
- };
870
-
871
- function _listeners(target, type, unwrap) {
872
- const events = target._events;
873
-
874
- if (events === undefined)
875
- return [];
876
-
877
- const evlistener = events[type];
878
- if (evlistener === undefined)
879
- return [];
880
-
881
- if (typeof evlistener === 'function')
882
- return unwrap ? [evlistener.listener || evlistener] : [evlistener];
883
-
884
- return unwrap ?
885
- unwrapListeners(evlistener) : arrayClone(evlistener);
886
- }
887
-
888
- /**
889
- * Returns a copy of the array of listeners for the event name
890
- * specified as `type`.
891
- * @param {string | symbol} type
892
- * @returns {Function[]}
893
- */
894
- EventEmitter.prototype.listeners = function listeners(type) {
895
- return _listeners(this, type, true);
896
- };
897
-
898
- /**
899
- * Returns a copy of the array of listeners and wrappers for
900
- * the event name specified as `type`.
901
- * @param {string | symbol} type
902
- * @returns {Function[]}
903
- */
904
- EventEmitter.prototype.rawListeners = function rawListeners(type) {
905
- return _listeners(this, type, false);
906
- };
907
-
908
- /**
909
- * Returns the number of listeners listening to the event name
910
- * specified as `type`.
911
- * @deprecated since v3.2.0
912
- * @param {EventEmitter} emitter
913
- * @param {string | symbol} type
914
- * @returns {number}
915
- */
916
- EventEmitter.listenerCount = function(emitter, type) {
917
- if (typeof emitter.listenerCount === 'function') {
918
- return emitter.listenerCount(type);
919
- }
920
- return emitter.listenerCount(type);
921
- };
922
-
923
- EventEmitter.prototype.listenerCount = listenerCount;
924
-
925
- /**
926
- * Returns the number of listeners listening to event name
927
- * specified as `type`.
928
- * @param {string | symbol} type
929
- * @returns {number}
930
- */
931
- function listenerCount(type) {
932
- const events = this._events;
933
-
934
- if (events !== undefined) {
935
- const evlistener = events[type];
936
-
937
- if (typeof evlistener === 'function') {
938
- return 1;
939
- } else if (evlistener !== undefined) {
940
- return evlistener.length;
941
- }
942
- }
943
-
944
- return 0;
945
- }
946
-
947
- /**
948
- * Returns an array listing the events for which
949
- * the emitter has registered listeners.
950
- * @returns {any[]}
951
- */
952
- EventEmitter.prototype.eventNames = function eventNames() {
953
- return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
954
- };
955
-
956
- function arrayClone(arr) {
957
- // At least since V8 8.3, this implementation is faster than the previous
958
- // which always used a simple for-loop
959
- switch (arr.length) {
960
- case 2: return [arr[0], arr[1]];
961
- case 3: return [arr[0], arr[1], arr[2]];
962
- case 4: return [arr[0], arr[1], arr[2], arr[3]];
963
- case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]];
964
- case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
965
- }
966
- return ArrayPrototypeSlice(arr);
967
- }
968
-
969
- function unwrapListeners(arr) {
970
- const ret = arrayClone(arr);
971
- for (let i = 0; i < ret.length; ++i) {
972
- const orig = ret[i].listener;
973
- if (typeof orig === 'function')
974
- ret[i] = orig;
975
- }
976
- return ret;
977
- }