@@ -2,30 +2,29 @@ const EventEmitter = require('events').EventEmitter;
2
2
const constants = require ( './constants' ) ;
3
3
const handleMethod = require ( './service/handlers' ) ;
4
4
const { NameExistsError } = require ( './errors' ) ;
5
-
6
- // new
7
5
const Name = require ( './service/name' ) ;
6
+
8
7
let {
9
8
assertBusNameValid,
10
9
assertObjectPathValid,
11
10
assertInterfaceNameValid,
12
11
} = require ( './validators' ) ;
12
+
13
13
let ProxyObject = require ( './client/proxy-object' ) ;
14
14
let { Interface } = require ( './service/interface' ) ;
15
15
16
- module . exports = function bus ( conn , opts ) {
16
+ module . exports = function bus ( conn ) {
17
17
if ( ! ( this instanceof bus ) ) {
18
18
return new bus ( conn ) ;
19
19
}
20
- if ( ! opts ) opts = { } ;
21
20
22
- var self = this ;
21
+ let self = this ;
23
22
this . connection = conn ;
24
23
this . serial = 1 ;
25
24
this . cookies = { } ; // TODO: rename to methodReturnHandlers
26
25
this . signals = new EventEmitter ( ) ;
27
26
this . exportedObjects = { } ;
28
-
27
+ this . _names = { } ;
29
28
this . nameOwners = { } ;
30
29
31
30
this . _handleNameOwnerChanged = function ( msg ) {
@@ -44,36 +43,37 @@ module.exports = function bus(conn, opts) {
44
43
} ;
45
44
46
45
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
+ }
48
49
msg . serial = self . serial ++ ;
49
50
this . cookies [ msg . serial ] = callback ;
50
51
self . connection . message ( msg ) ;
51
52
} ;
52
53
53
54
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
+ }
57
64
self . invoke ( msg , callback ) ;
58
65
} ;
59
66
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
+ } ) ;
73
73
} ;
74
74
75
75
this . sendSignal = function ( path , iface , name , signature , args ) {
76
- var signalMsg = {
76
+ let signalMsg = {
77
77
type : constants . messageType . signal ,
78
78
serial : self . serial ++ ,
79
79
interface : iface ,
@@ -89,7 +89,7 @@ module.exports = function bus(conn, opts) {
89
89
90
90
// Warning: errorName must respect the same rules as interface names (must contain a dot)
91
91
this . sendError = function ( msg , errorName , errorText ) {
92
- var reply = {
92
+ let reply = {
93
93
type : constants . messageType . error ,
94
94
serial : self . serial ++ ,
95
95
replySerial : msg . serial ,
@@ -102,7 +102,7 @@ module.exports = function bus(conn, opts) {
102
102
} ;
103
103
104
104
this . sendReply = function ( msg , signature , body ) {
105
- var reply = {
105
+ let reply = {
106
106
type : constants . messageType . methodReturn ,
107
107
serial : self . serial ++ ,
108
108
replySerial : msg . serial ,
@@ -115,50 +115,18 @@ module.exports = function bus(conn, opts) {
115
115
116
116
// route reply/error
117
117
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 ] ;
153
121
if ( handler ) {
154
122
delete self . cookies [ msg . replySerial ] ;
155
- var props = {
123
+ let props = {
156
124
connection : self . connection ,
157
125
bus : self ,
158
126
message : msg ,
159
127
signature : msg . signature
160
128
} ;
161
- var args = msg . body || [ ] ;
129
+ let args = msg . body || [ ] ;
162
130
if ( msg . type === constants . messageType . methodReturn ) {
163
131
args = [ null ] . concat ( args ) ; // first argument - no errors, null
164
132
handler . apply ( props , args ) ; // body as array of arguments
@@ -171,153 +139,23 @@ module.exports = function bus(conn, opts) {
171
139
self . signals . emit ( self . mangle ( msg ) , msg ) ;
172
140
} else {
173
141
// 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` ) ;
201
146
}
202
147
}
203
148
} ) ;
204
149
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
242
150
this . getProxyObject = function ( name , path ) {
243
151
let obj = new ProxyObject ( this , name , path ) ;
244
152
return obj . _init ( ) ;
245
153
} ;
246
154
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
+ } ) ;
321
159
322
160
// bus meta functions
323
161
this . addMatch = function ( match , callback ) {
@@ -334,10 +172,6 @@ module.exports = function bus(conn, opts) {
334
172
) ;
335
173
} ;
336
174
337
- this . getId = function ( callback ) {
338
- this . invokeDbus ( { member : 'GetId' } , callback ) ;
339
- } ;
340
-
341
175
this . requestName = function ( name , flags ) {
342
176
let that = this ;
343
177
flags = flags || 0 ;
@@ -368,46 +202,6 @@ module.exports = function bus(conn, opts) {
368
202
} ) ;
369
203
} ;
370
204
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
-
411
205
this . getNameOwner = function ( name , callback ) {
412
206
this . invokeDbus (
413
207
{ member : 'GetNameOwner' , signature : 's' , body : [ name ] } ,
@@ -427,11 +221,4 @@ module.exports = function bus(conn, opts) {
427
221
} ) ;
428
222
} ) ;
429
223
}
430
-
431
- this . nameHasOwner = function ( name , callback ) {
432
- this . invokeDbus (
433
- { member : 'NameHasOwner' , signature : 's' , body : [ name ] } ,
434
- callback
435
- ) ;
436
- } ;
437
224
} ;
0 commit comments