Files
coder/codersdk/serversentevents.go
Kyle Carberry 7ad87505c8 chore: move agent functions from codersdk into agentsdk (#5903)
* chore: rename `AgentConn` to `WorkspaceAgentConn`

The codersdk was becoming bloated with consts for the workspace
agent that made no sense to a reader. `Tailnet*` is an example
of these consts.

* chore: remove `Get` prefix from *Client functions

* chore: remove `BypassRatelimits` option in `codersdk.Client`

It feels wrong to have this as a direct option because it's so infrequently
needed by API callers. It's better to directly modify headers in the two
places that we actually use it.

* Merge `appearance.go` and `buildinfo.go` into `deployment.go`

* Merge `experiments.go` and `features.go` into `deployment.go`

* Fix `make gen` referencing old type names

* Merge `error.go` into `client.go`

`codersdk.Response` lived in `error.go`, which is wrong.

* chore: refactor workspace agent functions into agentsdk

It was odd conflating the codersdk that clients should use
with functions that only the agent should use. This separates
them into two SDKs that are closely coupled, but separate.

* Merge `insights.go` into `deployment.go`

* Merge `organizationmember.go` into `organizations.go`

* Merge `quota.go` into `workspaces.go`

* Rename `sse.go` to `serversentevents.go`

* Rename `codersdk.WorkspaceAppHostResponse` to `codersdk.AppHostResponse`

* Format `.vscode/settings.json`

* Fix outdated naming in `api.ts`

* Fix app host response

* Fix unsupported type

* Fix imported type
2023-01-29 15:47:24 -06:00

96 lines
2.1 KiB
Go

package codersdk
import (
"bufio"
"context"
"fmt"
"io"
"strings"
"golang.org/x/xerrors"
"github.com/coder/coder/coderd/tracing"
)
type ServerSentEvent struct {
Type ServerSentEventType `json:"type"`
Data interface{} `json:"data"`
}
type ServerSentEventType string
const (
ServerSentEventTypePing ServerSentEventType = "ping"
ServerSentEventTypeData ServerSentEventType = "data"
ServerSentEventTypeError ServerSentEventType = "error"
)
func ServerSentEventReader(ctx context.Context, rc io.ReadCloser) func() (*ServerSentEvent, error) {
_, span := tracing.StartSpan(ctx)
defer span.End()
reader := bufio.NewReader(rc)
nextLineValue := func(prefix string) ([]byte, error) {
var (
line string
err error
)
for {
line, err = reader.ReadString('\n')
if err != nil {
return nil, xerrors.Errorf("reading next string: %w", err)
}
if strings.TrimSpace(line) != "" {
break
}
}
if !strings.HasPrefix(line, fmt.Sprintf("%s: ", prefix)) {
return nil, xerrors.Errorf("expecting %s prefix, got: %s", prefix, line)
}
s := strings.TrimPrefix(line, fmt.Sprintf("%s: ", prefix))
s = strings.TrimSpace(s)
return []byte(s), nil
}
nextEvent := func() (*ServerSentEvent, error) {
for {
t, err := nextLineValue("event")
if err != nil {
return nil, xerrors.Errorf("reading next line value: %w", err)
}
switch ServerSentEventType(t) {
case ServerSentEventTypePing:
return &ServerSentEvent{
Type: ServerSentEventTypePing,
}, nil
case ServerSentEventTypeData:
d, err := nextLineValue("data")
if err != nil {
return nil, xerrors.Errorf("reading next line value: %w", err)
}
return &ServerSentEvent{
Type: ServerSentEventTypeData,
Data: d,
}, nil
case ServerSentEventTypeError:
d, err := nextLineValue("data")
if err != nil {
return nil, xerrors.Errorf("reading next line value: %w", err)
}
return &ServerSentEvent{
Type: ServerSentEventTypeError,
Data: d,
}, nil
default:
return nil, xerrors.Errorf("unknown event type: %s", t)
}
}
}
return nextEvent
}