instance.fs/v1

An instance's code files, for extensions that read and write them: deploy today, a live file drive next. It answers where the instance's folder is, where an extension may keep files it can rebuild (its cache), the one lock every writer takes, whether the instance runs, and how to restart it around a change that mustn't happen under a running app.

The code is the pb_* folders except pb_data and pb_cache, package.json and the bun lockfiles at the top, patches/, and the production node_modules tree at the top. pb_data and pb_cache are never reachable through this contract.

A service contract (fs.go), one provider. It holds no request lookup and no auth: callers say which instance by id, and decide for themselves who may call them.

Use

files := fsv1.Contract.Must(app)               // after registration: in a hook or at serve time
f, err := files.Instance(ctx, fsv1.Self)       // a single app's own files; a host passes the instance's id
dir, _ := f.Dir(ctx)                           // the instance folder
rel, path, err := fsv1.Resolve(dir, "pb_hooks/main.pb.js") // a client path, checked
unlock, _ := f.Lock(ctx)                       // every write holds it
defer unlock()
startErr, err := f.Restart(ctx, func(ctx context.Context) error { /* swap files in */ return nil })

The package also holds the code-path rules every user shares:

  • IsCodePath(p): what a deploy may write or delete (never pb_data or pb_cache).
  • IsReachable(p): what a file API may read or write (the code, node_modules, patches/; never pb_data, pb_cache or .git*).
  • Resolve(dir, p): a client path to a file inside dir, refusing unreachable paths, symlink crossings and anything that lands outside the folder (*PathError).

CacheDir(ctx, name) is the extension's folder for files it can rebuild, following PocketOne's pb_cache convention (pb_cache/<name> in the app's folder on a single app; Pocker and Docker hosts keep it outside the instance folder, in the host's pb_cache/instances/<id>/<name>): anything in it may be deleted at any time, it is never code and never reachable, and it is on the same filesystem as Dir, so an extension can stage files there and rename them in atomically.

Provide

Implement fsv1.Files: Instance(ctx, id) returns an fsv1.FS for each instance (fsv1.Self on a single app), or fsv1.ErrNoInstance. Return the same FS for the same instance every time: its Lock must be shared by every caller. fsv1.Local is an FS over a local folder to build on: give it the folder, and optionally a cache folder, whether it runs, how to stop it and how to start it again.

  • Single app: deploy provides it as the default: the app's own folder. The app can't stop itself, so Restart applies the change while it runs (each rename is atomic) and then restarts the app.
  • Host: the host's runtime provides it on the host scope (fsv1.Contract.ProvideIn), one per host since a host runs one runtime: pocker (Manager.Files(): stops the instance's app, starts it again) and docker (Runtime.Files(dir): stops the container; the next request starts a fresh one). fsv1.Instances is the Files they build on: one Local per known instance, kept so its lock is shared. instance.fs/v1 isn't host-providable, so code inside an instance never reaches the host's provider.

Extensions

Extension
docker provides
pocker provides
deploy provides (default)
deploy requires

License

MIT