Fix Buffer.from warning in last node.js versions, close #154

This commit is contained in:
Vitaly Puzrin 2019-02-28 21:51:49 +03:00
parent 021f307fa3
commit 54c6ca95a1
6 changed files with 18 additions and 16 deletions

View file

@ -10,6 +10,7 @@ var util = require('util');
var Benchmark = require('benchmark');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
var b = require('buffer-from');
var pako = require('../');
@ -47,11 +48,11 @@ fs.readdirSync(SAMPLES_DIRECTORY).sort().forEach(function (sample) {
content.string = fs.readFileSync(filepath, 'utf8');
content.deflateTyped = pako.deflate(content.typed, { level: LEVEL });
content.deflateBuffer = new Buffer(content.deflateTyped);
content.deflateBuffer = b(content.deflateTyped);
content.deflateString = pako.deflate(content.typed, { level: LEVEL, to: 'string' });
content.deflateRawTyped = pako.deflateRaw(content.typed, { level: LEVEL });
content.deflateRawBuffer = new Buffer(content.deflateRawTyped);
content.deflateRawBuffer = b(content.deflateRawTyped);
var title = util.format('(%d bytes raw / ~%d bytes compressed)', content.typed.length, content.deflateTyped.length);

View file

@ -31,6 +31,7 @@
"ansi": "^0.3.1",
"benchmark": "^2.1.4",
"browserify": "^16.2.3",
"buffer-from": "^1.1.1",
"eslint": "^5.9.0",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",

View file

@ -12,6 +12,7 @@ var testSamples = helpers.testSamples;
var assert = require('assert');
var fs = require('fs');
var path = require('path');
var b = require('buffer-from');
@ -178,7 +179,7 @@ describe('Deflate RAW', function () {
describe('Deflate dictionary', function () {
it('trivial dictionary', function () {
var dict = new Buffer('abcdefghijklmnoprstuvwxyz');
var dict = b('abcdefghijklmnoprstuvwxyz');
testSamples(zlib.deflateSync, pako.deflate, samples, { dictionary: dict });
});
@ -189,18 +190,18 @@ describe('Deflate dictionary', function () {
});
it('handles multiple pushes', function () {
var dict = new Buffer('abcd');
var dict = b('abcd');
var deflate = new pako.Deflate({ dictionary: dict });
deflate.push(new Buffer('hello'), false);
deflate.push(new Buffer('hello'), false);
deflate.push(new Buffer(' world'), true);
deflate.push(b('hello'), false);
deflate.push(b('hello'), false);
deflate.push(b(' world'), true);
if (deflate.err) { throw new Error(deflate.err); }
var uncompressed = pako.inflate(new Buffer(deflate.result), { dictionary: dict });
var uncompressed = pako.inflate(b(deflate.result), { dictionary: dict });
if (!helpers.cmpBuf(new Buffer('hellohello world'), uncompressed)) {
if (!helpers.cmpBuf(b('hellohello world'), uncompressed)) {
throw new Error('Result not equal for p -> z');
}
});

View file

@ -4,6 +4,7 @@
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var b = require('buffer-from');
var pako_utils = require('../lib/utils/common');
var pako = require('../index');
@ -48,9 +49,6 @@ function cmpBuf(a, b) {
}
function toBuffer(src) { return Buffer.from ? Buffer.from(src) : new Buffer(src); }
// Helper to test deflate/inflate with different options.
// Use zlib streams, because it's the only way to define options.
//
@ -60,7 +58,7 @@ function testSingle(zlib_method, pako_method, data, options) {
// hack for testing negative windowBits
if (zlib_options.windowBits < 0) { zlib_options.windowBits = -zlib_options.windowBits; }
var zlib_result = zlib_method(toBuffer(data), zlib_options);
var zlib_result = zlib_method(b(data), zlib_options);
var pako_result = pako_method(data, options);
// One more hack: gzip header contains OS code, that can vary.

View file

@ -168,7 +168,7 @@ describe('Inflate RAW', function () {
describe('Inflate with dictionary', function () {
it('should throw on the wrong dictionary', function () {
// var zCompressed = helpers.deflateSync('world', { dictionary: new Buffer('hello') });
// var zCompressed = helpers.deflateSync('world', { dictionary: b('hello') });
var zCompressed = new Uint8Array([ 120, 187, 6, 44, 2, 21, 43, 207, 47, 202, 73, 1, 0, 6, 166, 2, 41 ]);
assert.throws(function () {

View file

@ -7,6 +7,7 @@
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var b = require('buffer-from');
var pako = require('../index');
var cmp = require('./helpers').cmpBuf;
@ -42,7 +43,7 @@ describe('Encode/Decode', function () {
// Create sample, that contains all types of utf8 (1-4byte) after conversion
var utf16sample = a2utf16([ 0x1f3b5, 'a', 0x266a, 0x35, 0xe800, 0x10ffff, 0x0fffff ]);
// use node Buffer internal conversion as "done right"
var utf8sample = new Uint8Array(new Buffer(utf16sample));
var utf8sample = new Uint8Array(b(utf16sample));
it('utf-8 border detect', function () {
var ub = strings.utf8border;
@ -105,7 +106,7 @@ describe('Deflate/Inflate strings', function () {
var data = pako.deflate(sampleArray, { to: 'string', chunkSize: 99 });
assert.equal(typeof data, 'string');
assert.ok(cmp(new Buffer(data, 'binary'), pako.deflate(sampleArray)));
assert.ok(cmp(b(data, 'binary'), pako.deflate(sampleArray)));
});
it('Inflate binary string input', function () {