The Providers page asks IsOpenCode for each model of each provider, and it parsed "//"+host with url.Parse each time: url.parse was 20 MB of what 20 warm rounds of drift, state and providers allocated with the owner's real accounts, OpenCodeURL's share 7.5 MB. openCodeAt takes the host as Host gives it: whatever is before the last '@' aside, an IPv6 literal never OpenCode's, the port aside, then the same openCodeHost check OpenCodeURL ends in. Run on mote (mote-run, Go 1.26.8): - gofmt clean; go vet ok, also GOOS=windows and GOOS=linux. - TestOpenCodeAtReadsTheHostAsAURLWould alone: ok. It checks openCodeAt against OpenCodeURL for 18 hosts (user info, ports, IPv6, a trailing dot, look-alikes such as opencode.ai@evil.com and opencode.ai.evil.com) and that a look allocates nothing. - Broken by hand (openCodeAt answering through OpenCodeURL): red, "a look allocated 3 times, want 0". - go test -timeout 45m ./internal/provider ./internal/gateway: ok (156s, 681s), at 606743831; this head is it rebased on main. Real use: the owner's real-account sandbox, offline (proxy to :9), 20 warm rounds of /api/drift, /api/state and /api/providers, at this head: - net/url.parse 20.5 MB -> 5 MB (what is left is openCodeGateway and others, not IsOpenCode); all allocated 371 MB -> 327 MB. - /api/state and /api/drift bytes identical to 6a7a690be's (cmp); /api/providers differs only in each provider's "fetched" time.
23 lines
852 B
Go
23 lines
852 B
Go
package provider
|
|
|
|
import "testing"
|
|
|
|
// IsOpenCode reads its host as OpenCodeURL reads a URL, with no URL
|
|
// parsed for it: it is asked for every model of the providers' page.
|
|
func TestOpenCodeAtReadsTheHostAsAURLWould(t *testing.T) {
|
|
for _, h := range []string{
|
|
"opencode.ai", "OpenCode.AI", "opencode.ai.", "opencode.ai:443",
|
|
"zen.opencode.ai", "api.opencode.ai:8443", "u:p@opencode.ai",
|
|
"u@zen.opencode.ai:1", "notopencode.ai", "opencode.ai.evil.com",
|
|
"evil.com", "evil.com@", "opencode.ai@evil.com", "[::1]:443",
|
|
"[::1]", "127.0.0.1:3425", "", "localhost",
|
|
} {
|
|
if got, want := openCodeAt(h), OpenCodeURL("//"+h); got != want {
|
|
t.Errorf("%q: %v, a URL says %v", h, got, want)
|
|
}
|
|
}
|
|
if n := testing.AllocsPerRun(100, func() { openCodeAt("u@zen.opencode.ai:8443") }); n != 0 {
|
|
t.Fatalf("a look allocated %v times, want 0", n)
|
|
}
|
|
}
|