Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
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
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
*.pyc

# Logs and databases #
######################
*.log

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db
12 changes: 6 additions & 6 deletions concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ module.exports = function( exports ) {
return exports( function( write ) {
var buffer = ""
, types = { String: true, Buffer: true };

return function read( body, head ) {
// TODO: add timeout to flush?

if ( head && head.status >= 400 ) {
write( body, head )();
return function x(){ return x };
}

else if ( body && types[ body.constructor.name ] ) buffer += body;

else {
if ( buffer ) write = write( buffer );

buffer = "";
write = write.apply( undefined, arguments );
}

return arguments.length ? read : write;
}
});
Expand Down
20 changes: 10 additions & 10 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ module.exports = function( exports, imports ) {
// this is blank because we don't need to give run a downstream function,
// since we're not piping anything to stdout.
()

// this fires up a listener on port 4011
( node$listen, 0xFAB )

// let's route for static files. route is an app that takes two streams,
// one for matches and one for non-matches.
( route, /^\/static/ )
// we matched!

// the fs module takes one stream: the path name. it's just middleware
// that converts an upstream path name to the contents of the file.
( node$fs )
// first, get the current directory
( __dirname )

// then sleep for 500, just because we can
( sleep, 500 )

// then append the pathname
( head.url.pathname )
()
()
// we didn't match the /static url, so we keep going.

// now let's return the front page
( route, /^\/hello/ )
// since we've with'ed the html app above, each uppercase element
Expand All @@ -69,23 +69,23 @@ module.exports = function( exports, imports ) {
( HTML )
( BODY, { id: "show" } )
( "Hello, " )

( EM )
// this pattern looks for a url that ends with an alphanumeric string
( route, /^\/(\w+)$/ )
// looks like a path was provided! here, route.capture outputs
// the captured path segment at the given index.
( route.capture, 0 )
()
// we didn't match, so let's output a generic hello.
// we didn't match, so let's output a generic hello.
( "world" )
()

( "!" )
()
()
()

// this is a catchall 404 for any paths that haven't matched.
( "Not found.", { status: 404 } )
();
Expand Down
8 changes: 4 additions & 4 deletions head.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function( exports ) {
url: "href protocol host auth hostname port pathname search query hash capture",
headers: "accept acceptCharset acceptEncoding acceptLanguage acceptRanges authorization cacheControl connection cookie contentLength contentType date expect from host ifMatch ifModifiedSince ifNoneMatch ifRange ifUnmodifiedSince maxForwards pragma proxyAuthorization range referer te upgrade userAgent via warning"
};

for ( prop in props ) {
var subprops = props[ prop ].split( " " );

Expand All @@ -14,12 +14,12 @@ module.exports = function( exports ) {
head[ prop ][ subprop ] = getter( "head." + prop + "." + subprop );
}
}

return exports( head );

function writer( val ) {
return new Function( "write", "head", "return write(" + val + ")" );
}

function getter( name ){ return writer( writer( name ) ) }
function getter( name ){ return writer( writer( name ) ) }
};
24 changes: 12 additions & 12 deletions html.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,56 @@ module.exports = function( exports ) {
SOURCE WBR".split( " " )

];

for ( var isVoid = 0, names; names = tags[ isVoid ]; isVoid++ ) {
for ( var i = 0, name; name = names[ i++ ]; ) {
elem[ name ] = elem( name.toLowerCase(), !!isVoid );
}
}

elem.DOCTYPE = function( write, dec ) {
return write( "<!DOCTYPE " )( dec )( ">\n" );
}

elem.COMMENT = function( write ) {
write = write( "<!-- " );

return function read( obj ) {
if ( !arguments.length ) return write( " -->" );
write = write( obj );
return read;
return read;
}
}

return exports( elem );

function elem( name, isVoid ) {
return function( write, obj ) {
write = write( "<" + name );
write = attrs( write )( obj )( ">" );

if ( isVoid ) return write;

return function read( arg ) {
if ( !arguments.length ) return write( "</" + name + ">" );

write = write.apply( undefined, arguments );
return read;
};
}
}

function attrs( write ) {
return function read( obj ) {
for ( var name in obj ) {
write = write( " " )( name )( "=" );
write = attr( write )( obj[ name ] );
}

return write;
}
}

function attr( write ) {
return function read( value ) {
return write( "\"" )( value )( "\"" );
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ function imports( exports ) {
: exports.toString()
.split( /[^\w$]+/, exports.length + !!exports.name + 1 )
.slice( !!exports.name + 1 );

( function loop() {
var name = names.shift();

if ( !name ) exports.apply( undefined, libs );

else {
console.log( name.replace( /\W/g, "/" ), name.replace( /\W/g, "/" ) )

require( name.replace( /\W/g, "/" ) )( function( lib ) {
libs.push( lib );
loop();
Expand Down
2 changes: 1 addition & 1 deletion log.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function( exports ) {
console.log( msg );
return write;
}

return function read( body ) {
console.log( body );
write = write.apply( undefined, arguments );
Expand Down
4 changes: 2 additions & 2 deletions method.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function( exports, imports ) {
return imports( function( queue ) {
var names = "GET PUT POST DELETE HEAD".split( " " );

function method( write, name ) {
return queue( function( yes ) {
return queue( function( no ) {
Expand All @@ -11,7 +11,7 @@ module.exports = function( exports, imports ) {
});
});
}

for ( var i = names.length; i--; ) ( function( name ) {
method[ name ] = function( write ) {
return method( write, name );
Expand Down
8 changes: 4 additions & 4 deletions node/addContentLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ module.exports = function( exports, imports ) {
return write( function( write, head, body ) {
var buffered = queue()
, length = 0;

return upstream( render( function read( body, head ) {
buffered = buffered.apply( this, arguments );

if ( !arguments.length ) return buffered(
write( undefined, { headers: { "content-length": length } } )
);

if ( body ) length += body.length;

return read;
}, head, body ), head, body );
})();
Expand Down
2 changes: 1 addition & 1 deletion node/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function( exports, imports ) {
.on( "error", function(){ rest(
write( "File not found: " + path, { status: 404 } ), head, body
)});

return ignore;
}, head, body ), head, body), head, body );
});
Expand Down
4 changes: 2 additions & 2 deletions node/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module.exports = function( exports, imports ) {
server = http.createServer();
server.listen( where );
}

return queue( function( upstream ) {
upstream( listener( function( listener ) {
server.on( "request", listener );
}))

return upstream( write );
});
});
Expand Down
18 changes: 9 additions & 9 deletions node/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,45 @@ module.exports = function( exports, imports ) {
function listener( req, res ) {
var status = 200
, headers = {};

upstream( render(
read,

{
method: req.method,
headers: req.headers,
url: url.parse( "//" + req.headers.host + req.url, false, true )
},

function body( write ) {
req.on( "data", write ).on( "end", write );
}
));

function read( body, head ) {
if ( !arguments.length ) res.end();

else {
if ( head ) {
if ( "status" in head ) status = head.status;

if ( "headers" in head ) {
for ( var name in head.headers ) {
headers[ name ] = head.headers[ name ]
}
}
}

if ( body ) {
if ( headers ) {
res.writeHead( status, headers );
headers = undefined;
}

res.write( body );
}
}

return read;
}
}
Expand Down
12 changes: 6 additions & 6 deletions queue.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
module.exports = function( exports ) {
return exports( function( write, queue ) {
queue = queue || [];

var length = queue.length;
function drain( write, req ) {

function drain( write, req ) {
for ( var i = 0; i < length; ) {
write = write.apply( undefined, queue[ i++ ] );
}

return write();
};

return function read() {
if ( !arguments.length ) return write ? write( drain ) : drain;

queue[ length++ ] = arguments;
return read;
}
Expand Down
Loading