Skip to content

Commit f8bee01

Browse files
author
Tony Crisci
committed
remove dead code from bus.js
1 parent 8dc54b0 commit f8bee01

File tree

1 file changed

+39
-252
lines changed

1 file changed

+39
-252
lines changed

lib/bus.js

Lines changed: 39 additions & 252 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,29 @@ const EventEmitter = require('events').EventEmitter;
22
const constants = require('./constants');
33
const handleMethod = require('./service/handlers');
44
const { NameExistsError } = require('./errors');
5-
6-
// new
75
const Name = require('./service/name');
6+
87
let {
98
assertBusNameValid,
109
assertObjectPathValid,
1110
assertInterfaceNameValid,
1211
} = require('./validators');
12+
1313
let ProxyObject = require('./client/proxy-object');
1414
let { Interface } = require('./service/interface');
1515

16-
module.exports = function bus(conn, opts) {
16+
module.exports = function bus(conn) {
1717
if (!(this instanceof bus)) {
1818
return new bus(conn);
1919
}
20-
if (!opts) opts = {};
2120

22-
var self = this;
21+
let self = this;
2322
this.connection = conn;
2423
this.serial = 1;
2524
this.cookies = {}; // TODO: rename to methodReturnHandlers
2625
this.signals = new EventEmitter();
2726
this.exportedObjects = {};
28-
27+
this._names = {};
2928
this.nameOwners = {};
3029

3130
this._handleNameOwnerChanged = function(msg) {
@@ -44,36 +43,37 @@ module.exports = function bus(conn, opts) {
4443
};
4544

4645
this.invoke = function(msg, callback) {
47-
if (!msg.type) msg.type = constants.messageType.methodCall;
46+
if (!msg.type) {
47+
msg.type = constants.messageType.methodCall;
48+
}
4849
msg.serial = self.serial++;
4950
this.cookies[msg.serial] = callback;
5051
self.connection.message(msg);
5152
};
5253

5354
this.invokeDbus = function(msg, callback) {
54-
if (!msg.path) msg.path = '/org/freedesktop/DBus';
55-
if (!msg.destination) msg.destination = 'org.freedesktop.DBus';
56-
if (!msg['interface']) msg['interface'] = 'org.freedesktop.DBus';
55+
if (!msg.path) {
56+
msg.path = '/org/freedesktop/DBus';
57+
}
58+
if (!msg.destination) {
59+
msg.destination = 'org.freedesktop.DBus';
60+
}
61+
if (!msg['interface']) {
62+
msg['interface'] = 'org.freedesktop.DBus';
63+
}
5764
self.invoke(msg, callback);
5865
};
5966

60-
this.mangle = function(path, iface, member) {
61-
var obj = {};
62-
if (typeof path === 'object') {
63-
// handle one argumant case mangle(msg)
64-
obj.path = path.path;
65-
obj['interface'] = path['interface'];
66-
obj.member = path.member;
67-
} else {
68-
obj.path = path;
69-
obj['interface'] = iface;
70-
obj.member = member;
71-
}
72-
return JSON.stringify(obj);
67+
this.mangle = function(msg) {
68+
return JSON.stringify({
69+
path: msg.path,
70+
'interface': msg['interface'],
71+
member: msg.member
72+
});
7373
};
7474

7575
this.sendSignal = function(path, iface, name, signature, args) {
76-
var signalMsg = {
76+
let signalMsg = {
7777
type: constants.messageType.signal,
7878
serial: self.serial++,
7979
interface: iface,
@@ -89,7 +89,7 @@ module.exports = function bus(conn, opts) {
8989

9090
// Warning: errorName must respect the same rules as interface names (must contain a dot)
9191
this.sendError = function(msg, errorName, errorText) {
92-
var reply = {
92+
let reply = {
9393
type: constants.messageType.error,
9494
serial: self.serial++,
9595
replySerial: msg.serial,
@@ -102,7 +102,7 @@ module.exports = function bus(conn, opts) {
102102
};
103103

104104
this.sendReply = function(msg, signature, body) {
105-
var reply = {
105+
let reply = {
106106
type: constants.messageType.methodReturn,
107107
serial: self.serial++,
108108
replySerial: msg.serial,
@@ -115,50 +115,18 @@ module.exports = function bus(conn, opts) {
115115

116116
// route reply/error
117117
this.connection.on('message', function(msg) {
118-
function invoke(impl, func, resultSignature) {
119-
Promise.resolve()
120-
.then(function() {
121-
return func.apply(impl, (msg.body || []).concat(msg));
122-
})
123-
.then(
124-
function(methodReturnResult) {
125-
var methodReturnReply = {
126-
type: constants.messageType.methodReturn,
127-
serial: self.serial++,
128-
destination: msg.sender,
129-
replySerial: msg.serial
130-
};
131-
if (methodReturnResult !== null) {
132-
methodReturnReply.signature = resultSignature;
133-
methodReturnReply.body = [methodReturnResult];
134-
}
135-
self.connection.message(methodReturnReply);
136-
},
137-
function(e) {
138-
self.sendError(
139-
msg,
140-
e.dbusName || 'org.freedesktop.DBus.Error.Failed',
141-
e.message || ''
142-
);
143-
}
144-
);
145-
}
146-
147-
var handler;
148-
if (
149-
msg.type === constants.messageType.methodReturn ||
150-
msg.type === constants.messageType.error
151-
) {
152-
handler = self.cookies[msg.replySerial];
118+
if (msg.type === constants.messageType.methodReturn ||
119+
msg.type === constants.messageType.error) {
120+
let handler = self.cookies[msg.replySerial];
153121
if (handler) {
154122
delete self.cookies[msg.replySerial];
155-
var props = {
123+
let props = {
156124
connection: self.connection,
157125
bus: self,
158126
message: msg,
159127
signature: msg.signature
160128
};
161-
var args = msg.body || [];
129+
let args = msg.body || [];
162130
if (msg.type === constants.messageType.methodReturn) {
163131
args = [null].concat(args); // first argument - no errors, null
164132
handler.apply(props, args); // body as array of arguments
@@ -171,153 +139,23 @@ module.exports = function bus(conn, opts) {
171139
self.signals.emit(self.mangle(msg), msg);
172140
} else {
173141
// methodCall
174-
if (handleMethod(msg, self)) {
175-
return;
176-
}
177-
178-
// exported interfaces handlers
179-
var obj, iface, impl;
180-
if ((obj = self.exportedObjects[msg.path])) {
181-
if ((iface = obj[msg['interface']])) {
182-
// now we are ready to serve msg.member
183-
impl = iface[1];
184-
var func = impl[msg.member];
185-
if (!func) {
186-
self.sendError(
187-
msg,
188-
'org.freedesktop.DBus.Error.UnknownMethod',
189-
`Method "${msg.member}" on interface "${msg.interface}" doesn't exist`
190-
);
191-
return;
192-
}
193-
// TODO safety check here
194-
var resultSignature = iface[0].methods[msg.member][1];
195-
invoke(impl, func, resultSignature);
196-
return;
197-
} else {
198-
console.error(`Interface ${msg['interface']} is not supported`);
199-
// TODO: respond with standard dbus error
200-
}
142+
if (!handleMethod(msg, self)) {
143+
self.sendError(msg,
144+
'org.freedesktop.DBus.Error.UnknownMethod',
145+
`Method '${msg.member}' on interface '${msg.interface}' does not exist`);
201146
}
202147
}
203148
});
204149

205-
// new
206-
this._names = {};
207-
208-
this._removedErrorMessage = 'This function has been removed and will not be defined in a future version. Use `bus.requestName()` to request a name on the bus and `name.export()` to export interfaces. See the README for more info on how to use the service interface.';
209-
210-
// removed
211-
this.export = function(name, path, iface, nameFlags) {
212-
throw new Error(this._removedErrorMessage);
213-
}
214-
215-
// removed
216-
this.unexportName = function(name) {
217-
throw new Error(this._removedErrorMessage);
218-
}
219-
220-
// removed
221-
this.unexportPath = function(name, path) {
222-
throw new Error(this._removedErrorMessage);
223-
}
224-
225-
// removed
226-
this.unexportInterface = function(name, path, iface) {
227-
throw new Error(this._removedErrorMessage);
228-
}
229-
230-
// removed
231-
this.releaseName = function(name, callback) {
232-
throw new Error(this._removedErrorMessage);
233-
/*
234-
this.invokeDbus(
235-
{ member: 'ReleaseName', signature: 's', body: [name] },
236-
callback
237-
);
238-
*/
239-
};
240-
241-
// new
242150
this.getProxyObject = function(name, path) {
243151
let obj = new ProxyObject(this, name, path);
244152
return obj._init();
245153
};
246154

247-
// old
248-
this.exportInterface = function(obj, path, iface) {
249-
var entry;
250-
if (!self.exportedObjects[path]) {
251-
entry = self.exportedObjects[path] = {};
252-
} else {
253-
entry = self.exportedObjects[path];
254-
}
255-
entry[iface.name] = [iface, obj];
256-
// monkey-patch obj.emit()
257-
if (typeof obj.emit === 'function') {
258-
var oldEmit = obj.emit;
259-
obj.emit = function() {
260-
var args = Array.prototype.slice.apply(arguments);
261-
var signalName = args[0];
262-
if (!signalName) throw new Error('Trying to emit undefined signal');
263-
264-
//send signal to bus
265-
var signal;
266-
if (iface.signals && iface.signals[signalName]) {
267-
signal = iface.signals[signalName];
268-
var signalMsg = {
269-
type: constants.messageType.signal,
270-
serial: self.serial++,
271-
interface: iface.name,
272-
path: path,
273-
member: signalName
274-
};
275-
if (signal[0]) {
276-
signalMsg.signature = signal[0];
277-
signalMsg.body = args.slice(1);
278-
}
279-
self.connection.message(signalMsg);
280-
self.serial++;
281-
}
282-
// note that local emit is likely to be called before signal arrives
283-
// to remote subscriber
284-
oldEmit.apply(obj, args);
285-
};
286-
}
287-
// TODO: emit ObjectManager's InterfaceAdded
288-
};
289-
290-
// register name
291-
if (opts.direct !== true) {
292-
this.invokeDbus({ member: 'Hello' }, function(err, name) {
293-
if (err) throw new Error(err);
294-
self.name = name;
295-
});
296-
} else {
297-
self.name = null;
298-
}
299-
300-
function DBusObject(name, service) {
301-
this.name = name;
302-
this.service = service;
303-
this.as = function(name) {
304-
return this.proxy[name];
305-
};
306-
}
307-
308-
this.getObject = function(path, name, callback) {
309-
var service = this.getService(path);
310-
return service.getObject(name, callback);
311-
};
312-
313-
this.getInterface = function(path, objname, name, callback) {
314-
return this.getObject(path, objname, function(err, obj) {
315-
if (err) return callback(err);
316-
callback(null, obj.as(name));
317-
});
318-
};
319-
320-
// TODO: refactor
155+
this.invokeDbus({ member: 'Hello' }, function(err, name) {
156+
if (err) throw new Error(err);
157+
self.name = name;
158+
});
321159

322160
// bus meta functions
323161
this.addMatch = function(match, callback) {
@@ -334,10 +172,6 @@ module.exports = function bus(conn, opts) {
334172
);
335173
};
336174

337-
this.getId = function(callback) {
338-
this.invokeDbus({ member: 'GetId' }, callback);
339-
};
340-
341175
this.requestName = function(name, flags) {
342176
let that = this;
343177
flags = flags || 0;
@@ -368,46 +202,6 @@ module.exports = function bus(conn, opts) {
368202
});
369203
};
370204

371-
this.listNames = function(callback) {
372-
this.invokeDbus({ member: 'ListNames' }, callback);
373-
};
374-
375-
this.listActivatableNames = function(callback) {
376-
this.invokeDbus({ member: 'ListActivatableNames' }, callback);
377-
};
378-
379-
this.updateActivationEnvironment = function(env, callback) {
380-
this.invokeDbus(
381-
{
382-
member: 'UpdateActivationEnvironment',
383-
signature: 'a{ss}',
384-
body: [env]
385-
},
386-
callback
387-
);
388-
};
389-
390-
this.startServiceByName = function(name, flags, callback) {
391-
this.invokeDbus(
392-
{ member: 'StartServiceByName', signature: 'su', body: [name, flags] },
393-
callback
394-
);
395-
};
396-
397-
this.getConnectionUnixUser = function(name, callback) {
398-
this.invokeDbus(
399-
{ member: 'GetConnectionUnixUser', signature: 's', body: [name] },
400-
callback
401-
);
402-
};
403-
404-
this.getConnectionUnixProcessId = function(name, callback) {
405-
this.invokeDbus(
406-
{ member: 'GetConnectionUnixProcessID', signature: 's', body: [name] },
407-
callback
408-
);
409-
};
410-
411205
this.getNameOwner = function(name, callback) {
412206
this.invokeDbus(
413207
{ member: 'GetNameOwner', signature: 's', body: [name] },
@@ -427,11 +221,4 @@ module.exports = function bus(conn, opts) {
427221
});
428222
});
429223
}
430-
431-
this.nameHasOwner = function(name, callback) {
432-
this.invokeDbus(
433-
{ member: 'NameHasOwner', signature: 's', body: [name] },
434-
callback
435-
);
436-
};
437224
};

0 commit comments

Comments
 (0)