1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const _ = require('lodash');
|
7 | const config = require('./config');
|
8 | const delay = require('delay');
|
9 | const util = require('util');
|
10 | const uuid = require('uuid-random');
|
11 |
|
12 | const api = {};
|
13 | module.exports = api;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | api.delay = delay;
|
21 |
|
22 |
|
23 | api.BedrockError = function(message, type, details, cause) {
|
24 | Error.call(this, message);
|
25 | Error.captureStackTrace(this, this.constructor);
|
26 | this.name = type;
|
27 | this.message = message;
|
28 | this.details = details || null;
|
29 | this.cause = cause || null;
|
30 | };
|
31 | util.inherits(api.BedrockError, Error);
|
32 | api.BedrockError.prototype.name = 'BedrockError';
|
33 | api.BedrockError.prototype.toObject = function(options) {
|
34 | options = options || {};
|
35 | options.public = options.public || false;
|
36 |
|
37 |
|
38 | const rval = _toObject(this, options);
|
39 |
|
40 |
|
41 | if(!options.public && config.core.errors.showStack) {
|
42 |
|
43 | rval.stack = _parseStack(this.stack);
|
44 | }
|
45 |
|
46 | return rval;
|
47 | };
|
48 |
|
49 | api.BedrockError.prototype.isType = function(type) {
|
50 | return api.hasValue(this, 'name', type);
|
51 | };
|
52 |
|
53 | api.BedrockError.prototype.hasType = function(type) {
|
54 | return this.isType(type) || this.hasCauseOfType(type);
|
55 | };
|
56 |
|
57 | api.BedrockError.prototype.hasCauseOfType = function(type) {
|
58 | if(this.cause && this.cause instanceof api.BedrockError) {
|
59 | return this.cause.hasType(type);
|
60 | }
|
61 | return false;
|
62 | };
|
63 |
|
64 | const _genericErrorJSON = {
|
65 | message: 'An internal server error occurred.',
|
66 | type: 'bedrock.InternalServerError'
|
67 | };
|
68 |
|
69 | const _errorMessageRegex = /^Error:\s*/;
|
70 | const _errorAtRegex = /^\s+at\s*/;
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function _parseStack(stack) {
|
79 | try {
|
80 | const lines = stack.split('\n');
|
81 | const messageLines = [];
|
82 | const atLines = [];
|
83 | for(let i = 0; i < lines.length; ++i) {
|
84 | const line = lines[i];
|
85 |
|
86 | if(line.match(_errorAtRegex)) {
|
87 | atLines.push(line.replace(_errorAtRegex, ''));
|
88 | } else {
|
89 |
|
90 | messageLines.push(line.replace(_errorMessageRegex, ''));
|
91 | }
|
92 | }
|
93 | return {
|
94 | message: messageLines.join('\n'),
|
95 | at: atLines
|
96 | };
|
97 | } catch(e) {
|
98 |
|
99 | return stack;
|
100 | }
|
101 | }
|
102 |
|
103 | function _toObject(err, options) {
|
104 | if(!err) {
|
105 | return null;
|
106 | }
|
107 |
|
108 | if(options.public) {
|
109 |
|
110 |
|
111 | if(err instanceof api.BedrockError &&
|
112 | err.details && err.details.public) {
|
113 | const details = api.clone(err.details);
|
114 | delete details.public;
|
115 |
|
116 | let {cause} = err;
|
117 | if(!(cause && cause instanceof api.BedrockError &&
|
118 | cause.details && cause.details.public)) {
|
119 | cause = null;
|
120 | }
|
121 | return {
|
122 | message: err.message,
|
123 | type: err.name,
|
124 | details,
|
125 | cause: _toObject(cause, options)
|
126 | };
|
127 | } else {
|
128 |
|
129 | return _genericErrorJSON;
|
130 | }
|
131 | } else {
|
132 |
|
133 | if(err instanceof api.BedrockError) {
|
134 | return {
|
135 | message: err.message,
|
136 | type: err.name,
|
137 | details: err.details,
|
138 | cause: _toObject(err.cause, options)
|
139 | };
|
140 | } else {
|
141 | return {
|
142 | message: err.message,
|
143 | type: err.name,
|
144 | details: {
|
145 | inspect: util.inspect(err, false, 10),
|
146 | stack: _parseStack(err.stack)
|
147 | },
|
148 | cause: null
|
149 | };
|
150 | }
|
151 | }
|
152 | }
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | api.w3cDate = function(date) {
|
162 | if(date === undefined || date === null) {
|
163 | date = new Date();
|
164 | } else if(typeof date === 'number' || typeof date === 'string') {
|
165 | date = new Date(date);
|
166 | }
|
167 | return util.format('%d-%s-%sT%s:%s:%sZ',
|
168 | date.getUTCFullYear(),
|
169 | _zeroFill(date.getUTCMonth() + 1),
|
170 | _zeroFill(date.getUTCDate()),
|
171 | _zeroFill(date.getUTCHours()),
|
172 | _zeroFill(date.getUTCMinutes()),
|
173 | _zeroFill(date.getUTCSeconds()));
|
174 | };
|
175 |
|
176 | function _zeroFill(num) {
|
177 | return (num < 10) ? '0' + num : '' + num;
|
178 | }
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | api.extend = function() {
|
190 | let deep = false;
|
191 | let i = 0;
|
192 | if(arguments.length > 0 && typeof arguments[0] === 'boolean') {
|
193 | deep = arguments[0];
|
194 | ++i;
|
195 | }
|
196 | const target = arguments[i] || {};
|
197 | i++;
|
198 | for(; i < arguments.length; ++i) {
|
199 | const obj = arguments[i] || {};
|
200 | Object.keys(obj).forEach(function(name) {
|
201 | const value = obj[name];
|
202 | if(deep && api.isObject(value) && !Array.isArray(value)) {
|
203 | target[name] = api.extend(true, target[name], value);
|
204 | } else {
|
205 | target[name] = value;
|
206 | }
|
207 | });
|
208 | }
|
209 | return target;
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | api.isObject = function(value) {
|
220 | return (Object.prototype.toString.call(value) === '[object Object]');
|
221 | };
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | api.clone = function(value) {
|
231 | if(value && typeof value === 'object') {
|
232 | let rval;
|
233 | if(Array.isArray(value)) {
|
234 | rval = new Array(value.length);
|
235 | for(let i = 0; i < rval.length; i++) {
|
236 | rval[i] = api.clone(value[i]);
|
237 | }
|
238 | } else {
|
239 | rval = {};
|
240 | for(const j in value) {
|
241 | rval[j] = api.clone(value[j]);
|
242 | }
|
243 | }
|
244 | return rval;
|
245 | }
|
246 | return value;
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | api.config = {};
|
253 |
|
254 |
|
255 | function _isPath(maybePath) {
|
256 | return typeof maybePath === 'string' || Array.isArray(maybePath);
|
257 | }
|
258 |
|
259 | function _setDefault(object, path, value) {
|
260 |
|
261 | if(typeof path === 'string') {
|
262 | path = _.toPath(path);
|
263 | }
|
264 | if(path.length) {
|
265 | let target = _.get(object, path);
|
266 | if(!target) {
|
267 | target = value;
|
268 | _.set(object, path, target);
|
269 | }
|
270 | return target;
|
271 | } else {
|
272 | return object;
|
273 | }
|
274 | }
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 | api.config.Config = function(object, options) {
|
286 | this.object = object;
|
287 | this.options = options || {};
|
288 | };
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | api.config.Config.prototype.set = function(path, value) {
|
301 | if(!_isPath(path)) {
|
302 | Object.keys(path).forEach(key => _.set(this.object, key, path[key]));
|
303 | return;
|
304 | }
|
305 | _.set(this.object, path, value);
|
306 | };
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | api.config.Config.prototype.setDefault = function(path, value) {
|
325 | if(!_isPath(path)) {
|
326 | const paths = {};
|
327 | Object.keys(path).forEach(key => {
|
328 | paths[key] = _setDefault(this.object, key, path[key]);
|
329 | });
|
330 | return paths;
|
331 | }
|
332 | return _setDefault(this.object, path, value);
|
333 | };
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | api.config.Config.prototype.setComputed =
|
353 | function(path, fnOrExpression, options) {
|
354 | if(!_isPath(path)) {
|
355 | options = fnOrExpression;
|
356 | Object.keys(path).forEach(key => this.setComputed(
|
357 | key, path[key], options));
|
358 | return;
|
359 | }
|
360 | if(typeof fnOrExpression === 'string') {
|
361 |
|
362 | fnOrExpression = _.template(fnOrExpression);
|
363 | } else if(typeof fnOrExpression !== 'function') {
|
364 |
|
365 | return this.set(path, fnOrExpression);
|
366 | }
|
367 |
|
368 | if(typeof path === 'string') {
|
369 | path = _.toPath(path);
|
370 | }
|
371 |
|
372 | options = options || {};
|
373 | const locals = options.locals || this.options.locals || config;
|
374 |
|
375 | const targetPath = path.slice(0, -1);
|
376 |
|
377 | const targetKey = path.slice(-1);
|
378 |
|
379 | const parentDefault = options.parentDefault || {};
|
380 | const target = _setDefault(this.object, targetPath, parentDefault);
|
381 |
|
382 | let _isSet = false;
|
383 | let _value;
|
384 | Object.defineProperty(target, targetKey, {
|
385 | configurable: true,
|
386 | enumerable: true,
|
387 | get: () => {
|
388 | if(_isSet) {
|
389 | return _value;
|
390 | }
|
391 | return fnOrExpression(locals);
|
392 | },
|
393 | set: value => {
|
394 | _isSet = true;
|
395 | _value = value;
|
396 | }
|
397 | });
|
398 | };
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | api.config.Config.prototype.computer = function() {
|
410 | return this.setComputed.bind(this);
|
411 | };
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | api.config.Config.prototype.pushComputed =
|
424 | function(path, fnOrExpression, options) {
|
425 |
|
426 | const target = _.get(this.object, path, []);
|
427 |
|
428 | const pushPath = _.toPath(path);
|
429 | pushPath.push(target.length);
|
430 |
|
431 | const pushOptions = Object.assign({}, options, {parentDefault: []});
|
432 |
|
433 | this.setComputed(pushPath, fnOrExpression, pushOptions);
|
434 | };
|
435 |
|
436 |
|
437 |
|
438 |
|
439 | api.config.main = new api.config.Config(config);
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 | api.uuid = uuid;
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 | api.boolify = function(value) {
|
458 | if(typeof value === 'boolean') {
|
459 | return value;
|
460 | }
|
461 | if(typeof value === 'string' && value) {
|
462 | switch(value.toLowerCase()) {
|
463 | case 'true':
|
464 | case 't':
|
465 | case '1':
|
466 | case 'yes':
|
467 | case 'y':
|
468 | return true;
|
469 | case 'false':
|
470 | case 'f':
|
471 | case '0':
|
472 | case 'no':
|
473 | case 'n':
|
474 | return false;
|
475 | }
|
476 | }
|
477 |
|
478 | throw new Error('Invalid boolean:' + value);
|
479 | };
|
480 |
|
481 | api.callbackify = fn => {
|
482 | const callbackVersion = util.callbackify(fn);
|
483 | return function(...args) {
|
484 | const callback = args[args.length - 1];
|
485 | if(typeof callback === 'function') {
|
486 | return callbackVersion.apply(null, args);
|
487 | }
|
488 | return fn.apply(null, args);
|
489 | };
|
490 | };
|
491 |
|
492 |
|
493 | api.hasValue = (obj, key, value) => {
|
494 | const t = obj[key];
|
495 | if(Array.isArray(t)) {
|
496 | return t.includes(value);
|
497 | }
|
498 | return t === value;
|
499 | };
|