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,5 +1,5 @@
1
1
  import { IO } from './src/client/IOWebSocket.js'
2
- import { Boho, RAND, MBP, BohoMsg, Meta, MetaSize , sha256, Buffer } from 'boho'
2
+ import { Boho, RAND, MBP, BohoMsg, Meta, MetaSize, sha256, Buffer } from 'boho'
3
3
 
4
4
  Boho.RAND = RAND;
5
5
  Boho.BohoMsg = BohoMsg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iosignal",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "iosignal",
5
5
  "type": "module",
6
6
  "main": "./dist/iosignal.cjs",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "homepage": "https://iosignal.net",
51
51
  "keywords": [
52
- "io",
52
+ "websocket",
53
53
  "pubsub",
54
54
  "arduino",
55
55
  "iot",
@@ -2,18 +2,18 @@ import { STATUS } from './api_constant.js'
2
2
 
3
3
  const MIN_LEVEL = 200;
4
4
  const COMMANDS = [
5
- 'GET', 'SET',
6
- 'HGETALL', 'HGET',
7
- 'HSET', 'SADD',
5
+ 'GET', 'SET',
6
+ 'HGETALL', 'HGET',
7
+ 'HSET', 'SADD',
8
8
  'SISMEMBER', 'SMEMBERS',
9
- 'EXISTS', 'SREM',
10
- 'DEL', 'KEYS',
9
+ 'EXISTS', 'SREM',
10
+ 'DEL', 'KEYS',
11
11
  'SAVE'
12
12
  ]
13
13
 
14
- export class RedisAPI{
15
- constructor( redisClient, _minLevel ){
16
- if( !redisClient ){
14
+ export class RedisAPI {
15
+ constructor(redisClient, _minLevel) {
16
+ if (!redisClient) {
17
17
  throw new Error("RedisAPI constructor: no redisClient")
18
18
  }
19
19
  this.redisClient = redisClient
@@ -22,47 +22,44 @@ export class RedisAPI{
22
22
  }
23
23
 
24
24
 
25
- checkPermission(remote ,req){
26
- console.log('checkPermission', remote.level , 'vs', this.minLevel )
27
- if( remote.level >= this.minLevel ){
25
+ checkPermission(remote, req) {
26
+ console.log('checkPermission', remote.level, 'vs', this.minLevel)
27
+ if (remote.level >= this.minLevel) {
28
28
  return true
29
- }else{
29
+ } else {
30
30
  return false
31
31
  }
32
32
  }
33
33
 
34
34
 
35
- async request(remote, req ){
35
+ async request(remote, req) {
36
36
 
37
37
  let result;
38
- let status = STATUS.OK; // *0~127: ok , 128~*255 :error
38
+ let status = STATUS.OK;
39
39
  try {
40
- // console.log(req)
41
40
  let cmd = req.topic
42
41
  cmd = cmd.toUpperCase()
43
- if( COMMANDS.includes(cmd)){
42
+ if (COMMANDS.includes(cmd)) {
44
43
  console.log('RedisAPI call:', cmd, req.args)
45
44
 
46
- if( req.args.length > 0 ){
47
- result = await this.redisClient[cmd]( ...req.args )
48
- }else{
45
+ if (req.args.length > 0) {
46
+ result = await this.redisClient[cmd](...req.args)
47
+ } else {
49
48
  result = await this.redisClient[cmd]()
50
49
  }
51
50
 
52
- // console.log('RedisAPI: result:', result)
53
51
 
54
- }else{
55
- // result = 'RedisAPI: no such a cmd '+ cmd;
52
+ } else {
56
53
  status = STATUS.ERROR;
57
54
  }
58
- remote.response( req.mid, status , result)
55
+ remote.response(req.mid, status, result)
59
56
  } catch (e) {
60
- remote.response( req.mid, STATUS.ERROR ,e.message )
57
+ remote.response(req.mid, STATUS.ERROR, e.message)
61
58
  }
62
59
 
63
60
  }
64
61
 
65
62
  }
66
63
 
67
-
68
-
64
+
65
+
@@ -4,31 +4,28 @@ import { STATUS } from './api_constant.js'
4
4
 
5
5
  const MIN_LEVEL = 0;
6
6
 
7
- export function checkPermission(remote){
8
- if( remote.level >= MIN_LEVEL ){
7
+ export function checkPermission(remote) {
8
+ if (remote.level >= MIN_LEVEL) {
9
9
  return true
10
- }else{
10
+ } else {
11
11
  return false
12
12
  }
13
13
  }
14
14
 
15
- export async function echo( remote , req)
16
- {
17
- if( !req.args)
18
- remote.response( req.mid, STATUS.ERROR , 'no message to echo' )
15
+ export async function echo(remote, req) {
16
+ if (!req.args)
17
+ remote.response(req.mid, STATUS.ERROR, 'no message to echo')
19
18
  else
20
- remote.response( req.mid, STATUS.OK , req.args )
19
+ remote.response(req.mid, STATUS.OK, req.args)
21
20
  }
22
21
 
23
- export async function date( remote , req)
24
- {
22
+ export async function date(remote, req) {
25
23
  let r = new Date().toUTCString()
26
- remote.response( req.mid, STATUS.OK , r)
24
+ remote.response(req.mid, STATUS.OK, r)
27
25
  }
28
26
 
29
- export async function unixtime( remote , req)
30
- {
31
- let r = Math.floor( Date.now() /1000)
32
- remote.response( req.mid, STATUS.OK , r)
27
+ export async function unixtime(remote, req) {
28
+ let r = Math.floor(Date.now() / 1000)
29
+ remote.response(req.mid, STATUS.OK, r)
33
30
  }
34
31
 
@@ -3,94 +3,89 @@ import { STATUS } from './api_constant.js'
3
3
  const MIN_LEVEL = 255;
4
4
 
5
5
  export const commands = [
6
- 'cid','remotes','clients',
7
- 'channels','subscribers',
6
+ 'cid', 'remotes', 'clients',
7
+ 'channels', 'subscribers',
8
8
  'remote', 'client', 'close',
9
- 'addauth' , 'getauth' ,'delauth',
9
+ 'addauth', 'getauth', 'delauth',
10
10
  'getauthidlist', 'getdevicelist',
11
- 'adddevice' ,'getdevice','deldevice'
11
+ 'adddevice', 'getdevice', 'deldevice'
12
12
  ]
13
13
 
14
14
 
15
- export function checkPermission(remote){
16
- if( remote.level >= MIN_LEVEL ){
15
+ export function checkPermission(remote) {
16
+ if (remote.level >= MIN_LEVEL) {
17
17
  return true
18
- }else{
19
- return false
18
+ } else {
19
+ return false
20
20
  }
21
21
  }
22
22
 
23
- export async function request( remote, req) {
24
- // console.log(req )
25
- let result;
26
- let status = 0;
27
- try {
28
- let cmd = req.topic;
29
- cmd = cmd.toLowerCase()
30
- if (cmd == 'cid') {
31
- result = remote.manager.metric.getCIdList()
32
- } else if (cmd == 'remotes' || cmd == 'clients') {
33
- result = remote.manager.metric.getRemotes()
34
- } else if (cmd == 'channels') {
35
- result = remote.manager.metric.getChannelList()
36
- } else if (cmd == 'subscribers') {
37
- let ch = req.$[0]
38
- if (ch) result = remote.manager.metric.getSubscribers(ch)
39
- } else if (cmd == 'remote' || cmd == 'client') {
40
- let cid = req.$[0]
41
- let mode = req.$[1]
42
- if (cid) result = remote.manager.metric.getRemoteByCId(cid, mode)
43
-
44
- } else if (cmd == 'close') {
45
- if (req.$[0]) result = remote.manager.closeRemoteByCId(req.$[0])
46
-
47
- } else if (cmd == 'addauth' || cmd == 'adddevice') {
48
- // console.log('## api_sudo: addauth ', remote.manager.authManager , req.$.length)
49
- if (remote.manager.authManager && req.$.length == 4) {
50
- if (remote.manager.authManager.addAuth) {
51
- let did = req.$[0]
52
- let dkey = req.$[1]
53
- let cid = req.$[2]
54
- let level = req.$[3]
55
- // console.log('###### did,dkey,cid,level: ', did,dkey,cid,level)
56
- result = await remote.manager.authManager.addAuth(did, dkey, cid, level)
57
- }
58
-
23
+ export async function request(remote, req) {
24
+ let result;
25
+ let status = 0;
26
+ try {
27
+ let cmd = req.topic;
28
+ cmd = cmd.toLowerCase()
29
+ if (cmd == 'cid') {
30
+ result = remote.manager.metric.getCIdList()
31
+ } else if (cmd == 'remotes' || cmd == 'clients') {
32
+ result = remote.manager.metric.getRemotes()
33
+ } else if (cmd == 'channels') {
34
+ result = remote.manager.metric.getChannelList()
35
+ } else if (cmd == 'subscribers') {
36
+ let ch = req.$[0]
37
+ if (ch) result = remote.manager.metric.getSubscribers(ch)
38
+ } else if (cmd == 'remote' || cmd == 'client') {
39
+ let cid = req.$[0]
40
+ let mode = req.$[1]
41
+ if (cid) result = remote.manager.metric.getRemoteByCId(cid, mode)
42
+
43
+ } else if (cmd == 'close') {
44
+ if (req.$[0]) result = remote.manager.closeRemoteByCId(req.$[0])
45
+
46
+ } else if (cmd == 'addauth' || cmd == 'adddevice') {
47
+ if (remote.manager.authManager && req.$.length == 4) {
48
+ if (remote.manager.authManager.addAuth) {
49
+ let did = req.$[0]
50
+ let dkey = req.$[1]
51
+ let cid = req.$[2]
52
+ let level = req.$[3]
53
+ result = await remote.manager.authManager.addAuth(did, dkey, cid, level)
59
54
  }
60
- } else if (cmd == 'delauth' || cmd == 'deldevice') {
61
- if (remote.manager.authManager && req.$.length == 1) {
62
- if (remote.manager.authManager.delAuth) {
63
- let did = req.$[0]
64
- result = await remote.manager.authManager.delAuth(did)
65
- }
55
+
56
+ }
57
+ } else if (cmd == 'delauth' || cmd == 'deldevice') {
58
+ if (remote.manager.authManager && req.$.length == 1) {
59
+ if (remote.manager.authManager.delAuth) {
60
+ let did = req.$[0]
61
+ result = await remote.manager.authManager.delAuth(did)
66
62
  }
67
- } else if (cmd == 'getauth' || cmd == 'getdevice') {
68
- if (remote.manager.authManager && req.$.length == 1) {
69
- if (remote.manager.authManager.getAuth) {
70
- let did = req.$[0]
71
- result = await remote.manager.authManager.getAuth(did)
72
- }
63
+ }
64
+ } else if (cmd == 'getauth' || cmd == 'getdevice') {
65
+ if (remote.manager.authManager && req.$.length == 1) {
66
+ if (remote.manager.authManager.getAuth) {
67
+ let did = req.$[0]
68
+ result = await remote.manager.authManager.getAuth(did)
73
69
  }
74
- } else if (cmd == 'getauthidlist' || cmd == 'getdevicelist') {
75
- if (remote.manager.authManager ) {
76
- if (remote.manager.authManager.getAuthIdList ) {
77
- result = await remote.manager.authManager.getAuthIdList()
78
- }
70
+ }
71
+ } else if (cmd == 'getauthidlist' || cmd == 'getdevicelist') {
72
+ if (remote.manager.authManager) {
73
+ if (remote.manager.authManager.getAuthIdList) {
74
+ result = await remote.manager.authManager.getAuthIdList()
79
75
  }
80
- } else{
81
- status = STATUS.ERROR;
82
- result = "api sudo: no such a cmd: " + cmd
83
76
  }
84
-
85
- // console.log('req_sudo: result', result)
86
- remote.response(req.mid, status, result)
87
-
88
- } catch (e) {
89
- // console.log('req_sudo err:',e.message)
90
- remote.response(req.mid, STATUS.ERROR , e.message )
77
+ } else {
78
+ status = STATUS.ERROR;
79
+ result = "api sudo: no such a cmd: " + cmd
91
80
  }
92
-
81
+
82
+ remote.response(req.mid, status, result)
83
+
84
+ } catch (e) {
85
+ remote.response(req.mid, STATUS.ERROR, e.message)
93
86
  }
94
87
 
88
+ }
89
+
95
90
 
96
91
 
@@ -21,19 +21,19 @@ import { BohoAuth } from './BohoAuth.js';
21
21
  import { sha256 } from 'boho'
22
22
 
23
23
  export class Auth_Env extends BohoAuth {
24
- constructor( authInfo) {
24
+ constructor(authInfo) {
25
25
  super()
26
26
  let id_keys;
27
- if( authInfo ){
27
+ if (authInfo) {
28
28
  id_keys = authInfo.split(',')
29
- }else if ( process.env.BOHO_AUTH) {
29
+ } else if (process.env.BOHO_AUTH) {
30
30
  authInfo = process.env.BOHO_AUTH
31
31
  id_keys = process.env.BOHO_AUTH.split(',')
32
- }else{
32
+ } else {
33
33
  console.log("Auth_Env: None of process.env.BOHO_AUTH or authInfo")
34
34
  process.exit()
35
35
  }
36
-
36
+
37
37
  this.AUTH = new Map();
38
38
 
39
39
  if (id_keys.length >= 1) {
@@ -44,9 +44,7 @@ export class Auth_Env extends BohoAuth {
44
44
  let level = v.split('.')[2]
45
45
  level = parseInt(level)
46
46
 
47
- // console.log('typeof level', level, typeof level)
48
-
49
- if (did && key && typeof level =='number') {
47
+ if (did && key && typeof level == 'number') {
50
48
  let cid = did;
51
49
  this.addAuth(did, key, cid, level)
52
50
  } else {
@@ -66,7 +64,7 @@ export class Auth_Env extends BohoAuth {
66
64
  return this.AUTH.get(id)
67
65
  }
68
66
  async getAuthIdList() {
69
- return Array.from( this.AUTH.keys() )
67
+ return Array.from(this.AUTH.keys())
70
68
  }
71
69
 
72
70
  addAuth(id, keyStr, cid, level = 0) {
@@ -1,72 +1,68 @@
1
- /**
2
- * Auth_File
3
- */
4
1
  import { BohoAuth } from './BohoAuth.js';
5
2
  import { readFileSync } from 'fs'
6
3
  import { sha256 } from 'boho'
7
4
  import path from 'path'
8
5
 
9
- export class Auth_File extends BohoAuth{
10
- constructor( _path ){
6
+ export class Auth_File extends BohoAuth {
7
+ constructor(_path) {
11
8
  super()
12
9
  this.AUTH = new Map();
13
10
  let pathObj = path.parse(_path)
14
11
  this.path = path.resolve(_path)
15
- console.log('auth from file path:', this.path )
12
+ console.log('auth from file path:', this.path)
16
13
  let ext = pathObj.ext;
17
- if( ext.toLowerCase() == '.js' || ext.toLowerCase() == '.mjs'){
14
+ if (ext.toLowerCase() == '.js' || ext.toLowerCase() == '.mjs') {
18
15
  // Read auth data from file
19
- console.log( "#JS path:", this.path );
20
- this.loadAuthInfoFile_JS( this.path)
21
- }else if( ext.toLowerCase() == '.json') {
22
- console.log( "#JSON path:", this.path );
16
+ console.log("#JS path:", this.path);
17
+ this.loadAuthInfoFile_JS(this.path)
18
+ } else if (ext.toLowerCase() == '.json') {
19
+ console.log("#JSON path:", this.path);
23
20
  this.loadAuthInfoFile_JSON(this.path)
24
- }else{
21
+ } else {
25
22
  console.log('no authinfofile path.')
26
23
  }
27
24
  }
28
25
 
29
26
 
30
- async getAuth( id ){
31
- return this.AUTH.get( id )
27
+ async getAuth(id) {
28
+ return this.AUTH.get(id)
32
29
  }
33
-
30
+
34
31
  async getAuthIdList() {
35
- return Array.from( this.AUTH.keys() )
32
+ return Array.from(this.AUTH.keys())
36
33
  }
37
34
 
38
35
  //loaded when server start.
39
- loadAuthInfoFile_JS(path){
36
+ loadAuthInfoFile_JS(path) {
37
+
38
+ import(path).then((file) => {
40
39
 
41
- import( path ).then(( file ) => {
42
-
43
- console.log( file.authInfo );
44
- file.authInfo.forEach( item =>{
45
- this.addAuth(...item )
40
+ console.log(file.authInfo);
41
+ file.authInfo.forEach(item => {
42
+ this.addAuth(...item)
46
43
  })
47
- console.log('total AUTH INFO size: ', this.AUTH.size )
48
- }).catch(e=>{
44
+ console.log('total AUTH INFO size: ', this.AUTH.size)
45
+ }).catch(e => {
49
46
  console.log(e)
50
47
  })
51
48
 
52
49
 
53
50
  }
54
51
 
55
- loadAuthInfoFile_JSON(path){
56
- let file = readFileSync( path )
57
- file = new TextDecoder().decode( file )
58
- // console.log(file )
59
- let list = JSON.parse( file )
60
- list.forEach( item =>{
61
- this.addAuth(...item )
52
+ loadAuthInfoFile_JSON(path) {
53
+ let file = readFileSync(path)
54
+ file = new TextDecoder().decode(file)
55
+ let list = JSON.parse(file)
56
+ list.forEach(item => {
57
+ this.addAuth(...item)
62
58
  })
63
- console.log('total AUTH INFO size: ', this.AUTH.size )
59
+ console.log('total AUTH INFO size: ', this.AUTH.size)
64
60
 
65
61
  }
66
62
 
67
- addAuth( id, keyStr , cid , level = 0){
68
- let Base64hashKey = Buffer.from( sha256.hash(keyStr)).toString('base64')
69
- this.AUTH.set( id, { key: Base64hashKey, cid: cid, level: level } )
63
+ addAuth(id, keyStr, cid, level = 0) {
64
+ let Base64hashKey = Buffer.from(sha256.hash(keyStr)).toString('base64')
65
+ this.AUTH.set(id, { key: Base64hashKey, cid: cid, level: level })
70
66
  }
71
67
 
72
68
 
@@ -1,48 +1,42 @@
1
-
2
- /**
3
- * Auth_Redis
4
- */
5
1
  import { BohoAuth } from './BohoAuth.js';
6
2
  import { sha256 } from 'boho'
7
3
  const DEVICE_PREFIX = "device:"
8
4
 
9
- export class Auth_Redis extends BohoAuth{
10
- constructor( redisClient ){
5
+ export class Auth_Redis extends BohoAuth {
6
+ constructor(redisClient) {
11
7
  super()
12
- if( !redisClient ){
8
+ if (!redisClient) {
13
9
  throw new Error("AuthRedis constructor: no redisClient")
14
10
  }
15
11
  this.redis = redisClient
16
12
  }
17
13
 
18
14
  // get device key from DB. (for Boho auth.)
19
- async getAuth( id ){
15
+ async getAuth(id) {
20
16
  let result = await this.redis.hGetAll(DEVICE_PREFIX + id)
21
- // console.log('Auth_Redis: getAuth req id result',id, result)
22
- if(result.key) return result
17
+ if (result.key) return result
23
18
  }
24
19
 
25
20
 
26
21
  async getAuthIdList() {
27
- let result = await this.redis.keys( DEVICE_PREFIX + '*')
28
- result = result.map( v=> {
22
+ let result = await this.redis.keys(DEVICE_PREFIX + '*')
23
+ result = result.map(v => {
29
24
  return v.split(':')[1]
30
25
  })
31
- if(result) return result
26
+ if (result) return result
32
27
  }
33
28
 
34
- // add device auth info
35
- async addAuth( id, keyStr , cid = '', level = 0){
36
- // console.log('addAuth', id, keyStr, cid, level)
37
- let Base64hashKey = Buffer.from( sha256.hash( keyStr)).toString('base64')
38
- return this.redis.hSet( DEVICE_PREFIX + id, {'key': Base64hashKey, 'cid': cid ,'level': level} )
29
+ // add device auth info
30
+ async addAuth(id, keyStr, cid = '', level = 0) {
31
+ let Base64hashKey = Buffer.from(sha256.hash(keyStr)).toString('base64')
32
+ return this.redis.hSet(DEVICE_PREFIX + id, { 'key': Base64hashKey, 'cid': cid, 'level': level })
39
33
  }
40
34
 
41
- async delAuth( id ){
42
- return this.redis.del( DEVICE_PREFIX + id )
35
+ async delAuth(id) {
36
+ return this.redis.del(DEVICE_PREFIX + id)
43
37
  }
44
38
 
45
- async save( id ){
39
+ async save(id) {
46
40
  return this.redis.save()
47
41
  }
48
42