Documentation
¶
Overview ¶
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 ¶
- Constants
- Variables
- func NewReadCloser(reader io.Reader, closer io.Closer) io.ReadCloser
- func ParseCommentString(comment string) (compressorFlag, etag, contentType string)
- func RegisterBundle(path string, option ...Option) error
- func RegisterEmbeddedBundle(data []byte, name string)
- func RegisterEncryptedFolder(path, key string, option ...Option) error
- func RegisterFolder(path string, option ...Option) error
- func SetDecryptoKeyToEmbeddedBundle(name, key string) error
- func SetDecryptoKeyToExeBundle(key string) error
- func Unload(name string) error
- type BundleType
- type CompressionType
- type Decompressor
- type Decryptor
- type EncryptionType
- type FileEntry
- type ManifestEntry
- type Option
- type Progress
- type ROption
- type ReadCloser
- type Repository
- func (r *Repository) ClearCache()
- func (r *Repository) Dirs() []string
- func (r *Repository) FilesInDir(dirPath string) []string
- func (r *Repository) Find(candidatePaths ...string) (FileEntry, error)
- func (r *Repository) RegisterBundle(path string, option ...Option) error
- func (r *Repository) RegisterEncryptedFolder(path, key string, option ...Option) error
- func (r *Repository) RegisterFolder(path string, option ...Option) error
- func (r *Repository) RegisterRemoteManifest(manifestUrl string, option ...Option) (*Progress, error)
- func (r *Repository) SetCacheSize(size int) error
- func (r *Repository) SetDecryptoKeyToEmbeddedBundle(name, key string) error
- func (r *Repository) SetDecryptoKeyToExeBundle(key string) error
- func (r *Repository) Unload(name string) error
- func (r *Repository) Walk(root string, walkFn WalkFunc) error
- type WalkFunc
- type WebOption
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
Functions ¶
func NewReadCloser ¶
func ParseCommentString ¶
func RegisterBundle ¶
RegisterBundle registers single packed bundle file to repository
func RegisterEmbeddedBundle ¶
func RegisterEncryptedFolder ¶
RegisterEncryptedFolder registers folder to repository with decryption key
func RegisterFolder ¶
RegisterFolder registers folder to repository
func SetDecryptoKeyToEmbeddedBundle ¶
SetDecryptoKeyToEmbeddedBundle registers decrypto key for embedded encrypted assets.
func SetDecryptoKeyToExeBundle ¶
SetDecryptoKeyToExeBundle registers decrypto key for bundled assets appended to executable.
Types ¶
type BundleType ¶
type BundleType int
type CompressionType ¶
type CompressionType int
func (CompressionType) Flag ¶
func (c CompressionType) Flag() string
func (CompressionType) String ¶
func (c CompressionType) String() string
type EncryptionType ¶
type EncryptionType int
func (EncryptionType) Flag ¶
func (e EncryptionType) Flag() string
func (EncryptionType) String ¶
func (e EncryptionType) String() string
type FileEntry ¶
type ManifestEntry ¶ added in v1.1.0
type Progress ¶ added in v1.1.0
type Progress struct {
// contains filtered or unexported fields
}
func (Progress) DeleteFiles ¶ added in v1.1.0
func (Progress) DownloadFiles ¶ added in v1.1.0
type ReadCloser ¶
type ReadCloser struct {
// contains filtered or unexported fields
}
func (ReadCloser) Close ¶
func (rc ReadCloser) Close() error
type Repository ¶
type Repository struct { Cache *lru.TwoQueueCache // contains filtered or unexported fields }
func NewRepository ¶
func NewRepository(option ...ROption) *Repository
func (*Repository) ClearCache ¶
func (r *Repository) ClearCache()
func (*Repository) Dirs ¶ added in v1.1.0
func (r *Repository) Dirs() []string
func (*Repository) FilesInDir ¶ added in v1.1.0
func (r *Repository) FilesInDir(dirPath string) []string
func (*Repository) RegisterBundle ¶
func (r *Repository) RegisterBundle(path string, option ...Option) error
func (*Repository) RegisterEncryptedFolder ¶
func (r *Repository) RegisterEncryptedFolder(path, key string, option ...Option) error
func (*Repository) RegisterFolder ¶
func (r *Repository) RegisterFolder(path string, option ...Option) error
func (*Repository) RegisterRemoteManifest ¶ added in v1.1.0
func (r *Repository) RegisterRemoteManifest(manifestUrl string, option ...Option) (*Progress, error)
func (*Repository) SetCacheSize ¶
func (r *Repository) SetCacheSize(size int) error
func (*Repository) SetDecryptoKeyToEmbeddedBundle ¶
func (r *Repository) SetDecryptoKeyToEmbeddedBundle(name, key string) error
func (*Repository) SetDecryptoKeyToExeBundle ¶
func (r *Repository) SetDecryptoKeyToExeBundle(key string) error
func (*Repository) Unload ¶
func (r *Repository) Unload(name string) error