fsv1
import fsv1 "pocketone.dev/ext/contracts/instance/fs/v1"
Package fsv1 is the instance.fs/v1 contract: one instance's code files on local disk, for extensions that read and write them (ext/deploy today). It answers which folder, where an extension may keep files it can rebuild about the instance (its cache), one writers' lock, and whether the instance runs (and how to restart it around a change).
There is no request lookup and no auth in it: callers say which instance by id. On a single app the app provides it and is its only instance (Self); a multisite host provides it on its own scope (ProvideIn) for each of its instances, and its handlers pass the id the request routed to. Who may call is the caller's business (PocketBase's auth on a single app, the host's auth function on a host).
The package also holds the code-path rules: what belongs to an instance's code (IsCodePath), what a file API may reach (IsReachable) and how a client path maps to a file without escaping the folder (Resolve). pb_data and pb_cache are never reachable through this contract.
Source: ext/contracts/instance/fs/v1.
Constants
const (
DataDir = "pb_data"
CacheFolder = "pb_cache" // extensions' disposable files ([FS.CacheDir]; extkit's pb_cache convention)
NodeModulesDir = "node_modules"
PatchesDir = "patches"
MaxPathLength = 1024
)
const Self = ""
Self is the instance id of a single app: the app itself.
Variables
var Contract = extkit.NewContract[Files]("instance.fs/v1", extkit.One)
Contract is instance.fs/v1.
var ErrNoFolder = errors.New("instance.fs/v1: the instance has no folder yet")
ErrNoFolder: the instance has no folder yet.
var ErrNoInstance = errors.New("instance.fs/v1: no such instance")
ErrNoInstance: the provider has no instance with that id.
var ErrUnreachable = errors.New("outside the files you can reach (pb_hooks, pb_migrations, pb_public and other pb_* folders except pb_data and pb_cache, node_modules, package.json, bun.lock(b), patches/)")
ErrUnreachable is the reason for a path outside what IsReachable allows.
var RootCodeFiles = []string{"package.json", "bun.lock", "bun.lockb"}
RootCodeFiles are the code files at the top of an instance folder.
Functions
IsCodePath
func IsCodePath(p string) bool
IsCodePath reports whether p (relative to the instance folder) belongs to the code: what a deploy may write or delete. node_modules counts only at the top.
IsCodeRoot
func IsCodeRoot(name string) bool
IsCodeRoot: a top-level name the code lives under (or is).
IsGitSegment
func IsGitSegment(seg string) bool
IsGitSegment: .git, .gitignore, ... never belong to the code.
IsReachable
func IsReachable(p string) bool
IsReachable reports whether a file API may read or write p: the code, node_modules at the top or inside a code folder, and patches/. Never pb_data, pb_cache or .git*.
IsSafeRelativePath
func IsSafeRelativePath(p string) bool
IsSafeRelativePath: a relative POSIX path with no empty, "." or ".." segments, no backslash or NUL.
Resolve
func Resolve(dir, raw string) (rel, fsPath string, err error)
Resolve maps a client path (slash-separated, relative to dir; "" is dir itself) to a file path inside dir. It refuses paths IsReachable doesn't allow, paths that cross a symbolic link, and anything that would land outside dir. rel is the cleaned path.
Types
FS
type FS interface {
// Dir is the instance folder on local disk (it holds pb_hooks, pb_public, ... and pb_data, which this contract
// never reaches). An error when there's no folder yet (a single app before it bootstraps).
Dir(ctx context.Context) (string, error)
// CacheDir is a folder for files an extension can rebuild about the instance (name: the extension's, e.g.
// "deploy"), made if missing, following PocketOne's pb_cache convention: pb_cache/<name> in Dir on a single app; a
// host keeps it outside Dir (Pocker and Docker: the host's pb_cache/instances/<id>/<name>). Anything in it may be
// deleted at any time (a cache clear, an admin, a cleanup) and the extension carries on. It is never code and never
// reachable ([IsCodePath], [IsReachable]), and it is on the same filesystem as Dir, so renames between them are
// atomic.
CacheDir(ctx context.Context, name string) (string, error)
// Lock takes the instance's writers' lock: every write to the code files (a deploy's commit, a file API write)
// holds it, so they never interleave. Call unlock when done.
Lock(ctx context.Context) (func(), error)
// Running reports whether the instance is running now.
Running(ctx context.Context) (bool, error)
// Restart applies a change that mustn't happen under a running app: it stops the instance if it runs, calls apply
// (if not nil) while it's stopped, and has it run again, whether apply failed or not. An instance that isn't running
// gets apply only and stays as it was. An instance that can't stop itself (a single app) calls apply as it is and
// then restarts. err is apply's; startErr says the instance didn't come back.
Restart(ctx context.Context, apply func(context.Context) error) (startErr, err error)
}
FS is one instance's code files.
Files
type Files interface {
// Instance returns the instance's files ([Self] on a single app). ErrNoInstance when the provider has no such
// instance.
Instance(ctx context.Context, id string) (FS, error)
}
Files gives each instance's code files.
Instances
type Instances struct {
// Known reports whether the host has the instance (else [ErrNoInstance]). Required.
Known func(id string) bool
// New builds the instance's Local. Required.
New func(id string) *Local
// contains filtered or unexported fields
}
Instances is a Files for a host's instances, for runtimes to provide: it builds each instance's Local once and keeps it, so every caller shares its lock.
Instances.Instance
func (i *Instances) Instance(_ context.Context, id string) (FS, error)
Local
type Local struct {
// Folder returns the instance folder ("" while there is none yet). Required.
Folder func() string
// Cache returns an extension's cache folder. Default: <instance folder>/pb_cache/<name>.
Cache func(name string) string
// IsRunning reports whether the instance runs. Default: always.
IsRunning func() bool
// Stopped calls fn with the instance stopped; the instance runs again after (now or on its next request).
// Default: fn as it is (an app can't stop itself).
Stopped func(ctx context.Context, fn func(context.Context) error) error
// Start has the instance run again after Stopped, when that doesn't (a single app: its restart). Optional.
Start func(ctx context.Context) error
// contains filtered or unexported fields
}
Local is an FS over a folder on local disk, for providers to build on. Zero values are fine except where noted.
Local.CacheDir
func (l *Local) CacheDir(ctx context.Context, name string) (string, error)
Local.Dir
func (l *Local) Dir(context.Context) (string, error)
Local.Lock
func (l *Local) Lock(context.Context) (func(), error)
Local.Restart
func (l *Local) Restart(ctx context.Context, apply func(context.Context) error) (startErr, err error)
Local.Running
func (l *Local) Running(context.Context) (bool, error)
PathError
type PathError struct{ Path, Reason string }
PathError is a client path Resolve refuses.
PathError.Error
func (e *PathError) Error() string
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.