From 4636833f3dd8706fd7b66b7689ee644a4dca1940 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 19 Jan 2015 11:46:14 -0800 Subject: [PATCH] Fix module imports in Aphlict server Summary: This was broken in D11383. Basically, I had the `ws` module installed globally whilst testing, but the changes made do not work if the `ws` module is installed locally (i.e. in the `./support/aphlict/server/node_modules` directory). After poking around, it seems that this is due to the sandboxing that is done by `JX.require`. A quick fix is to just //not// use `JX.require`, although you may have a better idea? The error that is occurring is as follows: ``` <<< UNCAUGHT EXCEPTION! >>> Error: Cannot find module 'ws' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at extra.require (/usr/src/phabricator/webroot/rsrc/externals/javelin/core/init_node.js:48:16) at /usr/src/phabricator/support/aphlict/server/lib/AphlictClientServer.js:10:17 at Script.(anonymous function) [as runInNewContext] (vm.js:41:22) at Object.JX.require (/usr/src/phabricator/webroot/rsrc/externals/javelin/core/init_node.js:58:6) at Object. (/usr/src/phabricator/support/aphlict/server/aphlict_server.js:102:4) at Module._compile (module.js:456:26) >>> Server exited! ``` Test Plan: Now able to start the Aphlict server. Reviewers: joshuaspence Reviewed By: joshuaspence Subscribers: Korvin, epriestley Maniphest Tasks: T6987 Differential Revision: https://secure.phabricator.com/D11425 --- resources/celerity/map.php | 2 +- support/aphlict/server/aphlict_server.js | 14 +++++++------ .../aphlict/server/lib/AphlictAdminServer.js | 4 ++-- .../aphlict/server/lib/AphlictClientServer.js | 6 +++--- support/aphlict/server/lib/AphlictListener.js | 2 +- .../aphlict/server/lib/AphlictListenerList.js | 5 +++-- support/aphlict/server/lib/AphlictLog.js | 2 +- .../rsrc/externals/javelin/core/init_node.js | 20 ++++++------------- 8 files changed, 25 insertions(+), 30 deletions(-) diff --git a/resources/celerity/map.php b/resources/celerity/map.php index e850d81944..b4db8c453f 100644 --- a/resources/celerity/map.php +++ b/resources/celerity/map.php @@ -167,7 +167,7 @@ return array( 'rsrc/externals/javelin/core/__tests__/stratcom.js' => '88bf7313', 'rsrc/externals/javelin/core/__tests__/util.js' => 'e251703d', 'rsrc/externals/javelin/core/init.js' => '76e1fd61', - 'rsrc/externals/javelin/core/init_node.js' => 'd7dde471', + 'rsrc/externals/javelin/core/init_node.js' => '7212f324', 'rsrc/externals/javelin/core/install.js' => '05270951', 'rsrc/externals/javelin/core/util.js' => 'bdcfee9e', 'rsrc/externals/javelin/docs/Base.js' => '74676256', diff --git a/support/aphlict/server/aphlict_server.js b/support/aphlict/server/aphlict_server.js index 6b8795d41b..7d222d3b1c 100644 --- a/support/aphlict/server/aphlict_server.js +++ b/support/aphlict/server/aphlict_server.js @@ -4,9 +4,6 @@ var https = require('https'); var util = require('util'); var fs = require('fs'); -JX.require('lib/AphlictListenerList', __dirname); -JX.require('lib/AphlictLog', __dirname); - function parse_command_line_arguments(argv) { var config = { 'client-port': 22280, @@ -37,6 +34,8 @@ function parse_command_line_arguments(argv) { return config; } +require('./lib/AphlictLog'); + var debug = new JX.AphlictLog() .addConsole(console); @@ -71,6 +70,12 @@ try { 'documentation for instructions. ' + ex.toString()); } +// NOTE: Require these only after checking for the "ws" module, since they +// depend on it. + +require('./lib/AphlictAdminServer'); +require('./lib/AphlictClientServer'); + var ssl_config = { enabled: (config['ssl-key'] || config['ssl-cert']) }; @@ -94,9 +99,6 @@ if (config.test) { process.exit(0); } -JX.require('lib/AphlictAdminServer', __dirname); -JX.require('lib/AphlictClientServer', __dirname); - var server; if (ssl_config.enabled) { server = https.createServer({ diff --git a/support/aphlict/server/lib/AphlictAdminServer.js b/support/aphlict/server/lib/AphlictAdminServer.js index a36841dacd..0166475a55 100644 --- a/support/aphlict/server/lib/AphlictAdminServer.js +++ b/support/aphlict/server/lib/AphlictAdminServer.js @@ -1,6 +1,6 @@ -var JX = require('javelin').JX; +var JX = require('./javelin').JX; -JX.require('AphlictListenerList', __dirname); +require('./AphlictListenerList'); var http = require('http'); diff --git a/support/aphlict/server/lib/AphlictClientServer.js b/support/aphlict/server/lib/AphlictClientServer.js index b4fcbd7353..da139cc221 100644 --- a/support/aphlict/server/lib/AphlictClientServer.js +++ b/support/aphlict/server/lib/AphlictClientServer.js @@ -1,7 +1,7 @@ -var JX = require('javelin').JX; +var JX = require('./javelin').JX; -JX.require('AphlictListenerList', __dirname); -JX.require('AphlictLog', __dirname); +require('./AphlictListenerList'); +require('./AphlictLog'); var util = require('util'); var WebSocket = require('ws'); diff --git a/support/aphlict/server/lib/AphlictListener.js b/support/aphlict/server/lib/AphlictListener.js index d330274520..809a244a8e 100644 --- a/support/aphlict/server/lib/AphlictListener.js +++ b/support/aphlict/server/lib/AphlictListener.js @@ -1,4 +1,4 @@ -var JX = require('javelin').JX; +var JX = require('./javelin').JX; JX.install('AphlictListener', { construct: function(id, socket) { diff --git a/support/aphlict/server/lib/AphlictListenerList.js b/support/aphlict/server/lib/AphlictListenerList.js index 23e36a6285..9edefcdd5d 100644 --- a/support/aphlict/server/lib/AphlictListenerList.js +++ b/support/aphlict/server/lib/AphlictListenerList.js @@ -1,5 +1,6 @@ -var JX = require('javelin').JX; -JX.require('AphlictListener', __dirname); +var JX = require('./javelin').JX; + +require('./AphlictListener'); JX.install('AphlictListenerList', { construct: function() { diff --git a/support/aphlict/server/lib/AphlictLog.js b/support/aphlict/server/lib/AphlictLog.js index b9ee12459e..654f98f308 100644 --- a/support/aphlict/server/lib/AphlictLog.js +++ b/support/aphlict/server/lib/AphlictLog.js @@ -1,4 +1,4 @@ -var JX = require('javelin').JX; +var JX = require('./javelin').JX; var fs = require('fs'); var util = require('util'); diff --git a/webroot/rsrc/externals/javelin/core/init_node.js b/webroot/rsrc/externals/javelin/core/init_node.js index db32947302..cb53555ff3 100644 --- a/webroot/rsrc/externals/javelin/core/init_node.js +++ b/webroot/rsrc/externals/javelin/core/init_node.js @@ -23,9 +23,8 @@ JX.onload = function(func) { func(); }; -JX.require = function(thing, relative) { - relative = relative || __dirname + '/..'; - var path = relative + '/' + thing + '.js'; +JX.require = function(thing) { + var path = __dirname + '/../' + thing + '.js'; var content = fs.readFileSync(path); var dir = pathModule.dirname(path); @@ -37,17 +36,10 @@ JX.require = function(thing, relative) { } var extra = { - JX : this, - __DEV__ : 0, - window : {}, - __dirname : dir, - require : function(thing) { - if (thing == 'javelin') { - return require(dir + '/' + thing); - } else { - return require(thing); - } - } + JX: this, + __DEV__: 0, + window: {}, + __dirname: dir }; for (k in extra) {