| 363 | } |
| 364 | |
| 365 | func decode(input io.Reader, output chan<- Event) { |
| 366 | defer close(output) |
| 367 | var lastBuffer []byte |
| 368 | buffer := make([]byte, 4096) |
| 369 | currentState := stateInit |
| 370 | for { |
| 371 | n, err := input.Read(buffer) |
| 372 | if err != nil { |
| 373 | if !errors.Is(err, io.EOF) { |
| 374 | panic(fmt.Errorf("failed to read from input (%w)", err)) |
| 375 | } |
| 376 | break |
| 377 | } |
| 378 | if n == 0 { |
| 379 | break |
| 380 | } |
| 381 | |
| 382 | lines := bytes.Split(append(lastBuffer, buffer[:n]...), []byte("\n")) |
| 383 | lastBuffer = lines[len(lines)-1] |
| 384 | lines = lines[:len(lines)-1] |
| 385 | for _, line := range lines { |
| 386 | line = bytes.TrimSuffix(line, []byte("\r")) |
| 387 | currentState = parseLine(currentState, line, output) |
| 388 | } |
| 389 | } |
| 390 | _ = parseLine(currentState, lastBuffer, output) |
| 391 | } |
| 392 | |
| 393 | func tryParseJSONLine(line []byte) *jsonTestEvent { |
| 394 | if len(line) == 0 || line[0] != 123 { |