|
1 | | -const EventEmitter = require('events').EventEmitter; |
2 | | -const net = require('net'); |
3 | | - |
4 | 1 | const constants = require('./lib/constants'); |
5 | | -const message = require('./lib/message'); |
6 | | -const clientHandshake = require('./lib/handshake'); |
7 | 2 | const MessageBus = require('./lib/bus'); |
8 | | -const {getDbusAddressFromFs} = require('./lib/address-x11'); |
9 | 3 | const errors = require('./lib/errors'); |
10 | 4 | const variant = require('./lib/service/variant'); |
11 | 5 | const iface = require('./lib/service/interface'); |
12 | | - |
13 | | -function createStream(opts) { |
14 | | - let { busAddress } = opts; |
15 | | - |
16 | | - // XXX according to the dbus spec, we should start a new server if the bus |
17 | | - // address cannot be found. |
18 | | - if (!busAddress) { |
19 | | - busAddress = process.env.DBUS_SESSION_BUS_ADDRESS; |
20 | | - } |
21 | | - if (!busAddress) { |
22 | | - busAddress = getDbusAddressFromFs(); |
23 | | - } |
24 | | - |
25 | | - let addresses = busAddress.split(';'); |
26 | | - for (let i = 0; i < addresses.length; ++i) { |
27 | | - let address = addresses[i]; |
28 | | - let familyParams = address.split(':'); |
29 | | - let family = familyParams[0]; |
30 | | - let params = {}; |
31 | | - familyParams[1].split(',').map(function(p) { |
32 | | - let keyVal = p.split('='); |
33 | | - params[keyVal[0]] = keyVal[1]; |
34 | | - }); |
35 | | - |
36 | | - try { |
37 | | - switch (family.toLowerCase()) { |
38 | | - case 'tcp': |
39 | | - throw new Error('tcp dbus connections are not supported'); |
40 | | - case 'unix': |
41 | | - if (params.socket) { |
42 | | - return net.createConnection(params.socket); |
43 | | - } |
44 | | - if (params.abstract) { |
45 | | - let abs = require('abstract-socket'); |
46 | | - return abs.connect('\u0000' + params.abstract); |
47 | | - } |
48 | | - if (params.path) { |
49 | | - return net.createConnection(params.path); |
50 | | - } |
51 | | - throw new Error( |
52 | | - "not enough parameters for 'unix' connection - you need to specify 'socket' or 'abstract' or 'path' parameter" |
53 | | - ); |
54 | | - case 'unixexec': |
55 | | - let eventStream = require('event-stream'); |
56 | | - let spawn = require('child_process').spawn; |
57 | | - let args = []; |
58 | | - for (let n = 1; params['arg' + n]; n++) args.push(params['arg' + n]); |
59 | | - let child = spawn(params.path, args); |
60 | | - |
61 | | - return eventStream.duplex(child.stdin, child.stdout); |
62 | | - default: |
63 | | - throw new Error('unknown address type:' + family); |
64 | | - } |
65 | | - } catch (e) { |
66 | | - if (i < addresses.length - 1) { |
67 | | - console.warn(e.message); |
68 | | - continue; |
69 | | - } else { |
70 | | - throw e; |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | -} |
75 | | - |
76 | | -function createConnection(opts) { |
77 | | - let self = new EventEmitter(); |
78 | | - opts = opts || {}; |
79 | | - let stream = (self.stream = createStream(opts)); |
80 | | - stream.setNoDelay(); |
81 | | - |
82 | | - stream.on('error', function(err) { |
83 | | - // forward network and stream errors |
84 | | - self.emit('error', err); |
85 | | - }); |
86 | | - |
87 | | - stream.on('end', function() { |
88 | | - self.emit('end'); |
89 | | - self.message = function() { |
90 | | - self.emit('error', new Error('Tried to write a message to a closed stream')); |
91 | | - }; |
92 | | - }); |
93 | | - |
94 | | - self.end = function() { |
95 | | - stream.end(); |
96 | | - return self; |
97 | | - }; |
98 | | - |
99 | | - clientHandshake(stream, opts, function(error, guid) { |
100 | | - if (error) { |
101 | | - return self.emit('error', error); |
102 | | - } |
103 | | - self.guid = guid; |
104 | | - self.emit('connect'); |
105 | | - message.unmarshalMessages( |
106 | | - stream, |
107 | | - function(message) { |
108 | | - self.emit('message', message); |
109 | | - }, |
110 | | - opts |
111 | | - ); |
112 | | - }); |
113 | | - |
114 | | - self._messages = []; |
115 | | - |
116 | | - // pre-connect version, buffers all messages. replaced after connect |
117 | | - self.message = function(msg) { |
118 | | - self._messages.push(msg); |
119 | | - }; |
120 | | - |
121 | | - self.once('connect', function() { |
122 | | - self.state = 'connected'; |
123 | | - for (let i = 0; i < self._messages.length; ++i) { |
124 | | - stream.write(message.marshall(self._messages[i])); |
125 | | - } |
126 | | - self._messages.length = 0; |
127 | | - |
128 | | - // no need to buffer once connected |
129 | | - self.message = function(msg) { |
130 | | - stream.write(message.marshall(msg)); |
131 | | - }; |
132 | | - }); |
133 | | - |
134 | | - return self; |
135 | | -} |
| 6 | +const createConnection = require('./lib/connection.js'); |
136 | 7 |
|
137 | 8 | let createClient = function(params) { |
138 | 9 | let connection = createConnection(params || {}); |
|
0 commit comments