Builds: design 47 G A-373 (G-22-lead-answers-2.md) Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-grants/grantsstate"
|
|
)
|
|
|
|
// sealedObjects is the part's object bucket of user lists sealed to the bus node (novox/hq ADR 0547 clause 7), created on
|
|
// its first put: the composer grants the part its create, its puts and deletes, and nothing of another bucket.
|
|
type sealedObjects struct {
|
|
ctx context.Context
|
|
js jetstream.JetStream
|
|
}
|
|
|
|
func (o sealedObjects) store(ctx context.Context) (jetstream.ObjectStore, error) {
|
|
s, err := o.js.ObjectStore(ctx, grantsstate.GrantsUserListObjects)
|
|
if errors.Is(err, jetstream.ErrBucketNotFound) {
|
|
s, err = o.js.CreateObjectStore(ctx, jetstream.ObjectStoreConfig{Bucket: grantsstate.GrantsUserListObjects,
|
|
Description: "the bus's user list composed by the grants part, each sealed to the bus node's key under the hex of its digest (novox/hq ADR 0547 clause 7): written by mesh-grants alone, read by name by the node-engine of the node holding mesh-broker",
|
|
Storage: jetstream.FileStorage, MaxBytes: 64 << 20})
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the object bucket %s: %w", grantsstate.GrantsUserListObjects, err)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (o sealedObjects) PutObject(name string, data []byte) error {
|
|
ctx, cancel := context.WithTimeout(o.ctx, requestWithin)
|
|
defer cancel()
|
|
s, err := o.store(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = s.PutBytes(ctx, name, data)
|
|
return err
|
|
}
|
|
|
|
func (o sealedObjects) DeleteObject(name string) error {
|
|
ctx, cancel := context.WithTimeout(o.ctx, requestWithin)
|
|
defer cancel()
|
|
s, err := o.store(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.Delete(ctx, name); err != nil && !errors.Is(err, jetstream.ErrObjectNotFound) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetObject reads one object of the bucket whole: what the keys `list` and `pass` of the state user-list name (design 47 G A-373).
|
|
func (o sealedObjects) GetObject(name string) ([]byte, error) {
|
|
ctx, cancel := context.WithTimeout(o.ctx, requestWithin)
|
|
defer cancel()
|
|
s, err := o.store(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.GetBytes(ctx, name)
|
|
}
|