iosignal 3.0.0 → 3.1.0

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 (57) hide show
  1. package/README.ko.md +566 -0
  2. package/README.md +561 -2
  3. package/dist/io.d.ts +481 -0
  4. package/dist/io.js +3 -3
  5. package/dist/io.js.map +1 -1
  6. package/dist/io.min.js +3 -3
  7. package/dist/io.min.js.map +1 -1
  8. package/dist/iosignal.js +558 -104
  9. package/dist/types/client/IOCore.d.ts +408 -0
  10. package/dist/types/client/browser/IOWebSocket.d.ts +75 -0
  11. package/dist/types/common/constants.d.ts +169 -0
  12. package/dist/types/common/payload.d.ts +6 -0
  13. package/dist/types/common/quotaTable.d.ts +47 -0
  14. package/examples/react-chat-js/dist/assets/index-Cy4qaVcJ.js +56 -0
  15. package/examples/react-chat-js/dist/assets/index-NETrjYhz.css +1 -0
  16. package/examples/react-chat-js/dist/index.html +14 -0
  17. package/examples/react-chat-js/index.html +13 -0
  18. package/examples/react-chat-js/package-lock.json +1723 -0
  19. package/examples/react-chat-js/package.json +20 -0
  20. package/examples/react-chat-js/src/App.css +61 -0
  21. package/examples/react-chat-js/src/App.jsx +125 -0
  22. package/examples/react-chat-js/src/index.css +13 -0
  23. package/examples/react-chat-js/src/main.jsx +13 -0
  24. package/examples/react-chat-js/src/shared_io.js +12 -0
  25. package/examples/react-chat-js/vite.config.js +7 -0
  26. package/examples/server/index.js +12 -0
  27. package/examples/server/package-lock.json +47 -0
  28. package/examples/server/package.json +12 -0
  29. package/examples/svelte-chat-js/.devtools +3 -0
  30. package/examples/svelte-chat-js/README.md +38 -0
  31. package/examples/svelte-chat-js/package-lock.json +1505 -0
  32. package/examples/svelte-chat-js/package.json +22 -0
  33. package/examples/svelte-chat-js/src/app.css +105 -0
  34. package/examples/svelte-chat-js/src/app.d.ts +13 -0
  35. package/examples/svelte-chat-js/src/app.html +12 -0
  36. package/examples/svelte-chat-js/src/lib/images/github.svg +1 -0
  37. package/examples/svelte-chat-js/src/lib/images/svelte-logo.svg +1 -0
  38. package/examples/svelte-chat-js/src/lib/images/svelte-welcome.png +0 -0
  39. package/examples/svelte-chat-js/src/lib/images/svelte-welcome.webp +0 -0
  40. package/examples/svelte-chat-js/src/lib/shared_io.js +12 -0
  41. package/examples/svelte-chat-js/src/routes/+layout.svelte +9 -0
  42. package/examples/svelte-chat-js/src/routes/+page.js +3 -0
  43. package/examples/svelte-chat-js/src/routes/+page.svelte +162 -0
  44. package/examples/svelte-chat-js/static/favicon.svg +1 -0
  45. package/examples/svelte-chat-js/static/global.css +19 -0
  46. package/examples/svelte-chat-js/static/robots.txt +3 -0
  47. package/examples/svelte-chat-js/svelte.config.js +15 -0
  48. package/examples/svelte-chat-js/vite.config.js +6 -0
  49. package/package.json +9 -3
  50. package/rollup.config.js +11 -4
  51. package/src/client/IOCongSocket.js +20 -9
  52. package/src/client/IOCore.js +437 -79
  53. package/src/client/IOWS.js +12 -15
  54. package/src/client/browser/IOWebSocket.js +101 -17
  55. package/src/common/constants.js +91 -1
  56. package/tsconfig.json +24 -0
  57. package/docs/README.kr.md +0 -230
package/dist/io.d.ts ADDED
@@ -0,0 +1,481 @@
1
+ import * as boho from 'boho';
2
+ import EventEmitter from 'eventemitter3';
3
+
4
+ /**
5
+ * Core class for handling WebSocket communication.
6
+ * @augments {EventEmitter}
7
+ */
8
+ declare class IOCore extends EventEmitter<string | symbol, any> {
9
+ /**
10
+ * @param {string} url - The WebSocket URL to connect to.
11
+ */
12
+ constructor(url: string);
13
+ /**
14
+ * Client ID received from the server.
15
+ * @type {string}
16
+ */
17
+ cid: string;
18
+ /**
19
+ * IP address received from the server.
20
+ * @type {string}
21
+ */
22
+ ip: string;
23
+ /**
24
+ * The WebSocket instance.
25
+ * @type {WebSocket | null}
26
+ */
27
+ socket: WebSocket | null;
28
+ /**
29
+ * The default server URL.
30
+ * @type {string}
31
+ */
32
+ url: string;
33
+ /**
34
+ * Current connection state (number).
35
+ * @type {number}
36
+ */
37
+ state: number;
38
+ /**
39
+ * Current connection state (string).
40
+ * @type {string}
41
+ */
42
+ stateName: string;
43
+ /**
44
+ * Transmitted message counter.
45
+ * @type {number}
46
+ */
47
+ txCounter: number;
48
+ /**
49
+ * Received message counter.
50
+ * @type {number}
51
+ */
52
+ rxCounter: number;
53
+ /**
54
+ * Transmitted bytes counter.
55
+ * @type {number}
56
+ */
57
+ txBytes: number;
58
+ /**
59
+ * Received bytes counter.
60
+ * @type {number}
61
+ */
62
+ rxBytes: number;
63
+ /**
64
+ * Last transmit/receive time.
65
+ * @type {number}
66
+ */
67
+ lastTxRxTime: number;
68
+ /**
69
+ * Period for connection checker.
70
+ * @type {number}
71
+ */
72
+ connectionCheckerPeriod: number;
73
+ /**
74
+ * Interval ID for connection checker.
75
+ * @type {NodeJS.Timeout | null}
76
+ */
77
+ connectionCheckerIntervalID: NodeJS.Timeout | null;
78
+ /**
79
+ * Boho instance for encryption/decryption.
80
+ * @type {Boho}
81
+ */
82
+ boho: Boho$1;
83
+ /**
84
+ * Indicates if the connection is TLS (wss).
85
+ * @type {boolean}
86
+ */
87
+ TLS: boolean;
88
+ /**
89
+ * Encryption mode.
90
+ * @type {number}
91
+ */
92
+ encMode: number;
93
+ /**
94
+ * Indicates if authentication is used.
95
+ * @type {boolean}
96
+ */
97
+ useAuth: boolean;
98
+ /**
99
+ * Nickname.
100
+ * @type {string}
101
+ */
102
+ nick: string;
103
+ /**
104
+ * Set of subscribed channels.
105
+ * @type {Set<string>}
106
+ */
107
+ channels: Set<string>;
108
+ /**
109
+ * Map of promises for message responses.
110
+ * @type {Map<number, Array<Function>>}
111
+ */
112
+ promiseMap: Map<number, Array<Function>>;
113
+ /**
114
+ * Timeout for message promises.
115
+ * @type {number}
116
+ */
117
+ promiseTimeOut: number;
118
+ /**
119
+ * Message ID for promises.
120
+ * @type {number}
121
+ */
122
+ mid: number;
123
+ /**
124
+ * Quota level.
125
+ * @type {number}
126
+ */
127
+ level: number;
128
+ /**
129
+ * Quota table for current level.
130
+ * @type {object}
131
+ */
132
+ quota: object;
133
+ /**
134
+ * Server settings.
135
+ * @type {object}
136
+ */
137
+ serverSet: object;
138
+ /**
139
+ * Map of linked channels.
140
+ * @type {Map<string, Set<string>>}
141
+ */
142
+ linkMap: Map<string, Set<string>>;
143
+ /**
144
+ * Indicates if auto-reconnect is enabled.
145
+ * @type {boolean}
146
+ * @default true
147
+ * */
148
+ autoReconnect: boolean;
149
+ /**
150
+ * A flag to prevent duplicate close operations.
151
+ * @type {boolean}
152
+ * @private
153
+ */
154
+ private _closed;
155
+ /**
156
+ * Performs common cleanup for the connection. It clears pending promises,
157
+ * resets the socket reference, and sets the state to closed.
158
+ * This method is guarded to only run once.
159
+ * If autoReconnect is false, it also clears the keep-alive timer.
160
+ */
161
+ close(): void;
162
+ /**
163
+ * Disables auto-reconnect and closes the current connection.
164
+ * The instance can be re-opened manually later. For complete cleanup, use destroy().
165
+ */
166
+ stop(): void;
167
+ /**
168
+ * Permanently destroys the instance, cleaning up all resources.
169
+ * The instance will not be usable after this.
170
+ */
171
+ destroy(): void;
172
+ /**
173
+ * The core keep-alive logic. It checks if auto-reconnect is enabled.
174
+ * The actual check for the socket's state is implemented in the child classes.
175
+ */
176
+ keepAlive(): void;
177
+ /**
178
+ * Redirects the connection to a new URL.
179
+ * @param {string} url2 - The new URL to redirect to.
180
+ */
181
+ redirect(url2: string): void;
182
+ /**
183
+ * Opens the WebSocket connection.
184
+ * @param {string} [url] - Optional URL to connect to. If not provided, uses the instance's URL.
185
+ */
186
+ open(url?: string): void;
187
+ /**
188
+ * Handles the 'open' event of the WebSocket. Resets the closed flag and sets the state to open.
189
+ */
190
+ onOpen(): void;
191
+ /**
192
+ * Handles the 'close' event of the WebSocket.
193
+ */
194
+ onClose(): void;
195
+ /**
196
+ * Manually logs in with provided ID and key.
197
+ * @param {string} id - The user ID.
198
+ * @param {string} key - The user key.
199
+ * @returns {boolean}
200
+ */
201
+ login(id: string, key: string): boolean;
202
+ /**
203
+ * Sets up authentication for auto-login.
204
+ * @param {string} id - The user ID.
205
+ * @param {string} key - The user key.
206
+ * @returns {boolean}
207
+ */
208
+ auth(id: string, key: string): boolean;
209
+ /**
210
+ * Handles incoming data from the WebSocket.
211
+ * @param {Buffer} buffer - The incoming data buffer.
212
+ */
213
+ onData(buffer: Buffer$1): void;
214
+ /**
215
+ * Sends an IAM (I Am) message to the server.
216
+ * @param {string} [title] - Optional title for the IAM message.
217
+ */
218
+ iam(title?: string): void;
219
+ /**
220
+ * Sends a PING message to the server.
221
+ */
222
+ ping(): void;
223
+ /**
224
+ * Sends a PONG message to the server.
225
+ */
226
+ pong(): void;
227
+ /**
228
+ * Sends an ECHO message to the server.
229
+ * @param {*} [args] - Optional arguments to echo.
230
+ */
231
+ echo(args?: any): void;
232
+ /**
233
+ * Sends binary data.
234
+ * @param {...any} data - Data to send.
235
+ */
236
+ bin(...data: any[]): void;
237
+ /**
238
+ * Sends data over the WebSocket.
239
+ * @param {Buffer} data - The data buffer to send.
240
+ */
241
+ send(data: Buffer$1): void;
242
+ /**
243
+ * Determines if encryption should be used based on current mode and TLS status.
244
+ * @returns {boolean}
245
+ */
246
+ getEncryptionMode(): boolean;
247
+ /**
248
+ * Sends data with encryption based on the encryption mode.
249
+ * @param {Buffer} data - The data buffer to send.
250
+ * @param {boolean} [useEncryption] - Optional. Force encryption or not. If undefined, uses default policy.
251
+ */
252
+ send_enc_mode(data: Buffer$1, useEncryption?: boolean): void;
253
+ /**
254
+ * Sets a message promise for a given message ID.
255
+ * @param {number} mid - The message ID.
256
+ * @returns {Promise<any>}
257
+ */
258
+ setMsgPromise(mid: number): Promise<any>;
259
+ /**
260
+ * Tests and resolves/rejects a promise based on the incoming buffer.
261
+ * @param {Buffer} buffer - The incoming data buffer.
262
+ */
263
+ testPromise(buffer: Buffer$1): void;
264
+ /**
265
+ * Publishes a signal.
266
+ * @param {...any} args - Arguments for the signal.
267
+ */
268
+ publish(...args: any[]): void;
269
+ /**
270
+ * Sends a signal with a tag and arguments.
271
+ * @param {string} tag - The signal tag.
272
+ * @param {...any} args - Arguments for the signal.
273
+ * @throws {TypeError} If tag is not a string.
274
+ */
275
+ signal(tag: string, ...args: any[]): void;
276
+ /**
277
+ * Decrypts E2E data.
278
+ * @param {Buffer} data - The encrypted data.
279
+ * @param {string} key - The decryption key.
280
+ * @returns {Buffer}
281
+ */
282
+ decrypt_e2e(data: Buffer$1, key: string): Buffer$1;
283
+ /**
284
+ * Sends an E2E (End-to-End) encrypted signal.
285
+ * @param {string} tag - The signal tag.
286
+ * @param {Buffer} data - The data to encrypt and send.
287
+ * @param {string} key - The encryption key.
288
+ * @throws {TypeError} If tag is not a string.
289
+ */
290
+ signal_e2e(tag: string, data: Buffer$1, key: string): void;
291
+ /**
292
+ * Sets a value in the store.
293
+ * @param {string} storeName - The name of the store.
294
+ * @param {...any} args - Arguments to set.
295
+ * @returns {Promise<any>}
296
+ */
297
+ set(storeName: string, ...args: any[]): Promise<any>;
298
+ /**
299
+ * Gets a value from the store.
300
+ * @param {string} storeName - The name of the store.
301
+ * @returns {Promise<any>}
302
+ */
303
+ get(storeName: string): Promise<any>;
304
+ /**
305
+ * Sends a request to a target and topic.
306
+ * @param {string} target - The target of the request.
307
+ * @param {string} topic - The topic of the request.
308
+ * @param {...any} args - Optional arguments for the request.
309
+ * @returns {Promise<any>}
310
+ */
311
+ req(target: string, topic: string, ...args: any[]): Promise<any>;
312
+ /**
313
+ * Subscribes to a channel or channels.
314
+ * @param {string} tag - The tag(s) of the channel(s) to subscribe to (comma-separated).
315
+ * @throws {TypeError} If tag is not a string or exceeds length limit.
316
+ */
317
+ subscribe(tag: string): void;
318
+ /**
319
+ * Subscribes to a channel or channels with a promise.
320
+ * @param {string} tag - The tag(s) of the channel(s) to subscribe to (comma-separated).
321
+ * @returns {Promise<any>}
322
+ * @throws {TypeError} If tag is not a string or exceeds length limit.
323
+ */
324
+ subscribe_promise(tag: string): Promise<any>;
325
+ /**
326
+ * Subscribes to channels stored in memory (local cache).
327
+ */
328
+ subscribe_memory_channels(): void;
329
+ /**
330
+ * Unsubscribes from a channel or channels.
331
+ * @param {string} [tag=""] - The tag(s) of the channel(s) to unsubscribe from (comma-separated). If empty, unsubscribes from all.
332
+ * @throws {TypeError} If tag is not a string or exceeds length limit.
333
+ */
334
+ unsubscribe(tag?: string): void;
335
+ /**
336
+ * Listens for signals on a specific tag.
337
+ * @param {string} tag - The tag to listen on.
338
+ * @param {Function} handler - The callback function to handle the signal.
339
+ * @throws {TypeError} If tag is not a string, handler is not a function, or tag length is invalid.
340
+ */
341
+ listen(tag: string, handler: Function): void;
342
+ /**
343
+ * Links a local target to a remote tag and sets up a handler.
344
+ * @param {string} to - The local link target.
345
+ * @param {string} tag - The remote tag.
346
+ * @param {Function} handler - The callback function to handle the signal.
347
+ * @throws {TypeError} If 'to' or 'tag' are not strings, handler is not a function, or tag length is invalid.
348
+ */
349
+ link(to: string, tag: string, handler: Function): void;
350
+ /**
351
+ * Unlinks a specific tag from a local target.
352
+ * @param {string} to - The local link target.
353
+ * @param {string} tag - The tag to unlink.
354
+ * @throws {TypeError} If 'to' or 'tag' are not strings or tag length is invalid.
355
+ */
356
+ unlink(to: string, tag: string): void;
357
+ /**
358
+ * Unlinks all tags from a local target.
359
+ * @param {string} to - The local link target.
360
+ * @throws {TypeError} If 'to' is not a string.
361
+ */
362
+ unlinkAll(to: string): void;
363
+ /**
364
+ * Gets connection metrics.
365
+ * @returns {{tx: number, rx: number, txb: number, rxb: number, last: number}}
366
+ */
367
+ getMetric(): {
368
+ tx: number;
369
+ rx: number;
370
+ txb: number;
371
+ rxb: number;
372
+ last: number;
373
+ };
374
+ /**
375
+ * Gets the current connection state.
376
+ * @returns {number}
377
+ */
378
+ getState(): number;
379
+ /**
380
+ * Gets the current connection state name.
381
+ * @returns {string}
382
+ */
383
+ getStateName(): string;
384
+ /**
385
+ * Gets security-related information.
386
+ * @returns {{useAuth: boolean, isTLS: boolean, isAuthorized: boolean, encMode: number, usingEncryption: boolean}}
387
+ */
388
+ getSecurity(): {
389
+ useAuth: boolean;
390
+ isTLS: boolean;
391
+ isAuthorized: boolean;
392
+ encMode: number;
393
+ usingEncryption: boolean;
394
+ };
395
+ /**
396
+ * Changes the connection state and emits events.
397
+ * @param {string} state - The new state name (e.g., 'ready', 'closed').
398
+ * @param {string} [emitEventAndMessage] - Optional message to emit with the state change event.
399
+ */
400
+ stateChange(state: string, emitEventAndMessage?: string): void;
401
+ }
402
+ type Boho$1 = boho.Boho;
403
+ type Buffer$1 = boho.Buffer;
404
+
405
+ /**
406
+ * Browser WebSocket client extending IOCore.
407
+ * @augments {IOCore}
408
+ */
409
+ declare class IO extends IOCore {
410
+ /**
411
+ * The version of the client.
412
+ * @type {string}
413
+ */
414
+ static version: string;
415
+ /**
416
+ * The binary type for WebSocket messages.
417
+ * @type {string}
418
+ */
419
+ static binaryType: string;
420
+ /**
421
+ * The Boho library instance.
422
+ * @type {Boho}
423
+ */
424
+ static Boho: Boho;
425
+ /**
426
+ * The MBP (MessagePack-Boho) instance.
427
+ * @type {MBP}
428
+ */
429
+ static MBP: MBP;
430
+ /**
431
+ * The Buffer class from Boho.
432
+ * @type {Buffer}
433
+ */
434
+ static Buffer: Buffer;
435
+ /**
436
+ * Constants used by the client.
437
+ * @type {object}
438
+ */
439
+ static constants: object;
440
+ /**
441
+ * Tracks the number of IO instances created.
442
+ * @type {number}
443
+ */
444
+ static instanceCount: number;
445
+ /**
446
+ * Tracks the number of WebSocket objects created.
447
+ * @type {number}
448
+ */
449
+ static webSocketCount: number;
450
+ boundBrowserVisiblePing: any;
451
+ /**
452
+ * Pings the server when the browser tab becomes visible.
453
+ */
454
+ browserVisiblePing(): void;
455
+ /**
456
+ * Creates a new WebSocket connection.
457
+ * @param {string} url - The WebSocket URL to connect to.
458
+ */
459
+ createConnection(url: string): void;
460
+ /**
461
+ * Handles incoming WebSocket messages (arraybuffer type).
462
+ * @param {MessageEvent} event - The WebSocket message event.
463
+ */
464
+ onWebSocketMessage(event: MessageEvent): void;
465
+ /**
466
+ * Handles incoming WebSocket messages (blob type).
467
+ * @param {MessageEvent} event - The WebSocket message event.
468
+ */
469
+ onWebSocketMessageBlob(event: MessageEvent): Promise<void>;
470
+ /**
471
+ * Sends data over the WebSocket.
472
+ * @param {BufferSource} data - The data to send.
473
+ */
474
+ socket_send(data: BufferSource): void;
475
+ }
476
+ type Boho = boho.Boho;
477
+ type MBP = any;
478
+ type Buffer = boho.Buffer;
479
+
480
+ export { IO as default };
481
+ export type { Boho, Buffer, MBP };