diff --git a/modules/gitea/cmd/package-registry/whole_integration_test.go b/modules/gitea/cmd/package-registry/whole_integration_test.go index ec90b8933..b7b76057f 100644 --- a/modules/gitea/cmd/package-registry/whole_integration_test.go +++ b/modules/gitea/cmd/package-registry/whole_integration_test.go @@ -19,7 +19,9 @@ import ( "bytes" "context" "crypto/rand" + "crypto/sha256" "encoding/base64" + "encoding/hex" "encoding/json" "errors" "fmt" @@ -37,6 +39,7 @@ import ( "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + "git.novox.be/novox/mesh-sdk/go/buscalls" "git.novox.be/novox/mesh-sdk/go/testbus" "gitea/internal/packageseats" @@ -60,6 +63,8 @@ type player struct { in io.Writer next int answers map[string]chan wire + puts map[string][]byte // the put sessions open, by call + gets map[string][]byte // the get sessions open, by call } func (p *player) send(m map[string]any) { @@ -132,6 +137,10 @@ func (p *player) handle(m wire) { case m.Method == "mesh/publish", m.Method == "mesh/subscribe": // the module's finding writer emits its events and subscribes to findings-asked (design 47 G A-16); none is played p.answer(m.ID, map[string]any{}, nil) + case m.Method == buscalls.ObjectsPutChunk: + p.putChunk(m) + case m.Method == buscalls.ObjectsGetChunk: + p.getChunk(m) case strings.HasPrefix(m.Method, "mesh/objects."): p.objects(m, a.Objects, a.Name, a.Data) case strings.HasPrefix(m.Method, "mesh/state."): @@ -141,6 +150,59 @@ func (p *player) handle(m wire) { } } +// putChunk plays the runtime's put session (design 47 G §518, A-21): the chunks of one call in order, each answered as taken, the +// last put whole and answered as the put's. +func (p *player) putChunk(m wire) { + var c buscalls.ObjectsPutChunkRequest + _ = json.Unmarshal(m.Params, &c) + p.mu.Lock() + if p.puts == nil { + p.puts = map[string][]byte{} + } + if c.Seq == 0 || (len(p.puts[c.Call]) == 0 && c.Seq != 1) { + p.mu.Unlock() + p.answer(m.ID, buscalls.ObjectsChunkTaken{Seq: c.Seq, Error: "a put session begins at chunk 1"}, nil) + return + } + p.puts[c.Call] = append(p.puts[c.Call], c.Data...) + whole := p.puts[c.Call] + if c.Last { + delete(p.puts, c.Call) + } + p.mu.Unlock() + if !c.Last { + p.answer(m.ID, buscalls.ObjectsChunkTaken{Seq: c.Seq}, nil) + return + } + os, err := p.js.ObjectStore(p.ctx, "gitea_"+c.Objects) + if err != nil { + p.answer(m.ID, nil, err) + return + } + info, err := os.PutBytes(p.ctx, c.Name, whole) + if err != nil { + p.answer(m.ID, nil, err) + return + } + p.answer(m.ID, map[string]any{"name": info.Name, "size": info.Size, "digest": info.Digest, "seq": c.Seq}, nil) +} + +// getChunk plays the runtime's get session: the chunk of the session's value. +func (p *player) getChunk(m wire) { + var c buscalls.ObjectsGetChunkRequest + _ = json.Unmarshal(m.Params, &c) + p.mu.Lock() + whole := p.gets[c.Call] + p.mu.Unlock() + at := int(c.Seq-1) * buscalls.ChunkBytes + if whole == nil || c.Seq == 0 || at >= len(whole) { + p.answer(m.ID, buscalls.ObjectsChunk{Seq: c.Seq, Error: "no such chunk of the session"}, nil) + return + } + end := min(at+buscalls.ChunkBytes, len(whole)) + p.answer(m.ID, buscalls.ObjectsChunk{Seq: c.Seq, Last: end == len(whole), Data: whole[at:end]}, nil) +} + func (p *player) objects(m wire, local, name string, data []byte) { os, err := p.js.ObjectStore(p.ctx, "gitea_"+local) if err != nil { @@ -157,6 +219,20 @@ func (p *player) objects(m wire, local, name string, data []byte) { p.answer(m.ID, map[string]any{"name": info.Name, "size": info.Size, "digest": info.Digest}, nil) case "mesh/objects.get": got, err := os.GetBytes(p.ctx, name) + if err == nil && len(got) > buscalls.ChunkBytes { + // a value over one chunk is read by a session (design 47 G §490, §518) + sum := sha256.Sum256(got) + call := fmt.Sprintf("get-%s-%d", name, len(got)) + p.mu.Lock() + if p.gets == nil { + p.gets = map[string][]byte{} + } + p.gets[call] = got + p.mu.Unlock() + p.answer(m.ID, buscalls.ObjectsChunked{Chunked: json.RawMessage("true"), Size: uint64(len(got)), + Digest: "sha256:" + hex.EncodeToString(sum[:]), Call: call}, nil) + break + } p.answer(m.ID, map[string]any{"info": map[string]any{"name": name, "size": len(got)}, "data": got}, err) case "mesh/objects.delete": err := os.Delete(p.ctx, name)