From b30316e2b03e1e38cc4e09a2c511fb61d1b35c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Wed, 16 Nov 2011 02:24:31 +0100 Subject: [PATCH] [refactor] Use `utile.randomString` --- bin/forever | 3 ++- lib/forever.js | 28 ++-------------------------- lib/forever/monitor.js | 4 +++- lib/forever/service/cli.js | 8 +++++--- 4 files changed, 12 insertions(+), 31 deletions(-) diff --git a/bin/forever b/bin/forever index a7aeeabd..6f285dd4 100755 --- a/bin/forever +++ b/bin/forever @@ -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 = [ @@ -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'; diff --git a/lib/forever.js b/lib/forever.js index 635e2703..863c703d 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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; @@ -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'); @@ -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 diff --git a/lib/forever/monitor.js b/lib/forever/monitor.js index 18240827..70043e22 100644 --- a/lib/forever/monitor.js +++ b/lib/forever/monitor.js @@ -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'); // @@ -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; diff --git a/lib/forever/service/cli.js b/lib/forever/service/cli.js index 681648c4..2c89af19 100644 --- a/lib/forever/service/cli.js +++ b/lib/forever/service/cli.js @@ -6,7 +6,8 @@ * */ -var optimist = require('optimist'), +var utile = require('utile'), + optimist = require('optimist'), forever = require('../forever'), Service = require('./service'), argv; @@ -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'; @@ -206,4 +207,5 @@ var router = module.exports = function router(app) { app.cli('/resume', function (cmd, tty) { cmd.service.resume(); }); -}; \ No newline at end of file +}; +