Skip to content

Commit

Permalink
lib: explicit util.promisify() compat
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Jun 26, 2017
1 parent d5a252e commit 039ac52
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var stream = require('readable-stream');
var assert = require('assert');
var fs = require('fs');
var util = require('util');

// node-pre-gyp magic
var nodePreGyp = require('node-pre-gyp');
Expand Down Expand Up @@ -421,6 +422,11 @@ exports.decompress = function(string, opt, on_finish) {
return singleStringCoding(stream, string, on_finish);
};

if (util.promisify) {
exports.compress[util.promisify.custom] = exports.compress;
exports.decompress[util.promisify.custom] = exports.decompress;
}

exports.isXZ = function(buf) {
return buf && buf.length >= 6 &&
buf[0] === 0xfd &&
Expand Down
29 changes: 29 additions & 0 deletions test/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var assert = require('assert');
var fs = require('fs');
var util = require('util');
var helpers = require('./helpers.js');

var lzma = require('../');
Expand Down Expand Up @@ -140,3 +141,31 @@ describe('lzma.compress()/decompress() with ES6 Promises', function() {
});
});
});

describe('lzma.compress()/decompress() with util.promisify()', function() {
var majorVersion = process.version.match(/^v(\d+)\./);
if (majorVersion && +majorVersion[1] < 8) {
return;
}

var compress = util.promisify(lzma.compress);
var decompress = util.promisify(lzma.decompress);

it('can round-trip', function() {
return compress('Bananas', 5).then(function(result) {
assert.equal(result.toString('base64'), BananasCompressed);
return decompress(result);
}).then(function(result) {
assert.ok(Buffer.isBuffer(result));
assert.equal(result.toString(), 'Bananas');
});
});

it('fails for invalid input', function() {
return decompress('ABC').then(function(result) {
assert.ok(false); // never get here due to error
}).catch(function(err) {
assert.ok(err);
});
});
});

0 comments on commit 039ac52

Please sign in to comment.