Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate configuration #3772

Merged
merged 10 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ x.x.x Release notes (yyyy-MM-dd)

### Fixed
* A warning to polyfill `crypto.getRandomValues` was triggered prematurely ([#3714](https://github.com/realm/realm-js/issues/3714), since v10.4.0)
* Mutual exclusive configuration options (`sync`/`inMemory` and `sync`/`migration`) could lead to a crash. ([#3771](https://github.com/realm/realm-js/issues/3771), since v1.0.0)

### Compatibility
* MongoDB Realm Cloud.
Expand Down Expand Up @@ -3567,8 +3568,6 @@ The feature known as Partial synchronization has been renamed to Query-based syn
callback would produce incorrect results.

### Internal
* Upgraded Realm Core from v10.7.1 to 10.7.1.
* Upgraded Realm Core from v10.6.0 to 10.7.1
* None.

2.0.1 Release notes (2017-10-23)
Expand All @@ -3583,8 +3582,6 @@ The feature known as Partial synchronization has been renamed to Query-based syn
* None.

### Internal
* Upgraded Realm Core from v10.7.1 to 10.7.1.
* Upgraded Realm Core from v10.6.0 to 10.7.1
* Upgraded to Realm Sync 2.1.0.

2.0.0 Release notes (2017-10-17)
Expand Down
10 changes: 10 additions & 0 deletions src/js_realm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ bool RealmClass<T>::get_realm_config(ContextType ctx, size_t argc, const ValueTy
static const String in_memory_string = "inMemory";
ValueType in_memory_value = Object::get_property(ctx, object, in_memory_string);
if (!Value::is_undefined(ctx, in_memory_value) && Value::validated_to_boolean(ctx, in_memory_value, "inMemory")) {
if (config.force_sync_history || config.sync_config) {
throw std::invalid_argument("Options 'inMemory' and 'sync' are mutual exclusive.");
}
config.in_memory = true;
}

Expand Down Expand Up @@ -625,6 +628,10 @@ bool RealmClass<T>::get_realm_config(ContextType ctx, size_t argc, const ValueTy
static const String migration_string = "migration";
ValueType migration_value = Object::get_property(ctx, object, migration_string);
if (!Value::is_undefined(ctx, migration_value)) {
if (config.force_sync_history || config.sync_config) {
throw std::invalid_argument("Options 'migration' and 'sync' are mutual exclusive.");
}

FunctionType migration_function = Value::validated_to_function(ctx, migration_value, "migration");

if (config.schema_mode == SchemaMode::ResetFile) {
Expand Down Expand Up @@ -669,6 +676,9 @@ bool RealmClass<T>::get_realm_config(ContextType ctx, size_t argc, const ValueTy
static const String disable_format_upgrade_string = "disableFormatUpgrade";
ValueType disable_format_upgrade_value = Object::get_property(ctx, object, disable_format_upgrade_string);
if (!Value::is_undefined(ctx, disable_format_upgrade_value)) {
if (config.force_sync_history || config.sync_config) {
throw std::invalid_argument("Options 'disableFormatUpgrade' and 'sync' are mutual exclusive.");
}
config.disable_format_upgrade = Value::validated_to_boolean(ctx, disable_format_upgrade_value, "disableFormatUpgrade");
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/js/session-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,37 @@ module.exports = {
TestCase.assertNull(realm.syncSession);
},

async testRealmInvalidSyncConfiguration1() {
const config = {
sync: true,
inMemory: true,
};

return new Promise((resolve, reject) => {
return Realm.open(config)
.then(_ => reject())
.catch(_ => resolve());
});
},

async testRealmInvalidSyncConfiguration2() {
const partition = Utils.genPartition();
let credentials = Realm.Credentials.anonymous();
let app = new Realm.App(appConfig);


return new Promise((resolve, reject) => {
return app.logIn(credentials)
.then(user => {
let config = getSyncConfiguration(user, partition);
config.migration = (_) => { /* empty function */ };
return Realm.open(config)
.then(_ => reject())
.catch(_ => resolve());
});
});
},

testRealmOpen() {
if (!isNodeProcess) {
return;
Expand Down
31 changes: 22 additions & 9 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,38 @@ declare namespace Realm {
error?: ErrorCallback;
}

/**
* realm configuration
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~Configuration }
*/
interface Configuration {
interface BaseConfiguration {
encryptionKey?: ArrayBuffer | ArrayBufferView | Int8Array;
migration?: MigrationCallback;
schema?: (ObjectClass | ObjectSchema)[];
schemaVersion?: number;
shouldCompactOnLaunch?: (totalBytes: number, usedBytes: number) => boolean;
path?: string;
fifoFilesFallbackPath?: string;
readOnly?: boolean;
}

interface ConfigurationWithSync extends BaseConfiguration {
sync: SyncConfiguration,
migration?: never,
inMemory?: never,
deleteRealmIfMigrationNeeded?: never,
disableFormatUpgrade?: never
}

interface ConfigurationWithoutSync extends BaseConfiguration {
sync?: never,
kneth marked this conversation as resolved.
Show resolved Hide resolved
migration?: MigrationCallback;
inMemory?: boolean;
schema?: (ObjectClass | ObjectSchema)[];
schemaVersion?: number;
sync?: SyncConfiguration;
deleteRealmIfMigrationNeeded?: boolean;
disableFormatUpgrade?: boolean;
}

/**
* realm configuration
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~Configuration }
*/
type Configuration = ConfigurationWithSync | ConfigurationWithoutSync;

/**
* realm configuration used for overriding default configuration values.
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.html#~Configuration }
Expand Down