Thursday, September 9, 2010

Fun with Simple Fab Apps (in v0.5)

‹prev | My Chain | next›

I got over the hump with the new version (0.5) of fab.js. Tonight I would like to sanity check myself with what Jed describes as the very simplest v0.5 (fab) apps.

Again tonight, I will be using a fairly simple skeleton (fab) application:
// Don't crash on errors
process.on('uncaughtException', function (err) {
console.log('Runaway exception: ' + err.stack);
});

with ( require('fab') )

( fab )

// Listen on the FAB port
( listen, 0xFAB )

(prepend_hello)
(simplest_app)
(prepend_hello)
()

();

function prepend_hello(write) {
return function read(body) {
return write(" hello ")(body);
};
}
The prepend_hello() (fab) app will prepend the text " hello " to the current stream, which ought to aid in seeing what is happening.

First up, the simplest possible app, which should pass the stream back downstream:
function simplest_app(write) {
return write;
}
When I access the server via curl, I see:
cstrom@whitefall:~$ curl http://localhost:4011/ -d Foo
hello hello
Ah, interesting. I would have expected the write to short-circuit the stream, ignoring the last prepend_hello. Instead it does nothing—connecting the two streams, making no changes.

Next, I replace the simplest_app with the second simplest app:
function second_simplest_app(write) {
return write( "foo" );
}
Accessing the server via curl, I see:
cstrom@whitefall:~$ curl http://localhost:4011/ -d Foo
hello foo hello
Given the behavior of the simplest app, the behavior of the second simplest app is to be expected.

Last up tonight, I wonder how I might grab downstream body data. Ultimately, I am stumped. I can obtain the request header info with:
function read_from_downstream(write) {
return write( function read(write, obj, hunh) {
if (obj.body) return write(obj.body);
return write(obj.url.pathname);
});
}
But I am stumped how to re-read from the downstream app so that I could access the request body. From curl, I get:
curl http://localhost:4011/ -d Foo 
hello / hello
Ultimately, I am not too concerned. Fab is going to be changing quite a bit in the upcoming days, so this will get sorted out.

Day #221

2 comments:

  1. chris,

    the three parameters of a request in order are write, head, and body, where body is a stream.

    function( write ) {
    return write( function( write, head, body ) {
    ...
    })
    }

    jed

    ReplyDelete
  2. Dammit! I *knew* that third argument was there and I knew it was a stream, but I didn't put 2 and 2 together. Gah!

    Sometimes I should stop exploring sooner rather than later.

    Thanks for the pointer and all the help. Really jazzed for the new (fab) goodness now!

    ReplyDelete