lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 20/11/2020 19:31, Pierre Chapuis wrote:

> It would be interesting if you supported ltn12 source and sinks.


I didn't do anything special to support ltn12, but at first glance it "just works":

--- Code:

local ltn12 = require('ltn12')
local mime = require('mime')
local buffet = require('buffet')
local buffet_resty = require('buffet.resty')

local src = ltn12.source.chain(
    ltn12.source.cat(
        ltn12.source.string('Lorem ipsum dolor sit amet, consectetur '),
        ltn12.source.string('adipiscing elit, sed do eiusmod tempor '),
        ltn12.source.string('incididunt ut labore et dolore magna aliqua.'),
        ltn12.source.error('boom!'),
        ltn12.source.string('unreachable')
    ),
    ltn12.filter.chain(
        function(chunk) return chunk and chunk:upper() end,
        mime.wrap('text', 16)
    )
)

local bf = buffet_resty.new(src)
repeat
    local data, err, partial = bf:receive('*l')
    print(data or partial)
    if err then print(err) end
until err

print(buffet.get_iterator_error(bf))

--- Output:

LOREM IPSUM DOLO
R SIT AMET, CONS
ECTETUR ADIPISCI
NG ELIT, SED DO 
EIUSMOD TEMPOR I
NCIDIDUNT UT LAB
ORE ET DOLORE MA
GNA ALIQUA.
closed
boom!