EventSource
EventSource copied to clipboard
Add `\r\n` to Parser
Some of the cloud service may use \r\n as the separator when processing events.
Here is the modified code that I suggested to change.
private func splitBuffer(for data: Data) -> (completeData: [Data], remainingData: Data) {
let possibleSeparators: [[UInt8]] = [
[Self.lf, Self.lf],
[Self.cr, Self.lf],
]
var rawMessages = [Data]()
for separator in possibleSeparators {
// If event separator is not present do not parse any unfinished messages
guard let lastSeparator = data.lastRange(of: separator) else { continue }
let bufferRange = data.startIndex ..< lastSeparator.upperBound
let remainingRange = lastSeparator.upperBound ..< data.endIndex
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, visionOS 1.0, *) {
rawMessages = data[bufferRange].split(separator: separator)
} else {
rawMessages = data[bufferRange].split(by: separator)
}
return (rawMessages, data[remainingRange])
}
return ([], data)
}
Hi, yes, that would be a great addition. Thank you for the suggestion! Would you like to open the PR for that, or would you prefer that I handle it?
I will open a PR soon. I didn't expect your response to be so quick. Thanks.
Watching.
watching
Added in #28
That still some problem, it will lost the end message some situation. I still work on it, maybe add some test later, but mark here first.