Documentation
¶
Index ¶
Constants ¶
const ( FieldNameData = FieldName("data") FieldNameEvent = FieldName("event") FieldNameRetry = FieldName("retry") FieldNameID = FieldName("id") )
Valid field names.
Variables ¶
var ErrUnexpectedEOF = errors.New("go-sse: unexpected end of input")
ErrUnexpectedEOF is returned when the input is completely parsed but no complete field was found at the end.
Functions ¶
func NewlineIndex ¶
NewlineIndex returns the index of the first occurrence of a newline sequence (\n, \r, or \r\n). It also returns the sequence's length. If no sequence is found, index is equal to len(s) and length is 0.
Types ¶
type ChunkScanner ¶
type ChunkScanner struct {
// contains filtered or unexported fields
}
ChunkScanner reads through the lines of a byte slice. The returned chunks include the line endings. A line's ending is specified in the Event Stream standard's documentation: https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
func NewChunkScanner ¶
func NewChunkScanner(buffer []byte) ChunkScanner
NewChunkScanner creates a ChunkScanner that will start scanning from the given buffer.
func (*ChunkScanner) Chunk ¶
func (s *ChunkScanner) Chunk() (chunk []byte, endsInNewline bool)
Chunk returns the last scanned chunk of data. It also returns a flag that indicates whether the data ends in a newline sequence.
The returned chunk is nil only if no scan was run or if there was nothing to read from the provided source, otherwise its length is always greater than 0.
func (*ChunkScanner) Reset ¶
func (s *ChunkScanner) Reset(buffer []byte)
Reset changes the scanner's buffer. Calling Scan after reset will return the first chunk from this buffer.
func (*ChunkScanner) Scan ¶
func (s *ChunkScanner) Scan() bool
Scan retrieves the next chunk from the buffer. It returns true if there is data left in the buffer. Calling Scan multiple times after there is no data will change the buffer!
type Field ¶
A Field represents an unprocessed field of a single event. The Name is the field's identifier, which is used to process the fields afterwards. The Value is not owned by the Field struct.
As a special case, if a parser (ByteParser or ReaderParser) returns a field without a name, it means that a whole event was parsed. In other words, all the fields before the one without a name and after another such field are part of the same event.
type FieldParser ¶ added in v0.4.3
type FieldParser struct {
// contains filtered or unexported fields
}
FieldParser extracts fields from a byte slice.
func NewFieldParser ¶ added in v0.4.3
func NewFieldParser(b []byte) *FieldParser
NewFieldParser creates a parser that extracts fields from the given byte slice.
func (*FieldParser) Err ¶ added in v0.4.3
func (b *FieldParser) Err() error
Err returns the last error encountered by the parser. It is either nil or ErrUnexpectedEOF.
func (*FieldParser) Field ¶ added in v0.4.3
func (b *FieldParser) Field() Field
Field returns the last parsed field.
func (*FieldParser) Reset ¶ added in v0.4.3
func (b *FieldParser) Reset(p []byte)
Reset changes the buffer ByteParser parses fields from.
func (*FieldParser) Scan ¶ added in v0.4.3
func (b *FieldParser) Scan() bool
Scan parses the next available filed in the remaining buffer. It returns false if there are no fields to parse.
type Parser ¶ added in v0.4.3
type Parser struct {
// contains filtered or unexported fields
}
Parser extracts fields from a reader. Reading is buffered using a bufio.Scanner. The Parser also removes the UTF-8 BOM if it exists.