Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
var app = function(request) { return function(responder) { var w = responder(200, headers); event.readSomething(function(contents) { w.write(JSON.stringify(contents)); w.close(); }); }; } var middleware = header_middleware(app, function play_with_headers(res) { res.headers['last-modified'] = new Date(); }); var header_middleware(app,adjuster) { return function(request) { var r = app(requrest); if (typeof r == "function") { // Async return function(responder) { var appResponderProxy = function(status_, headers_) { var x = {status: status_, headers: headers_}; adjuster(x); // Return the writer to the app after adjusting headers return responder(x.status,x.headers) }; // Call the app async callback with our proxy responder fn return r(appResponderProxy); }; } else { adjuster(r); return r; } } } var body_middleware(app,adjuster) { return funciton(request) { var r = app(request); if (typeof r == "function") { return funciton(responder) { var appResponderProxy = function(s,h) { var w = responder(s,h), w_proxy = Object.create({}, w); w_proxy.write = function(chunk) { return w.write(adjuster(chunk)); } // Return the proxy object to the app return w_proxy; }; // Call the app async callback with our proxy responder fn return r(appResponderProxy); } } else { // Might more efficent ways of handling this but it should work r.body = r.body.map(adjuster); return r; } } }
This paste will be private.
From the Design Piracy series on my blog: