lua-resty-brotli

OpenResty/LuaJIT 的 Brotli Lua 绑定

$ opm get sjnam/lua-resty-brotli

名称

lua-resty-brotli - 使用 FFI 的 LuaJIT 的 Google Brotli 绑定。

状态

此库仍处于实验阶段,并且正在早期开发中。

安装

要安装 lua-resty-brotli,您需要首先安装带有共享库的 Brotli。然后,您可以通过将 lib/resty/brotli.lua 放置到您的 Lua 库路径中来安装 lua-resty-brotli

示例

简单用法 `` lua local brotlienc = require "resty.brotli.encoder" local brotlidec = require "resty.brotli.decoder"

local encoder = brotlienc:new() local decoder = brotlidec:new()

local txt = string.rep("ABCD", 1000) print("未压缩大小:", #txt) local c, err = encoder:compress(txt) print("压缩大小:", #c) local txt2, err = decoder:decompress(c) assert(txt == txt2) ``

在使用 lua-nginx-module 的 nginx 中

以下 nginx.conf 示例中,文档根目录 html/brotli 仅包含使用“.br”文件名扩展名的预压缩文件,而不是常规文件。 `` lua

静态内容

location /brotli { root html;

    rewrite_by_lua_block {
       local brotlidec = require "resty.brotli.decoder"
       local brotli_ok = false
       local header = ngx.var.http_accept_encoding
       if header then
          if string.find(header, "br") then
             brotli_ok = true
          end
       end
       if not brotli_ok then
          ngx.ctx.decoder = brotlidec:new()
       end
       ngx.ctx.brotli_ok = brotli_ok
       ngx.req.set_uri(ngx.var.uri..".br")
    }

    header_filter_by_lua_block {
       ngx.header["Vary"] = "Accept-Encoding"                
       if not ngx.ctx.bro_ok then
          ngx.header.content_length = nil
       else
          ngx.header["Content-Encoding"] = "br"
       end
    }

    body_filter_by_lua_block {
       if ngx.ctx.brotli_ok then
          return
       end

       local decoder = ngx.ctx.decoder
       local stream = decoder:decompressStream(ngx.arg[1])
       ngx.arg[1] = stream
       if decoder:resultSuccess() then
          decoder:destroy()
          ngx.arg[2] = true
       end
    }
}

动态内容

location /hello { content_by_lua_block { local name = ngx.var.arg_name or "world" local msg = string.rep("Hello,"..name.." ", 100) msg = string.rep(msg.."\n", 10000) ngx.header["Content-Length"] = #msg ngx.print(msg) }

    header_filter_by_lua_block {
        local brotlienc = require "resty.brotli.encoder"
        local brotli_ok = false
        local header = ngx.var.http_accept_encoding
        if header then
           if string.find(header, "br") then
              brotli_ok = true
           end
        end
        ngx.ctx.brotli_ok = brotli_ok
        ngx.header["Vary"] = "Accept-Encoding"
        if brotli_ok then
           ngx.header.content_length = nil
           ngx.header["Content-Encoding"] = "br"
           ngx.ctx.encoder = brotlienc:new()
        end
    }

    body_filter_by_lua_block {
        if not ngx.ctx.brotli_ok then
           return
        end                
        local encoder = ngx.ctx.encoder
        ngx.arg[1] = encoder:compressStream(ngx.arg[1])        
        if encoder:isFinished() then
           encoder:destroy()
           ngx.arg[2] = true
        end
    }
}
C<>``

方法

new

语法:encoder, err = brotlienc:new(options?)

语法:decoder, err = brotlidec:new()

创建 Brotli 编码器和解码器。

options 参数是包含以下键的 Lua 表

  • quality

    设置 Brotli 质量(压缩)级别。可接受的值范围为 011。(默认为 11)

  • lgwin

    设置 Brotli 窗口大小。窗口大小为 (1 << lgwin) - 16

  • mode

    压缩模式可以是 BROTLI_MODE_GENERIC(0,默认)、BROTLI_MODE_TEXT(1,用于 UTF-8 格式文本输入)或 BROTLI_MODE_FONT(2,用于 WOFF 2.0)。

destroy

  • 语法:encoder:destroy()

    反初始化并释放 BrotliEncoderState 实例。

  • 语法:decoder:destroy()

    反初始化并释放 BrotliDecoderState 实例。

compress

语法:encoded_buffer, err = encoder:compress(input_buffer)

将 input_buffer 中的数据压缩到 encoded_buffer 中。

compressStream

语法:buffer = encoder:compressStream(stream)

将输入流压缩到输出缓冲区。

decompress

语法:decoded_buffer, err = decoder:decompress(encoded_buffer)

将 encoded_buffer 中的数据解压缩到 decoded_buffer 中。

decompressStream

语法:buffer = decoder:decompressStream(encoded_buffer)

将 encoded_buffer 中的数据解压缩到缓冲区流中

作者

Soojin Nam (sjnam)

许可证

mit

依赖项

luajit

版本