| 1 |
|
|---|
| 2 | # MZ - Modernize node.js
|
|---|
| 3 |
|
|---|
| 4 | [![NPM version][npm-image]][npm-url]
|
|---|
| 5 | [![Build status][travis-image]][travis-url]
|
|---|
| 6 | [![Test coverage][coveralls-image]][coveralls-url]
|
|---|
| 7 | [![Dependency Status][david-image]][david-url]
|
|---|
| 8 | [![License][license-image]][license-url]
|
|---|
| 9 | [![Downloads][downloads-image]][downloads-url]
|
|---|
| 10 |
|
|---|
| 11 | Modernize node.js to current ECMAScript specifications!
|
|---|
| 12 | node.js will not update their API to ES6+ [for a while](https://github.com/joyent/node/issues/7549).
|
|---|
| 13 | This library is a wrapper for various aspects of node.js' API.
|
|---|
| 14 |
|
|---|
| 15 | ## Installation and Usage
|
|---|
| 16 |
|
|---|
| 17 | Set `mz` as a dependency and install it.
|
|---|
| 18 |
|
|---|
| 19 | ```bash
|
|---|
| 20 | npm i mz
|
|---|
| 21 | ```
|
|---|
| 22 |
|
|---|
| 23 | Then prefix the relevant `require()`s with `mz/`:
|
|---|
| 24 |
|
|---|
| 25 | ```js
|
|---|
| 26 | var fs = require('mz/fs')
|
|---|
| 27 |
|
|---|
| 28 | fs.exists(__filename).then(function (exists) {
|
|---|
| 29 | if (exists) // do something
|
|---|
| 30 | })
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | With ES2017, this will allow you to use async functions cleanly with node's core API:
|
|---|
| 34 |
|
|---|
| 35 | ```js
|
|---|
| 36 | const fs = require('mz/fs')
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | async function doSomething () {
|
|---|
| 40 | if (await fs.exists(__filename)) // do something
|
|---|
| 41 | }
|
|---|
| 42 | ```
|
|---|
| 43 |
|
|---|
| 44 | ## Promisification
|
|---|
| 45 |
|
|---|
| 46 | Many node methods are converted into promises.
|
|---|
| 47 | Any properties that are deprecated or aren't asynchronous will simply be proxied.
|
|---|
| 48 | The modules wrapped are:
|
|---|
| 49 |
|
|---|
| 50 | - `child_process`
|
|---|
| 51 | - `crypto`
|
|---|
| 52 | - `dns`
|
|---|
| 53 | - `fs` (uses `graceful-fs` if available)
|
|---|
| 54 | - `readline`
|
|---|
| 55 | - `zlib`
|
|---|
| 56 |
|
|---|
| 57 | ```js
|
|---|
| 58 | var exec = require('mz/child_process').exec
|
|---|
| 59 |
|
|---|
| 60 | exec('node --version').then(function (stdout) {
|
|---|
| 61 | console.log(stdout)
|
|---|
| 62 | })
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | ## Promise Engine
|
|---|
| 66 |
|
|---|
| 67 | `mz` uses [`any-promise`](https://github.com/kevinbeaty/any-promise).
|
|---|
| 68 |
|
|---|
| 69 | ## FAQ
|
|---|
| 70 |
|
|---|
| 71 | ### Can I use this in production?
|
|---|
| 72 |
|
|---|
| 73 | Yes, Node 4.x ships with stable promises support. For older engines,
|
|---|
| 74 | you should probably install your own promise implementation and register it with
|
|---|
| 75 | `require('any-promise/register')('bluebird')`.
|
|---|
| 76 |
|
|---|
| 77 | ### Will this make my app faster?
|
|---|
| 78 |
|
|---|
| 79 | Nope, probably slower actually.
|
|---|
| 80 |
|
|---|
| 81 | ### Can I add more features?
|
|---|
| 82 |
|
|---|
| 83 | Sure.
|
|---|
| 84 | Open an issue.
|
|---|
| 85 |
|
|---|
| 86 | Currently, the plans are to eventually support:
|
|---|
| 87 |
|
|---|
| 88 | - New APIs in node.js that are not available in older versions of node
|
|---|
| 89 | - ECMAScript7 Streams
|
|---|
| 90 |
|
|---|
| 91 | [bluebird]: https://github.com/petkaantonov/bluebird
|
|---|
| 92 |
|
|---|
| 93 | [npm-image]: https://img.shields.io/npm/v/mz.svg?style=flat-square
|
|---|
| 94 | [npm-url]: https://npmjs.org/package/mz
|
|---|
| 95 | [github-tag]: http://img.shields.io/github/tag/normalize/mz.svg?style=flat-square
|
|---|
| 96 | [github-url]: https://github.com/normalize/mz/tags
|
|---|
| 97 | [travis-image]: https://img.shields.io/travis/normalize/mz.svg?style=flat-square
|
|---|
| 98 | [travis-url]: https://travis-ci.org/normalize/mz
|
|---|
| 99 | [coveralls-image]: https://img.shields.io/coveralls/normalize/mz.svg?style=flat-square
|
|---|
| 100 | [coveralls-url]: https://coveralls.io/r/normalize/mz?branch=master
|
|---|
| 101 | [david-image]: http://img.shields.io/david/normalize/mz.svg?style=flat-square
|
|---|
| 102 | [david-url]: https://david-dm.org/normalize/mz
|
|---|
| 103 | [license-image]: http://img.shields.io/npm/l/mz.svg?style=flat-square
|
|---|
| 104 | [license-url]: LICENSE
|
|---|
| 105 | [downloads-image]: http://img.shields.io/npm/dm/mz.svg?style=flat-square
|
|---|
| 106 | [downloads-url]: https://npmjs.org/package/mz
|
|---|