extensions.js 28.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

'use strict';

/* global navigator */

const URL = require('url-parse');

let getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors || function(obj) {
    return Object.getOwnPropertyNames(obj).reduce(function (descriptors, name) {
        descriptors[name] = Object.getOwnPropertyDescriptor(obj, name);
        return descriptors;
    }, {});
};

const subscriptionObjectNameRegex = /^(class_)?(.*?)(_matches)?$/gm;

function setConstructorOnPrototype(klass) {
    if (klass.prototype.constructor !== klass) {
        Object.defineProperty(klass.prototype, 'constructor', { value: klass, configurable: true, writable: true });
    }
}

/**
 * Finds the permissions associated with a given Role or create them as needed.
 *
 * @param {RealmObject} Container RealmObject holding the permission list.
 * @param {List<Realm.Permissions.Permission>} list of permissions.
 * @param {string} name of the role to find or create permissions for.
 */
function findOrCreatePermissionForRole(realmObject, permissions, roleName) {
    let realm = realmObject._realm;
    if (!realm.isInTransaction) {
        throw Error("'findOrCreate' can only be called inside a write transaction.");
    }
    let permissionsObj = permissions.filtered(`role.name = '${roleName}'`)[0];
    if (permissionsObj === undefined) {
        let role = realm.objects("__Role").filtered(`name = '${roleName}'`)[0];
        if (role === undefined) {
            role = realm.create("__Role", {'name': roleName});
        }
        // Create new permissions object with all privileges disabled
        permissionsObj = realm.create("__Permission", { 'role': role });
        permissions.push(permissionsObj);
    }
    return permissionsObj;
}

/**
 * Adds the schema object if one isn't already defined
 */
function addSchemaIfNeeded(schemaList, schemaObj) {
    const name = schemaObj.schema.name;
    if (schemaList.find((obj) => obj && (obj.name === name || (obj.schema && obj.schema.name === name))) === undefined) {
        schemaList.push(schemaObj);
    }
}

function waitForCompletion(session, fn, timeout, timeoutErrorMessage) {
    const waiter = new Promise((resolve, reject) => {
        fn.call(session, (error) => {
            if (error === undefined) {
                setTimeout(() => resolve(), 1);
            } else {
                setTimeout(() => reject(error), 1);
            }
        });
    });
    if (timeout === undefined) {
        return waiter;
    }
    return Promise.race([
        waiter,
        new Promise((resolve, reject) => {
            setTimeout(() => {
                reject(timeoutErrorMessage);
            }, timeout)
        })
    ]);
}

function openLocalRealm(realmConstructor, config) {
    let promise = Promise.resolve(new realmConstructor(config));
    promise.progress = (callback) => { return promise; };
    promise.cancel = () => { };
    return promise;
}

module.exports = function(realmConstructor, context) {
    // Add the specified Array methods to the Collection prototype.
    Object.defineProperties(realmConstructor.Collection.prototype, require('./collection-methods'));

    setConstructorOnPrototype(realmConstructor.Collection);
    setConstructorOnPrototype(realmConstructor.List);
    setConstructorOnPrototype(realmConstructor.Results);
    setConstructorOnPrototype(realmConstructor.Object);

    //Add static methods to the Realm object
    Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({
        open(config) {
            // If no config is defined, we should just open the default realm
            if (config === undefined) {
                config = {};
            }

            // For local Realms we open the Realm and return it in a resolved Promise.
            if (!("sync" in config)) {
                return openLocalRealm(realmConstructor, config);
            }

            // Determine if we are opening an existing Realm or not.
            let behavior = realmConstructor.exists(config) ? "existingRealmFileBehavior" : "newRealmFileBehavior";

            // Define how the Realm file is opened
            let openLocalRealmImmediately = false; // Default is downloadBeforeOpen
            if (config.sync[behavior] !== undefined) {
                const type = config.sync[behavior].type;
                switch (type) {
                    case 'downloadBeforeOpen':
                        openLocalRealmImmediately = false;
                        break;
                    case 'openImmediately':
                        openLocalRealmImmediately = true;
                        break;
                    default:
                        throw Error(`Invalid type: '${type}'. Only 'downloadBeforeOpen' and 'openImmediately' is allowed.`)
                }
            }

            // If configured to do so, the synchronized Realm will be opened locally immediately.
            // If this is the first time the Realm is created, the schema will be created locally as well.
            if (openLocalRealmImmediately) {
                return openLocalRealm(realmConstructor, config);
            }

            // Otherwise attempt to synchronize the Realm state from the server before opening it.

            // First configure any timeOut and corresponding behavior.
            let openPromises = []
            if (config.sync[behavior] !== undefined && config.sync[behavior].timeOut !== undefined) {
                let timeOut = config.sync[behavior].timeOut;
                if (typeof timeOut !== 'number') {
                    throw new Error(`'timeOut' must be a number: '${timeOut}'`);
                }

                // Define the behavior in case of a timeout
                let throwOnTimeOut = true; // Default is to throw
                if (config.sync[behavior] !== undefined && config.sync[behavior].timeOutBehavior) {
                    const timeOutBehavior = config.sync[behavior].timeOutBehavior;
                    switch (timeOutBehavior) {
                        case 'throwException':
                            throwOnTimeOut = true;
                            break;
                        case 'openLocal':
                            throwOnTimeOut = false;
                            break;
                        default:
                            throw Error(`Invalid 'timeOutBehavior': '${timeOutBehavior}'. Only 'throwException' and 'openLocal' is allowed.`)
                    }
                }

                openPromises.push(new Promise((resolve, reject) => {
                    setTimeout(() => {
                        if (asyncOpenTask) {
                            asyncOpenTask.cancel();
                            asyncOpenTask = null;
                        }
                        if (throwOnTimeOut) {
                            reject(new Error(`${config.sync.url} could not be downloaded in the allocated time: ${timeOut} ms.`));
                        } else {
                            return resolve(openLocalRealm(realmConstructor, config));
                        }
                    }, timeOut)
                }));
            }

            // Configure promise responsible for downloading the Realm from the server
            let asyncOpenTask;
            let cancelled = false;
            openPromises.push(new Promise((resolve, reject) => {
                asyncOpenTask = realmConstructor._asyncOpen(config, (realm, error) => {
                    setTimeout(() => {
                        asyncOpenTask = null;
                        // The user may have cancelled the open between when
                        // the download completed and when we managed to
                        // actually invoke this, so recheck here.
                        if (cancelled) {
                            return;
                        }
                        if (error) {
                            reject(error);
                        } else {
                            resolve(realm);
                        }
                    }, 0);
                });
            }));

            // Return wrapped promises, allowing the users to control them.
            let openPromise = Promise.race(openPromises);
            openPromise.cancel = () => {
                if (asyncOpenTask) {
                    asyncOpenTask.cancel();
                    cancelled = true;
                }
            };
            openPromise.progress = (callback) => {
                if (asyncOpenTask) {
                    asyncOpenTask.addDownloadNotification(callback);
                }
                return openPromise;
            };
            return openPromise;
        },

        openAsync(config, callback, progressCallback) {
            const message = "Realm.openAsync is now deprecated in favor of Realm.open. This function will be removed in future versions.";
            (console.warn || console.log).call(console, message);

            let promise = this.open(config)
            if (progressCallback) {
                promise.progress(progressCallback)
            }

            promise.then(realm => {
                callback(null, realm)
            }).catch(error => {
                callback(error);
            });
        },

        createTemplateObject(objectSchema) {
            let obj = {};
            for (let key in objectSchema.properties) {

                let type;
                if (typeof objectSchema.properties[key] === 'string' || objectSchema.properties[key] instanceof String) {
                    // Simple declaration of the type
                    type = objectSchema.properties[key];
                } else {
                    // Advanced property setup
                    const property = objectSchema.properties[key];

                    // if optional is set, it wil take precedence over any `?` set on the type parameter
                    if (property.optional === true) {
                        continue;
                    }

                    // If a default value is explicitly set, always set the property
                    if (property.default !== undefined) {
                        obj[key] = property.default;
                        continue;
                    }

                    type = property.type;
                }

                // Set the default value for all required primitive types.
                // Lists are always treated as empty if not specified and references to objects are always optional
                switch (type) {
                    case 'bool': obj[key] = false; break;
                    case 'int': obj[key] = 0; break;
                    case 'float': obj[key] = 0.0; break;
                    case 'double': obj[key] = 0.0; break;
                    case 'string': obj[key] = ""; break;
                    case 'data': obj[key] = new ArrayBuffer(0); break;
                    case 'date': obj[key] = new Date(0); break;
                }
            }
            return obj;
        }
    }));

    // Add static properties to Realm Object
    const updateModeType = {
      All: 'all',
      Modified: 'modified',
      Never: 'never',
    };

    if (!realmConstructor.UpdateMode) {
      Object.defineProperty(realmConstructor, 'UpdateMode', {
        value: updateModeType,
        configurable: false,
      });
    }

    // Add sync methods
    if (realmConstructor.Sync) {
        let userMethods = require('./user-methods');
        Object.defineProperties(realmConstructor.Sync.User, getOwnPropertyDescriptors(userMethods.static));
        Object.defineProperties(realmConstructor.Sync.User.prototype, getOwnPropertyDescriptors(userMethods.instance));
        Object.defineProperty(realmConstructor.Sync.User, '_realmConstructor', { value: realmConstructor });
        realmConstructor.Sync.Credentials = {};
        Object.defineProperties(realmConstructor.Sync.Credentials, getOwnPropertyDescriptors(userMethods.credentials));
        realmConstructor.Sync.AuthError = require('./errors').AuthError;

        if (realmConstructor.Sync.removeAllListeners) {
            process.on('exit', realmConstructor.Sync.removeAllListeners);
            process.on('SIGINT', function () {
                realmConstructor.Sync.removeAllListeners();
                process.exit(2);
            });
            process.on('uncaughtException', function(e) {
                realmConstructor.Sync.removeAllListeners();
                /* eslint-disable no-console */
                console.log(e.stack);
                process.exit(99);
            });
        }

        if (!realmConstructor.Sync.setSyncLogger) {
            realmConstructor.Sync.setSyncLogger = function (level, message) {
                realmConstructor.Sync.setLogger(level, message);
            }
        }

        setConstructorOnPrototype(realmConstructor.Sync.User);
        setConstructorOnPrototype(realmConstructor.Sync.Session);

        // A configuration for a default Realm
        realmConstructor.automaticSyncConfiguration = function() {
            let user;

            if (arguments.length === 0) {
                let users = this.Sync.User.all;
                let identities = Object.keys(users);
                if (identities.length === 1) {
                    user = users[identities[0]];
                } else {
                    new Error(`One and only one user should be logged in but found ${users.length} users.`);
                }
            } else if (arguments.length === 1) {
                user = arguments[0];
            } else {
                new Error(`Zero or one argument expected.`);
            }

            let url = new URL(user.server);
            let secure = (url.protocol === 'https:')?'s':'';
            let port = (url.port === undefined)?'9080':url.port
            let realmUrl = `realm${secure}://${url.hostname}:${port}/default`;

            let config = {
                sync: {
                    user: user,
                    url: realmUrl,
                }
            };
            return config;
        };

        realmConstructor.Sync.openLocalRealmBehavior = {
            type: 'openImmediately'
        };

        realmConstructor.Sync.downloadBeforeOpenBehavior = {
            type: 'downloadBeforeOpen',
            timeOut: 30 * 1000,
            timeOutBehavior: 'throwException'
        };

        realmConstructor.Sync.setFeatureToken = function() {
            console.log('Realm.Sync.setFeatureToken() is deprecated and you can remove any calls to it.');
        }

        realmConstructor.Sync.Session.prototype.uploadAllLocalChanges = function(timeout) {
            return waitForCompletion(this, this._waitForUploadCompletion, timeout, `Uploading changes did not complete in ${timeout} ms.`);
        };

        realmConstructor.Sync.Session.prototype.downloadAllServerChanges = function(timeout) {
            return waitForCompletion(this, this._waitForDownloadCompletion, timeout, `Downloading changes did not complete in ${timeout} ms.`);
        };

        // Keep these value in sync with subscription_state.hpp
        realmConstructor.Sync.SubscriptionState = {
            Error: -1,      // An error occurred while creating or processing the partial sync subscription.
            Creating: 2,    // The subscription is being created.
            Pending: 0,     // The subscription was created, but has not yet been processed by the sync server.
            Complete: 1,    // The subscription has been processed by the sync server and data is being synced to the device.
            Invalidated: 3, // The subscription has been removed.
        };

        realmConstructor.Sync.ConnectionState = {
            Disconnected: "disconnected",
            Connecting: "connecting",
            Connected: "connected",
        };

        // Define the permission schemas as constructors so that they can be
        // passed into directly to functions which want object type names
        const Permission = function() {};
        Permission.schema = Object.freeze({
            name: '__Permission',
            properties: {
                role: '__Role',
                canRead: {type: 'bool', default: false},
                canUpdate: {type: 'bool', default: false},
                canDelete: {type: 'bool', default: false},
                canSetPermissions: {type: 'bool', default: false},
                canQuery: {type: 'bool', default: false},
                canCreate: {type: 'bool', default: false},
                canModifySchema: {type: 'bool', default: false},
            }
        });

        const User = function() {};
        User.schema = Object.freeze({
            name: '__User',
            primaryKey: 'id',
            properties: {
                id: 'string',
                role: '__Role'
            }
        });

        const Role = function() {};
        Role.schema = Object.freeze({
            name: '__Role',
            primaryKey: 'name',
            properties: {
                name: 'string',
                members: '__User[]'
            }
        });

        const Class = function() {};
        Class.schema = Object.freeze({
            name: '__Class',
            primaryKey: 'name',
            properties: {
                name: 'string',
                permissions: '__Permission[]'
            }
        });
        Class.prototype.findOrCreate = function(roleName) {
            return findOrCreatePermissionForRole(this, this.permissions, roleName);
        };

        const Realm = function() {};
        Realm.schema = Object.freeze({
            name: '__Realm',
            primaryKey: 'id',
            properties: {
                id: 'int',
                permissions: '__Permission[]'
            }
        });
        Realm.prototype.findOrCreate = function(roleName) {
            return findOrCreatePermissionForRole(this, this.permissions, roleName);
        };

        const permissionsSchema = {
            'Class': Class,
            'Permission': Permission,
            'Realm': Realm,
            'Role': Role,
            'User': User,
        };

        if (!realmConstructor.Permissions) {
            Object.defineProperty(realmConstructor, 'Permissions', {
                value: permissionsSchema,
                configurable: false
            });
        }

        const ResultSets = function() {};
        ResultSets.schema = Object.freeze({
            name: '__ResultSets',
            properties: {
                _name: { type: 'string', indexed: true, mapTo: 'name' },
                _query: {type: 'string', mapTo: 'query'},
                _matchesProperty: {type: 'string', mapTo: 'matches_property'},
                _queryParseCounter: {type: 'int',  mapTo: 'query_parse_counter'},
                _state: {type: 'int', mapTo: 'status'},
                _errorMessage: { type: 'string', mapTo: 'error_message'},
                _createdAt: { type: 'date', mapTo: 'created_at'},
                _updatedAt: { type: 'date', mapTo: 'updated_at'},
                _expiresAt: { type: 'date', optional: true, mapTo: 'expires_at'},
                _timeToLive: { type: 'int', optional: true, mapTo: 'time_to_live'},
            }
        });
        ResultSets.prototype._subscriptionUpdated = function(sub) {
            this._updatedAt = new Date();
            this._expiresAt = new Date(sub._updatedAt.getTime() + sub._timeToLive);
        };
        Object.defineProperties(ResultSets.prototype, {
            objectType: {
                enumerable: true,
                get: function() {
                    return this._matchesProperty.replace(subscriptionObjectNameRegex, '$2');
                }
            },
            name: {
                enumerable: true,
                get: function() {
                    return this._name;
                }
            },
            query: {
                enumerable: true,
                set: function(val) {
                    if (typeof val === 'string' || val instanceof String) {
                        this._query = val;
                    } else {
                        const queryDescription = val.description();
                        if (queryDescription === undefined) {
                            throw new Error("Updating a query must be done either using a String or a Results object.");
                        }
                        this._query = queryDescription;
                    }
                    this._errorMessage = '';
                    this._state = 0;
                    this._subscriptionUpdated(this);
                },
                get: function() {
                    return this._query;
                }
            },
            state: {
                enumerable: true,
                get: function() {
                    return this._state;
                }
            },
            error: {
                enumerable: true,
                get: function() {
                    return (this._errorMessage === '') ? undefined : this._errorMessage;
                }
            },
            createdAt: {
                enumerable: true,
                get: function() {
                    return this._createdAt;
                }
            },
            updatedAt: {
                enumerable: true,
                get: function() {
                    return this._updatedAt;
                }
            },
            expiresAt: {
                enumerable: true,
                get: function() {
                    return this._expiresAt;
                }
            },
            timeToLive: {
                enumerable: true,
                set: function(val) {
                    this._timeToLive = val;
                    this._subscriptionUpdated(this);
                },
                get: function() {
                    return this._timeToLive;
                }
            }
        });

        const subscriptionSchema = {
            'ResultSets': ResultSets
        };

        if (!realmConstructor.Subscription) {
            Object.defineProperty(realmConstructor, 'Subscription', {
                value: subscriptionSchema,
                configurable: false,
            });
        }

        // Add instance methods to the Realm object that are only applied if Sync is
        Object.defineProperties(realmConstructor.prototype, getOwnPropertyDescriptors({
            permissions(arg) {
                if (!this._isPartialRealm) {
                    throw new Error("Wrong Realm type. 'permissions()' is only available for Query-based Realms.");
                }
                // If no argument is provided, return the Realm-level permissions
                if (arg === undefined) {
                    return this.objects('__Realm').filtered(`id = 0`)[0];
                } else {
                    // Else try to find the corresponding Class-level permissions
                    let schemaName = this._schemaName(arg);
                    let classPermissions = this.objects('__Class').filtered(`name = '${schemaName}'`);
                    if (classPermissions.length === 0) {
                        throw Error(`Could not find Class-level permissions for '${schemaName}'`);
                    }
                    return classPermissions[0];
                }
            },

            subscriptions(name) {
                if (!this._isPartialRealm) {
                    throw new Error("Wrong Realm type. 'subscriptions()' is only available for Query-based Realms.");
                }
                let allSubscriptions = this.objects('__ResultSets');
                if (name) {
                    if (typeof(name) !== 'string') {
                        throw new Error(`string expected - got ${typeof(name)}.`);
                    }
                    if (name.includes('*') || name.includes('?')) {
                        allSubscriptions = allSubscriptions.filtered(`name LIKE '${name}'`);
                    } else {
                        allSubscriptions = allSubscriptions.filtered(`name == '${name}'`);
                    }
                }
                return allSubscriptions;
            },

            unsubscribe(name) {
                if (!this._isPartialRealm) {
                    throw new Error("Wrong Realm type. 'unsubscribe()' is only available for Query-based Realms.");
                }
                if (typeof(name) === 'string') {
                    if (name !== '') {
                        let named_subscriptions = this.objects('__ResultSets').filtered(`name == '${name}'`);
                        if (named_subscriptions.length === 0) {
                            return;
                        }
                        let doCommit = false;
                        if (!this.isInTransaction) {
                            this.beginTransaction();
                            doCommit = true;
                        }
                        this.delete(named_subscriptions);
                        if (doCommit) {
                            this.commitTransaction();
                        }
                    } else {
                        throw new Error('Non-empty string expected.');
                    }
                } else {
                    throw new Error(`string expected - got ${typeof(name)}.`);
                }
            }
        }));

        Object.defineProperties(realmConstructor, getOwnPropertyDescriptors({
            _extendQueryBasedSchema(schema) {
                addSchemaIfNeeded(schema, realmConstructor.Permissions.Class);
                addSchemaIfNeeded(schema, realmConstructor.Permissions.Permission);
                addSchemaIfNeeded(schema, realmConstructor.Permissions.Realm);
                addSchemaIfNeeded(schema, realmConstructor.Permissions.Role);
                addSchemaIfNeeded(schema, realmConstructor.Permissions.User);
                addSchemaIfNeeded(schema, realmConstructor.Subscription.ResultSets);
            },

            // Creates the user agent description for the JS binding itself. Users must specify the application
            // user agent using Realm.Sync.setUserAgent(...)
            _createUserAgentDescription() {
                // Detect if in ReactNative (running on a phone) or in a Node.js environment
                // Credit: https://stackoverflow.com/questions/39468022/how-do-i-know-if-my-code-is-running-as-react-native
                try {
                    var userAgent = "RealmJS/";
                    userAgent = userAgent + require('../package.json').version + " (" + context + ", ";
                    if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
                        // Running on ReactNative
                        const Platform = require('react-native').Platform;
                        userAgent += Platform.OS + ", v" + Platform.Version;
                    } else {
                        // Running on a normal machine
                        userAgent += process.version;
                    }
                    return userAgent += ")";
                } catch (e) {
                    return "RealmJS/Unknown"
                }
            },
        }));
    }

    // TODO: Remove this now useless object.
    var types = Object.freeze({
        'BOOL': 'bool',
        'INT': 'int',
        'FLOAT': 'float',
        'DOUBLE': 'double',
        'STRING': 'string',
        'DATE': 'date',
        'DATA': 'data',
        'OBJECT': 'object',
        'LIST': 'list',
    });
    Object.defineProperty(realmConstructor, 'Types', {
        get: function() {
            if (typeof console != 'undefined') {
                /* global console */
                /* eslint-disable no-console */
                var stack = new Error().stack.split("\n").slice(2).join("\n");
                var msg = '`Realm.Types` is deprecated! Please specify the type name as lowercase string instead!\n'+stack;
                if (console.warn != undefined) {
                    console.warn(msg);
                }
                else {
                    console.log(msg);
                }
                /* eslint-enable no-console */
            }
            return types;
        },
        configurable: true
    });
}