Documentation
¶
Overview ¶
Package zstd writes RFC 8878 Zstandard frames and calculates cache metadata over the encoded representation while it is emitted.
Host Go uses github.com/klauspost/compress/zstd. TinyGo uses this package's bounded encoder, which can also be selected on host Go with the shared force_tinygo_logic build tag. Both implementations expose the same API, calculate Result's SHA-256 digest over bytes successfully written, and write nothing to the destination until the caller writes, flushes, or closes.
Writer is poolable through Reset, which is how the fasthttp fork in this repository compresses responses under TinyGo.
Index ¶
Examples ¶
Constants ¶
const ContentEncoding = "zstd"
ContentEncoding is the HTTP content-coding token for Zstandard.
Variables ¶
var ( ErrClosed = errors.New("zstd: writer is closed") )
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures both the host-Go and TinyGo encoders.
type Result ¶
Result describes an encoded representation. SHA256 covers exactly Size bytes written to the destination, including the Zstandard frame headers.
func EncodeAll ¶
EncodeAll encodes src, returning the frame and its cache metadata. The digest is produced while the frame is written; the encoded bytes are not traversed a second time.
Example ¶
package main
import (
"fmt"
"github.com/shibukawa/tinygodriver/compress/zstd"
)
func main() {
encoded, result, err := zstd.EncodeAll([]byte("response body"))
if err != nil {
panic(err)
}
fmt.Println(len(encoded) == int(result.Size))
fmt.Println(zstd.ContentEncoding)
}
Output: true zstd
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer emits one Zstandard frame through github.com/klauspost/compress/zstd. Writer is not safe for concurrent use. Close must succeed before Result can be read.
func NewWriter ¶
NewWriter starts a host-Go Zstandard stream. Constructing a Writer writes nothing to the destination, so an encoder built and then abandoned leaves the destination untouched. TinyGo and builds using the force_tinygo_logic tag select the bounded TinyGo encoder instead.
func (*Writer) Flush ¶ added in v1.0.4
Flush emits the buffered input as complete blocks so that everything written so far can be decoded, and returns once those bytes reach the destination. It does not end the frame and it does not flush the destination itself. Flushing before a block fills reduces the compression ratio.
func (*Writer) Reset ¶ added in v1.2.4
Reset starts a new frame writing to w, keeping the encoder and its window so that a pooled Writer does not rebuild them. The ETag setting chosen at NewWriter is retained, nothing reaches w until the caller writes, flushes, or closes, and a nil w leaves the Writer in the error state NewWriter would have reported.