Permalink
Browse files

Merge pull request #51 from rksm/swank-connection-to-other-nodejs-ins…

…tance

Swank connection to other nodejs instance
  • Loading branch information...
2 parents 97ce552 + bf85069 commit c16581cece6ecf44d5418b40dc649da77c566702 @jonnay jonnay committed Oct 9, 2012
Showing with 66 additions and 7 deletions.
  1. +11 −1 README.md
  2. +20 −0 client/node.js
  3. +2 −0 client/stacktrace.js
  4. +28 −4 client/swank-js.js
  5. +2 −1 package.json
  6. +1 −0 user-agent-tests.js
  7. +2 −1 user-agent.js
View
@@ -298,7 +298,7 @@ the document URL for that page. When `node swank.js` is running embed
and you are ready to go!
-### Swank js as a bookmarklet ###
+### swank-js as a bookmarklet ###
You can bookmark
<a href="javascript:(function(d)%7Bwindow.swank_server%3D%27http%3A%2F%2Flocalhost%3A8009%2F%27%3Bif(!d.getElementById(%27swank-js-inj%27))%7Bvar%20h%3Dd.getElementsByTagName(%27head%27)%5B0%5D%2Cs%3Dd.createElement(%27script%27)%3Bs.id%3D%27swank-js-inj%27%3Bs.type%3D%27text%2Fjavascript%27%3Bs.src%3Dswank_server%2B%27swank-js%2Fswank-js-inject.js%27%3Bh.appendChild(s)%3B%7D%7D)(document)%3B">
@@ -308,6 +308,16 @@ href="javascript:(function()%7BSwankJS.disconnect()%3B%7D)()%3B">swank
disconnect</a>
links and use them on any page you'd like to play with.
+### swank-js from another node.js process ###
+
+When installed globally (`npm install -g swank-js`) you can connect another node.js process to the swank-js node process via
+
+```js
+require("swank-js/client/node").setupNodeJSClient();
+```
+
+Optionally you can pass in *swank_server_host* and *swank_server_port* as arguments to the `setupNodeJSClient` call.
+
Troubleshooting
---------------
View
@@ -0,0 +1,20 @@
+var SwankJS = require('./swank-js'),
+ SWANKJS_HOST = process.env.SWANKJS_HOST || "localhost",
+ SWANKJS_PORT = process.env.SWANKJS_PORT || 8009;
+
+SwankJS.setupNodeJSClient = function(host, port) {
+ var serverURL = 'http://' + (host || SWANKJS_HOST) + ':' + (port || SWANKJS_PORT);
+ this.setup(serverURL);
+}
+
+// making these objects available through eval for development purposes
+global.module = module;
+global.require = require;
+
+// if required by another module just export and leave start to the module
+if (module.parent) {
+ module.exports = SwankJS;
+} else {
+ // otherwise make the connection now
+ SwankJS.setupNodeJSClient();
+}
View
@@ -354,3 +354,5 @@ swank_printStackTrace.implementation.prototype = {
return '(?)';
}
};
+
+exports.swank_printStackTrace = swank_printStackTrace;
View
@@ -26,6 +26,21 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+var isNode = typeof module !== 'undefined' && module.exports;
+
+if (isNode) {
+ var io = require('socket.io-client'),
+ vm = require('vm');
+ global.SwankJS = SwankJS;
+ global.Completion = require('../Completion').Completion;
+ global.swank_printStackTrace = require('./stacktrace').swank_printStackTrace;
+ window = global;
+ window.navigator = {userAgent: "nodejs-swank-client"};
+ window.eval = function(code) {
+ return vm.runInThisContext(code, 'SwankJS node client');
+ }
+}
+
var SwankJS = {
socket: null,
connected: false,
@@ -85,8 +100,7 @@ SwankJS.makeSocketHandler = function makeSocketHandler (func) {
SwankJS.url = null;
SwankJS.setupSocket = function setupSocket (url) {
- if (url)
- this.url = url;
+ if (url) this.url = url;
this.socket = io.connect(this.url, { "force new connection": true });
var self = this;
this.socket.on(
@@ -164,13 +178,19 @@ SwankJS.setupSocket = function setupSocket (url) {
self.lastMessageTime = new Date().getTime();
self.evaluating = false;
}
- self.debug("result = %s", String(r));
+ var resultString;
+ try {
+ resultString = String(r);
+ } catch(e) {
+ resultString = "Error stringifying result: " + e;
+ }
+ self.debug("result = %s", resultString);
self.socket.send(
JSON.stringify(
{ "op": "result",
"id": m.id,
"error": null,
- "values": [String(r)]
+ "values": [resultString]
}
)
);
@@ -312,6 +332,10 @@ SwankJS.disconnect = function disconnect () {
}
};
+if (isNode) {
+ module.exports = SwankJS;
+}
+
/*
// we may need this later
View
@@ -23,7 +23,8 @@
"swank-js": "./swank.js"
},
"dependencies": {
- "socket.io": ">=0.8.7"
+ "socket.io": ">=0.9",
+ "socket.io-client": ">=0.9"
},
"scripts": {
"swank": "node ./swank.js"
View
@@ -76,6 +76,7 @@ var USER_AGENT_TESTS = [
[ "MSIE 2.0", "Mozilla/1.22 (compatible; MSIE 2.0d; Windows NT)" ],
[ "MSIE 1.5", "Mozilla/1.22 (compatible; MSIE 1.5; Windows NT)" ],
[ "MSIE 1.0", "Mozilla/1.0 (compatible; MSIE 1.0; Windows 3.11)" ], // made up
+ [ "nodejs/external", "nodejs-swank-client" ],
[ "unknown", "don't know" ],
[ "unknown", "" ]
];
View
@@ -39,7 +39,8 @@ var RX_TABLE = [
[ /^.*Qt\/(\d+\.\d+).*$/, "QtWeb $1" ],
[ /^.*WebKit.*$/, "WebKit" ],
[ /^.*Gecko.*$/, "Gecko" ],
- [ /^.*MSIE (\d+\.\d+).*$/, "MSIE $1" ]
+ [ /^.*MSIE (\d+\.\d+).*$/, "MSIE $1" ],
+ [ /^nodejs-swank-client$/, "nodejs/external" ]
];
exports.recognize = function recognize (name) {

0 comments on commit c16581c

Please sign in to comment.