brbundle: go.pyspa.org/brbundle Index | Files | Directories

package brbundle

import "go.pyspa.org/brbundle"

brbundle package provides asset bundler's runtime that uses Brotli (https://github.com/google/brotli).

Source repository is here: https://github.com/pyspa/brbundle

Install

To install runtime and commandline tool, type the following command:

$ go get go.pyspa.org/brbundle/...

Asset Handling

This package provides four kind of bundles to handle assets:

1. Embedding. Generate .go file that includes binary representation of asset files. This is best for libraries and so on that is needed to be go gettable.

2. Appended to executable. Generate .zip file internally and appended to executables. You can replace assets after building.

3. Single packed binary file. You can specify and import during runtime. I assume this used for DLC.

4. Folder. This is for debugging. You don't have to do anything to import asset

brbundle searches assets the following orders:

Folder -> Single binary file -> Assets on executable -> Embedded assets

Generate Bundle

The following command generates .go file:

$ brbundle embedded <src-dir>

The following command append assets to executable:

$ brbundle bundle <exec-file-path> <src-dir>

The following command generates single packed file:

$ brbundle pack <out-file-path> <src-dir>

The following command generates asset folder. You can use regular cp command even if you don't have to encrypto assets:

$ brbundle folder <dest-dir> <src-dir>

Standard Usage

It is easy to use the assets:

file, err := brbundle.Find("file.png")
reader, err := file.Reader()
img, err := image.Decode(reader)

Embedded assets and assets appended to executable are available by default. The following functions registers assets in single packed file and local folder:

brbundle.RegisterBundle("masterdata.pb")
brbundle.RegisterFolder("frontend/bist")

Web Framework Middlewares

You can save the earth by using brbundle. brbundle middlewares brotli content directly when browser supports it. Currently, more than 90% browsers already support (https://caniuse.com/#feat=brotli). brbundle provides the following frameworks' middleware:

* net/http
* echo
* gin
* fasthttp and fastrouter
* chi router

net/http:

m := http.NewServeMux()
m.Handle("/static/",
  http.StripPrefix("/static",
  brhttp.Mount())
http.ListenAndServe("localhost:8000", m)

These middlewares also support SPA(Single Page Application). More detail information is on Angular web site (https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml).

All samples are in examples folder: https://github.com/pyspa/brbundle/tree/master/examples

Compression Option

brbundle uses Brotli by default. If you pass --fast/-f option, brbundle uses Snappy (https://github.com/google/snappy) instead of Brotli. Snappy has low compression ratio, but very fast.

Encryption

brbundle supports encryption. You can generates encryption/decryption key by the following command:

$ brbundle key-gen
pBJ0IB3x4EogUVNqmlI4I0EV9+aGpozmIQvSfF+PLo0NfzeamIeaeXHoTqs

When creating bundles, you can pass the key via --crypto/-c option:

$ brbundle pack -c pBJ0IB3x4EogUVNqmlI4I0E... images.pb images

Keys should be passed the following functions:

// for embedded assets
// default name is empty string and you can change by using --name/-n option of brbundle command
brbundle.SetDecryptoKeyToEmbeddedBundle("name", "pBJ0IB3x4EogUVNqmlI4I0E...")
// for executable
brbundle.SetDecryptoKeyToExeBundle("pBJ0IB3x4EogUVNqmlI4I0E...")
// for bundle
brbundle.RegisterBundle("bundle.pb", brbundle.Option{
  DecryptoKey: "pBJ0IB3x4EogUVNqmlI4I0E...",
})
// for folder
brbundle.RegisterEncryptedFolder("public", "pBJ0IB3x4EogUVNqmlI4I0E...")

Index

Package Files

bundle.go decompress.go decrypto.go default.go doc.go embedded.go folderbundle.go manifestbundle.go packedbundle.go packedbundle_nojs.go repository.go repository_nojs.go slices.go types.go websupport.go

Constants

const (
    FolderBundleType   BundleType = 0
    ManifestBundleType            = 1
    PackedBundleType              = 2
    ExeBundleType                 = 3
    EmbeddedBundleType            = 4
)
const (
    NoCompression CompressionType = iota
    Brotli

    NoEncryption EncryptionType = iota
    AES
)
const (
    UseBrotli     = "b"
    UseLZ4        = "l"
    NotToCompress = "-"

    UseAES        = "a"
    NotToEncrypto = "-"
)
const ZIPMethodSnappy uint16 = 65535

Variables

var DefaultRepository = NewRepository()

DefaultRepository is a default repository instance

func NewReadCloser Uses

func NewReadCloser(reader io.Reader, closer io.Closer) io.ReadCloser

func ParseCommentString Uses

func ParseCommentString(comment string) (compressorFlag, etag, contentType string)

func RegisterBundle Uses

func RegisterBundle(path string, option ...Option) error

RegisterBundle registers single packed bundle file to repository

func RegisterEmbeddedBundle Uses

func RegisterEmbeddedBundle(data []byte, name string)

func RegisterEncryptedFolder Uses

func RegisterEncryptedFolder(path, key string, option ...Option) error

RegisterEncryptedFolder registers folder to repository with decryption key

func RegisterFolder Uses

func RegisterFolder(path string, option ...Option) error

RegisterFolder registers folder to repository

func SetDecryptoKeyToEmbeddedBundle Uses

func SetDecryptoKeyToEmbeddedBundle(name, key string) error

SetDecryptoKeyToEmbeddedBundle registers decrypto key for embedded encrypted assets.

func SetDecryptoKeyToExeBundle Uses

func SetDecryptoKeyToExeBundle(key string) error

SetDecryptoKeyToExeBundle registers decrypto key for bundled assets appended to executable.

func Unload Uses

func Unload(name string) error

Unload removes assets from default repository. The name is specified by option when registration.

type BundleType Uses

type BundleType int

type CompressionType Uses

type CompressionType int

func (CompressionType) Flag Uses

func (c CompressionType) Flag() string

func (CompressionType) String Uses

func (c CompressionType) String() string

type Decompressor Uses

type Decompressor func(io.Reader) io.Reader

type Decryptor Uses

type Decryptor interface {
    Decrypto(input io.Reader) (io.Reader, error)
}

type EncryptionType Uses

type EncryptionType int

func (EncryptionType) Flag Uses

func (e EncryptionType) Flag() string

func (EncryptionType) String Uses

func (e EncryptionType) String() string

type FileEntry Uses

type FileEntry interface {
    Reader() (io.ReadCloser, error)
    ReadAll() ([]byte, error)
    BrotliReader() (io.ReadCloser, error)
    Stat() os.FileInfo
    CompressedSize() int64
    Name() string
    Path() string
    EtagAndContentType() (string, string)
    GetLocalPath() (string, error)
}

func Find Uses

func Find(path string) (FileEntry, error)

Find returns assets in default asset repository.

type ManifestEntry Uses

type ManifestEntry struct {
    File string `json:"file"`
    Sha1 string `json:"sha1"`
    Size int    `json:"size"`
}

type Option Uses

type Option struct {
    DecryptoKey         string
    MountPoint          string
    Name                string
    Priority            int
    TempFolder          string
    ResetDownloadFolder bool
    ParallelDownload    int
}

type Progress Uses

type Progress struct {
    // contains filtered or unexported fields
}

func (Progress) DeleteFiles Uses

func (p Progress) DeleteFiles() []string

func (Progress) DownloadFiles Uses

func (p Progress) DownloadFiles() []string

func (Progress) KeepFiles Uses

func (p Progress) KeepFiles() []string

func (*Progress) Wait Uses

func (p *Progress) Wait() error

type ROption Uses

type ROption struct {
    OmitExeBundle          bool
    OmitEmbeddedBundle     bool
    OmitEnvVarFolderBundle bool
}

type ReadCloser Uses

type ReadCloser struct {
    // contains filtered or unexported fields
}

func (ReadCloser) Close Uses

func (rc ReadCloser) Close() error

func (ReadCloser) Read Uses

func (rc ReadCloser) Read(buf []byte) (int, error)

type Repository Uses

type Repository struct {
    Cache *lru.TwoQueueCache
    // contains filtered or unexported fields
}

func NewRepository Uses

func NewRepository(option ...ROption) *Repository

func (*Repository) ClearCache Uses

func (r *Repository) ClearCache()

func (*Repository) Dirs Uses

func (r *Repository) Dirs() []string

func (*Repository) FilesInDir Uses

func (r *Repository) FilesInDir(dirPath string) []string

func (*Repository) Find Uses

func (r *Repository) Find(candidatePaths ...string) (FileEntry, error)

func (*Repository) RegisterBundle Uses

func (r *Repository) RegisterBundle(path string, option ...Option) error

func (*Repository) RegisterEncryptedFolder Uses

func (r *Repository) RegisterEncryptedFolder(path, key string, option ...Option) error

func (*Repository) RegisterFolder Uses

func (r *Repository) RegisterFolder(path string, option ...Option) error

func (*Repository) RegisterRemoteManifest Uses

func (r *Repository) RegisterRemoteManifest(manifestUrl string, option ...Option) (*Progress, error)

func (*Repository) SetCacheSize Uses

func (r *Repository) SetCacheSize(size int) error

func (*Repository) SetDecryptoKeyToEmbeddedBundle Uses

func (r *Repository) SetDecryptoKeyToEmbeddedBundle(name, key string) error

func (*Repository) SetDecryptoKeyToExeBundle Uses

func (r *Repository) SetDecryptoKeyToExeBundle(key string) error

func (*Repository) Unload Uses

func (r *Repository) Unload(name string) error

func (*Repository) Walk Uses

func (r *Repository) Walk(root string, walkFn WalkFunc) error

type WalkFunc Uses

type WalkFunc func(path string, info os.FileInfo, err error) error

type WebOption Uses

type WebOption struct {
    Repository     *Repository
    SPAFallback    string
    MaxAge         time.Duration
    DirectoryIndex string
}

Directories

PathSynopsis
brchi
brecho
brfasthttp
brgin
brhttp
examples
websupport

Package brbundle imports 26 packages (graph) and is imported by 7 packages. Updated 2019-10-05. Refresh now. Tools for package owners.