2016-02-18 19:59:34 +00:00
////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////
2015-10-28 17:37:17 +00:00
2015-10-02 05:56:47 +00:00
'use strict' ;
2017-09-26 04:20:28 +00:00
const require _method = require ;
2016-12-22 12:20:19 +00:00
// Prevent React Native packager from seeing modules required with this
function nodeRequire ( module ) {
2017-09-26 04:20:28 +00:00
return require _method ( module ) ;
2016-10-04 22:02:51 +00:00
}
2016-12-22 12:20:19 +00:00
function getContext ( ) {
2017-01-06 13:30:32 +00:00
// If process is an object, we're probably running in Node or Electron
// From: http://stackoverflow.com/a/24279593/1417293
2016-12-22 12:20:19 +00:00
if ( typeof process === 'object' && process + '' === '[object process]' ) {
2018-01-16 09:11:30 +00:00
2017-05-29 06:33:06 +00:00
// Visual Studio Code defines the global.__debug__ object.
if ( typeof global !== 'undefined' && global . _ _debug _ _ ) {
return 'vscodedebugger' ;
}
2018-01-16 09:11:30 +00:00
2018-11-14 12:33:55 +00:00
return process . type === 'renderer' ? 'electron' : 'node.js' ;
2017-01-06 13:30:32 +00:00
}
// When running via Jest, the jest object is defined.
if ( typeof jest === 'object' ) {
2018-11-14 12:33:55 +00:00
return 'node.js' ;
2017-01-06 13:30:32 +00:00
}
2016-12-22 12:20:19 +00:00
2017-02-16 09:53:30 +00:00
if ( typeof navigator !== 'undefined' && navigator . product === 'ReactNative' ) { // eslint-disable-line no-undef
2017-03-15 10:21:16 +00:00
// If the navigator.userAgent contains the string "Chrome", we're likely
// running via the chrome debugger.
2018-01-16 09:11:30 +00:00
if ( typeof navigator !== 'undefined' &&
2017-03-15 10:21:16 +00:00
/Chrome/ . test ( navigator . userAgent ) ) { // eslint-disable-line no-undef
return 'chromedebugger' ;
}
2017-10-25 16:16:23 +00:00
// Check if its in remote js debugging mode
// https://stackoverflow.com/a/42839384/3090989
if ( typeof atob !== 'undefined' ) {
return 'chromedebugger' ;
}
2017-03-15 10:21:16 +00:00
// 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.
2017-02-16 09:53:30 +00:00
if ( typeof Realm === 'undefined' ) {
throw new Error ( 'Missing Realm constructor. Did you run "react-native link realm"? Please see https://realm.io/docs/react-native/latest/#missing-realm-constructor for troubleshooting' ) ;
2016-12-22 12:20:19 +00:00
}
2017-02-16 09:53:30 +00:00
return 'reactnative' ;
}
2016-12-22 12:20:19 +00:00
2018-01-16 09:11:30 +00:00
// If we're not running in React Native but we already injected the Realm class,
2017-02-16 09:53:30 +00:00
// we are probably running in a pure jscore environment
if ( typeof Realm !== 'undefined' ) {
2017-01-06 13:30:32 +00:00
return 'jscore' ;
2016-12-22 12:20:19 +00:00
}
2017-01-06 13:30:32 +00:00
// Visual Studio Code defines the global.__debug__ object.
if ( typeof global !== 'undefined' && global . _ _debug _ _ ) {
return 'vscodedebugger' ;
2016-12-22 12:20:19 +00:00
}
2017-01-06 13:30:32 +00:00
// Finally, if the navigator.userAgent contains the string "Chrome", we're likely
2017-03-15 10:21:16 +00:00
// running via the chrome debugger, even if navigator.product isn't set to "ReactNative"
2018-01-16 09:11:30 +00:00
if ( typeof navigator !== 'undefined' &&
2017-01-06 13:30:32 +00:00
/Chrome/ . test ( navigator . userAgent ) ) { // eslint-disable-line no-undef
return 'chromedebugger' ;
2016-12-22 12:20:19 +00:00
}
throw Error ( "Unknown execution context" ) ;
}
2016-02-29 05:40:27 +00:00
2018-11-19 09:45:58 +00:00
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"
}
}
2016-11-21 15:14:46 +00:00
var realmConstructor ;
2016-12-22 12:20:19 +00:00
2018-11-14 12:33:55 +00:00
const context = getContext ( ) ;
switch ( context ) {
case 'node.js' :
2016-12-22 12:20:19 +00:00
case 'electron' :
2018-11-14 12:33:55 +00:00
nodeRequire ( './submit-analytics' ) ( 'Run' , context ) ;
2016-12-22 12:20:19 +00:00
var binary = nodeRequire ( 'node-pre-gyp' ) ;
var path = nodeRequire ( 'path' ) ;
var pkg = path . resolve ( path . join ( _ _dirname , '../package.json' ) ) ;
var binding _path = binary . find ( pkg ) ;
2017-09-26 04:20:28 +00:00
realmConstructor = require _method ( binding _path ) . Realm ;
2016-12-22 12:20:19 +00:00
break ;
2018-01-16 09:11:30 +00:00
2016-12-22 12:20:19 +00:00
case 'reactnative' :
case 'jscore' :
2016-11-21 15:14:46 +00:00
realmConstructor = Realm ; // eslint-disable-line no-undef
2016-12-22 12:20:19 +00:00
break ;
case 'chromedebugger' :
case 'vscodedebugger' :
2016-11-21 15:14:46 +00:00
realmConstructor = require ( './browser' ) . default ; // (exported as ES6 module)
2016-12-22 12:20:19 +00:00
break ;
2016-11-21 15:14:46 +00:00
}
if ( ! realmConstructor ) {
2017-02-16 09:53:30 +00:00
throw Error ( "Error trying to establish execution context" ) ;
2015-10-02 05:56:47 +00:00
}
2016-02-29 05:40:27 +00:00
2016-10-10 22:45:01 +00:00
require ( './extensions' ) ( realmConstructor ) ;
2016-04-14 19:39:17 +00:00
2018-01-16 09:11:30 +00:00
if ( realmConstructor . Sync ) {
2018-11-14 12:33:55 +00:00
if ( context === 'node.js' ) {
2018-01-16 09:11:30 +00:00
nodeRequire ( './notifier' ) ( realmConstructor ) ;
2018-03-08 21:26:00 +00:00
if ( ! realmConstructor . Worker ) {
Object . defineProperty ( realmConstructor , 'Worker' , { value : nodeRequire ( './worker' ) } ) ;
}
2018-01-16 09:11:30 +00:00
}
2018-11-19 09:45:58 +00:00
realmConstructor . Sync . _initializeSyncManager ( createUserAgentDescription ( ) ) ;
2018-01-16 09:11:30 +00:00
}
2016-02-29 05:40:27 +00:00
module . exports = realmConstructor ;