iosignal 1.7.0 → 1.8.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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +254 -247
  3. package/auth_file.json +13 -13
  4. package/auth_file.mjs +23 -23
  5. package/dist/iosignal-iife.js +11 -0
  6. package/dist/iosignal-iife.js.map +1 -0
  7. package/dist/iosignal.cjs +1 -2
  8. package/dist/iosignal.js +11 -0
  9. package/dist/iosignal.js.map +1 -0
  10. package/index.js +19 -19
  11. package/indexWebBrowser.js +13 -13
  12. package/package.json +56 -59
  13. package/rollup.config.js +72 -69
  14. package/src/api/RedisAPI.js +65 -65
  15. package/src/api/api_constant.js +7 -7
  16. package/src/api/api_reply.js +31 -31
  17. package/src/api/api_sudo.js +91 -91
  18. package/src/auth/Auth_Env.js +76 -76
  19. package/src/auth/Auth_File.js +71 -71
  20. package/src/auth/Auth_Redis.js +44 -44
  21. package/src/auth/BohoAuth.js +164 -164
  22. package/src/client/CongPacket.js +141 -141
  23. package/src/client/IOCongSocket.js +81 -81
  24. package/src/client/IOCore.js +822 -822
  25. package/src/client/IOWS.js +80 -80
  26. package/src/client/IOWebSocket.js +111 -111
  27. package/src/common/constants.js +148 -148
  28. package/src/common/payload.js +81 -81
  29. package/src/common/quotaTable.js +68 -68
  30. package/src/common/util.js +61 -61
  31. package/src/server/FileLogger.js +30 -30
  32. package/src/server/Manager.js +433 -433
  33. package/src/server/Metric.js +155 -155
  34. package/src/server/Remote.js +207 -207
  35. package/src/server/RemoteCore.js +439 -439
  36. package/src/server/Server.js +163 -163
  37. package/src/server/serverOption.js +56 -56
  38. package/test-browser/favicon.ico +0 -0
  39. package/test-browser/index.html +96 -96
  40. package/test-browser/server.js +11 -11
  41. package/test-client-nodejs/client.cjs +17 -17
  42. package/test-client-nodejs/client.mjs +17 -17
  43. package/test-client-nodejs/client_api_reply.mjs +15 -15
  44. package/test-client-nodejs/stateChange.mjs +19 -19
  45. package/test-server/server-Auth_Env.js +19 -19
  46. package/test-server/server-Auth_File.js +12 -12
  47. package/test-server/server-api_reply.js +6 -6
  48. package/test-server/server-api_sudo.js +7 -7
  49. package/test-server/server-commonjs.cjs +8 -8
  50. package/test-server/server-esm.mjs +20 -20
  51. package/test-server/simple-server.mjs +11 -11
  52. package/test-signal-payloadType/signalPack.js +34 -34
  53. package/test-signal-payloadType/signaling.js +60 -60
  54. package/dist/iosignal.esm.js +0 -3
  55. package/dist/iosignal.esm.js.map +0 -1
  56. package/dist/iosignal.min.js +0 -3
  57. package/dist/iosignal.min.js.map +0 -1
@@ -1,163 +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
- }
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
+ 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,97 +1,97 @@
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
-
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-iife.js</h5>
22
+ <p>use global name: IO </p>
23
+ <p>IO has props: Boho, MBP, Buffer,, </p>
24
+ <h5>2. ESM: iosignal.js</h5>
25
+ <p> check the console.</p>
26
+ <h2>message log</h2>
27
+ <div id="messageView">...</div>
28
+ <script src="../dist/iosignal-iife.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.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
+
97
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
+