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