Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for schema, bug fix for port #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions jsgi-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
JSGI 0.3 Adapter for Node

To use provide a JSGI application (can be application stack) to the start
To use provide a JSGI application (can be application stack) to the start
function:

require("jsgi-node").start(function(request){
Expand All @@ -14,7 +14,7 @@ function:
});
});

This adapter should conform to the JSGI 0.3 (with promises) for full
This adapter should conform to the JSGI 0.3 (with promises) for full
asynchronous support. For example:

var fs = require("promised-io/fs");
Expand All @@ -37,6 +37,7 @@ var
function Request( request ) {
var url = request.url;
var questionIndex = url.indexOf("?");
this._env = {};
this.method = request.method;
this.nodeRequest = request;
this.headers = request.headers;
Expand All @@ -51,7 +52,7 @@ function Request( request ) {
if(this.method != "GET"){ // optimize GET
this.body = new Input( request );
}

}

Request.prototype = {
Expand All @@ -70,14 +71,17 @@ Request.prototype = {
return this._env || (this._env = {});
},
scriptName: "",
scheme:"http",
get scheme(){
return this.nodeRequest.connection.encrypted ? "https" : "http";
},
get host(){
var host = this.headers.host;
return host ? host.split(":")[0] : "";
},
get port(){
var host = this.headers.host;
return host ? (host.split(":")[1] || 80) : 80;
var isSsl = !!this.nodeRequest.connection.encrypted;
return host ? (host.split(":")[1] || (isSsl ? 443 : 80)) : 80;
},
get remoteAddr(){
return this.nodeRequest.connection.remoteAddress;
Expand All @@ -89,11 +93,11 @@ Request.prototype = {


function Input( request ) {
var
var
inputBuffer = [],
waitingForLength = Infinity;
function callback(data){
inputBuffer.push(data);
inputBuffer.push(data);
}
var deferred = defer();
request
Expand All @@ -103,7 +107,7 @@ function Input( request ) {
.addListener( "end", function() {
deferred.resolve();
});

this.forEach = function (each) {
if (this.encoding) {
request.setBodyEncoding( this.encoding );
Expand Down Expand Up @@ -160,6 +164,7 @@ function Response( response, stream ) {
try {
if ( typeof data.body === "string" ) {
response.write(data.body);
response.end();
}
else if ( typeof data.body.forEach !== "function" ) {
throw new Error("The body does not have a forEach function");
Expand Down Expand Up @@ -208,7 +213,7 @@ function Response( response, stream ) {
}catch(e3){
sys.puts(e3.stack);
}
}
}
}
}

Expand All @@ -226,7 +231,7 @@ function Listener( app ) {
jsgiResponse = app( request )
} catch( error ) {
jsgiResponse = { status:500, headers:{}, body:[error.stack] };
}
}
respond( jsgiResponse );
});
}
Expand All @@ -238,17 +243,17 @@ start.start = start;
function start( app, options ) {
app = new Listener( app );
options = options || {};

var port = options.port || 8080,
http;

if ( options.ssl ) {
http = require( "https" ).createServer( options.ssl, app ).listen( port );
} else {
http = require( "http" ).createServer( app ).listen( port );
}

sys.puts( "Server running on port " + port );
return http;
return http;
};
module.exports = start;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsgi-node",
"version": "0.3.0",
"name": "jsgi",
"version": "1.0.1",
"directories": { "lib": "." },
"main": "./jsgi-node",
"description": "JSGI middleware server for NodeJS",
Expand Down