iosignal 3.2.0 → 3.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.
@@ -18,20 +18,20 @@ export class Server extends EventEmitter {
18
18
  let pingT = parseInt(options.timeout)
19
19
  if (pingT && pingT >= 1000) serverOption.timeout = pingT
20
20
  }
21
-
22
21
  if (options.monitorPeriod) {
23
22
  let monitorT = parseInt(options.monitorPeriod)
24
23
  if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
25
24
  }
26
-
27
-
28
25
  if (options.showMessage) {
29
26
  serverOption.showMessage = options.showMessage
30
27
  }
31
-
32
28
  if (options.showMetric) {
33
29
  serverOption.showMetric = options.showMetric
34
30
  }
31
+ if (options.membersOnly) {
32
+ serverOption.membersOnly = options.membersOnly
33
+ }
34
+
35
35
 
36
36
  if (options.port || options.port == 0) {
37
37
  this.port = parseInt(options.port)
@@ -41,6 +41,10 @@ export class Server extends EventEmitter {
41
41
  this.httpServer = options.httpServer
42
42
  }
43
43
 
44
+ if (options.wsPath) {
45
+ this.wsPath = options.wsPath
46
+ }
47
+
44
48
  if (options.congPort || options.congPort == 0) {
45
49
  this.congPort = parseInt(options.congPort)
46
50
  }
@@ -53,7 +57,7 @@ export class Server extends EventEmitter {
53
57
  if (this.congPort || this.congPort == 0) this.serverCountToListen++;
54
58
 
55
59
  if (this.serverCountToListen === 0) {
56
- process.nextTick(() => this.emit('ready'));
60
+ throw new Error('Cannot create Server: At least one of "port", "httpServer", or "congPort" must be specified.');
57
61
  } else {
58
62
  if (this.port || this.port == 0 || this.httpServer) this.startWSServer();
59
63
  if (this.congPort || this.congPort == 0) this.startCongServer();
@@ -63,14 +67,11 @@ export class Server extends EventEmitter {
63
67
 
64
68
 
65
69
  startWSServer() {
66
-
67
70
  // console.log('optoins', options )
68
71
  if (this.port || this.port == 0) {
69
- console.log('opening WebSocket Server port:', this.port)
70
- this.wss = new WebSocketServer({ port: this.port })
71
- } else if (this.httpServer) {
72
- console.log('opening WebSocket Server external httpServer:')
73
- this.wss = new WebSocketServer({ server: this.httpServer })
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 })
74
75
  } else {
75
76
  throw new TypeError(
76
77
  'One and only one of the "port", "httpServer"' +
@@ -78,7 +79,6 @@ export class Server extends EventEmitter {
78
79
  );
79
80
  }
80
81
 
81
-
82
82
  this.wss.once('listening', () => {
83
83
  this.port = this.wss.address().port;
84
84
  console.log('wss server bound port:', this.port );
@@ -89,7 +89,6 @@ export class Server extends EventEmitter {
89
89
  });
90
90
 
91
91
  // this.wss.setMaxListeners(0)
92
-
93
92
  this.wss.on('error', (err) => {
94
93
  console.error('### ws server error:', err.message)
95
94
  if (err.code == 'EADDRINUSE') {
@@ -97,8 +96,8 @@ export class Server extends EventEmitter {
97
96
  }
98
97
  })
99
98
 
100
- this.wss.on('close', (err) => {
101
- console.log('### WS server closed.', err)
99
+ this.wss.on('close', () => {
100
+ console.log('### WS server is closed.')
102
101
  })
103
102
 
104
103
  this.wss.on('connection', (ws, req) => {
@@ -110,8 +109,6 @@ export class Server extends EventEmitter {
110
109
  }
111
110
 
112
111
  startCongServer() {
113
- console.log('opening CongSocket Server port:', this.congPort)
114
-
115
112
  this.congServer = net.createServer((socket) => {
116
113
  this.manager.addRemote(socket)
117
114
  })
@@ -121,7 +118,7 @@ export class Server extends EventEmitter {
121
118
  process.exit()
122
119
  }
123
120
  }).on('close', () => {
124
- console.log('### cong server closed.')
121
+ console.log('### congsocket server is closed.')
125
122
  }).listen(this.congPort, () => {
126
123
  this.congPort = this.congServer.address().port;
127
124
  console.log('congsocket server bound port:', this.congPort );
@@ -132,64 +129,76 @@ export class Server extends EventEmitter {
132
129
  });
133
130
  }
134
131
 
135
-
136
-
132
+ /**
133
+ * API register.
134
+ * target: api_name <string>
135
+ * api: api_module <function module|class instance>
136
+ * return this
137
+ */
137
138
  api(target, api) {
138
139
 
139
- this.apiNames.add(target)
140
-
141
- if (!api.checkPermission || typeof api.checkPermission != 'function') {
142
- throw new Error('wrong api interface. no checkPermission function.')
140
+ if( typeof target !== 'string'){
141
+ throw new Error(`api( target ,api ) target name is not a string.`)
143
142
  }
144
-
145
-
146
- if (typeof api.request == 'function' && Array.isArray(api.commands)) {
147
- console.log(`API TYPE1. A Class with one request function. target: ${target} list: ${api.commands}`)
148
-
149
- this.on(target, (remote, req) => {
150
- if (api.checkPermission(remote, req)) {
151
- api.request(remote, req)
152
- } else {
153
- remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
143
+ if (!api.checkPermission || typeof api.checkPermission != 'function') {
144
+ throw new Error(`API ${target} : no checkPermission or not a function.`)
145
+ }
146
+ if(!api.commands || !Array.isArray(api.commands) ){
147
+ throw new Error(`API ${target} : no commands or !Array.`)
148
+ }
149
+
150
+ // API TYPE 1. single request() function.
151
+ if ( api.request && typeof api.request == 'function' ) {
152
+ this.on(target, async (remote, req) => {
153
+ try {
154
+ if (!api.checkPermission(remote, req)) {
155
+ remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
156
+ return
157
+ }
158
+ if( api.commands.includes( req.topic )){
159
+ console.log('server api req', req )
160
+ await api.request(remote, req)
161
+ }else{
162
+ remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
163
+ }
164
+ } catch (error) {
165
+ console.error(`Unhandled API Error in target [${target}]:`, error);
166
+ remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
154
167
  }
155
168
  })
156
-
157
169
  } else {
158
- let apiList = []
159
- Object.keys(api).forEach(v => {
160
- if (typeof api[v] === 'function') apiList.push(v)
161
- console.log('api list', typeof api[v], v)
162
- })
163
-
164
- console.log(`API TYPE2. Multiple function list. target: ${target} list:${apiList}`)
165
-
166
- this.on(target, (remote, req) => {
167
- let r;
168
- if (!api.checkPermission(remote, req)) {
169
- r = "NO_PERMISSION."
170
- } else {
171
- if (api[req.topic]) {
172
- console.log(`API TYPE2. request target: ${target} has topic:${req.topic}`)
173
- api[req.topic](remote, req)
170
+ // API TYPE 2. multiple functions.
171
+ this.on(target, async (remote, req) => {
172
+ try {
173
+ if (!api.checkPermission(remote, req)) {
174
+ remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
174
175
  return
175
- } else {
176
- r = `target: ${req.target} has not topic name: ${req.topic}`
177
176
  }
177
+ if( api.commands.includes( req.topic )){
178
+ await api[req.topic](remote, req)
179
+ }else{
180
+ remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
181
+ }
182
+ } catch (error) {
183
+ console.error(`Unhandled API Error in target [${target}]:`, error);
184
+ remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
178
185
  }
179
-
180
- remote.response(req.mid, STATUS.ERROR, r)
181
186
  })
182
187
  }
183
-
188
+ this.apiNames.add(target)
189
+ console.log(`[API registed: ${target} ] accept commands: ${api.commands}`)
184
190
  return this
185
191
  }
186
192
 
187
193
  close(callback) {
188
- console.log('closing io-signal server...')
194
+ console.log('closing iosignal server...')
189
195
  if (this.manager) {
190
196
  this.manager.close()
191
197
  }
192
198
 
199
+ this.apiNames.forEach(v => this.removeAllListeners(v))
200
+ this.apiNames.clear()
201
+
193
202
  let serverCount = 0;
194
203
  if (this.wss) serverCount++;
195
204
  if (this.congServer) serverCount++;
@@ -203,14 +212,14 @@ export class Server extends EventEmitter {
203
212
  const onClosed = () => {
204
213
  closedCount++;
205
214
  if (closedCount === serverCount) {
206
- console.log('io-signal server closed.')
215
+ console.log('IOSignal server is closed.')
207
216
  if (callback) callback();
208
217
  }
209
218
  }
210
219
 
211
220
  if (this.wss) {
212
221
  this.wss.close(() => {
213
- console.log('wss server closed.')
222
+ // console.log('wss server closed.')
214
223
  onClosed()
215
224
  })
216
225
  }
@@ -218,7 +227,7 @@ export class Server extends EventEmitter {
218
227
  if (this.congServer) {
219
228
  this.congServer.close((err) => {
220
229
  if (err) console.error('congServer close error', err)
221
- console.log('cong server closed.')
230
+ // console.log('cong server closed.')
222
231
  onClosed()
223
232
  })
224
233
  }
@@ -5,6 +5,7 @@ export let serverOption = {
5
5
  port: null, // WebSocket
6
6
  congPort: null, // CongSocket
7
7
  httpServer: null,
8
+ wsPath: null,
8
9
  timeout: 50000,
9
10
  showMessage: 'none',
10
11
  showMetric: 0,
@@ -46,10 +47,10 @@ export let serverOption = {
46
47
  },
47
48
 
48
49
  auth: {
49
- delay_auth_fail: 1000,
50
+ delay_auth_fail: 600,
50
51
  },
51
52
 
52
- memberOnly: false
53
+ membersOnly: false
53
54
 
54
55
  }
55
56