source: trip-planner-front/node_modules/finalhandler/node_modules/debug/src/node.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.9 KB
Line 
1/**
2 * Module dependencies.
3 */
4
5var tty = require('tty');
6var util = require('util');
7
8/**
9 * This is the Node.js implementation of `debug()`.
10 *
11 * Expose `debug()` as the module.
12 */
13
14exports = module.exports = require('./debug');
15exports.init = init;
16exports.log = log;
17exports.formatArgs = formatArgs;
18exports.save = save;
19exports.load = load;
20exports.useColors = useColors;
21
22/**
23 * Colors.
24 */
25
26exports.colors = [6, 2, 3, 4, 5, 1];
27
28/**
29 * Build up the default `inspectOpts` object from the environment variables.
30 *
31 * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
32 */
33
34exports.inspectOpts = Object.keys(process.env).filter(function (key) {
35 return /^debug_/i.test(key);
36}).reduce(function (obj, key) {
37 // camel-case
38 var prop = key
39 .substring(6)
40 .toLowerCase()
41 .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
42
43 // coerce string value into JS value
44 var val = process.env[key];
45 if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
46 else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
47 else if (val === 'null') val = null;
48 else val = Number(val);
49
50 obj[prop] = val;
51 return obj;
52}, {});
53
54/**
55 * The file descriptor to write the `debug()` calls to.
56 * Set the `DEBUG_FD` env variable to override with another value. i.e.:
57 *
58 * $ DEBUG_FD=3 node script.js 3>debug.log
59 */
60
61var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
62
63if (1 !== fd && 2 !== fd) {
64 util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')()
65}
66
67var stream = 1 === fd ? process.stdout :
68 2 === fd ? process.stderr :
69 createWritableStdioStream(fd);
70
71/**
72 * Is stdout a TTY? Colored output is enabled when `true`.
73 */
74
75function useColors() {
76 return 'colors' in exports.inspectOpts
77 ? Boolean(exports.inspectOpts.colors)
78 : tty.isatty(fd);
79}
80
81/**
82 * Map %o to `util.inspect()`, all on a single line.
83 */
84
85exports.formatters.o = function(v) {
86 this.inspectOpts.colors = this.useColors;
87 return util.inspect(v, this.inspectOpts)
88 .split('\n').map(function(str) {
89 return str.trim()
90 }).join(' ');
91};
92
93/**
94 * Map %o to `util.inspect()`, allowing multiple lines if needed.
95 */
96
97exports.formatters.O = function(v) {
98 this.inspectOpts.colors = this.useColors;
99 return util.inspect(v, this.inspectOpts);
100};
101
102/**
103 * Adds ANSI color escape codes if enabled.
104 *
105 * @api public
106 */
107
108function formatArgs(args) {
109 var name = this.namespace;
110 var useColors = this.useColors;
111
112 if (useColors) {
113 var c = this.color;
114 var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
115
116 args[0] = prefix + args[0].split('\n').join('\n' + prefix);
117 args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
118 } else {
119 args[0] = new Date().toUTCString()
120 + ' ' + name + ' ' + args[0];
121 }
122}
123
124/**
125 * Invokes `util.format()` with the specified arguments and writes to `stream`.
126 */
127
128function log() {
129 return stream.write(util.format.apply(util, arguments) + '\n');
130}
131
132/**
133 * Save `namespaces`.
134 *
135 * @param {String} namespaces
136 * @api private
137 */
138
139function save(namespaces) {
140 if (null == namespaces) {
141 // If you set a process.env field to null or undefined, it gets cast to the
142 // string 'null' or 'undefined'. Just delete instead.
143 delete process.env.DEBUG;
144 } else {
145 process.env.DEBUG = namespaces;
146 }
147}
148
149/**
150 * Load `namespaces`.
151 *
152 * @return {String} returns the previously persisted debug modes
153 * @api private
154 */
155
156function load() {
157 return process.env.DEBUG;
158}
159
160/**
161 * Copied from `node/src/node.js`.
162 *
163 * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
164 * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
165 */
166
167function createWritableStdioStream (fd) {
168 var stream;
169 var tty_wrap = process.binding('tty_wrap');
170
171 // Note stream._type is used for test-module-load-list.js
172
173 switch (tty_wrap.guessHandleType(fd)) {
174 case 'TTY':
175 stream = new tty.WriteStream(fd);
176 stream._type = 'tty';
177
178 // Hack to have stream not keep the event loop alive.
179 // See https://github.com/joyent/node/issues/1726
180 if (stream._handle && stream._handle.unref) {
181 stream._handle.unref();
182 }
183 break;
184
185 case 'FILE':
186 var fs = require('fs');
187 stream = new fs.SyncWriteStream(fd, { autoClose: false });
188 stream._type = 'fs';
189 break;
190
191 case 'PIPE':
192 case 'TCP':
193 var net = require('net');
194 stream = new net.Socket({
195 fd: fd,
196 readable: false,
197 writable: true
198 });
199
200 // FIXME Should probably have an option in net.Socket to create a
201 // stream from an existing fd which is writable only. But for now
202 // we'll just add this hack and set the `readable` member to false.
203 // Test: ./node test/fixtures/echo.js < /etc/passwd
204 stream.readable = false;
205 stream.read = null;
206 stream._type = 'pipe';
207
208 // FIXME Hack to have stream not keep the event loop alive.
209 // See https://github.com/joyent/node/issues/1726
210 if (stream._handle && stream._handle.unref) {
211 stream._handle.unref();
212 }
213 break;
214
215 default:
216 // Probably an error on in uv_guess_handle()
217 throw new Error('Implement me. Unknown stream file type!');
218 }
219
220 // For supporting legacy API we put the FD here.
221 stream.fd = fd;
222
223 stream._isStdio = true;
224
225 return stream;
226}
227
228/**
229 * Init logic for `debug` instances.
230 *
231 * Create a new `inspectOpts` object in case `useColors` is set
232 * differently for a particular `debug` instance.
233 */
234
235function init (debug) {
236 debug.inspectOpts = {};
237
238 var keys = Object.keys(exports.inspectOpts);
239 for (var i = 0; i < keys.length; i++) {
240 debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
241 }
242}
243
244/**
245 * Enable namespaces listed in `process.env.DEBUG` initially.
246 */
247
248exports.enable(load());
Note: See TracBrowser for help on using the repository browser.