From 46f6ca7e87b24badc3155a850446edcc014d4b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=9B=9B?= Date: Sun, 10 May 2015 11:39:21 +0800 Subject: [PATCH] support callback --- child_process.js | 2 +- crypto.js | 2 +- dns.js | 2 +- fs.js | 12 +++++++--- test/mz.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ zlib.js | 2 +- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/child_process.js b/child_process.js index f0feff6..06d5d9e 100644 --- a/child_process.js +++ b/child_process.js @@ -1,5 +1,5 @@ -require('thenify-all')( +require('thenify-all').withCallback( require('child_process'), exports, [ 'exec', diff --git a/crypto.js b/crypto.js index 683ae4f..60f579c 100644 --- a/crypto.js +++ b/crypto.js @@ -1,5 +1,5 @@ -require('thenify-all')( +require('thenify-all').withCallback( require('crypto'), exports, [ 'pbkdf2', diff --git a/dns.js b/dns.js index 9e139f8..3a52fc1 100644 --- a/dns.js +++ b/dns.js @@ -1,5 +1,5 @@ -require('thenify-all')( +require('thenify-all').withCallback( require('dns'), exports, [ 'lookup', diff --git a/fs.js b/fs.js index f04655b..270948b 100644 --- a/fs.js +++ b/fs.js @@ -40,10 +40,16 @@ var api = [ typeof fs.access === 'function' && api.push('access') -require('thenify-all')(fs, exports, api) +require('thenify-all').withCallback(fs, exports, api) -// don't know enough about promises to do this haha -exports.exists = function (filename) { +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) diff --git a/test/mz.js b/test/mz.js index 1f9a6bb..d50db00 100644 --- a/test/mz.js +++ b/test/mz.js @@ -27,6 +27,24 @@ describe('fs', 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 () { @@ -44,6 +62,22 @@ describe('child_process', function () { 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 () { @@ -55,6 +89,16 @@ describe('crypto', function () { done() }) }) + + describe('callback support', function () { + it('.randomBytes()', function (done) { + crypto.randomBytes(8, function (err, buf) { + assert(!err) + assert.equal(buf.length, 8) + done() + }) + }) + }) }) describe('zlib', function () { @@ -68,4 +112,18 @@ describe('zlib', function () { 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() + }) + }) + }) + }) }) diff --git a/zlib.js b/zlib.js index d114d66..a05c26a 100644 --- a/zlib.js +++ b/zlib.js @@ -1,5 +1,5 @@ -require('thenify-all')( +require('thenify-all').withCallback( require('zlib'), exports, [ 'deflate',