iosignal 4.6.1 → 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.
@@ -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 '../api/api_constant.js'
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.apiNames = new Set()
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
- * API register.
134
- * target: api_name <string>
135
- * api: api_module <function module|class instance>
133
+ * RPC service register.
134
+ * target: service_name <string>
135
+ * service: service_module <function module|class instance>
136
136
  * return this
137
137
  */
138
- api(target, api) {
138
+ attach(target, service_module) {
139
139
 
140
140
  if( typeof target !== 'string'){
141
- throw new Error(`api( target ,api ) target name is not a string.`)
141
+ throw new Error(`service( target ,service ) target name is not a string.`)
142
142
  }
143
- if (!api.checkPermission || typeof api.checkPermission != 'function') {
144
- throw new Error(`API ${target} : no checkPermission or not a function.`)
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(!api.commands || !Array.isArray(api.commands) ){
147
- throw new Error(`API ${target} : no commands or !Array.`)
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
- // API TYPE 1. single request() function.
151
- if ( api.request && typeof api.request == 'function' ) {
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 (!api.checkPermission(remote, req)) {
154
+ if (!service_module.checkPermission(remote, req)) {
155
155
  remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
156
156
  return
157
157
  }
158
- if( api.commands.includes( req.topic )){
159
- // console.log('server api req', req )
160
- await api.request(remote, req)
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 API Error in target [${target}]:`, error);
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
- // API TYPE 2. multiple functions.
170
+ // Service TYPE 2. multiple functions.
171
171
  this.on(target, async (remote, req) => {
172
172
  try {
173
- if (!api.checkPermission(remote, req)) {
173
+ if (!service_module.checkPermission(remote, req)) {
174
174
  remote.response(req.mid, STATUS.ERROR, "NO_PERMISSION.")
175
175
  return
176
176
  }
177
- if( api.commands.includes( req.topic )){
178
- await api[req.topic](remote, req)
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 API Error in target [${target}]:`, error);
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.apiNames.add(target)
189
- // console.log(`[API registed: ${target} ] accept commands: ${api.commands}`)
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.apiNames.forEach(v => this.removeAllListeners(v))
200
- this.apiNames.clear()
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
- * RedisAPI <IOSignal API>
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.req('redis','hget','user:id' )
8
+ * await io.call('redis','hget','user:id' )
9
9
  *
10
10
  */
11
- import { STATUS } from './api_constant.js'
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 RedisAPI {
21
+ export class RedisService {
22
22
  constructor(redisClient, _minLevel) {
23
23
  if (!redisClient) {
24
- throw new Error("RedisAPI constructor: no redisClient")
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 request(remote, req) {
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 API example.
3
+ * simple IOSignal RPC Service example.
4
4
  */
5
5
 
6
- import { STATUS } from './api_constant.js'
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.req('reply','echo','hello')
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.req('reply','date')
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.req('reply','unixtime')
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 './api_constant.js'
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 request(remote, req) {
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 = "api sudo: no such a cmd: " + cmd
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
@@ -5,7 +5,7 @@
5
5
  "outDir": "./dist/types",
6
6
  "allowJs": true,
7
7
  "moduleResolution": "node",
8
- "target": "es2015",
8
+ "target": "es2020",
9
9
  "lib": ["es2020", "dom"],
10
10
  "baseUrl": ".",
11
11
  "paths": {