nupp.compression

Compressing and decompressing a stream of bytes.

Ask for a format, then use its one-shot helpers or wrap a nupp.io reader or writer:

local compression = require("nupp.compression")

const gzip = compression.format("gzip")

const encoded = gzip:newWriter(destination, {
    gzip = {level = 6},
})

encoded:write("hello")

assert(encoded:finish())

A factory consumes the reader or writer it wraps. A compression writer has to end in finish(); closing or dropping one unfinished aborts the stream and raises, because a truncated member is worse than a loud failure.

Adapter policy and format options are separate, which is why the options table is keyed by format name.

Built-in formats#

Name Container Writer options Reader options
gzip RFC 1952 gzip gzip = { level = 0..9 } gzip = { concatenatedMembers = boolean }
zlib RFC 1950 zlib zlib = { level = 0..9 } none
deflate-raw RFC 1951 raw DEFLATE deflateRaw = { level = 0..9 } none

All three take levels zero through nine. Gzip checks its CRC and size trailers and reads concatenated members by default, zlib checks its Adler-32 trailer, and raw DEFLATE has no checksum at all. Trailing bytes are rejected in every case.

A provider-added format keys its options by its own canonical name; deflateRaw above is the built-in ergonomic spelling of deflate-raw.

Submodules

ModuleDescription
nupp.compression.spiImplementation interfaces for compression stream catalogs.

Module contents

Types

TypeKindDescription
CompressionWriterinterfaceAn ordinary writer whose consuming finish emits the final trailer.
DeflateRawWriterOptionsinterfaceOptions in the deflateRaw block passed to a compression writer.
FormatrecordRetained compression format descriptor.
GzipReaderOptionsinterfaceOptions in the gzip block passed to a decompression reader.
GzipWriterOptionsinterfaceOptions in the gzip block passed to a compression writer.
ReaderOptionsinterfaceCommon decompression-reader policy.
WriterOptionsinterfaceCommon compression-writer policy.
ZlibWriterOptionsinterfaceOptions in the zlib block passed to a compression writer.

Functions

FunctionKindDescription
formatfunctionAcquires a retained format descriptor or raises when unavailable.
formatsfunctionLists available canonical format names in ascending order.

Types#

CompressionWriterinterface#

interface CompressionWriter is io.Writer ...

An ordinary writer whose consuming finish emits the final trailer.

Members

NameKindDescription
closemethodAborts an unfinished stream and raises after releasing owned resources.
finishmethodCompletes the stream, flushes and closes the owned destination.

closemethod#

close: @nosuspend function(takes self: CompressionWriter): nil

Aborts an unfinished stream and raises after releasing owned resources.

Arguments
NameTypeDescription
takes selfCompressionWriter
Returns
TypeDescription
nil
Raises
TypeCondition
string

whenever the stream was not consumed by finish

finishmethod#

finish: function(takes self: CompressionWriter): (boolean, string?)

Completes the stream, flushes and closes the owned destination.

Arguments
NameTypeDescription
takes selfCompressionWriter
Returns
TypeDescription
boolean
string?

DeflateRawWriterOptionsinterface#

interface DeflateRawWriterOptions ...

Options in the deflateRaw block passed to a compression writer.

Members

NameKindDescription
levelfieldCompression level from zero through nine; six by default.

levelfield#

level: integer?

Compression level from zero through nine; six by default.

Formatrecord#

record Format ...

Retained compression format descriptor.

Members

NameKindDescription
namefieldCanonical format name.
implementationfield
newReadermethodWraps and owns a reader, validating the stream before EOF.
newWritermethodWraps and owns a writer.
compressmethodCompresses a borrowed span into a destination buffer.
compressmethodCompresses a string into a destination buffer.
compressmethodCompresses a borrowed span into a new string.
compressmethodCompresses a string into a new string.
decompressmethodDecompresses a borrowed span into a destination buffer.
decompressmethodDecompresses a string into a destination buffer.
decompressmethodDecompresses a borrowed span into a new string with a 64 MiB default cap.
decompressmethodDecompresses a string into a new string with a 64 MiB default cap.

namefield#

name: string
@readonly

Canonical format name.

implementationfield#

implementation: provider.Format
@private

newReadermethod#

newReader: function newReader(self, takes source: io.Reader, options: ReaderOptions?): io.Reader

Wraps and owns a reader, validating the stream before EOF.

Arguments
NameTypeDescription
selfany
takes sourceio.Reader
optionsReaderOptions?
Returns
TypeDescription
io.Reader
Raises
TypeCondition
string

when options are invalid or a format block does not match

newWritermethod#

newWriter: function newWriter(self, takes destination: io.Writer, options: WriterOptions?): CompressionWriter

Wraps and owns a writer. Consume the result with finish().

Arguments
NameTypeDescription
selfany
takes destinationio.Writer
optionsWriterOptions?
Returns
TypeDescription
CompressionWriter
Raises
TypeCondition
string

when options are invalid or a format block does not match

compressmethod#

compress: function compress(self, borrows bytes: span.ByteSpan, exclusive destination: io.Buffer, options: WriterOptions?): nil

Compresses a borrowed span into a destination buffer.

Arguments
NameTypeDescription
selfany
borrows bytesspan.ByteSpan
exclusive destinationio.Buffer
optionsWriterOptions?
Returns
TypeDescription
nil
Raises
TypeCondition
string

when options are invalid or compression fails

compressmethod#

compress: function compress(self, bytes: string, exclusive destination: io.Buffer, options: WriterOptions?): nil

Compresses a string into a destination buffer.

Arguments
NameTypeDescription
selfany
bytesstring
exclusive destinationio.Buffer
optionsWriterOptions?
Returns
TypeDescription
nil
Raises
TypeCondition
string

when options are invalid or compression fails

compressmethod#

compress: function compress(self, borrows bytes: span.ByteSpan, options: WriterOptions?): string

Compresses a borrowed span into a new string.

Arguments
NameTypeDescription
selfany
borrows bytesspan.ByteSpan
optionsWriterOptions?
Returns
TypeDescription
string
Raises
TypeCondition
string

when options are invalid or compression fails

compressmethod#

compress: function compress(self, bytes: string, options: WriterOptions?): string

Compresses a string into a new string.

Arguments
NameTypeDescription
selfany
bytesstring
optionsWriterOptions?
Returns
TypeDescription
string
Raises
TypeCondition
string

when options are invalid or compression fails

decompressmethod#

decompress: function decompress(self, borrows bytes: span.ByteSpan, exclusive destination: io.Buffer, options: ReaderOptions?): nil

Decompresses a borrowed span into a destination buffer.

Arguments
NameTypeDescription
selfany
borrows bytesspan.ByteSpan
exclusive destinationio.Buffer
optionsReaderOptions?
Returns
TypeDescription
nil
Raises
TypeCondition
string

when options are invalid, the stream is invalid or a limit is exceeded

decompressmethod#

decompress: function decompress(self, bytes: string, exclusive destination: io.Buffer, options: ReaderOptions?): nil

Decompresses a string into a destination buffer.

Arguments
NameTypeDescription
selfany
bytesstring
exclusive destinationio.Buffer
optionsReaderOptions?
Returns
TypeDescription
nil
Raises
TypeCondition
string

when options are invalid, the stream is invalid or a limit is exceeded

decompressmethod#

decompress: function decompress(self, borrows bytes: span.ByteSpan, options: ReaderOptions?): string

Decompresses a borrowed span into a new string with a 64 MiB default cap.

Arguments
NameTypeDescription
selfany
borrows bytesspan.ByteSpan
optionsReaderOptions?
Returns
TypeDescription
string
Raises
TypeCondition
string

when options are invalid, the stream is invalid or a limit is exceeded

decompressmethod#

decompress: function decompress(self, bytes: string, options: ReaderOptions?): string

Decompresses a string into a new string with a 64 MiB default cap.

Arguments
NameTypeDescription
selfany
bytesstring
optionsReaderOptions?
Returns
TypeDescription
string
Raises
TypeCondition
string

when options are invalid, the stream is invalid or a limit is exceeded

GzipReaderOptionsinterface#

interface GzipReaderOptions ...

Options in the gzip block passed to a decompression reader.

Members

NameKindDescription
concatenatedMembersfieldWhether to decode concatenated gzip members; true by default.

concatenatedMembersfield#

concatenatedMembers: boolean?

Whether to decode concatenated gzip members; true by default.

GzipWriterOptionsinterface#

interface GzipWriterOptions ...

Options in the gzip block passed to a compression writer.

Members

NameKindDescription
levelfieldCompression level from zero through nine; six by default.

levelfield#

level: integer?

Compression level from zero through nine; six by default.

ReaderOptionsinterface#

interface ReaderOptions ...

Common decompression-reader policy.

Members

NameKindDescription
workspaceBytesfieldProvider workspace in bytes; 65536 by default.
maxOutputBytesfieldMost decompressed bytes to produce; 64 MiB by default.
maxExpansionRatiofieldMaximum output-to-input ratio after a 1 KiB input allowance.
unboundedfieldExplicitly removes the default output cap.
gzipfieldGzip-specific options, accepted only by the gzip descriptor.

workspaceBytesfield#

workspaceBytes: integer?

Provider workspace in bytes; 65536 by default.

maxOutputBytesfield#

maxOutputBytes: integer?

Most decompressed bytes to produce; 64 MiB by default.

maxExpansionRatiofield#

maxExpansionRatio: number?

Maximum output-to-input ratio after a 1 KiB input allowance.

unboundedfield#

unbounded: boolean?

Explicitly removes the default output cap.

gzipfield#

Gzip-specific options, accepted only by the gzip descriptor.

WriterOptionsinterface#

interface WriterOptions ...

Common compression-writer policy.

Members

NameKindDescription
workspaceBytesfieldProvider workspace in bytes; 65536 by default.
gzipfieldGzip-specific options, accepted only by the gzip descriptor.
zlibfieldZlib-specific options, accepted only by the zlib descriptor.
deflateRawfieldRaw-DEFLATE-specific options, accepted only by the deflate-raw descriptor.

workspaceBytesfield#

workspaceBytes: integer?

Provider workspace in bytes; 65536 by default.

gzipfield#

Gzip-specific options, accepted only by the gzip descriptor.

zlibfield#

Zlib-specific options, accepted only by the zlib descriptor.

deflateRawfield#

Raw-DEFLATE-specific options, accepted only by the deflate-raw descriptor.

ZlibWriterOptionsinterface#

interface ZlibWriterOptions ...

Options in the zlib block passed to a compression writer.

Members

NameKindDescription
levelfieldCompression level from zero through nine; six by default.

levelfield#

level: integer?

Compression level from zero through nine; six by default.

Functions#

formatfunction#

function format(name: string): Format

Acquires a retained format descriptor or raises when unavailable.

Arguments

NameTypeDescription
namestring

Returns

TypeDescription
Format

formatsfunction#

function formats(): {string}

Lists available canonical format names in ascending order.

Returns

TypeDescription
{string}