-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathclient.go
More file actions
175 lines (162 loc) · 4.46 KB
/
Copy pathclient.go
File metadata and controls
175 lines (162 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package streaming
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// NewRequest returns an http.Request against the streaming API for query.
func NewRequest(baseURL string, query string) (*http.Request, error) {
u := baseURL + "/search/stream?q=" + url.QueryEscape(query)
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "text/event-stream")
return req, nil
}
// Decoder decodes streaming events from a Server Sent Event stream. We only
// support streams which are generated by Sourcegraph. IE this is not a fully
// compliant Server Sent Events decoder.
type Decoder struct {
OnProgress func(*Progress)
OnMatches func([]EventMatch)
OnFilters func([]*EventFilter)
OnAlert func(*EventAlert)
OnError func(*EventError)
OnUnknown func(event, data []byte)
}
func (rr Decoder) ReadAll(r io.Reader) error {
const maxPayloadSize = 10 * 1024 * 1024 // 10mb
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 0, 4096), maxPayloadSize)
// bufio.ScanLines, except we look for two \n\n which separate events.
split := func(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte("\n\n")); i >= 0 {
return i + 2, data[:i], nil
}
// If we're at EOF, we have a final, non-terminated event. This should
// be empty.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
scanner.Split(split)
for scanner.Scan() {
// event: $event\n
// data: json($data)\n\n
data := scanner.Bytes()
before, after, ok := bytes.Cut(data, []byte("\n"))
if !ok {
return fmt.Errorf("malformed event, no newline: %s", data)
}
eventK, event := splitColon(before)
dataK, data := splitColon(after)
if !bytes.Equal(eventK, []byte("event")) {
return fmt.Errorf("malformed event, expected event: %s", eventK)
}
if !bytes.Equal(dataK, []byte("data")) {
return fmt.Errorf("malformed event %s, expected data: %s", eventK, dataK)
}
if bytes.Equal(event, []byte("progress")) {
if rr.OnProgress == nil {
continue
}
var d Progress
if err := json.Unmarshal(data, &d); err != nil {
return fmt.Errorf("failed to decode progress payload: %w", err)
}
rr.OnProgress(&d)
} else if bytes.Equal(event, []byte("matches")) {
if rr.OnMatches == nil {
continue
}
var d []eventMatchUnmarshaller
if err := json.Unmarshal(data, &d); err != nil {
return fmt.Errorf("failed to decode matches payload: %w", err)
}
m := make([]EventMatch, 0, len(d))
for _, e := range d {
m = append(m, e.EventMatch)
}
rr.OnMatches(m)
} else if bytes.Equal(event, []byte("filters")) {
if rr.OnFilters == nil {
continue
}
var d []*EventFilter
if err := json.Unmarshal(data, &d); err != nil {
return fmt.Errorf("failed to decode filters payload: %w", err)
}
rr.OnFilters(d)
} else if bytes.Equal(event, []byte("alert")) {
if rr.OnAlert == nil {
continue
}
var d EventAlert
if err := json.Unmarshal(data, &d); err != nil {
return fmt.Errorf("failed to decode alert payload: %w", err)
}
rr.OnAlert(&d)
} else if bytes.Equal(event, []byte("error")) {
if rr.OnError == nil {
continue
}
var d EventError
if err := json.Unmarshal(data, &d); err != nil {
return fmt.Errorf("failed to decode error payload: %w", err)
}
rr.OnError(&d)
} else if bytes.Equal(event, []byte("done")) {
// Always the last event
break
} else {
if rr.OnUnknown == nil {
continue
}
rr.OnUnknown(event, data)
}
}
return scanner.Err()
}
func splitColon(data []byte) ([]byte, []byte) {
before, after, ok := bytes.Cut(data, []byte(":"))
if !ok {
return bytes.TrimSpace(data), nil
}
return bytes.TrimSpace(before), bytes.TrimSpace(after)
}
type eventMatchUnmarshaller struct {
EventMatch
}
func (r *eventMatchUnmarshaller) UnmarshalJSON(b []byte) error {
var typeU struct {
Type MatchType `json:"type"`
}
if err := json.Unmarshal(b, &typeU); err != nil {
return err
}
switch typeU.Type {
case ContentMatchType:
r.EventMatch = &EventContentMatch{}
case RepoMatchType:
r.EventMatch = &EventRepoMatch{}
case SymbolMatchType:
r.EventMatch = &EventSymbolMatch{}
case CommitMatchType:
r.EventMatch = &EventCommitMatch{}
case PathMatchType:
r.EventMatch = &EventPathMatch{}
default:
return fmt.Errorf("unknown MatchType %v", typeU.Type)
}
return json.Unmarshal(b, r.EventMatch)
}