websocketerror.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. 'use strict'
  2. const { webidl } = require('../../webidl')
  3. const { validateCloseCodeAndReason } = require('../util')
  4. const { kConstruct } = require('../../../core/symbols')
  5. const { kEnumerableProperty } = require('../../../core/util')
  6. function createInheritableDOMException () {
  7. // https://github.com/nodejs/node/issues/59677
  8. class Test extends DOMException {
  9. get reason () {
  10. return ''
  11. }
  12. }
  13. if (new Test().reason !== undefined) {
  14. return DOMException
  15. }
  16. return new Proxy(DOMException, {
  17. construct (target, args, newTarget) {
  18. const instance = Reflect.construct(target, args, target)
  19. Object.setPrototypeOf(instance, newTarget.prototype)
  20. return instance
  21. }
  22. })
  23. }
  24. class WebSocketError extends createInheritableDOMException() {
  25. #closeCode
  26. #reason
  27. constructor (message = '', init = undefined) {
  28. message = webidl.converters.DOMString(message, 'WebSocketError', 'message')
  29. // 1. Set this 's name to " WebSocketError ".
  30. // 2. Set this 's message to message .
  31. super(message, 'WebSocketError')
  32. if (init === kConstruct) {
  33. return
  34. } else if (init !== null) {
  35. init = webidl.converters.WebSocketCloseInfo(init)
  36. }
  37. // 3. Let code be init [" closeCode "] if it exists , or null otherwise.
  38. let code = init.closeCode ?? null
  39. // 4. Let reason be init [" reason "] if it exists , or the empty string otherwise.
  40. const reason = init.reason ?? ''
  41. // 5. Validate close code and reason with code and reason .
  42. validateCloseCodeAndReason(code, reason)
  43. // 6. If reason is non-empty, but code is not set, then set code to 1000 ("Normal Closure").
  44. if (reason.length !== 0 && code === null) {
  45. code = 1000
  46. }
  47. // 7. Set this 's closeCode to code .
  48. this.#closeCode = code
  49. // 8. Set this 's reason to reason .
  50. this.#reason = reason
  51. }
  52. get closeCode () {
  53. return this.#closeCode
  54. }
  55. get reason () {
  56. return this.#reason
  57. }
  58. /**
  59. * @param {string} message
  60. * @param {number|null} code
  61. * @param {string} reason
  62. */
  63. static createUnvalidatedWebSocketError (message, code, reason) {
  64. const error = new WebSocketError(message, kConstruct)
  65. error.#closeCode = code
  66. error.#reason = reason
  67. return error
  68. }
  69. }
  70. const { createUnvalidatedWebSocketError } = WebSocketError
  71. delete WebSocketError.createUnvalidatedWebSocketError
  72. Object.defineProperties(WebSocketError.prototype, {
  73. closeCode: kEnumerableProperty,
  74. reason: kEnumerableProperty,
  75. [Symbol.toStringTag]: {
  76. value: 'WebSocketError',
  77. writable: false,
  78. enumerable: false,
  79. configurable: true
  80. }
  81. })
  82. webidl.is.WebSocketError = webidl.util.MakeTypeAssertion(WebSocketError)
  83. module.exports = { WebSocketError, createUnvalidatedWebSocketError }