mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-08 16:48:51 +00:00
fix multiline handler thing for when we're don't have any stdin.
This commit is contained in:
29
helpers.go
29
helpers.go
@@ -49,7 +49,7 @@ func getJsonsOrBlank() iter.Seq[string] {
|
|||||||
var curr strings.Builder
|
var curr strings.Builder
|
||||||
|
|
||||||
return func(yield func(string) bool) {
|
return func(yield func(string) bool) {
|
||||||
for stdinLine := range getStdinLinesOrBlank() {
|
hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
|
||||||
// we're look for an event, but it may be in multiple lines, so if json parsing fails
|
// we're look for an event, but it may be in multiple lines, so if json parsing fails
|
||||||
// we'll try the next line until we're successful
|
// we'll try the next line until we're successful
|
||||||
curr.WriteString(stdinLine)
|
curr.WriteString(stdinLine)
|
||||||
@@ -57,24 +57,34 @@ func getJsonsOrBlank() iter.Seq[string] {
|
|||||||
|
|
||||||
var dummy any
|
var dummy any
|
||||||
if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil {
|
if err := json.Unmarshal([]byte(stdinEvent), &dummy); err != nil {
|
||||||
continue
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if !yield(stdinEvent) {
|
if !yield(stdinEvent) {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
curr.Reset()
|
curr.Reset()
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
if !hasStdin {
|
||||||
|
yield("{}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStdinLinesOrBlank() iter.Seq[string] {
|
func getStdinLinesOrBlank() iter.Seq[string] {
|
||||||
return func(yield func(string) bool) {
|
return func(yield func(string) bool) {
|
||||||
if hasStdinLines := writeStdinLinesOrNothing(yield); !hasStdinLines {
|
hasStdin := writeStdinLinesOrNothing(func(stdinLine string) bool {
|
||||||
return
|
if !yield(stdinLine) {
|
||||||
} else {
|
return false
|
||||||
return
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
if !hasStdin {
|
||||||
|
yield("")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,10 +117,7 @@ func writeStdinLinesOrNothing(yield func(string) bool) (hasStdinLines bool) {
|
|||||||
}
|
}
|
||||||
hasEmittedAtLeastOne = true
|
hasEmittedAtLeastOne = true
|
||||||
}
|
}
|
||||||
if !hasEmittedAtLeastOne {
|
return hasEmittedAtLeastOne
|
||||||
yield("")
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
} else {
|
} else {
|
||||||
// not piped
|
// not piped
|
||||||
return false
|
return false
|
||||||
|
|||||||
Reference in New Issue
Block a user