middleware

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package middleware provides a composable, scope-aware interceptor pipeline for Trove's read/write data path. Middleware wraps the data stream — not the HTTP request — so it works identically via REST, WebSocket, or Go API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Direction

type Direction int

Direction controls which data paths a middleware participates in.

const (
	// DirectionRead indicates the middleware participates in downloads/gets.
	DirectionRead Direction = 1 << iota

	// DirectionWrite indicates the middleware participates in uploads/puts.
	DirectionWrite

	// DirectionReadWrite indicates the middleware participates in both paths.
	DirectionReadWrite = DirectionRead | DirectionWrite
)

func (Direction) String

func (d Direction) String() string

String returns a human-readable direction name.

type Middleware

type Middleware interface {
	// Name returns the middleware identifier (e.g., "encrypt", "compress").
	Name() string

	// Direction returns which data paths this middleware participates in.
	Direction() Direction
}

Middleware is the base interface every middleware implements. A concrete middleware implements ReadMiddleware, WriteMiddleware, or both.

type ReadMiddleware

type ReadMiddleware interface {
	// WrapReader wraps a reader on the download path. The info parameter
	// provides metadata about the object being read.
	WrapReader(ctx context.Context, r io.ReadCloser, info *driver.ObjectInfo) (io.ReadCloser, error)
}

ReadMiddleware intercepts data being read (downloaded).

type Registration

type Registration struct {
	// Middleware is the middleware instance.
	Middleware Middleware

	// Scope determines when this middleware is active.
	// When nil, defaults to ScopeGlobal.
	Scope Scope

	// Direction overrides the middleware's own Direction() when non-zero.
	// This allows registering a bidirectional middleware for only reads or writes.
	Direction Direction

	// Priority controls execution order. Lower values run first.
	// Default is 0. Use negative values to run before default priority.
	Priority int
}

Registration binds a middleware to a scope and optional direction override.

type Resolver

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

Resolver assembles the effective middleware pipeline per-operation. It evaluates all registrations against the current context, bucket, and key, then returns an ordered pipeline sorted by priority.

func NewResolver

func NewResolver() *Resolver

NewResolver creates a new pipeline resolver with LRU caching.

func (*Resolver) Register

func (r *Resolver) Register(reg Registration)

Register adds a middleware registration. Thread-safe.

func (*Resolver) Registrations

func (r *Resolver) Registrations() []Registration

Registrations returns a copy of all current registrations.

func (*Resolver) Remove

func (r *Resolver) Remove(name string, scope Scope)

Remove removes all registrations matching the given middleware name and scope. If scope is nil, removes all registrations with the given name.

func (*Resolver) ResolveRead

func (r *Resolver) ResolveRead(ctx context.Context, bucket, key string) []ReadMiddleware

ResolveRead returns the ordered read middleware pipeline for the given operation.

func (*Resolver) ResolveWrite

func (r *Resolver) ResolveWrite(ctx context.Context, bucket, key string) []WriteMiddleware

ResolveWrite returns the ordered write middleware pipeline for the given operation.

type Scope

type Scope interface {
	// Match returns true if the middleware should be active.
	Match(ctx context.Context, bucket, key string) bool

	// String returns a human-readable description for debugging.
	String() string
}

Scope determines whether a middleware should be active for a given operation. Scopes are evaluated at runtime against the request context, bucket, and key.

func And

func And(scopes ...Scope) Scope

And combines scopes — all must match.

func ForBuckets

func ForBuckets(buckets ...string) Scope

ForBuckets creates a scope matching exact bucket names.

func ForContentTypes

func ForContentTypes(types ...string) Scope

ForContentTypes creates a scope matching MIME type prefixes.

func ForKeys

func ForKeys(patterns ...string) Scope

ForKeys creates a scope matching glob patterns on object keys.

func Not

func Not(scope Scope) Scope

Not inverts a scope.

func Or

func Or(scopes ...Scope) Scope

Or combines scopes — any must match.

func When

func When(fn func(ctx context.Context, bucket, key string) bool) Scope

When creates a scope from an arbitrary predicate function. Use this for tenant scoping, feature flags, or any runtime condition.

func WhenDesc

func WhenDesc(desc string, fn func(ctx context.Context, bucket, key string) bool) Scope

WhenDesc creates a scope from a predicate with a description for debugging.

type ScopeAnd

type ScopeAnd struct {
	Scopes []Scope
}

ScopeAnd requires ALL child scopes to match.

func (*ScopeAnd) Match

func (s *ScopeAnd) Match(ctx context.Context, bucket, key string) bool

Match returns true only if all child scopes match.

func (*ScopeAnd) String

func (s *ScopeAnd) String() string

String returns the scope description.

type ScopeBucket

type ScopeBucket struct {
	Buckets []string
}

ScopeBucket matches operations on specific bucket(s).

func (*ScopeBucket) Match

func (s *ScopeBucket) Match(_ context.Context, bucket, _ string) bool

Match returns true if the bucket matches any of the configured buckets.

func (*ScopeBucket) String

func (s *ScopeBucket) String() string

String returns the scope description.

type ScopeContentType

type ScopeContentType struct {
	Types []string
}

ScopeContentType matches by the object's content type prefix.

func (*ScopeContentType) Match

func (s *ScopeContentType) Match(_ context.Context, _, key string) bool

Match checks the key extension against configured content types. For full content-type matching, use ScopeFunc with context-based lookup.

func (*ScopeContentType) String

func (s *ScopeContentType) String() string

String returns the scope description.

type ScopeFunc

type ScopeFunc struct {
	Fn   func(ctx context.Context, bucket, key string) bool
	Desc string
}

ScopeFunc is an arbitrary predicate for custom runtime evaluation. Use this for tenant scoping, feature flags, or any runtime condition.

func (*ScopeFunc) Match

func (s *ScopeFunc) Match(ctx context.Context, bucket, key string) bool

Match delegates to the predicate function.

func (*ScopeFunc) String

func (s *ScopeFunc) String() string

String returns the scope description.

type ScopeGlobal

type ScopeGlobal struct{}

ScopeGlobal matches every operation. This is the default when no scope is set.

func (ScopeGlobal) Match

func (ScopeGlobal) Match(_ context.Context, _, _ string) bool

Match always returns true.

func (ScopeGlobal) String

func (ScopeGlobal) String() string

String returns the scope description.

type ScopeKeyPattern

type ScopeKeyPattern struct {
	Patterns []string
}

ScopeKeyPattern matches operations on keys matching glob patterns.

func (*ScopeKeyPattern) Match

func (s *ScopeKeyPattern) Match(_ context.Context, _, key string) bool

Match returns true if the key matches any of the configured patterns.

func (*ScopeKeyPattern) String

func (s *ScopeKeyPattern) String() string

String returns the scope description.

type ScopeNot

type ScopeNot struct {
	Inner Scope
}

ScopeNot inverts a scope.

func (*ScopeNot) Match

func (s *ScopeNot) Match(ctx context.Context, bucket, key string) bool

Match returns the inverse of the inner scope.

func (*ScopeNot) String

func (s *ScopeNot) String() string

String returns the scope description.

type ScopeOr

type ScopeOr struct {
	Scopes []Scope
}

ScopeOr requires ANY child scope to match.

func (*ScopeOr) Match

func (s *ScopeOr) Match(ctx context.Context, bucket, key string) bool

Match returns true if any child scope matches.

func (*ScopeOr) String

func (s *ScopeOr) String() string

String returns the scope description.

type WriteMiddleware

type WriteMiddleware interface {
	// WrapWriter wraps a writer on the upload path. The key parameter
	// identifies the object being written.
	WrapWriter(ctx context.Context, w io.WriteCloser, key string) (io.WriteCloser, error)
}

WriteMiddleware intercepts data being written (uploaded).

Directories

Path Synopsis
Package compress provides Zstd compression middleware for Trove.
Package compress provides Zstd compression middleware for Trove.
Package dedup provides content-hash deduplication middleware for Trove.
Package dedup provides content-hash deduplication middleware for Trove.
Package encrypt provides AES-256-GCM encryption middleware for Trove.
Package encrypt provides AES-256-GCM encryption middleware for Trove.
Package scan provides content scanning middleware for Trove.
Package scan provides content scanning middleware for Trove.
Package watermark provides invisible metadata watermarking middleware for Trove.
Package watermark provides invisible metadata watermarking middleware for Trove.

Jump to

Keyboard shortcuts

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