events.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. 'use strict'
  2. const { webidl } = require('../fetch/webidl')
  3. const { kEnumerableProperty } = require('../core/util')
  4. const { MessagePort } = require('worker_threads')
  5. /**
  6. * @see https://html.spec.whatwg.org/multipage/comms.html#messageevent
  7. */
  8. class MessageEvent extends Event {
  9. #eventInit
  10. constructor (type, eventInitDict = {}) {
  11. webidl.argumentLengthCheck(arguments, 1, { header: 'MessageEvent constructor' })
  12. type = webidl.converters.DOMString(type)
  13. eventInitDict = webidl.converters.MessageEventInit(eventInitDict)
  14. super(type, eventInitDict)
  15. this.#eventInit = eventInitDict
  16. }
  17. get data () {
  18. webidl.brandCheck(this, MessageEvent)
  19. return this.#eventInit.data
  20. }
  21. get origin () {
  22. webidl.brandCheck(this, MessageEvent)
  23. return this.#eventInit.origin
  24. }
  25. get lastEventId () {
  26. webidl.brandCheck(this, MessageEvent)
  27. return this.#eventInit.lastEventId
  28. }
  29. get source () {
  30. webidl.brandCheck(this, MessageEvent)
  31. return this.#eventInit.source
  32. }
  33. get ports () {
  34. webidl.brandCheck(this, MessageEvent)
  35. if (!Object.isFrozen(this.#eventInit.ports)) {
  36. Object.freeze(this.#eventInit.ports)
  37. }
  38. return this.#eventInit.ports
  39. }
  40. initMessageEvent (
  41. type,
  42. bubbles = false,
  43. cancelable = false,
  44. data = null,
  45. origin = '',
  46. lastEventId = '',
  47. source = null,
  48. ports = []
  49. ) {
  50. webidl.brandCheck(this, MessageEvent)
  51. webidl.argumentLengthCheck(arguments, 1, { header: 'MessageEvent.initMessageEvent' })
  52. return new MessageEvent(type, {
  53. bubbles, cancelable, data, origin, lastEventId, source, ports
  54. })
  55. }
  56. }
  57. /**
  58. * @see https://websockets.spec.whatwg.org/#the-closeevent-interface
  59. */
  60. class CloseEvent extends Event {
  61. #eventInit
  62. constructor (type, eventInitDict = {}) {
  63. webidl.argumentLengthCheck(arguments, 1, { header: 'CloseEvent constructor' })
  64. type = webidl.converters.DOMString(type)
  65. eventInitDict = webidl.converters.CloseEventInit(eventInitDict)
  66. super(type, eventInitDict)
  67. this.#eventInit = eventInitDict
  68. }
  69. get wasClean () {
  70. webidl.brandCheck(this, CloseEvent)
  71. return this.#eventInit.wasClean
  72. }
  73. get code () {
  74. webidl.brandCheck(this, CloseEvent)
  75. return this.#eventInit.code
  76. }
  77. get reason () {
  78. webidl.brandCheck(this, CloseEvent)
  79. return this.#eventInit.reason
  80. }
  81. }
  82. // https://html.spec.whatwg.org/multipage/webappapis.html#the-errorevent-interface
  83. class ErrorEvent extends Event {
  84. #eventInit
  85. constructor (type, eventInitDict) {
  86. webidl.argumentLengthCheck(arguments, 1, { header: 'ErrorEvent constructor' })
  87. super(type, eventInitDict)
  88. type = webidl.converters.DOMString(type)
  89. eventInitDict = webidl.converters.ErrorEventInit(eventInitDict ?? {})
  90. this.#eventInit = eventInitDict
  91. }
  92. get message () {
  93. webidl.brandCheck(this, ErrorEvent)
  94. return this.#eventInit.message
  95. }
  96. get filename () {
  97. webidl.brandCheck(this, ErrorEvent)
  98. return this.#eventInit.filename
  99. }
  100. get lineno () {
  101. webidl.brandCheck(this, ErrorEvent)
  102. return this.#eventInit.lineno
  103. }
  104. get colno () {
  105. webidl.brandCheck(this, ErrorEvent)
  106. return this.#eventInit.colno
  107. }
  108. get error () {
  109. webidl.brandCheck(this, ErrorEvent)
  110. return this.#eventInit.error
  111. }
  112. }
  113. Object.defineProperties(MessageEvent.prototype, {
  114. [Symbol.toStringTag]: {
  115. value: 'MessageEvent',
  116. configurable: true
  117. },
  118. data: kEnumerableProperty,
  119. origin: kEnumerableProperty,
  120. lastEventId: kEnumerableProperty,
  121. source: kEnumerableProperty,
  122. ports: kEnumerableProperty,
  123. initMessageEvent: kEnumerableProperty
  124. })
  125. Object.defineProperties(CloseEvent.prototype, {
  126. [Symbol.toStringTag]: {
  127. value: 'CloseEvent',
  128. configurable: true
  129. },
  130. reason: kEnumerableProperty,
  131. code: kEnumerableProperty,
  132. wasClean: kEnumerableProperty
  133. })
  134. Object.defineProperties(ErrorEvent.prototype, {
  135. [Symbol.toStringTag]: {
  136. value: 'ErrorEvent',
  137. configurable: true
  138. },
  139. message: kEnumerableProperty,
  140. filename: kEnumerableProperty,
  141. lineno: kEnumerableProperty,
  142. colno: kEnumerableProperty,
  143. error: kEnumerableProperty
  144. })
  145. webidl.converters.MessagePort = webidl.interfaceConverter(MessagePort)
  146. webidl.converters['sequence<MessagePort>'] = webidl.sequenceConverter(
  147. webidl.converters.MessagePort
  148. )
  149. const eventInit = [
  150. {
  151. key: 'bubbles',
  152. converter: webidl.converters.boolean,
  153. defaultValue: false
  154. },
  155. {
  156. key: 'cancelable',
  157. converter: webidl.converters.boolean,
  158. defaultValue: false
  159. },
  160. {
  161. key: 'composed',
  162. converter: webidl.converters.boolean,
  163. defaultValue: false
  164. }
  165. ]
  166. webidl.converters.MessageEventInit = webidl.dictionaryConverter([
  167. ...eventInit,
  168. {
  169. key: 'data',
  170. converter: webidl.converters.any,
  171. defaultValue: null
  172. },
  173. {
  174. key: 'origin',
  175. converter: webidl.converters.USVString,
  176. defaultValue: ''
  177. },
  178. {
  179. key: 'lastEventId',
  180. converter: webidl.converters.DOMString,
  181. defaultValue: ''
  182. },
  183. {
  184. key: 'source',
  185. // Node doesn't implement WindowProxy or ServiceWorker, so the only
  186. // valid value for source is a MessagePort.
  187. converter: webidl.nullableConverter(webidl.converters.MessagePort),
  188. defaultValue: null
  189. },
  190. {
  191. key: 'ports',
  192. converter: webidl.converters['sequence<MessagePort>'],
  193. get defaultValue () {
  194. return []
  195. }
  196. }
  197. ])
  198. webidl.converters.CloseEventInit = webidl.dictionaryConverter([
  199. ...eventInit,
  200. {
  201. key: 'wasClean',
  202. converter: webidl.converters.boolean,
  203. defaultValue: false
  204. },
  205. {
  206. key: 'code',
  207. converter: webidl.converters['unsigned short'],
  208. defaultValue: 0
  209. },
  210. {
  211. key: 'reason',
  212. converter: webidl.converters.USVString,
  213. defaultValue: ''
  214. }
  215. ])
  216. webidl.converters.ErrorEventInit = webidl.dictionaryConverter([
  217. ...eventInit,
  218. {
  219. key: 'message',
  220. converter: webidl.converters.DOMString,
  221. defaultValue: ''
  222. },
  223. {
  224. key: 'filename',
  225. converter: webidl.converters.USVString,
  226. defaultValue: ''
  227. },
  228. {
  229. key: 'lineno',
  230. converter: webidl.converters['unsigned long'],
  231. defaultValue: 0
  232. },
  233. {
  234. key: 'colno',
  235. converter: webidl.converters['unsigned long'],
  236. defaultValue: 0
  237. },
  238. {
  239. key: 'error',
  240. converter: webidl.converters.any
  241. }
  242. ])
  243. module.exports = {
  244. MessageEvent,
  245. CloseEvent,
  246. ErrorEvent
  247. }