Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added promisify to util polyfill #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
155 changes: 114 additions & 41 deletions polyfills/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function format(f) {
var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
var str = String(f).replace(formatRegExp, function (x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
Expand Down Expand Up @@ -65,7 +65,7 @@ export function format(f) {
export function deprecate(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (isUndefined(global.process)) {
return function() {
return function () {
return deprecate(fn, msg).apply(this, arguments);
};
}
Expand Down Expand Up @@ -102,12 +102,12 @@ export function debuglog(set) {
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
var pid = 0;
debugs[set] = function() {
debugs[set] = function () {
var msg = format.apply(null, arguments);
console.error('%s %d: %s', set, pid, msg);
};
} else {
debugs[set] = function() {};
debugs[set] = function () { };
}
}
return debugs[set];
Expand Down Expand Up @@ -149,19 +149,19 @@ export function inspect(obj, opts) {

// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39]
'bold': [1, 22],
'italic': [3, 23],
'underline': [4, 24],
'inverse': [7, 27],
'white': [37, 39],
'grey': [90, 39],
'black': [30, 39],
'blue': [34, 39],
'cyan': [36, 39],
'green': [32, 39],
'magenta': [35, 39],
'red': [31, 39],
'yellow': [33, 39]
};

// Don't use 'blue' not visible on cmd.exe
Expand All @@ -183,7 +183,7 @@ function stylizeWithColor(str, styleType) {

if (style) {
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
Expand All @@ -198,7 +198,7 @@ function stylizeNoColor(str, styleType) {
function arrayToHash(array) {
var hash = {};

array.forEach(function(val, idx) {
array.forEach(function (val, idx) {
hash[val] = true;
});

Expand All @@ -210,12 +210,12 @@ function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
var ret = value.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
Expand All @@ -240,7 +240,7 @@ function formatValue(ctx, value, recurseTimes) {
// IE doesn't make error fields non-enumerable
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
if (isError(value)
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
return formatError(value);
}

Expand Down Expand Up @@ -308,7 +308,7 @@ function formatValue(ctx, value, recurseTimes) {
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else {
output = keys.map(function(key) {
output = keys.map(function (key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
});
}
Expand All @@ -324,8 +324,8 @@ function formatPrimitive(ctx, value) {
return ctx.stylize('undefined', 'undefined');
if (isString(value)) {
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
return ctx.stylize(simple, 'string');
}
if (isNumber(value))
Expand Down Expand Up @@ -362,6 +362,17 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
return output;
}

var getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors ||
function getOwnPropertyDescriptors(obj) {
var keys = Object.keys(obj);
var descriptors = {};
for (var i = 0; i < keys.length; i++) {
descriptors[keys[i]] = Object.getOwnPropertyDescriptor(obj, keys[i]);
}
return descriptors;
};



function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
var name, str, desc;
Expand Down Expand Up @@ -443,6 +454,67 @@ function reduceToSingleString(output, base, braces) {
}


var kCustomPromisifiedSymbol = typeof Symbol !== 'undefined' ? Symbol('util.promisify.custom') : undefined;

function promisify(original) {
// Lazy-load to avoid a circular dependency.
if (typeof original !== 'function')
throw new TypeError('The "original" argument must be of type Function');

if (kCustomPromisifiedSymbol && original[kCustomPromisifiedSymbol]) {
var fn = original[kCustomPromisifiedSymbol];
if (typeof fn !== 'function') {
throw new TypeError('The "util.promisify.custom" argument must be of type Function');
}
Object.defineProperty(fn, kCustomPromisifiedSymbol, {
value: fn, enumerable: false, writable: false, configurable: true
});
return fn;
}

// Names to create an object from in case the callback receives multiple
// arguments, e.g. ['bytesRead', 'buffer'] for fs.read.
function fn() {
var promiseResolve, promiseReject;
var promise = new Promise(function (resolve, reject) {
promiseResolve = resolve;
promiseReject = reject;
});

var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
args.push(function (err, value) {
if (err) {
promiseReject(err);
} else {
promiseResolve(value);
}
});

try {
original.apply(this, args);
} catch (err) {
promiseReject(err);
}

return promise;
}

Object.setPrototypeOf(fn, Object.getPrototypeOf(original));

if (kCustomPromisifiedSymbol) Object.defineProperty(fn, kCustomPromisifiedSymbol, {
value: fn, enumerable: false, writable: false, configurable: true
});
return Object.defineProperties(
fn,
getOwnPropertyDescriptors(original)
);
}

promisify.custom = kCustomPromisifiedSymbol

// NOTE: These type checking functions intentionally don't use `instanceof`
// because it is fragile and can be easily faked with `Object.create()`.
export function isArray(ar) {
Expand Down Expand Up @@ -491,7 +563,7 @@ export function isDate(d) {

export function isError(e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error);
(objectToString(e) === '[object Error]' || e instanceof Error);
}

export function isFunction(arg) {
Expand All @@ -500,11 +572,11 @@ export function isFunction(arg) {

export function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
}

export function isBuffer(maybeBuf) {
Expand All @@ -522,14 +594,14 @@ function pad(n) {


var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
'Oct', 'Nov', 'Dec'];

// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
}

Expand All @@ -554,7 +626,7 @@ export function log() {
* @param {function} superCtor Constructor function to inherit prototype from.
*/
import inherits from './inherits';
export {inherits}
export { inherits }

export function _extend(origin, add) {
// Don't do anything if add isn't an object
Expand Down Expand Up @@ -594,5 +666,6 @@ export default {
inspect: inspect,
deprecate: deprecate,
format: format,
debuglog: debuglog
}
debuglog: debuglog,
promisify: promisify
}