This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.mjs
4092 lines (4012 loc) · 150 KB
/
entry.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { performance } from 'node:perf_hooks'
import {
ByteLengthQueuingStrategy,
CountQueuingStrategy,
ReadableByteStreamController,
ReadableStream,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
ReadableStreamDefaultController,
ReadableStreamDefaultReader,
TransformStream,
WritableStream,
WritableStreamDefaultController,
WritableStreamDefaultWriter,
} from 'node:stream/web'
import { File, FormData, Headers, Request, Response, fetch } from 'undici'
import { setTimeout as setTimeout$1, clearTimeout as clearTimeout$1 } from 'node:timers'
import { N as NodeApp, h as server_default, i as deserializeManifest } from './chunks/astro.ff1242f6.mjs'
import { Readable } from 'stream'
import https from 'https'
import path from 'path'
import { fileURLToPath } from 'url'
import fs from 'fs'
import http from 'http'
import send from 'send'
import enableDestroy from 'server-destroy'
import {
_ as _page0,
a as _page1,
b as _page2,
c as _page3,
d as _page4,
e as _page5,
f as _page6,
g as _page7,
h as _page8,
i as _page9,
} from './chunks/pages/all.5f06c641.mjs'
import 'mime'
import 'cookie'
import 'kleur/colors'
import 'slash'
import 'path-to-regexp'
import 'tls'
import 'string-width'
import 'html-escaper'
import 'sharp'
import 'node:fs/promises'
import 'node:path'
import 'node:url'
import 'http-cache-semantics'
import 'node:os'
import 'image-size'
import 'magic-string'
import 'node:stream'
import '@vercel/analytics'
import '@astrojs/rss'
import '@supabase/supabase-js'
import 'moment'
/* empty css */ import 'svgo'
import 'limax'
import 'reading-time'
/** Returns the function bound to the given object. */
const __function_bind = Function.bind.bind(Function.call)
/** Returns whether the object prototype exists in another object. */
const __object_isPrototypeOf = Function.call.bind(Object.prototype.isPrototypeOf)
/** Current high resolution millisecond timestamp. */
const __performance_now = performance.now
// @ts-expect-error
const INTERNALS = new WeakMap()
const internalsOf = (target, className, propName) => {
const internals = INTERNALS.get(target)
if (!internals) throw new TypeError(`${className}.${propName} can only be used on instances of ${className}`)
return internals
}
const allowStringTag = (value) => (value.prototype[Symbol.toStringTag] = value.name)
class DOMException extends Error {
constructor(message = '', name = 'Error') {
super(message)
this.code = 0
this.name = name
}
}
DOMException.INDEX_SIZE_ERR = 1
DOMException.DOMSTRING_SIZE_ERR = 2
DOMException.HIERARCHY_REQUEST_ERR = 3
DOMException.WRONG_DOCUMENT_ERR = 4
DOMException.INVALID_CHARACTER_ERR = 5
DOMException.NO_DATA_ALLOWED_ERR = 6
DOMException.NO_MODIFICATION_ALLOWED_ERR = 7
DOMException.NOT_FOUND_ERR = 8
DOMException.NOT_SUPPORTED_ERR = 9
DOMException.INUSE_ATTRIBUTE_ERR = 10
DOMException.INVALID_STATE_ERR = 11
DOMException.SYNTAX_ERR = 12
DOMException.INVALID_MODIFICATION_ERR = 13
DOMException.NAMESPACE_ERR = 14
DOMException.INVALID_ACCESS_ERR = 15
DOMException.VALIDATION_ERR = 16
DOMException.TYPE_MISMATCH_ERR = 17
DOMException.SECURITY_ERR = 18
DOMException.NETWORK_ERR = 19
DOMException.ABORT_ERR = 20
DOMException.URL_MISMATCH_ERR = 21
DOMException.QUOTA_EXCEEDED_ERR = 22
DOMException.TIMEOUT_ERR = 23
DOMException.INVALID_NODE_TYPE_ERR = 24
DOMException.DATA_CLONE_ERR = 25
allowStringTag(DOMException)
/**
* Assert a condition.
* @param condition The condition that it should satisfy.
* @param message The error message.
* @param args The arguments for replacing placeholders in the message.
*/
function assertType(condition, message, ...args) {
if (!condition) {
throw new TypeError(format(message, args))
}
}
/**
* Convert a text and arguments to one string.
* @param message The formating text
* @param args The arguments.
*/
function format(message, args) {
let i = 0
return message.replace(/%[os]/gu, () => anyToString(args[i++]))
}
/**
* Convert a value to a string representation.
* @param x The value to get the string representation.
*/
function anyToString(x) {
if (typeof x !== 'object' || x === null) {
return String(x)
}
return Object.prototype.toString.call(x)
}
let currentErrorHandler
/**
* Print a error message.
* @param maybeError The error object.
*/
function reportError(maybeError) {
try {
const error = maybeError instanceof Error ? maybeError : new Error(anyToString(maybeError))
// Call the user-defined error handler if exists.
if (currentErrorHandler);
// Dispatch an `error` event if this is on a browser.
if (typeof dispatchEvent === 'function' && typeof ErrorEvent === 'function') {
dispatchEvent(new ErrorEvent('error', { error, message: error.message }))
}
// Emit an `uncaughtException` event if this is on Node.js.
//istanbul ignore else
else if (typeof process !== 'undefined' && typeof process.emit === 'function') {
process.emit('uncaughtException', error)
return
}
// Otherwise, print the error.
console.error(error)
} catch (_a) {
// ignore.
}
}
let currentWarnHandler
/**
* The warning information.
*/
class Warning {
constructor(code, message) {
this.code = code
this.message = message
}
/**
* Report this warning.
* @param args The arguments of the warning.
*/
warn(...args) {
var _a
try {
// Call the user-defined warning handler if exists.
if (currentWarnHandler);
// Otherwise, print the warning.
const stack = ((_a = new Error().stack) !== null && _a !== void 0 ? _a : '').replace(
/^(?:.+?\n){2}/gu,
'\n'
)
console.warn(this.message, ...args, stack)
} catch (_b) {
// Ignore.
}
}
}
const InitEventWasCalledWhileDispatching = new Warning('W01', 'Unable to initialize event under dispatching.')
const FalsyWasAssignedToCancelBubble = new Warning(
'W02',
"Assigning any falsy value to 'cancelBubble' property has no effect."
)
const TruthyWasAssignedToReturnValue = new Warning(
'W03',
"Assigning any truthy value to 'returnValue' property has no effect."
)
const NonCancelableEventWasCanceled = new Warning('W04', 'Unable to preventDefault on non-cancelable events.')
const CanceledInPassiveListener = new Warning(
'W05',
'Unable to preventDefault inside passive event listener invocation.'
)
const EventListenerWasDuplicated = new Warning(
'W06',
"An event listener wasn't added because it has been added already: %o, %o"
)
const OptionWasIgnored = new Warning(
'W07',
"The %o option value was abandoned because the event listener wasn't added as duplicated."
)
const InvalidEventListener = new Warning(
'W08',
"The 'callback' argument must be a function or an object that has 'handleEvent' method: %o"
)
/*eslint-disable class-methods-use-this */
/**
* An implementation of `Event` interface, that wraps a given event object.
* `EventTarget` shim can control the internal state of this `Event` objects.
* @see https://dom.spec.whatwg.org/#event
*/
class Event {
/**
* @see https://dom.spec.whatwg.org/#dom-event-none
*/
static get NONE() {
return NONE
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
*/
static get CAPTURING_PHASE() {
return CAPTURING_PHASE
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-at_target
*/
static get AT_TARGET() {
return AT_TARGET
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
*/
static get BUBBLING_PHASE() {
return BUBBLING_PHASE
}
/**
* Initialize this event instance.
* @param type The type of this event.
* @param eventInitDict Options to initialize.
* @see https://dom.spec.whatwg.org/#dom-event-event
*/
constructor(type, eventInitDict) {
Object.defineProperty(this, 'isTrusted', {
value: false,
enumerable: true,
})
const opts = eventInitDict !== null && eventInitDict !== void 0 ? eventInitDict : {}
internalDataMap.set(this, {
type: String(type),
bubbles: Boolean(opts.bubbles),
cancelable: Boolean(opts.cancelable),
composed: Boolean(opts.composed),
target: null,
currentTarget: null,
stopPropagationFlag: false,
stopImmediatePropagationFlag: false,
canceledFlag: false,
inPassiveListenerFlag: false,
dispatchFlag: false,
timeStamp: Date.now(),
})
}
/**
* The type of this event.
* @see https://dom.spec.whatwg.org/#dom-event-type
*/
get type() {
return $(this).type
}
/**
* The event target of the current dispatching.
* @see https://dom.spec.whatwg.org/#dom-event-target
*/
get target() {
return $(this).target
}
/**
* The event target of the current dispatching.
* @deprecated Use the `target` property instead.
* @see https://dom.spec.whatwg.org/#dom-event-srcelement
*/
get srcElement() {
return $(this).target
}
/**
* The event target of the current dispatching.
* @see https://dom.spec.whatwg.org/#dom-event-currenttarget
*/
get currentTarget() {
return $(this).currentTarget
}
/**
* The event target of the current dispatching.
* This doesn't support node tree.
* @see https://dom.spec.whatwg.org/#dom-event-composedpath
*/
composedPath() {
const currentTarget = $(this).currentTarget
if (currentTarget) {
return [currentTarget]
}
return []
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-none
*/
get NONE() {
return NONE
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-capturing_phase
*/
get CAPTURING_PHASE() {
return CAPTURING_PHASE
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-at_target
*/
get AT_TARGET() {
return AT_TARGET
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-bubbling_phase
*/
get BUBBLING_PHASE() {
return BUBBLING_PHASE
}
/**
* The current event phase.
* @see https://dom.spec.whatwg.org/#dom-event-eventphase
*/
get eventPhase() {
return $(this).dispatchFlag ? 2 : 0
}
/**
* Stop event bubbling.
* Because this shim doesn't support node tree, this merely changes the `cancelBubble` property value.
* @see https://dom.spec.whatwg.org/#dom-event-stoppropagation
*/
stopPropagation() {
$(this).stopPropagationFlag = true
}
/**
* `true` if event bubbling was stopped.
* @deprecated
* @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
*/
get cancelBubble() {
return $(this).stopPropagationFlag
}
/**
* Stop event bubbling if `true` is set.
* @deprecated Use the `stopPropagation()` method instead.
* @see https://dom.spec.whatwg.org/#dom-event-cancelbubble
*/
set cancelBubble(value) {
if (value) {
$(this).stopPropagationFlag = true
} else {
FalsyWasAssignedToCancelBubble.warn()
}
}
/**
* Stop event bubbling and subsequent event listener callings.
* @see https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation
*/
stopImmediatePropagation() {
const data = $(this)
data.stopPropagationFlag = data.stopImmediatePropagationFlag = true
}
/**
* `true` if this event will bubble.
* @see https://dom.spec.whatwg.org/#dom-event-bubbles
*/
get bubbles() {
return $(this).bubbles
}
/**
* `true` if this event can be canceled by the `preventDefault()` method.
* @see https://dom.spec.whatwg.org/#dom-event-cancelable
*/
get cancelable() {
return $(this).cancelable
}
/**
* `true` if the default behavior will act.
* @deprecated Use the `defaultPrevented` proeprty instead.
* @see https://dom.spec.whatwg.org/#dom-event-returnvalue
*/
get returnValue() {
return !$(this).canceledFlag
}
/**
* Cancel the default behavior if `false` is set.
* @deprecated Use the `preventDefault()` method instead.
* @see https://dom.spec.whatwg.org/#dom-event-returnvalue
*/
set returnValue(value) {
if (!value) {
setCancelFlag($(this))
} else {
TruthyWasAssignedToReturnValue.warn()
}
}
/**
* Cancel the default behavior.
* @see https://dom.spec.whatwg.org/#dom-event-preventdefault
*/
preventDefault() {
setCancelFlag($(this))
}
/**
* `true` if the default behavior was canceled.
* @see https://dom.spec.whatwg.org/#dom-event-defaultprevented
*/
get defaultPrevented() {
return $(this).canceledFlag
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-composed
*/
get composed() {
return $(this).composed
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-istrusted
*/
//istanbul ignore next
get isTrusted() {
return false
}
/**
* @see https://dom.spec.whatwg.org/#dom-event-timestamp
*/
get timeStamp() {
return $(this).timeStamp
}
/**
* @deprecated Don't use this method. The constructor did initialization.
*/
initEvent(type, bubbles = false, cancelable = false) {
const data = $(this)
if (data.dispatchFlag) {
InitEventWasCalledWhileDispatching.warn()
return
}
internalDataMap.set(this, {
...data,
type: String(type),
bubbles: Boolean(bubbles),
cancelable: Boolean(cancelable),
target: null,
currentTarget: null,
stopPropagationFlag: false,
stopImmediatePropagationFlag: false,
canceledFlag: false,
})
}
}
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const NONE = 0
const CAPTURING_PHASE = 1
const AT_TARGET = 2
const BUBBLING_PHASE = 3
/**
* Private data for event wrappers.
*/
const internalDataMap = new WeakMap()
/**
* Get private data.
* @param event The event object to get private data.
* @param name The variable name to report.
* @returns The private data of the event.
*/
function $(event, name = 'this') {
const retv = internalDataMap.get(event)
assertType(
retv != null,
"'%s' must be an object that Event constructor created, but got another one: %o",
name,
event
)
return retv
}
/**
* https://dom.spec.whatwg.org/#set-the-canceled-flag
* @param data private data.
*/
function setCancelFlag(data) {
if (data.inPassiveListenerFlag) {
CanceledInPassiveListener.warn()
return
}
if (!data.cancelable) {
NonCancelableEventWasCanceled.warn()
return
}
data.canceledFlag = true
}
// Set enumerable
Object.defineProperty(Event, 'NONE', { enumerable: true })
Object.defineProperty(Event, 'CAPTURING_PHASE', { enumerable: true })
Object.defineProperty(Event, 'AT_TARGET', { enumerable: true })
Object.defineProperty(Event, 'BUBBLING_PHASE', { enumerable: true })
const keys$1 = Object.getOwnPropertyNames(Event.prototype)
for (let i = 0; i < keys$1.length; ++i) {
if (keys$1[i] === 'constructor') {
continue
}
Object.defineProperty(Event.prototype, keys$1[i], { enumerable: true })
}
/**
* An implementation of `Event` interface, that wraps a given event object.
* This class controls the internal state of `Event`.
* @see https://dom.spec.whatwg.org/#interface-event
*/
class EventWrapper extends Event {
/**
* Wrap a given event object to control states.
* @param event The event-like object to wrap.
*/
static wrap(event) {
return new (getWrapperClassOf(event))(event)
}
constructor(event) {
super(event.type, {
bubbles: event.bubbles,
cancelable: event.cancelable,
composed: event.composed,
})
if (event.cancelBubble) {
super.stopPropagation()
}
if (event.defaultPrevented) {
super.preventDefault()
}
internalDataMap$1.set(this, { original: event })
// Define accessors
const keys = Object.keys(event)
for (let i = 0; i < keys.length; ++i) {
const key = keys[i]
if (!(key in this)) {
Object.defineProperty(this, key, defineRedirectDescriptor(event, key))
}
}
}
stopPropagation() {
super.stopPropagation()
const { original } = $$1(this)
if ('stopPropagation' in original) {
original.stopPropagation()
}
}
get cancelBubble() {
return super.cancelBubble
}
set cancelBubble(value) {
super.cancelBubble = value
const { original } = $$1(this)
if ('cancelBubble' in original) {
original.cancelBubble = value
}
}
stopImmediatePropagation() {
super.stopImmediatePropagation()
const { original } = $$1(this)
if ('stopImmediatePropagation' in original) {
original.stopImmediatePropagation()
}
}
get returnValue() {
return super.returnValue
}
set returnValue(value) {
super.returnValue = value
const { original } = $$1(this)
if ('returnValue' in original) {
original.returnValue = value
}
}
preventDefault() {
super.preventDefault()
const { original } = $$1(this)
if ('preventDefault' in original) {
original.preventDefault()
}
}
get timeStamp() {
const { original } = $$1(this)
if ('timeStamp' in original) {
return original.timeStamp
}
return super.timeStamp
}
}
/**
* Private data for event wrappers.
*/
const internalDataMap$1 = new WeakMap()
/**
* Get private data.
* @param event The event object to get private data.
* @returns The private data of the event.
*/
function $$1(event) {
const retv = internalDataMap$1.get(event)
assertType(retv != null, "'this' is expected an Event object, but got", event)
return retv
}
/**
* Cache for wrapper classes.
* @type {WeakMap<Object, Function>}
* @private
*/
const wrapperClassCache = new WeakMap()
// Make association for wrappers.
wrapperClassCache.set(Object.prototype, EventWrapper)
/**
* Get the wrapper class of a given prototype.
* @param originalEvent The event object to wrap.
*/
function getWrapperClassOf(originalEvent) {
const prototype = Object.getPrototypeOf(originalEvent)
if (prototype == null) {
return EventWrapper
}
let wrapper = wrapperClassCache.get(prototype)
if (wrapper == null) {
wrapper = defineWrapper(getWrapperClassOf(prototype), prototype)
wrapperClassCache.set(prototype, wrapper)
}
return wrapper
}
/**
* Define new wrapper class.
* @param BaseEventWrapper The base wrapper class.
* @param originalPrototype The prototype of the original event.
*/
function defineWrapper(BaseEventWrapper, originalPrototype) {
class CustomEventWrapper extends BaseEventWrapper {}
const keys = Object.keys(originalPrototype)
for (let i = 0; i < keys.length; ++i) {
Object.defineProperty(
CustomEventWrapper.prototype,
keys[i],
defineRedirectDescriptor(originalPrototype, keys[i])
)
}
return CustomEventWrapper
}
/**
* Get the property descriptor to redirect a given property.
*/
function defineRedirectDescriptor(obj, key) {
const d = Object.getOwnPropertyDescriptor(obj, key)
return {
get() {
const original = $$1(this).original
const value = original[key]
if (typeof value === 'function') {
return value.bind(original)
}
return value
},
set(value) {
const original = $$1(this).original
original[key] = value
},
configurable: d.configurable,
enumerable: d.enumerable,
}
}
/**
* Create a new listener.
* @param callback The callback function.
* @param capture The capture flag.
* @param passive The passive flag.
* @param once The once flag.
* @param signal The abort signal.
* @param signalListener The abort event listener for the abort signal.
*/
function createListener(callback, capture, passive, once, signal, signalListener) {
return {
callback,
flags: (capture ? 1 /* Capture */ : 0) | (passive ? 2 /* Passive */ : 0) | (once ? 4 /* Once */ : 0),
signal,
signalListener,
}
}
/**
* Set the `removed` flag to the given listener.
* @param listener The listener to check.
*/
function setRemoved(listener) {
listener.flags |= 8 /* Removed */
}
/**
* Check if the given listener has the `capture` flag or not.
* @param listener The listener to check.
*/
function isCapture(listener) {
return (listener.flags & 1) /* Capture */ === 1 /* Capture */
}
/**
* Check if the given listener has the `passive` flag or not.
* @param listener The listener to check.
*/
function isPassive(listener) {
return (listener.flags & 2) /* Passive */ === 2 /* Passive */
}
/**
* Check if the given listener has the `once` flag or not.
* @param listener The listener to check.
*/
function isOnce(listener) {
return (listener.flags & 4) /* Once */ === 4 /* Once */
}
/**
* Check if the given listener has the `removed` flag or not.
* @param listener The listener to check.
*/
function isRemoved(listener) {
return (listener.flags & 8) /* Removed */ === 8 /* Removed */
}
/**
* Call an event listener.
* @param listener The listener to call.
* @param target The event target object for `thisArg`.
* @param event The event object for the first argument.
* @param attribute `true` if this callback is an event attribute handler.
*/
function invokeCallback({ callback }, target, event) {
try {
if (typeof callback === 'function') {
callback.call(target, event)
} else if (typeof callback.handleEvent === 'function') {
callback.handleEvent(event)
}
} catch (thrownError) {
reportError(thrownError)
}
}
/**
* Find the index of given listener.
* This returns `-1` if not found.
* @param list The listener list.
* @param callback The callback function to find.
* @param capture The capture flag to find.
*/
function findIndexOfListener({ listeners }, callback, capture) {
for (let i = 0; i < listeners.length; ++i) {
if (listeners[i].callback === callback && isCapture(listeners[i]) === capture) {
return i
}
}
return -1
}
/**
* Add the given listener.
* Does copy-on-write if needed.
* @param list The listener list.
* @param callback The callback function.
* @param capture The capture flag.
* @param passive The passive flag.
* @param once The once flag.
* @param signal The abort signal.
*/
function addListener(list, callback, capture, passive, once, signal) {
let signalListener
if (signal) {
signalListener = removeListener.bind(null, list, callback, capture)
signal.addEventListener('abort', signalListener)
}
const listener = createListener(callback, capture, passive, once, signal, signalListener)
if (list.cow) {
list.cow = false
list.listeners = [...list.listeners, listener]
} else {
list.listeners.push(listener)
}
return listener
}
/**
* Remove a listener.
* @param list The listener list.
* @param callback The callback function to find.
* @param capture The capture flag to find.
* @returns `true` if it mutated the list directly.
*/
function removeListener(list, callback, capture) {
const index = findIndexOfListener(list, callback, capture)
if (index !== -1) {
return removeListenerAt(list, index)
}
return false
}
/**
* Remove a listener.
* @param list The listener list.
* @param index The index of the target listener.
* @param disableCow Disable copy-on-write if true.
* @returns `true` if it mutated the `listeners` array directly.
*/
function removeListenerAt(list, index, disableCow = false) {
const listener = list.listeners[index]
// Set the removed flag.
setRemoved(listener)
// Dispose the abort signal listener if exists.
if (listener.signal) {
listener.signal.removeEventListener('abort', listener.signalListener)
}
// Remove it from the array.
if (list.cow && !disableCow) {
list.cow = false
list.listeners = list.listeners.filter((_, i) => i !== index)
return false
}
list.listeners.splice(index, 1)
return true
}
/**
* Create a new `ListenerListMap` object.
*/
function createListenerListMap() {
return Object.create(null)
}
/**
* Get the listener list of the given type.
* If the listener list has not been initialized, initialize and return it.
* @param listenerMap The listener list map.
* @param type The event type to get.
*/
function ensureListenerList(listenerMap, type) {
var _a
return (_a = listenerMap[type]) !== null && _a !== void 0
? _a
: (listenerMap[type] = {
attrCallback: undefined,
attrListener: undefined,
cow: false,
listeners: [],
})
}
/**
* An implementation of the `EventTarget` interface.
* @see https://dom.spec.whatwg.org/#eventtarget
*/
class EventTarget {
/**
* Initialize this instance.
*/
constructor() {
internalDataMap$2.set(this, createListenerListMap())
}
// Implementation
addEventListener(type0, callback0, options0) {
const listenerMap = $$2(this)
const { callback, capture, once, passive, signal, type } = normalizeAddOptions(type0, callback0, options0)
if (callback == null || (signal === null || signal === void 0 ? void 0 : signal.aborted)) {
return
}
const list = ensureListenerList(listenerMap, type)
// Find existing listener.
const i = findIndexOfListener(list, callback, capture)
if (i !== -1) {
warnDuplicate(list.listeners[i], passive, once, signal)
return
}
// Add the new listener.
addListener(list, callback, capture, passive, once, signal)
}
// Implementation
removeEventListener(type0, callback0, options0) {
const listenerMap = $$2(this)
const { callback, capture, type } = normalizeOptions(type0, callback0, options0)
const list = listenerMap[type]
if (callback != null && list) {
removeListener(list, callback, capture)
}
}
// Implementation
dispatchEvent(e) {
const list = $$2(this)[String(e.type)]
if (list == null) {
return true
}
const event = e instanceof Event ? e : EventWrapper.wrap(e)
const eventData = $(event, 'event')
if (eventData.dispatchFlag) {
throw new DOMException('This event has been in dispatching.')
}
eventData.dispatchFlag = true
eventData.target = eventData.currentTarget = this
if (!eventData.stopPropagationFlag) {
const { cow, listeners } = list
// Set copy-on-write flag.
list.cow = true
// Call listeners.
for (let i = 0; i < listeners.length; ++i) {
const listener = listeners[i]
// Skip if removed.
if (isRemoved(listener)) {
continue
}
// Remove this listener if has the `once` flag.
if (isOnce(listener) && removeListenerAt(list, i, !cow)) {
// Because this listener was removed, the next index is the
// same as the current value.
i -= 1
}
// Call this listener with the `passive` flag.
eventData.inPassiveListenerFlag = isPassive(listener)
invokeCallback(listener, this, event)
eventData.inPassiveListenerFlag = false
// Stop if the `event.stopImmediatePropagation()` method was called.
if (eventData.stopImmediatePropagationFlag) {
break
}
}
// Restore copy-on-write flag.
if (!cow) {
list.cow = false
}
}
eventData.target = null
eventData.currentTarget = null
eventData.stopImmediatePropagationFlag = false
eventData.stopPropagationFlag = false
eventData.dispatchFlag = false
return !eventData.canceledFlag
}
}
/**
* Internal data.
*/
const internalDataMap$2 = new WeakMap()
/**
* Get private data.
* @param target The event target object to get private data.
* @param name The variable name to report.
* @returns The private data of the event.
*/
function $$2(target, name = 'this') {
const retv = internalDataMap$2.get(target)
assertType(
retv != null,
"'%s' must be an object that EventTarget constructor created, but got another one: %o",
name,
target
)
return retv
}
/**
* Normalize options.
* @param options The options to normalize.
*/
function normalizeAddOptions(type, callback, options) {
var _a
assertCallback(callback)
if (typeof options === 'object' && options !== null) {
return {
type: String(type),
callback: callback !== null && callback !== void 0 ? callback : undefined,
capture: Boolean(options.capture),
passive: Boolean(options.passive),
once: Boolean(options.once),
signal: (_a = options.signal) !== null && _a !== void 0 ? _a : undefined,
}
}
return {