iosignal 5.1.4 → 5.3.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 (43) hide show
  1. package/README.md +163 -0
  2. package/dist/{io.d.ts → browser/esm/io.d.ts} +10 -12
  3. package/dist/browser/esm/io.js +23 -0
  4. package/dist/browser/esm/io.js.map +1 -0
  5. package/dist/browser/iife/io.js +23 -0
  6. package/dist/browser/iife/io.js.map +1 -0
  7. package/dist/{iosignal.cjs → node/iosignal.cjs} +124 -119
  8. package/dist/{iosignal.js → node/iosignal.js} +123 -118
  9. package/dist/types/client/IOCore.d.ts +3 -4
  10. package/dist/types/client/browser/IOWebSocket.d.ts +5 -5
  11. package/dist/types/common/payload.d.ts +2 -2
  12. package/package.json +32 -15
  13. package/dist/io.js +0 -23
  14. package/dist/io.js.map +0 -1
  15. package/dist/io.min.js +0 -23
  16. package/dist/io.min.js.map +0 -1
  17. package/index.js +0 -26
  18. package/rollup.config.js +0 -59
  19. package/src/auth/BohoAuth.js +0 -196
  20. package/src/auth/key_providers/FileKeyProvider.js +0 -59
  21. package/src/auth/key_providers/RedisKeyProvider.js +0 -46
  22. package/src/auth/key_providers/StringKeyProvider.js +0 -54
  23. package/src/client/CongPacket.js +0 -143
  24. package/src/client/IOCongSocket.js +0 -92
  25. package/src/client/IOCore.js +0 -1202
  26. package/src/client/IOWS.js +0 -78
  27. package/src/client/browser/IOWebSocket.js +0 -202
  28. package/src/common/constants.js +0 -210
  29. package/src/common/payload.js +0 -82
  30. package/src/common/quotaTable.js +0 -68
  31. package/src/common/util.js +0 -64
  32. package/src/server/FileLogger.js +0 -30
  33. package/src/server/Manager.js +0 -450
  34. package/src/server/Metrics.js +0 -155
  35. package/src/server/Remote.js +0 -216
  36. package/src/server/RemoteCore.js +0 -427
  37. package/src/server/Server.js +0 -236
  38. package/src/server/serverOption.js +0 -56
  39. package/src/services/RedisService.js +0 -53
  40. package/src/services/constant.js +0 -7
  41. package/src/services/replyService.js +0 -39
  42. package/src/services/sudoService.js +0 -85
  43. package/tsconfig.json +0 -24
@@ -1,236 +0,0 @@
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 '../services/constant.js'
7
-
8
- export class Server extends EventEmitter {
9
-
10
- constructor(options, authManager) {
11
- super();
12
- this.serviceNames = new Set()
13
- this.wss = {};
14
- this.port = null;
15
- this.congPort = null;
16
-
17
- if (options.timeout) {
18
- let pingT = parseInt(options.timeout)
19
- if (pingT && pingT >= 1000) serverOption.timeout = pingT
20
- }
21
- if (options.monitorPeriod) {
22
- let monitorT = parseInt(options.monitorPeriod)
23
- if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
24
- }
25
- if (options.showMessage) {
26
- serverOption.showMessage = options.showMessage
27
- }
28
- if (options.showMetric) {
29
- serverOption.showMetric = options.showMetric
30
- }
31
- if (options.membersOnly) {
32
- serverOption.membersOnly = options.membersOnly
33
- }
34
-
35
-
36
- if (options.port || options.port == 0) {
37
- this.port = parseInt(options.port)
38
- }
39
-
40
- if (options.httpServer) {
41
- this.httpServer = options.httpServer
42
- }
43
-
44
- if (options.wsPath) {
45
- this.wsPath = options.wsPath
46
- }
47
-
48
- if (options.congPort || options.congPort == 0) {
49
- this.congPort = parseInt(options.congPort)
50
- }
51
-
52
- this.manager = new Manager(this, authManager)
53
-
54
- this.serverCountToListen = 0;
55
- this.listeningServerCount = 0;
56
- if (this.port || this.port == 0 || this.httpServer) this.serverCountToListen++;
57
- if (this.congPort || this.congPort == 0) this.serverCountToListen++;
58
-
59
- if (this.serverCountToListen === 0) {
60
- throw new Error('Cannot create Server: At least one of "port", "httpServer", or "congPort" must be specified.');
61
- } else {
62
- if (this.port || this.port == 0 || this.httpServer) this.startWSServer();
63
- if (this.congPort || this.congPort == 0) this.startCongServer();
64
- }
65
-
66
- }
67
-
68
-
69
- startWSServer() {
70
- // console.log('optoins', options )
71
- if (this.port || this.port == 0) {
72
- this.wss = new WebSocketServer({ port: this.port , path: this.wsPath })
73
- } else if (this.httpServer ) {
74
- this.wss = new WebSocketServer({ server: this.httpServer, path: this.wsPath })
75
- } else {
76
- throw new TypeError(
77
- 'One and only one of the "port", "httpServer"' +
78
- 'must be specified'
79
- );
80
- }
81
-
82
- this.wss.once('listening', () => {
83
- this.port = this.wss.address().port;
84
- // console.log('wss server bound port:', this.port );
85
- this.listeningServerCount++;
86
- if (this.listeningServerCount === this.serverCountToListen) {
87
- this.emit('ready');
88
- }
89
- });
90
-
91
- // this.wss.setMaxListeners(0)
92
- this.wss.on('error', (err) => {
93
- console.error('### ws server error:', err.message)
94
- if (err.code == 'EADDRINUSE') {
95
- process.exit()
96
- }
97
- })
98
-
99
- this.wss.on('close', () => {
100
- // console.log('### WS server is closed.')
101
- })
102
-
103
- this.wss.on('connection', (ws, req) => {
104
- ws.socketType = 'websocket'
105
- this.manager.addRemote(ws, req)
106
- })
107
-
108
-
109
- }
110
-
111
- startCongServer() {
112
- this.congServer = net.createServer((socket) => {
113
- this.manager.addRemote(socket)
114
- })
115
- .on('error', (err) => {
116
- console.log('### cong server error:', err.message)
117
- if (err.code == 'EADDRINUSE') {
118
- process.exit()
119
- }
120
- }).on('close', () => {
121
- // console.log('### congsocket server is closed.')
122
- }).listen(this.congPort, () => {
123
- this.congPort = this.congServer.address().port;
124
- // console.log('congsocket server bound port:', this.congPort );
125
- this.listeningServerCount++;
126
- if (this.listeningServerCount === this.serverCountToListen) {
127
- this.emit('ready');
128
- }
129
- });
130
- }
131
-
132
- /**
133
- * RPC service register.
134
- * service: service_name <string>
135
- * service_module <function module|class instance>
136
- * return this
137
- */
138
- attach(service, service_module) {
139
-
140
- if( typeof service !== 'string'){
141
- throw new Error(`attach service name is not a string.`)
142
- }
143
- if (!service_module.checkPermission || typeof service_module.checkPermission != 'function') {
144
- throw new Error(`Service ${service} : no checkPermission or not a function.`)
145
- }
146
- if(!service_module.commands || !Array.isArray(service_module.commands) ){
147
- throw new Error(`Service ${service} : no commands or !Array.`)
148
- }
149
-
150
- // Service TYPE 1. single call() function.
151
- if ( service_module.call && typeof service_module.call == 'function' ) {
152
- this.on(service, (remote, req) => {
153
- try {
154
- if (!service_module.checkPermission(remote, req)) {
155
- remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
156
- return
157
- }
158
- if( service_module.commands.includes( req.topic )){
159
- // console.log('server service_module req', req )
160
- service_module.call(remote, req)
161
- }else{
162
- remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
163
- }
164
- } catch (error) {
165
- console.error(`Unhandled Service Error in service [${service}]:`, error);
166
- remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
167
- }
168
- })
169
- } else {
170
- // Service TYPE 2. multiple functions.
171
- this.on(service, (remote, req) => {
172
- try {
173
- if (!service_module.checkPermission(remote, req)) {
174
- remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
175
- return
176
- }
177
- if( service_module.commands.includes( req.topic )){
178
- service_module[req.topic](remote, req)
179
- }else{
180
- remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
181
- }
182
- } catch (error) {
183
- console.error(`Unhandled Service Error in service [${service}]:`, error);
184
- remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
185
- }
186
- })
187
- }
188
- this.serviceNames.add(service)
189
- // console.log(`[Service attached: ${service} ] accept commands: ${service_module.commands}`)
190
- return this
191
- }
192
-
193
- close(callback) {
194
- console.log('closing iosignal server...')
195
- if (this.manager) {
196
- this.manager.close()
197
- }
198
-
199
- this.serviceNames.forEach(v => this.removeAllListeners(v))
200
- this.serviceNames.clear()
201
-
202
- let serverCount = 0;
203
- if (this.wss) serverCount++;
204
- if (this.congServer) serverCount++;
205
-
206
- if (serverCount === 0) {
207
- if (callback) process.nextTick(callback);
208
- return;
209
- }
210
-
211
- let closedCount = 0;
212
- const onClosed = () => {
213
- closedCount++;
214
- if (closedCount === serverCount) {
215
- console.log('IOSignal server is closed.')
216
- if (callback) callback();
217
- }
218
- }
219
-
220
- if (this.wss) {
221
- this.wss.close(() => {
222
- // console.log('wss server closed.')
223
- onClosed()
224
- })
225
- }
226
-
227
- if (this.congServer) {
228
- this.congServer.close((err) => {
229
- // if (err) console.error('congServer close error', err)
230
- // console.log('cong server closed.')
231
- onClosed()
232
- })
233
- }
234
- }
235
-
236
- }
@@ -1,56 +0,0 @@
1
-
2
-
3
- export let serverOption = {
4
- clientTracking: false,
5
- port: null, // WebSocket
6
- congPort: null, // CongSocket
7
- httpServer: null,
8
- wsPath: null,
9
- timeout: 50000,
10
- showMessage: 'none',
11
- showMetric: 0,
12
- showChannel: 0,
13
- monitorPeriod: 5000,
14
- fileLogger: {
15
- connection: {
16
- use: false,
17
- path: 'connection.log'
18
- },
19
- auth: {
20
- use: false,
21
- path: 'auth.log'
22
- },
23
- attack: {
24
- use: false,
25
- path: 'attack.log'
26
- }
27
- },
28
- useQuota: {
29
- signalSize: false,
30
- publishCounter: false,
31
- trafficRate: false,
32
- disconnect: false
33
- },
34
- defaultQuotaIndex: 3,
35
- adminLevel: 255,
36
-
37
- debug: {
38
- slow: false,
39
- delay: 500,
40
- showAuthInfo: false
41
- },
42
-
43
- retain: {
44
- isAvailable: true,
45
- limitSize: 100000,
46
- limitCounter: 1000
47
- },
48
-
49
- auth: {
50
- delay_auth_fail: 600
51
- },
52
- membersOnly: false
53
-
54
- }
55
-
56
-
@@ -1,53 +0,0 @@
1
- /**
2
- * RedisService <IOSignal RPC Service>
3
- *
4
- * iosignal 클라이언트의 redis 질의 요청을 받으면,
5
- * redisClient 로 연결된 redis-server 에게 질의 후 결과를 되돌려 준다.
6
- *
7
- * client ex:
8
- * await io.call('redis','hget','user:id' )
9
- *
10
- */
11
- import { STATUS } from './constant.js'
12
-
13
- const MIN_LEVEL = 200;
14
- const COMMANDS = [
15
- 'GET', 'SET', 'HGETALL', 'HGET', 'HSET', 'SADD', 'SISMEMBER',
16
- 'SMEMBERS', 'EXISTS', 'SREM', 'DEL', 'KEYS', 'SAVE',
17
- 'get', 'set', 'hGetAll', 'hGet', 'hSet', 'sAdd', 'sIsMember',
18
- 'sMembers', 'exists', 'sRem', 'del', 'keys', 'save'
19
- ]
20
-
21
- export class RedisService {
22
- constructor(redisClient, _minLevel) {
23
- if (!redisClient) {
24
- throw new Error("RedisService constructor: no redisClient")
25
- }
26
- this.redisClient = redisClient
27
- this.minLevel = _minLevel ? _minLevel : MIN_LEVEL
28
- this.commands = COMMANDS
29
- }
30
-
31
- checkPermission(remote, req) {
32
- return (remote.level >= this.minLevel) ? true : false;
33
- }
34
-
35
- async call(remote, req) {
36
- let result;
37
- try {
38
- let cmd = req.topic
39
- if (req.args?.length > 0) {
40
- result = await this.redisClient[cmd](...req.args)
41
- } else {
42
- result = await this.redisClient[cmd]()
43
- }
44
- remote.response(req.mid, STATUS.OK, result)
45
- } catch (e) {
46
- remote.response(req.mid, STATUS.ERROR, e.message)
47
- }
48
- }
49
-
50
- }
51
-
52
-
53
-
@@ -1,7 +0,0 @@
1
-
2
- export const STATUS = {
3
- OK: 0,
4
- //0~127: success
5
- //128~255: fail
6
- ERROR: 255
7
- }
@@ -1,39 +0,0 @@
1
-
2
- /**
3
- * simple IOSignal RPC Service example.
4
- */
5
-
6
- import { STATUS } from './constant.js'
7
- export const commands = ['echo', 'date', 'unixtime'];
8
-
9
- const MIN_LEVEL = 0;
10
- export function checkPermission(remote) {
11
- return (remote.level >= MIN_LEVEL) ? true : false;
12
- }
13
-
14
- /**
15
- * client api call example: await io.call('reply','echo','hello')
16
- */
17
- export async function echo(remote, req) {
18
- if (!req.args)
19
- remote.response(req.mid, STATUS.ERROR, 'no message to echo')
20
- else
21
- remote.response(req.mid, STATUS.OK, req.args)
22
- }
23
-
24
- /**
25
- * client api call example: await io.call('reply','date')
26
- */
27
- export async function date(remote, req) {
28
- let r = new Date().toUTCString()
29
- remote.response(req.mid, STATUS.OK, r)
30
- }
31
-
32
- /**
33
- * client api call example: await io.call('reply','unixtime')
34
- */
35
- export async function unixtime(remote, req) {
36
- let r = Math.floor(Date.now() / 1000)
37
- remote.response(req.mid, STATUS.OK, r)
38
- }
39
-
@@ -1,85 +0,0 @@
1
- import { STATUS } from './constant.js'
2
-
3
- export const commands = [
4
- 'cid', 'remotes', 'clients',
5
- 'channels', 'subscribers',
6
- 'remote', 'client', 'close',
7
- 'addauth', 'getauth', 'delauth',
8
- 'getauthidlist', 'getdevicelist',
9
- 'adddevice', 'getdevice', 'deldevice'
10
- ]
11
-
12
- const MIN_LEVEL = 255;
13
- export function checkPermission(remote) {
14
- return (remote.level >= MIN_LEVEL) ? true : false;
15
- }
16
-
17
- export async function call(remote, req) {
18
- let result;
19
- let status = 0;
20
- try {
21
- let cmd = req.topic;
22
- cmd = cmd.toLowerCase()
23
- if (cmd == 'cid') {
24
- result = remote.manager.metrics.getCIdList()
25
- } else if (cmd == 'remotes' || cmd == 'clients') {
26
- result = remote.manager.metrics.getRemotes()
27
- } else if (cmd == 'channels') {
28
- result = remote.manager.metrics.getChannelList()
29
- } else if (cmd == 'subscribers') {
30
- let ch = req.$[0]
31
- if (ch) result = remote.manager.metrics.getSubscribers(ch)
32
- } else if (cmd == 'remote' || cmd == 'client') {
33
- let cid = req.$[0]
34
- let mode = req.$[1]
35
- if (cid) result = remote.manager.metrics.getRemoteByCId(cid, mode)
36
-
37
- } else if (cmd == 'close') {
38
- if (req.$[0]) result = remote.manager.closeRemoteByCId(req.$[0])
39
-
40
- } else if (cmd == 'addauth' || cmd == 'adddevice') {
41
- if (remote.manager.authManager && req.$.length == 4) {
42
- if (remote.manager.authManager.addAuth) {
43
- let did = req.$[0]
44
- let dkey = req.$[1]
45
- let cid = req.$[2]
46
- let level = req.$[3]
47
- result = await remote.manager.authManager.addAuth(did, dkey, cid, level)
48
- }
49
-
50
- }
51
- } else if (cmd == 'delauth' || cmd == 'deldevice') {
52
- if (remote.manager.authManager && req.$.length == 1) {
53
- if (remote.manager.authManager.delAuth) {
54
- let did = req.$[0]
55
- result = await remote.manager.authManager.delAuth(did)
56
- }
57
- }
58
- } else if (cmd == 'getauth' || cmd == 'getdevice') {
59
- if (remote.manager.authManager && req.$.length == 1) {
60
- if (remote.manager.authManager.getAuth) {
61
- let did = req.$[0]
62
- result = await remote.manager.authManager.getAuth(did)
63
- }
64
- }
65
- } else if (cmd == 'getauthidlist' || cmd == 'getdevicelist') {
66
- if (remote.manager.authManager) {
67
- if (remote.manager.authManager.getAuthIdList) {
68
- result = await remote.manager.authManager.getAuthIdList()
69
- }
70
- }
71
- } else {
72
- status = STATUS.ERROR;
73
- result = "service sudo: no such a cmd: " + cmd
74
- }
75
-
76
- remote.response(req.mid, status, result)
77
-
78
- } catch (e) {
79
- remote.response(req.mid, STATUS.ERROR, e.message)
80
- }
81
-
82
- }
83
-
84
-
85
-
package/tsconfig.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "declaration": true,
4
- "emitDeclarationOnly": true,
5
- "outDir": "./dist/types",
6
- "allowJs": true,
7
- "moduleResolution": "node",
8
- "target": "es2020",
9
- "lib": ["es2020", "dom"],
10
- "baseUrl": ".",
11
- "paths": {
12
- "@src/*": ["src/*"]
13
- }
14
- },
15
- "include": [
16
- "src/client/browser/IOWebSocket.js",
17
- "src/client/IOCore.js",
18
- "src/common/constants.js",
19
- "package.json"
20
- ],
21
- "exclude": [
22
- "node_modules"
23
- ]
24
- }