-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller.js
More file actions
375 lines (343 loc) · 10.4 KB
/
controller.js
File metadata and controls
375 lines (343 loc) · 10.4 KB
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
var _ = require('lodash');
var util = require('util');
var fs = require('fs');
var path = require('path');
var datefmt = require('dateformat');
var extend = require('deep-extend');
var BetterLog = require('./log');
var morgan = require('./morgan');
var _mode = ''; // The current mode
var _morgan = null; // The real morgan object
var _groups = {}; // Map group => array of sections
var _modes = {}; // Map mode => mode options
var _formats = {}; // Map type => format function
// Display options
var _display = {
dateformat: 'yyyy-mm-dd HH:MM:ss',
stackIndex: 1,
maxTraceDepth: 20,
};
// Map section => Map type => Writable
var _outputs = {
_default: {
_default: process.stdout,
error: process.stderr,
warn: process.stderr,
}
};
// Map [section|type] => Boolean
var _visible = {
'_default/_default': true
};
// Helper methods
var _objectify = function (arr, val) {
var obj = {};
arr.forEach(function (k) { obj[k] = val });
return obj;
}
var _dedupe = function (arr) {
return Object.keys(_objectify(arr, true));
}
var _resolveSections = function (sectionsOrGroups) {
if (!Array.isArray(sectionsOrGroups)) return [];
return [].concat.apply([], sectionsOrGroups.map(function (sectionOrGroup) { return _resolveSection(sectionOrGroup) }));
}
var _resolveSection = function (sectionOrGroup) {
if (typeof sectionOrGroup !== 'string') return [];
if (_groups[sectionOrGroup]) {
return _resolveSections(_groups[sectionOrGroup]);
}
return [sectionOrGroup];
}
var _writeToOutput = function (section, type, msg) {
var output = _getOutputStream(section, type);
if (output) {
output.write(String(msg));
}
}
var _isWritable = function (stream) {
return (stream &&
typeof stream.write === 'function' &&
typeof stream.end === 'function')
}
var _getStack = function (stackIndex) {
// get call stack, and analyze it
// get all file,method and line number
// https://github.com/v8/v8/wiki/Stack%20Trace%20API
var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/i;
var stackReg2 = /at\s+()(.*):(\d*):(\d*)/i;
var stacklist = (new Error()).stack.split('\n').slice(3);
var s = stacklist[stackIndex];
var sp = stackReg.exec(s) || stackReg2.exec(s);
var stack = {};
if (sp && sp.length === 5) {
stack.fn = sp[1] || '<anonymous>';
stack.path = sp[2];
stack.line = sp[3];
stack.pos = sp[4];
stack.file = path.basename(stack.path);
stack.stack = stacklist.join('\n');
}
return stack;
}
var _makeFormatter = function (format) {
var needStack = /{{(fn|path|line|pos|file|stack)}}/i.test(format);
return function () {
var display = this;
var output = format;
output = output.replace(/{{timestamp}}/gi, datefmt(display.dateformat));
output = output.replace(/{{type}}/gi, display.logType);
output = output.replace(/{{section}}/gi, display.section);
if (needStack) {
var stack = _getStack(display.stackIndex);
output = output.replace(/{{fn}}/gi, stack.fn);
output = output.replace(/{{path}}/gi, stack.path);
output = output.replace(/{{line}}/gi, stack.line);
output = output.replace(/{{pos}}/gi, stack.pos);
output = output.replace(/{{file}}/gi, stack.file);
output = output.replace(/{{stack}}/gi, stack.stack);
}
var args = Array.prototype.slice.call(arguments);
if (!args.length) return '';
if (typeof args[0] === 'string') {
var message = args.shift().replace(/%[sdjt]/g, function(x) {
if (!args.length) return x;
if (x === '%s') {
return String(args.shift());
}
if (x === '%d') {
return Number(args.shift());
}
if (x === '%j') {
try {
var obj = args.shift();
if (obj instanceof Error) {
return JSON.stringify(obj, ['message', 'stack', 'type', 'name']);
}
return JSON.stringify(obj);
} catch(e) {
return '[Circular]';
}
}
return x;
})
}
args = args.map(function (arg) {
if (typeof arg === 'object') {
try {
return util.inspect(arg, { depth: display.maxTraceDepth });
} catch(e) {
return '[Circular]';
}
}
return String(arg)
});
if (message !== undefined) {
args.unshift(message);
}
output = output.replace(/{{message}}/gi, args.join(' '));
return output;
}
}
var _shouldShow = function (section, type) {
var combined = section + '/' + type;
var type = '_default/' + type;
var section = section + '/_default';
var def = '_default/_default';
if (_visible[combined] !== undefined) return _visible[combined];
if (_visible[section] !== undefined && _visible[type] !== undefined) {
if (_visible[section] && _visible[type]) return true;
if (!_visible[section] && !_visible[type]) return false;
return _visible[def];
}
if (_visible[section] !== undefined) return _visible[section];
if (_visible[type] !== undefined) return _visible[type];
return _visible[def];
}
var _makeLog = function (type) {
if (typeof type !== 'string') return;
return function () {
var log = this;
var section = log.section;
if (!_shouldShow(section, type)) return;
var message = _formats[type].apply(extend({ section: log.section, logType: type }, _display), arguments);
log.push(message);
_writeToOutput(section, type, message);
}
}
var _getOutputStream = function (section, type) {
if (typeof section !== 'string') return null;
if (typeof type !== 'string') return null;
if (_outputs[section]) {
if (_outputs[section][type]) {
return _outputs[section][type];
}
if (_outputs[section]['_default']) {
return _outputs[section]['_default'];
}
}
if (_outputs['_default']) {
if (_outputs['_default'][type]) {
return _outputs['_default'][type];
}
if (_outputs['_default']['_default']) {
return _outputs['_default']['_default'];
}
}
return null;
}
var _refreshMorgan = function (opts) {
opts = opts || {};
opts.stream = _getOutputStream('morgan', 'morgan')
morgan.token('datefmt', function () {
return datefmt(new Date(), _display.dateformat);
});
_morgan = morgan(exports.format('morgan'), opts);
}
exports.display = function (key, val) {
if (typeof key !== 'string') return;
_display[key] = val;
}
exports.modes = function () {
return Object.keys(_modes);
}
exports.show = function (input) {
if (!input) {
_visible = {
'_default/_default': true
}
return;
}
if (typeof input !== 'string') return;
var parts = input.split('/', 2);
var section = parts[0];
var type = parts[1] || '_default';
if (_formats[section] && parts.length === 1) {
type = section;
section = '_default';
}
extend(_visible, _objectify(_resolveSection(section).map(function (section) { return section + '/' + type; }), true));
}
exports.hide = function (input) {
if (!input) {
_visible = {
'_default/_default': false
}
return;
}
if (typeof input !== 'string') return;
var parts = input.split('/', 2);
var section = parts[0];
var type = parts[1] || '_default';
if (_formats[section] && parts.length === 1) {
type = section;
section = '_default';
}
extend(_visible, _objectify(_resolveSection(section).map(function (section) { return section + '/' + type; }), false));
}
exports.reset = function () {
_visible = {
'_default/_default': true
};
}
exports.mode = function (modeName, modeOptions) {
if (arguments.length === 0) return _mode;
if (typeof modeName !== 'string') return;
if (modeOptions === undefined) {
var modeOptions = _modes[modeName];
if (typeof modeOptions !== 'object') return;
_mode = modeName;
_visible = {};
if (modeOptions.show) {
modeOptions.show.forEach(function (section) {
exports.show(section);
})
}
if (modeOptions.hide) {
modeOptions.hide.forEach(function (section) {
exports.hide(section);
})
}
if (modeOptions.showByDefault !== undefined) {
_visible['_default/_default'] = modeOptions.showByDefault;
} else {
_visible['_default/_default'] = true;
}
} else if (!modeOptions) {
return delete _modes[modeName];
}
if (typeof modeOptions !== 'object') return;
_modes[modeName] = modeOptions;
}
exports.groups = function () {
return Object.keys(_groups);
}
exports.group = function (groupName, groupSections) {
if (typeof groupName !== 'string') return;
if (groupSections === undefined) {
return _groups[groupName] || null;
} else if (!groupSections) {
return delete _groups[groupName];
}
if (!Array.isArray(groupSections)) return;
_groups[groupName] = _dedupe(groupSections);
}
exports.formats = function () {
return Object.keys(_formats);
}
exports.format = function (logTypeName, logFormatter) {
if (typeof logTypeName !== 'string') return;
if (logFormatter === undefined) {
return _formats[logTypeName] || '';
} else if (!logFormatter) {
delete BetterLog.prototype[logTypeName];
delete _formats[logTypeName];
}
if (logTypeName === 'morgan') {
_formats[logTypeName] = logFormatter;
return _refreshMorgan();
}
if (typeof logFormatter === 'string') {
logFormatter = _makeFormatter(logFormatter);
}
if (typeof logFormatter !== 'function') return;
_formats[logTypeName] = logFormatter;
BetterLog.prototype[logTypeName] = _makeLog(logTypeName);
}
// Usage:
// - output(writeable)
// - output(section, writeable)
// - output(type, writeable)
// - output('section/type', writeable)
exports.output = function (section, stream) {
if (section === undefined) return;
if (typeof section !== 'string' || stream === undefined) {
stream = section;
section = '_default';
}
if (typeof stream === 'string') {
stream = fs.createWriteStream(stream, { flags: 'a' });
}
if (!_isWritable(stream)) {
throw new Error('output is not writable');
}
if (_formats[section]) {
section = '_default/' + section;
}
var parts = section.split('/', 2);
var sectionOrGroup = parts[0];
var type = parts.length === 2 ? parts[1] : '_default';
_resolveSection(sectionOrGroup).forEach(function (section) {
if (section === 'morgan' || (section === '_default' && !_outputs['morgan'])) {
_refreshMorgan();
}
_outputs[section] = _outputs[section] || {};
_outputs[section][type] = stream;
})
}
exports.morgan = function (opts) {
_refreshMorgan(opts);
return function (req, res, next) {
_morgan(req, res, next);
}
}