Workaround to load realm from ubuntu_server.js

This commit is contained in:
yenda 2018-11-29 17:03:48 +01:00
parent 07c3b1f722
commit edffdd3d36
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
1 changed files with 22 additions and 44 deletions

View File

@ -18,6 +18,13 @@
'use strict'; '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; const require_method = require;
// Prevent React Native packager from seeing modules required with this // 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 // If process is an object, we're probably running in Node or Electron
// From: http://stackoverflow.com/a/24279593/1417293 // From: http://stackoverflow.com/a/24279593/1417293
if (typeof process === 'object' && process + '' === '[object process]') { if (typeof process === 'object' && process + '' === '[object process]') {
// Visual Studio Code defines the global.__debug__ object. // Visual Studio Code defines the global.__debug__ object.
if (typeof global !== 'undefined' && global.__debug__) { if (typeof global !== 'undefined' && global.__debug__) {
return 'vscodedebugger'; return 'vscodedebugger';
} }
return process.type === 'renderer' ? 'electron' : 'node.js'; return process.type === 'renderer' ? 'electron' : 'nodejs';
} }
// When running via Jest, the jest object is defined. // When running via Jest, the jest object is defined.
if (typeof jest === 'object') { if (typeof jest === 'object') {
return 'node.js'; return 'nodejs';
} }
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { // eslint-disable-line no-undef if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { // eslint-disable-line no-undef
// If the navigator.userAgent contains the string "Chrome", we're likely // If the navigator.userAgent contains the string "Chrome", we're likely
// running via the chrome debugger. // running via the chrome debugger.
if (typeof navigator !== 'undefined' && if (typeof navigator !== 'undefined' &&
/Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef
return 'chromedebugger'; return 'chromedebugger';
} }
@ -57,6 +64,9 @@ function getContext() {
return 'chromedebugger'; return 'chromedebugger';
} }
// react-native-desktop expects "nodejs" env
return 'nodejs';
// Otherwise, we must be in a "normal" react native situation. // Otherwise, we must be in a "normal" react native situation.
// In that case, the Realm type should have been injected by the native code. // 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. // If it hasn't, the user likely forgot to run link.
@ -66,7 +76,7 @@ function getContext() {
return 'reactnative'; 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 // we are probably running in a pure jscore environment
if (typeof Realm !== 'undefined') { if (typeof Realm !== 'undefined') {
return 'jscore'; return 'jscore';
@ -79,7 +89,7 @@ function getContext() {
// Finally, if the navigator.userAgent contains the string "Chrome", we're likely // 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" // 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 /Chrome/.test(navigator.userAgent)) { // eslint-disable-line no-undef
return 'chromedebugger'; return 'chromedebugger';
} }
@ -87,35 +97,12 @@ function getContext() {
throw Error("Unknown execution context"); 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; var realmConstructor;
const context = getContext(); switch(getContext()) {
switch(context) { case 'nodejs':
case 'node.js':
case 'electron': case 'electron':
nodeRequire('./submit-analytics')('Run', context); nodeRequire('./submit-analytics')('Run');
var binary = nodeRequire('node-pre-gyp'); var binary = nodeRequire('node-pre-gyp');
var path = nodeRequire('path'); var path = nodeRequire('path');
@ -124,7 +111,7 @@ switch(context) {
realmConstructor = require_method(binding_path).Realm; realmConstructor = require_method(binding_path).Realm;
break; break;
case 'reactnative': case 'reactnative':
case 'jscore': case 'jscore':
realmConstructor = Realm; // eslint-disable-line no-undef realmConstructor = Realm; // eslint-disable-line no-undef
@ -142,14 +129,5 @@ if (!realmConstructor) {
require('./extensions')(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; module.exports = realmConstructor;
}