From 73e443747e0f0d4c20363dd6bd72f6f904d2bea0 Mon Sep 17 00:00:00 2001 From: Gui Sabran Date: Thu, 26 Dec 2024 19:45:26 -0800 Subject: [PATCH] flush stdout buffer --- ...el+StdioPipe.swift => DataChannel+Stdio.swift} | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) rename Sources/JSONRPC/{DataChannel+StdioPipe.swift => DataChannel+Stdio.swift} (58%) diff --git a/Sources/JSONRPC/DataChannel+StdioPipe.swift b/Sources/JSONRPC/DataChannel+Stdio.swift similarity index 58% rename from Sources/JSONRPC/DataChannel+StdioPipe.swift rename to Sources/JSONRPC/DataChannel+Stdio.swift index 9de2eb5..721979a 100644 --- a/Sources/JSONRPC/DataChannel+StdioPipe.swift +++ b/Sources/JSONRPC/DataChannel+Stdio.swift @@ -21,12 +21,23 @@ extension FileHandle { } extension DataChannel { + @available(*, deprecated, renamed: "stdio", message: "Use stdio instead") public static func stdioPipe() -> DataChannel { + stdio() + } + + public static func stdio() -> DataChannel { - let writeHandler: DataChannel.WriteHandler = { - FileHandle.standardOutput.write($0) + let writeHandler: DataChannel.WriteHandler = { data in + // Add a line break to flush the stdout buffer. + var data = data + data.append(contentsOf: lineBreak) + + FileHandle.standardOutput.write(data) } return DataChannel(writeHandler: writeHandler, dataSequence: FileHandle.standardInput.dataStream) } + + private static let lineBreak = [UInt8(ascii: "\n")] }