scoundrel-remote-eval 1.0.3 → 1.0.5

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.3",
4
+ "version": "1.0.5",
5
5
  "description": "",
6
6
  "main": "src/index.js",
7
7
  "scripts": {
@@ -37,4 +37,21 @@ describe("referenceWithProxy", () => {
37
37
 
38
38
  expect(result).toEqual(["test1", "test2"])
39
39
  })
40
+
41
+ it("reads attributes from a reference", async () => {
42
+ const testArray = await shared.client.newObjectWithReference("Array")
43
+
44
+ await testArray.callMethod("push", "test1")
45
+ await testArray.callMethod("push", "test2")
46
+
47
+ const result = await testArray.serialize()
48
+
49
+ expect(result).toEqual(["test1", "test2"])
50
+
51
+ const firstValue = await testArray.readAttribute(0)
52
+ const secondValue = await testArray.readAttribute(1)
53
+
54
+ expect(firstValue).toEqual("test1")
55
+ expect(secondValue).toEqual("test2")
56
+ })
40
57
  })
@@ -35,4 +35,15 @@ describe("scoundrel - web-socket - javascript", () => {
35
35
 
36
36
  expect(result).toEqual(["test1", "test2"])
37
37
  })
38
+
39
+ it("returns results from method calls", async () => {
40
+ const stringObject = await shared.client.newObjectWithReference("Array")
41
+
42
+ await stringObject.callMethod("push", "test1")
43
+ await stringObject.callMethod("push", "test2")
44
+
45
+ const result = await stringObject.callMethod("join", ", ")
46
+
47
+ expect(result).toEqual("test1, test2")
48
+ })
38
49
  })
@@ -16,13 +16,15 @@ export default class Client {
16
16
  }
17
17
 
18
18
  async callMethodOnReference(referenceId, methodName, ...args) {
19
- return await this.backend.send({
19
+ const result = await this.backend.send({
20
20
  args: this.parseArg(args),
21
21
  command: "call_method_on_reference",
22
22
  method_name: methodName,
23
23
  reference_id: referenceId,
24
24
  with: "result"
25
25
  })
26
+
27
+ return result.response
26
28
  }
27
29
 
28
30
  async callMethodOnReferenceWithReference(referenceId, methodName, ...args) {
@@ -62,6 +64,19 @@ export default class Client {
62
64
  return this.spawnReference(id)
63
65
  }
64
66
 
67
+ async getObject(objectName) {
68
+ const result = await this.backend.send({
69
+ command: "get_object",
70
+ object_name: objectName
71
+ })
72
+
73
+ if (!result) throw new Error("Blank result given")
74
+
75
+ const id = result.object_id
76
+
77
+ return this.spawnReference(id)
78
+ }
79
+
65
80
  async newObjectWithReference(className, ...args) {
66
81
  const result = await this.backend.send({
67
82
  args: this.parseArg(args),
@@ -76,7 +91,7 @@ export default class Client {
76
91
  return this.spawnReference(id)
77
92
  }
78
93
 
79
- isPlainObject = (input) => {
94
+ isPlainObject(input) {
80
95
  if (input && typeof input === "object" && !Array.isArray(input)) {
81
96
  return true
82
97
  }
@@ -119,6 +134,16 @@ export default class Client {
119
134
  return this.spawnReference(id)
120
135
  }
121
136
 
137
+ async readAttributeOnReference(referenceId, attributeName) {
138
+ const result = await this.backend.send({
139
+ command: "read_attribute",
140
+ attribute_name: attributeName,
141
+ reference_id: referenceId,
142
+ with: "result"
143
+ })
144
+ return result.response
145
+ }
146
+
122
147
  async serializeReference(referenceId) {
123
148
  const json = await this.backend.send({command: "serialize_reference", reference_id: referenceId})
124
149
 
@@ -14,6 +14,10 @@ export default class Reference {
14
14
  return await this.client.callMethodOnReferenceWithReference(this.id, methodName, ...args)
15
15
  }
16
16
 
17
+ async readAttribute(attributeName, ...args) {
18
+ return await this.client.readAttributeOnReference(this.id, attributeName, ...args)
19
+ }
20
+
17
21
  async readAttributeWithReference(attributeName, ...args) {
18
22
  return await this.client.readAttributeOnReferenceWithReference(this.id, attributeName, ...args)
19
23
  }
@@ -9,9 +9,24 @@ export default class ServerClient {
9
9
 
10
10
  onCommand = (commandId, data) => {
11
11
  try {
12
- if (data.command == "new_object_with_reference") {
13
- const className = data.class_name
12
+ if (data.command == "get_object") {
13
+ const serverObject = this.server.getObject(data.object_name)
14
+ let object
15
+
16
+ if (serverObject) {
17
+ object = serverObject
18
+ } else {
19
+ object = global[data.object_name]
20
+
21
+ if (!object) throw new Error(`No such object: ${data.object_name}`)
22
+ }
23
+
14
24
  const objectId = ++this.objectsCount
25
+
26
+ this.objects[objectId] = object
27
+ this.respondToCommand(commandId, {object_id: objectId})
28
+ } else if (data.command == "new_object_with_reference") {
29
+ const className = data.class_name
15
30
  let object
16
31
 
17
32
  if (typeof className == "string") {
@@ -30,8 +45,9 @@ export default class ServerClient {
30
45
  throw new Error(`Don't know how to handle class name: ${typeof className}`)
31
46
  }
32
47
 
33
- this.objects[objectId] = object
48
+ const objectId = ++this.objectsCount
34
49
 
50
+ this.objects[objectId] = object
35
51
  this.respondToCommand(commandId, {object_id: objectId})
36
52
  } else if (data.command == "call_method_on_reference") {
37
53
  const referenceId = data.reference_id
@@ -53,6 +69,24 @@ export default class ServerClient {
53
69
  if (!object) throw new Error(`No object by that ID: ${referenceId}`)
54
70
 
55
71
  this.respondToCommand(commandId, JSON.stringify(object))
72
+ } else if (data.command == "read_attribute") {
73
+ const attributeName = data.attribute_name
74
+ const referenceId = data.reference_id
75
+ const returnWith = data.with
76
+ const object = this.objects[referenceId]
77
+
78
+ if (!object) throw new Error(`No object by that ID: ${referenceId}`)
79
+
80
+ const attribute = object[attributeName]
81
+
82
+ if (returnWith == "reference") {
83
+ const objectId = ++this.objectsCount
84
+
85
+ this.objects[objectId] = attribute
86
+ this.respondToCommand(commandId, {response: objectId})
87
+ } else {
88
+ this.respondToCommand(commandId, {response: attribute})
89
+ }
56
90
  } else {
57
91
  this.clientBackend.send({type: "command_response", command_id: commandId, error: `Unknown command: ${data.command}`})
58
92
  }
@@ -6,9 +6,10 @@ export default class ScoundrelServer {
6
6
  this.backend.onNewClient(this.onNewClient)
7
7
  this.clients = []
8
8
  this._classes = {}
9
+ this._objects = {}
9
10
  }
10
11
 
11
- close = () => this.backend.close()
12
+ close() { this.backend.close() }
12
13
 
13
14
  onNewClient = (clientBackend) => {
14
15
  const client = new Client(clientBackend, this)
@@ -22,7 +23,17 @@ export default class ScoundrelServer {
22
23
  this._classes[className] = classInstance
23
24
  }
24
25
 
26
+ registerObject(objectName, objectInstance) {
27
+ if (objectName in this._objects) throw new Error(`Object already exists: ${objectName}`)
28
+
29
+ this._objects[objectName] = objectInstance
30
+ }
31
+
25
32
  getClass(className) {
26
33
  return this._classes[className]
27
34
  }
35
+
36
+ getObject(objectName) {
37
+ return this._objects[objectName]
38
+ }
28
39
  }