Skip to content

Commit c0ebc6e

Browse files
authored
Merge pull request #18 from Farfurix/buffer-case
fix: added the Buffer case
2 parents 849bb8c + 11a4cf3 commit c0ebc6e

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
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: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,24 @@ var GLOBAL = (function getGlobal () {
1111
})();
1212

1313
var TYPED_ARRAY_CTORS = {
14-
'Int8Array': Int8Array,
15-
'Uint8Array': Uint8Array,
16-
'Uint8ClampedArray': Uint8ClampedArray,
17-
'Int16Array': Int16Array,
18-
'Uint16Array': Uint16Array,
19-
'Int32Array': Int32Array,
20-
'Uint32Array': Uint32Array,
21-
'Float32Array': Float32Array,
22-
'Float64Array': Float64Array
14+
'Int8Array': typeof Int8Array === 'function' ? Int8Array : void 0,
15+
'Uint8Array': typeof Uint8Array === 'function' ? Uint8Array : void 0,
16+
'Uint8ClampedArray': typeof Uint8ClampedArray === 'function' ? Uint8ClampedArray : void 0,
17+
'Int16Array': typeof Int16Array === 'function' ? Int16Array : void 0,
18+
'Uint16Array': typeof Uint16Array === 'function' ? Uint16Array : void 0,
19+
'Int32Array': typeof Int32Array === 'function' ? Int32Array : void 0,
20+
'Uint32Array': typeof Uint32Array === 'function' ? Uint32Array : void 0,
21+
'Float32Array': typeof Float32Array === 'function' ? Float32Array : void 0,
22+
'Float64Array': typeof Float64Array === 'function' ? Float64Array : void 0
2323
};
2424

25-
function isFunction (value) {
26-
return typeof value === 'function';
27-
}
28-
29-
var ARRAY_BUFFER_SUPPORTED = isFunction(ArrayBuffer);
30-
var MAP_SUPPORTED = isFunction(Map);
31-
var SET_SUPPORTED = isFunction(Set);
25+
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === 'function';
26+
var MAP_SUPPORTED = typeof Map === 'function';
27+
var SET_SUPPORTED = typeof Set === 'function';
28+
var BUFFER_FROM_SUPPORTED = typeof Buffer === 'function';
3229

3330
var TYPED_ARRAY_SUPPORTED = function (typeName) {
34-
return isFunction(TYPED_ARRAY_CTORS[typeName]);
31+
return !!TYPED_ARRAY_CTORS[typeName];
3532
};
3633

3734
// Saved proto functions
@@ -413,6 +410,25 @@ var builtInTransforms = [
413410
}
414411
},
415412

413+
{
414+
type: '[[Buffer]]',
415+
416+
shouldTransform: function (type, val) {
417+
return BUFFER_FROM_SUPPORTED && val instanceof Buffer;
418+
},
419+
420+
toSerializable: function (buffer) {
421+
return arrSlice.call(buffer);
422+
},
423+
424+
fromSerializable: function (val) {
425+
if (BUFFER_FROM_SUPPORTED)
426+
return Buffer.from(val);
427+
428+
return val;
429+
}
430+
},
431+
416432
{
417433
type: '[[TypedArray]]',
418434

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)