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
34
lib/index.js
34
lib/index.js
|
@ -22,26 +22,38 @@ function node_require(module) {
|
||||||
return require(module);
|
return require(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
var realmConstructor;
|
function isNode() {
|
||||||
|
return typeof process == 'object' && (('' + process) == '[object process]' || typeof jest == 'object')
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof Realm != 'undefined') {
|
// function isElectronRenderer() {
|
||||||
// The global Realm constructor should be available on device (using JavaScriptCore).
|
// return isNode() && process.type === 'renderer'
|
||||||
realmConstructor = Realm; // eslint-disable-line no-undef
|
// }
|
||||||
// eslint-disable-next-line
|
|
||||||
} else if (typeof window != 'undefined') {
|
var realmConstructor;
|
||||||
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
|
if (isNode()) {
|
||||||
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.
|
// If process is defined, we're running in node.
|
||||||
require('./submit-analytics')('Run');
|
require('./submit-analytics')('Run');
|
||||||
|
|
||||||
// Prevent React Native packager from seeing this module.
|
// Prevent React Native packager from seeing this module.
|
||||||
var binary = node_require('node-pre-gyp');
|
var binary = node_require('node-pre-gyp');
|
||||||
var path = node_require('path');
|
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;
|
realmConstructor = require(binding_path).Realm;
|
||||||
} else {
|
} 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!');
|
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue