iosignal 2.2.1 → 3.0.1

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 (49) hide show
  1. package/README.md +248 -114
  2. package/dist/io.d.ts +481 -0
  3. package/dist/io.js +4 -5
  4. package/dist/io.js.map +1 -1
  5. package/dist/io.min.js +4 -5
  6. package/dist/io.min.js.map +1 -1
  7. package/dist/iosignal.js +9559 -0
  8. package/dist/types/client/IOCore.d.ts +408 -0
  9. package/dist/types/client/browser/IOWebSocket.d.ts +75 -0
  10. package/dist/types/common/constants.d.ts +169 -0
  11. package/dist/types/common/payload.d.ts +6 -0
  12. package/dist/types/common/quotaTable.d.ts +47 -0
  13. package/docs/README.ko.md +371 -0
  14. package/docs/iosignal_architecture.png +0 -0
  15. package/examples/react-chat-js/dist/assets/index-Cy4qaVcJ.js +56 -0
  16. package/examples/react-chat-js/dist/assets/index-NETrjYhz.css +1 -0
  17. package/examples/react-chat-js/dist/index.html +14 -0
  18. package/examples/react-chat-js/index.html +13 -0
  19. package/examples/react-chat-js/package-lock.json +1628 -0
  20. package/examples/react-chat-js/package.json +20 -0
  21. package/examples/react-chat-js/src/App.css +61 -0
  22. package/examples/react-chat-js/src/App.jsx +125 -0
  23. package/examples/react-chat-js/src/index.css +13 -0
  24. package/examples/react-chat-js/src/main.jsx +13 -0
  25. package/examples/react-chat-js/src/shared_io.js +12 -0
  26. package/examples/react-chat-js/vite.config.js +7 -0
  27. package/examples/server/index.js +12 -0
  28. package/examples/server/package.json +12 -0
  29. package/package.json +24 -12
  30. package/rollup.config.js +13 -8
  31. package/src/auth/Auth_File.js +1 -1
  32. package/src/client/IOCongSocket.js +20 -9
  33. package/src/client/IOCore.js +422 -77
  34. package/src/client/IOWS.js +12 -15
  35. package/src/client/browser/IOWebSocket.js +217 -0
  36. package/src/common/constants.js +91 -1
  37. package/test-nodejs/server-Auth_File.js +1 -1
  38. package/tsconfig.json +24 -0
  39. package/dist/iosignal.cjs +0 -23
  40. package/dist/iosignal.mjs +0 -23
  41. package/img/iosignal_stack.png +0 -0
  42. package/src/client/IOWebSocket.js +0 -118
  43. package/test-nodejs/client.cjs +0 -17
  44. package/test-nodejs/server-commonjs.cjs +0 -9
  45. /package/{auth_file.mjs → auth_file.js} +0 -0
  46. /package/test-nodejs/{client.mjs → client.js} +0 -0
  47. /package/test-nodejs/{client_api_reply.mjs → client_api_reply.js} +0 -0
  48. /package/test-nodejs/{server-esm.mjs → server.js} +0 -0
  49. /package/test-nodejs/{simple-server.mjs → simple-server.js} +0 -0
@@ -1,4 +1,5 @@
1
1
  import { IOCore } from "./IOCore.js";
2
+ import { STATES } from "../common/constants.js";
2
3
  import { WebSocket } from "ws";
3
4
 
4
5
  // Node.js 'ws' websocket
@@ -10,32 +11,28 @@ export class IOWS extends IOCore {
10
11
 
11
12
 
12
13
 
14
+ /**
15
+ * Closes ws WebSocket and cleans resources.
16
+ */
13
17
  close() {
14
18
  if (this.socket) {
15
- this.socket.onclose = null
16
- this.socket.onmessage = null
17
- this.socket.onerror = null
18
- this.socket.close();
19
- this.socket = null;
19
+ this.socket.removeAllListeners();
20
+ if (this.socket.readyState !== WebSocket.CLOSED) {
21
+ this.socket.close();
22
+ }
20
23
  }
21
- this.emit('close')
24
+ super.close();
22
25
  }
23
26
 
24
27
 
25
-
26
- stop() {
27
- this.close()
28
- clearInterval(this.connectionCheckerIntervalID);
29
- this.connectionCheckerIntervalID = null
30
- }
31
-
32
28
  keepAlive() {
33
- if (!this.socket || this.socket?.readyState === 3) {
29
+ if (!this.autoReconnect) return;
30
+ // Reconnect only if the socket is closed and the state reflects that.
31
+ if ((!this.socket || this.socket.readyState === WebSocket.CLOSED) && this.state === STATES.CLOSED) {
34
32
  this.open();
35
33
  }
36
34
  }
37
35
 
38
-
39
36
  createConnection(url) {
40
37
  // node WebSocket
41
38
  this.socket = new WebSocket(url);
@@ -0,0 +1,217 @@
1
+ import {version} from "../../../package.json";
2
+ import { IOCore } from "../IOCore.js";
3
+ import Boho from 'boho'
4
+ import * as constants from '../../common/constants.js'
5
+
6
+ /**
7
+ * @typedef {import("boho").Boho} Boho
8
+ * @typedef {import("boho").MBP} MBP
9
+ * @typedef {import("boho").Buffer} Buffer
10
+ */
11
+
12
+ const Buffer = Boho.Buffer
13
+
14
+ /**
15
+ * Browser WebSocket client extending IOCore.
16
+ * @augments {IOCore}
17
+ */
18
+ export default class IO extends IOCore {
19
+ /**
20
+ * The version of the client.
21
+ * @type {string}
22
+ */
23
+ static version = version
24
+ /**
25
+ * The binary type for WebSocket messages.
26
+ * @type {string}
27
+ */
28
+ static binaryType = "arraybuffer"
29
+ /**
30
+ * The Boho library instance.
31
+ * @type {Boho}
32
+ */
33
+ static Boho = Boho;
34
+ /**
35
+ * The MBP (MessagePack-Boho) instance.
36
+ * @type {MBP}
37
+ */
38
+ static MBP = Boho.MBP;
39
+ /**
40
+ * The Buffer class from Boho.
41
+ * @type {Buffer}
42
+ */
43
+ static Buffer = Boho.Buffer;
44
+ /**
45
+ * Constants used by the client.
46
+ * @type {object}
47
+ */
48
+ static constants = constants
49
+
50
+ /**
51
+ * Tracks the number of IO instances created.
52
+ * @type {number}
53
+ */
54
+ static instanceCount = 0;
55
+
56
+ /**
57
+ * Tracks the number of WebSocket objects created.
58
+ * @type {number}
59
+ */
60
+ static webSocketCount = 0;
61
+
62
+
63
+
64
+ /**
65
+ * Creates an instance of IO.
66
+ * @param {string} url - The WebSocket URL to connect to.
67
+ */
68
+ constructor(url) {
69
+ super(url);
70
+ IO.instanceCount++;
71
+ this.boundBrowserVisiblePing = this.browserVisiblePing.bind(this)
72
+ document.addEventListener('visibilitychange', this.boundBrowserVisiblePing);
73
+ if (url) this.open();
74
+ }
75
+
76
+ /**
77
+ * Pings the server when the browser tab becomes visible.
78
+ */
79
+ browserVisiblePing() {
80
+ if (document.visibilityState === 'visible') {
81
+ this.ping()
82
+ }
83
+ }
84
+
85
+
86
+ /**
87
+ * Closes the WebSocket connection and cleans up its event handlers.
88
+ * This is intended for temporary disconnections, and the connection might be re-established by keepAlive.
89
+ */
90
+ close() {
91
+ if (this.socket) {
92
+ this.socket.onopen = null;
93
+ this.socket.onclose = null;
94
+ this.socket.onmessage = null;
95
+ this.socket.onerror = null;
96
+ if (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING) {
97
+ this.socket.close();
98
+ }
99
+ }
100
+ super.close();
101
+ }
102
+
103
+ /**
104
+ * Permanently stops the connection and cleans up its specific resources.
105
+ * For complete cleanup and to make the instance unusable, use destroy().
106
+ */
107
+ stop() {
108
+ super.stop();
109
+ }
110
+
111
+ /**
112
+ * Permanently destroys the instance, cleaning up all resources including global event listeners.
113
+ * The instance will not be usable after this.
114
+ */
115
+ destroy() {
116
+ document.removeEventListener('visibilitychange', this.boundBrowserVisiblePing);
117
+ super.destroy();
118
+ }
119
+
120
+
121
+ /**
122
+ * Keeps the connection alive by re-opening if closed.
123
+ */
124
+ keepAlive() {
125
+ if (!this.autoReconnect) return;
126
+ // Reconnect only if the socket is closed and the state reflects that.
127
+ if ((!this.socket || this.socket.readyState === WebSocket.CLOSED) && this.state === IO.constants.STATES.CLOSED) {
128
+ this.open();
129
+ }
130
+ }
131
+
132
+
133
+
134
+ /**
135
+ * Creates a new WebSocket connection.
136
+ * @param {string} url - The WebSocket URL to connect to.
137
+ */
138
+ createConnection(url) {
139
+ // Web Browser WebSocket
140
+ IO.webSocketCount++;
141
+ this.socket = new WebSocket(url);
142
+ this.socket.binaryType = IO.binaryType
143
+
144
+ this.stateChange('opening')
145
+
146
+ this.socket.onopen = () => {
147
+
148
+ if (!this.socket) {
149
+ return;
150
+ }
151
+
152
+ if (this.socket.binaryType == "arraybuffer") {
153
+ this.socket.onmessage = this.onWebSocketMessage.bind(this);
154
+ } else { // blob
155
+ this.socket.onmessage = this.onWebSocketMessageBlob.bind(this);
156
+ }
157
+ this.emit('open');
158
+ };
159
+
160
+ this.socket.onerror = (e) => {
161
+ this.emit('error', e)
162
+ }
163
+
164
+ this.socket.onclose = () => {
165
+ this.emit('close');
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Handles incoming WebSocket messages (arraybuffer type).
171
+ * @param {MessageEvent} event - The WebSocket message event.
172
+ */
173
+ onWebSocketMessage(event) {
174
+ // event.data is arrayBuffer or text_message
175
+ this.rxCounter++;
176
+ this.lastTxRxTime = Date.now();
177
+ let buffer = Buffer.from(event.data)
178
+ this.rxBytes += buffer.byteLength
179
+ this.emit('socket_data', buffer);
180
+ }
181
+
182
+ /**
183
+ * Handles incoming WebSocket messages (blob type).
184
+ * @param {MessageEvent} event - The WebSocket message event.
185
+ */
186
+ async onWebSocketMessageBlob(event) {
187
+ // event.data is Blob or text_message
188
+ this.rxCounter++;
189
+ this.lastTxRxTime = Date.now();
190
+ let buffer;
191
+ if (event.data instanceof Blob) {
192
+ let ab = await event.data.arrayBuffer()
193
+ buffer = Buffer.from(ab)
194
+ } else {
195
+ buffer = Buffer.from(event.data)
196
+ }
197
+ this.rxBytes += buffer.byteLength
198
+ this.emit('socket_data', buffer);
199
+ }
200
+
201
+ /**
202
+ * Sends data over the WebSocket.
203
+ * @param {BufferSource} data - The data to send.
204
+ */
205
+ socket_send(data) {
206
+ if (this.socket?.readyState === 1) {
207
+ this.socket.send(data)
208
+ this.txCounter++;
209
+ this.txBytes += data.byteLength
210
+ this.lastTxRxTime = Date.now();
211
+ } else {
212
+ console.log('.')
213
+ }
214
+ }
215
+
216
+ }
217
+
@@ -1,4 +1,16 @@
1
1
 
2
+ /**
3
+ * @typedef {object} STATES
4
+ * @property {number} OPENING
5
+ * @property {number} OPEN
6
+ * @property {number} CLOSING
7
+ * @property {number} CLOSED
8
+ * @property {number} SERVER_READY
9
+ * @property {number} AUTH_FAIL
10
+ * @property {number} AUTH_READY
11
+ * @property {number} READY
12
+ * @property {number} REDIRECTING
13
+ */
2
14
  // Client STATES: name and number
3
15
  export const STATES = {
4
16
  OPENING: 0,
@@ -13,7 +25,17 @@ export const STATES = {
13
25
  }
14
26
  for (let c in STATES) { STATES[STATES[c]] = c }
15
27
 
16
- // server side client state
28
+ /**
29
+ * @typedef {object} CLIENT_STATE
30
+ * @property {number} INIT
31
+ * @property {number} SENT_SERVER_READY
32
+ * @property {number} RECV_AUTH_REQ
33
+ * @property {number} SENT_SERVER_NONCE
34
+ * @property {number} RECV_AUTH_HMAC
35
+ * @property {number} AUTH_FAIL
36
+ * @property {number} AUTH_READY
37
+ * @property {number} CID_READY
38
+ */
17
39
  export const CLIENT_STATE = {
18
40
  INIT: 0,
19
41
  SENT_SERVER_READY: 1,
@@ -26,6 +48,12 @@ export const CLIENT_STATE = {
26
48
  }
27
49
  for (let c in CLIENT_STATE) { CLIENT_STATE[CLIENT_STATE[c]] = c }
28
50
 
51
+ /**
52
+ * @typedef {object} ENC_MODE
53
+ * @property {number} NO
54
+ * @property {number} YES
55
+ * @property {number} AUTO
56
+ */
29
57
  export let ENC_MODE = {
30
58
  NO: 0,
31
59
  YES: 1,
@@ -35,6 +63,15 @@ export let ENC_MODE = {
35
63
  for (let c in ENC_MODE) { ENC_MODE[ENC_MODE[c]] = c }
36
64
 
37
65
 
66
+ /**
67
+ * @typedef {object} SIZE_LIMIT
68
+ * @property {number} TAG_LEN1
69
+ * @property {number} TAG_LEN2
70
+ * @property {number} CONNECTION_CHECKER_PERIOD
71
+ * @property {number} PROMISE_TIMEOUT
72
+ * @property {number} DID
73
+ * @property {number} CID
74
+ */
38
75
  export const SIZE_LIMIT = {
39
76
  TAG_LEN1: 255,
40
77
  TAG_LEN2: 65535,
@@ -44,6 +81,15 @@ export const SIZE_LIMIT = {
44
81
  CID: 12
45
82
  }
46
83
 
84
+ /**
85
+ * @typedef {object} PAYLOAD_TYPE
86
+ * @property {number} EMPTY
87
+ * @property {number} TEXT
88
+ * @property {number} BINARY
89
+ * @property {number} OBJECT
90
+ * @property {number} MJSON
91
+ * @property {number} MBA
92
+ */
47
93
  export let PAYLOAD_TYPE = {
48
94
  EMPTY: 0,
49
95
  TEXT: 1,
@@ -55,6 +101,40 @@ export let PAYLOAD_TYPE = {
55
101
  for (let c in PAYLOAD_TYPE) { PAYLOAD_TYPE[PAYLOAD_TYPE[c]] = c }
56
102
 
57
103
 
104
+ /**
105
+ * @typedef {object} IOMsg
106
+ * @property {number} SERVER_READY
107
+ * @property {number} CID_REQ
108
+ * @property {number} CID_RES
109
+ * @property {number} QUOTA_LEVEL
110
+ * @property {number} SERVER_CLEAR_AUTH
111
+ * @property {number} SERVER_REDIRECT
112
+ * @property {number} LOOP
113
+ * @property {number} ECHO
114
+ * @property {number} PING
115
+ * @property {number} PONG
116
+ * @property {number} CLOSE
117
+ * @property {number} SIGNAL
118
+ * @property {number} SIGNAL_REQ
119
+ * @property {number} SIGNAL_E2E
120
+ * @property {number} SUBSCRIBE
121
+ * @property {number} SUBSCRIBE_REQ
122
+ * @property {number} UNSUBSCRIBE
123
+ * @property {number} SERVER_SIGNAL
124
+ * @property {number} IAM
125
+ * @property {number} IAM_RES
126
+ * @property {number} SET
127
+ * @property {number} RESPONSE_CODE
128
+ * @property {number} RESPONSE_MBP
129
+ * @property {number} REQUEST
130
+ * @property {number} RESPONSE
131
+ * @property {number} FLOW_MODE
132
+ * @property {number} WAIT
133
+ * @property {number} RESUME
134
+ * @property {number} TIME_OUT
135
+ * @property {number} OVER_SIZE
136
+ * @property {number} OVER_FLOW
137
+ */
58
138
  // IO message's one-byte headers.
59
139
  export let IOMsg = {
60
140
 
@@ -134,11 +214,21 @@ export let IOMsg = {
134
214
  for (let c in IOMsg) { IOMsg[IOMsg[c]] = c }
135
215
  // console.log( IOMsg );
136
216
 
217
+ /**
218
+ * @typedef {object} API_TYPE
219
+ * @property {string} REQUEST_RESPONSE
220
+ * @property {string} ONE_WAY
221
+ */
137
222
  export const API_TYPE = {
138
223
  'REQUEST_RESPONSE': 'requet_response',
139
224
  'ONE_WAY': 'one_way'
140
225
  }
141
226
 
227
+ /**
228
+ * @typedef {object} STATUS
229
+ * @property {number} OK
230
+ * @property {number} ERROR
231
+ */
142
232
  // api response status code
143
233
  export const STATUS = {
144
234
  OK: 0,
@@ -7,6 +7,6 @@ const options = {
7
7
  }
8
8
 
9
9
  // const server = new Server( options, new Auth_File('../authInfo.json') ) // JSON version. cannot add comments.
10
- const server = new Server(options, new Auth_File('../auth_file.mjs')) // JS version. it support comments.
10
+ const server = new Server(options, new Auth_File('../auth_file.js')) // JS version. it support comments.
11
11
 
12
12
 
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "emitDeclarationOnly": true,
5
+ "outDir": "./dist/types",
6
+ "allowJs": true,
7
+ "moduleResolution": "node",
8
+ "target": "es2015",
9
+ "lib": ["es2020", "dom"],
10
+ "baseUrl": ".",
11
+ "paths": {
12
+ "@src/*": ["src/*"]
13
+ }
14
+ },
15
+ "include": [
16
+ "src/client/browser/IOWebSocket.js",
17
+ "src/client/IOCore.js",
18
+ "src/common/constants.js",
19
+ "package.json"
20
+ ],
21
+ "exclude": [
22
+ "node_modules"
23
+ ]
24
+ }