iosignal 4.6.0 → 4.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.
- package/README.md +33 -7
- package/dist/io.d.ts +4 -4
- package/dist/io.js +3 -3
- package/dist/io.js.map +1 -1
- package/dist/io.min.js +3 -3
- package/dist/io.min.js.map +1 -1
- package/dist/iosignal.js +61 -71
- package/dist/types/client/IOCore.d.ts +4 -4
- package/dist/types/common/constants.d.ts +2 -12
- package/index.js +4 -4
- package/package.json +1 -4
- package/src/auth/key_providers/RedisKeyProvider.js +2 -2
- package/src/client/IOCore.js +8 -8
- package/src/common/constants.js +5 -15
- package/src/server/Manager.js +1 -1
- package/src/server/RemoteCore.js +3 -3
- package/src/server/Server.js +27 -27
- package/src/{api/RedisAPI.js → services/RedisService.js} +6 -6
- package/src/{api/api_reply.js → services/replyService.js} +5 -5
- package/src/{api/api_sudo.js → services/sudoService.js} +3 -3
- package/tsconfig.json +1 -1
- /package/src/{api/api_constant.js → services/constant.js} +0 -0
package/src/server/Server.js
CHANGED
|
@@ -3,13 +3,13 @@ import EventEmitter from 'events'
|
|
|
3
3
|
import { WebSocketServer } from 'ws'
|
|
4
4
|
import { Manager } from './Manager.js'
|
|
5
5
|
import { serverOption } from './serverOption.js'
|
|
6
|
-
import { STATUS } from '../
|
|
6
|
+
import { STATUS } from '../services/constant.js'
|
|
7
7
|
|
|
8
8
|
export class Server extends EventEmitter {
|
|
9
9
|
|
|
10
10
|
constructor(options, authManager) {
|
|
11
11
|
super();
|
|
12
|
-
this.
|
|
12
|
+
this.serviceNames = new Set()
|
|
13
13
|
this.wss = {};
|
|
14
14
|
this.port = null;
|
|
15
15
|
this.congPort = null;
|
|
@@ -130,63 +130,63 @@ export class Server extends EventEmitter {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
/**
|
|
133
|
-
*
|
|
134
|
-
* target:
|
|
135
|
-
*
|
|
133
|
+
* RPC service register.
|
|
134
|
+
* target: service_name <string>
|
|
135
|
+
* service: service_module <function module|class instance>
|
|
136
136
|
* return this
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
attach(target, service_module) {
|
|
139
139
|
|
|
140
140
|
if( typeof target !== 'string'){
|
|
141
|
-
throw new Error(`
|
|
141
|
+
throw new Error(`service( target ,service ) target name is not a string.`)
|
|
142
142
|
}
|
|
143
|
-
if (!
|
|
144
|
-
throw new Error(`
|
|
143
|
+
if (!service_module.checkPermission || typeof service_module.checkPermission != 'function') {
|
|
144
|
+
throw new Error(`Service ${target} : no checkPermission or not a function.`)
|
|
145
145
|
}
|
|
146
|
-
if(!
|
|
147
|
-
throw new Error(`
|
|
146
|
+
if(!service_module.commands || !Array.isArray(service_module.commands) ){
|
|
147
|
+
throw new Error(`Service ${target} : no commands or !Array.`)
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
//
|
|
151
|
-
if (
|
|
150
|
+
// Service TYPE 1. single call() function.
|
|
151
|
+
if ( service_module.call && typeof service_module.call == 'function' ) {
|
|
152
152
|
this.on(target, async (remote, req) => {
|
|
153
153
|
try {
|
|
154
|
-
if (!
|
|
154
|
+
if (!service_module.checkPermission(remote, req)) {
|
|
155
155
|
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
|
|
156
156
|
return
|
|
157
157
|
}
|
|
158
|
-
if(
|
|
159
|
-
// console.log('server
|
|
160
|
-
await
|
|
158
|
+
if( service_module.commands.includes( req.topic )){
|
|
159
|
+
// console.log('server service_module req', req )
|
|
160
|
+
await service_module.call(remote, req)
|
|
161
161
|
}else{
|
|
162
162
|
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
163
163
|
}
|
|
164
164
|
} catch (error) {
|
|
165
|
-
console.error(`Unhandled
|
|
165
|
+
console.error(`Unhandled Service Error in target [${target}]:`, error);
|
|
166
166
|
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
167
167
|
}
|
|
168
168
|
})
|
|
169
169
|
} else {
|
|
170
|
-
//
|
|
170
|
+
// Service TYPE 2. multiple functions.
|
|
171
171
|
this.on(target, async (remote, req) => {
|
|
172
172
|
try {
|
|
173
|
-
if (!
|
|
173
|
+
if (!service_module.checkPermission(remote, req)) {
|
|
174
174
|
remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
|
|
175
175
|
return
|
|
176
176
|
}
|
|
177
|
-
if(
|
|
178
|
-
await
|
|
177
|
+
if( service_module.commands.includes( req.topic )){
|
|
178
|
+
await service_module[req.topic](remote, req)
|
|
179
179
|
}else{
|
|
180
180
|
remote.response(req.mid, STATUS.ERROR, "UNKNOWN_COMMAND");
|
|
181
181
|
}
|
|
182
182
|
} catch (error) {
|
|
183
|
-
console.error(`Unhandled
|
|
183
|
+
console.error(`Unhandled Service Error in target [${target}]:`, error);
|
|
184
184
|
remote.response(req.mid, STATUS.ERROR, "INTERNAL_SERVER_ERROR");
|
|
185
185
|
}
|
|
186
186
|
})
|
|
187
187
|
}
|
|
188
|
-
this.
|
|
189
|
-
// console.log(`[
|
|
188
|
+
this.serviceNames.add(target)
|
|
189
|
+
// console.log(`[Service attached: ${target} ] accept commands: ${service_module.commands}`)
|
|
190
190
|
return this
|
|
191
191
|
}
|
|
192
192
|
|
|
@@ -196,8 +196,8 @@ export class Server extends EventEmitter {
|
|
|
196
196
|
this.manager.close()
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
this.
|
|
200
|
-
this.
|
|
199
|
+
this.serviceNames.forEach(v => this.removeAllListeners(v))
|
|
200
|
+
this.serviceNames.clear()
|
|
201
201
|
|
|
202
202
|
let serverCount = 0;
|
|
203
203
|
if (this.wss) serverCount++;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* RedisService <IOSignal RPC Service>
|
|
3
3
|
*
|
|
4
4
|
* iosignal 클라이언트의 redis 질의 요청을 받으면,
|
|
5
5
|
* redisClient 로 연결된 redis-server 에게 질의 후 결과를 되돌려 준다.
|
|
6
6
|
*
|
|
7
7
|
* client ex:
|
|
8
|
-
* await io.
|
|
8
|
+
* await io.call('redis','hget','user:id' )
|
|
9
9
|
*
|
|
10
10
|
*/
|
|
11
|
-
import { STATUS } from './
|
|
11
|
+
import { STATUS } from './constant.js'
|
|
12
12
|
|
|
13
13
|
const MIN_LEVEL = 200;
|
|
14
14
|
const COMMANDS = [
|
|
@@ -18,10 +18,10 @@ const COMMANDS = [
|
|
|
18
18
|
'sMembers', 'exists', 'sRem', 'del', 'keys', 'save'
|
|
19
19
|
]
|
|
20
20
|
|
|
21
|
-
export class
|
|
21
|
+
export class RedisService {
|
|
22
22
|
constructor(redisClient, _minLevel) {
|
|
23
23
|
if (!redisClient) {
|
|
24
|
-
throw new Error("
|
|
24
|
+
throw new Error("RedisService constructor: no redisClient")
|
|
25
25
|
}
|
|
26
26
|
this.redisClient = redisClient
|
|
27
27
|
this.minLevel = _minLevel ? _minLevel : MIN_LEVEL
|
|
@@ -32,7 +32,7 @@ export class RedisAPI {
|
|
|
32
32
|
return (remote.level >= this.minLevel) ? true : false;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
async
|
|
35
|
+
async call(remote, req) {
|
|
36
36
|
let result;
|
|
37
37
|
try {
|
|
38
38
|
let cmd = req.topic
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
|
-
* simple IOSignal
|
|
3
|
+
* simple IOSignal RPC Service example.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { STATUS } from './
|
|
6
|
+
import { STATUS } from './constant.js'
|
|
7
7
|
export const commands = ['echo', 'date', 'unixtime'];
|
|
8
8
|
|
|
9
9
|
const MIN_LEVEL = 0;
|
|
@@ -12,7 +12,7 @@ export function checkPermission(remote) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* client api call example: await io.
|
|
15
|
+
* client api call example: await io.call('reply','echo','hello')
|
|
16
16
|
*/
|
|
17
17
|
export async function echo(remote, req) {
|
|
18
18
|
if (!req.args)
|
|
@@ -22,7 +22,7 @@ export async function echo(remote, req) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* client api call example: await io.
|
|
25
|
+
* client api call example: await io.call('reply','date')
|
|
26
26
|
*/
|
|
27
27
|
export async function date(remote, req) {
|
|
28
28
|
let r = new Date().toUTCString()
|
|
@@ -30,7 +30,7 @@ export async function date(remote, req) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* client api call example: await io.
|
|
33
|
+
* client api call example: await io.call('reply','unixtime')
|
|
34
34
|
*/
|
|
35
35
|
export async function unixtime(remote, req) {
|
|
36
36
|
let r = Math.floor(Date.now() / 1000)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { STATUS } from './
|
|
1
|
+
import { STATUS } from './constant.js'
|
|
2
2
|
|
|
3
3
|
export const commands = [
|
|
4
4
|
'cid', 'remotes', 'clients',
|
|
@@ -14,7 +14,7 @@ export function checkPermission(remote) {
|
|
|
14
14
|
return (remote.level >= MIN_LEVEL) ? true : false;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export async function
|
|
17
|
+
export async function call(remote, req) {
|
|
18
18
|
let result;
|
|
19
19
|
let status = 0;
|
|
20
20
|
try {
|
|
@@ -70,7 +70,7 @@ export async function request(remote, req) {
|
|
|
70
70
|
}
|
|
71
71
|
} else {
|
|
72
72
|
status = STATUS.ERROR;
|
|
73
|
-
result = "
|
|
73
|
+
result = "service sudo: no such a cmd: " + cmd
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
remote.response(req.mid, status, result)
|
package/tsconfig.json
CHANGED
|
File without changes
|