-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (192 loc) · 5.11 KB
/
Copy pathindex.js
File metadata and controls
227 lines (192 loc) · 5.11 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
/**
* Common utilities module.
*
* @module x2node-common
*/
'use strict';
/////////////////////////////////////////////////////////////////////////////////
// Externals.
/////////////////////////////////////////////////////////////////////////////////
/**
* Node.js <code>Error</code> object.
*
* @external Error
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/errors.html#errors_class_error}
*/
/**
* Node.js <code>EventEmitter</code> object.
*
* @external EventEmitter
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/events.html#events_class_eventemitter}
*/
/**
* Node.js <code>Buffer</code> object.
*
* @external Buffer
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/buffer.html#buffer_class_buffer}
*/
/**
* Node.js <code>stream.Readable</code> object.
*
* @external stream.Readable
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/stream.html#stream_class_stream_readable}
*/
/**
* Node.js parsed URL object.
*
* @external Url
* @see {@link https://nodejs.org/dist/latest-v4.x/docs/api/url.html}
*/
/////////////////////////////////////////////////////////////////////////////////
// Errors
/////////////////////////////////////////////////////////////////////////////////
/**
* General framework usage error, such as invalid arguments, inappropriate
* function call, etc.
*
* @extends external:Error
*/
exports.X2UsageError = class extends Error {
/**
* Create new error to throw.
*
* @param {string} message The error description.
*/
constructor(message) {
super(message);
this.name = 'X2UsageError';
Error.captureStackTrace(this, this.constructor);
}
}
/**
* Provided syntax error.
*
* @extends external:Error
*/
exports.X2SyntaxError = class extends Error {
/**
* Create new error to throw.
*
* @param {string} message The error description.
*/
constructor(message) {
super(message);
this.name = 'X2SyntaxError';
Error.captureStackTrace(this, this.constructor);
}
}
/**
* Provided data error.
*
* @extends external:Error
*/
exports.X2DataError = class extends Error {
/**
* Create new error to throw.
*
* @param {string} message The error description.
*/
constructor(message) {
super(message);
this.name = 'X2DataError';
Error.captureStackTrace(this, this.constructor);
}
}
/////////////////////////////////////////////////////////////////////////////////
// Logger
/////////////////////////////////////////////////////////////////////////////////
/**
* Debug loggers.
*
* @private
* @type {Object.<string,function>}
*/
const DEBUG_LOGGERS = {};
/**
* Build message build functions list.
*
* @private
* @param {string} [section] Debug log section, nothing if error log.
* @returns {Array.<function>} Message builder functions.
*/
function buildMessageBuilder(section) {
const logOptions = process.env.X2_LOG || '';
const msgBuilder = new Array();
if (!/(^|,)nots(,|$)/i.test(logOptions))
msgBuilder.push(() => (new Date()).toISOString());
if (!/(^|,)nopid(,|$)/i.test(logOptions))
msgBuilder.push(() => String(process.pid));
let m;
const envRE = new RegExp('(?:^|,)env:([^,]+)', 'gi');
while ((m = envRE.exec(logOptions)) !== null) {
const envName = m[1];
msgBuilder.push(() => process.env[envName]);
}
if (section && !/(^|,)nosec(,|$)/i.test(logOptions))
msgBuilder.push(() => section);
return msgBuilder;
}
/**
* Get debug logger.
*
* @param {string} section Section being debugged.
* @returns {function} Debug logger function that takes one argument, which is
* the debug message. The function also has a read-only Boolean property
* <code>enabled</code>, which tells if the logger is enabled or if it is a noop.
*/
exports.getDebugLogger = function(section) {
const sectionUC = section.toUpperCase();
let logger = DEBUG_LOGGERS[sectionUC];
if (!logger) {
const re = new RegExp(`\\b${sectionUC}\\b`, 'i');
const enabled = re.test(process.env.NODE_DEBUG);
if (enabled) {
const msgBuilder = buildMessageBuilder(sectionUC)
const numParts = msgBuilder.length;
if (numParts > 0) {
const f = msgBuilder[numParts - 1];
msgBuilder[numParts - 1] = () => f() + ':';
}
msgBuilder.push(msg => msg);
logger = function(msg) {
/* eslint-disable no-console */
console.error(msgBuilder.map(p => p(msg)).join(' '));
/* eslint-enable no-console */
};
} else {
logger = function() {};
}
Object.defineProperty(logger, 'enabled', {
value: enabled,
writable: false
});
DEBUG_LOGGERS[sectionUC] = logger;
}
return logger;
};
/**
* The error logger function.
*
* @private
* @type {function}
*/
let errorLogger;
/**
* Log application error.
*
* @param {string} msg Error message.
* @param {external:Error} [err] Optional error object.
*/
exports.error = function(msg, err) {
if (!errorLogger) {
const msgBuilder = buildMessageBuilder();
msgBuilder.push(() => 'ERROR:');
msgBuilder.push((msg, err) => `${msg}${err ? '\n' + err.stack : ''}`);
errorLogger = function(msg, err) {
/* eslint-disable no-console */
console.error(msgBuilder.map(p => p(msg, err)).join(' '));
/* eslint-enable no-console */
};
}
errorLogger(msg, err);
};