Skip to content

Commit

Permalink
[refactor] Use utile.randomString
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki authored and indexzero committed Nov 23, 2011
1 parent dbf46c3 commit b30316e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
3 changes: 2 additions & 1 deletion bin/forever
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path'),
fs = require('fs'),
winston = require('winston'),
util = require('util'),
utile = require('utile'),
forever = require('./../lib/forever');

var action, accepts = [
Expand Down Expand Up @@ -181,7 +182,7 @@ if (!options.sourceDir) {
options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/';
}

var uid = forever.randomString(24);
var uid = utile.randomString(4);
options.uid = uid;
options.pidFile = options.pidFile || uid + '.pid';
options.logFile = argv.l || uid + '.log';
Expand Down
28 changes: 2 additions & 26 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var fs = require('fs'),
timespan = require('timespan'),
spawn = require('child_process').spawn,
psTree = require('ps-tree'),
utile = require('utile'),
winston = require('winston');

var forever = exports;
Expand Down Expand Up @@ -315,7 +316,7 @@ forever.start = function (script, options) {
//
forever.startDaemon = function (script, options) {
options = options || {};
options.uid = options.uid || forever.randomString(24);
options.uid = options.uid || utile.randomString(4);
options.logFile = forever.logFilePath(options.logFile || options.uid + '.log');
options.pidFile = forever.pidFilePath(options.pidFile || options.uid + '.pid');

Expand Down Expand Up @@ -792,31 +793,6 @@ forever.cleanLogsSync = function (processes) {
});
};

//
// ### function randomString (bits)
// #### @bits {Number} Bit-length of the base64 string to return.
// Returns a pseude-random ASCII string which contains at least
// the specified number of bits of entropy the return value is a string of
// length ⌈bits/6⌉ of characters from the base64 alphabet.
//
forever.randomString = function (bits) {
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-',
rand, i, ret = '';

//
// in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
//
while (bits > 0) {
rand = Math.floor(Math.random() * 0x100000000); // 32-bit integer
// base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
for (i = 26; i > 0 && bits > 0; i -= 6, bits -= 6) {
ret += chars[0x3F & rand >>> i];
}
}

return ret;
};

//
// ### function logFilePath (logFile)
// #### @logFile {string} Log file path
Expand Down
4 changes: 3 additions & 1 deletion lib/forever/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var util = require('util'),
winston = require('winston'),
watch = require('watch'),
minimatch = require('minimatch'),
psTree = require('ps-tree'),
utile = require('utile'),
forever = require('../forever');

//
Expand All @@ -33,7 +35,7 @@ var Monitor = exports.Monitor = function (script, options) {
this.silent = options.silent || false;
this.forever = options.forever || false;
this.killTree = options.killTree !== false;
this.uid = options.uid || forever.randomString(24);
this.uid = options.uid || utile.randomString(4);
this.pidFile = options.pidFile || path.join(forever.config.get('pidPath'), this.uid + '.pid');
this.max = options.max;
this.killTTL = options.killTTL;
Expand Down
8 changes: 5 additions & 3 deletions lib/forever/service/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*
*/

var optimist = require('optimist'),
var utile = require('utile'),
optimist = require('optimist'),
forever = require('../forever'),
Service = require('./service'),
argv;
Expand Down Expand Up @@ -95,7 +96,7 @@ var router = module.exports = function router(app) {
options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/';
}

uid = options.uid || forever.randomString(24);
uid = options.uid || utile.randomString(4);
options.uid = uid;
options.pidFile = options.pidFile || uid + '.pid';
options.logFile = argv.l || uid + '.log';
Expand Down Expand Up @@ -206,4 +207,5 @@ var router = module.exports = function router(app) {
app.cli('/resume', function (cmd, tty) {
cmd.service.resume();
});
};
};

0 comments on commit b30316e

Please sign in to comment.