iosignal 3.2.0 → 4.0.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 (76) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/README.en.md +56 -0
  3. package/README.md +39 -782
  4. package/dist/io.d.ts +10 -3
  5. package/dist/io.js +4 -4
  6. package/dist/io.js.map +1 -1
  7. package/dist/io.min.js +4 -4
  8. package/dist/io.min.js.map +1 -1
  9. package/dist/iosignal.js +266 -217
  10. package/dist/types/client/IOCore.d.ts +10 -3
  11. package/package.json +3 -4
  12. package/src/api/RedisAPI.js +19 -31
  13. package/src/api/api_reply.js +14 -6
  14. package/src/api/api_sudo.js +2 -8
  15. package/src/auth/Auth_Redis.js +8 -3
  16. package/src/auth/BohoAuth.js +12 -12
  17. package/src/client/CongPacket.js +2 -0
  18. package/src/client/IOCore.js +98 -52
  19. package/src/server/Manager.js +12 -12
  20. package/src/server/Metric.js +1 -2
  21. package/src/server/RemoteCore.js +27 -34
  22. package/src/server/Server.js +69 -60
  23. package/src/server/serverOption.js +3 -2
  24. package/README.ko.md +0 -566
  25. package/docs/iosignal_architecture.png +0 -0
  26. package/examples/react-chat-js/dist/assets/index-Cy4qaVcJ.js +0 -56
  27. package/examples/react-chat-js/dist/assets/index-NETrjYhz.css +0 -1
  28. package/examples/react-chat-js/dist/index.html +0 -14
  29. package/examples/react-chat-js/index.html +0 -13
  30. package/examples/react-chat-js/package-lock.json +0 -1723
  31. package/examples/react-chat-js/package.json +0 -20
  32. package/examples/react-chat-js/src/App.css +0 -61
  33. package/examples/react-chat-js/src/App.jsx +0 -125
  34. package/examples/react-chat-js/src/index.css +0 -13
  35. package/examples/react-chat-js/src/main.jsx +0 -13
  36. package/examples/react-chat-js/src/shared_io.js +0 -12
  37. package/examples/react-chat-js/vite.config.js +0 -7
  38. package/examples/server/index.js +0 -12
  39. package/examples/server/package-lock.json +0 -47
  40. package/examples/server/package.json +0 -12
  41. package/examples/svelte-chat-js/.devtools +0 -3
  42. package/examples/svelte-chat-js/README.md +0 -38
  43. package/examples/svelte-chat-js/package-lock.json +0 -1505
  44. package/examples/svelte-chat-js/package.json +0 -22
  45. package/examples/svelte-chat-js/src/app.css +0 -105
  46. package/examples/svelte-chat-js/src/app.d.ts +0 -13
  47. package/examples/svelte-chat-js/src/app.html +0 -12
  48. package/examples/svelte-chat-js/src/lib/images/github.svg +0 -1
  49. package/examples/svelte-chat-js/src/lib/images/svelte-logo.svg +0 -1
  50. package/examples/svelte-chat-js/src/lib/images/svelte-welcome.png +0 -0
  51. package/examples/svelte-chat-js/src/lib/images/svelte-welcome.webp +0 -0
  52. package/examples/svelte-chat-js/src/lib/shared_io.js +0 -12
  53. package/examples/svelte-chat-js/src/routes/+layout.svelte +0 -9
  54. package/examples/svelte-chat-js/src/routes/+page.js +0 -3
  55. package/examples/svelte-chat-js/src/routes/+page.svelte +0 -162
  56. package/examples/svelte-chat-js/static/favicon.svg +0 -1
  57. package/examples/svelte-chat-js/static/global.css +0 -19
  58. package/examples/svelte-chat-js/static/robots.txt +0 -3
  59. package/examples/svelte-chat-js/svelte.config.js +0 -15
  60. package/examples/svelte-chat-js/vite.config.js +0 -6
  61. package/test-browser/favicon.ico +0 -0
  62. package/test-browser/index.html +0 -95
  63. package/test-browser/server.js +0 -12
  64. package/test-nodejs/client-playToggle.js +0 -19
  65. package/test-nodejs/client.js +0 -17
  66. package/test-nodejs/client_api_reply.js +0 -15
  67. package/test-nodejs/server-Auth_Env.js +0 -19
  68. package/test-nodejs/server-Auth_File.js +0 -12
  69. package/test-nodejs/server-api_reply.js +0 -6
  70. package/test-nodejs/server-api_sudo.js +0 -7
  71. package/test-nodejs/server.js +0 -20
  72. package/test-nodejs/signalPack.js +0 -34
  73. package/test-nodejs/signaling.js +0 -61
  74. package/test-nodejs/simple-server.js +0 -13
  75. /package/{auth_file.js → auth_file_sample.js} +0 -0
  76. /package/{auth_file.json → auth_file_sample.json} +0 -0
@@ -27,15 +27,13 @@ export class RemoteCore {
27
27
  this.ssid = RemoteCore.ssid++; // ordered index number
28
28
  this.manager = manager;
29
29
 
30
- this.cid; // Communication Id
31
- this.did; // Device Id
32
- this.uid; // User Id
33
- this.nick = ""
30
+ this.cid = ''; // Communication Id
31
+ this.did = ''; // Device Id
32
+ this.uid = ''; // User Id
33
+ this.nick = ''
34
34
 
35
-
36
- this.lastEchoMessage = "N"
37
35
  this.privateNode = false
38
- this.HOME_CHANNEL = ""
36
+ this.HOME_CHANNEL = ''
39
37
  this.level = serverOption.defaultQuotaIndex;
40
38
  this.quota = quotaTable[this.level];
41
39
  this.isAdmin = false;
@@ -51,7 +49,8 @@ export class RemoteCore {
51
49
  setState(state) {
52
50
  this.state = state
53
51
  if (serverOption.debug.showAuthInfo) {
54
- this.stateLog.push(state)
52
+ // let s = state + ":" + CLIENT_STATE[ state]
53
+ this.stateLog.push( state )
55
54
  console.log(this.stateLog.join('>'))
56
55
  }
57
56
  }
@@ -66,7 +65,7 @@ export class RemoteCore {
66
65
 
67
66
 
68
67
  showMessageLog(message, isBinary) {
69
- let from = this.boho.isAuthorized ? `did: ${this.did} ${this.cid}@` : `${this.cid}@`
68
+ let from = this.boho.isAuthorized ? `[FROM]did: ${this.did} cid:${this.cid}` : `[FROM]cid:${this.cid}`
70
69
  if (isBinary) {
71
70
  let msgTypeName = IOMsg[message[0]]
72
71
  if (!msgTypeName) msgTypeName = Boho.BohoMsg[message[0]]
@@ -165,6 +164,7 @@ export class RemoteCore {
165
164
  case IOMsg.CID_REQ:
166
165
  if (this.state < CLIENT_STATE.SENT_SERVER_READY) {
167
166
  // Protocol violation: CID_REQ was sent before receiving the SERVER_READY signal.
167
+ console.log('CID_REQ before SERVER_READY')
168
168
  this.close()
169
169
  }
170
170
 
@@ -182,22 +182,16 @@ export class RemoteCore {
182
182
  this.setState(CLIENT_STATE.CID_READY)
183
183
  break;
184
184
 
185
-
186
- case IOMsg.ECHO: // TEXT only
187
- try {
188
- let msg = decoder.decode(message.subarray(1))
189
- this.lastEchoMessage = msg
190
- } catch (error) {
191
- // console.log('ECHO message is not a text')
185
+ case IOMsg.ECHO:
186
+ if( message.byteLength < 30) {
187
+ this.send(message, isBinary)
192
188
  }
193
- this.send(message, isBinary)
194
189
  break;
195
190
 
196
191
  case IOMsg.IAM:
197
- if (message.byteLength > 1) {
198
- let iamInfo = message.subarray(1)
199
- this.nick = decoder.decode(iamInfo)
200
- console.log('iam nick reset', this.nick)
192
+ if (message.byteLength > 1 && message.byteLength < 30) {
193
+ // if iam request has a text message then reset the nick property.
194
+ this.nick = decoder.decode(message.subarray(1))
201
195
  }
202
196
  this.iamResponse()
203
197
  break;
@@ -218,7 +212,7 @@ export class RemoteCore {
218
212
 
219
213
  case IOMsg.UNSUBSCRIBE:
220
214
  if (message.byteLength == 2) {
221
- this.manager.unsubscribe([""], this)
215
+ this.manager.unsubscribe([''], this)
222
216
  } else if (message.byteLength >= 3) {
223
217
  let tagLen = message.readUInt8(1)
224
218
 
@@ -292,7 +286,7 @@ export class RemoteCore {
292
286
  case Boho.BohoMsg.AUTH_REQ:
293
287
  if (!this.manager.authManager) return
294
288
  if (this.state < CLIENT_STATE.SENT_SERVER_READY) {
295
- // console.log('protocol error. auth_req before server_ready')
289
+ console.log('protocol error. auth_req before server_ready')
296
290
  this.close();
297
291
  }
298
292
  this.setState(CLIENT_STATE.RECV_AUTH_REQ)
@@ -305,22 +299,23 @@ export class RemoteCore {
305
299
  case Boho.BohoMsg.AUTH_HMAC:
306
300
  if (!this.manager.authManager) return
307
301
  if (this.state < CLIENT_STATE.SENT_SERVER_NONCE) {
308
- // console.log('protocol error. auth_hmac before server_nonce')
302
+ console.log('protocol error. auth_hmac before server_nonce')
309
303
  this.close();
310
304
  }
311
305
  this.setState(CLIENT_STATE.RECV_AUTH_HMAC)
312
306
  //async
313
307
  this.manager.authManager.verify_auth_hmac(message, this)
314
308
  .then(authInfo => {
315
- if (authInfo) {
316
- this.manager.deligateSignal(remote, '@$name', authInfo.name)
309
+ if (authInfo?.name) {
310
+ this.manager.deligateSignal(this, '@$name', authInfo.name)
317
311
  // console.log('device login success authInfo', authInfo)
318
312
  // this.manager.sender('@$name', authInfo.name )
319
313
  }
320
314
 
315
+ // console.log('[VERIFY AUTH_HMAC] SubClass of BohoAuth return authInfo:', authInfo )
321
316
  })
322
317
  .catch(e => {
323
-
318
+ console.log('authInfo return fail.', e )
324
319
  })
325
320
 
326
321
  return;
@@ -348,23 +343,19 @@ export class RemoteCore {
348
343
  response(msgId, statusCode, body) {
349
344
  // console.log('response body', body)
350
345
  if (body) {
351
-
352
346
  this.send_enc_mode(MBP.pack(
353
347
  MB('#type', '8', IOMsg.RESPONSE_MBP),
354
348
  MB('status', '8', statusCode),
355
349
  MB('mid', '16', msgId),
356
350
  MB('body', body)
357
351
  ))
358
-
359
352
  } else {
360
353
  this.send_enc_mode(MBP.pack(
361
354
  MB('#type', '8', IOMsg.RESPONSE_MBP),
362
355
  MB('status', '8', statusCode),
363
356
  MB('mid', '16', msgId)
364
357
  ))
365
-
366
358
  }
367
-
368
359
  }
369
360
 
370
361
 
@@ -407,9 +398,9 @@ export class RemoteCore {
407
398
 
408
399
 
409
400
 
410
- iamResponse(info = "") {
401
+ iamResponse(info = '') {
411
402
 
412
- if (info == "") {
403
+ if (info == '') {
413
404
  let channels = []
414
405
  for (let tag of this.channels.keys()) {
415
406
  channels.push(tag)
@@ -417,15 +408,17 @@ export class RemoteCore {
417
408
 
418
409
  info = {
419
410
  "ssid": this.ssid,
420
- "uid": this.uid,
421
411
  "cid": this.cid,
422
412
  "did": this.did,
413
+ "uid": this.uid,
414
+ "level": this.level,
423
415
  "nick": this.nick,
424
416
  "ip": this.ip
425
417
  , 'tag': channels
426
418
  }
427
419
  }
428
420
 
421
+ // console.log( 'iam res info', info )
429
422
  let pack = MBP.pack(
430
423
  MB('#MsgType', '8', IOMsg.IAM_RES),
431
424
  MB('#info', info)
@@ -18,20 +18,20 @@ export class Server extends EventEmitter {
18
18
  let pingT = parseInt(options.timeout)
19
19
  if (pingT && pingT >= 1000) serverOption.timeout = pingT
20
20
  }
21
-
22
21
  if (options.monitorPeriod) {
23
22
  let monitorT = parseInt(options.monitorPeriod)
24
23
  if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
25
24
  }
26
-
27
-
28
25
  if (options.showMessage) {
29
26
  serverOption.showMessage = options.showMessage
30
27
  }
31
-
32
28
  if (options.showMetric) {
33
29
  serverOption.showMetric = options.showMetric
34
30
  }
31
+ if (options.membersOnly) {
32
+ serverOption.membersOnly = options.membersOnly
33
+ }
34
+
35
35
 
36
36
  if (options.port || options.port == 0) {
37
37
  this.port = parseInt(options.port)
@@ -41,6 +41,10 @@ export class Server extends EventEmitter {
41
41
  this.httpServer = options.httpServer
42
42
  }
43
43
 
44
+ if (options.wsPath) {
45
+ this.wsPath = options.wsPath
46
+ }
47
+
44
48
  if (options.congPort || options.congPort == 0) {
45
49
  this.congPort = parseInt(options.congPort)
46
50
  }
@@ -53,7 +57,7 @@ export class Server extends EventEmitter {
53
57
  if (this.congPort || this.congPort == 0) this.serverCountToListen++;
54
58
 
55
59
  if (this.serverCountToListen === 0) {
56
- process.nextTick(() => this.emit('ready'));
60
+ throw new Error('Cannot create Server: At least one of "port", "httpServer", or "congPort" must be specified.');
57
61
  } else {
58
62
  if (this.port || this.port == 0 || this.httpServer) this.startWSServer();
59
63
  if (this.congPort || this.congPort == 0) this.startCongServer();
@@ -63,14 +67,11 @@ export class Server extends EventEmitter {
63
67
 
64
68
 
65
69
  startWSServer() {
66
-
67
70
  // console.log('optoins', options )
68
71
  if (this.port || this.port == 0) {
69
- console.log('opening WebSocket Server port:', this.port)
70
- this.wss = new WebSocketServer({ port: this.port })
71
- } else if (this.httpServer) {
72
- console.log('opening WebSocket Server external httpServer:')
73
- this.wss = new WebSocketServer({ server: this.httpServer })
72
+ this.wss = new WebSocketServer({ port: this.port , path: this.wsPath })
73
+ } else if (this.httpServer ) {
74
+ this.wss = new WebSocketServer({ server: this.httpServer, path: this.wsPath })
74
75
  } else {
75
76
  throw new TypeError(
76
77
  'One and only one of the "port", "httpServer"' +
@@ -78,7 +79,6 @@ export class Server extends EventEmitter {
78
79
  );
79
80
  }
80
81
 
81
-
82
82
  this.wss.once('listening', () => {
83
83
  this.port = this.wss.address().port;
84
84
  console.log('wss server bound port:', this.port );
@@ -89,7 +89,6 @@ export class Server extends EventEmitter {
89
89
  });
90
90
 
91
91
  // this.wss.setMaxListeners(0)
92
-
93
92
  this.wss.on('error', (err) => {
94
93
  console.error('### ws server error:', err.message)
95
94
  if (err.code == 'EADDRINUSE') {
@@ -97,8 +96,8 @@ export class Server extends EventEmitter {
97
96
  }
98
97
  })
99
98
 
100
- this.wss.on('close', (err) => {
101
- console.log('### WS server closed.', err)
99
+ this.wss.on('close', () => {
100
+ console.log('### WS server is closed.')
102
101
  })
103
102
 
104
103
  this.wss.on('connection', (ws, req) => {
@@ -110,8 +109,6 @@ export class Server extends EventEmitter {
110
109
  }
111
110
 
112
111
  startCongServer() {
113
- console.log('opening CongSocket Server port:', this.congPort)
114
-
115
112
  this.congServer = net.createServer((socket) => {
116
113
  this.manager.addRemote(socket)
117
114
  })
@@ -121,7 +118,7 @@ export class Server extends EventEmitter {
121
118
  process.exit()
122
119
  }
123
120
  }).on('close', () => {
124
- console.log('### cong server closed.')
121
+ console.log('### congsocket server is closed.')
125
122
  }).listen(this.congPort, () => {
126
123
  this.congPort = this.congServer.address().port;
127
124
  console.log('congsocket server bound port:', this.congPort );
@@ -132,64 +129,76 @@ export class Server extends EventEmitter {
132
129
  });
133
130
  }
134
131
 
135
-
136
-
132
+ /**
133
+ * API register.
134
+ * target: api_name <string>
135
+ * api: api_module <function module|class instance>
136
+ * return this
137
+ */
137
138
  api(target, api) {
138
139
 
139
- this.apiNames.add(target)
140
-
141
- if (!api.checkPermission || typeof api.checkPermission != 'function') {
142
- throw new Error('wrong api interface. no checkPermission function.')
140
+ if( typeof target !== 'string'){
141
+ throw new Error(`api( target ,api ) target name is not a string.`)
143
142
  }
144
-
145
-
146
- if (typeof api.request == 'function' && Array.isArray(api.commands)) {
147
- console.log(`API TYPE1. A Class with one request function. target: ${target} list: ${api.commands}`)
148
-
149
- this.on(target, (remote, req) => {
150
- if (api.checkPermission(remote, req)) {
151
- api.request(remote, req)
152
- } else {
153
- remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
143
+ if (!api.checkPermission || typeof api.checkPermission != 'function') {
144
+ throw new Error(`API ${target} : no checkPermission or not a function.`)
145
+ }
146
+ if(!api.commands || !Array.isArray(api.commands) ){
147
+ throw new Error(`API ${target} : no commands or !Array.`)
148
+ }
149
+
150
+ // API TYPE 1. single request() function.
151
+ if ( api.request && typeof api.request == 'function' ) {
152
+ this.on(target, async (remote, req) => {
153
+ try {
154
+ if (!api.checkPermission(remote, req)) {
155
+ remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
156
+ return
157
+ }
158
+ if( api.commands.includes( req.topic )){
159
+ console.log('server api req', req )
160
+ await api.request(remote, req)
161
+ }else{
162
+ remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
163
+ }
164
+ } catch (error) {
165
+ console.error(`Unhandled API Error in target [${target}]:`, error);
166
+ remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
154
167
  }
155
168
  })
156
-
157
169
  } else {
158
- let apiList = []
159
- Object.keys(api).forEach(v => {
160
- if (typeof api[v] === 'function') apiList.push(v)
161
- console.log('api list', typeof api[v], v)
162
- })
163
-
164
- console.log(`API TYPE2. Multiple function list. target: ${target} list:${apiList}`)
165
-
166
- this.on(target, (remote, req) => {
167
- let r;
168
- if (!api.checkPermission(remote, req)) {
169
- r = "NO_PERMISSION."
170
- } else {
171
- if (api[req.topic]) {
172
- console.log(`API TYPE2. request target: ${target} has topic:${req.topic}`)
173
- api[req.topic](remote, req)
170
+ // API TYPE 2. multiple functions.
171
+ this.on(target, async (remote, req) => {
172
+ try {
173
+ if (!api.checkPermission(remote, req)) {
174
+ remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
174
175
  return
175
- } else {
176
- r = `target: ${req.target} has not topic name: ${req.topic}`
177
176
  }
177
+ if( api.commands.includes( req.topic )){
178
+ await api[req.topic](remote, req)
179
+ }else{
180
+ remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
181
+ }
182
+ } catch (error) {
183
+ console.error(`Unhandled API Error in target [${target}]:`, error);
184
+ remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
178
185
  }
179
-
180
- remote.response(req.mid, STATUS.ERROR, r)
181
186
  })
182
187
  }
183
-
188
+ this.apiNames.add(target)
189
+ console.log(`[API registed: ${target} ] accept commands: ${api.commands}`)
184
190
  return this
185
191
  }
186
192
 
187
193
  close(callback) {
188
- console.log('closing io-signal server...')
194
+ console.log('closing iosignal server...')
189
195
  if (this.manager) {
190
196
  this.manager.close()
191
197
  }
192
198
 
199
+ this.apiNames.forEach(v => this.removeAllListeners(v))
200
+ this.apiNames.clear()
201
+
193
202
  let serverCount = 0;
194
203
  if (this.wss) serverCount++;
195
204
  if (this.congServer) serverCount++;
@@ -203,14 +212,14 @@ export class Server extends EventEmitter {
203
212
  const onClosed = () => {
204
213
  closedCount++;
205
214
  if (closedCount === serverCount) {
206
- console.log('io-signal server closed.')
215
+ console.log('IOSignal server is closed.')
207
216
  if (callback) callback();
208
217
  }
209
218
  }
210
219
 
211
220
  if (this.wss) {
212
221
  this.wss.close(() => {
213
- console.log('wss server closed.')
222
+ // console.log('wss server closed.')
214
223
  onClosed()
215
224
  })
216
225
  }
@@ -218,7 +227,7 @@ export class Server extends EventEmitter {
218
227
  if (this.congServer) {
219
228
  this.congServer.close((err) => {
220
229
  if (err) console.error('congServer close error', err)
221
- console.log('cong server closed.')
230
+ // console.log('cong server closed.')
222
231
  onClosed()
223
232
  })
224
233
  }
@@ -5,6 +5,7 @@ export let serverOption = {
5
5
  port: null, // WebSocket
6
6
  congPort: null, // CongSocket
7
7
  httpServer: null,
8
+ wsPath: null,
8
9
  timeout: 50000,
9
10
  showMessage: 'none',
10
11
  showMetric: 0,
@@ -46,10 +47,10 @@ export let serverOption = {
46
47
  },
47
48
 
48
49
  auth: {
49
- delay_auth_fail: 1000,
50
+ delay_auth_fail: 600,
50
51
  },
51
52
 
52
- memberOnly: false
53
+ membersOnly: false
53
54
 
54
55
  }
55
56