scoundrel-remote-eval 1.0.0 → 1.0.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "scoundrel-remote-eval",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.2",
5
5
  "description": "",
6
6
  "main": "src/index.js",
7
7
  "scripts": {
package/src/logger.js CHANGED
@@ -8,7 +8,15 @@ export default class Logger {
8
8
  this.debug = newValue
9
9
  }
10
10
 
11
+ error(...args) {
12
+ return this._sendToConsole("error", ...args)
13
+ }
14
+
11
15
  log(...args) {
16
+ return this._sendToConsole("log", ...args)
17
+ }
18
+
19
+ _sendToConsole(logType, ...args) {
12
20
  if (!this.debug) {
13
21
  return
14
22
  }
@@ -17,12 +25,12 @@ export default class Logger {
17
25
  const callbackArgs = args[0]()
18
26
 
19
27
  if (Array.isArray(callbackArgs)) {
20
- console.log(this.scopeName, ...callbackArgs)
28
+ console[logType](this.scopeName, ...callbackArgs)
21
29
  } else {
22
- console.log(this.scopeName, callbackArgs)
30
+ console[logType](this.scopeName, callbackArgs)
23
31
  }
24
32
  } else {
25
- console.log(this.scopeName, ...args)
33
+ console[logType](this.scopeName, ...args)
26
34
  }
27
35
  }
28
36
  }
@@ -1,9 +1,10 @@
1
1
  export default class ServerClient {
2
- constructor(clientBackend) {
2
+ constructor(clientBackend, server) {
3
3
  this.clientBackend = clientBackend
4
4
  this.clientBackend.onCommand(this.onCommand)
5
5
  this.objects = {}
6
6
  this.objectsCount = 0
7
+ this.server = server
7
8
  }
8
9
 
9
10
  onCommand = (commandId, data) => {
@@ -14,11 +15,17 @@ export default class ServerClient {
14
15
  let object
15
16
 
16
17
  if (typeof className == "string") {
17
- const classInstance = global[className]
18
+ const ServerClassInstance = this.server.getClass(className)
18
19
 
19
- if (!classInstance) throw new Error(`No such class: ${className}`)
20
+ if (ServerClassInstance) {
21
+ object = new ServerClassInstance[className](...data.args)
22
+ } else {
23
+ const classInstance = global[className]
20
24
 
21
- object = new global[className](...data.args)
25
+ if (!classInstance) throw new Error(`No such class: ${className}`)
26
+
27
+ object = new global[className](...data.args)
28
+ }
22
29
  } else {
23
30
  throw new Error(`Don't know how to handle class name: ${typeof className}`)
24
31
  }
@@ -5,13 +5,24 @@ export default class ScoundrelServer {
5
5
  this.backend = backend
6
6
  this.backend.onNewClient(this.onNewClient)
7
7
  this.clients = []
8
+ this._classes = {}
8
9
  }
9
10
 
10
11
  close = () => this.backend.close()
11
12
 
12
13
  onNewClient = (clientBackend) => {
13
- const client = new Client(clientBackend)
14
+ const client = new Client(clientBackend, this)
14
15
 
15
16
  this.clients.push(client)
16
17
  }
18
+
19
+ registerClass(className, classInstance) {
20
+ if (className in this._classes) throw new Error(`Class already exists: ${className}`)
21
+
22
+ this._classes[className] = classInstance
23
+ }
24
+
25
+ getClass(className) {
26
+ return this._classes[className]
27
+ }
17
28
  }