Skip to content

Commit df509e5

Browse files
committed
Add support for peer-to-peer DBus
1 parent 850786d commit df509e5

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ module.exports.sessionBus = function(opts) {
4141
return createClient(opts);
4242
};
4343

44+
/**
45+
* Create a new {@link MessageBus} client to communicate peer-to-peer.
46+
*
47+
* @param {object} [stream] - Duplex stream for communication.
48+
* @param {object} [options] - Options for `MessageBus` creation.
49+
*/
50+
module.exports.peerBus = function(stream, opts) {
51+
return createClient(Object.assign({}, opts, { stream }));
52+
};
53+
4454
/**
4555
* Use JSBI as a polyfill for long integer types ('x' and 't') in the client
4656
* and the service. This is required for Node verisons that do not support the

lib/bus.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MessageBus extends EventEmitter {
7474

7575
/**
7676
* The unique name of the bus connection. This will be `null` until the
77-
* `MessageBus` is connected.
77+
* `MessageBus` is connected, unless we're operating in peer-to-peer mode.
7878
* @memberof MessageBus#
7979
* @member {string} name
8080
*/
@@ -159,22 +159,28 @@ class MessageBus extends EventEmitter {
159159
this.emit('error', err);
160160
});
161161

162-
const helloMessage = new Message({
163-
path: '/org/freedesktop/DBus',
164-
destination: 'org.freedesktop.DBus',
165-
interface: 'org.freedesktop.DBus',
166-
member: 'Hello'
167-
});
168-
169-
this.call(helloMessage)
170-
.then((msg) => {
171-
this.name = msg.body[0];
162+
if (conn.mode === 'p2p') {
163+
process.nextTick(() => {
172164
this.emit('connect');
173-
})
174-
.catch((err) => {
175-
this.emit('error', err);
176-
throw new Error(err);
177165
});
166+
} else {
167+
const helloMessage = new Message({
168+
path: '/org/freedesktop/DBus',
169+
destination: 'org.freedesktop.DBus',
170+
interface: 'org.freedesktop.DBus',
171+
member: 'Hello'
172+
});
173+
174+
this.call(helloMessage)
175+
.then((msg) => {
176+
this.name = msg.body[0];
177+
this.emit('connect');
178+
})
179+
.catch((err) => {
180+
this.emit('error', err);
181+
throw new Error(err);
182+
});
183+
}
178184
}
179185

180186
/**
@@ -468,6 +474,10 @@ class MessageBus extends EventEmitter {
468474

469475
this._matchRules[match] = 1;
470476

477+
if (this._connection.mode === 'p2p') {
478+
return Promise.resolve();
479+
}
480+
471481
// TODO catch error and update refcount
472482
const msg = new Message({
473483
path: '/org/freedesktop/DBus',
@@ -496,6 +506,10 @@ class MessageBus extends EventEmitter {
496506

497507
delete this._matchRules[match];
498508

509+
if (this._connection.mode === 'p2p') {
510+
return Promise.resolve();
511+
}
512+
499513
// TODO catch error and update refcount
500514
const msg = new Message({
501515
path: '/org/freedesktop/DBus',

lib/connection.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,15 @@ function createStream (opts) {
9090
function createConnection (opts) {
9191
const self = new EventEmitter();
9292
opts = opts || {};
93-
const stream = (self.stream = createStream(opts));
94-
stream.setNoDelay && stream.setNoDelay();
93+
let { stream } = opts;
94+
if (stream) {
95+
self.mode = 'p2p';
96+
} else {
97+
self.mode = 'bus';
98+
stream = createStream(opts);
99+
stream.setNoDelay && stream.setNoDelay();
100+
}
101+
self.stream = stream;
95102

96103
stream.on('error', function (err) {
97104
// forward network and stream errors

types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
declare module 'dbus-next' {
33
import { EventEmitter } from "events";
4+
import { Duplex } from "stream";
45

56
export type ObjectPath = string;
67
export type PropertyAccess = "read" | "write" | "readwrite";
@@ -142,4 +143,5 @@ declare module 'dbus-next' {
142143
export function setBigIntCompat(state: boolean): void;
143144
export function systemBus(): MessageBus;
144145
export function sessionBus(options?: BusOptions): MessageBus;
146+
export function peerBus(stream: Duplex, options?: BusOptions): MessageBus;
145147
}

0 commit comments

Comments
 (0)