scoundrel-remote-eval 1.0.0 → 1.0.1

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.1",
5
5
  "description": "",
6
6
  "main": "src/index.js",
7
7
  "scripts": {
@@ -1,11 +1,10 @@
1
1
  import Logger from "../../../logger.js"
2
2
 
3
- const logger = new Logger("Scoundrel WebSocket")
4
-
5
- // logger.setDebug(true)
6
-
7
3
  export default class WebSocket {
8
4
  constructor(ws) {
5
+ this.logger = new Logger("Scoundrel WebSocket")
6
+ // this.logger.setDebug(true)
7
+
9
8
  this.ws = ws
10
9
  this.ws.addEventListener("error", this.onSocketError)
11
10
  this.ws.addEventListener("open", this.onSocketOpen)
@@ -20,7 +19,7 @@ export default class WebSocket {
20
19
  }
21
20
 
22
21
  onSocketError = (event) => {
23
- logger.error(() => ["onSocketError", event])
22
+ this.logger.error(() => ["onSocketError", event])
24
23
  }
25
24
 
26
25
  onSocketMessage = (event) => {
@@ -41,7 +40,7 @@ export default class WebSocket {
41
40
  }
42
41
 
43
42
  onSocketOpen = (event) => {
44
- logger.log(() =>"onSocketOpen")
43
+ this.logger.log(() =>"onSocketOpen")
45
44
  }
46
45
 
47
46
  send(data) {
@@ -53,9 +52,7 @@ export default class WebSocket {
53
52
  })
54
53
 
55
54
  this.commands[commandCount] = {resolve, reject}
56
-
57
- logger.log(() => ["Sending", sendData])
58
-
55
+ this.logger.log(() => ["Sending", sendData])
59
56
  this.ws.send(sendData)
60
57
  })
61
58
  }
@@ -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
  }