ws-arch: containers/Archive (the in-memory Archive value shared by archive formats); api: F.load passes the file name to decoders (opts.name) and unwraps single-stream archives whose entry sniffs as an archive (.tar.gz -> tar, meta.compression)

This commit is contained in:
4DBug committed 2026-10-31 08:23:15 -05:00
1 parent cdcb5b0b77
commit 0ca32501a4
2 files changed
+218 -1

No files matched your search

+42 -1
View File
@@ -164,6 +164,42 @@ end
-- ------------------------------------------------------------------------------------------------ API
export type Options = { [string]: any } -- format (name or extension), category, path, data, name, + format options
local load: (src: any, opts: Options?) -> (any, Err.Err?)
-- single-stream archives (gzip / zst / xz / bz2 / br: meta.stream) whose one entry sniffs as another archive (a
-- .tar.gz) load as that inner archive, with meta.compression = the outer format. opts.unwrap = false keeps the outer
-- one; an explicit opts.format also does.
local function unwrap(value: any, formatName: string, o: Options): any
if o.unwrap == false or o.format ~= nil or (o._depth or 0) >= 3 then
return value
end
if type(value) ~= "table" or value.kind ~= "archive" or type(value.meta) ~= "table" or not value.meta.stream then
return value
end
local ok, list = pcall(value.list, value)
if not ok or type(list) ~= "table" or #list ~= 1 then
return value
end
local rok, inner = pcall(value.read, value, list[1].name)
if not rok or type(inner) ~= "buffer" then
return value
end
local e = detect(inner, nil, "archive", nil)
if not e then
return value
end
local io = table.clone(o)
io.category = "archive"
io.name = list[1].name
io._depth = (o._depth or 0) + 1
local v = load(inner, io)
if type(v) ~= "table" or type(v.meta) ~= "table" then
return value
end
v.meta.compression = formatName
return v
end
-- load(src, opts?) -> value | nil, Err
function F.load(src: any, opts: Options?): (any, Err.Err?)
local o = opts or {}
@@ -171,6 +207,10 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
if not buf then
return nil, ierr
end
if name and o.name == nil then
o = table.clone(o)
o.name = name -- formats may use the file name (single-stream archives name their entry after it)
end
local entry, derr = detect(buf, name, o.category, o.format)
if not entry then
return nil, derr
@@ -202,8 +242,9 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
return nil, Err.wrap(err or Err.new(Err.CORRUPT, "decode failed"), entry.name)
end
tag(value, entry.name)
return value
return unwrap(value, entry.name, o)
end
load = F.load
-- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err
function F.open(src: any, opts: Options?): (any, Err.Err?)