Skip to content

Commit

Permalink
lib: refactor return style
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jul 17, 2017
1 parent 45bb40f commit eb7a704
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 44 deletions.
9 changes: 6 additions & 3 deletions lib/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ class Application {
callMethod(connection, interfaceName, methodName, args, callback) {
const appInterface = this.api[interfaceName];
if (!appInterface) {
return callback(errors.ERR_INTERFACE_NOT_FOUND);
callback(errors.ERR_INTERFACE_NOT_FOUND);
return;
}

const method = appInterface[methodName];
if (!method) {
return callback(errors.ERR_METHOD_NOT_FOUND);
callback(errors.ERR_METHOD_NOT_FOUND);
return;
}

if (method.length !== args.length + 2) {
return callback(errors.ERR_INVALID_SIGNATURE);
callback(errors.ERR_INVALID_SIGNATURE);
return;
}

method(connection, ...args, callback);
Expand Down
8 changes: 6 additions & 2 deletions lib/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ module.exports = class Cli extends EventEmitter {
processLine(line, callback) {
const [type, leftover] = utils.split(line.trim(), ' ', 1);
if (!type) {
return callback(null);
callback(null);
return;
}

const cmd = utils.tryCompleter(type, this.commandProcessor);
Expand All @@ -65,7 +66,10 @@ module.exports = class Cli extends EventEmitter {
this.log(`Unknown command '${cmd}'`);
} else {
processor.call(this.lineProcessor, leftover, (err, result) => {
if (err) return this._logErr(err);
if (err) {
this._logErr(err);
return;
}
this.log(result);
});
}
Expand Down
28 changes: 20 additions & 8 deletions lib/cli/command-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,25 @@ module.exports = class CommandProcessor extends EventEmitter {
}

call(interfaceName, methodName, args, callback) {
if (!this.cli.connectInitiated) return callback(new Error('Not connected'));
if (!this.cli.connectInitiated) {
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
return callback(new Error('Connection in progress'));
callback(new Error('Connection in progress'));
return;
}
this.cli.connection.callMethod(interfaceName, methodName, args, callback);
}

event(interfaceName, eventName, args, callback) {
if (!this.cli.connectInitiated) return callback(new Error('Not connected'));
if (!this.cli.connectInitiated) {
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
return callback(new Error('Connection in progress'));
callback(new Error('Connection in progress'));
return;
}
this.cli.connection.emitRemoteEvent(interfaceName, eventName, args);
callback();
Expand Down Expand Up @@ -127,14 +135,16 @@ module.exports = class CommandProcessor extends EventEmitter {
args = [host];
break;
default:
return callback(new Error(`Unknown protocol '${protocol}'`));
callback(new Error(`Unknown protocol '${protocol}'`));
return;
}
this.cli.connectInitiated = true;
transport.connectAndInspect(appName, null, interfaces, ...args,
(err, connection, api) => {
if (err) {
this.cli.connectInitiated = false;
return callback(err);
callback(err);
return;
}
this.cli.connection = connection;
this.cli.api = filterApiCompletions(api, ['_', 'domain']);
Expand All @@ -153,10 +163,12 @@ module.exports = class CommandProcessor extends EventEmitter {

disconnect(callback) {
if (!this.cli.connectInitiated) {
return callback(new Error('Not connected'));
callback(new Error('Not connected'));
return;
}
if (!this.cli.connection) {
return callback(new Error('Wait till connection is established'));
callback(new Error('Wait till connection is established'));
return;
}
this.cli.connection.close();
this.cli.connection = null;
Expand Down
53 changes: 38 additions & 15 deletions lib/cli/line-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@ module.exports = class LineProcessor {

call(tokens, callback) {
if (tokens === undefined) {
return callback(reportMissingArgument('Interface name'));
callback(reportMissingArgument('Interface name'));
return;
}
const args = utils.split(tokens, ' ', 2);
if (args.length === 1) {
return callback(reportMissingArgument('Method name'));
callback(reportMissingArgument('Method name'));
return;
}
let methodArgs;
try {
methodArgs = jstp.parse('[' + args[2] + ']');
} catch (err) {
return callback(err);
callback(err);
return;
}
this.commandProcessor.call(args[0], args[1], methodArgs,
(err, ...result) => {
if (err) return callback(err);
if (err) {
callback(err);
return;
}
const iface = args[0];
const method = args[1];
const res = jstp.stringify(result);
Expand All @@ -40,27 +46,34 @@ module.exports = class LineProcessor {

event(tokens, callback) {
if (tokens === undefined) {
return callback(reportMissingArgument('Interface name'));
callback(reportMissingArgument('Interface name'));
return;
}
const args = utils.split(tokens, ' ', 2);
if (args.length === 1) {
return callback(reportMissingArgument('Event name'));
callback(reportMissingArgument('Event name'));
return;
}
let eventArgs;
try {
eventArgs = jstp.parse('[' + args[2] + ']');
} catch (err) {
return callback(err);
callback(err);
return;
}
this.commandProcessor.event(args[0], args[1], eventArgs, (err) => {
if (err) return callback(err);
if (err) {
callback(err);
return;
}
callback(null, `Event ${args[0]}.${args[1]} successfully emitted`);
});
}

connect(tokens, callback) {
if (tokens === undefined) {
return callback(reportMissingArgument('Host'));
callback(reportMissingArgument('Host'));
return;
}
const args = utils.split(tokens, ' ', 2);
let [scheme, authority] = utils.split(args[0], '://', 1, true);
Expand All @@ -71,34 +84,44 @@ module.exports = class LineProcessor {
const [host, portString] = utils.split(authority, ':', 2, true);
let port;
if (!host) {
return callback(reportMissingArgument('Host'));
callback(reportMissingArgument('Host'));
return;
}
if (scheme !== 'ipc') {
if (!portString) {
return callback(reportMissingArgument('Port'));
callback(reportMissingArgument('Port'));
return;
}
port = Number(portString);
if (isNaN(port) || port < 0 || port >= 65536) {
return callback(new Error(`Port has incorrect value: ${portString}`));
callback(new Error(`Port has incorrect value: ${portString}`));
return;
}
}
const appName = args[1];
if (appName === undefined) {
return callback(reportMissingArgument('Application name'));
callback(reportMissingArgument('Application name'));
return;
}
const interfaces = args[2] ? utils.split(args[2], ' ') : [];
this.commandProcessor.connect(
scheme, host, port, appName, interfaces,
(err) => {
if (err) return callback(err);
if (err) {
callback(err);
return;
}
callback(null, 'Connection established');
}
);
}

disconnect(_, callback) {
this.commandProcessor.disconnect((err) => {
if (err) return callback(err);
if (err) {
callback(err);
return;
}
callback(null, 'Successful disconnect');
});
}
Expand Down
24 changes: 16 additions & 8 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ class Connection extends EventEmitter {

const kind = keys[0];
if (!this.handshakeDone && kind !== 'handshake') {
return this._rejectPacket(packet, true);
this._rejectPacket(packet, true);
return;
}

const handler = PACKET_HANDLERS[kind];
Expand Down Expand Up @@ -341,7 +342,8 @@ class Connection extends EventEmitter {
//
_processHandshakePacket(packet, keys) {
if (this.handshakeDone) {
return this._rejectPacket(packet, true);
this._rejectPacket(packet, true);
return;
}

if (packet.handshake[1]) { // if there is an application name
Expand All @@ -357,14 +359,16 @@ class Connection extends EventEmitter {
//
_processHandshakeRequest(packet, keys) {
if (!this.server) {
return this._handshakeError(errors.ERR_NOT_A_SERVER);
this._handshakeError(errors.ERR_NOT_A_SERVER);
return;
}

const applicationName = packet.handshake[1];
const application = this.server.applications[applicationName];

if (!application) {
return this._handshakeError(errors.ERR_APP_NOT_FOUND);
this._handshakeError(errors.ERR_APP_NOT_FOUND);
return;
}

this.application = application;
Expand All @@ -386,7 +390,8 @@ class Connection extends EventEmitter {
//
_onSessionCreated(error, username, sessionId) {
if (error) {
return this._handshakeError(errors.ERR_AUTH_FAILED);
this._handshakeError(errors.ERR_AUTH_FAILED);
return;
}

this.username = username;
Expand Down Expand Up @@ -451,7 +456,8 @@ class Connection extends EventEmitter {
const callback = this._remoteCallbackWrapper.bind(this, packetId);

if (!args) {
return callback(errors.ERR_INVALID_SIGNATURE);
callback(errors.ERR_INVALID_SIGNATURE);
return;
}

try {
Expand All @@ -475,9 +481,11 @@ class Connection extends EventEmitter {
delete this._callbacks[packetId];

if (packet.ok) {
return callback(null, ...packet.ok);
callback(null, ...packet.ok);
return;
} else if (packet.error) {
return callback(errors.RemoteError.fromJstpArray(packet.error));
callback(errors.RemoteError.fromJstpArray(packet.error));
return;
}
}
this._rejectPacket(packet);
Expand Down
3 changes: 2 additions & 1 deletion lib/simple-auth-policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = class SimpleAuthPolicy extends EventEmitter {
//
startSession(connection, application, strategy, credentials, callback) {
if (strategy !== 'anonymous') {
return callback(errors.ERR_AUTH_FAILED);
callback(errors.ERR_AUTH_FAILED);
return;
}

const sessionId = uuid4();
Expand Down
3 changes: 2 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class Transport extends EventEmitter {
try {
this._buffer = jsrs.parseNetworkPackets(this._buffer, packets);
} catch (error) {
return this.socket.destroy(error);
this.socket.destroy(error);
return;
}

const packetsCount = packets.length;
Expand Down
18 changes: 14 additions & 4 deletions lib/transport-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ const newConnectFn = (
) => (appName, client, ...options) => {
const callback = common.extractCallback(options);
connFactory(...options, (error, rawConnection) => {
if (error) return callback(error);
if (error) {
callback(error);
return;
}

// eslint-disable-next-line new-cap
const transport = new transportClass(rawConnection);
Expand All @@ -49,7 +52,10 @@ const newConnectFn = (
}
const connection = new Connection(transport, null, client);
client.connectPolicy(appName, connection, (error, connection) => {
if (error) return callback(error, connection);
if (error) {
callback(error, connection);
return;
}
if (client.heartbeatInterval) {
connection.startHeartbeat(client.heartbeatInterval);
}
Expand All @@ -67,7 +73,10 @@ const newConnectAndInspectFn = (connFactory, transportClass) => {
return (appName, client, interfaces, ...options) => {
const callback = common.extractCallback(options);
connect(appName, client, ...options, (error, connection) => {
if (error) return callback(error);
if (error) {
callback(error);
return;
}
const proxies = Object.create(null);
let errored = false;
const len = interfaces.length;
Expand All @@ -78,7 +87,8 @@ const newConnectAndInspectFn = (connFactory, transportClass) => {
count++;
if (error) {
errored = true;
return callback(error, connection);
callback(error, connection);
return;
}
proxies[name] = proxy;
if (count === len) callback(null, connection, proxies);
Expand Down
6 changes: 4 additions & 2 deletions lib/ws-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class Transport extends EventEmitter {
try {
packet = jsrs.parse(data);
} catch (error) {
return this.emit('error', error);
this.emit('error', error);
return;
}

this.emit('packet', packet);
Expand Down Expand Up @@ -102,7 +103,8 @@ const initServer = function(options, httpServer) {

httpServer.wsServer.on('request', (request) => {
if (!httpServer.isOriginAllowed(request.origin)) {
return request.reject();
request.reject();
return;
}

const connection = request.accept(
Expand Down

0 comments on commit eb7a704

Please sign in to comment.