mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
Adds tsweb/compserve: content negotiation for precompressed static variants, with a transcode-to-identity fallback for clients that do not accept an encoding (including when the raw file is absent), and CompressWriter, which live-compresses dynamic responses with zstd in its fastest mode, streamed incrementally with no buffering. Negotiation is q-value and wildcard aware (gzip;q=0 previously matched gzip). client/web serves its prebuilt embedded assets through compserve, replacing brotli with zstd; the embedded FS is wrapped in tsweb/vcstime for conditional-request mod times. tsweb/compserve/gzip.go keeps transitional serving of gzip variants from pre-zstd file systems (such as the currently published web-client-prebuilt module): passthrough to gzip-accepting clients, transcoded to identity otherwise; it becomes inert once a zstd-only module is published. util/zstdframe gains pooled GetDecoder and GetStreamingEncoder (concurrency=1). util/precompress is now a build-time tool, generating zstd variants only. cmd/tsconnect and cmd/build-webclient consume the new precompress/compserve split. tsweb.AcceptsEncoding and tsweb/tswebutil are removed; negotiation lives in compserve and the deprecated shim had no callers. go.mod bumps web-client-prebuilt. Also fixes a transcoding bug where http.ServeContent's size probe via the promoted zstd.Decoder.WriteTo could report a zero length, serving empty bodies. Updates tailscale/corp#20099 Signed-off-by: James Tucker <[email protected]>
100 lines
2.5 KiB
Go
100 lines
2.5 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package precompress provides build-time support for precompressing static
|
|
// resources (using Zstandard), to avoid the cost of repeatedly compressing
|
|
// unchanging resources. Serving the resulting variants is handled by
|
|
// tailscale.com/tsweb/compserve.
|
|
package precompress
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
"golang.org/x/sync/errgroup"
|
|
"tailscale.com/tsweb/compserve"
|
|
)
|
|
|
|
// PrecompressDir compresses static assets in dirPath using Zstandard.
|
|
func PrecompressDir(dirPath string, options Options) error {
|
|
var eg errgroup.Group
|
|
err := fs.WalkDir(os.DirFS(dirPath), ".", func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
return nil
|
|
}
|
|
if !compressibleExtensions[filepath.Ext(p)] {
|
|
return nil
|
|
}
|
|
p = path.Join(dirPath, p)
|
|
if options.ProgressFn != nil {
|
|
options.ProgressFn(p)
|
|
}
|
|
|
|
eg.Go(func() error {
|
|
return Precompress(p, options)
|
|
})
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return eg.Wait()
|
|
}
|
|
|
|
type Options struct {
|
|
// FastCompression controls whether compression should be optimized for
|
|
// speed rather than size.
|
|
FastCompression bool
|
|
// ProgressFn, if non-nil, is invoked when a file in the directory is about
|
|
// to be compressed.
|
|
ProgressFn func(path string)
|
|
}
|
|
|
|
var compressibleExtensions = map[string]bool{
|
|
".js": true,
|
|
".css": true,
|
|
}
|
|
|
|
func Precompress(path string, options Options) error {
|
|
contents, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fi, err := os.Lstat(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
zstdLevel := zstd.WithEncoderLevel(zstd.SpeedBestCompression)
|
|
if options.FastCompression {
|
|
zstdLevel = zstd.WithEncoderLevel(zstd.SpeedFastest)
|
|
}
|
|
return writeCompressed(contents, func(w io.Writer) (io.WriteCloser, error) {
|
|
// Per RFC 8878, encoders should avoid window sizes larger than 8MB, which is the max that Chrome accepts.
|
|
return zstd.NewWriter(w, zstdLevel, zstd.WithWindowSize(8<<20))
|
|
}, path+compserve.Zstd.Ext, fi.Mode())
|
|
}
|
|
|
|
func writeCompressed(contents []byte, compressedWriterCreator func(io.Writer) (io.WriteCloser, error), outputPath string, outputMode fs.FileMode) error {
|
|
var buf bytes.Buffer
|
|
compressedWriter, err := compressedWriterCreator(&buf)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := compressedWriter.Write(contents); err != nil {
|
|
return err
|
|
}
|
|
if err := compressedWriter.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(outputPath, buf.Bytes(), outputMode)
|
|
}
|