Skip to content

Commit

Permalink
support callback
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed May 10, 2015
1 parent e27a054 commit db10b4f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion child_process.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require('thenify-all')(
require('thenify-all').withCallback(
require('child_process'),
exports, [
'exec',
Expand Down
2 changes: 1 addition & 1 deletion crypto.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require('thenify-all')(
require('thenify-all').withCallback(
require('crypto'),
exports, [
'pbkdf2',
Expand Down
2 changes: 1 addition & 1 deletion dns.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require('thenify-all')(
require('thenify-all').withCallback(
require('dns'),
exports, [
'lookup',
Expand Down
12 changes: 9 additions & 3 deletions fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions test/mz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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()
})
})
})
})
})
2 changes: 1 addition & 1 deletion zlib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require('thenify-all')(
require('thenify-all').withCallback(
require('zlib'),
exports, [
'deflate',
Expand Down

0 comments on commit db10b4f

Please sign in to comment.