iosignal 1.6.0 → 1.7.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 (45) hide show
  1. package/dist/iosignal.cjs +2 -2
  2. package/dist/iosignal.esm.js +1 -1
  3. package/dist/iosignal.esm.js.map +1 -1
  4. package/dist/iosignal.min.js +1 -1
  5. package/dist/iosignal.min.js.map +1 -1
  6. package/indexWebBrowser.js +1 -1
  7. package/package.json +2 -2
  8. package/src/api/RedisAPI.js +23 -26
  9. package/src/api/api_reply.js +12 -15
  10. package/src/api/api_sudo.js +67 -72
  11. package/src/auth/Auth_Env.js +7 -9
  12. package/src/auth/Auth_File.js +31 -35
  13. package/src/auth/Auth_Redis.js +15 -21
  14. package/src/auth/BohoAuth.js +68 -72
  15. package/src/client/CongPacket.js +15 -24
  16. package/src/client/IOCongSocket.js +26 -33
  17. package/src/client/IOCore.js +6 -35
  18. package/src/client/IOWS.js +22 -23
  19. package/src/client/IOWebSocket.js +11 -7
  20. package/src/common/constants.js +21 -32
  21. package/src/common/payload.js +47 -48
  22. package/src/common/quotaTable.js +10 -10
  23. package/src/common/util.js +38 -38
  24. package/src/server/FileLogger.js +9 -9
  25. package/src/server/Manager.js +39 -55
  26. package/src/server/Metric.js +54 -53
  27. package/src/server/Remote.js +207 -0
  28. package/src/server/{ServerRemoteCore.js → RemoteCore.js} +53 -81
  29. package/src/server/Server.js +22 -25
  30. package/src/server/serverOption.js +10 -10
  31. package/test-browser/index.html +30 -26
  32. package/test-browser/server.js +1 -1
  33. package/test-client-nodejs/client.cjs +6 -6
  34. package/test-client-nodejs/client.mjs +6 -6
  35. package/test-client-nodejs/client_api_reply.mjs +8 -8
  36. package/test-client-nodejs/stateChange.mjs +7 -7
  37. package/test-server/server-Auth_Env.js +2 -2
  38. package/test-server/server-Auth_File.js +1 -1
  39. package/test-server/server-api_reply.js +2 -2
  40. package/test-server/server-api_sudo.js +3 -3
  41. package/test-server/server-esm.mjs +9 -8
  42. package/test-server/simple-server.mjs +6 -6
  43. package/test-signal-payloadType/signalPack.js +1 -1
  44. package/test-signal-payloadType/signaling.js +4 -4
  45. package/src/server/ServerRemote.js +0 -227
@@ -1,92 +1,88 @@
1
- /**
2
- * BohoAuth
3
- */
4
-
5
1
  import { MBP } from 'meta-buffer-pack'
6
- import { BohoMsg , Meta } from 'boho'
2
+ import { BohoMsg, Meta } from 'boho'
7
3
  import { quotaTable } from '../common/quotaTable.js'
8
4
  import { serverOption } from '../server/serverOption.js'
9
- import { IOMsg ,CLIENT_STATE } from '../common/constants.js'
5
+ import { IOMsg, CLIENT_STATE } from '../common/constants.js'
10
6
  import { FileLogger } from '../server/FileLogger.js'
11
7
 
12
8
  const decoder = new TextDecoder()
13
- export class BohoAuth{
14
- constructor(){
9
+ export class BohoAuth {
10
+ constructor() {
15
11
  this.authLogger;
16
- if( serverOption.fileLogger.auth.use ){
17
- this.authLogger = new FileLogger( serverOption.fileLogger.auth.path)
12
+ if (serverOption.fileLogger.auth.use) {
13
+ this.authLogger = new FileLogger(serverOption.fileLogger.auth.path)
18
14
  console.log('Auth: begin file logger.[auth]')
19
15
  }
20
16
  }
21
17
 
22
- send_auth_fail( peer , reason){
23
- if( this.authLogger ){
18
+ send_auth_fail(peer, reason) {
19
+ if (this.authLogger) {
24
20
  let peerInfo = `FAIL #${peer.ssid} reason:${reason} `
25
- this.authLogger.log( peerInfo )
26
- }
21
+ this.authLogger.log(peerInfo)
22
+ }
27
23
 
28
- console.log('## AUTH_FAIL reason:', reason )
24
+ console.log('## AUTH_FAIL reason:', reason)
29
25
  // peer.send( Buffer.from( [BohoMsg.AUTH_FAIL] ))
30
- peer.setState( CLIENT_STATE.AUTH_FAIL )
31
- setTimeout(e=>{
32
- peer.send( Buffer.from( [BohoMsg.AUTH_FAIL] ))
33
- }, serverOption.auth.delay_auth_fail )
26
+ peer.setState(CLIENT_STATE.AUTH_FAIL)
27
+ setTimeout(e => {
28
+ peer.send(Buffer.from([BohoMsg.AUTH_FAIL]))
29
+ }, serverOption.auth.delay_auth_fail)
34
30
  }
35
31
 
36
- async verify_auth_hmac( auth_hmac , peer ){
32
+ async verify_auth_hmac(auth_hmac, peer) {
37
33
  try {
38
34
  //1. unpack
39
- let infoPack = MBP.unpack( auth_hmac , Meta.AUTH_HMAC )
40
- if(!infoPack){
41
- this.send_auth_fail( peer ,'unpack auth_pack fail');
35
+ let infoPack = MBP.unpack(auth_hmac, Meta.AUTH_HMAC)
36
+ if (!infoPack) {
37
+ this.send_auth_fail(peer, 'unpack auth_pack fail');
42
38
  return
43
39
  }
44
40
 
45
41
  let id = ""
46
- if( infoPack.id8.includes(0)){
47
- id = decoder.decode( infoPack.id8.subarray( 0, infoPack.id8.indexOf(0) ) );
48
- }else{
49
- id = decoder.decode( infoPack.id8 );
42
+ if (infoPack.id8.includes(0)) {
43
+ id = decoder.decode(infoPack.id8.subarray(0, infoPack.id8.indexOf(0)));
44
+ } else {
45
+ id = decoder.decode(infoPack.id8);
50
46
  }
51
47
 
52
48
  //2. get key of id from DB
53
- let authInfo = await this.getAuth( id )
49
+ let authInfo = await this.getAuth(id)
54
50
 
55
51
  // console.log('##### authInfo',authInfo )
56
- if(serverOption.debug.showAuthInfo ){
52
+ if (serverOption.debug.showAuthInfo) {
57
53
  }
58
-
59
- if( !authInfo ){
60
- this.send_auth_fail( peer , 'NO ID:'+ id);
54
+
55
+ if (!authInfo) {
56
+ this.send_auth_fail(peer, 'NO ID:' + id);
61
57
  return
62
58
  }
63
59
 
64
60
  // console.log('db authInfo.key: ', authInfo.key)
65
- peer.boho.copy_id8( infoPack.id8 )
61
+ peer.boho.copy_id8(infoPack.id8)
66
62
  // type of key
67
- let authKey ;
63
+ let authKey;
68
64
  const SHA256_HASH_BASE64_LEN = 44
69
- if( authInfo.key.length == SHA256_HASH_BASE64_LEN){
65
+ if (authInfo.key.length == SHA256_HASH_BASE64_LEN) {
70
66
  // hashed key
71
- authKey = Buffer.from( authInfo.key, 'base64')
72
- peer.boho.copy_key( authKey )
73
- }else{
67
+ authKey = Buffer.from(authInfo.key, 'base64')
68
+ peer.boho.copy_key(authKey)
69
+ } else {
74
70
  // plain key
75
- peer.boho.set_key( authInfo.key )
71
+ peer.boho.set_key(authInfo.key)
76
72
  }
77
-
73
+
78
74
  //3. check hmac
79
75
  // console.log('-- found: authKey of id: ', id, authKey.toString('hex'))
80
- let auth_ack = peer.boho.check_auth_hmac( infoPack )
76
+ let auth_ack = peer.boho.check_auth_hmac(infoPack)
81
77
 
82
78
  // console.log('auth_ack', auth_ack )
83
- if( !auth_ack ){
84
- this.send_auth_fail( peer, 'hmac dismatched' );
79
+ if (!auth_ack) {
80
+ this.send_auth_fail(peer, 'hmac dismatched');
85
81
  return
86
82
  }
87
83
 
88
84
  //4. get info
89
- // console.log('#### auth success' )
85
+ // console.log('#### auth success' )
90
86
 
91
87
  //5. check duplicate login.
92
88
  // current policy. Deny duplicate login
@@ -95,74 +91,74 @@ export class BohoAuth{
95
91
  // 1. send clear_auth signal to the old connection.
96
92
  // 2. close old connection.
97
93
  // 3. close new connection. retry from begining.
98
- if( peer.manager.cid2remote.has( authInfo.cid ) ){
99
- let old = peer.manager.cid2remote.get(authInfo.cid )
100
- if( old == peer ){
94
+ if (peer.manager.cid2remote.has(authInfo.cid)) {
95
+ let old = peer.manager.cid2remote.get(authInfo.cid)
96
+ if (old == peer) {
101
97
  console.log('## trying RELOGIN with SAME ID. ignored.')
102
98
  return
103
99
  }
104
- console.log('### clear_auth and close old connection:', old.cid )
105
- old.send( Buffer.from( [ IOMsg.SERVER_CLEAR_AUTH ]))
106
- old.close()
100
+ console.log('### clear_auth and close old connection:', old.cid)
101
+ old.send(Buffer.from([IOMsg.SERVER_CLEAR_AUTH]))
102
+ old.close()
107
103
  peer.close() // auto relogin
108
104
  return
109
105
  }
110
106
 
111
107
 
112
108
  //6. delete current (temp rand)cid if exist.
113
- if( peer.cid ){
114
- peer.manager.cid2remote.delete( peer.cid )
109
+ if (peer.cid) {
110
+ peer.manager.cid2remote.delete(peer.cid)
115
111
  }
116
112
 
117
113
  //7. setting info.
118
114
  peer.did = id
119
115
  peer.cid = authInfo.cid
120
- peer.nick = authInfo.cid // temporary: nick as cid
116
+ peer.nick = authInfo.cid // temporary: nick as cid
121
117
 
122
118
  //8. setting quota level.
123
119
  let quotaLevel = serverOption.defaultQuotaIndex;
124
- if(authInfo.level) quotaLevel = authInfo.level;
125
- quotaLevel = parseInt( quotaLevel )
126
- let newQuota = quotaTable[ quotaLevel ];
127
- if(!newQuota){
120
+ if (authInfo.level) quotaLevel = authInfo.level;
121
+ quotaLevel = parseInt(quotaLevel)
122
+ let newQuota = quotaTable[quotaLevel];
123
+ if (!newQuota) {
128
124
  let err = 'no index quotaTable for auth.level: ' + quotaLevel
129
125
  console.log('##AUTH:DATA ERROR##', err)
130
- this.send_auth_fail( peer, err );
126
+ this.send_auth_fail(peer, err);
131
127
  return
132
- }else{
128
+ } else {
133
129
  peer.level = quotaLevel;
134
130
  peer.quota = newQuota;
135
131
  }
136
132
 
137
- if( peer.level === serverOption.adminLevel){
138
- console.log('## LOGIN ADMIN CID:', peer.cid )
133
+ if (peer.level === serverOption.adminLevel) {
134
+ console.log('## LOGIN ADMIN CID:', peer.cid)
139
135
  peer.isAdmin = true;
140
136
  }
141
137
  // console.log('auth: peer.quota', peer.quota )
142
138
  // send quota.level
143
- peer.send( Buffer.from( [IOMsg.QUOTA_LEVEL, quotaLevel] ))
139
+ peer.send(Buffer.from([IOMsg.QUOTA_LEVEL, quotaLevel]))
144
140
 
145
141
  //9. set cid2remote
146
- peer.manager.cid2remote.set( peer.cid , peer )
142
+ peer.manager.cid2remote.set(peer.cid, peer)
147
143
  // console.log( 'done: cid2remote:', peer.manager.cid2remote.keys() )
148
144
  // console.log("LOGIN: ", `id: ${ peer.did}(${peer.cid})` )
149
145
  //10. send ack.
150
- peer.send( auth_ack )
151
- peer.setState( CLIENT_STATE.AUTH_READY )
152
- if( this.authLogger ){
153
- let peerInfo = `OK #${peer.ssid} cid: ${peer.cid} did:${ peer.did }`
154
- if( peer.isAdmin ) peerInfo = "#ADMIN# "+ peerInfo
155
- this.authLogger.log( peerInfo )
156
- }
146
+ peer.send(auth_ack)
147
+ peer.setState(CLIENT_STATE.AUTH_READY)
148
+ if (this.authLogger) {
149
+ let peerInfo = `OK #${peer.ssid} cid: ${peer.cid} did:${peer.did}`
150
+ if (peer.isAdmin) peerInfo = "#ADMIN# " + peerInfo
151
+ this.authLogger.log(peerInfo)
152
+ }
157
153
  return authInfo
158
154
  } catch (error) {
159
- this.send_auth_fail( peer , 'caught: unknown error' + error );
155
+ this.send_auth_fail(peer, 'caught: unknown error' + error);
160
156
  }
161
157
 
162
158
  }
163
159
 
164
160
 
165
161
 
166
-
162
+
167
163
  }
168
164
 
@@ -27,8 +27,8 @@ export function pack(payload) {
27
27
 
28
28
  } else if (payload.byteLength < 2 ** 24) { // 3bytes len
29
29
  let len4Buffer = Buffer.alloc(4)
30
- len4Buffer.writeUint32LE( payload.byteLength )
31
- let cropLen3 = len4Buffer.subarray(0,3)
30
+ len4Buffer.writeUint32LE(payload.byteLength)
31
+ let cropLen3 = len4Buffer.subarray(0, 3)
32
32
  return MBP.pack(
33
33
  MBP.MB('#type', '8', CongType.TYPE_LEN3),
34
34
  MBP.MB('#payloadLen3', cropLen3),
@@ -45,12 +45,9 @@ export function pack(payload) {
45
45
 
46
46
  }
47
47
 
48
- // let totalCong = 0;
49
-
50
48
  export class CongRx extends Transform {
51
49
  constructor(options) {
52
50
  super(options)
53
- // console.log('new congRx. totalCong:', ++totalCong )
54
51
  this.buffer = Buffer.alloc(0)
55
52
  this.frames = []
56
53
  this.rxi = 0;
@@ -63,7 +60,6 @@ export class CongRx extends Transform {
63
60
  this.addData(chunk)
64
61
  if (this.frames.length > 0) {
65
62
  this.frames.forEach(frame => {
66
- // console.log('emit frame:', frame)
67
63
  this.push(frame)
68
64
  })
69
65
  this.frames = []
@@ -73,12 +69,11 @@ export class CongRx extends Transform {
73
69
 
74
70
 
75
71
  addData(chunk) {
76
- // console.log('congpack chunk:', chunk )
77
72
  let c = chunk.byteLength;
78
73
  let i = 0;
79
- while( c-- ){
74
+ while (c--) {
80
75
  this.rxi++
81
- if( chunk[ i++ ] == 0 ){
76
+ if (chunk[i++] == 0) {
82
77
  this.rxi_zero++
83
78
  }
84
79
  }
@@ -88,7 +83,6 @@ export class CongRx extends Transform {
88
83
  } else {
89
84
  this.buffer = chunk
90
85
  }
91
- // console.log('buffer before parse', this.buffer )
92
86
  this.parse()
93
87
  }
94
88
 
@@ -97,32 +91,29 @@ export class CongRx extends Transform {
97
91
  let head = this.buffer[0]
98
92
  let headerLen;
99
93
  let payloadSize;
100
- // find header
101
- // console.log('>> parser head, buffer:', head , this.buffer )
102
94
 
103
95
  if (head == CongType.TYPE_LEN1) {
104
- headerLen = 2;
96
+ headerLen = 2;
105
97
  if (this.buffer.byteLength < headerLen) return;
106
- payloadSize = this.buffer.readUint8(1)
98
+ payloadSize = this.buffer.readUint8(1)
107
99
 
108
100
  } else if (head == CongType.TYPE_LEN2) {
109
- headerLen = 3;
101
+ headerLen = 3;
110
102
  if (this.buffer.byteLength < headerLen) return;
111
- payloadSize = this.buffer.readUint16LE(1)
103
+ payloadSize = this.buffer.readUint16LE(1)
112
104
 
113
105
  } else if (head == CongType.TYPE_LEN3) {
114
- headerLen = 4;
106
+ headerLen = 4;
115
107
  if (this.buffer.byteLength < headerLen) return;
116
- payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536
108
+ payloadSize = this.buffer.readUint16LE(1) + this.buffer.readUint8(3) * 65536
117
109
 
118
110
  } else if (head == CongType.TYPE_LEN4) {
119
- headerLen = 5;
111
+ headerLen = 5;
120
112
  if (this.buffer.byteLength < headerLen) return;
121
- payloadSize = this.buffer.readUint32LE(1)
113
+ payloadSize = this.buffer.readUint32LE(1)
122
114
 
123
115
  } else {
124
- // console.log('CongPacket: UNKNOWN_HEAD drop', this.buffer)
125
- this.emit('wrong',this.buffer)
116
+ this.emit('wrong', this.buffer)
126
117
  this.buffer = Buffer.alloc(0)
127
118
  }
128
119
 
@@ -135,8 +126,8 @@ export class CongRx extends Transform {
135
126
  this.frames.push(this.buffer.subarray(headerLen, headerLen + payloadSize))
136
127
  this.buffer = this.buffer.subarray(headerLen + payloadSize)
137
128
  this.parse()
138
- } else {
139
- //not ready
129
+ } else {
130
+ // not ready
140
131
  // console.log('+')
141
132
  return
142
133
  }
@@ -1,12 +1,12 @@
1
1
  import { IOCore } from "./IOCore.js"
2
2
  import { pack, CongRx } from './CongPacket.js'
3
- import net from'net'
3
+ import net from 'net'
4
4
 
5
5
 
6
- export class IOCongSocket extends IOCore{
7
- constructor( url ) {
8
- super( url );
9
- if(url) this.open();
6
+ export class IOCongSocket extends IOCore {
7
+ constructor(url) {
8
+ super(url);
9
+ if (url) this.open();
10
10
  }
11
11
 
12
12
  close() {
@@ -19,68 +19,61 @@ export class IOCongSocket extends IOCore{
19
19
  clearInterval(this.connectionCheckerIntervalID);
20
20
  this.connectionCheckerIntervalID = null
21
21
  }
22
-
22
+
23
23
  keepAlive() {
24
24
  let state = this.socket?.readyState;
25
- if ( !this.socket || !(state === 'open' || state === 'opening') ) {
25
+ if (!this.socket || !(state === 'open' || state === 'opening')) {
26
26
  this.open();
27
- }
27
+ }
28
28
  }
29
29
 
30
30
 
31
- createConnection(url){
31
+ createConnection(url) {
32
32
  // TCP Socket
33
- let urlObj = new URL( url )
34
- // console.log(urlObj)
35
- if( urlObj.protocol != "cong:"){
36
- urlObj = new URL( 'cong://'+ url )
33
+ let urlObj = new URL(url)
34
+ if (urlObj.protocol != "cong:") {
35
+ urlObj = new URL('cong://' + url)
37
36
  }
38
- // console.log('congSocket url:', urlObj )
39
- this.socket = net.createConnection( urlObj.port, urlObj.hostname )
37
+ this.socket = net.createConnection(urlObj.port, urlObj.hostname)
40
38
  this.stateChange('opening')
41
39
 
42
- this.socket.on('connect' , ()=>{
43
- // console.log('cong connected' )
40
+ this.socket.on('connect', () => {
44
41
  this.congRx = new CongRx();
45
- this.socket.pipe( this.congRx )
42
+ this.socket.pipe(this.congRx)
46
43
  this.congRx.on("data", this.onTCPSocketMessage.bind(this));
47
- this.emit('open')
44
+ this.emit('open')
48
45
  })
49
46
 
50
- this.socket.on('error', e=>{
47
+ this.socket.on('error', e => {
51
48
  this.emit('error', e)
52
49
  })
53
50
 
54
- this.socket.on('close', ()=>{
51
+ this.socket.on('close', () => {
55
52
  this.emit('close');
56
53
  })
57
54
 
58
55
  }
59
56
 
60
- onTCPSocketMessage( data ) {
57
+ onTCPSocketMessage(data) {
61
58
  this.rxCounter++;
62
59
  this.rxBytes += data.byteLength
63
60
  this.lastTxRxTime = Date.now();
64
- // console.log('>> congRx data.len:', data.byteLength )
65
- // console.log('>> cong.rxi, rxi_zero:', this.congRx.rxi, this.congRx.rxi_zero )
66
- this.emit('socket_data', data );
67
-
61
+ this.emit('socket_data', data);
62
+
68
63
  }
69
64
 
70
- socket_send(data) {
71
- if( this.socket?.readyState === 'open'){
72
- // console.log('tcp send raw payload:', data)
65
+ socket_send(data) {
66
+ if (this.socket?.readyState === 'open') {
73
67
  let packData = pack(data);
74
- // console.log('tcp send cong packed:', packData)
75
- this.socket.write( packData )
68
+ this.socket.write(packData)
76
69
  this.txCounter++;
77
70
  this.txBytes += packData.byteLength;
78
71
  this.lastTxRxTime = Date.now();
79
- }else{
72
+ } else {
80
73
  console.log('.')
81
74
  }
82
75
  }
83
-
76
+
84
77
  }
85
78
 
86
79
 
@@ -120,7 +120,6 @@ export class IOCore extends EventEmitter {
120
120
  }
121
121
  this.useAuth = true
122
122
  let auth_pack = this.boho.auth_req()
123
- // console.log('auth_req_pack', auth_pack )
124
123
  this.send(auth_pack)
125
124
  }
126
125
 
@@ -144,27 +143,21 @@ export class IOCore extends EventEmitter {
144
143
  }
145
144
 
146
145
  onData(buffer) {
147
- // console.log('remote rcv socket_message', buffer )
148
- //check first byte (remote message type)
149
146
  let msgType = buffer[0];
150
147
  let decoded;
151
148
 
152
149
  if (msgType === BohoMsg.ENC_488) {
153
150
  decoded = this.boho.decrypt_488(buffer)
154
151
  if (decoded) {
155
- // console.log( decoded )
156
152
  msgType = decoded[0]
157
153
  buffer = decoded
158
- // console.log('DECODED MsgType:', IOMsg[ msgType ] )
159
154
  } else {
160
155
  // console.log('DEC_FAIL', buffer.byteLength)
161
156
  }
162
157
  } else if (msgType === BohoMsg.ENC_E2E) {
163
- // console.log('rcv ENC_E2E' )
164
158
 
165
159
  try {
166
160
  decoded = this.boho.decrypt_488(buffer)
167
- //헤더를 읽고 헤더크기만큼만 해석한다.
168
161
  if (decoded) {
169
162
  // console.log( 'ENC_E2E decoded ', decoded )
170
163
  msgType = decoded[0]
@@ -217,9 +210,9 @@ export class IOCore extends EventEmitter {
217
210
 
218
211
  case IOMsg.CID_RES:
219
212
  let cidStr = decoder.decode(buffer.subarray(1))
220
- // console.log( '>> CID_RES: ' ,cidStr )
221
213
  this.cid = cidStr;
222
- // change state before subscribe.
214
+
215
+ // **IMPORTANT** change state before subscribe.
223
216
  this.stateChange('ready', 'cid_ready')
224
217
  this.subscribe_memory_channels()
225
218
  break;
@@ -271,7 +264,6 @@ export class IOCore extends EventEmitter {
271
264
  try {
272
265
  let str = decoder.decode(buffer.subarray(1))
273
266
  let ss = JSON.parse(str)
274
- // console.log('SERVER_SIGNAL', JSON.stringify(ss))
275
267
 
276
268
  if (ss.event && ss.data) {
277
269
  this.serverSet = ss.data;
@@ -287,7 +279,6 @@ export class IOCore extends EventEmitter {
287
279
  try {
288
280
  let setPack = MBP.unpack(buffer)
289
281
  if (setPack) {
290
- // console.log('[SET] topic: ', setPack.topic)
291
282
  this.emit(setPack.topic, ...setPack.args)
292
283
  }
293
284
  } catch (error) {
@@ -312,12 +303,12 @@ export class IOCore extends EventEmitter {
312
303
  */
313
304
  switch (payloadType) {
314
305
 
315
- case PAYLOAD_TYPE.EMPTY: // 0
306
+ case PAYLOAD_TYPE.EMPTY:
316
307
  if (tag.indexOf('@') === 0) this.emit('@', null, tag)
317
308
  else this.emit(tag, null, tag)
318
309
  break;
319
310
 
320
- case PAYLOAD_TYPE.TEXT: // 1
311
+ case PAYLOAD_TYPE.TEXT:
321
312
  // !! Must remove null char before decode in JS.
322
313
  // string payload contains null char for the c/cpp devices.
323
314
  let payloadStringWithoutNull = payloadBuffer.subarray(0, payloadBuffer.byteLength - 1)
@@ -326,7 +317,7 @@ export class IOCore extends EventEmitter {
326
317
  if (tag !== '@') this.emit(tag, oneString, tag)
327
318
  break;
328
319
 
329
- case PAYLOAD_TYPE.BINARY: // 2
320
+ case PAYLOAD_TYPE.BINARY:
330
321
  if (tag.indexOf('@') === 0) this.emit('@', payloadBuffer, tag)
331
322
  if (tag !== '@') this.emit(tag, payloadBuffer, tag)
332
323
  break;
@@ -512,35 +503,26 @@ export class IOCore extends EventEmitter {
512
503
  setMsgPromise(mid) {
513
504
  return new Promise((resolve, reject) => {
514
505
  this.promiseMap.set(mid, [resolve, reject])
515
- // console.log('set promise. mid, size', mid, this.promiseMap.size)
516
506
  setTimeout(e => {
517
507
  if (this.promiseMap.has(mid)) {
518
508
  reject('timeout');
519
509
  this.promiseMap.delete(mid)
520
- // console.log('promise timeout. mid, size:', mid, this.promiseMap.size)
521
510
  }
522
511
  }, this.promiseTimeOut);
523
512
  })
524
513
  }
525
514
 
526
515
  testPromise(buffer) {
527
- // console.log('mbp buffer : ', buffer , buffer.byteLength)
528
- // let mbp = ( buffer.byteLength > 4 ) ? buffer.subarray(4) : ""
529
516
 
530
517
  let res = MBP.unpack(buffer)
531
518
  if (!res) return
532
- // console.log( res )
533
-
534
- // console.log(`RESPONSE_MBP MID: ${mid} status: ${status} ,mbp: ${ buffer.subarray(4)} `)
535
519
 
536
520
  if (this.promiseMap.has(res.mid)) {
537
- // console.log('res promise msg', mid)
538
521
  let [resolve, reject] = this.promiseMap.get(res.mid)
539
522
  this.promiseMap.delete(res.mid)
540
523
 
541
524
  if (res.status < 128) {
542
525
  res.ok = true;
543
- // console.log( 'unpack meta:', meta)
544
526
  resolve(res)
545
527
  } else {
546
528
  res.ok = false;
@@ -611,7 +593,6 @@ export class IOCore extends EventEmitter {
611
593
 
612
594
 
613
595
  req(target, topic, ...args) {
614
- // console.log('common_req args', args)
615
596
  if (!target || !topic)
616
597
  return Promise.reject(new Error('request need target and topic)'))
617
598
  let sigPack;
@@ -631,7 +612,6 @@ export class IOCore extends EventEmitter {
631
612
  MBP.MB('topic', topic)
632
613
  )
633
614
  }
634
- // console.log('<< adminPack', this.mid, sigPack)
635
615
  this.send_enc_mode(sigPack)
636
616
  return this.setMsgPromise(this.mid)
637
617
  }
@@ -659,7 +639,6 @@ export class IOCore extends EventEmitter {
659
639
  subscribe_promise(tag) {
660
640
  if (typeof tag !== 'string') throw TypeError('tag should be string.')
661
641
  if (this.state !== STATES.READY) {
662
- // console.log('not ready state:', this.state )
663
642
  return Promise.reject('subscribe_promise:: connection is not ready')
664
643
  }
665
644
 
@@ -678,7 +657,6 @@ export class IOCore extends EventEmitter {
678
657
  subscribe_memory_channels() { //local cache . auto_resubscribe
679
658
  if (this.channels.size == 0) return
680
659
  let chList = Array.from(this.channels).join(',')
681
- // console.log('<< subscibe memory channels by cid', chList , this.cid )
682
660
 
683
661
  this.subscribe_promise(chList)
684
662
  .then((res) => {
@@ -690,11 +668,9 @@ export class IOCore extends EventEmitter {
690
668
  }
691
669
 
692
670
  unsubscribe(tag = "") {
693
- // console.log('unsub', tag)
694
671
  if (typeof tag !== 'string') throw TypeError('tag should be string.')
695
672
 
696
- if (tag == "") {
697
- // console.log('unsub all')
673
+ if (tag == "") { // blank tag means unsubscribe all
698
674
  this.channels.clear();
699
675
  } else {
700
676
  let tagList = tag.split(',')
@@ -721,7 +697,6 @@ export class IOCore extends EventEmitter {
721
697
  if (tag.indexOf('@') !== 0) {
722
698
  this.channels.add(tag)
723
699
  }
724
- // console.log('channels:', this.channels )
725
700
  this.on(tag, handler)
726
701
  // do not subscribe now.
727
702
  // will subscribe when receive CID_RES signal from server.
@@ -751,7 +726,6 @@ export class IOCore extends EventEmitter {
751
726
  this.linkMap.set(to, linkSet)
752
727
  this.on(tag, handler)
753
728
  this.subscribe(tag)
754
- // console.log('link [to] linkMap:', to, this.linkMap )
755
729
 
756
730
  }
757
731
 
@@ -775,7 +749,6 @@ export class IOCore extends EventEmitter {
775
749
  }
776
750
  }
777
751
 
778
- // console.log('unlink linkMap result:', this.linkMap )
779
752
  }
780
753
 
781
754
  unlinkAll(to) {
@@ -791,7 +764,6 @@ export class IOCore extends EventEmitter {
791
764
  }
792
765
  this.linkMap.delete(to)
793
766
 
794
- // console.log('unlinkAll linkMap result:', this.linkMap )
795
767
  }
796
768
 
797
769
 
@@ -838,7 +810,6 @@ export class IOCore extends EventEmitter {
838
810
  if (emitEventAndMessage) this.emit(eventName, emitEventAndMessage)
839
811
 
840
812
  if (this.stateName !== eventName) {
841
- // console.log(`state: ${this.stateName} => ${eventName}` )
842
813
  this.stateName = eventName
843
814
  this.emit('change', eventName)
844
815
  }