Skip to content

Commit

Permalink
Don't fail if type array type is not supported (fixes #1). Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Jul 11, 2016
1 parent b766307 commit 08d0914
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
46 changes: 26 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ var GLOBAL = (function getGlobal () {
return savedEval('this');
})();

var TYPED_ARRAY_SUPPORTED = typeof ArrayBuffer === 'function';
var MAP_SUPPORTED = typeof Map === 'function';
var SET_SUPPORTED = typeof Set === 'function';
var ARRAY_BUFFER_SUPPORTED = typeof ArrayBuffer === 'function';
var MAP_SUPPORTED = typeof Map === 'function';
var SET_SUPPORTED = typeof Set === 'function';

var TYPED_ARRAY_CTORS = [
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array'
];


// Saved proto functions
Expand Down Expand Up @@ -370,7 +382,7 @@ var builtInTransforms = [
type: '[[ArrayBuffer]]',

shouldTransform: function (type, val) {
return TYPED_ARRAY_SUPPORTED && val instanceof ArrayBuffer;
return ARRAY_BUFFER_SUPPORTED && val instanceof ArrayBuffer;
},

toSerializable: function (buffer) {
Expand All @@ -380,7 +392,7 @@ var builtInTransforms = [
},

fromSerializable: function (val) {
if (TYPED_ARRAY_SUPPORTED) {
if (ARRAY_BUFFER_SUPPORTED) {
var buffer = new ArrayBuffer(val.length);
var view = new Int8Array(buffer);

Expand All @@ -397,17 +409,14 @@ var builtInTransforms = [
type: '[[TypedArray]]',

shouldTransform: function (type, val) {
return TYPED_ARRAY_SUPPORTED && (
val instanceof Int8Array ||
val instanceof Uint8Array ||
val instanceof Uint8ClampedArray ||
val instanceof Int16Array ||
val instanceof Uint16Array ||
val instanceof Int32Array ||
val instanceof Uint32Array ||
val instanceof Float32Array ||
val instanceof Float64Array
);
for (var i = 0; i < TYPED_ARRAY_CTORS.length; i++) {
var ctorName = TYPED_ARRAY_CTORS[i];

if (typeof GLOBAL[ctorName] === 'function' && val instanceof GLOBAL[ctorName])
return true;
}

return false;
},

toSerializable: function (arr) {
Expand All @@ -418,10 +427,7 @@ var builtInTransforms = [
},

fromSerializable: function (val) {
if (TYPED_ARRAY_SUPPORTED)
return new GLOBAL[val.ctorName](val.arr);

return val.arr;
return typeof GLOBAL[val.ctorName] === 'function' ? new GLOBAL[val.ctorName](val.arr) : val.arr;
}
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "replicator",
"version": "1.0.0",
"version": "1.0.1",
"description": "Advanced JavaScript objects serialization.",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,21 @@ describe('Built-in transforms', function () {
assert(!actual.set.has('yo'));
});
});

describe('Regression', function () {
var replicator = new Replicator();

it('Should not throw if one of the typed array types is not supported (GH-1)', function () {
var arr = new Uint8Array([1, 230]);
var savedUint8Array = global.Uint8Array;

global.Uint8Array = void 0;

var actual = replicator.decode(replicator.encode(arr));

assert.strictEqual(actual[0], 1);
assert.strictEqual(actual[1], 230);

global.Uint8Array = savedUint8Array;
});
});

0 comments on commit 08d0914

Please sign in to comment.