Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/oklog/ulid/v2"
Expand Down Expand Up @@ -402,3 +403,101 @@ func mib(c ByteCount) ByteCount {
func gib(c ByteCount) ByteCount {
return c * ByteCount(1024) * ByteCount(1024) * ByteCount(1024)
}

func ByteCountHumanReadable(count ByteCount) string {
trimFloatString := func(value float64, precision int, suffix string) string {
s := fmt.Sprintf("%."+strconv.Itoa(precision)+"f", value)
s = strings.TrimRight(s, "0")
s = strings.TrimRight(s, ".")
return s + suffix
}

if 1024*1024*1024*1024 <= count {
return trimFloatString(
float64(1000*count/(1024*1024*1024*1024))/1000.0,
2,
"tib",
)
} else if 1024*1024*1024 <= count {
return trimFloatString(
float64(1000*count/(1024*1024*1024))/1000.0,
2,
"gib",
)
} else if 1024*1024 <= count {
return trimFloatString(
float64(1000*count/(1024*1024))/1000.0,
2,
"mib",
)
} else if 1024 <= count {
return trimFloatString(
float64(1000*count/(1024))/1000.0,
2,
"kib",
)
} else {
return fmt.Sprintf("%db", count)
}
}

func ParseByteCount(humanReadable string) (ByteCount, error) {
humanReadableLower := strings.ToLower(humanReadable)
tibLower := "tib"
gibLower := "gib"
mibLower := "mib"
kibLower := "kib"
bLower := "b"
if strings.HasSuffix(humanReadableLower, tibLower) {
countFloat, err := strconv.ParseFloat(
humanReadableLower[0:len(humanReadableLower)-len(tibLower)],
64,
)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countFloat * 1024 * 1024 * 1024 * 1024), nil
} else if strings.HasSuffix(humanReadableLower, gibLower) {
countFloat, err := strconv.ParseFloat(
humanReadableLower[0:len(humanReadableLower)-len(gibLower)],
64,
)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countFloat * 1024 * 1024 * 1024), nil
} else if strings.HasSuffix(humanReadableLower, mibLower) {
countFloat, err := strconv.ParseFloat(
humanReadableLower[0:len(humanReadableLower)-len(mibLower)],
64,
)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countFloat * 1024 * 1024), nil
} else if strings.HasSuffix(humanReadableLower, kibLower) {
countFloat, err := strconv.ParseFloat(
humanReadableLower[0:len(humanReadableLower)-len(kibLower)],
64,
)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countFloat * 1024), nil
} else if strings.HasSuffix(humanReadableLower, bLower) {
countFloat, err := strconv.ParseFloat(
humanReadableLower[0:len(humanReadableLower)-len(bLower)],
64,
)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countFloat), nil
} else {
countInt, err := strconv.ParseInt(humanReadableLower, 10, 63)
if err != nil {
return ByteCount(0), err
}
return ByteCount(countInt), nil
}
}
35 changes: 35 additions & 0 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,38 @@ func TestMultiHopId(t *testing.T) {
assert.Equal(t, tail, ids[2])
assert.Equal(t, m3, m4)
}

func TestByteCount(t *testing.T) {
assert.Equal(t, ByteCountHumanReadable(ByteCount(0)), "0b")
assert.Equal(t, ByteCountHumanReadable(ByteCount(5*1024*1024*1024*1024)), "5tib")

count, err := ParseByteCount("2")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(2))
assert.Equal(t, ByteCountHumanReadable(count), "2b")

count, err = ParseByteCount("5B")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(5))
assert.Equal(t, ByteCountHumanReadable(count), "5b")

count, err = ParseByteCount("123KiB")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(123*1024))
assert.Equal(t, ByteCountHumanReadable(count), "123kib")

count, err = ParseByteCount("5MiB")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(5*1024*1024))
assert.Equal(t, ByteCountHumanReadable(count), "5mib")

count, err = ParseByteCount("1.7GiB")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(17*1024*1024*1024)/ByteCount(10))
assert.Equal(t, ByteCountHumanReadable(count), "1.7gib")

count, err = ParseByteCount("13.1TiB")
assert.Equal(t, err, nil)
assert.Equal(t, count, ByteCount(131*1024*1024*1024*1024)/ByteCount(10))
assert.Equal(t, ByteCountHumanReadable(count), "13.1tib")
}
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/oklog/ulid/v2 v2.1.1
github.com/quic-go/quic-go v0.52.0
golang.org/x/crypto v0.38.0
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b
golang.org/x/net v0.40.0
golang.org/x/crypto v0.39.0
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476
golang.org/x/net v0.41.0
golang.org/x/term v0.32.0
google.golang.org/protobuf v1.36.6
src.agwa.name/tlshacks v0.0.0-20231008131857-90d701ba3225
Expand All @@ -25,11 +25,11 @@ require (
github.com/onsi/ginkgo/v2 v2.23.4 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/mock v0.5.2 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.14.0 // indirect
golang.org/x/mod v0.25.0 // indirect
golang.org/x/sync v0.15.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.25.0 // indirect
golang.org/x/tools v0.33.0 // indirect
golang.org/x/text v0.26.0 // indirect
golang.org/x/tools v0.34.0 // indirect

)

Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,29 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b h1:QoALfVG9rhQ/M7vYDScfPdWjGL9dlsVVM5VGh7aKoAA=
golang.org/x/exp v0.0.0-20250531010427-b6e5de432a8b/go.mod h1:U6Lno4MTRCDY+Ba7aCcauB9T60gsv5s4ralQzP72ZoQ=
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 h1:bsqhLWFR6G6xiQcb+JoGqdKdRU6WzPWmK8E0jxTjzo4=
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
Expand All @@ -66,9 +76,13 @@ golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M=
golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
Expand Down
Loading
Loading