Support running realm-js in Electron renderer (#667)
Electron is a combination of a node and Chromium processes. Chromium processes can have node bindings enabled and can therefore run most npm modules seamlessly. The current `lib/index.js` bootstrapping logic results in a ReferenceError on the following statement: typeof Realm != 'undefined' Thus, checking whether running under node first allows realm-js to run inside the Electron renderer (Chromium) process.
This commit is contained in:
parent
df18d1f925
commit
bcec963629
32
lib/index.js
32
lib/index.js
|
@ -22,8 +22,26 @@ function node_require(module) {
|
|||
return require(module);
|
||||
}
|
||||
|
||||
var realmConstructor;
|
||||
function isNode() {
|
||||
return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')
|
||||
}
|
||||
|
||||
// function isElectronRenderer() {
|
||||
// return isNode() && process.type === 'renderer'
|
||||
// }
|
||||
|
||||
var realmConstructor;
|
||||
if (isNode()) {
|
||||
// If process is defined, we're running in node.
|
||||
require('./submit-analytics')('Run');
|
||||
|
||||
// Prevent React Native packager from seeing this module.
|
||||
var binary = node_require('node-pre-gyp');
|
||||
var path = node_require('path');
|
||||
var pkg = path.resolve(path.join(__dirname,'../package.json'));
|
||||
var binding_path = binary.find(pkg);
|
||||
realmConstructor = require(binding_path).Realm;
|
||||
} else {
|
||||
if (typeof Realm != 'undefined') {
|
||||
// The global Realm constructor should be available on device (using JavaScriptCore).
|
||||
realmConstructor = Realm; // eslint-disable-line no-undef
|
||||
|
@ -32,16 +50,10 @@ if (typeof Realm != 'undefined') {
|
|||
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
|
||||
realmConstructor = require('./browser').default; // (exported as ES6 module)
|
||||
// eslint-disable-next-line
|
||||
} else if (typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')) {
|
||||
// If process is defined, we're running in node.
|
||||
require('./submit-analytics')('Run');
|
||||
}
|
||||
}
|
||||
|
||||
// Prevent React Native packager from seeing this module.
|
||||
var binary = node_require('node-pre-gyp');
|
||||
var path = node_require('path');
|
||||
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
|
||||
realmConstructor = require(binding_path).Realm;
|
||||
} else {
|
||||
if (!realmConstructor) {
|
||||
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue