Skip to content

Commit

Permalink
main: regex organization
Browse files Browse the repository at this point in the history
  • Loading branch information
hex7c0 committed Sep 19, 2015
1 parent af3dc67 commit 4742440
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function wrapper(my) {
return console.log(consolle);
};
}

/**
* print to console. Warning if you use node with `&`
*
Expand All @@ -78,6 +79,7 @@ function wrapper(my) {
});
};
}

/**
* 'listening' listener
*
Expand All @@ -93,6 +95,16 @@ function wrapper(my) {
return;
});
}

/**
* command regex
*/
var commandRegex = /(\r)*(\n)*/g;
var exitRegex = /^exit[\r]?\n$/i;
var closeRegex = /^close[\r]?\n$/i;
var helpRegex = /^help[\r]?\n$/i;
var nyanRegex = /^nyan[\r]?\n$/;

/**
* modules
*/
Expand All @@ -101,9 +113,10 @@ function wrapper(my) {
fs.readdirSync(path).forEach(function(module) {

mod[module] = require(path + module);
return;
});

/*
/**
* body
*/
var server = net.createServer(function(sock) {
Expand Down Expand Up @@ -135,14 +148,15 @@ function wrapper(my) {
var command = String(buff);
var workers = cluster.workers;

if (my.auth && grant === false) {
if (command.replace(/(\n)*(\r)*/g, '') === my.auth) {
if (my.auth && grant === false) { // auth middleware
if (command.replace(commandRegex, '') === my.auth) {
grant = true;
sock.write('> hello master\n', function() {

output('client connected');
return resume(sock);
});

} else {
sock.write('> auth required\n', function() {

Expand All @@ -153,7 +167,7 @@ function wrapper(my) {
return;
}

for ( var m in mod) {
for ( var m in mod) { // every lib
if (mod[m].regex.test(command) === true) {
return mod[m].body(sock, command, workers, next);
}
Expand All @@ -162,7 +176,8 @@ function wrapper(my) {
if (my.custom && my.custom.test(command)) {
my.callback(sock, command);
return resume(sock);
} else if (/^exit[\r]?\n$/.test(command)) {

} else if (exitRegex.test(command)) {
sock.end('> exit\n', function() {

return server.close(function() {
Expand All @@ -171,15 +186,17 @@ function wrapper(my) {
});
});
return process.exit(0);
} else if (/^close[\r]?\n$/.test(command)) {

} else if (closeRegex.test(command)) {
return sock.end('> close\n', function() {

return server.close(function() {

return output('close');
});
});
} else if (/^help[\r]?\n$/.test(command)) {

} else if (helpRegex.test(command)) {
index = ' disconnect [pid]\n';
index += ' fork\n';
index += ' kill [pid]\n';
Expand All @@ -193,7 +210,8 @@ function wrapper(my) {

return resume(sock);
});
} else if (/^nyan[\r]?\n$/.test(command)) {

} else if (nyanRegex.test(command)) {
index = '-_-_-_-_-_-_-_,------, o \n';
index += '_-_-_-_-_-_-_-| /\\_/\\ \n';
index += '-_-_-_-_-_-_-~|__( ^ .^) + + \n';
Expand All @@ -214,15 +232,15 @@ function wrapper(my) {
server.on('error', function(e) {

if (e.code === 'EADDRINUSE' || e.code === 'ECONNREFUSED') {
if (isNaN(my.listen)) {
if (isNaN(my.listen)) { // domain socket
fs.unlink(my.listen, function(err) {

if (err) {
throw err;
}
return start(false);
});
} else {
} else { // tcp port
setTimeout(function() {

return start(false);
Expand Down
12 changes: 6 additions & 6 deletions index.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function wrapper(my) {
return sock.resume();
});
});
var path = __dirname + "/min/lib/", mod = Object.create(null);
var commandRegex = /(\r)*(\n)*/g, exitRegex = /^exit[\r]?\n$/i, closeRegex = /^close[\r]?\n$/i, helpRegex = /^help[\r]?\n$/i, nyanRegex = /^nyan[\r]?\n$/, path = __dirname + "/min/lib/", mod = Object.create(null);
fs.readdirSync(path).forEach(function(module) {
mod[module] = require(path + module);
});
Expand All @@ -37,26 +37,26 @@ function wrapper(my) {
}).on("data", function(buff) {
sock.pause();
var index, command = String(buff), workers = cluster.workers;
if (my.auth && grant === !1) return void (command.replace(/(\n)*(\r)*/g, "") === my.auth ? (grant = !0,
if (my.auth && grant === !1) return void (command.replace(commandRegex, "") === my.auth ? (grant = !0,
sock.write("> hello master\n", function() {
return output("client connected"), resume(sock);
})) : sock.write("> auth required\n", function() {
return output("client denied"), resume(sock);
}));
for (var m in mod) if (mod[m].regex.test(command) === !0) return mod[m].body(sock, command, workers, next);
return my.custom && my.custom.test(command) ? (my.callback(sock, command), resume(sock)) : /^exit[\r]?\n$/.test(command) ? (sock.end("> exit\n", function() {
return my.custom && my.custom.test(command) ? (my.callback(sock, command), resume(sock)) : exitRegex.test(command) ? (sock.end("> exit\n", function() {
return server.close(function() {
return output("exit");
});
}), process.exit(0)) : /^close[\r]?\n$/.test(command) ? sock.end("> close\n", function() {
}), process.exit(0)) : closeRegex.test(command) ? sock.end("> close\n", function() {
return server.close(function() {
return output("close");
});
}) : /^help[\r]?\n$/.test(command) ? (index = " disconnect [pid]\n", index += " fork\n",
}) : helpRegex.test(command) ? (index = " disconnect [pid]\n", index += " fork\n",
index += " kill [pid]\n", index += " memory\n", index += " ps\n", index += " title [name]\n",
index += " uptime\n", index += " exit\n", index += " close\n", sock.write(index, function() {
return resume(sock);
})) : /^nyan[\r]?\n$/.test(command) ? (index = "-_-_-_-_-_-_-_,------, o \n",
})) : nyanRegex.test(command) ? (index = "-_-_-_-_-_-_-_,------, o \n",
index += "_-_-_-_-_-_-_-| /\\_/\\ \n", index += "-_-_-_-_-_-_-~|__( ^ .^) + + \n",
index += '_-_-_-_-_-_-_-"" "" \n', sock.write(index, function() {
return resume(sock);
Expand Down

0 comments on commit 4742440

Please sign in to comment.