diff --git a/README.md b/README.md index 1998ea1..0af917c 100644 --- a/README.md +++ b/README.md @@ -381,18 +381,18 @@ The [compatibility mode](https://github.com/kawanet/msgpack-lite/issues/22) resp ```js // default mode handles both str and bin formats individually msgpack.encode("Aa"); // => (str format) -msgpack.encode(new Buffer([0x41, 0x61])); // => (bin format) +msgpack.encode(Buffer.from([0x41, 0x61])); // => (bin format) -msgpack.decode(new Buffer([0xa2, 0x41, 0x61])); // => 'Aa' (String) -msgpack.decode(new Buffer([0xc4, 0x02, 0x41, 0x61])); // => (Buffer) +msgpack.decode(Buffer.from([0xa2, 0x41, 0x61])); // => 'Aa' (String) +msgpack.decode(Buffer.from([0xc4, 0x02, 0x41, 0x61])); // => (Buffer) // compatibility mode handles only raw format both for String and Buffer var options = {codec: msgpack.createCodec({useraw: true})}; msgpack.encode("Aa", options); // => (raw format) -msgpack.encode(new Buffer([0x41, 0x61]), options); // => (raw format) +msgpack.encode(Buffer.from([0x41, 0x61]), options); // => (raw format) -msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options); // => (Buffer) -msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String) +msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options); // => (Buffer) +msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String) ``` ### Repository diff --git a/test/10.encode.js b/test/10.encode.js index cc1bfbb..b3f6aab 100755 --- a/test/10.encode.js +++ b/test/10.encode.js @@ -101,15 +101,15 @@ function run_tests(codecopt) { it("c4-c6: bin 8/16/32", function() { this.timeout(30000); var bin; - bin = Buffer(1); + bin = Buffer.alloc(1); bin.fill(0); assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc4, 1], bin)); - bin = Buffer(256); + bin = Buffer.alloc(256); bin.fill(0); assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc5, 1, 0], bin)); - bin = Buffer(65536); + bin = Buffer.alloc(65536); bin.fill(0); assert.deepEqual(toArray(msgpack.encode(bin, options)), concat([0xc6, 0, 1, 0, 0], bin)); }); @@ -149,16 +149,16 @@ function run_tests(codecopt) { for (var i = 0; i < 17; i++) src += src; str = src.substr(0, 0xFF); - assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xd9, 0xFF], Buffer(str))); + assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xd9, 0xFF], Buffer.from(str))); str = src.substr(0, 0x0100); - assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0x01, 0x00], Buffer(str))); + assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0x01, 0x00], Buffer.from(str))); str = src.substr(0, 0xFFFF); - assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0xFF, 0xFF], Buffer(str))); + assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xda, 0xFF, 0xFF], Buffer.from(str))); str = src.substr(0, 0x010000); - assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer(str))); + assert.deepEqual(toArray(msgpack.encode(str, options)), concat([0xdb, 0x00, 0x01, 0x00, 0x00], Buffer.from(str))); }); // array 16 -- 0xdc diff --git a/test/11.decode.js b/test/11.decode.js index 4342044..9a6edc6 100755 --- a/test/11.decode.js +++ b/test/11.decode.js @@ -13,7 +13,7 @@ Uint8ArrayBridge.concat = Uint8ArrayBridge_concat; describe(TITLE, function() { describe("Buffer", function() { - run_tests(Buffer); + run_tests(BufferBridge); }); describe("Array", function() { @@ -153,12 +153,12 @@ function run_tests(BUFFER) { it("ca-cb: float 32/64", function() { var buf; - buf = Buffer(5); + buf = Buffer.alloc(5); buf.writeUInt8(0xCA, 0); buf.writeFloatBE(0.5, 1); assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5); - buf = Buffer(9); + buf = Buffer.alloc(9); buf.writeUInt8(0xCB, 0); buf.writeDoubleBE(0.5, 1); assert.deepEqual(msgpack.decode(BUFFER(buf)), 0.5); @@ -315,6 +315,18 @@ function run_tests(BUFFER) { }); } +function BufferBridge(array) { + if ("number" === typeof array) { + array = Buffer.alloc(array); + } else { + array = Buffer.from(array); + } + + return array; +} + +BufferBridge.concat = Buffer.concat; + function ArrayBridge(array) { if ("number" === typeof array) { array = init_seq([], array); diff --git a/test/14.codec.js b/test/14.codec.js index 7ee8f38..af7fe83 100755 --- a/test/14.codec.js +++ b/test/14.codec.js @@ -21,7 +21,7 @@ describe(TITLE, function() { function test(type) { // fixext 1 -- 0xd4 - var source = new Buffer([0xd4, type, type]); + var source = Buffer.from([0xd4, type, type]); var decoded = msgpack.decode(source, options); assert.equal(decoded.type, type); assert.equal(decoded.buffer.length, 1); @@ -82,7 +82,7 @@ function MyClass(value) { } function myClassPacker(obj) { - return new Buffer([obj.value]); + return Buffer.from([obj.value]); } function myClassUnpacker(buffer) { diff --git a/test/15.useraw.js b/test/15.useraw.js index 1eb33e3..c9e445e 100755 --- a/test/15.useraw.js +++ b/test/15.useraw.js @@ -19,10 +19,10 @@ describe(TITLE, function() { options = {codec: msgpack.createCodec({useraw: true})}; // raw - assert.deepEqual(toArray(msgpack.decode(new Buffer([0xa1, 65]), options)), [65]); + assert.deepEqual(toArray(msgpack.decode(Buffer.from([0xa1, 65]), options)), [65]); // str - assert.equal(msgpack.decode(new Buffer([0xa1, 65])), "A"); + assert.equal(msgpack.decode(Buffer.from([0xa1, 65])), "A"); }); it("useraw (encode)", function() { @@ -30,7 +30,7 @@ describe(TITLE, function() { assert.deepEqual(toArray(msgpack.encode("A", options)), [0xa1, 65]); // raw (Buffer) - assert.deepEqual(toArray(msgpack.encode(new Buffer([65]), options)), [0xa1, 65]); + assert.deepEqual(toArray(msgpack.encode(Buffer.from([65]), options)), [0xa1, 65]); // str assert.deepEqual(toArray(msgpack.encode("A")), [0xa1, 65]); @@ -67,7 +67,7 @@ describe(TITLE, function() { TESTS.forEach(test); function test(length) { - var source = new Buffer(length); + var source = Buffer.alloc(length); for (var i = 0; i < length; i++) { source[i] = 65; // "A" } diff --git a/test/16.binarraybuffer.js b/test/16.binarraybuffer.js index 4759f03..cdc78e5 100755 --- a/test/16.binarraybuffer.js +++ b/test/16.binarraybuffer.js @@ -25,7 +25,7 @@ describeSkip(TITLE, function() { options = {codec: msgpack.createCodec({binarraybuffer: true, preset: true})}; // bin (Buffer) - decoded = msgpack.decode(new Buffer([0xc4, 2, 65, 66]), options); + decoded = msgpack.decode(Buffer.from([0xc4, 2, 65, 66]), options); assert.ok(decoded instanceof ArrayBuffer); assert.ok(!Buffer.isBuffer(decoded)); assert.deepEqual(toArray(decoded), [65, 66]); @@ -73,12 +73,12 @@ describeSkip(TITLE, function() { // addExtPacker() and getExtPacker() methods need a valid constructor name. // IE10 and iOS7 Safari may give another constructor name than Buffer. // At those cases, below will be encoded as Uint8Array: [0xd5, 0x12, 97, 98] - var b = new Buffer(1); + var b = Buffer.alloc(1); var c = b.constructor; var d = (c && c.name === "Buffer") ? it : it.skip; d("encode Buffer ext format 0x1B", function() { // fixext 2 (Buffer) - var encoded = msgpack.encode(new Buffer([97, 98]), options); + var encoded = msgpack.encode(Buffer.from([97, 98]), options); assert.deepEqual(toArray(encoded), [0xd5, 0x1b, 97, 98]); }); diff --git a/test/17.uint8array.js b/test/17.uint8array.js index a999c03..423643d 100755 --- a/test/17.uint8array.js +++ b/test/17.uint8array.js @@ -31,7 +31,7 @@ describe(TITLE, function() { assert.ok(!Buffer.isBuffer(encoded)); // bigger data - var big = new Buffer(8192); // 8KB + var big = Buffer.alloc(8192); // 8KB big[big.length - 1] = 99; var source = [big, big, big, big, big, big, big, big]; // 64KB encoded = msgpack.encode(source, options); diff --git a/test/20.roundtrip.js b/test/20.roundtrip.js index b26dd82..909329d 100755 --- a/test/20.roundtrip.js +++ b/test/20.roundtrip.js @@ -207,7 +207,7 @@ function run_tests(codecopt) { it("buffer", function() { this.timeout(30000); pattern(2, 65537).forEach(function(length, idx) { - var value = new Buffer(length); + var value = Buffer.alloc(length); value.fill(idx); assert.equal(value.length, length); var encoded = msgpack.encode(value, options); diff --git a/test/23.extbuffer.js b/test/23.extbuffer.js index 80b1c32..983b762 100755 --- a/test/23.extbuffer.js +++ b/test/23.extbuffer.js @@ -29,8 +29,8 @@ describe(TITLE, function() { function testExtBuffer(type) { // fixext 8 -- 0xd7 - var header = new Buffer([0xd7, type]); - var content = new Buffer(8); + var header = Buffer.from([0xd7, type]); + var content = Buffer.alloc(8); for (var i = 0; i < 8; i++) { content[i] = (type + i) & 0x7F; } @@ -49,14 +49,14 @@ describe(TITLE, function() { function testExtBufferArray(type) { function content(j) { var x = j * type; - return Buffer([x & 0x7F, (x + 1) & 0x7F]); + return Buffer.from([x & 0x7F, (x + 1) & 0x7F]); } // fixarray len 10 - var arrayHeader = new Buffer([0x9a]); + var arrayHeader = Buffer.from([0x9a]); var fullBuffer = arrayHeader; for (var j = 0; j < 10; j++) { // fixext 2 -- 0xd5 - var header = new Buffer([0xd5, type]); + var header = Buffer.from([0xd5, type]); fullBuffer = Buffer.concat([fullBuffer, header, content(j)]); } var decoded = msgpack.decode(fullBuffer); diff --git a/test/30.stream.js b/test/30.stream.js index 3988990..3407e5b 100755 --- a/test/30.stream.js +++ b/test/30.stream.js @@ -108,7 +108,7 @@ describe(TITLE, function() { // write a single byte into the decode stream function each(x) { - decoder.write(Buffer([x])); + decoder.write(Buffer.from([x])); } function onData(data) { diff --git a/test/61.encode-only.js b/test/61.encode-only.js index 296849f..1078352 100755 --- a/test/61.encode-only.js +++ b/test/61.encode-only.js @@ -15,7 +15,7 @@ describe(TITLE, function() { assert.deepEqual(toArray(encode("a")), [161, 97]); // ExtBuffer - var ext = new ExtBuffer(new Buffer([1]), 127); + var ext = new ExtBuffer(Buffer.from([1]), 127); assert.ok(ext instanceof ExtBuffer); assert.deepEqual(toArray(encode(ext)), [212, 127, 1]); }); diff --git a/test/62.decode-only.js b/test/62.decode-only.js index edc1096..057e1a5 100755 --- a/test/62.decode-only.js +++ b/test/62.decode-only.js @@ -15,7 +15,7 @@ describe(TITLE, function() { assert.equal(decode([161, 97]), "a"); // ExtBuffer - var ext = decode(new Buffer([212, 127, 1])); + var ext = decode(Buffer.from([212, 127, 1])); assert.ok(ext instanceof ExtBuffer); assert.equal(ext.buffer[0], 1); assert.equal(ext.type, 127);