(#5200) - don't clone special objcts like Workers
diff --git a/packages/pouchdb-utils/src/clone.js b/packages/pouchdb-utils/src/clone.js index 36da3f2..e2d6760 100644 --- a/packages/pouchdb-utils/src/clone.js +++ b/packages/pouchdb-utils/src/clone.js
@@ -1,5 +1,6 @@ import isBinaryObject from './isBinaryObject'; import cloneBinaryObject from './cloneBinaryObject'; +import isPlainObject from './isPlainObject'; function clone(object) { var newObject; @@ -28,6 +29,10 @@ return cloneBinaryObject(object); } + if (!isPlainObject(object)) { + return object; // don't clone objects like Workers + } + newObject = {}; for (i in object) { if (Object.prototype.hasOwnProperty.call(object, i)) {
diff --git a/packages/pouchdb-utils/src/isPlainObject.js b/packages/pouchdb-utils/src/isPlainObject.js new file mode 100644 index 0000000..57a8b33 --- /dev/null +++ b/packages/pouchdb-utils/src/isPlainObject.js
@@ -0,0 +1,19 @@ +// most of this is borrowed from lodash.isPlainObject: +// https://github.com/fis-components/lodash.isplainobject/ +// blob/29c358140a74f252aeb08c9eb28bef86f2217d4a/index.js + +var funcToString = Function.prototype.toString; +var objectCtorString = funcToString.call(Object); + +function isPlainObject(value) { + var proto = Object.getPrototypeOf(value); + /* istanbul ignore if */ + if (proto === null) { // not sure when this happens, but I guess it can + return true; + } + var Ctor = proto.constructor; + return (typeof Ctor == 'function' && + Ctor instanceof Ctor && funcToString.call(Ctor) == objectCtorString); +} + +export default isPlainObject; \ No newline at end of file
diff --git a/tests/unit/test.clone.js b/tests/unit/test.clone.js new file mode 100644 index 0000000..f81d654 --- /dev/null +++ b/tests/unit/test.clone.js
@@ -0,0 +1,33 @@ + +'use strict'; + +var should = require('chai').should(); +var PouchDB = require('../../packages/pouchdb-for-coverage'); +var clone = PouchDB.utils.clone; + +describe('test.clone.js', function () { + + it('Clones regular objects', function () { + var obj1 = {foo: 'bar'}; + var obj2 = clone(obj1); + obj1.baz = 'quuz'; + should.not.exist(obj2.baz); + }); + + it('Doesn\'t clone fancy objects', function () { + + function Kitty() { + } + + Kitty.prototype.meow = function () { + return 'meow'; + }; + + var obj1 = {kitty: new Kitty()}; + var obj2 = clone(obj1); + obj1.kitty.meow().should.equal('meow'); + obj2.kitty.meow().should.equal('meow'); + obj1.kitty.foo = 'bar'; + obj2.kitty.foo.should.equal('bar'); + }); +});