|
| 1 | +// Copyright 2025, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package util |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "io" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type LineOutput struct { |
| 14 | + Line string |
| 15 | + Error error |
| 16 | +} |
| 17 | + |
| 18 | +type lineBuf struct { |
| 19 | + buf []byte |
| 20 | + inLongLine bool |
| 21 | +} |
| 22 | + |
| 23 | +const maxLineLength = 128 * 1024 |
| 24 | + |
| 25 | +func ReadLineWithTimeout(ch chan LineOutput, timeout time.Duration) (string, error) { |
| 26 | + select { |
| 27 | + case output := <-ch: |
| 28 | + if output.Error != nil { |
| 29 | + return "", output.Error |
| 30 | + } |
| 31 | + return output.Line, nil |
| 32 | + case <-time.After(timeout): |
| 33 | + return "", context.DeadlineExceeded |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func streamToLines_processBuf(lineBuf *lineBuf, readBuf []byte, lineFn func([]byte)) { |
| 38 | + for len(readBuf) > 0 { |
| 39 | + nlIdx := bytes.IndexByte(readBuf, '\n') |
| 40 | + if nlIdx == -1 { |
| 41 | + if lineBuf.inLongLine || len(lineBuf.buf)+len(readBuf) > maxLineLength { |
| 42 | + lineBuf.buf = nil |
| 43 | + lineBuf.inLongLine = true |
| 44 | + return |
| 45 | + } |
| 46 | + lineBuf.buf = append(lineBuf.buf, readBuf...) |
| 47 | + return |
| 48 | + } |
| 49 | + if !lineBuf.inLongLine && len(lineBuf.buf)+nlIdx <= maxLineLength { |
| 50 | + line := append(lineBuf.buf, readBuf[:nlIdx]...) |
| 51 | + lineFn(line) |
| 52 | + } |
| 53 | + lineBuf.buf = nil |
| 54 | + lineBuf.inLongLine = false |
| 55 | + readBuf = readBuf[nlIdx+1:] |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func StreamToLines(input io.Reader, lineFn func([]byte)) error { |
| 60 | + var lineBuf lineBuf |
| 61 | + readBuf := make([]byte, 64*1024) |
| 62 | + for { |
| 63 | + n, err := input.Read(readBuf) |
| 64 | + streamToLines_processBuf(&lineBuf, readBuf[:n], lineFn) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// starts a goroutine to drive the channel |
| 72 | +// line output does not include the trailing newline |
| 73 | +func StreamToLinesChan(input io.Reader) chan LineOutput { |
| 74 | + ch := make(chan LineOutput) |
| 75 | + go func() { |
| 76 | + defer close(ch) |
| 77 | + err := StreamToLines(input, func(line []byte) { |
| 78 | + ch <- LineOutput{Line: string(line)} |
| 79 | + }) |
| 80 | + if err != nil && err != io.EOF { |
| 81 | + ch <- LineOutput{Error: err} |
| 82 | + } |
| 83 | + }() |
| 84 | + return ch |
| 85 | +} |
| 86 | + |
| 87 | +// LineWriter is an io.Writer that processes data line-by-line via a callback. |
| 88 | +// Lines do not include the trailing newline. Lines longer than maxLineLength are dropped. |
| 89 | +type LineWriter struct { |
| 90 | + lineBuf lineBuf |
| 91 | + lineFn func([]byte) |
| 92 | +} |
| 93 | + |
| 94 | +// NewLineWriter creates a new LineWriter with the given callback function. |
| 95 | +func NewLineWriter(lineFn func([]byte)) *LineWriter { |
| 96 | + return &LineWriter{ |
| 97 | + lineFn: lineFn, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Write implements io.Writer, processing the data and calling the callback for each complete line. |
| 102 | +func (lw *LineWriter) Write(p []byte) (n int, err error) { |
| 103 | + streamToLines_processBuf(&lw.lineBuf, p, lw.lineFn) |
| 104 | + return len(p), nil |
| 105 | +} |
| 106 | + |
| 107 | +// Flush outputs any remaining buffered data as a final line. |
| 108 | +// Should be called when the input stream is complete (e.g., at EOF). |
| 109 | +func (lw *LineWriter) Flush() { |
| 110 | + if len(lw.lineBuf.buf) > 0 && !lw.lineBuf.inLongLine { |
| 111 | + lw.lineFn(lw.lineBuf.buf) |
| 112 | + lw.lineBuf.buf = nil |
| 113 | + } |
| 114 | +} |
0 commit comments