123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- 'use strict'
- const { webidl } = require('../fetch/webidl')
- const { kEnumerableProperty } = require('../core/util')
- const { MessagePort } = require('worker_threads')
- /**
- * @see https://html.spec.whatwg.org/multipage/comms.html#messageevent
- */
- class MessageEvent extends Event {
- #eventInit
- constructor (type, eventInitDict = {}) {
- webidl.argumentLengthCheck(arguments, 1, { header: 'MessageEvent constructor' })
- type = webidl.converters.DOMString(type)
- eventInitDict = webidl.converters.MessageEventInit(eventInitDict)
- super(type, eventInitDict)
- this.#eventInit = eventInitDict
- }
- get data () {
- webidl.brandCheck(this, MessageEvent)
- return this.#eventInit.data
- }
- get origin () {
- webidl.brandCheck(this, MessageEvent)
- return this.#eventInit.origin
- }
- get lastEventId () {
- webidl.brandCheck(this, MessageEvent)
- return this.#eventInit.lastEventId
- }
- get source () {
- webidl.brandCheck(this, MessageEvent)
- return this.#eventInit.source
- }
- get ports () {
- webidl.brandCheck(this, MessageEvent)
- if (!Object.isFrozen(this.#eventInit.ports)) {
- Object.freeze(this.#eventInit.ports)
- }
- return this.#eventInit.ports
- }
- initMessageEvent (
- type,
- bubbles = false,
- cancelable = false,
- data = null,
- origin = '',
- lastEventId = '',
- source = null,
- ports = []
- ) {
- webidl.brandCheck(this, MessageEvent)
- webidl.argumentLengthCheck(arguments, 1, { header: 'MessageEvent.initMessageEvent' })
- return new MessageEvent(type, {
- bubbles, cancelable, data, origin, lastEventId, source, ports
- })
- }
- }
- /**
- * @see https://websockets.spec.whatwg.org/#the-closeevent-interface
- */
- class CloseEvent extends Event {
- #eventInit
- constructor (type, eventInitDict = {}) {
- webidl.argumentLengthCheck(arguments, 1, { header: 'CloseEvent constructor' })
- type = webidl.converters.DOMString(type)
- eventInitDict = webidl.converters.CloseEventInit(eventInitDict)
- super(type, eventInitDict)
- this.#eventInit = eventInitDict
- }
- get wasClean () {
- webidl.brandCheck(this, CloseEvent)
- return this.#eventInit.wasClean
- }
- get code () {
- webidl.brandCheck(this, CloseEvent)
- return this.#eventInit.code
- }
- get reason () {
- webidl.brandCheck(this, CloseEvent)
- return this.#eventInit.reason
- }
- }
- // https://html.spec.whatwg.org/multipage/webappapis.html#the-errorevent-interface
- class ErrorEvent extends Event {
- #eventInit
- constructor (type, eventInitDict) {
- webidl.argumentLengthCheck(arguments, 1, { header: 'ErrorEvent constructor' })
- super(type, eventInitDict)
- type = webidl.converters.DOMString(type)
- eventInitDict = webidl.converters.ErrorEventInit(eventInitDict ?? {})
- this.#eventInit = eventInitDict
- }
- get message () {
- webidl.brandCheck(this, ErrorEvent)
- return this.#eventInit.message
- }
- get filename () {
- webidl.brandCheck(this, ErrorEvent)
- return this.#eventInit.filename
- }
- get lineno () {
- webidl.brandCheck(this, ErrorEvent)
- return this.#eventInit.lineno
- }
- get colno () {
- webidl.brandCheck(this, ErrorEvent)
- return this.#eventInit.colno
- }
- get error () {
- webidl.brandCheck(this, ErrorEvent)
- return this.#eventInit.error
- }
- }
- Object.defineProperties(MessageEvent.prototype, {
- [Symbol.toStringTag]: {
- value: 'MessageEvent',
- configurable: true
- },
- data: kEnumerableProperty,
- origin: kEnumerableProperty,
- lastEventId: kEnumerableProperty,
- source: kEnumerableProperty,
- ports: kEnumerableProperty,
- initMessageEvent: kEnumerableProperty
- })
- Object.defineProperties(CloseEvent.prototype, {
- [Symbol.toStringTag]: {
- value: 'CloseEvent',
- configurable: true
- },
- reason: kEnumerableProperty,
- code: kEnumerableProperty,
- wasClean: kEnumerableProperty
- })
- Object.defineProperties(ErrorEvent.prototype, {
- [Symbol.toStringTag]: {
- value: 'ErrorEvent',
- configurable: true
- },
- message: kEnumerableProperty,
- filename: kEnumerableProperty,
- lineno: kEnumerableProperty,
- colno: kEnumerableProperty,
- error: kEnumerableProperty
- })
- webidl.converters.MessagePort = webidl.interfaceConverter(MessagePort)
- webidl.converters['sequence<MessagePort>'] = webidl.sequenceConverter(
- webidl.converters.MessagePort
- )
- const eventInit = [
- {
- key: 'bubbles',
- converter: webidl.converters.boolean,
- defaultValue: false
- },
- {
- key: 'cancelable',
- converter: webidl.converters.boolean,
- defaultValue: false
- },
- {
- key: 'composed',
- converter: webidl.converters.boolean,
- defaultValue: false
- }
- ]
- webidl.converters.MessageEventInit = webidl.dictionaryConverter([
- ...eventInit,
- {
- key: 'data',
- converter: webidl.converters.any,
- defaultValue: null
- },
- {
- key: 'origin',
- converter: webidl.converters.USVString,
- defaultValue: ''
- },
- {
- key: 'lastEventId',
- converter: webidl.converters.DOMString,
- defaultValue: ''
- },
- {
- key: 'source',
- // Node doesn't implement WindowProxy or ServiceWorker, so the only
- // valid value for source is a MessagePort.
- converter: webidl.nullableConverter(webidl.converters.MessagePort),
- defaultValue: null
- },
- {
- key: 'ports',
- converter: webidl.converters['sequence<MessagePort>'],
- get defaultValue () {
- return []
- }
- }
- ])
- webidl.converters.CloseEventInit = webidl.dictionaryConverter([
- ...eventInit,
- {
- key: 'wasClean',
- converter: webidl.converters.boolean,
- defaultValue: false
- },
- {
- key: 'code',
- converter: webidl.converters['unsigned short'],
- defaultValue: 0
- },
- {
- key: 'reason',
- converter: webidl.converters.USVString,
- defaultValue: ''
- }
- ])
- webidl.converters.ErrorEventInit = webidl.dictionaryConverter([
- ...eventInit,
- {
- key: 'message',
- converter: webidl.converters.DOMString,
- defaultValue: ''
- },
- {
- key: 'filename',
- converter: webidl.converters.USVString,
- defaultValue: ''
- },
- {
- key: 'lineno',
- converter: webidl.converters['unsigned long'],
- defaultValue: 0
- },
- {
- key: 'colno',
- converter: webidl.converters['unsigned long'],
- defaultValue: 0
- },
- {
- key: 'error',
- converter: webidl.converters.any
- }
- ])
- module.exports = {
- MessageEvent,
- CloseEvent,
- ErrorEvent
- }
|