filecache

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package filecache is the content-addressed blob store edvabe uses for template file contexts.

The E2B SDK's Template.build() tars each COPY/copyItems step's source paths client-side, computes a hash over the file contents and metadata, and asks the server via GET /templates/{id}/files/{hash} whether the blob is already present. On a miss, the server hands back a short-lived upload URL; the SDK PUTs the tar there. The hash is not a SHA-256 of the tar stream — it incorporates relative paths, file modes, sizes, and a COPY instruction prefix — so the server stores blobs keyed by this opaque hash without re-verifying. Cache is deduplicated across templates — two templates that COPY the same files share a single on-disk blob.

The on-disk layout is flat: <root>/<hash>.tar. Writes are atomic via a .part sidecar that's renamed into place on success, so a crash or a concurrent Put never leaves a reader seeing a half-written blob.

Index

Constants

This section is empty.

Variables

View Source
var ErrHashMismatch = errors.New("filecache: hash mismatch")

ErrHashMismatch is kept for API compatibility but is no longer returned by Put — the SDK's hash is computed over file metadata, not the tar stream, so server-side re-verification is not possible.

View Source
var ErrInvalidToken = errors.New("filecache: invalid upload token")

ErrInvalidToken is returned when a token fails signature or expiry verification. Callers should map this to HTTP 401.

Functions

func HashBytes

func HashBytes(b []byte) string

HashBytes returns the hex-encoded sha256 of b. Useful for tests and for callers that need to pre-compute a hash before calling Put.

Types

type Cache

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

Cache is a content-addressed blob store rooted at Root. The zero value is not usable; construct with New.

func New

func New(root string) (*Cache, error)

New constructs a Cache with the given root directory. The directory is created if it does not already exist.

func (*Cache) Has

func (c *Cache) Has(hash string) (bool, error)

Has reports whether the given blob exists in the cache. Returns an error only for I/O problems, not for a missing blob (which returns false, nil).

func (*Cache) Open

func (c *Cache) Open(hash string) (io.ReadCloser, error)

Open returns a reader over the cached blob. Caller must close the returned reader. Returns os.ErrNotExist if the blob is not in the cache.

func (*Cache) Put

func (c *Cache) Put(hash string, r io.Reader) error

Put writes the reader's bytes into the cache under the given hash. Put is idempotent: if the blob is already present, the existing file is kept and the input reader is drained but not used. The hash is treated as an opaque client-supplied key — the SDK computes it over file contents, paths, and modes, not over the tar stream, so the server does not re-verify.

func (*Cache) Root

func (c *Cache) Root() string

Root returns the directory the cache is rooted at.

type Signer

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

Signer mints and verifies short-lived upload tokens for the content-addressed file cache.

The SDK workflow: edvabe's GET /templates/{id}/files/{hash} handler returns a URL that embeds a signed token. The SDK POSTs the tar to that URL; edvabe's upload handler verifies the token before writing to the cache. This keeps the upload path from needing an authenticated session while still preventing arbitrary upload spam.

Token format: "<base64(hmac)>.<expiryUnix>". HMAC is over "<hash>.<expiryUnix>" using a process-local secret generated once at startup.

func NewRandomSigner

func NewRandomSigner(ttl time.Duration) (*Signer, error)

NewRandomSigner seeds the signer with a fresh random secret. Tokens minted by one process instance cannot be verified by another — this is deliberate. The upload path is ephemeral and has no reason to survive a restart.

func NewSigner

func NewSigner(opts SignerOptions) *Signer

NewSigner returns a Signer. A non-nil secret is required for deterministic verification — NewRandomSigner is the convenience for production.

func (*Signer) Sign

func (s *Signer) Sign(hash string) string

Sign returns a token that authorizes a single upload for hash. The token carries its own expiry; verification is stateless.

func (*Signer) Verify

func (s *Signer) Verify(hash, token string) error

Verify returns nil if the token authorizes an upload for the given hash and has not expired. Any failure returns ErrInvalidToken.

type SignerOptions

type SignerOptions struct {
	Secret []byte
	TTL    time.Duration
	Now    func() time.Time
}

SignerOptions configures NewSigner. The zero value produces a 5 minute TTL and a random 32-byte secret; tests override both.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL