From 350fd45086a0406359da028652a2bbfb0ba67e8a Mon Sep 17 00:00:00 2001 From: Mark Stosberg Date: Thu, 6 Aug 2015 11:36:19 -0400 Subject: [PATCH] Refactor: encapsulate code that will be shared with the sync version. --- lib/secure-random-string.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/secure-random-string.js b/lib/secure-random-string.js index 20810c7..6813a6a 100644 --- a/lib/secure-random-string.js +++ b/lib/secure-random-string.js @@ -12,15 +12,19 @@ function srs(options, cb) { crypto.randomBytes(length, function(ex, buf) { if (ex) throw ex; + cb(_finish(buf)); + }); + + function _finish (buf) { var string = buf.toString('base64'); if (options.urlsafe) { string = string.replace(/\//g,'_').replace(/\+/g,'-'); } - - cb(string.substr(0, length)); - }); + return string.substr(0, length); + } }; + module.exports = srs;