Support compressed resources for sync

This commit is contained in:
Gregory Schier 2017-07-31 15:09:16 -07:00
parent 41d5e86377
commit 00846a0cec
2 changed files with 24 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import * as store from './storage';
import * as misc from '../common/misc'; import * as misc from '../common/misc';
import Logger from './logger'; import Logger from './logger';
import {trackEvent} from '../analytics/index'; import {trackEvent} from '../analytics/index';
import * as zlib from 'zlib';
export const START_DELAY = 1E3; export const START_DELAY = 1E3;
export const PULL_PERIOD = 15E3; export const PULL_PERIOD = 15E3;
@ -542,6 +543,7 @@ async function _handleChangeAndPush (event, doc, timestamp) {
*/ */
const _fetchResourceGroupPromises = {}; const _fetchResourceGroupPromises = {};
const _resourceGroupCache = {}; const _resourceGroupCache = {};
export async function fetchResourceGroup (resourceGroupId, invalidateCache = false) { export async function fetchResourceGroup (resourceGroupId, invalidateCache = false) {
if (invalidateCache) { if (invalidateCache) {
delete _resourceGroupCache[resourceGroupId]; delete _resourceGroupCache[resourceGroupId];
@ -630,7 +632,14 @@ async function _getResourceGroupSymmetricKey (resourceGroupId) {
export async function encryptDoc (resourceGroupId, doc) { export async function encryptDoc (resourceGroupId, doc) {
try { try {
const symmetricKey = await _getResourceGroupSymmetricKey(resourceGroupId); const symmetricKey = await _getResourceGroupSymmetricKey(resourceGroupId);
// TODO: Turn on compression once enough users are on version >= 5.7.0
// const jsonStr = JSON.stringify(doc);
// const docStr = zlib.gzipSync(jsonStr);
// Don't use compression for now
const docStr = JSON.stringify(doc); const docStr = JSON.stringify(doc);
const message = crypt.encryptAES(symmetricKey, docStr); const message = crypt.encryptAES(symmetricKey, docStr);
return JSON.stringify(message); return JSON.stringify(message);
} catch (e) { } catch (e) {
@ -650,6 +659,12 @@ export async function decryptDoc (resourceGroupId, messageJSON) {
throw e; throw e;
} }
try {
decrypted = zlib.gunzipSync(decrypted);
} catch (err) {
// It's not compressed (legacy), which is okay for now
}
try { try {
return JSON.parse(decrypted); return JSON.parse(decrypted);
} catch (e) { } catch (e) {
@ -730,7 +745,13 @@ export async function createResourceForDoc (doc) {
} }
export async function getOrCreateResourceForDoc (doc) { export async function getOrCreateResourceForDoc (doc) {
let resource = await store.getResourceByDocId(doc._id); let [resource, ...extras] = await store.findResourcesByDocId(doc._id);
// Sometimes there may be multiple resources created by accident for
// the same doc. Let's delete the extras here if there are any.
for (const resource of extras) {
await store.removeResource(resource);
}
if (resource) { if (resource) {
return resource; return resource;

View File

@ -99,12 +99,12 @@ export async function insertResource (resource) {
export async function updateResource (resource, ...patches) { export async function updateResource (resource, ...patches) {
const newDoc = Object.assign({}, resource, ...patches); const newDoc = Object.assign({}, resource, ...patches);
await _execDB(TYPE_RESOURCE, 'update', {_id: resource._id}, newDoc); await _execDB(TYPE_RESOURCE, 'update', {_id: resource._id}, newDoc, {multi: true});
return newDoc; return newDoc;
} }
export function removeResource (resource) { export function removeResource (resource) {
return _execDB(TYPE_RESOURCE, 'remove', {_id: resource._id}); return _execDB(TYPE_RESOURCE, 'remove', {_id: resource._id}, {multi: true});
} }
// ~~~~~~ // // ~~~~~~ //