scoundrel-remote-eval 1.0.3 → 1.0.4
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 +1 -1
- package/spec/reference-with-proxy-spec.js +17 -0
- package/src/client/index.js +24 -1
- package/src/client/reference.js +4 -0
- package/src/server/client/index.js +37 -3
- package/src/server/index.js +12 -1
package/package.json
CHANGED
|
@@ -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
|
})
|
package/src/client/index.js
CHANGED
|
@@ -62,6 +62,19 @@ export default class Client {
|
|
|
62
62
|
return this.spawnReference(id)
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
async getObject(objectName) {
|
|
66
|
+
const result = await this.backend.send({
|
|
67
|
+
command: "get_object",
|
|
68
|
+
object_name: objectName
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
if (!result) throw new Error("Blank result given")
|
|
72
|
+
|
|
73
|
+
const id = result.object_id
|
|
74
|
+
|
|
75
|
+
return this.spawnReference(id)
|
|
76
|
+
}
|
|
77
|
+
|
|
65
78
|
async newObjectWithReference(className, ...args) {
|
|
66
79
|
const result = await this.backend.send({
|
|
67
80
|
args: this.parseArg(args),
|
|
@@ -76,7 +89,7 @@ export default class Client {
|
|
|
76
89
|
return this.spawnReference(id)
|
|
77
90
|
}
|
|
78
91
|
|
|
79
|
-
isPlainObject
|
|
92
|
+
isPlainObject(input) {
|
|
80
93
|
if (input && typeof input === "object" && !Array.isArray(input)) {
|
|
81
94
|
return true
|
|
82
95
|
}
|
|
@@ -119,6 +132,16 @@ export default class Client {
|
|
|
119
132
|
return this.spawnReference(id)
|
|
120
133
|
}
|
|
121
134
|
|
|
135
|
+
async readAttributeOnReference(referenceId, attributeName) {
|
|
136
|
+
const result = await this.backend.send({
|
|
137
|
+
command: "read_attribute",
|
|
138
|
+
attribute_name: attributeName,
|
|
139
|
+
reference_id: referenceId,
|
|
140
|
+
with: "result"
|
|
141
|
+
})
|
|
142
|
+
return result.response
|
|
143
|
+
}
|
|
144
|
+
|
|
122
145
|
async serializeReference(referenceId) {
|
|
123
146
|
const json = await this.backend.send({command: "serialize_reference", reference_id: referenceId})
|
|
124
147
|
|
package/src/client/reference.js
CHANGED
|
@@ -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 == "
|
|
13
|
-
const
|
|
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
|
-
|
|
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
|
}
|
package/src/server/index.js
CHANGED
|
@@ -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
|
|
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
|
}
|