Skip to content

Commit

Permalink
re kawanet#98: new Buffer() is deprecated
Browse files Browse the repository at this point in the history
(cherry picked from commit 3b66536)
  • Loading branch information
kawanet authored and p2k committed Jun 13, 2023
1 parent 7133f9c commit fe6294b
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 35 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,18 @@ The compatibility mode respects for [msgpack's old spec](https://github.com/msgp
```js
// default mode handles both str and bin formats individually
msgpack.encode("Aa"); // => <Buffer a2 41 61> (str format)
msgpack.encode(new Buffer([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)
msgpack.encode(Buffer.from([0x41, 0x61])); // => <Buffer c4 02 41 61> (bin format)

msgpack.decode(new Buffer([0xa2, 0x41, 0x61])); // => 'Aa' (String)
msgpack.decode(new Buffer([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61])); // => 'Aa' (String)
msgpack.decode(Buffer.from([0xc4, 0x02, 0x41, 0x61])); // => <Buffer 41 61> (Buffer)

// compatibility mode handles only raw format both for String and Buffer
var options = {codec: msgpack.createCodec({useraw: true})};
msgpack.encode("Aa", options); // => <Buffer a2 41 61> (raw format)
msgpack.encode(new Buffer([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)
msgpack.encode(Buffer.from([0x41, 0x61]), options); // => <Buffer a2 41 61> (raw format)

msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
msgpack.decode(new Buffer([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options); // => <Buffer 41 61> (Buffer)
msgpack.decode(Buffer.from([0xa2, 0x41, 0x61]), options).toString(); // => 'Aa' (String)
```

### Repository
Expand Down
14 changes: 7 additions & 7 deletions test/10.encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions test/11.decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Uint8ArrayBridge.concat = Uint8ArrayBridge_concat;

describe(TITLE, function() {
describe("Buffer", function() {
run_tests(Buffer);
run_tests(BufferBridge);
});

describe("Array", function() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/14.codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -82,7 +82,7 @@ function MyClass(value) {
}

function myClassPacker(obj) {
return new Buffer([obj.value]);
return Buffer.from([obj.value]);
}

function myClassUnpacker(buffer) {
Expand Down
8 changes: 4 additions & 4 deletions test/15.useraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ 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() {
// raw (String)
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]);
Expand Down Expand Up @@ -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"
}
Expand Down
6 changes: 3 additions & 3 deletions test/16.binarraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
});

Expand Down
2 changes: 1 addition & 1 deletion test/17.uint8array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/20.roundtrip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions test/23.extbuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/30.stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/61.encode-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand Down
2 changes: 1 addition & 1 deletion test/62.decode-only.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fe6294b

Please sign in to comment.