|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +// Usage: |
| 4 | +// |
| 5 | +// go run parse_eventstream.go [dir] |
| 6 | +// |
| 7 | +// Finds all .resp.bin files in dir (default: current directory) and |
| 8 | +// generates a corresponding .resp.decoded file for each one. Existing |
| 9 | +// .resp.decoded files are overwritten. |
| 10 | +// |
| 11 | +// To decode a single file: |
| 12 | +// |
| 13 | +// go run parse_eventstream.go path/to/file.resp.bin |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "encoding/base64" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream" |
| 26 | + "github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream/eventstreamapi" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + arg := "." |
| 31 | + if len(os.Args) > 1 { |
| 32 | + arg = os.Args[1] |
| 33 | + } |
| 34 | + |
| 35 | + info, err := os.Stat(arg) |
| 36 | + if err != nil { |
| 37 | + fmt.Fprintf(os.Stderr, "stat %s: %v\n", arg, err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + |
| 41 | + if !info.IsDir() { |
| 42 | + if err := decodeFile(arg, os.Stdout); err != nil { |
| 43 | + fmt.Fprintf(os.Stderr, "%v\n", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + files, err := filepath.Glob(filepath.Join(arg, "*.resp.bin")) |
| 50 | + if err != nil { |
| 51 | + fmt.Fprintf(os.Stderr, "glob: %v\n", err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + if len(files) == 0 { |
| 55 | + fmt.Fprintf(os.Stderr, "no .resp.bin files found in %s\n", arg) |
| 56 | + os.Exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + for _, binFile := range files { |
| 60 | + outFile := strings.TrimSuffix(binFile, ".resp.bin") + ".resp.decoded" |
| 61 | + f, err := os.Create(outFile) |
| 62 | + if err != nil { |
| 63 | + fmt.Fprintf(os.Stderr, "create %s: %v\n", outFile, err) |
| 64 | + continue |
| 65 | + } |
| 66 | + if err := decodeFile(binFile, f); err != nil { |
| 67 | + fmt.Fprintf(os.Stderr, "decode %s: %v\n", binFile, err) |
| 68 | + } |
| 69 | + f.Close() |
| 70 | + fmt.Printf("%s -> %s\n", filepath.Base(binFile), filepath.Base(outFile)) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func decodeFile(path string, w *os.File) error { |
| 75 | + data, err := os.ReadFile(path) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("read %s: %w", path, err) |
| 78 | + } |
| 79 | + |
| 80 | + decoder := eventstream.NewDecoder() |
| 81 | + reader := bytes.NewReader(data) |
| 82 | + frameNum := 0 |
| 83 | + |
| 84 | + for { |
| 85 | + msg, err := decoder.Decode(reader, nil) |
| 86 | + if err != nil { |
| 87 | + break |
| 88 | + } |
| 89 | + frameNum++ |
| 90 | + |
| 91 | + messageType := msg.Headers.Get(eventstreamapi.MessageTypeHeader) |
| 92 | + eventType := msg.Headers.Get(eventstreamapi.EventTypeHeader) |
| 93 | + |
| 94 | + fmt.Fprintf(w, "=== Frame %d ===\n", frameNum) |
| 95 | + fmt.Fprintf(w, " message-type: %s\n", headerStr(messageType)) |
| 96 | + fmt.Fprintf(w, " event-type: %s\n", headerStr(eventType)) |
| 97 | + |
| 98 | + if headerStr(eventType) != "chunk" { |
| 99 | + fmt.Fprintf(w, " payload: %s\n\n", string(msg.Payload)) |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + var chunk struct { |
| 104 | + Bytes string `json:"bytes"` |
| 105 | + } |
| 106 | + if err := json.Unmarshal(msg.Payload, &chunk); err != nil { |
| 107 | + fmt.Fprintf(w, " unmarshal error: %v\n\n", err) |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + decoded, err := base64.StdEncoding.DecodeString(chunk.Bytes) |
| 112 | + if err != nil { |
| 113 | + fmt.Fprintf(w, " base64 decode error: %v\n\n", err) |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + var pretty json.RawMessage |
| 118 | + if err := json.Unmarshal(decoded, &pretty); err != nil { |
| 119 | + fmt.Fprintf(w, " json: %s\n\n", string(decoded)) |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + indented, _ := json.MarshalIndent(pretty, " ", " ") |
| 124 | + fmt.Fprintf(w, " body:\n %s\n\n", string(indented)) |
| 125 | + } |
| 126 | + |
| 127 | + fmt.Fprintf(w, "Total frames: %d\n", frameNum) |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func headerStr(h eventstream.Value) string { |
| 132 | + if h == nil { |
| 133 | + return "<nil>" |
| 134 | + } |
| 135 | + return h.String() |
| 136 | +} |
0 commit comments