Skip to content

Commit

Permalink
refactor(tools): separate gzip functions from tools.utils (#11875)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Oct 31, 2023
1 parent 47ff7da commit a8de91a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 51 deletions.
1 change: 1 addition & 0 deletions kong-3.6.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ build = {
["kong.tools.protobuf"] = "kong/tools/protobuf.lua",
["kong.tools.mime_type"] = "kong/tools/mime_type.lua",
["kong.tools.request_aware_table"] = "kong/tools/request_aware_table.lua",
["kong.tools.gzip"] = "kong/tools/gzip.lua",
["kong.tools.string"] = "kong/tools/string.lua",
["kong.tools.table"] = "kong/tools/table.lua",
["kong.tools.sha256"] = "kong/tools/sha256.lua",
Expand Down
62 changes: 62 additions & 0 deletions kong/tools/gzip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
local buffer = require "string.buffer"
local zlib = require "ffi-zlib"


local inflate_gzip = zlib.inflateGzip
local deflate_gzip = zlib.deflateGzip


local _M = {}


-- lua-ffi-zlib allocated buffer of length +1,
-- so use 64KB - 1 instead
local GZIP_CHUNK_SIZE = 65535


local function read_input_buffer(input_buffer)
return function(size)
local data = input_buffer:get(size)
return data ~= "" and data or nil
end
end


local function write_output_buffer(output_buffer)
return function(data)
return output_buffer:put(data)
end
end


local function gzip_helper(inflate_or_deflate, input)
local input_buffer = buffer.new(0):set(input)
local output_buffer = buffer.new()
local ok, err = inflate_or_deflate(read_input_buffer(input_buffer),
write_output_buffer(output_buffer),
GZIP_CHUNK_SIZE)
if not ok then
return nil, err
end

return output_buffer:get()
end


--- Gzip compress the content of a string
-- @tparam string str the uncompressed string
-- @return gz (string) of the compressed content, or nil, err to if an error occurs
function _M.deflate_gzip(str)
return gzip_helper(deflate_gzip, str)
end


--- Gzip decompress the content of a string
-- @tparam string gz the Gzip compressed string
-- @return str (string) of the decompressed content, or nil, err to if an error occurs
function _M.inflate_gzip(gz)
return gzip_helper(inflate_gzip, gz)
end


return _M
52 changes: 1 addition & 51 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

local ffi = require "ffi"
local uuid = require "resty.jit-uuid"
local buffer = require "string.buffer"
local pl_stringx = require "pl.stringx"
local pl_utils = require "pl.utils"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local zlib = require "ffi-zlib"

local C = ffi.C
local ffi_new = ffi.new
Expand All @@ -35,8 +33,6 @@ local join = pl_stringx.join
local split = pl_stringx.split
local re_find = ngx.re.find
local re_match = ngx.re.match
local inflate_gzip = zlib.inflateGzip
local deflate_gzip = zlib.deflateGzip
local setmetatable = setmetatable

ffi.cdef[[
Expand Down Expand Up @@ -1038,53 +1034,6 @@ do
end


do
-- lua-ffi-zlib allocated buffer of length +1,
-- so use 64KB - 1 instead
local GZIP_CHUNK_SIZE = 65535

local function read_input_buffer(input_buffer)
return function(size)
local data = input_buffer:get(size)
return data ~= "" and data or nil
end
end

local function write_output_buffer(output_buffer)
return function(data)
return output_buffer:put(data)
end
end

local function gzip_helper(inflate_or_deflate, input)
local input_buffer = buffer.new(0):set(input)
local output_buffer = buffer.new()
local ok, err = inflate_or_deflate(read_input_buffer(input_buffer),
write_output_buffer(output_buffer),
GZIP_CHUNK_SIZE)
if not ok then
return nil, err
end

return output_buffer:get()
end

--- Gzip compress the content of a string
-- @tparam string str the uncompressed string
-- @return gz (string) of the compressed content, or nil, err to if an error occurs
function _M.deflate_gzip(str)
return gzip_helper(deflate_gzip, str)
end

--- Gzip decompress the content of a string
-- @tparam string gz the Gzip compressed string
-- @return str (string) of the decompressed content, or nil, err to if an error occurs
function _M.inflate_gzip(gz)
return gzip_helper(inflate_gzip, gz)
end
end


local get_mime_type
local get_response_type
local get_error_template
Expand Down Expand Up @@ -1432,6 +1381,7 @@ _M.get_updated_monotonic_ms = get_updated_monotonic_ms

do
local modules = {
"kong.tools.gzip",
"kong.tools.table",
"kong.tools.sha256",
"kong.tools.yield",
Expand Down

1 comment on commit a8de91a

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:a8de91a79e61b32bc78324a391bcdea24222783b
Artifacts available https://github.com/Kong/kong/actions/runs/6701244043

Please sign in to comment.