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:
Brandon Smith 2016-11-21 10:14:46 -05:00 committed by Kristian Dupont
parent df18d1f925
commit bcec963629
1 changed files with 23 additions and 11 deletions

View File

@ -22,26 +22,38 @@ function node_require(module) {
return require(module);
}
var realmConstructor;
function isNode() {
return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')
}
if (typeof Realm != 'undefined') {
// The global Realm constructor should be available on device (using JavaScriptCore).
realmConstructor = Realm; // eslint-disable-line no-undef
// eslint-disable-next-line
} else if (typeof window != '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')) {
// 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 binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
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
// eslint-disable-next-line
} else if (typeof window != '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
}
}
if (!realmConstructor) {
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
}