-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
569 lines (473 loc) · 15 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
var
// Module Dependencies
fs = require('fs')
, path = require('path')
, config = require('./config')
, errors = require('./errors')
// Third Party Dependencies
, lodash = require('lodash')
, bcrypt = require('bcrypt')
, ok = require('okay')
, request = require('request')
, async = require('async')
, MailComposer = require('mailcomposer').MailComposer
, Mailgun = require('mailgun').Mailgun
, Balanced = require('balanced-official')
, uuid = require('node-uuid')
, ironMQ = require('iron_mq')
, rollbar = require("rollbar")
, Handlebars = require('hbs')
, moment = require('moment-timezone')
, mandrill = require('mandrill-api/mandrill')
, twilio = require('twilio')
, Bitly = require('bitly')
// Make underscores/async functionality available on utils
, utils = lodash.extend({}, lodash, {async: async}, require('./public/js/lib/utils'))
;
var local = {};
if (fs.existsSync('./local-config.json')){
local = require('./local-config.json');
}
utils.Plan = require('plan.js');
utils.useragent = require('useragent');
utils.Promise = require('bluebird');
utils.http = utils.Promise.promisify( request );
utils.request = request;
utils.twilio = twilio(config.twilio.account, config.twilio.token);
utils.bitly = new Bitly(config.bitly.username, config.bitly.apiKey);
utils.words = require('pluralize');
utils.CronJob = require('cron').CronJob;
utils.overload = require('leFunc');
utils.deepExtend = require('deep-extend');
utils.template = Handlebars.compile.bind( Handlebars );
utils.s3 = require('knox');
utils.uuid = uuid;
// ironMQ stuff
utils.iron = new ironMQ.Client({token: config.ironMQ.token, project_id: config.ironMQ.projectId});
utils.queues = {
debit: utils.iron.queue('debit')
};
// rollbar stuff
rollbar.init(config.rollbar.accessToken);
utils.rollbar = rollbar;
//balanced stuff
utils.balanced = new Balanced({
marketplace_uri: config.balanced.marketplaceUri
, secret: config.balanced.secret
});
utils.stripe = require('stripe')(config.stripe.secret);
utils.normalize = function( x, MIN, MAX ){
return ( x - MIN ) / ( MAX - MIN );
};
utils.getTestEmail = function( id ){
var email = config.testEmail.split('@');
email[0] += '+' + ( id ? id : parseInt(Math.random()*9999999).toString(36) );
if ( local.emailSalt ) email[0] += local.emailSalt;
return email.join('@');
};
utils.editAllKeys = function( obj, fn ){
var val, newKey;
for ( var key in obj ){
val = obj[ key ];
if ( typeof val === 'object' && val !== null && val !== undefined ){
utils.editAllKeys( val, fn );
}
newKey = fn( key, val, obj );
if ( key !== newKey ){
obj[ newKey ] = val;
delete obj[ key ];
}
}
return obj;
};
utils.test = {};
function getRequestMethod( method, opts ){
return function( url, data, callback ){
var options = utils.extend({
url: [ config.baseUrl, url ].join( url[0] === '/' ? '' : '/' )
, method: method
, form: data
, jar: true
}, opts || {} );
if ( [ 'get', 'del' ].indexOf( method ) > -1 ){
delete options.form;
}
return request( options, [ 'get', 'del' ].indexOf( method ) > -1 ? data : callback );
};
}
// Regular HTTP requests
[
'get', 'post'
].forEach( function( method ){
utils.test[ method ] = getRequestMethod( method );
});
// For consuming JSON
utils.test.json = {};
[
'get', 'post', 'put', 'patch', 'del'
].forEach( function( method ){
utils.test.json[ method ] = getRequestMethod( method, { json: true } );
});
utils.test.loginAsUserId = function( id, callback ){
return utils.test.login( utils.getTestEmail(), 'password', callback );
};
utils.test.login = function( user, password, callback ){
var data = { email: user, password: password };
return utils.test.post( '/login', data, callback );
};
utils.test.logout = function( callback ){
return utils.test.get( '/auth/logout', callback );
};
utils.async.log = function(){
var args = arguments;
return function(){
console.log.apply( console, args );
return arguments[ arguments.length - 1 ]();
};
};
utils.async.noop = function(){
var callback = arguments[ arguments.length - 1 ];
if ( typeof callback === 'function' ) return callback();
};
/**
* Async.parallel that does not bail on error.
* Instead it will return an array or object
* of errors, continuing until each function is
* complete
* @param {Array|Object} fns The list of functions to run
* @param {Function} callback ( errors, results )
*/
utils.async.parallelNoBail = function( fns, limit, callback ){
if ( typeof limit === 'function' ){
callback = limit;
limit = null;
}
if ( limit ){
return utils.async.noBail( 'parallelLimit', fns, limit, callback )
}
return utils.async.noBail( 'parallel', fns, callback );
};
/**
* Async.series that does not bail on error.
* Instead it will return an array or object
* of errors, continuing until each function is
* complete
* @param {Array|Object} fns The list of functions to run
* @param {Function} callback ( errors, results )
*/
utils.async.seriesNoBail = function( fns, callback ){
return utils.async.noBail( 'series', fns, callback );
};
utils.async.noBail = function( op, fns, limit, callback ){
if ( typeof fns !== 'object' ) throw new Error('`parallelNoBail` - Invalid first argument');
if ( typeof limit === 'function' ){
callback = limit;
limit = null;
}
var hadError = false;
var noBailFns = Array.isArray( fns ) ? [] : {};
var errors = Array.isArray( fns ) ? [] : {};
Object.keys( fns ).forEach( function( key ){
noBailFns[ key ] = function( done ){
fns[ key ]( function( error ){
if ( error ){
hadError = true;
if ( Array.isArray( errors ) ) errors.push( error );
else errors[ key ] = error;
}
done.apply( null, [null].concat( Array.prototype.slice.call( arguments, 1 ) ) );
});
}
});
var args = [ noBailFns ];
if ( limit ) args.push( limit );
args.push(function( error, results ){
callback( hadError ? errors : null, results );
});
async[ op ].apply( async, args );
};
utils.stage = function(fns){
var current = function(){
var args = Array.prototype.slice.call(arguments, 0);
var callback = args.pop();
// Redefine current after first call so that it doesn't default to 'start'
current = function(name){
if (!fns.hasOwnProperty(name)) throw new Error('Cannot find stage item: ', name);
fns[name].apply(null, Array.prototype.slice.call(arguments, 1).concat(current, callback));
};
fns.start.apply(null, args.concat(current, callback));
};
return current;
};
utils.get = function(url, options, callback){
if (typeof options === "function"){
callback = options;
options = {};
}
options = utils.extend({
url: url
, method: "GET"
, json: true
}, options);
request(options, callback);
};
utils.post = function(url, data, options, callback){
if (typeof options === "function"){
callback = options;
options = {};
}
options = utils.extend({
url: url
, method: "POST"
, json: true
, form: data
}, options);
request(options, callback);
};
utils.put = function(url, data, options, callback){
if (typeof options === "function"){
callback = options;
options = {};
}
options = utils.extend({
url: url
, method: "PUT"
, json: true
, form: data
}, options);
request(options, callback);
};
utils.patch = function(url, data, options, callback){
if (typeof options === "function"){
callback = options;
options = {};
}
options = utils.extend({
url: url
, method: "PATCH"
, json: true
, form: data
}, options);
request(options, callback);
};
utils.del = function(url, callback){
var options = {
url: url
, method: "DELETE"
, json: true
};
request(options, callback);
};
var mailgun = new Mailgun(config.mailgun.apiKey);
mandrill = new mandrill.Mandrill(config.mandrill.apiKey);
/**
* Send mail. Why are there two versions? Because sendMail1
* is already being used throughout the codebase and I need
* to add attachments to my mail now. Adding it to sendMail1
* seems like too daunting of a task because... I mean...
* look at it! I would have to add yet ANOTHER argument to
* that thing
*
* Options: {
* to: '[email protected]'
* , from: '[email protected]'
* , subject: 'ohai!'
* , html: '<html></html>'
* , body: 'Plain text'
* , reply_to: '[email protected]'
* , attachment: {
* // Whatever mailcomposer accepts
* // See https://github.com/andris9/mailcomposer#add-attachments
* filePath: '~/love-letter.pdf'
* }
* , attachments: [ * array of objects described above * ]
* , headers: {
* "x-mailer": "Noemailer 1.0"
* }
* }
*
* @param {Object} options The full email options sent to composer
* @param {Function} callback callback( error )
*/
utils.sendMail2 = function( options, callback ){
callback = callback || options.callback || utils.noop;
if ( !config.emailEnabled ) return callback();
var composer = new MailComposer();
// Remove whitespace from email to remove possible rendering bugs
if ( options.html ) options.html = options.html.replace(/>\s+</g, '><').trim();
composer.setMessageOption( options );
if ( options.attachments ) options.attachments.forEach( composer.addAttachment );
if ( options.attachment ) composer.addAttachment( options.attachment );
if ( typeof options.headers === 'object' ){
for ( var key in options.headers ){
composer.addHeader( key, options.headers[ key ] );
}
}
composer.buildMessage( function( error, message ){
if ( error ) return callback( error );
utils.sendRawEmail( options.from, options.to, message, callback );
});
};
utils.sendRawEmail = function( from, to, text, callback ){
if ( config.emailProvider === 'mailgun' ){
mailgun.sendRaw( from, to, text, callback );
} else if ( config.emailProvider === 'mandrill' ){
mandrill.messages.sendRaw({
from_email: from
, to: Array.isArray( to ) ? to : [ to ]
, raw_message: text
}, function( result ){ callback( null, result ); }, callback );
}
};
utils.sendMail = function(to, from, subject, html, text, callback) {
if (lodash.isFunction(text) && callback === undefined) {
callback = text;
text = undefined;
}
var options;
if (lodash.isObject(to) && !lodash.isArray(to)) {
callback = from;
from = undefined;
options = to;
from = options.from;
to = options.to;
subject = options.subject;
html = options.html;
text = options.body
} else {
options = {
to: to
, from: from
, reply_to: from
, subject: subject
, html: html
, body: text
}
}
// Remove whitespace from email to remove possible rendering bugs
if (options.html) options.html = options.html.replace(/>\s+</g, '><').trim();
if (!callback) callback = function(){};
if (!config.emailEnabled) return callback(); // :TODO: log or output an event so that we can test against the event
var send = function(mimeBody) {
mailgun.sendRaw(mimeBody, callback)
};
var composer = new MailComposer();
composer.setMessageOption(options);
composer.buildMessage(function(err, msg) {
if (err && lodash.isFunction(callback)) return callback(err);
mailgun.sendRaw(from, to, msg, callback);
});
}
utils.isNotBlank = function (value) {
if(value != null && value !="") return true;
return false;
}
utils.parseBool = function(value){
if (value == 'true' || value == '1') return true;
if (value == 'false' || value == '0') return false;
return true;
};
/**
* Encrypts consumer passwords
* @param {String} password The password to encrypt
* @param {String} salt The salt to use with the password
* @param {Function} callback Takes (err, hash, salt)
* @return {null}
*/
utils.encryptPassword = function(password, salt, callback){
if (!callback && typeof salt == 'function') {
callback = salt;
salt = null;
}
if (!password) return (callback || function(){})(null);
var genSalt = (!salt) ? bcrypt.genSalt : function(v, cb) { cb(null, salt); };
genSalt.call(bcrypt, 10, ok(function(salt) {
bcrypt.hash(password + config.passwordSalt, salt, ok(function(hash) {
callback(null, hash, salt);
}));
}));
};
/**
* Copmares a non-encrypted password with an encrypted one
* @param {String} password The non-encrypted string
* @param {String} encrypted The encrypted string
* @param {Function} callback Contains the results (error, success)
* @return {Null}
*/
utils.comparePasswords = function(password, encrypted, callback){
//do not compare null values
if(!encrypted) return callback(null, false);
bcrypt.compare(password + config.passwordSalt, encrypted, ok(function(success) {
if (!success){
bcrypt.compare(password, encrypted, ok(function(success) {
callback(null, success);
}));
}
else {
callback(null, success);
}
}));
};
utils.sendJSON = function(res, json){
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
// res.header('Access-Control-Allow-Origin', 'http://local.goodybag.com');
// res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
// res.header('Access-Control-Allow-Credentials', true);
// res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept, Authorization');
// res.cookie('remember', 1, { domain : "test-project" });
res.json(json);
};
utils.sendError = function(res, error, details){
res.status(error.httpCode || (details || {}).httpCode || 500);
if (error instanceof Error) {
error = utils.extend({}, errors.runtime.ERROR, { message: error.message, stack: error.stack });
}
if (typeof error === 'string') {
error = errors[error];
}
if ( !error.details ) error.details = utils.pick(details, ['message', 'stack']);
utils.sendJSON(res, { error: error });
};
utils.noop = function(){};
/**
* Contains the default structure for errors
* @param {String} message Nice message to be displayed
* @param {String} type The type of error
* @return {Object} The error object
*/
utils.error = function(message, type){
return {
message: message
, type: type
};
};
utils.queryParams = function(data, urlEncode){
if (typeof data !== "object") return "";
var params = "?";
for (var key in data){
if ([null, undefined, ""].indexOf(data[key]) > -1) continue;
if (utils.isArray(data[key])){
for (var i = 0, l = data[key].length; i < l; ++i){
params += key + (urlEncode ? "%5B%5D=" : "[]=" ) + data[key][i] + "&";
}
} else {
params += key + "=" + data[key] + "&";
}
}
return params.substring(0, params.length - 1);
};
utils.getProperty = function( obj, prop ) {
if (prop.indexOf('.') < 0) return obj[prop];
var parts = prop.split('.')
, last = parts.pop()
, len = parts.length
, idx = 1
, current = parts[0];
while( (obj = obj[current]) && idx < len ) {
current = parts[idx++];
}
if ( obj )
return obj[last];
return obj;
};
module.exports = utils;