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 all 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)
* Disabled executable stack on Linux. ([#3752](https://github.com/realm/realm-js/issues/3752), since v10.2.0)
* Don't hang when using the network after hot-reloading an RN app. ([#3668](https://github.com/realm/realm-js/issues/3668))

Expand Down Expand Up @@ -3569,8 +3570,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 @@ -3585,8 +3584,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
8 changes: 4 additions & 4 deletions docs/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,16 @@ class Realm {
}
/**
* This describes the different options used to create a {@link Realm} instance.
*
*
* See {@tutorial http-proxy} for details on how use an HTTP forward proxy with this library.
*
*
* @typedef Realm~Configuration
* @type {Object}
* @property {ArrayBuffer|ArrayBufferView} [encryptionKey] - The 512-bit (64-byte) encryption
* key used to encrypt and decrypt all data in the Realm.
* @property {callback(Realm, Realm)} [migration] - The function to run if a migration is needed.
* This function should provide all the logic for converting data models from previous schemas
* to the new schema.
* to the new schema. This option is incompatible with `sync`.
* This function takes two arguments:
* - `oldRealm` - The Realm before migration is performed.
* - `newRealm` - The Realm that uses the latest `schema`, which should be modified as necessary.
Expand All @@ -378,7 +378,7 @@ class Realm {
* still requires a path (can be the default path) to identify the Realm so other processes can
* open the same Realm. The file will also be used as swap space if the Realm becomes bigger than
* what fits in memory, but it is not persistent and will be removed when the last instance
* is closed.
* is closed. This option is incompatible with `sync`.
* @property {boolean} [readOnly=false] - Specifies if this Realm should be opened as read-only.
* @property {boolean} [disableFormatUpgrade=false] - Specifies if this Realm's file format should
* be automatically upgraded if it was created with an older version of the Realm library.
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
4 changes: 2 additions & 2 deletions tests/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 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;
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 All @@ -143,9 +156,6 @@ declare namespace Realm {

type ObjectChangeCallback = (object: Object, changes: ObjectChangeSet) => void;

interface PartialConfiguration extends Partial<Realm.Configuration> {
}

/**
* Object
* @see { @link https://realm.io/docs/javascript/latest/api/Realm.Object.html }
Expand Down