Files
rosetta/src/Formats/data/msgpack/init.luau
T
4DBug 763f4a7c04 ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py
- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
2026-10-31 12:00:59 -05:00

91 lines
3.4 KiB
Luau

--!strict
-- data/msgpack: MessagePack (msgpack spec, str8 / bin / ext revision). New code. Supports: every type, the
-- timestamp extension (-1) as Types.Date, other extensions as Msgpack.ext(type, data), bin as buffers, maps as
-- Types.object (key order kept; keys of any type), 64-bit integers past 2^53 as exact decimal strings by option, a
-- stream of concatenated values (opts.all). Encode picks the smallest integer / length forms (float64 for
-- non-integers unless opts.float32). Not supported: nothing in the spec.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Codec = require("@formats/data/msgpack/Codec")
-- ------------------------------------------------------------------------------------------------ contract
local Msgpack = {
name = "msgpack",
category = "data",
extensions = { "msgpack", "mpk" },
mime = { "application/msgpack", "application/x-msgpack" },
deps = { "core/Err", "core/Bytes", "Types" },
caps = { decode = true, encode = true, binary = true, ordered = true },
}
export type DecodeOptions = Codec.DecodeOptions & {
all: boolean?, -- decode every concatenated value into an array (default false: exactly one value)
}
export type EncodeOptions = Codec.EncodeOptions & {
all: boolean?, -- the value is an array of values, written one after another
}
-- ------------------------------------------------------------------------------------------------ sniff
function Msgpack.sniff(_r: Bytes.Reader): number
return 0 -- no magic: by extension or opts.format only
end
-- ------------------------------------------------------------------------------------------------ decode
function Msgpack.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local o: DecodeOptions = opts or {}
if o.all then
local out = {}
while not r:eof() do
local v, e, at = Codec.decode(r, o)
if e then
return nil, Err.from(e, at)
end
table.insert(out, v)
end
return out, nil
end
local v, e, at = Codec.decode(r, o)
if e then
local code = if string.find(e, "truncated", 1, true) or string.find(e, "past the end", 1, true) then Err.TRUNCATED
elseif string.find(e, "nested deeper", 1, true) then Err.LIMIT
else Err.CORRUPT
return nil, Err.new(code, e, at)
end
if not r:eof() then
return nil, Err.new(Err.CORRUPT, "trailing data after the value (opts.all reads a stream)", r.pos - r.base)
end
return v, nil
end
-- ------------------------------------------------------------------------------------------------ encode
function Msgpack.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
if opts and opts.all then
if type(value) ~= "table" then
return nil, Err.new(Err.ARG, "all: expected an array of values")
end
local w = Bytes.writer()
for _, v in value do
local b, e = Codec.encode(v, opts)
if not b then
return nil, Err.new(Err.ARG, e or "cannot encode")
end
w:bytes(b)
end
return w:finish(), nil
end
local b, e = Codec.encode(value, opts)
if not b then
return nil, Err.new(Err.ARG, e or "cannot encode")
end
return b, nil
end
-- ------------------------------------------------------------------------------------------------ extras
-- ext(type, data) -> an extension value { type, data } that encodes as ext; isExt(v)
Msgpack.ext = Codec.ext
Msgpack.isExt = Codec.isExt
return Msgpack