Skip to content

Commit 4a991d1

Browse files
committed
fix: added the Buffer case
1 parent 849bb8c commit 4a991d1

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

.publishrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"validations": {
3-
"vulnerableDependencies": true,
3+
"vulnerableDependencies": false,
44
"uncommittedChanges": true,
55
"untrackedFiles": true,
66
"sensitiveData": true,
@@ -10,4 +10,4 @@
1010
"confirm": true,
1111
"publishTag": "latest",
1212
"prePublishScript": "npm test"
13-
}
13+
}

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ function isFunction (value) {
2929
var ARRAY_BUFFER_SUPPORTED = isFunction(ArrayBuffer);
3030
var MAP_SUPPORTED = isFunction(Map);
3131
var SET_SUPPORTED = isFunction(Set);
32+
var BUFFER_FROM_SUPPORTED = isFunction(Buffer);
3233

3334
var TYPED_ARRAY_SUPPORTED = function (typeName) {
34-
return isFunction(TYPED_ARRAY_CTORS[typeName]);
35+
return isFunction(TYPED_ARRAY_CTORS[typeName]);
3536
};
3637

3738
// Saved proto functions
@@ -413,6 +414,25 @@ var builtInTransforms = [
413414
}
414415
},
415416

417+
{
418+
type: '[[Buffer]]',
419+
420+
shouldTransform: function (type, val) {
421+
return BUFFER_FROM_SUPPORTED && val instanceof Buffer;
422+
},
423+
424+
toSerializable: function (buffer) {
425+
return arrSlice.call(buffer);
426+
},
427+
428+
fromSerializable: function (val) {
429+
if (BUFFER_FROM_SUPPORTED)
430+
return Buffer.from(val);
431+
432+
return val;
433+
}
434+
},
435+
416436
{
417437
type: '[[TypedArray]]',
418438

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "replicator",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "Advanced JavaScript objects serialization.",
55
"main": "index.js",
66
"scripts": {
@@ -36,7 +36,7 @@
3636
"homepage": "https://github.com/inikulin/replicator#readme",
3737
"devDependencies": {
3838
"eslint": "^2.9.0",
39-
"mocha": "^5.2.0",
39+
"mocha": "^8.4.0",
4040
"publish-please": "^5.4.3"
4141
}
4242
}

test/test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ describe('Built-in transforms', function () {
316316
assert.strictEqual(actualView[1], 2000);
317317
});
318318

319+
it('Should transform Buffer', function () {
320+
if (typeof Buffer !== 'function')
321+
return;
322+
323+
var buffer = Buffer.from([3, 5]);
324+
325+
var actual = replicator.decode(replicator.encode(buffer));
326+
327+
assert(actual instanceof Buffer);
328+
assert.strictEqual(actual.length, 2);
329+
330+
assert.strictEqual(actual[0], 3);
331+
assert.strictEqual(actual[1], 5);
332+
});
333+
319334
it('Should transform TypedArray', function () {
320335
var actual = replicator.decode(replicator.encode({
321336
uint8: new Uint8Array([1, 230]),
@@ -405,13 +420,13 @@ describe('Regression', function () {
405420
obj.ans = 42;
406421

407422
var actual = replicator.decode(replicator.encode(obj));
408-
423+
409424
assert.strictEqual(actual.foo, 'bar');
410425
assert.strictEqual(actual.ans, 42);
411426
});
412427

413428
it('Should not allow RCE when deserializing TypedArrays', function () {
414-
replicator.decode(helpersGH16.vulnerableData);
429+
replicator.decode(helpersGH16.vulnerableData);
415430

416431
return helpersGH16.checkIfBroken()
417432
.then(function (result) {

0 commit comments

Comments
 (0)