util: add Map and Set inspection support
[platform/upstream/nodejs.git] / lib / util.js
1 'use strict';
2
3 const uv = process.binding('uv');
4 const Debug = require('vm').runInDebugContext('Debug');
5
6 const formatRegExp = /%[sdj%]/g;
7 exports.format = function(f) {
8   if (typeof f !== 'string') {
9     var objects = [];
10     for (var i = 0; i < arguments.length; i++) {
11       objects.push(inspect(arguments[i]));
12     }
13     return objects.join(' ');
14   }
15
16   var i = 1;
17   var args = arguments;
18   var len = args.length;
19   var str = String(f).replace(formatRegExp, function(x) {
20     if (x === '%%') return '%';
21     if (i >= len) return x;
22     switch (x) {
23       case '%s': return String(args[i++]);
24       case '%d': return Number(args[i++]);
25       case '%j':
26         try {
27           return JSON.stringify(args[i++]);
28         } catch (_) {
29           return '[Circular]';
30         }
31       default:
32         return x;
33     }
34   });
35   for (var x = args[i]; i < len; x = args[++i]) {
36     if (x === null || (typeof x !== 'object' && typeof x !== 'symbol')) {
37       str += ' ' + x;
38     } else {
39       str += ' ' + inspect(x);
40     }
41   }
42   return str;
43 };
44
45
46 // Mark that a method should not be used.
47 // Returns a modified function which warns once by default.
48 // If --no-deprecation is set, then it is a no-op.
49 exports.deprecate = function(fn, msg) {
50   // Allow for deprecating things in the process of starting up.
51   if (global.process === undefined) {
52     return function() {
53       return exports.deprecate(fn, msg).apply(this, arguments);
54     };
55   }
56
57   if (process.noDeprecation === true) {
58     return fn;
59   }
60
61   var warned = false;
62   function deprecated() {
63     if (!warned) {
64       if (process.throwDeprecation) {
65         throw new Error(msg);
66       } else if (process.traceDeprecation) {
67         console.trace(msg);
68       } else {
69         console.error(msg);
70       }
71       warned = true;
72     }
73     return fn.apply(this, arguments);
74   }
75
76   return deprecated;
77 };
78
79
80 var debugs = {};
81 var debugEnviron;
82 exports.debuglog = function(set) {
83   if (debugEnviron === undefined)
84     debugEnviron = process.env.NODE_DEBUG || '';
85   set = set.toUpperCase();
86   if (!debugs[set]) {
87     if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
88       var pid = process.pid;
89       debugs[set] = function() {
90         var msg = exports.format.apply(exports, arguments);
91         console.error('%s %d: %s', set, pid, msg);
92       };
93     } else {
94       debugs[set] = function() {};
95     }
96   }
97   return debugs[set];
98 };
99
100
101 /**
102  * Echos the value of a value. Trys to print the value out
103  * in the best way possible given the different types.
104  *
105  * @param {Object} obj The object to print out.
106  * @param {Object} opts Optional options object that alters the output.
107  */
108 /* legacy: obj, showHidden, depth, colors*/
109 function inspect(obj, opts) {
110   // default options
111   var ctx = {
112     seen: [],
113     stylize: stylizeNoColor
114   };
115   // legacy...
116   if (arguments.length >= 3) ctx.depth = arguments[2];
117   if (arguments.length >= 4) ctx.colors = arguments[3];
118   if (typeof opts === 'boolean') {
119     // legacy...
120     ctx.showHidden = opts;
121   } else if (opts) {
122     // got an "options" object
123     exports._extend(ctx, opts);
124   }
125   // set default options
126   if (ctx.showHidden === undefined) ctx.showHidden = false;
127   if (ctx.depth === undefined) ctx.depth = 2;
128   if (ctx.colors === undefined) ctx.colors = false;
129   if (ctx.customInspect === undefined) ctx.customInspect = true;
130   if (ctx.colors) ctx.stylize = stylizeWithColor;
131   return formatValue(ctx, obj, ctx.depth);
132 }
133 exports.inspect = inspect;
134
135
136 // http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
137 inspect.colors = {
138   'bold' : [1, 22],
139   'italic' : [3, 23],
140   'underline' : [4, 24],
141   'inverse' : [7, 27],
142   'white' : [37, 39],
143   'grey' : [90, 39],
144   'black' : [30, 39],
145   'blue' : [34, 39],
146   'cyan' : [36, 39],
147   'green' : [32, 39],
148   'magenta' : [35, 39],
149   'red' : [31, 39],
150   'yellow' : [33, 39]
151 };
152
153 // Don't use 'blue' not visible on cmd.exe
154 inspect.styles = {
155   'special': 'cyan',
156   'number': 'yellow',
157   'boolean': 'yellow',
158   'undefined': 'grey',
159   'null': 'bold',
160   'string': 'green',
161   'symbol': 'green',
162   'date': 'magenta',
163   // "name": intentionally not styling
164   'regexp': 'red'
165 };
166
167
168 function stylizeWithColor(str, styleType) {
169   var style = inspect.styles[styleType];
170
171   if (style) {
172     return '\u001b[' + inspect.colors[style][0] + 'm' + str +
173            '\u001b[' + inspect.colors[style][1] + 'm';
174   } else {
175     return str;
176   }
177 }
178
179
180 function stylizeNoColor(str, styleType) {
181   return str;
182 }
183
184
185 function arrayToHash(array) {
186   var hash = {};
187
188   array.forEach(function(val, idx) {
189     hash[val] = true;
190   });
191
192   return hash;
193 }
194
195
196 function inspectPromise(p) {
197   var mirror = Debug.MakeMirror(p, true);
198   if (!mirror.isPromise())
199     return null;
200   return {status: mirror.status(), value: mirror.promiseValue().value_};
201 }
202
203
204 function formatValue(ctx, value, recurseTimes) {
205   // Provide a hook for user-specified inspect functions.
206   // Check that value is an object with an inspect function on it
207   if (ctx.customInspect &&
208       value &&
209       typeof value.inspect === 'function' &&
210       // Filter out the util module, it's inspect function is special
211       value.inspect !== exports.inspect &&
212       // Also filter out any prototype objects using the circular check.
213       !(value.constructor && value.constructor.prototype === value)) {
214     var ret = value.inspect(recurseTimes, ctx);
215     if (typeof ret !== 'string') {
216       ret = formatValue(ctx, ret, recurseTimes);
217     }
218     return ret;
219   }
220
221   // Primitive types cannot have properties
222   var primitive = formatPrimitive(ctx, value);
223   if (primitive) {
224     return primitive;
225   }
226
227   // Look up the keys of the object.
228   var keys = Object.keys(value);
229   var visibleKeys = arrayToHash(keys);
230
231   if (ctx.showHidden) {
232     keys = Object.getOwnPropertyNames(value);
233     keys = keys.concat(Object.getOwnPropertySymbols(value));
234   }
235
236   // This could be a boxed primitive (new String(), etc.), check valueOf()
237   // NOTE: Avoid calling `valueOf` on `Date` instance because it will return
238   // a number which, when object has some additional user-stored `keys`,
239   // will be printed out.
240   var formatted;
241   var raw = value;
242   try {
243     // the .valueOf() call can fail for a multitude of reasons
244     if (!isDate(value))
245       raw = value.valueOf();
246   } catch (e) {
247     // ignore...
248   }
249
250   if (typeof raw === 'string') {
251     // for boxed Strings, we have to remove the 0-n indexed entries,
252     // since they just noisey up the output and are redundant
253     keys = keys.filter(function(key) {
254       return !(key >= 0 && key < raw.length);
255     });
256   }
257
258   // Some type of object without properties can be shortcutted.
259   if (keys.length === 0) {
260     if (typeof value === 'function') {
261       var name = value.name ? ': ' + value.name : '';
262       return ctx.stylize('[Function' + name + ']', 'special');
263     }
264     if (isRegExp(value)) {
265       return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
266     }
267     if (isDate(value)) {
268       return ctx.stylize(Date.prototype.toString.call(value), 'date');
269     }
270     if (isError(value)) {
271       return formatError(value);
272     }
273     // now check the `raw` value to handle boxed primitives
274     if (typeof raw === 'string') {
275       formatted = formatPrimitiveNoColor(ctx, raw);
276       return ctx.stylize('[String: ' + formatted + ']', 'string');
277     }
278     if (typeof raw === 'number') {
279       formatted = formatPrimitiveNoColor(ctx, raw);
280       return ctx.stylize('[Number: ' + formatted + ']', 'number');
281     }
282     if (typeof raw === 'boolean') {
283       formatted = formatPrimitiveNoColor(ctx, raw);
284       return ctx.stylize('[Boolean: ' + formatted + ']', 'boolean');
285     }
286   }
287
288   var base = '', empty = false, braces, formatter;
289
290   if (Array.isArray(value)) {
291     braces = ['[', ']'];
292     empty = value.length === 0;
293     formatter = formatArray;
294   } else if (value instanceof Set) {
295     braces = ['Set {', '}'];
296     // With `showHidden`, `length` will display as a hidden property for
297     // arrays. For consistency's sake, do the same for `size`, even though this
298     // property isn't selected by Object.getOwnPropertyNames().
299     if (ctx.showHidden)
300       keys.unshift('size');
301     empty = value.size === 0;
302     formatter = formatSet;
303   } else if (value instanceof Map) {
304     braces = ['Map {', '}'];
305     // Ditto.
306     if (ctx.showHidden)
307       keys.unshift('size');
308     empty = value.size === 0;
309     formatter = formatMap;
310   } else {
311     // Only create a mirror if the object superficially looks like a Promise.
312     var promiseInternals = value instanceof Promise && inspectPromise(value);
313     if (promiseInternals) {
314       braces = ['Promise {', '}'];
315       formatter = formatPromise;
316     } else {
317       braces = ['{', '}'];
318       empty = true;  // No other data than keys.
319       formatter = formatObject;
320     }
321   }
322
323   empty = empty === true && keys.length === 0;
324
325   // Make functions say that they are functions
326   if (typeof value === 'function') {
327     var n = value.name ? ': ' + value.name : '';
328     base = ' [Function' + n + ']';
329   }
330
331   // Make RegExps say that they are RegExps
332   if (isRegExp(value)) {
333     base = ' ' + RegExp.prototype.toString.call(value);
334   }
335
336   // Make dates with properties first say the date
337   if (isDate(value)) {
338     base = ' ' + Date.prototype.toUTCString.call(value);
339   }
340
341   // Make error with message first say the error
342   if (isError(value)) {
343     base = ' ' + formatError(value);
344   }
345
346   // Make boxed primitive Strings look like such
347   if (typeof raw === 'string') {
348     formatted = formatPrimitiveNoColor(ctx, raw);
349     base = ' ' + '[String: ' + formatted + ']';
350   }
351
352   // Make boxed primitive Numbers look like such
353   if (typeof raw === 'number') {
354     formatted = formatPrimitiveNoColor(ctx, raw);
355     base = ' ' + '[Number: ' + formatted + ']';
356   }
357
358   // Make boxed primitive Booleans look like such
359   if (typeof raw === 'boolean') {
360     formatted = formatPrimitiveNoColor(ctx, raw);
361     base = ' ' + '[Boolean: ' + formatted + ']';
362   }
363
364   if (empty === true) {
365     return braces[0] + base + braces[1];
366   }
367
368   if (recurseTimes < 0) {
369     if (isRegExp(value)) {
370       return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
371     } else {
372       return ctx.stylize('[Object]', 'special');
373     }
374   }
375
376   ctx.seen.push(value);
377
378   var output = formatter(ctx, value, recurseTimes, visibleKeys, keys);
379
380   ctx.seen.pop();
381
382   return reduceToSingleString(output, base, braces);
383 }
384
385
386 function formatPrimitive(ctx, value) {
387   if (value === undefined)
388     return ctx.stylize('undefined', 'undefined');
389
390   // For some reason typeof null is "object", so special case here.
391   if (value === null)
392     return ctx.stylize('null', 'null');
393
394   var type = typeof value;
395
396   if (type === 'string') {
397     var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
398         .replace(/'/g, "\\'")
399         .replace(/\\"/g, '"') + '\'';
400     return ctx.stylize(simple, 'string');
401   }
402   if (type === 'number') {
403     // Format -0 as '-0'. Strict equality won't distinguish 0 from -0,
404     // so instead we use the fact that 1 / -0 < 0 whereas 1 / 0 > 0 .
405     if (value === 0 && 1 / value < 0)
406       return ctx.stylize('-0', 'number');
407     return ctx.stylize('' + value, 'number');
408   }
409   if (type === 'boolean')
410     return ctx.stylize('' + value, 'boolean');
411   // es6 symbol primitive
412   if (type === 'symbol')
413     return ctx.stylize(value.toString(), 'symbol');
414 }
415
416
417 function formatPrimitiveNoColor(ctx, value) {
418   var stylize = ctx.stylize;
419   ctx.stylize = stylizeNoColor;
420   var str = formatPrimitive(ctx, value);
421   ctx.stylize = stylize;
422   return str;
423 }
424
425
426 function formatError(value) {
427   return '[' + Error.prototype.toString.call(value) + ']';
428 }
429
430
431 function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
432   return keys.map(function(key) {
433     return formatProperty(ctx, value, recurseTimes, visibleKeys, key, false);
434   });
435 }
436
437
438 function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
439   var output = [];
440   for (var i = 0, l = value.length; i < l; ++i) {
441     if (hasOwnProperty(value, String(i))) {
442       output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
443           String(i), true));
444     } else {
445       output.push('');
446     }
447   }
448   keys.forEach(function(key) {
449     if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
450       output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
451           key, true));
452     }
453   });
454   return output;
455 }
456
457
458 function formatSet(ctx, value, recurseTimes, visibleKeys, keys) {
459   var output = [];
460   value.forEach(function(v) {
461     var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
462     var str = formatValue(ctx, v, nextRecurseTimes);
463     output.push(str);
464   });
465   keys.forEach(function(key) {
466     output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
467                                key, false));
468   });
469   return output;
470 }
471
472
473 function formatMap(ctx, value, recurseTimes, visibleKeys, keys) {
474   var output = [];
475   value.forEach(function(v, k) {
476     var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
477     var str = formatValue(ctx, k, nextRecurseTimes);
478     str += ' => ';
479     str += formatValue(ctx, v, nextRecurseTimes);
480     output.push(str);
481   });
482   keys.forEach(function(key) {
483     output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
484                                key, false));
485   });
486   return output;
487 }
488
489 function formatPromise(ctx, value, recurseTimes, visibleKeys, keys) {
490   var output = [];
491   var internals = inspectPromise(value);
492   if (internals.status === 'pending') {
493     output.push('<pending>');
494   } else {
495     var nextRecurseTimes = recurseTimes === null ? null : recurseTimes - 1;
496     var str = formatValue(ctx, internals.value, nextRecurseTimes);
497     if (internals.status === 'rejected') {
498       output.push('<rejected> ' + str);
499     } else {
500       output.push(str);
501     }
502   }
503   keys.forEach(function(key) {
504     output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
505                                key, false));
506   });
507   return output;
508 }
509
510
511 function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
512   var name, str, desc;
513   desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
514   if (desc.get) {
515     if (desc.set) {
516       str = ctx.stylize('[Getter/Setter]', 'special');
517     } else {
518       str = ctx.stylize('[Getter]', 'special');
519     }
520   } else {
521     if (desc.set) {
522       str = ctx.stylize('[Setter]', 'special');
523     }
524   }
525   if (!hasOwnProperty(visibleKeys, key)) {
526     if (typeof key === 'symbol') {
527       name = '[' + ctx.stylize(key.toString(), 'symbol') + ']';
528     } else {
529       name = '[' + key + ']';
530     }
531   }
532   if (!str) {
533     if (ctx.seen.indexOf(desc.value) < 0) {
534       if (recurseTimes === null) {
535         str = formatValue(ctx, desc.value, null);
536       } else {
537         str = formatValue(ctx, desc.value, recurseTimes - 1);
538       }
539       if (str.indexOf('\n') > -1) {
540         if (array) {
541           str = str.split('\n').map(function(line) {
542             return '  ' + line;
543           }).join('\n').substr(2);
544         } else {
545           str = '\n' + str.split('\n').map(function(line) {
546             return '   ' + line;
547           }).join('\n');
548         }
549       }
550     } else {
551       str = ctx.stylize('[Circular]', 'special');
552     }
553   }
554   if (name === undefined) {
555     if (array && key.match(/^\d+$/)) {
556       return str;
557     }
558     name = JSON.stringify('' + key);
559     if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
560       name = name.substr(1, name.length - 2);
561       name = ctx.stylize(name, 'name');
562     } else {
563       name = name.replace(/'/g, "\\'")
564                  .replace(/\\"/g, '"')
565                  .replace(/(^"|"$)/g, "'")
566                  .replace(/\\\\/g, '\\');
567       name = ctx.stylize(name, 'string');
568     }
569   }
570
571   return name + ': ' + str;
572 }
573
574
575 function reduceToSingleString(output, base, braces) {
576   var length = output.reduce(function(prev, cur) {
577     return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
578   }, 0);
579
580   if (length > 60) {
581     return braces[0] +
582            // If the opening "brace" is too large, like in the case of "Set {",
583            // we need to force the first item to be on the next line or the
584            // items will not line up correctly.
585            (base === '' && braces[0].length === 1 ? '' : base + '\n ') +
586            ' ' +
587            output.join(',\n  ') +
588            ' ' +
589            braces[1];
590   }
591
592   return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
593 }
594
595
596 // NOTE: These type checking functions intentionally don't use `instanceof`
597 // because it is fragile and can be easily faked with `Object.create()`.
598 exports.isArray = Array.isArray;
599
600 function isBoolean(arg) {
601   return typeof arg === 'boolean';
602 }
603 exports.isBoolean = isBoolean;
604
605 function isNull(arg) {
606   return arg === null;
607 }
608 exports.isNull = isNull;
609
610 function isNullOrUndefined(arg) {
611   return arg === null || arg === undefined;
612 }
613 exports.isNullOrUndefined = isNullOrUndefined;
614
615 function isNumber(arg) {
616   return typeof arg === 'number';
617 }
618 exports.isNumber = isNumber;
619
620 function isString(arg) {
621   return typeof arg === 'string';
622 }
623 exports.isString = isString;
624
625 function isSymbol(arg) {
626   return typeof arg === 'symbol';
627 }
628 exports.isSymbol = isSymbol;
629
630 function isUndefined(arg) {
631   return arg === undefined;
632 }
633 exports.isUndefined = isUndefined;
634
635 function isRegExp(re) {
636   return re !== null && typeof re === 'object' &&
637          objectToString(re) === '[object RegExp]';
638 }
639 exports.isRegExp = isRegExp;
640
641 function isObject(arg) {
642   return arg !== null && typeof arg === 'object';
643 }
644 exports.isObject = isObject;
645
646 function isDate(d) {
647   return d !== null && typeof d === 'object' &&
648          objectToString(d) === '[object Date]';
649 }
650 exports.isDate = isDate;
651
652 function isError(e) {
653   return e !== null && typeof e === 'object' &&
654       (objectToString(e) === '[object Error]' || e instanceof Error);
655 }
656 exports.isError = isError;
657
658 function isFunction(arg) {
659   return typeof arg === 'function';
660 }
661 exports.isFunction = isFunction;
662
663 function isPrimitive(arg) {
664   return arg === null ||
665          typeof arg !== 'object' && typeof arg !== 'function';
666 }
667 exports.isPrimitive = isPrimitive;
668
669 function isBuffer(arg) {
670   return arg instanceof Buffer;
671 }
672 exports.isBuffer = isBuffer;
673
674 function objectToString(o) {
675   return Object.prototype.toString.call(o);
676 }
677
678
679 function pad(n) {
680   return n < 10 ? '0' + n.toString(10) : n.toString(10);
681 }
682
683
684 const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
685                 'Oct', 'Nov', 'Dec'];
686
687 // 26 Feb 16:19:34
688 function timestamp() {
689   var d = new Date();
690   var time = [pad(d.getHours()),
691               pad(d.getMinutes()),
692               pad(d.getSeconds())].join(':');
693   return [d.getDate(), months[d.getMonth()], time].join(' ');
694 }
695
696
697 // log is just a thin wrapper to console.log that prepends a timestamp
698 exports.log = function() {
699   console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
700 };
701
702
703 /**
704  * Inherit the prototype methods from one constructor into another.
705  *
706  * The Function.prototype.inherits from lang.js rewritten as a standalone
707  * function (not on Function.prototype). NOTE: If this file is to be loaded
708  * during bootstrapping this function needs to be rewritten using some native
709  * functions as prototype setup using normal JavaScript does not work as
710  * expected during bootstrapping (see mirror.js in r114903).
711  *
712  * @param {function} ctor Constructor function which needs to inherit the
713  *     prototype.
714  * @param {function} superCtor Constructor function to inherit prototype from.
715  * @throws {TypeError} Will error if either constructor is null, or if
716  *     the super constructor lacks a prototype.
717  */
718 exports.inherits = function(ctor, superCtor) {
719
720   if (ctor === undefined || ctor === null)
721     throw new TypeError('The constructor to `inherits` must not be ' +
722                         'null or undefined.');
723
724   if (superCtor === undefined || superCtor === null)
725     throw new TypeError('The super constructor to `inherits` must not ' +
726                         'be null or undefined.');
727
728   if (superCtor.prototype === undefined)
729     throw new TypeError('The super constructor to `inherits` must ' +
730                         'have a prototype.');
731
732   ctor.super_ = superCtor;
733   ctor.prototype = Object.create(superCtor.prototype, {
734     constructor: {
735       value: ctor,
736       enumerable: false,
737       writable: true,
738       configurable: true
739     }
740   });
741 };
742
743 exports._extend = function(origin, add) {
744   // Don't do anything if add isn't an object
745   if (add === null || typeof add !== 'object') return origin;
746
747   var keys = Object.keys(add);
748   var i = keys.length;
749   while (i--) {
750     origin[keys[i]] = add[keys[i]];
751   }
752   return origin;
753 };
754
755 function hasOwnProperty(obj, prop) {
756   return Object.prototype.hasOwnProperty.call(obj, prop);
757 }
758
759
760 // Deprecated old stuff.
761
762 exports.p = exports.deprecate(function() {
763   for (var i = 0, len = arguments.length; i < len; ++i) {
764     console.error(exports.inspect(arguments[i]));
765   }
766 }, 'util.p: Use console.error() instead');
767
768
769 exports.exec = exports.deprecate(function() {
770   return require('child_process').exec.apply(this, arguments);
771 }, 'util.exec is now called `child_process.exec`.');
772
773
774 exports.print = exports.deprecate(function() {
775   for (var i = 0, len = arguments.length; i < len; ++i) {
776     process.stdout.write(String(arguments[i]));
777   }
778 }, 'util.print: Use console.log instead');
779
780
781 exports.puts = exports.deprecate(function() {
782   for (var i = 0, len = arguments.length; i < len; ++i) {
783     process.stdout.write(arguments[i] + '\n');
784   }
785 }, 'util.puts: Use console.log instead');
786
787
788 exports.debug = exports.deprecate(function(x) {
789   process.stderr.write('DEBUG: ' + x + '\n');
790 }, 'util.debug: Use console.error instead');
791
792
793 exports.error = exports.deprecate(function(x) {
794   for (var i = 0, len = arguments.length; i < len; ++i) {
795     process.stderr.write(arguments[i] + '\n');
796   }
797 }, 'util.error: Use console.error instead');
798
799
800 exports.pump = exports.deprecate(function(readStream, writeStream, callback) {
801   var callbackCalled = false;
802
803   function call(a, b, c) {
804     if (callback && !callbackCalled) {
805       callback(a, b, c);
806       callbackCalled = true;
807     }
808   }
809
810   readStream.addListener('data', function(chunk) {
811     if (writeStream.write(chunk) === false) readStream.pause();
812   });
813
814   writeStream.addListener('drain', function() {
815     readStream.resume();
816   });
817
818   readStream.addListener('end', function() {
819     writeStream.end();
820   });
821
822   readStream.addListener('close', function() {
823     call();
824   });
825
826   readStream.addListener('error', function(err) {
827     writeStream.end();
828     call(err);
829   });
830
831   writeStream.addListener('error', function(err) {
832     readStream.destroy();
833     call(err);
834   });
835 }, 'util.pump(): Use readableStream.pipe() instead');
836
837
838 exports._errnoException = function(err, syscall, original) {
839   var errname = uv.errname(err);
840   var message = syscall + ' ' + errname;
841   if (original)
842     message += ' ' + original;
843   var e = new Error(message);
844   e.code = errname;
845   e.errno = errname;
846   e.syscall = syscall;
847   return e;
848 };
849
850
851 exports._exceptionWithHostPort = function(err,
852                                           syscall,
853                                           address,
854                                           port,
855                                           additional) {
856   var details;
857   if (port && port > 0) {
858     details = address + ':' + port;
859   } else {
860     details = address;
861   }
862
863   if (additional) {
864     details += ' - Local (' + additional + ')';
865   }
866   var ex = exports._errnoException(err, syscall, details);
867   ex.address = address;
868   if (port) {
869     ex.port = port;
870   }
871   return ex;
872 };