Comparing version 0.10.4 to 0.11.0
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.10.4", | ||
"version": "0.11.0", | ||
"homepage": "https://github.com/defunctzombie/node-util", | ||
@@ -24,3 +24,4 @@ "repository": { | ||
"scripts": { | ||
"test": "node test/node/*.js && zuul test/browser/*.js" | ||
"test": "node test/node/index.js", | ||
"test:browsers": "airtap test/browser/index.js" | ||
}, | ||
@@ -32,3 +33,6 @@ "dependencies": { | ||
"devDependencies": { | ||
"zuul": "~1.0.9" | ||
"airtap": "0.0.6", | ||
"is-async-supported": "~1.2.0", | ||
"run-series": "~1.1.4", | ||
"tape": "~4.9.0" | ||
}, | ||
@@ -35,0 +39,0 @@ "browser": { |
@@ -1,9 +0,13 @@ | ||
# util | ||
# util [![Build Status](https://travis-ci.org/defunctzombie/node-util.png?branch=master)](https://travis-ci.org/defunctzombie/node-util) | ||
[![Build Status](https://travis-ci.org/defunctzombie/node-util.png?branch=master)](https://travis-ci.org/defunctzombie/node-util) | ||
> Node.js's [util][util] module for all engines. | ||
node.js [util](http://nodejs.org/api/util.html) module as a module | ||
This implements the Node.js [`util`][util] module for environments that do not have it, like browsers. | ||
## install via [npm](npmjs.org) | ||
## Install | ||
You usually do not have to install `util` yourself. If your code runs in Node.js, `util` is built in. If your code runs in the browser, bundlers like [browserify](https://github.com/browserify/browserify) or [webpack](https://github.com/webpack/webpack) also include the `util` module. | ||
But if none of those apply, with npm do: | ||
```shell | ||
@@ -13,4 +17,33 @@ npm install util | ||
## browser support | ||
## Usage | ||
This module also works in modern browsers. If you need legacy browser support you will need to polyfill ES5 features. | ||
```javascript | ||
var util = require('util') | ||
var EventEmitter = require('events') | ||
function MyClass() { EventEmitter.call(this) } | ||
util.inherits(MyClass, EventEmitter) | ||
``` | ||
## Browser Support | ||
The `util` module uses ES5 features. If you need to support very old browsers like IE8, use a shim like [`es5-shim`](https://www.npmjs.com/package/es5-shim). You need both the shim and the sham versions of `es5-shim`. | ||
To use `util.promisify` and `util.callbackify`, Promises must already be available. If you need to support browsers like IE11 that do not support Promises, use a shim. [es6-promise](https://github.com/stefanpenner/es6-promise) is a popular one but there are many others available on npm. | ||
## API | ||
See the [Node.js util docs][util]. `util` currently supports the Node 8 LTS API. However, some of the methods are outdated. The `inspect` and `format` methods included in this module are a lot more simple and barebones than the ones in Node.js. | ||
## Contributing | ||
PRs are very welcome! The main way to contribute to `util` is by porting features, bugfixes and tests from Node.js. Ideally, code contributions to this module are copy-pasted from Node.js and transpiled to ES5, rather than reimplemented from scratch. Matching the Node.js code as closely as possible makes maintenance simpler when new changes land in Node.js. | ||
This module intends to provide exactly the same API as Node.js, so features that are not available in the core `util` module will not be accepted. Feature requests should instead be directed at [nodejs/node](https://github.com/nodejs/node) and will be added to this module once they are implemented in Node.js. | ||
If there is a difference in behaviour between Node.js's `util` module and this module, please open an issue! | ||
## License | ||
[MIT](./LICENSE) | ||
[util]: https://nodejs.org/docs/latest-v8.x/api/util.html |
117
util.js
@@ -22,2 +22,12 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
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; | ||
}; | ||
var formatRegExp = /%[sdj%]/g; | ||
@@ -588,1 +598,108 @@ exports.format = function(f) { | ||
} | ||
var kCustomPromisifiedSymbol = typeof Symbol !== 'undefined' ? Symbol('util.promisify.custom') : undefined; | ||
exports.promisify = function promisify(original) { | ||
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; | ||
} | ||
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) | ||
); | ||
} | ||
exports.promisify.custom = kCustomPromisifiedSymbol | ||
function callbackifyOnRejected(reason, cb) { | ||
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M). | ||
// Because `null` is a special error value in callbacks which means "no error | ||
// occurred", we error-wrap so the callback consumer can distinguish between | ||
// "the promise rejected with null" or "the promise fulfilled with undefined". | ||
if (!reason) { | ||
var newReason = new Error('Promise was rejected with a falsy value'); | ||
newReason.reason = reason; | ||
reason = newReason; | ||
} | ||
return cb(reason); | ||
} | ||
function callbackify(original) { | ||
if (typeof original !== 'function') { | ||
throw new TypeError('The "original" argument must be of type Function'); | ||
} | ||
// We DO NOT return the promise as it gives the user a false sense that | ||
// the promise is actually somehow related to the callback's execution | ||
// and that the callback throwing will reject the promise. | ||
function callbackified() { | ||
var args = []; | ||
for (var i = 0; i < arguments.length; i++) { | ||
args.push(arguments[i]); | ||
} | ||
var maybeCb = args.pop(); | ||
if (typeof maybeCb !== 'function') { | ||
throw new TypeError('The last argument must be of type Function'); | ||
} | ||
var self = this; | ||
var cb = function() { | ||
return maybeCb.apply(self, arguments); | ||
}; | ||
// In true node style we process the callback on `nextTick` with all the | ||
// implications (stack, `uncaughtException`, `async_hooks`) | ||
original.apply(this, args) | ||
.then(function(ret) { process.nextTick(cb, null, ret) }, | ||
function(rej) { process.nextTick(callbackifyOnRejected, rej, cb) }); | ||
} | ||
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original)); | ||
Object.defineProperties(callbackified, | ||
getOwnPropertyDescriptors(original)); | ||
return callbackified; | ||
} | ||
exports.callbackify = callbackify; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24307
7
611
49
0
4