websocket.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /// <reference types="node" />
  2. import type { Blob } from 'buffer'
  3. import type { MessagePort } from 'worker_threads'
  4. import {
  5. EventTarget,
  6. Event,
  7. EventInit,
  8. EventListenerOptions,
  9. AddEventListenerOptions,
  10. EventListenerOrEventListenerObject
  11. } from './patch'
  12. import Dispatcher from './dispatcher'
  13. import { HeadersInit } from './fetch'
  14. export type BinaryType = 'blob' | 'arraybuffer'
  15. interface WebSocketEventMap {
  16. close: CloseEvent
  17. error: Event
  18. message: MessageEvent
  19. open: Event
  20. }
  21. interface WebSocket extends EventTarget {
  22. binaryType: BinaryType
  23. readonly bufferedAmount: number
  24. readonly extensions: string
  25. onclose: ((this: WebSocket, ev: WebSocketEventMap['close']) => any) | null
  26. onerror: ((this: WebSocket, ev: WebSocketEventMap['error']) => any) | null
  27. onmessage: ((this: WebSocket, ev: WebSocketEventMap['message']) => any) | null
  28. onopen: ((this: WebSocket, ev: WebSocketEventMap['open']) => any) | null
  29. readonly protocol: string
  30. readonly readyState: number
  31. readonly url: string
  32. close(code?: number, reason?: string): void
  33. send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void
  34. readonly CLOSED: number
  35. readonly CLOSING: number
  36. readonly CONNECTING: number
  37. readonly OPEN: number
  38. addEventListener<K extends keyof WebSocketEventMap>(
  39. type: K,
  40. listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
  41. options?: boolean | AddEventListenerOptions
  42. ): void
  43. addEventListener(
  44. type: string,
  45. listener: EventListenerOrEventListenerObject,
  46. options?: boolean | AddEventListenerOptions
  47. ): void
  48. removeEventListener<K extends keyof WebSocketEventMap>(
  49. type: K,
  50. listener: (this: WebSocket, ev: WebSocketEventMap[K]) => any,
  51. options?: boolean | EventListenerOptions
  52. ): void
  53. removeEventListener(
  54. type: string,
  55. listener: EventListenerOrEventListenerObject,
  56. options?: boolean | EventListenerOptions
  57. ): void
  58. }
  59. export declare const WebSocket: {
  60. prototype: WebSocket
  61. new (url: string | URL, protocols?: string | string[] | WebSocketInit): WebSocket
  62. readonly CLOSED: number
  63. readonly CLOSING: number
  64. readonly CONNECTING: number
  65. readonly OPEN: number
  66. }
  67. interface CloseEventInit extends EventInit {
  68. code?: number
  69. reason?: string
  70. wasClean?: boolean
  71. }
  72. interface CloseEvent extends Event {
  73. readonly code: number
  74. readonly reason: string
  75. readonly wasClean: boolean
  76. }
  77. export declare const CloseEvent: {
  78. prototype: CloseEvent
  79. new (type: string, eventInitDict?: CloseEventInit): CloseEvent
  80. }
  81. interface MessageEventInit<T = any> extends EventInit {
  82. data?: T
  83. lastEventId?: string
  84. origin?: string
  85. ports?: (typeof MessagePort)[]
  86. source?: typeof MessagePort | null
  87. }
  88. interface MessageEvent<T = any> extends Event {
  89. readonly data: T
  90. readonly lastEventId: string
  91. readonly origin: string
  92. readonly ports: ReadonlyArray<typeof MessagePort>
  93. readonly source: typeof MessagePort | null
  94. initMessageEvent(
  95. type: string,
  96. bubbles?: boolean,
  97. cancelable?: boolean,
  98. data?: any,
  99. origin?: string,
  100. lastEventId?: string,
  101. source?: typeof MessagePort | null,
  102. ports?: (typeof MessagePort)[]
  103. ): void;
  104. }
  105. export declare const MessageEvent: {
  106. prototype: MessageEvent
  107. new<T>(type: string, eventInitDict?: MessageEventInit<T>): MessageEvent<T>
  108. }
  109. interface WebSocketInit {
  110. protocols?: string | string[],
  111. dispatcher?: Dispatcher,
  112. headers?: HeadersInit
  113. }