| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | ) |
| 10 | |
| 11 | func preflightSessionEventMessages(ctx context.Context, path string, raw []byte, existingMessages, existingCollectionItems int, limits sessionReplayLimits) (messageCount, collectionItems int, err error) { |
| 12 | dec := json.NewDecoder(&contextReader{ctx: ctx, reader: bytes.NewReader(raw)}) |
| 13 | tok, err := dec.Token() |
| 14 | if err != nil { |
| 15 | return 0, existingCollectionItems, err |
| 16 | } |
| 17 | if delim, ok := tok.(json.Delim); !ok || delim != '[' { |
| 18 | return 0, existingCollectionItems, fmt.Errorf("messages must be an array") |
| 19 | } |
| 20 | collectionItems = existingCollectionItems |
| 21 | for dec.More() { |
| 22 | if err := ctx.Err(); err != nil { |
| 23 | return 0, existingCollectionItems, err |
| 24 | } |
| 25 | if existingMessages+messageCount >= limits.maxMessages { |
| 26 | return 0, existingCollectionItems, sessionReplayLimitError( |
| 27 | path, "messages", int64(existingMessages+messageCount+1), int64(limits.maxMessages), |
| 28 | ) |
| 29 | } |
| 30 | messageCount++ |
| 31 | if err := preflightSessionEventValue(ctx, path, dec, &collectionItems, limits.maxCollectionItems); err != nil { |
| 32 | return 0, existingCollectionItems, err |
| 33 | } |
| 34 | } |
| 35 | if _, err := dec.Token(); err != nil { |
| 36 | return 0, existingCollectionItems, err |
| 37 | } |
| 38 | return messageCount, collectionItems, nil |
| 39 | } |
| 40 | |
| 41 | // preflightSessionEventValue walks JSON without materializing nested values. |
| 42 | func preflightSessionEventValue(ctx context.Context, path string, dec *json.Decoder, collectionItems *int, maxCollectionItems int) error { |
| 43 | if err := ctx.Err(); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | tok, err := dec.Token() |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | delim, ok := tok.(json.Delim) |
| 51 | if !ok { |
| 52 | return nil |
| 53 | } |
| 54 | switch delim { |
| 55 | case '{': |
| 56 | for dec.More() { |
| 57 | key, err := dec.Token() |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | if _, ok := key.(string); !ok { |
| 62 | return fmt.Errorf("object key must be a string") |
| 63 | } |
| 64 | if err := preflightSessionEventValue(ctx, path, dec, collectionItems, maxCollectionItems); err != nil { |
| 65 | return err |
| 66 | } |
| 67 | } |
| 68 | end, err := dec.Token() |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if end != json.Delim('}') { |
| 73 | return fmt.Errorf("object is not terminated") |
| 74 | } |
| 75 | return nil |
| 76 | case '[': |
| 77 | for dec.More() { |
| 78 | if err := ctx.Err(); err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if *collectionItems >= maxCollectionItems { |
| 82 | return sessionReplayLimitError(path, "message_collection_items", int64(*collectionItems+1), int64(maxCollectionItems)) |
| 83 | } |
| 84 | (*collectionItems)++ |
| 85 | if err := preflightSessionEventValue(ctx, path, dec, collectionItems, maxCollectionItems); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | } |
| 89 | end, err := dec.Token() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if end != json.Delim(']') { |
| 94 | return fmt.Errorf("array is not terminated") |
| 95 | } |
| 96 | return nil |
| 97 | default: |
| 98 | return fmt.Errorf("unexpected JSON delimiter %q", delim) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | type contextReader struct { |
| 103 | ctx context.Context |
| 104 | reader io.Reader |
| 105 | } |
| 106 | |
| 107 | func (r *contextReader) Read(p []byte) (int, error) { |
| 108 | if err := r.ctx.Err(); err != nil { |
| 109 | return 0, err |
| 110 | } |
| 111 | return r.reader.Read(p) |
| 112 | } |
| 113 |