From 6419b2a99963c6a92f1fc3e198e9fd807d683491 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Tue, 17 Nov 2015 17:41:28 +0200 Subject: [PATCH] fix: Correct behaviour of _.cloneDeep when working with buffers --- lib/Instance.ts | 16 ++++++++-------- lib/ModelHelpers.ts | 30 ++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/Instance.ts b/lib/Instance.ts index 1493319..64e914a 100644 --- a/lib/Instance.ts +++ b/lib/Instance.ts @@ -43,7 +43,7 @@ export class Instance { this._isNew = !!isNew; this._isPartial = isPartial; this._original = document; - this._modified = _.cloneDeep(document); + this._modified = model.helpers.cloneDocument(document); _.each(model.core.plugins,(plugin: Plugin) => { if (plugin.newInstance) plugin.newInstance(this, model); @@ -162,15 +162,15 @@ export class Instance { }); return Bluebird.resolve().then(() => { - conditions = _.cloneDeep(conditions); + conditions = this._model.helpers.cloneConditions(conditions); _.merge(conditions, { _id: this._modified._id }); if (!changes) { var validation = this._model.helpers.validate(this._modified); if (validation.failed) return Bluebird.reject(validation.error).bind(this).nodeify(callback); - var original = _.cloneDeep(this._original); - var modified = _.cloneDeep(this._modified); + var original = this._model.helpers.cloneDocument(this._original); + var modified = this._model.helpers.cloneDocument(this._modified); changes = this._model.helpers.diff(original, modified); } @@ -221,7 +221,7 @@ export class Instance { }).then((latest: TDocument) => { if(!latest) { this._isNew = true; - this._original = _.cloneDeep(this._modified); + this._original = this._model.helpers.cloneDocument(this._modified); return Bluebird.resolve(this); } @@ -229,7 +229,7 @@ export class Instance { this._isPartial = false; this._isNew = false; this._modified = value; - this._original = _.cloneDeep(value); + this._original = this._model.helpers.cloneDocument(value); return this; }); }).nodeify(callback); @@ -263,7 +263,7 @@ export class Instance { if (!newDocument) { this._isPartial = true; this._isNew = true; - this._original = _.cloneDeep(this._modified); + this._original = this._model.helpers.cloneDocument(this._modified); return >this; } @@ -271,7 +271,7 @@ export class Instance { this._isNew = false; this._isPartial = false; this._original = doc; - this._modified = _.cloneDeep(doc); + this._modified = this._model.helpers.cloneDocument(doc); return this; }); diff --git a/lib/ModelHelpers.ts b/lib/ModelHelpers.ts index fd0181c..d7f62dd 100644 --- a/lib/ModelHelpers.ts +++ b/lib/ModelHelpers.ts @@ -47,8 +47,9 @@ export class ModelHelpers { */ transformToDB(document: T): T { for (var property in this.model.transforms) - if(document.hasOwnProperty(property)) + if(document.hasOwnProperty(property)) { document[property] = this.model.transforms[property].toDB(document[property]); + } return document; } @@ -59,7 +60,7 @@ export class ModelHelpers { * @returns {any} A new document cloned from the original and transformed */ convertToDB(document: T): T { - var doc: T = _.cloneDeep(document); + var doc: T = this.cloneDocument(document); return this.transformToDB(doc); } @@ -73,4 +74,29 @@ export class ModelHelpers { omnom.diff(original, modified); return omnom.changes; } + + /** + * Clones the given document recursively, taking into account complex types like + * Buffers correctly. + * + * @param {any} The document you wish to clone deeply. + */ + cloneDocument(original: T): T { + return _.cloneDeep(original, (value) => { + if(Buffer.isBuffer(value)) { + return (value).slice(); + } + }); + } + + /** + * Clones the given document recursively, taking into account complex types like + * Buffers correctly. Optimized for working with query documents instead of true + * documents. + * + * @param {any} The document you wish to clone deeply. + */ + cloneConditions(original: T): T { + return this.cloneDocument(original); + } }