From edffdd3d36f42e9ade787cad73b707a7658ae44a Mon Sep 17 00:00:00 2001 From: yenda Date: Thu, 29 Nov 2018 17:03:48 +0100 Subject: [PATCH] Workaround to load realm from ubuntu_server.js --- lib/index.js | 66 ++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/lib/index.js b/lib/index.js index bc3c307c..eff358c4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,6 +18,13 @@ 'use strict'; +console.log("outerRealmConstructor: " + typeof outerRealmConstructor); +if (typeof outerRealmConstructor !== 'undefined') { + var realmConstructor = outerRealmConstructor; + module.exports = realmConstructor; +} + +if (typeof outerRealmConstructor === 'undefined') { const require_method = require; // Prevent React Native packager from seeing modules required with this @@ -29,24 +36,24 @@ function getContext() { // If process is an object, we're probably running in Node or Electron // From: http://stackoverflow.com/a/24279593/1417293 if (typeof process === 'object' && process + '' === '[object process]') { - + // Visual Studio Code defines the global.__debug__ object. if (typeof global !== 'undefined' && global.__debug__) { return 'vscodedebugger'; } - - return process.type === 'renderer' ? 'electron' : 'node.js'; + + return process.type === 'renderer' ? 'electron' : 'nodejs'; } // When running via Jest, the jest object is defined. if (typeof jest === 'object') { - return 'node.js'; + return 'nodejs'; } if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { // eslint-disable-line no-undef // If the navigator.userAgent contains the string "Chrome", we're likely // running via the chrome debugger. - if (typeof navigator !== 'undefined' && + if (typeof navigator !== 'undefined' && /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef return 'chromedebugger'; } @@ -57,6 +64,9 @@ function getContext() { return 'chromedebugger'; } + // react-native-desktop expects "nodejs" env + return 'nodejs'; + // Otherwise, we must be in a "normal" react native situation. // In that case, the Realm type should have been injected by the native code. // If it hasn't, the user likely forgot to run link. @@ -66,7 +76,7 @@ function getContext() { return 'reactnative'; } - // If we're not running in React Native but we already injected the Realm class, + // If we're not running in React Native but we already injected the Realm class, // we are probably running in a pure jscore environment if (typeof Realm !== 'undefined') { return 'jscore'; @@ -79,7 +89,7 @@ function getContext() { // Finally, if the navigator.userAgent contains the string "Chrome", we're likely // running via the chrome debugger, even if navigator.product isn't set to "ReactNative" - if (typeof navigator !== 'undefined' && + if (typeof navigator !== 'undefined' && /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef return 'chromedebugger'; } @@ -87,35 +97,12 @@ function getContext() { throw Error("Unknown execution context"); } -function 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/"; - var context = getContext(); - 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" - } -} - - var realmConstructor; -const context = getContext(); -switch(context) { - case 'node.js': +switch(getContext()) { + case 'nodejs': case 'electron': - nodeRequire('./submit-analytics')('Run', context); + nodeRequire('./submit-analytics')('Run'); var binary = nodeRequire('node-pre-gyp'); var path = nodeRequire('path'); @@ -124,7 +111,7 @@ switch(context) { realmConstructor = require_method(binding_path).Realm; break; - + case 'reactnative': case 'jscore': realmConstructor = Realm; // eslint-disable-line no-undef @@ -142,14 +129,5 @@ if (!realmConstructor) { require('./extensions')(realmConstructor); -if (realmConstructor.Sync) { - if (context === 'node.js') { - nodeRequire('./notifier')(realmConstructor); - if (!realmConstructor.Worker) { - Object.defineProperty(realmConstructor, 'Worker', {value: nodeRequire('./worker')}); - } - } - realmConstructor.Sync._initializeSyncManager(createUserAgentDescription()); -} - module.exports = realmConstructor; +}