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