Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created February 5, 2012 22:29
Show Gist options
  • Save max-mapper/1748217 to your computer and use it in GitHub Desktop.
Save max-mapper/1748217 to your computer and use it in GitHub Desktop.
node bufferedstream proxy
var request = require('request');
var BufferedStream = require('morestreams').BufferedStream;
function requestHandler(req, res) {
// immediately pipe the incoming request into a paused bufferedstream.
// if you try to pipe after a nextTick it won't work because data starts
// arriving immediately
var bufferedStream = new BufferedStream
bufferedStream.pause()
req.pipe(bufferedStream)
before(req, res, function(err) {
// after the async stuff finishes then you can connect proxy
createProxy(req, res, bufferedStream)
})
}
// example function that might do some async stuff (e.g. validation, logging).
// you only need bufferedstream if you have to do async stuff between when a
// request comes in and when you want to .pipe() that request somewhere
function before(req, res, callback) {
process.nextTick(function() {
callback()
})
}
function createProxy(req, resp, bufferedStream) {
// req.pipe() copies the incoming http request properties onto the new
// request instance
var proxy = request()
req.pipe(proxy)
bufferedStream.pipe(proxy)
proxy.pipe(resp)
bufferedStream.resume()
}
@max-mapper
Copy link
Author

context: for the https://github.com/maxogden/datacouch project I run node inbetween the browser and couchdb.

when someone wants to save data into couchdb some JSON gets PUT through node, but I wanted to go grab that users permissions data (based on their cookie) to make sure that they are allowed to make modifications to the database that they were saving into. this permissions data lookup is an async call.

essentially the patterns demonstrated in this gist allow me to still keep everything as streaming as possible while I wait for an async call to finish before proxying the JSON on to couchdb

@max-mapper
Copy link
Author

also piping twice to the same response will result in you getting a warning that says You have already piped to this stream. Pipeing twice is likely to break the request but @mikeal is gonna fix request so that there is a method exposed that will let you clone the settings of an http request without having to use .pipe()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment