From bcec9636292bbbd0e793ee1e188251bc689d638f Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Mon, 21 Nov 2016 10:14:46 -0500 Subject: [PATCH] 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. --- lib/index.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index b176d768..7c2273a8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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!'); }