Skip to content

Commit

Permalink
Merge pull request #80 from erjoalgo/master
Browse files Browse the repository at this point in the history
fixed Script.createContext undefined in newer versions of node
  • Loading branch information
jonnay committed Aug 4, 2015
2 parents 7fd60ca + dc8ce24 commit 8f90196
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
5 changes: 4 additions & 1 deletion swank-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ Remote.prototype.sendResult = function sendResult (id, values) {
};

function DefaultRemote () {
this.context = Script.createContext();
// https://github.com/jashkenas/coffeescript/issues/3498
// newer version of node uses vm.createContext(), so Script.createContext fails
this.context = (Script.createContext || vm.createContext)();

for (var i in global) this.context[i] = global[i];
this.context.module = module;
this.context.require = function(id, options) {
Expand Down
61 changes: 35 additions & 26 deletions swank.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,27 +272,7 @@ HttpListener.prototype.doProxyRequest = function doProxyRequest (targetUrl, requ
request.headers["host"] = hostname + (port == 80 ? "" : ":" + port);
delete request.headers["accept-encoding"]; // we don't want gzipped pages, do we?

// note on http client error handling:
// http://rentzsch.tumblr.com/post/664884799/node-js-handling-refused-http-client-connections
var proxy = http.createClient(port, hostname);
proxy.addListener(
'error', function handleError (e) {
console.log("proxy error: %s", e);
if (done)
return;
if (headersSent) {
response.end();
return;
}
response.writeHead(502, {'Content-Type': 'text/plain; charset=utf-8'});
response.end("swank-js: unable to forward the request");
});

console.log("PROXY: %s %s", request.method, request.url);
var proxyRequest = proxy.request(request.method, request.url, request.headers);

proxyRequest.addListener(
'response', function (proxyResponse) {
var onResponse = function (proxyResponse) {
var contentType = proxyResponse.headers["content-type"];
var statusCode = proxyResponse.statusCode;
console.log("==> status %s", statusCode);
Expand All @@ -309,7 +289,7 @@ HttpListener.prototype.doProxyRequest = function doProxyRequest (targetUrl, requ
response.writeHead(statusCode, headers);
headersSent = true;
}
proxyResponse.addListener(
proxyResponse.on(
'data', function (chunk) {
if (chunks !== null) {
chunks.push(chunk);
Expand All @@ -321,7 +301,7 @@ HttpListener.prototype.doProxyRequest = function doProxyRequest (targetUrl, requ
}
response.write(chunk, 'binary');
});
proxyResponse.addListener(
proxyResponse.on(
'end', function() {
if (chunks !== null) {
console.log("^^MOD: %s %s", request.method, request.url);
Expand All @@ -345,12 +325,41 @@ HttpListener.prototype.doProxyRequest = function doProxyRequest (targetUrl, requ
response.end();
done = true;
});
});
request.addListener(
};
// note on http client error handling:
// http://rentzsch.tumblr.com/post/664884799/node-js-handling-refused-http-client-connections

// this is deprecated
// var proxy = http.createClient(port, hostname);
var proxyRequest = http.request({
hostname:hostname,
port:port,
path:request.url,
method:request.method,
headers:request.headers,
}, onResponse);

proxyRequest.on(
'error', function handleError (e) {
console.log("proxy error: %s", e);
if (done)
return;
if (headersSent) {
response.end();
return;
}
response.writeHead(502, {'Content-Type': 'text/plain; charset=utf-8'});
response.end("swank-js: unable to forward the request");
});

console.log("PROXY: %s %s", request.method, request.url);
// var proxyRequest = proxy.request(request.method, request.url, request.headers);

proxyRequest.on(
'data', function(chunk) {
proxyRequest.write(chunk, 'binary');
});
request.addListener(
proxyRequest.on(
'end', function() {
proxyRequest.end();
});
Expand Down

0 comments on commit 8f90196

Please sign in to comment.