Wednesday 10 February 2016

klobb: an experimental server abstraction that focuses on simplicity and immutability that i started

source: http://ift.tt/1LhHWa1 central idea I'm experimenting with is once of abstraction.In Node, the root abstraction that we build on top of looks like this:http.createServer((req, res) => { // mutate res and req res.end(); }).listen(3000); You get mutable references req and res, you mutate them at the same time, and then you tell node to flush the response.With klobb, my goal is to wrap this with a functional abstraction to see if I can recreate some pleasure of modeling the request/response cycle in a functional language like Clojure:Requests and Responses are immutable (http://ift.tt/1NYi5HD) records.Handlers are functions Request -> ResponseMiddleware are higher-order functions Handler -> Handler.function serve(handler) { http.createServer((nreq, nres) => { // Map the Node request onto an immutable request const request = Request.fromNode(nreq); // All of your logic takes place here (handler) with immutable records const response = await handler(request); // Map the immutable response back on to the Node response Response.send(response, res); }).listen(3000); } For example, if you want to update a cookie, instead of calling res.cookies.set(key, val) (mutation), you would instead use basic data manipulation to return a response where the response.cookies (Map) has all of the cookies you want to send the client. In the outer middleware, klobb syncs your cookie map onto the underlying Node request (Set-Cookie headers).async function handler(request) { const counter = 1 + (Cookie.get('counter', request) || 0); return Response.ok(`You've viewed this page ${counter} times`) .tap(Cookie.set('counter', counter)) .tap(Cookie.set('session_id', 'xxxxxx')) .setHeader('X-Whatever', 'Some Value') } So far it seems the main challenge is user-experience. Immutable.js provides a rich API for manipulating data, but I'm definitely going to have to put thought into reducing boilerplate without too much cheating.Also, this is my first time using Babel. I had originally written klobb with http://ift.tt/1wKTmyL (simulating await with yield), but I stumbled upon http://ift.tt/23yH78e (another http tool) and rewrote klobb to use their Babel scaffolding, something I still struggle to understand and debug.Anyways, the project is really early and experimental, taking most of its inspiration from Clojure's ring abstraction which was so simple that it blew my mind back when I started working with it.I would love to hear any sort of feedback or ideas. I've been churning out middleware (http://ift.tt/1o3ZhhZ) to try to rapidly get to a place where I can tell where the pain points are.I've also got a weak hello-world app on heroku (http://ift.tt/1LhHWa5) that shows some of the middleware I've got working so far like serving static assets, cookies, and not-modified headers. nothin exciting yet.

Submitted February 10, 2016 at 08:56PM by danneu

No comments:

Post a Comment