voodoojs 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +77 -0
  2. package/dist/chunk-234ZLC6W.js +401 -0
  3. package/dist/chunk-4HQEOXTK.js +10271 -0
  4. package/dist/chunk-5777LJVW.js +64 -0
  5. package/dist/chunk-5CKGDARU.js +1845 -0
  6. package/dist/chunk-A2UOVQBP.js +82 -0
  7. package/dist/chunk-E27NRARW.js +16 -0
  8. package/dist/chunk-JZIYRIY6.js +1196 -0
  9. package/dist/chunk-NNU6WOOU.js +641 -0
  10. package/dist/chunk-PQZEVFVZ.js +448 -0
  11. package/dist/chunk-RJUNPXQF.js +946 -0
  12. package/dist/chunk-U76IRJKH.js +72 -0
  13. package/dist/essential.cjs +13889 -0
  14. package/dist/essential.d.cts +24 -0
  15. package/dist/essential.d.ts +24 -0
  16. package/dist/essential.js +51 -0
  17. package/dist/gpu.cjs +2008 -0
  18. package/dist/gpu.d.cts +68 -0
  19. package/dist/gpu.d.ts +68 -0
  20. package/dist/gpu.js +273 -0
  21. package/dist/http.cjs +467 -0
  22. package/dist/http.d.cts +148 -0
  23. package/dist/http.d.ts +148 -0
  24. package/dist/http.js +7 -0
  25. package/dist/index-CaLD-0oh.d.cts +608 -0
  26. package/dist/index-CaLD-0oh.d.ts +608 -0
  27. package/dist/index-DTllqUtj.d.cts +261 -0
  28. package/dist/index-DTllqUtj.d.ts +261 -0
  29. package/dist/index.cjs +23063 -0
  30. package/dist/index.d.cts +1603 -0
  31. package/dist/index.d.ts +1603 -0
  32. package/dist/index.js +6924 -0
  33. package/dist/query-CKJ4oSpG.d.cts +1595 -0
  34. package/dist/query-DQFRmu3u.d.ts +1595 -0
  35. package/dist/reactivity.cjs +676 -0
  36. package/dist/reactivity.d.cts +188 -0
  37. package/dist/reactivity.d.ts +188 -0
  38. package/dist/reactivity.js +4 -0
  39. package/dist/socket.cjs +2685 -0
  40. package/dist/socket.d.cts +167 -0
  41. package/dist/socket.d.ts +167 -0
  42. package/dist/socket.js +238 -0
  43. package/dist/style-XEUAGGJK.js +5 -0
  44. package/dist/utils.cjs +397 -0
  45. package/dist/utils.d.cts +111 -0
  46. package/dist/utils.d.ts +111 -0
  47. package/dist/utils.js +4 -0
  48. package/dist/voodoo.core.js +8213 -0
  49. package/dist/voodoo.core.min.js +146 -0
  50. package/dist/voodoo.full.js +21193 -0
  51. package/dist/voodoo.full.min.js +1784 -0
  52. package/dist/voodoo.js +14185 -0
  53. package/dist/voodoo.min.js +420 -0
  54. package/package.json +127 -0
@@ -0,0 +1,261 @@
1
+ /**
2
+ * @module socket
3
+ *
4
+ * Real-time with the same ergonomics as the `http` module: patterns in one place,
5
+ * interceptors, reconnection with progressive backoff, and no dependencies.
6
+ * Where `http` has `retry`, here there's `reconnect`; where it has
7
+ * `interceptors.request/response`, here there's `interceptors.outgoing/incoming`.
8
+ *
9
+ * ```js
10
+ * const s = V.socket('wss://exemplo.com') // native WebSocket
11
+ * const chat = V.socket('/', { transport: 'socket.io' }) // Socket.IO protocol
12
+ *
13
+ * chat.on('mensagem', (dados) => console.log(dados))
14
+ * chat.emit('entrar', { sala: 'geral' })
15
+ * chat.state // reactive
16
+ * chat.connected // reactive
17
+ * chat.close()
18
+ * V.socket.close() // closes all
19
+ * ```
20
+ *
21
+ * ## Two conventions that need to be clear
22
+ *
23
+ * 1. **Events over native WebSocket.** Raw WebSocket has no concept of
24
+ * named events: it carries text. For `emit`/`on` to work on both
25
+ * transports, the native transport uses a JSON envelope,
26
+ * `{"event":"nome","data":...}`, going and coming back. Those talking to a server
27
+ * that doesn't use this format have `send()` and `on('message')`, which pass
28
+ * raw content without interpreting any event name.
29
+ * 2. **Heartbeat on native transport.** The `readyState` lies: when the network drops
30
+ * without FIN, it keeps saying `OPEN` for minutes. So the module sends a
31
+ * `ping` from time to time and tears down the connection when nothing comes back. The text
32
+ * of the ping is configurable and `heartbeat: 0` disables it all. In the
33
+ * Socket.IO transport none of this is used: the server sends the ping, and the
34
+ * protocol already defines the timings.
35
+ */
36
+ /** Possible connection states, in natural cycle order. */
37
+ type SocketState = 'connecting' | 'open' | 'closing' | 'closed' | 'reconnecting';
38
+ type SocketTransport = 'ws' | 'socket.io';
39
+ /** Room lifecycle, from join request to exit. */
40
+ type RoomState = 'joining' | 'joined' | 'left';
41
+ /**
42
+ * Event listener. The second parameter only appears when the server requested
43
+ * acknowledgment (ack) for that event, and calling it sends the response to the server.
44
+ */
45
+ type SocketListener = (data: unknown, ack?: (resposta: unknown) => void) => void;
46
+ /** A message coming in or going out, in the format seen by interceptors. */
47
+ interface SocketMessage {
48
+ /** Event name. `message` when the message carries no name. */
49
+ event: string;
50
+ /** Message payload. */
51
+ data: unknown;
52
+ /** Socket address, to identify the connection within the interceptor. */
53
+ url: string;
54
+ /** Text exactly as it came from the wire. Absent in outgoing messages. */
55
+ raw?: string;
56
+ }
57
+ /**
58
+ * Message interceptor. Returning an object modifies the message, returning
59
+ * `null` discards it, and returning nothing keeps the original.
60
+ */
61
+ type SocketInterceptor = (message: SocketMessage) => SocketMessage | null | void;
62
+ /**
63
+ * Minimum of what the module needs from a WebSocket. Exists so a test double
64
+ * can take the place of the native one via `socket.defaults.WebSocket`, without
65
+ * needing any network.
66
+ */
67
+ interface WebSocketLike {
68
+ readyState: number;
69
+ send(data: string): void;
70
+ close(code?: number, reason?: string): void;
71
+ onopen: ((event?: unknown) => void) | null;
72
+ onclose: ((event?: unknown) => void) | null;
73
+ onerror: ((event?: unknown) => void) | null;
74
+ onmessage: ((event: {
75
+ data: unknown;
76
+ }) => void) | null;
77
+ }
78
+ type WebSocketCtor = new (url: string, protocols?: string | string[]) => WebSocketLike;
79
+ interface SocketOptions {
80
+ /** `ws` uses pure WebSocket. `socket.io` uses the Engine.IO/Socket.IO protocol. */
81
+ transport?: SocketTransport;
82
+ /** Handshake subprotocols, passed to the native constructor. */
83
+ protocols?: string | string[];
84
+ /** Auto-reconnects when connection drops without being closed by `close()`. */
85
+ reconnect?: boolean;
86
+ /** Initial wait before reconnecting, in ms. Doubles with each attempt. */
87
+ reconnectDelay?: number;
88
+ /** Maximum wait between attempts, in ms. */
89
+ reconnectMaxDelay?: number;
90
+ /** Maximum number of attempts. `Infinity` retries forever. */
91
+ reconnectMaxAttempts?: number;
92
+ /** Fraction from 0 to 1 randomized on top of the wait, to prevent client sync. */
93
+ jitter?: number;
94
+ /** Interval between pings on native transport, in ms. `0` disables. */
95
+ heartbeat?: number;
96
+ /** Time without response before considering the connection dead, in ms. */
97
+ heartbeatTimeout?: number;
98
+ /** Text sent as ping. `null` observes silence without sending anything. */
99
+ pingPayload?: string | null;
100
+ /** Text the server returns as pong, ignored in message delivery. */
101
+ pongPayload?: string | null;
102
+ /** How many messages are buffered while the connection isn't open. */
103
+ queueLimit?: number;
104
+ /** Auto-converts JSON, falling back to plain text on parse failure. */
105
+ json?: boolean;
106
+ /** Path of the Engine.IO endpoint. Only for `transport: 'socket.io'`. */
107
+ path?: string;
108
+ /** Socket.IO namespace. This implementation only supports the default. */
109
+ namespace?: string;
110
+ /** Authentication data sent in the Socket.IO CONNECT packet. */
111
+ auth?: Record<string, unknown> | null;
112
+ /** WebSocket implementation used instead of the global one. */
113
+ WebSocket?: WebSocketCtor | null;
114
+ /** Creates the connection closed. `open()` is what opens it. */
115
+ manual?: boolean;
116
+ /** Event sent to the server to request joining a room. */
117
+ joinEvent?: string;
118
+ /** Event sent to the server to request leaving a room. */
119
+ leaveEvent?: string;
120
+ /** Event where the server sends the complete member list for a room. */
121
+ presenceEvent?: string;
122
+ /** Event where the server announces someone joined. */
123
+ memberJoinEvent?: string;
124
+ /** Event where the server announces someone left. */
125
+ memberLeaveEvent?: string;
126
+ /** How many messages each room buffers in `messages`. */
127
+ roomBuffer?: number;
128
+ }
129
+ /** Configuration of a specific room. */
130
+ interface RoomOptions {
131
+ /**
132
+ * Marks the room as private. This is a request, not a guarantee: the server always
133
+ * decides who enters and what's transmitted. See `docs/websocket.md`.
134
+ */
135
+ privada?: boolean;
136
+ /** Same as `privada`, for those writing the API in English. */
137
+ private?: boolean;
138
+ /** How many messages this room buffers. Default `defaults.roomBuffer`. */
139
+ buffer?: number;
140
+ }
141
+ /**
142
+ * A room (or channel) within a connection.
143
+ *
144
+ * Names exist in Portuguese and English because the directive exposes `$room` in
145
+ * Portuguese in HTML and the programmatic API follows the English of the rest of the module.
146
+ */
147
+ interface SocketRoom {
148
+ /** Room name, as requested from the server. */
149
+ readonly name: string;
150
+ /** `true` when the room was requested as private. */
151
+ readonly private: boolean;
152
+ readonly privada: boolean;
153
+ /** Reactive state of the room. */
154
+ readonly state: RoomState;
155
+ readonly estado: RoomState;
156
+ /** Who the server says is in the room. Empty if it sends nothing. */
157
+ readonly members: unknown[];
158
+ readonly membros: unknown[];
159
+ /** Latest messages received in the room, up to the buffer limit. */
160
+ readonly messages: unknown[];
161
+ readonly mensagens: unknown[];
162
+ /** Listen to an event in this room. Returns the function that cancels it. */
163
+ on(event: string, listener: SocketListener): () => void;
164
+ /** Cancel a listener, all of an event, or all of the room. */
165
+ off(event?: string, listener?: SocketListener): void;
166
+ /** Send to all members of the room. */
167
+ emit(event: string, data?: unknown): boolean;
168
+ enviar(event: string, data?: unknown): boolean;
169
+ /** Send only to a recipient within this room. */
170
+ to(destino: string): {
171
+ emit(event: string, data?: unknown): boolean;
172
+ };
173
+ /** Leave the room, clear listeners, and stop rejoining on reconnect. */
174
+ leave(): void;
175
+ sair(): void;
176
+ }
177
+ /** Real-time connection returned by `V.socket()`. */
178
+ interface VoodooSocket {
179
+ /** Final address, already resolved with the transport endpoint. */
180
+ readonly url: string;
181
+ /** Reactive connection state. */
182
+ readonly state: SocketState;
183
+ /** `true` while the connection is open. Reactive. */
184
+ readonly connected: boolean;
185
+ /** How many consecutive reconnection attempts have failed. Reactive. */
186
+ readonly attempts: number;
187
+ /** How many messages are waiting for the connection to open. Reactive. */
188
+ readonly queued: number;
189
+ /** Last error, already as text. Reactive. */
190
+ readonly error: string | null;
191
+ /** WebSocket in use, for advanced cases. `null` while closed. */
192
+ readonly raw: WebSocketLike | null;
193
+ /** Listen to an event. Returns the function that cancels the subscription. */
194
+ on(event: string, listener: SocketListener): () => void;
195
+ /** Listen only to the next occurrence. */
196
+ once(event: string, listener: SocketListener): () => void;
197
+ /** Cancel a listener, all of an event, or all listeners. */
198
+ off(event?: string, listener?: SocketListener): void;
199
+ /** Send a named event. Before opening, enters the queue. */
200
+ emit(event: string, data?: unknown, ack?: (resposta: unknown) => void): boolean;
201
+ /** Send raw payload, without event name. */
202
+ send(data: unknown): boolean;
203
+ /** Open the connection. Used by `manual` and after `close()`. */
204
+ open(): void;
205
+ /** Close intentionally: doesn't reconnect, clears timers, and empties the queue. */
206
+ close(code?: number, reason?: string): void;
207
+ /** Join a room. Calling twice with the same name returns the same room. */
208
+ join(name: string, options?: RoomOptions): SocketRoom;
209
+ /** Leave a room by name. */
210
+ leave(name: string): void;
211
+ /** Rooms this connection is in or joining. */
212
+ readonly rooms: SocketRoom[];
213
+ /** Send directly to a recipient, outside any room. */
214
+ to(destino: string): {
215
+ emit(event: string, data?: unknown): boolean;
216
+ };
217
+ }
218
+ interface SocketDefaults extends Required<Omit<SocketOptions, 'protocols' | 'auth' | 'WebSocket'>> {
219
+ /** Prefix applied to relative addresses, like the `baseURL` of http. */
220
+ baseURL: string;
221
+ auth: Record<string, unknown> | null;
222
+ WebSocket: WebSocketCtor | null;
223
+ }
224
+ /** Resolves `/chat` to `ws://host/chat` and `https://x` to `wss://x`. */
225
+ declare function resolveSocketURL(url: string, baseURL?: string): string;
226
+ /** `true` when some WebSocket implementation is available. */
227
+ declare function socketSupported(): boolean;
228
+ /**
229
+ * Creates a real-time connection.
230
+ *
231
+ * Without WebSocket in the environment (SSR, or a jsdom without the API), nothing is thrown: returns
232
+ * an inert socket, permanently `closed`, with `error` filled. A page
233
+ * rendered on the server cannot break because of a `v-socket`.
234
+ */
235
+ declare function createSocket(url: string, options?: SocketOptions): VoodooSocket;
236
+ interface SocketFactory {
237
+ (url: string, options?: SocketOptions): VoodooSocket;
238
+ /** Defaults applied to every new connection, like `http.defaults`. */
239
+ defaults: SocketDefaults;
240
+ /** Message interceptors, in the format of `http.interceptors`. */
241
+ interceptors: {
242
+ incoming: {
243
+ use(fn: SocketInterceptor): () => void;
244
+ };
245
+ outgoing: {
246
+ use(fn: SocketInterceptor): () => void;
247
+ };
248
+ };
249
+ /** Closes all open connections. */
250
+ close(): void;
251
+ /** Active connections right now. */
252
+ readonly open: VoodooSocket[];
253
+ /** `true` when the environment has WebSocket. */
254
+ supported(): boolean;
255
+ /** Switches the WebSocket implementation used by default. Useful in testing. */
256
+ setWebSocket(impl: WebSocketCtor | null): void;
257
+ }
258
+ declare const socket: SocketFactory;
259
+ type Socket = typeof socket;
260
+
261
+ export { type RoomOptions as R, type SocketMessage as S, type VoodooSocket as V, type WebSocketCtor as W, type RoomState as a, type SocketOptions as b, type SocketRoom as c, type SocketState as d, type SocketTransport as e, createSocket as f, socketSupported as g, type Socket as h, type SocketDefaults as i, type SocketFactory as j, type SocketInterceptor as k, type SocketListener as l, type WebSocketLike as m, resolveSocketURL as r, socket as s };
@@ -0,0 +1,261 @@
1
+ /**
2
+ * @module socket
3
+ *
4
+ * Real-time with the same ergonomics as the `http` module: patterns in one place,
5
+ * interceptors, reconnection with progressive backoff, and no dependencies.
6
+ * Where `http` has `retry`, here there's `reconnect`; where it has
7
+ * `interceptors.request/response`, here there's `interceptors.outgoing/incoming`.
8
+ *
9
+ * ```js
10
+ * const s = V.socket('wss://exemplo.com') // native WebSocket
11
+ * const chat = V.socket('/', { transport: 'socket.io' }) // Socket.IO protocol
12
+ *
13
+ * chat.on('mensagem', (dados) => console.log(dados))
14
+ * chat.emit('entrar', { sala: 'geral' })
15
+ * chat.state // reactive
16
+ * chat.connected // reactive
17
+ * chat.close()
18
+ * V.socket.close() // closes all
19
+ * ```
20
+ *
21
+ * ## Two conventions that need to be clear
22
+ *
23
+ * 1. **Events over native WebSocket.** Raw WebSocket has no concept of
24
+ * named events: it carries text. For `emit`/`on` to work on both
25
+ * transports, the native transport uses a JSON envelope,
26
+ * `{"event":"nome","data":...}`, going and coming back. Those talking to a server
27
+ * that doesn't use this format have `send()` and `on('message')`, which pass
28
+ * raw content without interpreting any event name.
29
+ * 2. **Heartbeat on native transport.** The `readyState` lies: when the network drops
30
+ * without FIN, it keeps saying `OPEN` for minutes. So the module sends a
31
+ * `ping` from time to time and tears down the connection when nothing comes back. The text
32
+ * of the ping is configurable and `heartbeat: 0` disables it all. In the
33
+ * Socket.IO transport none of this is used: the server sends the ping, and the
34
+ * protocol already defines the timings.
35
+ */
36
+ /** Possible connection states, in natural cycle order. */
37
+ type SocketState = 'connecting' | 'open' | 'closing' | 'closed' | 'reconnecting';
38
+ type SocketTransport = 'ws' | 'socket.io';
39
+ /** Room lifecycle, from join request to exit. */
40
+ type RoomState = 'joining' | 'joined' | 'left';
41
+ /**
42
+ * Event listener. The second parameter only appears when the server requested
43
+ * acknowledgment (ack) for that event, and calling it sends the response to the server.
44
+ */
45
+ type SocketListener = (data: unknown, ack?: (resposta: unknown) => void) => void;
46
+ /** A message coming in or going out, in the format seen by interceptors. */
47
+ interface SocketMessage {
48
+ /** Event name. `message` when the message carries no name. */
49
+ event: string;
50
+ /** Message payload. */
51
+ data: unknown;
52
+ /** Socket address, to identify the connection within the interceptor. */
53
+ url: string;
54
+ /** Text exactly as it came from the wire. Absent in outgoing messages. */
55
+ raw?: string;
56
+ }
57
+ /**
58
+ * Message interceptor. Returning an object modifies the message, returning
59
+ * `null` discards it, and returning nothing keeps the original.
60
+ */
61
+ type SocketInterceptor = (message: SocketMessage) => SocketMessage | null | void;
62
+ /**
63
+ * Minimum of what the module needs from a WebSocket. Exists so a test double
64
+ * can take the place of the native one via `socket.defaults.WebSocket`, without
65
+ * needing any network.
66
+ */
67
+ interface WebSocketLike {
68
+ readyState: number;
69
+ send(data: string): void;
70
+ close(code?: number, reason?: string): void;
71
+ onopen: ((event?: unknown) => void) | null;
72
+ onclose: ((event?: unknown) => void) | null;
73
+ onerror: ((event?: unknown) => void) | null;
74
+ onmessage: ((event: {
75
+ data: unknown;
76
+ }) => void) | null;
77
+ }
78
+ type WebSocketCtor = new (url: string, protocols?: string | string[]) => WebSocketLike;
79
+ interface SocketOptions {
80
+ /** `ws` uses pure WebSocket. `socket.io` uses the Engine.IO/Socket.IO protocol. */
81
+ transport?: SocketTransport;
82
+ /** Handshake subprotocols, passed to the native constructor. */
83
+ protocols?: string | string[];
84
+ /** Auto-reconnects when connection drops without being closed by `close()`. */
85
+ reconnect?: boolean;
86
+ /** Initial wait before reconnecting, in ms. Doubles with each attempt. */
87
+ reconnectDelay?: number;
88
+ /** Maximum wait between attempts, in ms. */
89
+ reconnectMaxDelay?: number;
90
+ /** Maximum number of attempts. `Infinity` retries forever. */
91
+ reconnectMaxAttempts?: number;
92
+ /** Fraction from 0 to 1 randomized on top of the wait, to prevent client sync. */
93
+ jitter?: number;
94
+ /** Interval between pings on native transport, in ms. `0` disables. */
95
+ heartbeat?: number;
96
+ /** Time without response before considering the connection dead, in ms. */
97
+ heartbeatTimeout?: number;
98
+ /** Text sent as ping. `null` observes silence without sending anything. */
99
+ pingPayload?: string | null;
100
+ /** Text the server returns as pong, ignored in message delivery. */
101
+ pongPayload?: string | null;
102
+ /** How many messages are buffered while the connection isn't open. */
103
+ queueLimit?: number;
104
+ /** Auto-converts JSON, falling back to plain text on parse failure. */
105
+ json?: boolean;
106
+ /** Path of the Engine.IO endpoint. Only for `transport: 'socket.io'`. */
107
+ path?: string;
108
+ /** Socket.IO namespace. This implementation only supports the default. */
109
+ namespace?: string;
110
+ /** Authentication data sent in the Socket.IO CONNECT packet. */
111
+ auth?: Record<string, unknown> | null;
112
+ /** WebSocket implementation used instead of the global one. */
113
+ WebSocket?: WebSocketCtor | null;
114
+ /** Creates the connection closed. `open()` is what opens it. */
115
+ manual?: boolean;
116
+ /** Event sent to the server to request joining a room. */
117
+ joinEvent?: string;
118
+ /** Event sent to the server to request leaving a room. */
119
+ leaveEvent?: string;
120
+ /** Event where the server sends the complete member list for a room. */
121
+ presenceEvent?: string;
122
+ /** Event where the server announces someone joined. */
123
+ memberJoinEvent?: string;
124
+ /** Event where the server announces someone left. */
125
+ memberLeaveEvent?: string;
126
+ /** How many messages each room buffers in `messages`. */
127
+ roomBuffer?: number;
128
+ }
129
+ /** Configuration of a specific room. */
130
+ interface RoomOptions {
131
+ /**
132
+ * Marks the room as private. This is a request, not a guarantee: the server always
133
+ * decides who enters and what's transmitted. See `docs/websocket.md`.
134
+ */
135
+ privada?: boolean;
136
+ /** Same as `privada`, for those writing the API in English. */
137
+ private?: boolean;
138
+ /** How many messages this room buffers. Default `defaults.roomBuffer`. */
139
+ buffer?: number;
140
+ }
141
+ /**
142
+ * A room (or channel) within a connection.
143
+ *
144
+ * Names exist in Portuguese and English because the directive exposes `$room` in
145
+ * Portuguese in HTML and the programmatic API follows the English of the rest of the module.
146
+ */
147
+ interface SocketRoom {
148
+ /** Room name, as requested from the server. */
149
+ readonly name: string;
150
+ /** `true` when the room was requested as private. */
151
+ readonly private: boolean;
152
+ readonly privada: boolean;
153
+ /** Reactive state of the room. */
154
+ readonly state: RoomState;
155
+ readonly estado: RoomState;
156
+ /** Who the server says is in the room. Empty if it sends nothing. */
157
+ readonly members: unknown[];
158
+ readonly membros: unknown[];
159
+ /** Latest messages received in the room, up to the buffer limit. */
160
+ readonly messages: unknown[];
161
+ readonly mensagens: unknown[];
162
+ /** Listen to an event in this room. Returns the function that cancels it. */
163
+ on(event: string, listener: SocketListener): () => void;
164
+ /** Cancel a listener, all of an event, or all of the room. */
165
+ off(event?: string, listener?: SocketListener): void;
166
+ /** Send to all members of the room. */
167
+ emit(event: string, data?: unknown): boolean;
168
+ enviar(event: string, data?: unknown): boolean;
169
+ /** Send only to a recipient within this room. */
170
+ to(destino: string): {
171
+ emit(event: string, data?: unknown): boolean;
172
+ };
173
+ /** Leave the room, clear listeners, and stop rejoining on reconnect. */
174
+ leave(): void;
175
+ sair(): void;
176
+ }
177
+ /** Real-time connection returned by `V.socket()`. */
178
+ interface VoodooSocket {
179
+ /** Final address, already resolved with the transport endpoint. */
180
+ readonly url: string;
181
+ /** Reactive connection state. */
182
+ readonly state: SocketState;
183
+ /** `true` while the connection is open. Reactive. */
184
+ readonly connected: boolean;
185
+ /** How many consecutive reconnection attempts have failed. Reactive. */
186
+ readonly attempts: number;
187
+ /** How many messages are waiting for the connection to open. Reactive. */
188
+ readonly queued: number;
189
+ /** Last error, already as text. Reactive. */
190
+ readonly error: string | null;
191
+ /** WebSocket in use, for advanced cases. `null` while closed. */
192
+ readonly raw: WebSocketLike | null;
193
+ /** Listen to an event. Returns the function that cancels the subscription. */
194
+ on(event: string, listener: SocketListener): () => void;
195
+ /** Listen only to the next occurrence. */
196
+ once(event: string, listener: SocketListener): () => void;
197
+ /** Cancel a listener, all of an event, or all listeners. */
198
+ off(event?: string, listener?: SocketListener): void;
199
+ /** Send a named event. Before opening, enters the queue. */
200
+ emit(event: string, data?: unknown, ack?: (resposta: unknown) => void): boolean;
201
+ /** Send raw payload, without event name. */
202
+ send(data: unknown): boolean;
203
+ /** Open the connection. Used by `manual` and after `close()`. */
204
+ open(): void;
205
+ /** Close intentionally: doesn't reconnect, clears timers, and empties the queue. */
206
+ close(code?: number, reason?: string): void;
207
+ /** Join a room. Calling twice with the same name returns the same room. */
208
+ join(name: string, options?: RoomOptions): SocketRoom;
209
+ /** Leave a room by name. */
210
+ leave(name: string): void;
211
+ /** Rooms this connection is in or joining. */
212
+ readonly rooms: SocketRoom[];
213
+ /** Send directly to a recipient, outside any room. */
214
+ to(destino: string): {
215
+ emit(event: string, data?: unknown): boolean;
216
+ };
217
+ }
218
+ interface SocketDefaults extends Required<Omit<SocketOptions, 'protocols' | 'auth' | 'WebSocket'>> {
219
+ /** Prefix applied to relative addresses, like the `baseURL` of http. */
220
+ baseURL: string;
221
+ auth: Record<string, unknown> | null;
222
+ WebSocket: WebSocketCtor | null;
223
+ }
224
+ /** Resolves `/chat` to `ws://host/chat` and `https://x` to `wss://x`. */
225
+ declare function resolveSocketURL(url: string, baseURL?: string): string;
226
+ /** `true` when some WebSocket implementation is available. */
227
+ declare function socketSupported(): boolean;
228
+ /**
229
+ * Creates a real-time connection.
230
+ *
231
+ * Without WebSocket in the environment (SSR, or a jsdom without the API), nothing is thrown: returns
232
+ * an inert socket, permanently `closed`, with `error` filled. A page
233
+ * rendered on the server cannot break because of a `v-socket`.
234
+ */
235
+ declare function createSocket(url: string, options?: SocketOptions): VoodooSocket;
236
+ interface SocketFactory {
237
+ (url: string, options?: SocketOptions): VoodooSocket;
238
+ /** Defaults applied to every new connection, like `http.defaults`. */
239
+ defaults: SocketDefaults;
240
+ /** Message interceptors, in the format of `http.interceptors`. */
241
+ interceptors: {
242
+ incoming: {
243
+ use(fn: SocketInterceptor): () => void;
244
+ };
245
+ outgoing: {
246
+ use(fn: SocketInterceptor): () => void;
247
+ };
248
+ };
249
+ /** Closes all open connections. */
250
+ close(): void;
251
+ /** Active connections right now. */
252
+ readonly open: VoodooSocket[];
253
+ /** `true` when the environment has WebSocket. */
254
+ supported(): boolean;
255
+ /** Switches the WebSocket implementation used by default. Useful in testing. */
256
+ setWebSocket(impl: WebSocketCtor | null): void;
257
+ }
258
+ declare const socket: SocketFactory;
259
+ type Socket = typeof socket;
260
+
261
+ export { type RoomOptions as R, type SocketMessage as S, type VoodooSocket as V, type WebSocketCtor as W, type RoomState as a, type SocketOptions as b, type SocketRoom as c, type SocketState as d, type SocketTransport as e, createSocket as f, socketSupported as g, type Socket as h, type SocketDefaults as i, type SocketFactory as j, type SocketInterceptor as k, type SocketListener as l, type WebSocketLike as m, resolveSocketURL as r, socket as s };