EventSource icon indicating copy to clipboard operation
EventSource copied to clipboard

Add `\r\n` to Parser

Open Lakr233 opened this issue 9 months ago • 2 comments

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)
}

Lakr233 avatar Feb 26 '25 05:02 Lakr233

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?

Recouse avatar Feb 26 '25 12:02 Recouse

I will open a PR soon. I didn't expect your response to be so quick. Thanks.

Lakr233 avatar Feb 26 '25 12:02 Lakr233

Watching.

JadianZheng avatar Jul 04 '25 06:07 JadianZheng

watching

Lakr233 avatar Jul 05 '25 05:07 Lakr233

Added in #28

Recouse avatar Jul 07 '25 06:07 Recouse

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.

JadianZheng avatar Jul 15 '25 01:07 JadianZheng