BR compress&decompress library, dependencies google's brotli project.
[TOC]
This library is product ready.
This Lua library use brotli do compress and decompress. User need download brotli and complie as dynamic library.
User need put libbrotlicommon.so.1、libbrotlidec.so、libbrotlienc.so in the lua_package_cpath directory.
local br = require("resty.ffi-br")
local ok, err
local enc, dec
enc, err = br.new_encoder(1024)
if not enc then
ngx.say("new encoder fail " .. err)
ngx.exit(200)
end
dec, err = br.new_decoder(1024)
if not dec then
ngx.say("new decoder fail " .. err)
ngx.exit(200)
end
local function gen_text(size)
local l = {}
for _ = 0, size do
local s = string.char(math.random(32, 126))
l[#l+1] = s
end
return table.concat(l)
end
local text_len = 4096
local text = gen_text(text_len)
local input, output, output_table
local function new_input(t)
local text = t
local text_len = #text
local count = 0
return function(size)
local start = count > 0 and size*count or 1
local finish = (size*(count+1) - 1)
count = count + 1
if start > text_len then
return nil, true
end
local data = text:sub(start, finish)
return data
end
end
input = new_input(text)
output_table = {}
output = function(data) output_table[#output_table+1] = data end
ok, err = enc:compressStream(input, output)
if not ok then
ngx.say("compress fail " .. err)
ngx.exit(200)
end
local compressed = table.concat(output_table, "")
input = new_input(compressed)
output_table = {}
ok, err = dec:decompressStream(input, output)
if not ok then
ngx.say("decompress fail " .. err)
ngx.exit(200)
end
local final = table.concat(output_table, "")
if final == text then
ngx.say("compress & decompress success")
else
ngx.say("compress & decompress fail")
end
ngx.say(string.format("ver enc: %s dec: %s", enc:version(), dec:version()))syntax:
local br = require "resty.ffi-br"
local enc, err = br.new_encoder(size, opts)create new br encode for compress.
- size: buffer size
- opts: options table, support
- quality: Brotli compression quality (compression) level. Acceptable values are in the range from 0 to 11.
- lgwin: Brotli window size. Acceptable values from 10 to 24, when lgwin 10 window size is 1k, when 24 window size is 16m.
syntax:
local ok, err = enc:compressStream(input, output)compress data from input function and call output function save compressed data.
syntax:
local br = require "resty.ffi-br"
local enc, err = br.new_decoder(size, opts)create new br decoder for decompress.
- size: buffer size
- opts: options table, support
- large_window: Flag that determines if "Large Window Brotli" is used.
- disable_ring_buffer_reallocation: Disable "canny" ring buffer allocation strategy. Ring buffer is allocated according to window size, despite the real size of the content.
syntax:
local ok, err = enc:decompressStream(input, output)decompress data from input function and call output function save decompressed data.