Fix IncompatibleSyncedRealm support

This commit is contained in:
blagoev 2017-09-29 21:43:04 +03:00
parent 8e984feedb
commit 6d9f85759e
6 changed files with 134 additions and 62 deletions

View File

@ -2,4 +2,4 @@ PACKAGE_NAME=realm-js
VERSION=2.0.0-rc13
REALM_CORE_VERSION=3.2.1
REALM_SYNC_VERSION=2.0.0-rc24
REALM_OBJECT_SERVER_VERSION=2.0.0-alpha.36
REALM_OBJECT_SERVER_VERSION=2.0.0-alpha.40

View File

@ -32,19 +32,3 @@ AuthError.__proto__ = Error;
AuthError.prototype.__proto__ = Error.prototype;
exports['AuthError'] = AuthError;
function IncompatibleSyncedRealmError(configuration) {
const error = Error.call(this);
this.name = 'IncompatibleSyncedRealmError';
this.message = error.message;
this.stack = error.stack;
Object.assign(this, configuration);
}
IncompatibleSyncedRealmError.__proto__ = Error;
IncompatibleSyncedRealmError.prototype.__proto__ = Error.prototype;
exports['IncompatibleSyncedRealmError'] = IncompatibleSyncedRealmError;

View File

@ -19,7 +19,7 @@
'use strict';
const IncompatibleSyncedRealmError = require('./errors').IncompatibleSyncedRealmError;
let getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function(obj) {
return Object.getOwnPropertyNames(obj).reduce(function (descriptors, name) {
descriptors[name] = Object.getOwnPropertyDescriptor(obj, name);
@ -61,9 +61,6 @@ module.exports = function(realmConstructor) {
//FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented
setTimeout(() => { resolve(syncedRealm); }, 1);
} catch (e) {
if (e.message === 'IncompatibleSyncedRealm') {
reject(new IncompatibleSyncedRealmError(e.configuration));
}
reject(e);
}
}
@ -101,9 +98,6 @@ module.exports = function(realmConstructor) {
//FIXME: RN hangs here. Remove when node's makeCallback alternative is implemented
setTimeout(() => { callback(null, syncedRealm); }, 1);
} catch (e) {
if (e.message === 'IncompatibleSyncedRealm') {
throw new IncompatibleSyncedRealmError(e.configuration);
}
setTimeout(() => { callback(e); }, 1);
}
}

View File

@ -252,32 +252,24 @@ public:
};
private:
static void translateSharedGroupOpenException(ContextType ctx, realm::Realm::Config config) {
try {
throw;
}
catch (RealmFileException const& ex) {
switch (ex.kind()) {
case RealmFileException::Kind::IncompatibleSyncedRealm: {
// create an object which is going to be used as exception:
// { message: 'IncompatibleSyncedRealmException', configuration: { path: ... } }
ObjectType configuration = Object::create_empty(ctx);
Object::set_property(ctx, configuration, "path", Value::from_string(ctx, ex.path()));
Object::set_property(ctx, configuration, "schema_mode", Value::from_string(ctx, "readOnly"));
if (!config.encryption_key.empty()) {
Object::set_property(ctx, configuration, "encryption_key", Value::from_binary(ctx, BinaryData(&config.encryption_key[0], 64)));
}
ObjectType object = Object::create_empty(ctx);
Object::set_property(ctx, object, "message", Value::from_string(ctx, "IncompatibleSyncedRealm"));
Object::set_property(ctx, object, "configuration", configuration);
throw Exception<T>(ctx, object);
static void handleRealmFileException(ContextType ctx, realm::Realm::Config config, const RealmFileException& ex) {
switch (ex.kind()) {
case RealmFileException::Kind::IncompatibleSyncedRealm: {
ObjectType configuration = Object::create_empty(ctx);
Object::set_property(ctx, configuration, "path", Value::from_string(ctx, ex.path()));
Object::set_property(ctx, configuration, "readOnly", Value::from_boolean(ctx, true));
if (!config.encryption_key.empty()) {
Object::set_property(ctx, configuration, "encryption_key", Value::from_binary(ctx, BinaryData(&config.encryption_key[0], 64)));
}
default:
throw;
ObjectType object = Object::create_empty(ctx);
Object::set_property(ctx, object, "name", Value::from_string(ctx, "IncompatibleSyncedRealmError"));
Object::set_property(ctx, object, "configuration", configuration);
throw Exception<T>(ctx, object);
}
}
default:
throw;
}
}
static std::string validated_notification_name(ContextType ctx, const ValueType &value) {
@ -514,8 +506,11 @@ SharedRealm RealmClass<T>::create_shared_realm(ContextType ctx, realm::Realm::Co
try {
realm = realm::Realm::get_shared_realm(config);
}
catch (const RealmFileException& ex) {
handleRealmFileException(ctx, config, ex);
}
catch (...) {
translateSharedGroupOpenException(ctx, config);
throw;
}
GlobalContextType global_context = Context<T>::get_global_context(ctx);
@ -729,8 +724,11 @@ void RealmClass<T>::wait_for_download_completion(ContextType ctx, ObjectType thi
try {
realm = realm::Realm::get_shared_realm(config);
}
catch (const RealmFileException& ex) {
handleRealmFileException(ctx, config, ex);
}
catch (...) {
translateSharedGroupOpenException(ctx, config);
throw;
}
if (auto sync_config = config.sync_config)

Binary file not shown.

View File

@ -34,12 +34,14 @@ function node_require(module) {
let tmp;
let fs;
let execFile;
let path;
if (isNodeProccess) {
tmp = node_require('tmp');
fs = node_require('fs');
execFile = node_require('child_process').execFile;
tmp.setGracefulCleanup();
path = node_require("path");
}
@ -76,6 +78,14 @@ function promisifiedLogin(server, username, password) {
});
}
function copyFileToTempDir(filename) {
let tmpDir = tmp.dirSync();
let content = fs.readFileSync(filename);
let tmpFile = tmp.fileSync({ dir: tmpDir.name });
fs.appendFileSync(tmpFile.fd, content);
return tmpFile.name;
}
function runOutOfProcess(nodeJsFilePath) {
var nodeArgs = Array.prototype.slice.call(arguments);
let tmpDir = tmp.dirSync();
@ -461,21 +471,107 @@ module.exports = {
});
},
testIncompatibleSyncedRealm() {
testIncompatibleSyncedRealmOpen() {
let realm = "sync-v1.realm";
if (isNodeProccess) {
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
}
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
return new Promise((resolve, _reject) => {
const config = { path: 'sync-1.x.realm', sync: { user, url: 'realm:://localhost:9080/~/sync-1.x' } };
try {
const realm = new Realm(config);
}
catch (e) {
if (e instanceof IncompatibleSyncedRealmError) {
const backupRealm = new Realm(e.configuration);
TestCase.assertNotEqual(backupRealm.objects('Person').length, 0);
resolve();
const config = {
path: realm,
sync: {
user,
error : err => cosole.log(err),
url: 'realm://localhost:9080/~/sync-v1'
}
};
Realm.open(config)
.then(realm =>
_reject("Should fail with IncompatibleSyncedRealmError"))
.catch(e => {
if (e.name == "IncompatibleSyncedRealmError") {
const backupRealm = new Realm(e.configuration);
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
resolve();
return;
}
_reject("Failed with unexpected error" + JSON.stringify(e));
});
});
});
},
testIncompatibleSyncedRealmOpenAsync() {
let realm = "sync-v1.realm";
if (isNodeProccess) {
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
}
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
return new Promise((resolve, _reject) => {
const config = {
path: realm,
sync: {
user,
error : err => cosole.log(err),
url: 'realm://localhost:9080/~/sync-v1'
}
};
Realm.openAsync(config, (error, realm) => {
if (!error) {
_reject("Should fail with IncompatibleSyncedRealmError");
return;
}
if (error.name == "IncompatibleSyncedRealmError") {
const backupRealm = new Realm(error.configuration);
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
resolve();
return;
}
_reject("Failed with unexpected error" + JSON.stringify(error));
});
});
});
},
testIncompatibleSyncedRealmConsructor() {
let realm = "sync-v1.realm";
if (isNodeProccess) {
realm = copyFileToTempDir(path.join(process.cwd(), "data", realm));
}
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password').then(user => {
return new Promise((resolve, _reject) => {
const config = {
path: realm,
sync: {
user,
error : err => cosole.log(err),
url: 'realm://localhost:9080/~/sync-v1'
}
};
try {
const realm = new Realm(config);
_reject("Should fail with IncompatibleSyncedRealmError");
}
catch (e) {
if (e.name == "IncompatibleSyncedRealmError") {
const backupRealm = new Realm(e.configuration);
TestCase.assertEqual(backupRealm.objects('Dog').length, 3);
resolve();
return;
}
_reject("Failed with unexpected error" + JSON.stringify(e));
}
}
_reject();
});
});
},