iosignal 1.6.1 → 1.7.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 (51) hide show
  1. package/README.md +254 -247
  2. package/auth_file.json +13 -13
  3. package/auth_file.mjs +23 -23
  4. package/dist/iosignal.cjs +2 -2
  5. package/dist/iosignal.esm.js.map +1 -1
  6. package/dist/iosignal.min.js.map +1 -1
  7. package/index.js +19 -19
  8. package/indexWebBrowser.js +13 -13
  9. package/package.json +60 -59
  10. package/rollup.config.js +69 -69
  11. package/src/api/RedisAPI.js +65 -68
  12. package/src/api/api_constant.js +7 -7
  13. package/src/api/api_reply.js +31 -34
  14. package/src/api/api_sudo.js +91 -96
  15. package/src/auth/Auth_Env.js +76 -78
  16. package/src/auth/Auth_File.js +71 -75
  17. package/src/auth/Auth_Redis.js +44 -50
  18. package/src/auth/BohoAuth.js +164 -168
  19. package/src/client/CongPacket.js +141 -150
  20. package/src/client/IOCongSocket.js +81 -88
  21. package/src/client/IOCore.js +822 -851
  22. package/src/client/IOWS.js +80 -81
  23. package/src/client/IOWebSocket.js +111 -112
  24. package/src/common/constants.js +148 -159
  25. package/src/common/payload.js +81 -82
  26. package/src/common/quotaTable.js +68 -68
  27. package/src/common/util.js +61 -61
  28. package/src/server/FileLogger.js +30 -30
  29. package/src/server/Manager.js +433 -449
  30. package/src/server/Metric.js +155 -154
  31. package/src/server/Remote.js +207 -0
  32. package/src/server/{ServerRemoteCore.js → RemoteCore.js} +439 -467
  33. package/src/server/Server.js +163 -166
  34. package/src/server/serverOption.js +56 -56
  35. package/test-browser/favicon.ico +0 -0
  36. package/test-browser/index.html +96 -93
  37. package/test-browser/server.js +11 -11
  38. package/test-client-nodejs/client.cjs +17 -17
  39. package/test-client-nodejs/client.mjs +17 -17
  40. package/test-client-nodejs/client_api_reply.mjs +15 -15
  41. package/test-client-nodejs/stateChange.mjs +19 -19
  42. package/test-server/server-Auth_Env.js +19 -19
  43. package/test-server/server-Auth_File.js +12 -12
  44. package/test-server/server-api_reply.js +6 -6
  45. package/test-server/server-api_sudo.js +7 -7
  46. package/test-server/server-commonjs.cjs +8 -8
  47. package/test-server/server-esm.mjs +20 -19
  48. package/test-server/simple-server.mjs +11 -11
  49. package/test-signal-payloadType/signalPack.js +34 -34
  50. package/test-signal-payloadType/signaling.js +60 -60
  51. package/src/server/ServerRemote.js +0 -227
@@ -1,166 +1,163 @@
1
- import net from 'net'
2
- import EventEmitter from 'events'
3
- import { WebSocketServer } from 'ws'
4
- import { Manager } from './Manager.js'
5
- import { serverOption } from './serverOption.js'
6
- import { STATUS } from '../api/api_constant.js'
7
-
8
- export class Server extends EventEmitter {
9
-
10
- constructor(options, authManager ) {
11
- super();
12
- this.apiNames = new Set()
13
- this.wss = {};
14
-
15
- if (options.timeout) {
16
- let pingT = parseInt(options.timeout)
17
- if (pingT && pingT >= 1000) serverOption.timeout = pingT
18
- }
19
-
20
- if (options.monitorPeriod) {
21
- let monitorT = parseInt(options.monitorPeriod)
22
- if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
23
- }
24
-
25
-
26
- if (options.showMessage) {
27
- serverOption.showMessage = options.showMessage
28
- }
29
-
30
- if (options.showMetric) {
31
- serverOption.showMetric = options.showMetric
32
- }
33
-
34
- if (options.timeout) {
35
- serverOption.timeout = options.timeout
36
- }
37
-
38
- if (options.port) {
39
- serverOption.port = parseInt(options.port)
40
- }
41
-
42
- if (options.httpServer) {
43
- serverOption.httpServer = options.httpServer
44
- }
45
-
46
- if (options.congPort) {
47
- serverOption.congPort = parseInt(options.congPort)
48
- }
49
-
50
- this.manager = new Manager(this, authManager)
51
-
52
- if (serverOption.port || serverOption.httpServer ) this.startWSServer(serverOption)
53
- if( serverOption.congPort) this.startCongServer(serverOption.congPort)
54
-
55
- // console.log('serverOption:', serverOption)
56
- }
57
-
58
-
59
- startWSServer(serverOption) {
60
-
61
- if(serverOption.port ){
62
- console.log('opening WebSocket Server port:', serverOption.port)
63
- this.wss = new WebSocketServer({ port: serverOption.port })
64
- }else if( serverOption.httpServer ){
65
- console.log('opening WebSocket Server external httpServer:' )
66
- this.wss = new WebSocketServer({ server: serverOption.httpServer })
67
- }else{
68
- throw new TypeError(
69
- 'One and only one of the "port", "httpServer"' +
70
- 'must be specified'
71
- );
72
- }
73
-
74
-
75
- // this.wss.setMaxListeners(0)
76
-
77
- this.wss.on('error', (err) => {
78
- console.error('### ws server error:', err.message)
79
- if(err.code == 'EADDRINUSE'){
80
- process.exit()
81
- }
82
- })
83
-
84
- this.wss.on('close', (err) => {
85
- console.log('### WS server closed.', err)
86
- })
87
-
88
- this.wss.on('connection', (ws, req) => {
89
- ws.socketType = 'websocket'
90
- this.manager.addRemote(ws, req)
91
- })
92
-
93
-
94
- }
95
-
96
- startCongServer( congPort ) {
97
- console.log('opening CongSocket Server:', congPort)
98
-
99
- this.congServer = net.createServer((socket) => {
100
- this.manager.addRemote(socket)
101
- })
102
- .on('error', (err) => {
103
- console.log('### cong server error:', err.message)
104
- if(err.code == 'EADDRINUSE'){
105
- process.exit()
106
- }
107
- }).listen( congPort, () => {
108
- // console.log('congsocket server bound' );
109
- });
110
- }
111
-
112
-
113
-
114
- api(target, api) {
115
-
116
- this.apiNames.add(target)
117
-
118
- // common checkPermission
119
- if (!api.checkPermission || typeof api.checkPermission != 'function') {
120
- throw new Error('wrong api interface. no checkPermission function.')
121
- }
122
-
123
-
124
- if(typeof api.request == 'function' && Array.isArray( api.commands )){
125
- console.log(`API TYPE1. A Class with one request function. target: ${target} list: ${api.commands}` )
126
-
127
- this.on(target, (remote, req) => {
128
- if (api.checkPermission(remote, req)) {
129
- api.request(remote, req)
130
- } else {
131
- remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
132
- }
133
- })
134
-
135
- }else{
136
- let apiList = []
137
- Object.keys(api).forEach(v => {
138
- if (typeof api[v] === 'function') apiList.push(v)
139
- console.log('api list',typeof api[v] ,v )
140
- })
141
-
142
- console.log(`API TYPE2. Multiple function list. target: ${target} list:${apiList}`)
143
-
144
- this.on(target, (remote, req) => {
145
- let r;
146
- if (!api.checkPermission(remote, req)) {
147
- r = "NO_PERMISSION."
148
- } else {
149
- // if (apiList.includes(req.topic)) {
150
- if ( api[req.topic] ) {
151
- console.log(`API TYPE2. request target: ${target} has topic:${req.topic}`)
152
- api[req.topic](remote, req)
153
- return
154
- } else {
155
- r = `target: ${req.target} has not topic name: ${req.topic}`
156
- }
157
- }
158
-
159
- remote.response(req.mid, STATUS.ERROR, r)
160
- })
161
- }
162
-
163
- return this
164
- }
165
-
166
- }
1
+ import net from 'net'
2
+ import EventEmitter from 'events'
3
+ import { WebSocketServer } from 'ws'
4
+ import { Manager } from './Manager.js'
5
+ import { serverOption } from './serverOption.js'
6
+ import { STATUS } from '../api/api_constant.js'
7
+
8
+ export class Server extends EventEmitter {
9
+
10
+ constructor(options, authManager) {
11
+ super();
12
+ this.apiNames = new Set()
13
+ this.wss = {};
14
+
15
+ if (options.timeout) {
16
+ let pingT = parseInt(options.timeout)
17
+ if (pingT && pingT >= 1000) serverOption.timeout = pingT
18
+ }
19
+
20
+ if (options.monitorPeriod) {
21
+ let monitorT = parseInt(options.monitorPeriod)
22
+ if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
23
+ }
24
+
25
+
26
+ if (options.showMessage) {
27
+ serverOption.showMessage = options.showMessage
28
+ }
29
+
30
+ if (options.showMetric) {
31
+ serverOption.showMetric = options.showMetric
32
+ }
33
+
34
+ if (options.timeout) {
35
+ serverOption.timeout = options.timeout
36
+ }
37
+
38
+ if (options.port) {
39
+ serverOption.port = parseInt(options.port)
40
+ }
41
+
42
+ if (options.httpServer) {
43
+ serverOption.httpServer = options.httpServer
44
+ }
45
+
46
+ if (options.congPort) {
47
+ serverOption.congPort = parseInt(options.congPort)
48
+ }
49
+
50
+ this.manager = new Manager(this, authManager)
51
+
52
+ if (serverOption.port || serverOption.httpServer) this.startWSServer(serverOption)
53
+ if (serverOption.congPort) this.startCongServer(serverOption.congPort)
54
+
55
+ }
56
+
57
+
58
+ startWSServer(serverOption) {
59
+
60
+ if (serverOption.port) {
61
+ console.log('opening WebSocket Server port:', serverOption.port)
62
+ this.wss = new WebSocketServer({ port: serverOption.port })
63
+ } else if (serverOption.httpServer) {
64
+ console.log('opening WebSocket Server external httpServer:')
65
+ this.wss = new WebSocketServer({ server: serverOption.httpServer })
66
+ } else {
67
+ throw new TypeError(
68
+ 'One and only one of the "port", "httpServer"' +
69
+ 'must be specified'
70
+ );
71
+ }
72
+
73
+
74
+ // this.wss.setMaxListeners(0)
75
+
76
+ this.wss.on('error', (err) => {
77
+ console.error('### ws server error:', err.message)
78
+ if (err.code == 'EADDRINUSE') {
79
+ process.exit()
80
+ }
81
+ })
82
+
83
+ this.wss.on('close', (err) => {
84
+ console.log('### WS server closed.', err)
85
+ })
86
+
87
+ this.wss.on('connection', (ws, req) => {
88
+ ws.socketType = 'websocket'
89
+ this.manager.addRemote(ws, req)
90
+ })
91
+
92
+
93
+ }
94
+
95
+ startCongServer(congPort) {
96
+ console.log('opening CongSocket Server:', congPort)
97
+
98
+ this.congServer = net.createServer((socket) => {
99
+ this.manager.addRemote(socket)
100
+ })
101
+ .on('error', (err) => {
102
+ console.log('### cong server error:', err.message)
103
+ if (err.code == 'EADDRINUSE') {
104
+ process.exit()
105
+ }
106
+ }).listen(congPort, () => {
107
+ // console.log('congsocket server bound' );
108
+ });
109
+ }
110
+
111
+
112
+
113
+ api(target, api) {
114
+
115
+ this.apiNames.add(target)
116
+
117
+ if (!api.checkPermission || typeof api.checkPermission != 'function') {
118
+ throw new Error('wrong api interface. no checkPermission function.')
119
+ }
120
+
121
+
122
+ if (typeof api.request == 'function' && Array.isArray(api.commands)) {
123
+ console.log(`API TYPE1. A Class with one request function. target: ${target} list: ${api.commands}`)
124
+
125
+ this.on(target, (remote, req) => {
126
+ if (api.checkPermission(remote, req)) {
127
+ api.request(remote, req)
128
+ } else {
129
+ remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
130
+ }
131
+ })
132
+
133
+ } else {
134
+ let apiList = []
135
+ Object.keys(api).forEach(v => {
136
+ if (typeof api[v] === 'function') apiList.push(v)
137
+ console.log('api list', typeof api[v], v)
138
+ })
139
+
140
+ console.log(`API TYPE2. Multiple function list. target: ${target} list:${apiList}`)
141
+
142
+ this.on(target, (remote, req) => {
143
+ let r;
144
+ if (!api.checkPermission(remote, req)) {
145
+ r = "NO_PERMISSION."
146
+ } else {
147
+ if (api[req.topic]) {
148
+ console.log(`API TYPE2. request target: ${target} has topic:${req.topic}`)
149
+ api[req.topic](remote, req)
150
+ return
151
+ } else {
152
+ r = `target: ${req.target} has not topic name: ${req.topic}`
153
+ }
154
+ }
155
+
156
+ remote.response(req.mid, STATUS.ERROR, r)
157
+ })
158
+ }
159
+
160
+ return this
161
+ }
162
+
163
+ }
@@ -1,56 +1,56 @@
1
-
2
-
3
- export let serverOption = {
4
- clientTracking: false,
5
- port: 0, //wsPort
6
- httpServer: null,
7
- congPort: 0,
8
- timeout: 50000,
9
- showMessage: 'none',
10
- showMetric: 0,
11
- showChannel: 0,
12
- monitorPeriod: 5000,
13
- fileLogger:{
14
- connection: {
15
- use: false,
16
- path: 'connection.log'
17
- },
18
- auth: {
19
- use: false,
20
- path: 'auth.log'
21
- },
22
- attack:{
23
- use: false,
24
- path:'attack.log'
25
- }
26
- },
27
- useQuota:{
28
- signalSize: false,
29
- publishCounter: false,
30
- trafficRate: false,
31
- disconnect: false
32
- },
33
- defaultQuotaIndex: 3,
34
- adminLevel: 255,
35
-
36
- debug:{
37
- slow: false,
38
- delay: 500,
39
- showAuthInfo: false
40
- },
41
-
42
- retain:{
43
- isAvailable: true,
44
- limitSize: 100000,
45
- limitCounter: 1000
46
- },
47
-
48
- auth:{
49
- delay_auth_fail: 1000,
50
- },
51
-
52
- memberOnly: false
53
-
54
- }
55
-
56
-
1
+
2
+
3
+ export let serverOption = {
4
+ clientTracking: false,
5
+ port: 0, //wsPort
6
+ httpServer: null,
7
+ congPort: 0,
8
+ timeout: 50000,
9
+ showMessage: 'none',
10
+ showMetric: 0,
11
+ showChannel: 0,
12
+ monitorPeriod: 5000,
13
+ fileLogger: {
14
+ connection: {
15
+ use: false,
16
+ path: 'connection.log'
17
+ },
18
+ auth: {
19
+ use: false,
20
+ path: 'auth.log'
21
+ },
22
+ attack: {
23
+ use: false,
24
+ path: 'attack.log'
25
+ }
26
+ },
27
+ useQuota: {
28
+ signalSize: false,
29
+ publishCounter: false,
30
+ trafficRate: false,
31
+ disconnect: false
32
+ },
33
+ defaultQuotaIndex: 3,
34
+ adminLevel: 255,
35
+
36
+ debug: {
37
+ slow: false,
38
+ delay: 500,
39
+ showAuthInfo: false
40
+ },
41
+
42
+ retain: {
43
+ isAvailable: true,
44
+ limitSize: 100000,
45
+ limitCounter: 1000
46
+ },
47
+
48
+ auth: {
49
+ delay_auth_fail: 1000,
50
+ },
51
+
52
+ memberOnly: false
53
+
54
+ }
55
+
56
+
File without changes
@@ -1,94 +1,97 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Document</title>
8
- <style>
9
- #messageView{
10
- border: 1px solid red;
11
- padding: 2em;
12
- }
13
- </style>
14
- </head>
15
- <body>
16
- <h2>Simple pub sub example.</h2>
17
- <p>you should start server first.</p>
18
- <p>this index.html load two types of modules. </p>
19
- <h5>1. IIFE: iosignal.min.js</h5>
20
- <p>use global name: IO </p>
21
- <p>IO has props: Boho, MBP, Buffer,, </p>
22
- <h5>2. ESM: iosignal.esm.js</h5>
23
- <p> check the console.</p>
24
- <h2>message log</h2>
25
- <div id="messageView">...</div>
26
- <script src="../dist/iosignal.min.js"></script>
27
- <script>
28
- console.log('IIFE:')
29
- console.log('IO', IO)
30
- console.log('MBP', IO.MBP )
31
- console.log('Buffer', IO.Buffer)
32
- console.log('Boho', IO.Boho )
33
- console.log('sha256.hash("hi")', IO.Boho.sha256.hash('hi'))
34
-
35
- var ESM_IO ;
36
- // IO.binaryType = "blob"
37
- var io1 = new IO('ws://localhost:7777')
38
- var io2 = new IO('ws://localhost:7777')
39
- var io3 = new IO('ws://localhost:7777')
40
-
41
- io1.on('error', errorHandler )
42
- io2.on('error', errorHandler )
43
- io3.on('error', errorHandler )
44
-
45
- let channelName = 'io'
46
- // classic style subsribing
47
- io1.on('ready',e=>{
48
- io1.subscribe(channelName)
49
- io1.on(channelName, (...args)=>{
50
- // console.log('io1 received', args )
51
- let msg = '[io1] ' + JSON.stringify( args )
52
- addMessage(msg)
53
- })
54
- })
55
-
56
- // modern style subscribing
57
- io2.listen(channelName, (...args)=>{
58
- // console.log('io2 receive', args )
59
- let msg = '[io2] ' + JSON.stringify( args )
60
- addMessage(msg)
61
- })
62
-
63
- setInterval(e=>{
64
- io3.signal(channelName, 'single string') // single string payload
65
- io3.signal(channelName, Date.now(), 'a', 2 , {key: 3} ) //multiple payload
66
- io3.signal(channelName ) // pure signal without payload.
67
- },3000)
68
-
69
- function addMessage(msg){
70
-
71
- let p = document.createElement('p')
72
- p.textContent = msg
73
- let v = document.querySelector('#messageView')
74
- if(v.childElementCount > 20) v.innerHTML = ""
75
- v.prepend( p )
76
- }
77
-
78
- function errorHandler(e){
79
- console.log(e.target)
80
- addMessage('ws.error')
81
- }
82
- </script>
83
- <!-- <script type="module">
84
- import { IO, Boho, MBP, Buffer, sha256 } from "../dist/iosignal.esm.js"
85
- ESM_IO = IO
86
- console.log('ESM:')
87
- console.log('IO', IO)
88
- console.log('Boho', Boho )
89
- console.log('MBP', MBP )
90
- console.log('Buffer', Buffer)
91
- console.log('sha256.hash("hi")', sha256.hash('hi'))
92
- </script> -->
93
- </body>
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8">
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+ <title>Document</title>
9
+ <style>
10
+ #messageView {
11
+ border: 1px solid red;
12
+ padding: 2em;
13
+ }
14
+ </style>
15
+ </head>
16
+
17
+ <body>
18
+ <h2>Simple pub sub example.</h2>
19
+ <p>you should start server first.</p>
20
+ <p>this index.html load two types of modules. </p>
21
+ <h5>1. IIFE: iosignal.min.js</h5>
22
+ <p>use global name: IO </p>
23
+ <p>IO has props: Boho, MBP, Buffer,, </p>
24
+ <h5>2. ESM: iosignal.esm.js</h5>
25
+ <p> check the console.</p>
26
+ <h2>message log</h2>
27
+ <div id="messageView">...</div>
28
+ <script src="../dist/iosignal.min.js"></script>
29
+ <script>
30
+ console.log('IIFE:')
31
+ console.log('IO', IO)
32
+ console.log('MBP', IO.MBP)
33
+ console.log('Buffer', IO.Buffer)
34
+ console.log('Boho', IO.Boho)
35
+ console.log('sha256.hash("hi")', IO.Boho.sha256.hash('hi'))
36
+
37
+ var ESM_IO;
38
+ IO.binaryType = "blob"
39
+ var io1 = new IO('ws://localhost:7777')
40
+ var io2 = new IO('ws://localhost:7777')
41
+ var io3 = new IO('ws://localhost:7777')
42
+
43
+ io1.on('error', errorHandler)
44
+ io2.on('error', errorHandler)
45
+ io3.on('error', errorHandler)
46
+
47
+ let channelName = 'io'
48
+ // classic style subsribing
49
+ io1.on('ready', e => {
50
+ io1.subscribe(channelName)
51
+ io1.on(channelName, (...args) => {
52
+ // console.log('io1 received', args )
53
+ let msg = '[io1] ' + JSON.stringify(args)
54
+ addMessage(msg)
55
+ })
56
+ })
57
+
58
+ // modern style subscribing
59
+ io2.listen(channelName, (...args) => {
60
+ // console.log('io2 receive', args )
61
+ let msg = '[io2] ' + JSON.stringify(args)
62
+ addMessage(msg)
63
+ })
64
+
65
+ setInterval(e => {
66
+ io3.signal(channelName, 'single string') // single string payload
67
+ io3.signal(channelName, Date.now(), 'a', 2, { key: 3 }) //multiple payload
68
+ io3.signal(channelName) // pure signal without payload.
69
+ }, 3000)
70
+
71
+ function addMessage(msg) {
72
+
73
+ let p = document.createElement('p')
74
+ p.textContent = msg
75
+ let v = document.querySelector('#messageView')
76
+ if (v.childElementCount > 20) v.innerHTML = ""
77
+ v.prepend(p)
78
+ }
79
+
80
+ function errorHandler(e) {
81
+ console.log(e.target)
82
+ addMessage('ws.error')
83
+ }
84
+ </script>
85
+ <!-- <script type="module">
86
+ import { IO, Boho, MBP, Buffer, sha256 } from "../dist/iosignal.esm.js"
87
+ ESM_IO = IO
88
+ console.log('ESM:')
89
+ console.log('IO', IO)
90
+ console.log('Boho', Boho )
91
+ console.log('MBP', MBP )
92
+ console.log('Buffer', Buffer)
93
+ console.log('sha256.hash("hi")', sha256.hash('hi'))
94
+ </script> -->
95
+ </body>
96
+
94
97
  </html>
@@ -1,11 +1,11 @@
1
- import { Server } from 'iosignal'
2
-
3
- const options = {
4
- port: 7777,
5
- showMetric: 2,
6
- showMessage: 'message'
7
- }
8
-
9
- const server = new Server( options )
10
-
11
-
1
+ import { Server } from 'iosignal'
2
+
3
+ const options = {
4
+ port: 7777,
5
+ showMetric: 2,
6
+ showMessage: 'message'
7
+ }
8
+
9
+ const server = new Server(options)
10
+
11
+