Something went wrong. Try again.
An unofficial terminal client for Tangled, optimized for humans and agents. forked from aly.codes/tg
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124package spindle
import ( "bytes" "fmt" "io" "math"
cborgen "github.com/whyrusleeping/cbor-gen")
const ( maxCBORStringBytes = 1 << 20 maxCBORMapEntries = 256)
// decodeCBORMaps decodes consecutive CBOR maps from a WebSocket binary frame.// Each spindle log frame is a header map followed by a body map.func decodeCBORMaps(data []byte) ([]map[string]any, error) { r := bytes.NewReader(data) var maps []map[string]any for r.Len() > 0 { m, err := decodeCBORMap(r) if err != nil { return nil, err } maps = append(maps, m) } return maps, nil}
func decodeCBORMap(r io.Reader) (map[string]any, error) { majorType, length, err := cborgen.CborReadHeader(r) if err != nil { return nil, fmt.Errorf("read map header: %w", err) } if majorType != 5 { return nil, fmt.Errorf("expected CBOR map, got major type %d", majorType) } if length > maxCBORMapEntries { return nil, fmt.Errorf("CBOR map has %d entries; maximum is %d", length, maxCBORMapEntries) } result := make(map[string]any, length) for i := uint64(0); i < length; i++ { key, err := decodeCBORValue(r) if err != nil { return nil, fmt.Errorf("read map key %d: %w", i, err) } strKey, ok := key.(string) if !ok { return nil, fmt.Errorf("expected string map key, got %T", key) } value, err := decodeCBORValue(r) if err != nil { return nil, fmt.Errorf("read map value for %q: %w", strKey, err) } result[strKey] = value } return result, nil}
func decodeCBORValue(r io.Reader) (any, error) { majorType, value, err := cborgen.CborReadHeader(r) if err != nil { return nil, err } switch majorType { case 0: if value > math.MaxInt64 { return nil, fmt.Errorf("positive integer %d overflows int64", value) } return int64(value), nil case 1: if value > math.MaxInt64 { return nil, fmt.Errorf("negative integer magnitude %d overflows int64", value) } return -int64(value) - 1, nil case 3: if value > maxCBORStringBytes { return nil, fmt.Errorf("CBOR string has %d bytes; maximum is %d", value, maxCBORStringBytes) } buf := make([]byte, value) if _, err := io.ReadFull(r, buf); err != nil { return nil, fmt.Errorf("read string: %w", err) } return string(buf), nil case 7: switch value { case 20: return false, nil case 21: return true, nil case 22: return nil, nil default: return nil, fmt.Errorf("unsupported simple value %d", value) } default: return nil, fmt.Errorf("unsupported CBOR major type %d", majorType) }}
func mapString(m map[string]any, key string) string { if s, ok := m[key].(string); ok { return s } return ""}
func mapInt(m map[string]any, key string) int64 { switch n := m[key].(type) { case int64: return n } return 0}
func mapStringOpt(m map[string]any, key string) *string { if s, ok := m[key].(string); ok && s != "" { return &s } return nil}