Issue with pouch.transformation during replication in ionic app


#1

i am currently involved in developing hybrid mobile applications using ionic framework, using pouchdb(client-side) and couchdb(server-side) for data storage. Everything was working quite smooth until i decided to implement security on data stored in pouchdb, for that i am using transform-pouch.

In which i am encrypting data in the incoming method and decrypting data in outgoing method.

Below is the used code snippet.

var transformFunctions = {
     incoming: function(doc) {
         Object.keys(doc).forEach(function(field) {
             if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
                 if (service.isJsonObject(doc[field])) {
                     doc[field] = CipherService.encrypt(JSON.stringify(doc[field]), "password");
                 } else {
                     doc[field] = CipherService.encrypt(doc[field], "password");
                 }
             }
         });
         return doc;
     },
     outgoing: function(doc) {
         Object.keys(doc).forEach(function(field) {
             if (field !== '_id' && field !== '_rev' && field !== '_revisions') {
                 var decData = CipherService.decrypt(doc[field], "password");
                 if (service.isJsonString(decData)) {
                     doc[field] = JSON.parse(decData);
                 } else {
                     doc[field] = decData;
                 }
             }
         });
         return doc;
     }
 };

 service.commonDB.transform(transformFunctions);

Now the problem i am facing particularly arises in live replication, in the outgoing function it is getting already decrypted data. i have already defined changed event of pouchdb.

localChanges = service.localDB.changes({
            since: 'now',
            live: 'true',
            include_docs: true
        }).on('change', function(change) {
            var docData = change.doc;
            if (!change.deleted) {
                if (docData.endUser) {
                    $rootScope.$broadcast("endUser", docData);
                } else {
                    $rootScope.$broadcast(docData._id, docData);
                }
            } else {
                $rootScope.$apply(function() {
                    $rootScope.$broadcast('delete', docData._id);
                });
            }
        }).on('paused', function(info) {            
            console.log(JSON.stringify(info));
        }).on('error', function(err) {
            console.log(err);
        });

Due to that I am getting an error in the outgoing function during decryption.I am not able to understand how outgoing function is getting already decrypted doc. ??

So is there any issue of transform-pouch with live replication? Has any one faced similar issues ?

I have already tried this with single time replication (Single-shot) on application start and it’s working fine. But with live replication it is creating problem.

see the below debug call stack

i have debugged and identify that yellow highlighted function passing already decrypted data to outgoing function.

so may be it has problem with live replication or pouchdb data change event.

Any out there to the rescue ??.