Here is some older material on pipeline coding in Lua you might be interested in that describes this problem in terms of Filters, Sources, and Sinks: http://lua-users.org/wiki/FiltersSourcesAndSinks
LuaSocket implementation of the above: http://w3.impa.br/~diego/software/luasocket/ltn12.html