package server import ( "strings" "unicode/utf8" ) // chatShellOutput is the bounded tail of one operator command's merged output. // Offsets are absolute bytes of everything the command wrote, so a client can // tell a continuation from a gap. The buffer may grow to twice its limit before // compacting, which keeps a flooding command from copying the tail per write. type chatShellOutput struct { limit int data []byte start int64 total int64 emitted int64 } func newChatShellOutput(limit int) *chatShellOutput { return &chatShellOutput{limit: limit} } func (o *chatShellOutput) write(p []byte) { o.data = append(o.data, p...) o.total += int64(len(p)) if len(o.data) <= 2*o.limit { return } cut := runeStartAfter(o.data, len(o.data)-o.limit) o.data = append([]byte(nil), o.data[cut:]...) o.start += int64(cut) } // tail returns at most max retained bytes ending at the newest byte, starting // on a character boundary, with the absolute offset of its first byte. func (o *chatShellOutput) tail(max int) (string, int64) { from := 0 if len(o.data) > max { from = runeStartAfter(o.data, len(o.data)-max) } return validChatShellText(o.data[from:]), o.start + int64(from) } // pending returns the oldest output not yet emitted, at most max bytes, and // marks it emitted. An incomplete trailing character is held back unless // final is set. func (o *chatShellOutput) pending(max int, final bool) (text string, offset, end int64, ok bool) { text, offset, end, ok = o.chunk(o.emitted, o.total, max, final) if ok { o.emitted = end } return text, offset, end, ok } // unemitted is how many written bytes no frame has carried yet. func (o *chatShellOutput) unemitted() int64 { return o.total - max(o.emitted, o.start) } // skipUnemitted gives up all but the newest max unemitted bytes (watchers // that stopped draining): the next frame starts after a visible gap. func (o *chatShellOutput) skipUnemitted(max int) { o.emitted = o.newestFrom(o.emitted, max) } // newestFrom moves from forward so that at most max retained bytes follow // it, onto a character boundary. func (o *chatShellOutput) newestFrom(from int64, max int) int64 { from = maxInt64(from, o.start) if o.total-from <= int64(max) { return from } return o.start + int64(runeStartAfter(o.data, int(o.total-int64(max)-o.start))) } // chunk returns the retained bytes from absolute offset from (the oldest // retained byte when from was dropped) up to limit, at most max of them, // never splitting a character inside the retained bytes. An incomplete // character at the newest byte is held back unless final is set. offset is // where the text starts, which is after from when from was dropped. func (o *chatShellOutput) chunk(from, limit int64, max int, final bool) (text string, offset, end int64, ok bool) { from = maxInt64(from, o.start) limit = min(limit, o.total) if from >= limit { return "", 0, 0, false } data := o.data[from-o.start : limit-o.start] if len(data) > max { data = data[:runeStartBefore(data, max)] } else if limit < o.total { data = data[:runeStartBefore(o.data[from-o.start:], len(data))] } else if !final { data = data[:completeUTF8Prefix(data)] } if len(data) == 0 { return "", 0, 0, false } return validChatShellText(data), from, from + int64(len(data)), true } func maxInt64(a, b int64) int64 { if a > b { return a } return b } // runeStartBefore moves index back (at most three bytes) to a byte that // starts a character, so a cut at it never splits one (zero when the only // character there is incomplete); invalid bytes, which start nothing, are // cut where they are. func runeStartBefore(data []byte, index int) int { for back := 0; back < utf8.UTFMax && index-back >= 0; back++ { if index-back >= len(data) || utf8.RuneStart(data[index-back]) { return index - back } } return index } // runeStartAfter moves index forward (at most three bytes) to the first byte // that starts a character, so a cut never splits one. func runeStartAfter(data []byte, index int) int { for step := 0; step < utf8.UTFMax-1 && index < len(data) && !utf8.RuneStart(data[index]); step++ { index++ } return index } // completeUTF8Prefix is the length of data without a trailing partial // character that a later read may complete. func completeUTF8Prefix(data []byte) int { for back := 1; back <= utf8.UTFMax-1 && back <= len(data); back++ { if utf8.RuneStart(data[len(data)-back]) { if !utf8.FullRune(data[len(data)-back:]) { return len(data) - back } break } } return len(data) } // validChatShellText keeps binary output representable in a JSON frame. func validChatShellText(data []byte) string { return strings.ToValidUTF8(string(data), "�") }