luapower/zlib

How to use this library on an incoming http stream

Opened this issue · 4 comments

I'm currently using https://github.com/brimworks/lua-zlib but would like to try & benchmark your library because it uses luajit's ffi. What I'd like to do is instantiate an inflate once and then pass it chunks as they come in. This my implementation building on lua-zlib:

local zlib = require "zlib"
local function onNewResponse()
  local inflate = zlib.inflate()
  local function onReceive(chunk) -- this function gets called whenever a new tcp chunk is received
    chunk = inflate(chunk)
    ...
  end
end

Could you help me figure out what the equivalent code would be? I've not been able to "resume" inflation with your API because I cannot call "next" on the tcp chunk reader, rather it calls the inflate.

capr commented

in these kinds of situations where you have two callback-based APIs that you must interleave, you can use a coroutine to invert the control of one of the APIs, i.e. transform it from callback-based to iterator-based (i.e. from push-style to pull-style).

so either wrap zlib.deflate() in a coroutine and call that as an iterator, or yield on onNewResponse and wrap your main loop in a coroutine if possible. Sorry I forgot to make a higher-level API similar to lua-zlib, but feel free to PR me if you want to code that yourself.

capr commented

yes, you can certainly extend the lib with a pull-style API, if you look at the code it's a while loop -- break that into a function with 3 branches (begin, next, finish) and you can get your inflate(s0) -> s API.

capr commented

OTOH don't dismiss coroutines outright, they are basically made for this purpose, so that you can write linear code instead of an explicit state-machine.