Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback support #15

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 19 additions & 13 deletions fs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

var fs;
var Promise = require('native-or-bluebird')
var fs
try {
fs = require('graceful-fs');
fs = require('graceful-fs')
} catch(err) {
fs = require('fs');
fs = require('fs')
}

var api = [
Expand Down Expand Up @@ -37,16 +38,21 @@ var api = [
'appendFile',
]

var access = fs.access
typeof access === 'function' && api.push('access')
typeof fs.access === 'function' && api.push('access')

require('thenify-all')(fs, exports, api)
require('thenify-all').withCallback(fs, exports, api)

var promisify = require('thenify')

// don't know enough about promises to do this haha
exports.exists = promisify(function exists(filename, done) {
fs.stat(filename, function (err) {
done(null, !err)
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)
})
})
})
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"repository": "normalize/mz",
"dependencies": {
"native-or-bluebird": "1",
"thenify": "3",
"thenify-all": "1"
},
"devDependencies": {
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