Skip to content

Commit 4518825

Browse files
author
Tony Crisci
committed
proxy-object: init from xml string
1 parent e84b91b commit 4518825

File tree

3 files changed

+77
-24
lines changed

3 files changed

+77
-24
lines changed

lib/bus.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ class MessageBus extends EventEmitter {
169169
*
170170
* @param name {string} - the well-known name on the bus.
171171
* @param path {string} - the object path exported on the name.
172+
* @param [xml] {string} - xml introspection data.
172173
* @returns {Promise} - a Promise that resolves with the `ProxyObject`.
173174
*/
174-
getProxyObject(name, path) {
175+
getProxyObject(name, path, xml) {
175176
let obj = new ProxyObject(this, name, path);
176-
return obj._init();
177+
return obj._init(xml);
177178
};
178179

179180
/**

lib/client/proxy-object.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,41 @@ class ProxyObject {
113113
}
114114
}
115115

116-
_init() {
116+
_init(xml) {
117117
return new Promise((resolve, reject) => {
118-
let introspectMessage = new Message({
119-
destination: this.name,
120-
path: this.path,
121-
interface: 'org.freedesktop.DBus.Introspectable',
122-
member: 'Introspect',
123-
signature: '',
124-
body: []
125-
});
118+
if (xml) {
119+
this._parser.parseString(xml, (err, data) => {
120+
if (err) {
121+
return reject(err);
122+
}
123+
this._initXml(data);
124+
resolve(this);
125+
});
126+
} else {
127+
let introspectMessage = new Message({
128+
destination: this.name,
129+
path: this.path,
130+
interface: 'org.freedesktop.DBus.Introspectable',
131+
member: 'Introspect',
132+
signature: '',
133+
body: []
134+
});
126135

127-
this.bus.call(introspectMessage)
128-
.then((msg) => {
129-
let xml = msg.body[0];
130-
this._parser.parseString(xml, (err, data) => {
131-
if (err) {
132-
return reject(err);
133-
}
134-
this._initXml(data);
135-
resolve(this);
136+
this.bus.call(introspectMessage)
137+
.then((msg) => {
138+
let xml = msg.body[0];
139+
this._parser.parseString(xml, (err, data) => {
140+
if (err) {
141+
return reject(err);
142+
}
143+
this._initXml(data);
144+
resolve(this);
145+
});
146+
})
147+
.catch((err) => {
148+
return reject(err);
136149
});
137-
})
138-
.catch((err) => {
139-
return reject(err);
140-
});
150+
}
141151
});
142152
}
143153

test/integration/client-standard-dbus.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,45 @@ test('get stats', async () => {
3131
expect(busNames.signature).toBe('u');
3232
expect(busNames.value).toBeGreaterThan(0);
3333
});
34+
35+
test('provided xml', async () => {
36+
let xml = `
37+
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
38+
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
39+
<node name="/com/example/sample_object0">
40+
<interface name="com.example.SampleInterface0">
41+
<method name="Frobate">
42+
<arg name="foo" type="i" direction="in"/>
43+
<arg name="bar" type="s" direction="out"/>
44+
<arg name="baz" type="a{us}" direction="out"/>
45+
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
46+
</method>
47+
<method name="Bazify">
48+
<arg name="bar" type="(iiu)" direction="in"/>
49+
<arg name="bar" type="v" direction="out"/>
50+
</method>
51+
<method name="Mogrify">
52+
<arg name="bar" type="(iiav)" direction="in"/>
53+
</method>
54+
<signal name="Changed">
55+
<arg name="new_value" type="b"/>
56+
</signal>
57+
<signal name="ChangedMulti">
58+
<arg name="new_value1" type="b"/>
59+
<arg name="new_value2" type="y"/>
60+
</signal>
61+
<property name="Bar" type="y" access="write"/>
62+
</interface>
63+
<node name="child_of_sample_object"/>
64+
<node name="another_child_of_sample_object"/>
65+
</node>
66+
`;
67+
let object = await bus.getProxyObject('com.example.Sample', '/com/example/sample_object0', xml);
68+
69+
let iface = object.getInterface('com.example.SampleInterface0');
70+
expect(object.nodes.length).toEqual(2);
71+
expect(iface.Frobate).toBeDefined();
72+
expect(iface.Bazify).toBeDefined();
73+
expect(iface.Mogrify).toBeDefined();
74+
expect(iface.$signals.find((s) => s.name == 'Changed')).toBeDefined();
75+
});

0 commit comments

Comments
 (0)