-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.go
More file actions
39 lines (33 loc) · 1.02 KB
/
Copy pathexport.go
File metadata and controls
39 lines (33 loc) · 1.02 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
package auditlog
import (
"bufio"
"encoding/json/jsontext"
"encoding/json/v2"
"fmt"
"io"
)
// encodeEvent marshals a single Event as JSON to the encoder using the
// canonical escaping options (HTML + JS escaping). Returns a wrapped
// ErrRenderFailed on encoding failure. Shared by writeEventsNDJSON and
// NDJSONStreamer.OnEvent so both paths produce identical NDJSON.
func encodeEvent(encoder *jsontext.Encoder, evt Event) error {
err := json.MarshalEncode(encoder, evt,
jsontext.EscapeForHTML(true),
jsontext.EscapeForJS(true),
)
if err != nil {
return fmt.Errorf("%w: encode event %d: %w", ErrRenderFailed, evt.Sequence, err)
}
return nil
}
// writeEventsNDJSON writes events as newline-delimited JSON (one JSON object per line).
func writeEventsNDJSON(writer io.Writer, events []Event) error {
buf := bufio.NewWriter(writer)
encoder := jsontext.NewEncoder(buf)
for _, evt := range events {
if err := encodeEvent(encoder, evt); err != nil {
return err
}
}
return wrapFlushError("ndjson buffer", buf.Flush())
}