iosignal 1.3.1 → 1.4.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iosignal",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "iosignal",
5
5
  "type": "module",
6
6
  "main": "./dist/iosignal.cjs",
@@ -26,7 +26,7 @@
26
26
  "start": "node ./test-browser/server.js"
27
27
  },
28
28
  "engines": {
29
- "node": ">=18.19.0"
29
+ "node": ">=15.0.0"
30
30
  },
31
31
  "author": {
32
32
  "name": "Lee Dongeun",
@@ -37,7 +37,7 @@
37
37
  "boho": "^0.8.2",
38
38
  "commander": "^9.5.0",
39
39
  "eventemitter3": "^5.0.1",
40
- "meta-buffer-pack": "^1.3.1",
40
+ "meta-buffer-pack": "^1.4.0",
41
41
  "ws": "^8.16.0"
42
42
  },
43
43
  "optionalDependencies": {
@@ -50,6 +50,11 @@
50
50
  },
51
51
  "homepage": "https://iosignal.net",
52
52
  "keywords": [
53
- "io","pubsub","arduino","iot","auth","signal"
53
+ "io",
54
+ "pubsub",
55
+ "arduino",
56
+ "iot",
57
+ "auth",
58
+ "signal"
54
59
  ]
55
60
  }
@@ -346,7 +346,7 @@ export class IOCore extends EventEmitter {
346
346
  break;
347
347
 
348
348
  case PAYLOAD_TYPE.MBA:
349
- let mbaObject = MBP.unpack(buffer)
349
+ let mbaObject = MBP.unpack(payloadBuffer)
350
350
  if (tag.indexOf('@') === 0) this.emit('@', ...mbaObject.args, tag)
351
351
  if (tag !== '@') this.emit(tag, ...mbaObject.args, tag)
352
352
  break;
@@ -17,12 +17,13 @@ export function getSignalPack( tag, ...args ){
17
17
  MBP.MB('#payloadType', '8', payload.type )
18
18
  )
19
19
  }else if( payload.type == PAYLOAD_TYPE.MBA ){
20
+ let mbaBuffer = MBP.pack( MBP.MBA(...args))
20
21
  sigPack = MBP.pack(
21
22
  MBP.MB('#MsgType','8', IOMsg.SIGNAL) ,
22
23
  MBP.MB('#tagLen','8', tagEncoded.byteLength),
23
24
  MBP.MB('#tag', tagEncoded),
24
25
  MBP.MB('#payloadType', '8', payload.type ),
25
- MBP.MBA(...args)
26
+ MBP.MB('#mbaBuffer', mbaBuffer )
26
27
  )
27
28
  }else {
28
29
  sigPack = MBP.pack(
@@ -74,4 +75,9 @@ export function parsePayload( args){
74
75
  }
75
76
 
76
77
  return { type: type, buffer: pack }
78
+ }
79
+
80
+ export function getPayloadFromSignalPack( signalPack ){
81
+ let tagLen = signalPack.readUint8(1)
82
+ return signalPack.subarray(3 + tagLen)
77
83
  }
@@ -50,6 +50,13 @@ export let quotaTable = {
50
50
 
51
51
  // you can add your custom quota level.
52
52
 
53
+ // Remote service node
54
+ // High Quota, No root permission.
55
+ 200: {
56
+ signalSize: 1048576 * 20,
57
+ publishCounter: 10000,
58
+ trafficRate: 1048576 * 100
59
+ },
53
60
  // super admin or root user.
54
61
  // to monitor, metric, sudo command, db acess
55
62
  255: {
@@ -141,7 +141,7 @@ export class Manager{
141
141
  }
142
142
 
143
143
 
144
-
144
+ // unix wall like
145
145
  serverSignal(obj) {
146
146
  let sigPack = MBP.pack(
147
147
  MBP.MB('#MsgType', '8', IOMsg.SERVER_SIGNAL),
@@ -188,6 +188,21 @@ export class Manager{
188
188
  }
189
189
  }
190
190
 
191
+ serverSignalTo( tag, ...args ){
192
+ let cid = tag.split('@')[0]
193
+ let topic = tag.split('@')[1]
194
+ let sigPack = getSignalPack( '@'+topic, ...args )
195
+ // console.log('tag split', cid )
196
+ if (cid && this.cid2remote.has(cid)) {
197
+ let targetRemote = this.cid2remote.get(cid)
198
+ console.log('target', targetRemote.state ,targetRemote.cid )
199
+ targetRemote.send_enc_mode(sigPack)
200
+ return
201
+ }else{
202
+ return "no cid"
203
+ }
204
+ }
205
+
191
206
  sender(tag, remote, message) {
192
207
  if(serverOption.memberOnly && !remote.boho.isAuthorized){
193
208
  // console.log("## MemberOnly, reject unAuthorized remote." )
@@ -200,7 +215,7 @@ export class Manager{
200
215
 
201
216
  let cidIndex = tag.indexOf('@');
202
217
  if (cidIndex === 0) {
203
- // ** CID_PUB is not uni-cast but multic-cast.
218
+ // ** CID_PUB multi-cast
204
219
  // [cid_pub] @topic , @$retainTopic
205
220
  // modify signalpack with cid_appneded tag.
206
221
  tag = remote.cid + tag;
@@ -214,6 +229,7 @@ export class Manager{
214
229
  if (this.cid2remote.has(targetCId)) {
215
230
  //rm cid from tag.
216
231
  let ommitCIdTag = '@' + tag.split('@')[1]
232
+ // console.log('uni-cast tag change from', tag, 'to' , ommitCIdTag )
217
233
  message = this.getNewSignalTagMessage(message, ommitCIdTag)
218
234
  // console.log(`origin tag: ${tag} omitTag: ${this.getSignalTag(message)}`)
219
235
  this.cid2remote.get(targetCId).send_enc_mode(message)
@@ -25,7 +25,7 @@ export class ServerRemote extends ServerRemoteCore {
25
25
 
26
26
  if( util.isPrivateIP(this.ip)){
27
27
  // connection from local or private network has default private channel.
28
- this.HOME_CHANNEL = 'PRIVATE:' //this.manager.getPrivateChannel(this)
28
+ this.HOME_CHANNEL = 'PRIVATE:'
29
29
  this.privateNode = true
30
30
  }else{
31
31
  // client have default channel name as global IP address.
@@ -62,14 +62,12 @@ export class ServerRemote extends ServerRemoteCore {
62
62
 
63
63
  socket.on('error', e=>{ console.log('TCP Socket error',e )})
64
64
  socket.on('close', e=>{
65
- // console.log('cong socket close event')
66
65
  this.manager.removeRemote(this);
67
66
  })
68
67
 
69
68
  }
70
-
71
-
72
69
  }
70
+
73
71
  onWrongCongPacketMessage( message){
74
72
  /*
75
73
  * three category CongSocket connection.
@@ -104,12 +102,10 @@ export class ServerRemote extends ServerRemoteCore {
104
102
  }
105
103
  }
106
104
 
107
- //
108
- permissionChecker(){
109
-
105
+
106
+ permissionChecker(){
110
107
  // type1.
111
108
  // accept some traffic before authorize.
112
-
113
109
  const limitBytes = 100
114
110
 
115
111
  let txBytes = this.socket.bytesWritten || this.socket._socket?.bytesWritten
@@ -117,10 +113,8 @@ export class ServerRemote extends ServerRemoteCore {
117
113
 
118
114
  if( txBytes == undefined) txBytes = 0
119
115
  if( rxBytes == undefined) rxBytes = 0
120
-
121
116
  // console.log( txBytes, rxBytes )
122
117
 
123
-
124
118
  }
125
119
 
126
120
 
@@ -136,7 +130,7 @@ export class ServerRemote extends ServerRemoteCore {
136
130
  return req.headers["x-forwarded-proto"]; //https or undefined
137
131
  }
138
132
 
139
- // 정리요함
133
+
140
134
  // nginx https reversproxy , http redirect , direct tcp
141
135
  getRemoteIP(req) { //req or ip string
142
136
  let ip;
@@ -154,11 +148,9 @@ export class ServerRemote extends ServerRemoteCore {
154
148
  ip = "0.0.0.0";
155
149
  }
156
150
  // console.log('getRemoteIP', ip )
157
-
158
151
  return ip;
159
152
  }
160
153
 
161
-
162
154
  ping(){
163
155
  if(this.socketType === 'websocket'){
164
156
  this.socket.ping()
@@ -73,19 +73,19 @@ export class ServerRemoteCore {
73
73
 
74
74
 
75
75
  showMessageLog(message, isBinary) {
76
- let from = this.boho.isAuthorized ? ` id: ${this.did}(${this.cid})` : ""
76
+ let from = this.boho.isAuthorized ? `did: ${this.did} ${this.cid}@` : `${this.cid}@`
77
77
  if (isBinary) {
78
78
  let msgTypeName = IOMsg[message[0]]
79
79
  if (!msgTypeName) msgTypeName = BohoMsg[message[0]]
80
- msgTypeName = '#' + this.ssid + '(' + this.cid + ') [ ' + msgTypeName + ' ]';
80
+ msgTypeName =' [' + msgTypeName + ']';
81
81
  if (message.byteLength > 40) {
82
- console.log(msgTypeName + from + ' LEN:', message.length);
82
+ console.log(from + msgTypeName + ' LEN:', message.length);
83
83
  } else {
84
- console.log(msgTypeName + from, message);
84
+ console.log(from + msgTypeName , message);
85
85
  }
86
86
 
87
87
  } else {
88
- console.log(from + '[TEXT MSG] %s', message);
88
+ console.log(from + ' [TEXT] %s', message);
89
89
  }
90
90
  }
91
91
 
@@ -0,0 +1,34 @@
1
+ import { parsePayload, getSignalPack, getPayloadFromSignalPack, PAYLOAD_TYPE, MBP } from 'iosignal'
2
+
3
+ const type0 = []
4
+ const type1t = ['a1']
5
+ const type1n = [11]
6
+ const type2 = [new Uint8Array(5)]
7
+ const type3 = [{ name: 'type3' }]
8
+ const type4a = ['a2', 222]
9
+ const type4b = [{ name: 'a3' }, 333]
10
+ const type5 = [{ a: 1 }, Buffer.from('BBBBBB')]
11
+
12
+ // zero or one argument support: 0:EMPTY, 1:TEXT , 2:BINARY signal payload type
13
+ console.log(PAYLOAD_TYPE[parsePayload(type0).type], type0) // empty
14
+ console.log(PAYLOAD_TYPE[parsePayload(type1t).type], type1t) // text
15
+ console.log(PAYLOAD_TYPE[parsePayload(type1n).type], type1n) // number text. there is no Number type of signalPaylod.
16
+ console.log(PAYLOAD_TYPE[parsePayload(type2).type], type2) // binary.
17
+
18
+ // over two arguments support: 3:Object, 4:MJSON and 5:MBA can hold Number item.
19
+ console.log(PAYLOAD_TYPE[parsePayload(type3).type], type3) // Object
20
+ console.log(PAYLOAD_TYPE[parsePayload(type4a).type], type4a) // MJSON
21
+ console.log(PAYLOAD_TYPE[parsePayload(type4b).type], type4b) // MJSON
22
+ console.log(PAYLOAD_TYPE[parsePayload(type5).type], type5) // MBA
23
+
24
+
25
+ let signalTag = 'channel'
26
+ let signalPack = getSignalPack( signalTag, ...type5)
27
+ console.log('signalPack', signalPack)
28
+
29
+ let payloadBuffer = getPayloadFromSignalPack(signalPack)
30
+ let unpack = MBP.unpack(payloadBuffer)
31
+ console.log(unpack)
32
+
33
+
34
+
@@ -0,0 +1,61 @@
1
+ import { IO, Server} from 'iosignal'
2
+
3
+ let url = 'ws://localhost:5555'
4
+ let server = new Server({
5
+ port: 5555
6
+ // ,showMessage: 'message'
7
+ })
8
+ let done = false;
9
+
10
+ let tx = new IO(url)
11
+ tx.on('ready', () => {
12
+ console.log('tx ready', tx.cid)
13
+ })
14
+
15
+
16
+ let rx = new IO(url)
17
+ rx.on('ready', () => {
18
+ console.log('rx ready', rx.cid)
19
+ })
20
+
21
+ rx.listen('@', (...args) => {
22
+ console.log('uni-cast rxi', args)
23
+ })
24
+
25
+ rx.listen('channel', (...args) => {
26
+ console.log('multi-cast rxi', args)
27
+ })
28
+
29
+ function sendSignals() {
30
+ // uni-casting: tag is receiver CID@.
31
+ let tag = rx.cid + '@topic'
32
+ tx.signal(tag, ...arg1)
33
+ tx.signal(tag, ...arg2)
34
+ tx.signal(tag, ...arg3)
35
+ tx.signal(tag, ...arg4)
36
+ tx.signal(tag, ...arg5)
37
+
38
+ // multi-casting: tag is channel name.
39
+ tag = 'channel'
40
+ tx.signal(tag, ...arg1)
41
+ tx.signal(tag, ...arg2)
42
+ tx.signal(tag, ...arg3)
43
+ tx.signal(tag, ...arg4)
44
+ tx.signal(tag, ...arg5)
45
+ }
46
+
47
+
48
+ const arg1 = ['a1']
49
+ const arg2 = ['a2', 222]
50
+ const arg3 = [{ name: 'a3' }, 333]
51
+ const arg4 = [{a:1}, Buffer.from('KKKKK')]
52
+ const arg5 = [new Uint8Array(20)]
53
+
54
+
55
+ setTimeout(() => {
56
+ sendSignals();
57
+ }, 1000)
58
+
59
+ setTimeout(() => {
60
+ process.exit()
61
+ }, 2000)