-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
322 lines (277 loc) · 6.59 KB
/
index.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
var mongodb = require('mongodb');
var util = require('util');
Function.prototype.stringify = function () {
var match = this.toString().match(/[^]*\/\*([^]*)\*\/\s*\}$/);
return match ? match[1] : '';
};
function _wrap_console() {
var new_console = {
log: function () {
new_console._stdout.push(util.format.apply(global, arguments));
global.console.log.apply(global, arguments);
},
_stdout: []
};
for (var i in global.console){
if (!new_console[i]){
new_console[i] = global.console[i];
}
}
return new_console;
}
function _wrap_callback(cb, console) {
return function (error, data) {
var result = {
stdout: console._stdout
};
if (error)
result.error = error;
else
result.result = data;
cb(null, result);
};
}
function ip (ipAddress) {
var range_check = require('range_check');
return {
in_range: function (ranges) {
return range_check.in_range(ipAddress, ranges);
}
};
}
function mongo (url, callback) {
var getDb = require('mongo-getdb');
if (!url || /^mongo\:\/\//.exec(url)){
throw new Error('invalid mongodb url');
}
getDb(url, callback);
}
['Long', 'Double', 'ObjectID', 'Timestamp', 'BSON'].forEach(function (k) {
mongo[k] = mongodb[k];
});
function mysql (options) {
var mysqlDriver = require('mysql');
return mysqlDriver.createConnection(options);
}
var mysql_pooles = {};
function mysql_pool (options) {
var mysqlDriver = require('mysql');
if (typeof options === 'string') {
var ConnectionConfig = require('mysql/lib/ConnectionConfig');
options = ConnectionConfig.parseUrl(options);
}
if (!options.connectionLimit) {
options.connectionLimit = 30;
}
var key = typeof options === 'string' ? options : JSON.stringify(options);
if (mysql_pooles[key]) {
console.log('pool length: ', mysql_pooles[key]._connectionQueue.length);
return mysql_pooles[key];
}
var pool = (mysql_pooles[key] = mysqlDriver.createPool(options));
//once the pool creates a new connection
pool.on('connection', function (connection) {
//when the connection emits an error,
//remove the connection from the pool
connection.on('error', function () {
connection.destroy();
});
//make a ping in the connection every 30seconds
setInterval(function () {
connection.ping(function(){});
}, 30 * 1000);
});
return pool;
}
function postgres (connString, callback) {
var pg = require('pg');
var client = new pg.Client(connString);
client.connect(function(err) {
if (err) return callback(err);
callback(null, client);
});
}
var api = (module.exports = {
mongo: mongo,
mysql: mysql,
mysql_pool: mysql_pool,
postgres: postgres,
Buffer: global.Buffer,
ip: ip,
querystring: require('querystring'),
ObjectID: require('mongodb').ObjectID,
_wrap_callback: _wrap_callback,
_wrap_console: _wrap_console
});
Object.defineProperty(api, 'async', {
configurable: false,
enumerable: true,
get: function () {
return require('async');
}
});
Object.defineProperty(api, 'pg', {
configurable: false,
enumerable: true,
get: function () {
return require('pg');
}
});
Object.defineProperty(api, 'jwt', {
configurable: false,
enumerable: true,
get: function () {
return require('jsonwebtoken');
}
});
Object.defineProperty(api, 'cql', {
configurable: false,
enumerable: true,
get: function () {
return require('node-cassandra-cql');
}
});
Object.defineProperty(api, '_', {
configurable: false,
enumerable: true,
get: function () {
return require('lodash');
}
});
Object.defineProperty(api, 'Pubnub', {
configurable: false,
enumerable: true,
get: function () {
return require('pubnub');
}
});
Object.defineProperty(api, 'sqlserver', {
configurable: false,
enumerable: true,
get: function () {
var sqlserver = {
connect: function (config) {
var Connection = require('tedious').Connection;
return new Connection(config);
},
Request: require('tedious').Request,
Types: require('tedious').TYPES
};
return sqlserver;
}
});
Object.defineProperty(api, 'request', {
configurable: false,
enumerable: true,
get: function () {
return require('request');
}
});
Object.defineProperty(api, 'bcrypt', {
configurable: false,
enumerable: true,
get: function () {
return require('bcrypt');
}
});
Object.defineProperty(api, 'crypto', {
configurable: false,
enumerable: true,
get: function () {
return require('crypto');
}
});
Object.defineProperty(api, 'pbkdf2', {
configurable: false,
enumerable: true,
get: function () {
return require('easy-pbkdf2')();
}
});
Object.defineProperty(api, 'xmldom', {
configurable: false,
enumerable: true,
get: function () {
return require('xmldom');
}
});
Object.defineProperty(api, 'xml2js', {
configurable: false,
enumerable: true,
get: function () {
return require('xml2js');
}
});
Object.defineProperty(api, 'xpath', {
configurable: false,
enumerable: true,
get: function () {
return require('xpath');
}
});
Object.defineProperty(api, 'couchbase', {
configurable: false,
enumerable: true,
get: function () {
return require('couchbase');
}
});
Object.defineProperty(api, 'xtend', {
configurable: false,
enumerable: true,
get: function () {
return require('xtend');
}
});
Object.defineProperty(api, 'q', {
configurable: false,
enumerable: true,
get: function () {
return require('q');
}
});
Object.defineProperty(api, 'azure_storage', {
configurable: false,
enumerable: true,
get: function () {
return require('azure-storage');
}
});
Object.defineProperty(api, 'Auth0', {
configurable: false,
enumerable: true,
get: function () {
return require('auth0');
}
});
// using capital letters because it is a factory
// it must then be invoked var knex = Knex(...)
// ref: https://github.com/tgriesser/knex/blob/master/knex.js
Object.defineProperty(api, 'Knex', {
configurable: false,
enumerable: true,
get: function () {
return require('knex');
}
});
Object.defineProperty(api, 'ValidationError', {
configurable: false,
enumerable: true,
get: function () {
return require('./ValidationError');
}
});
Object.defineProperty(api, 'WrongUsernameOrPasswordError', {
configurable: false,
enumerable: true,
get: function () {
return require('./WrongUsernameOrPasswordError');
}
});
Object.defineProperty(api, 'UnauthorizedError', {
configurable: false,
enumerable: true,
get: function () {
return require('./UnauthorizedError');
}
});