Skip to content

Commit 6912ae2

Browse files
author
Tony Crisci
committed
move constants to enums
1 parent 402d57c commit 6912ae2

7 files changed

Lines changed: 306 additions & 246 deletions

File tree

index.js

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

44-
/**
45-
* A flag for {@link MessageBus#requestName} to indicate this name allows other
46-
* clients to replace it as the name owner on request.
47-
*
48-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
49-
* @constant DBUS_NAME_FLAG_ALLOW_REPLACEMENT
50-
*/
51-
module.exports.DBUS_NAME_FLAG_ALLOW_REPLACEMENT = constants.DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
52-
53-
/**
54-
* A flag for {@link MessageBus#requestName} to indicate this request should
55-
* replace an existing name if that name allows replacement.
56-
*
57-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
58-
* @constant DBUS_NAME_FLAG_REPLACE_EXISTING
59-
*/
60-
module.exports.DBUS_NAME_FLAG_REPLACE_EXISTING = constants.DBUS_NAME_FLAG_REPLACE_EXISTING;
61-
62-
/**
63-
* A flag for {@link MessageBus#requestName} to indicate this request should
64-
* not enter the queue of clients requesting this name if it is taken.
65-
*
66-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
67-
* @constant DBUS_NAME_FLAG_DO_NOT_QUEUE
68-
*/
69-
module.exports.DBUS_NAME_FLAG_DO_NOT_QUEUE = constants.DBUS_NAME_FLAG_DO_NOT_QUEUE;
70-
71-
/**
72-
* An enum for the return value of {@link MessageBus#requestName} to indicate
73-
* the caller is now the primary owner of the name, replacing any previous
74-
* owner.
75-
*
76-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
77-
* @constant DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
78-
*/
79-
module.exports.DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = constants.DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
80-
81-
/**
82-
* An enum for the return value of {@link MessageBus#requestName} to indicate
83-
* the name already had an owner, `DBUS_NAME_FLAG_DO_NOT_QUEUE` was not
84-
* specified, and either the current owner did not specify
85-
* `DBUS_NAME_FLAG_ALLOW_REPLACEMENT` or the requesting application did not
86-
* specify `DBUS_NAME_FLAG_REPLACE_EXISTING`.
87-
*
88-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
89-
* @constant DBUS_REQUEST_NAME_REPLY_IN_QUEUE
90-
*/
91-
module.exports.DBUS_REQUEST_NAME_REPLY_IN_QUEUE = constants.DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
92-
93-
/**
94-
* An enum for the return value of {@link MessageBus#requestName} to indicate
95-
* the name already has an owner, `DBUS_NAME_FLAG_DO_NOT_QUEUE` was specified,
96-
* and either `DBUS_NAME_FLAG_ALLOW_REPLACEMENT` was not specified by the
97-
* current owner, or `DBUS_NAME_FLAG_REPLACE_EXISTING` was not specified by the
98-
* requesting application.
99-
*
100-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
101-
* @constant DBUS_REQUEST_NAME_REPLY_EXISTS
102-
*/
103-
module.exports.DBUS_REQUEST_NAME_REPLY_EXISTS = constants.DBUS_REQUEST_NAME_REPLY_EXISTS;
104-
105-
/**
106-
* An enum for the return value of {@link MessageBus#requestName} to indicate
107-
* the application trying to request ownership of a name is already the owner
108-
* of it.
109-
*
110-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
111-
* @constant DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
112-
*/
113-
module.exports.DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = constants.DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
114-
115-
/**
116-
* An enumn for the return value of {@link MessageBus#releaseName} to indicate
117-
* the caller has released his claim on the given name. Either the caller was
118-
* the primary owner of the name, and the name is now unused or taken by
119-
* somebody waiting in the queue for the name, or the caller was waiting in the
120-
* queue for the name and has now been removed from the queue.
121-
*
122-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-release-name}
123-
* @constant DBUS_RELEASE_NAME_REPLY_RELEASED
124-
*/
125-
module.exports.DBUS_RELEASE_NAME_REPLY_RELEASED = constants.DBUS_RELEASE_NAME_REPLY_RELEASED;
126-
127-
/**
128-
* An enumn for the return value of {@link MessageBus#releaseName} to indicate
129-
* the given name does not exist on this bus.
130-
*
131-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-release-name}
132-
* @constant DBUS_RELEASE_NAME_REPLY_NON_EXISTENT
133-
*/
134-
module.exports.DBUS_RELEASE_NAME_REPLY_NON_EXISTENT = constants.DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
135-
136-
/**
137-
* An enumn for the return value of {@link MessageBus#releaseName} to indicate
138-
* the caller was not the primary owner of this name, and was also not waiting
139-
* in the queue to own this name.
140-
*
141-
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-release-name}
142-
* @constant DBUS_RELEASE_NAME_REPLY_NOT_OWNER
143-
*/
144-
module.exports.DBUS_RELEASE_NAME_REPLY_NOT_OWNER = constants.DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
145-
146-
/**
147-
* An enum value for the {@link Message} `type` member to indicate the message
148-
* is a method call.
149-
*
150-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
151-
* @constant MESSAGE_TYPE_METHOD_CALL
152-
*/
153-
module.exports.MESSAGE_TYPE_METHOD_CALL = constants.messageType.METHOD_CALL;
154-
155-
/**
156-
* An enum value for the {@link Message} `type` member to indicate the message
157-
* is a method return.
158-
*
159-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
160-
* @constant MESSAGE_TYPE_METHOD_RETURN
161-
*/
162-
module.exports.MESSAGE_TYPE_METHOD_RETURN = constants.messageType.METHOD_RETURN;
163-
164-
/**
165-
* An enum value for the {@link Message} `type` member to indicate the message
166-
* is a signal.
167-
*
168-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
169-
* @constant MESSAGE_TYPE_SIGNAL
170-
*/
171-
module.exports.MESSAGE_TYPE_SIGNAL = constants.messageType.SIGNAL;
172-
173-
/**
174-
* An enum value for the {@link Message} `type` member to indicate the message
175-
* is an error reply.
176-
*
177-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
178-
* @constant MESSAGE_TYPE_ERROR
179-
*/
180-
module.exports.MESSAGE_TYPE_ERROR = constants.messageType.ERROR;
181-
182-
/**
183-
* A flag for the {@link Message} `flags` member to indicate no reply is
184-
* expected from this message.
185-
*
186-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
187-
* @constant MESSAGE_FLAG_NO_REPLY_EXPECTED
188-
*/
189-
module.exports.MESSAGE_FLAG_NO_REPLY_EXPECTED = constants.flags.noReplyExpected;
190-
191-
/**
192-
* A flag for the {@link Message} `flags` member to indicate this message
193-
* should not autostart a service.
194-
*
195-
* @see https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol
196-
* @constant MESSAGE_FLAG_NO_AUTO_START
197-
*/
198-
module.exports.MESSAGE_FLAG_NO_AUTO_START = constants.flags.noAutoStart;
199-
20044
/**
20145
* Use JSBI as a polyfill for long integer types ('x' and 't') in the client
20246
* and the service. This is required for Node verisons that do not support the
@@ -208,6 +52,12 @@ module.exports.MESSAGE_FLAG_NO_AUTO_START = constants.flags.noAutoStart;
20852
*/
20953
module.exports.setBigIntCompat = require('./lib/library-options').setBigIntCompat
21054

55+
module.exports.NameFlag = constants.NameFlag;
56+
module.exports.RequestNameReply = constants.RequestNameReply;
57+
module.exports.ReleaseNameReply = constants.ReleaseNameReply;
58+
module.exports.MessageType = constants.MessageType;
59+
module.exports.MessageFlag = constants.MessageFlag;
60+
21161
module.exports.interface = iface;
21262
module.exports.Variant = Variant;
21363
module.exports.Message = Message;

lib/bus.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@ const {Message} = require('./message-type');
66
const ServiceObject = require('./service/object');
77
const xml2js = require('xml2js');
88

9-
let {
9+
const {
10+
METHOD_CALL,
11+
METHOD_RETURN,
12+
ERROR,
13+
SIGNAL,
14+
} = constants.MessageType;
15+
16+
const {
17+
NO_REPLY_EXPECTED
18+
} = constants.MessageFlag;
19+
20+
const {
1021
assertBusNameValid,
1122
assertObjectPathValid,
1223
assertInterfaceNameValid,
1324
} = require('./validators');
1425

15-
let ProxyObject = require('./client/proxy-object');
16-
let { Interface } = require('./service/interface');
26+
const ProxyObject = require('./client/proxy-object');
27+
const { Interface } = require('./service/interface');
1728

1829
const xmlHeader = `<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">\n`
1930

@@ -67,14 +78,14 @@ class MessageBus extends EventEmitter {
6778
this.name = null;
6879

6980
let handleMessage = (msg) => {
70-
if (msg.type === constants.messageType.METHOD_RETURN ||
71-
msg.type === constants.messageType.ERROR) {
81+
if (msg.type === METHOD_RETURN ||
82+
msg.type === ERROR) {
7283
let handler = this._methodReturnHandlers[msg.replySerial];
7384
if (handler) {
7485
delete this._methodReturnHandlers[msg.replySerial];
7586
handler(msg);
7687
}
77-
} else if (msg.type === constants.messageType.SIGNAL) {
88+
} else if (msg.type === SIGNAL) {
7889
// if this is a name owner changed message, cache the new name owner
7990
let {sender, path, iface, member} = msg;
8091
if (sender === 'org.freedesktop.DBus' &&
@@ -171,8 +182,8 @@ class MessageBus extends EventEmitter {
171182
* @see {@link https://dbus.freedesktop.org/doc/dbus-specification.html#bus-messages-request-name}
172183
*
173184
* @param name {string} - the well-known name on the bus to request.
174-
* @param flags {NameFlags} - DBus name flags which affect the behavior of taking the name.
175-
* @returns {Promise} - a Promise that resolves with the `RequestNameReply`.
185+
* @param flags {NameFlag} - DBus name flags which affect the behavior of taking the name.
186+
* @returns {Promise} - a Promise that resolves with the {@link RequestNameReply}.
176187
*/
177188
requestName(name, flags) {
178189
flags = flags || 0;
@@ -200,7 +211,7 @@ class MessageBus extends EventEmitter {
200211
* Release this name. Requests that the name should no longer be owned by the
201212
* {@link MessageBus}.
202213
*
203-
* @returns {Promise} A Promise that will resolve with the `ReleaseNameReply`.
214+
* @returns {Promise} A Promise that will resolve with the {@link ReleaseNameReply}.
204215
*/
205216
releaseName(name) {
206217
return new Promise((resolve, reject) => {
@@ -241,8 +252,7 @@ class MessageBus extends EventEmitter {
241252
}
242253

243254
/**
244-
* A function to call when a message of type {@link
245-
* MESSAGE_TYPE_METHOD_RETURN} is received. User handlers are run before
255+
* A function to call when a message of type {@link MessageType.METHOD_RETURN} is received. User handlers are run before
246256
* default handlers.
247257
*
248258
* @callback methodHandler
@@ -256,7 +266,7 @@ class MessageBus extends EventEmitter {
256266
* MessageBus#removeMethodHandler}
257267
*
258268
* @param {methodHandler} - A function to handle a {@link Message} of type
259-
* {@link MESSAGE_TYPE_METHOD_RETURN}. Takes the `Message` as the first
269+
* {@link MessageType.METHOD_RETURN}. Takes the `Message` as the first
260270
* argument. Return `true` if the method is handled and no further handlers
261271
* will run.
262272
*/
@@ -268,7 +278,7 @@ class MessageBus extends EventEmitter {
268278
* Remove a user method return handler that was previously added with {@link
269279
* MessageBus#addMethodHandler}.
270280
*
271-
* @param {methodHandler} - A function that was previously added as a method handler with {@link
281+
* @param {methodHandler} - A function that was previously added as a method handler.
272282
*/
273283
removeMethodHandler(fn) {
274284
for (let i = 0; i < this._methodHandlers.length; ++i) {
@@ -279,7 +289,7 @@ class MessageBus extends EventEmitter {
279289
}
280290

281291
/**
282-
* Send a {@link Message} of type {@link MESSAGE_TYPE_METHOD_CALL} to the bus
292+
* Send a {@link Message} of type {@link MessageType.METHOD_CALL} to the bus
283293
* and wait for the reply.
284294
*
285295
* @example
@@ -291,28 +301,28 @@ class MessageBus extends EventEmitter {
291301
* });
292302
* let reply = await bus.call(message);
293303
*
294-
* @param {Message} msg - The message to send. Must be a METHOD_CALL.
295-
* @returns {Promise} reply - A `Promise` that resolves to the `Message`
296-
* which is a reply to the call.
304+
* @param {Message} msg - The message to send.
305+
* @returns {Promise} reply - A `Promise` that resolves to the {@link
306+
* Message} which is a reply to the call.
297307
*/
298308
call(msg) {
299309
return new Promise((resolve, reject) => {
300310
if (!(msg instanceof Message)) {
301311
throw new Error('The call() method takes a Message class as the first argument.');
302312
}
303-
if (msg.type !== constants.messageType.METHOD_CALL) {
313+
if (msg.type !== METHOD_CALL) {
304314
throw new Error('Only messages of type METHOD_CALL can expect a call reply.');
305315
}
306316
if (msg.serial === null || msg._sent) {
307317
msg.serial = this.newSerial();
308318
}
309319
msg._sent = true;
310-
if (msg.flags & constants.flags.noReplyExpected) {
320+
if (msg.flags & NO_REPLY_EXPECTED) {
311321
resolve(null);
312322
} else {
313323
this._methodReturnHandlers[msg.serial] = (reply) => {
314324
this._nameOwners[msg.destination] = reply.sender;
315-
if (reply.type === constants.messageType.ERROR) {
325+
if (reply.type === ERROR) {
316326
return reject(new DBusError(reply.errorName, reply.body[0], reply));
317327
} else {
318328
return resolve(reply);

0 commit comments

Comments
 (0)