pax_global_header00006660000000000000000000000064131562706000014512gustar00rootroot0000000000000052 comment=f3d4c7933ab03ad60353bacd70663614ab4482e9 mz-2.7.0/000077500000000000000000000000001315627060000121465ustar00rootroot00000000000000mz-2.7.0/.gitignore000066400000000000000000000000561315627060000141370ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage mz-2.7.0/.travis.yml000066400000000000000000000002471315627060000142620ustar00rootroot00000000000000node_js: - 4 - 6 - 8 sudo: false language: node_js script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" mz-2.7.0/HISTORY.md000066400000000000000000000021611315627060000136310ustar00rootroot00000000000000 2.7.0 / 2017-09-13 ================== * feat: support fs.copyFile (#58) 2.6.0 / 2016-11-22 ================== * Added fdatasync to fs api (#46) 2.5.0 / 2016-11-04 ================== * feat: support fs.mkdtemp 2.4.0 / 2016-03-23 ================== * add `fs.truncate()` [#34](https://github.com/normalize/mz/pull/34) 2.3.1 / 2016-02-01 ================== * update `any-promise@v1` 2.3.0 / 2016-01-30 ================== * feat(package): switch to `any-promise` to support more promise engines 2.2.0 / 2016-01-24 ================== * feat(package): add index.js to files 2.1.0 / 2015-10-15 ================== * support for readline library 2.0.0 / 2015-05-24 ================== * support callbacks as well 1.2.0 / 2014-12-16 ================== * refactor promisification to `thenify` and `thenify-all` 1.1.0 / 2014-11-14 ================== * use `graceful-fs` if available 1.0.1 / 2014-08-18 ================== * don't use `bluebird.promisify()` - unnecessarily wraps runtime errors, causing issues 1.0.0 / 2014-06-18 ================== * use `bluebird` by default if found * support node 0.8 mz-2.7.0/LICENSE000066400000000000000000000021411315627060000131510ustar00rootroot00000000000000 The MIT License (MIT) Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mz-2.7.0/README.md000066400000000000000000000055271315627060000134360ustar00rootroot00000000000000 # MZ - Modernize node.js [![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependency Status][david-image]][david-url] [![License][license-image]][license-url] [![Downloads][downloads-image]][downloads-url] Modernize node.js to current ECMAScript specifications! node.js will not update their API to ES6+ [for a while](https://github.com/joyent/node/issues/7549). This library is a wrapper for various aspects of node.js' API. ## Installation and Usage Set `mz` as a dependency and install it. ```bash npm i mz ``` Then prefix the relevant `require()`s with `mz/`: ```js var fs = require('mz/fs') fs.exists(__filename).then(function (exists) { if (exists) // do something }) ``` With ES2017, this will allow you to use async functions cleanly with node's core API: ```js const fs = require('mz/fs') async function doSomething () { if (await fs.exists(__filename)) // do something } ``` ## Promisification Many node methods are converted into promises. Any properties that are deprecated or aren't asynchronous will simply be proxied. The modules wrapped are: - `child_process` - `crypto` - `dns` - `fs` (uses `graceful-fs` if available) - `readline` - `zlib` ```js var exec = require('mz/child_process').exec exec('node --version').then(function (stdout) { console.log(stdout) }) ``` ## Promise Engine `mz` uses [`any-promise`](https://github.com/kevinbeaty/any-promise). ## FAQ ### Can I use this in production? Yes, Node 4.x ships with stable promises support. For older engines, you should probably install your own promise implementation and register it with `require('any-promise/register')('bluebird')`. ### Will this make my app faster? Nope, probably slower actually. ### Can I add more features? Sure. Open an issue. Currently, the plans are to eventually support: - New APIs in node.js that are not available in older versions of node - ECMAScript7 Streams [bluebird]: https://github.com/petkaantonov/bluebird [npm-image]: https://img.shields.io/npm/v/mz.svg?style=flat-square [npm-url]: https://npmjs.org/package/mz [github-tag]: http://img.shields.io/github/tag/normalize/mz.svg?style=flat-square [github-url]: https://github.com/normalize/mz/tags [travis-image]: https://img.shields.io/travis/normalize/mz.svg?style=flat-square [travis-url]: https://travis-ci.org/normalize/mz [coveralls-image]: https://img.shields.io/coveralls/normalize/mz.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/normalize/mz?branch=master [david-image]: http://img.shields.io/david/normalize/mz.svg?style=flat-square [david-url]: https://david-dm.org/normalize/mz [license-image]: http://img.shields.io/npm/l/mz.svg?style=flat-square [license-url]: LICENSE [downloads-image]: http://img.shields.io/npm/dm/mz.svg?style=flat-square [downloads-url]: https://npmjs.org/package/mz mz-2.7.0/child_process.js000066400000000000000000000001611315627060000153230ustar00rootroot00000000000000 require('thenify-all').withCallback( require('child_process'), exports, [ 'exec', 'execFile', ] ) mz-2.7.0/crypto.js000066400000000000000000000002071315627060000140230ustar00rootroot00000000000000 require('thenify-all').withCallback( require('crypto'), exports, [ 'pbkdf2', 'pseudoRandomBytes', 'randomBytes' ] ) mz-2.7.0/dns.js000066400000000000000000000003601315627060000132670ustar00rootroot00000000000000 require('thenify-all').withCallback( require('dns'), exports, [ 'lookup', 'resolve', 'resolve4', 'resolve6', 'resolveCname', 'resolveMx', 'resolveNs', 'resolveSrv', 'resolveTxt', 'reverse' ] ) mz-2.7.0/fs.js000066400000000000000000000020241315627060000131120ustar00rootroot00000000000000 var Promise = require('any-promise') var fs try { fs = require('graceful-fs') } catch(err) { fs = require('fs') } var api = [ 'appendFile', 'chmod', 'chown', 'close', 'fchmod', 'fchown', 'fdatasync', 'fstat', 'fsync', 'ftruncate', 'futimes', 'lchown', 'link', 'lstat', 'mkdir', 'open', 'read', 'readFile', 'readdir', 'readlink', 'realpath', 'rename', 'rmdir', 'stat', 'symlink', 'truncate', 'unlink', 'utimes', 'write', 'writeFile' ] typeof fs.access === 'function' && api.push('access') typeof fs.copyFile === 'function' && api.push('copyFile') typeof fs.mkdtemp === 'function' && api.push('mkdtemp') require('thenify-all').withCallback(fs, exports, api) exports.exists = function (filename, callback) { // callback if (typeof callback === 'function') { return fs.stat(filename, function (err) { callback(null, !err); }) } // or promise return new Promise(function (resolve) { fs.stat(filename, function (err) { resolve(!err) }) }) } mz-2.7.0/index.js000066400000000000000000000003161315627060000136130ustar00rootroot00000000000000module.exports = { fs: require('./fs'), dns: require('./dns'), zlib: require('./zlib'), crypto: require('./crypto'), readline: require('./readline'), child_process: require('./child_process') } mz-2.7.0/package.json000066400000000000000000000017641315627060000144440ustar00rootroot00000000000000{ "name": "mz", "description": "modernize node.js to current ECMAScript standards", "version": "2.7.0", "author": { "name": "Jonathan Ong", "email": "me@jongleberry.com", "url": "http://jongleberry.com", "twitter": "https://twitter.com/jongleberry" }, "license": "MIT", "repository": "normalize/mz", "dependencies": { "any-promise": "^1.0.0", "object-assign": "^4.0.1", "thenify-all": "^1.0.0" }, "devDependencies": { "istanbul": "^0.4.0", "bluebird": "^3.0.0", "mocha": "^3.0.0" }, "scripts": { "test": "mocha --reporter spec", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot" }, "keywords": [ "promisify", "promise", "thenify", "then", "es6" ], "files": [ "index.js", "child_process.js", "crypto.js", "dns.js", "fs.js", "readline.js", "zlib.js" ] } mz-2.7.0/readline.js000066400000000000000000000032211315627060000142650ustar00rootroot00000000000000var readline = require('readline') var Promise = require('any-promise') var objectAssign = require('object-assign') var Interface = readline.Interface function wrapCompleter (completer) { if (completer.length === 2) return completer return function (line, cb) { var result = completer(line) if (typeof result.then !== 'function') { return cb(null, result) } result.catch(cb).then(function (result) { process.nextTick(function () { cb(null, result) }) }) } } function InterfaceAsPromised (input, output, completer, terminal) { if (arguments.length === 1) { var options = input if (typeof options.completer === 'function') { options = objectAssign({}, options, { completer: wrapCompleter(options.completer) }) } Interface.call(this, options) } else { if (typeof completer === 'function') { completer = wrapCompleter(completer) } Interface.call(this, input, output, completer, terminal) } } InterfaceAsPromised.prototype = Object.create(Interface.prototype) InterfaceAsPromised.prototype.question = function (question, callback) { if (typeof callback === 'function') { return Interface.prototype.question.call(this, question, callback) } var self = this return new Promise(function (resolve) { Interface.prototype.question.call(self, question, resolve) }) } objectAssign(exports, readline, { Interface: InterfaceAsPromised, createInterface: function (input, output, completer, terminal) { if (arguments.length === 1) { return new InterfaceAsPromised(input) } return new InterfaceAsPromised(input, output, completer, terminal) } }) mz-2.7.0/test/000077500000000000000000000000001315627060000131255ustar00rootroot00000000000000mz-2.7.0/test/mz.js000066400000000000000000000131361315627060000141150ustar00rootroot00000000000000 var assert = require('assert') describe('fs', function () { var fs = require('../fs') it('.stat()', function (done) { fs.stat(__filename).then(function (stats) { assert.equal(typeof stats.size, 'number') done() }).catch(done) }) it('.statSync()', function () { var stats = fs.statSync(__filename) assert.equal(typeof stats.size, 'number') }) it('.exists()', function (done) { fs.exists(__filename).then(function (exists) { assert(exists) done() }).catch(done) }) it('.existsSync()', function () { var exists = fs.existsSync(__filename) assert(exists) }) describe('callback support', function () { it('.stat()', function (done) { fs.stat(__filename, function (err, stats) { assert(!err) assert.equal(typeof stats.size, 'number') done() }) }) it('.exists()', function (done) { fs.exists(__filename, function (err, exists) { assert(!err) assert(exists) done() }) }) }) }) describe('child_process', function () { var cp = require('../child_process') it('.exec().then()', function (done) { cp.exec('node --version').then(function (stdout) { assert.equal(stdout.toString('utf8')[0], 'v') done() }) }) it('.exec().catch()', function (done) { cp.exec('lkajsdfkljalskdfjalsdf').catch(function (err) { done() }) }) describe('callback support', function () { it('.exec() success', function (done) { cp.exec('node --version', function (err, stdout) { assert.equal(stdout.toString('utf8')[0], 'v') done() }) }) it('.exec() err', function (done) { cp.exec('lkajsdfkljalskdfjalsdf', function (err) { assert(err) done() }) }) }) }) describe('crypto', function () { var crypto = require('../crypto') it('.randomBytes().then()', function (done) { crypto.randomBytes(8).then(function (buf) { assert.equal(buf.length, 8) done() }) }) describe('callback support', function () { it('.randomBytes()', function (done) { crypto.randomBytes(8, function (err, buf) { assert(!err) assert.equal(buf.length, 8) done() }) }) }) }) describe('readline', function () { var stream = require('stream') var readline = require('../readline') var Promise = require('any-promise') it('.question().then()', function (done) { var input = stream.PassThrough() var output = stream.PassThrough() var rl = readline.createInterface({ input: input, output: output }) rl.question('a').then(function (answer) { assert.equal(answer, 'b') done() }) assert.equal(output.read(), 'a') input.write('b\n') }) it('completer support', function (done) { function completer (line) { assert.equal(line, 'b') return Promise.resolve([['bTESTSTRING'], line]) } var input = stream.PassThrough() var output = stream.PassThrough() var bufferedOutput = '' var rl = readline.createInterface({ input: input, output: output, completer: completer, terminal: true }) rl.question('a').then(function (answer) { assert.equal(answer, 'bTESTSTRING') done() }) function onOutputData (data) { bufferedOutput += data.toString() if (bufferedOutput.match(/TESTSTRING/)) { input.write('\n') output.removeListener('data', onOutputData) } } output.on('data', onOutputData) input.write('b\t') }) describe('callback support', function () { it('.question()', function (done) { var input = stream.PassThrough() var output = stream.PassThrough() var rl = readline.createInterface({ input: input, output: output }) rl.question('a', function (answer) { assert.equal(answer, 'b') done() }) assert.equal(output.read(), 'a') input.write('b\n') }) it('completer support sync', function (done) { function completer (line) { assert.equal(line, 'b') return [['bTESTSTRING'], line] } var input = stream.PassThrough() var output = stream.PassThrough() var rl = readline.createInterface({ input: input, output: output, completer: completer, terminal: true }) rl.question('a').then(function (answer) { assert.ok(output.read().toString().match(/TESTSTRING/)) done() }) input.write('b\t') input.write('\n') }) it('completer support async', function (done) { function completer (line, cb) { assert.equal(line, 'b') cb(null, [['bTESTSTRING'], line]) } var input = stream.PassThrough() var output = stream.PassThrough() var rl = readline.createInterface({ input: input, output: output, completer: completer, terminal: true }) rl.question('a').then(function (answer) { assert.ok(output.read().toString().match(/TESTSTRING/)) done() }) input.write('b\t') input.write('\n') }) }) }) describe('zlib', function () { var zlib = require('../zlib') it('.gzip().then().gunzip()', function (done) { zlib.gzip('lol').then(function (res) { return zlib.gunzip(res) }).then(function (string) { assert.equal(string, 'lol') done() }) }) describe('callback support', function () { it('.gzip() and .gunzip()', function (done) { zlib.gzip('lol', function (err, res) { assert(!err) assert(Buffer.isBuffer(res)) zlib.gunzip(res, function (err, string) { assert(!err) assert.equal(string, 'lol') done() }) }) }) }) }) mz-2.7.0/zlib.js000066400000000000000000000002651315627060000134470ustar00rootroot00000000000000 require('thenify-all').withCallback( require('zlib'), exports, [ 'deflate', 'deflateRaw', 'gzip', 'gunzip', 'inflate', 'inflateRaw', 'unzip', ] )