Skip to content

Commit

Permalink
Internalizing the message framing system
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Nov 30, 2023
1 parent 3bef8f4 commit af169ca
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 13 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ For the most part, this library strives to be a straightforward version of the s
- `Server`: a protocol that describes the essential server functionality
- `Snippet`: makes it easier to interpret the contents of completion results
- `TokenRepresentation`: maintains the state of a document's semantic tokens
- `AsyncMessageFramingSequence`: parsers an input AsyncSequence with LSP's HTTP header-based message framing (see also `MessageFraming`)
- `AsyncByteSequence`: transforms a sequence of Data into a sequence of bytes
- `DataChannel.withMessageFraming`: wraps an existing JSONRPC DataChannel up with HTTP header-based message framing

## Supported Features

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// Converts a sequence of Data objects into a sequence of bytes.
public struct AsyncByteSequence<Base> : AsyncSequence where Base : AsyncSequence, Base.Element == Data {
struct AsyncByteSequence<Base> : AsyncSequence where Base : AsyncSequence, Base.Element == Data {
public typealias Element = UInt8

public struct AsyncIterator : AsyncIteratorProtocol {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

/// Sequence that reads data framed by the LSP base protocol specification.
public struct AsyncMessageFramingSequence<Base> : AsyncSequence where Base : AsyncSequence, Base.Element == UInt8 {
struct AsyncMessageFramingSequence<Base> : AsyncSequence where Base : AsyncSequence, Base.Element == UInt8 {
public typealias Element = Data

public struct AsyncIterator : AsyncIteratorProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enum MessageFramingError: Error {
case contentLengthMissing
}

public struct MessageFraming {
struct MessageFraming {
public static func frame(_ data: Data) -> Data {
let length = data.count

Expand All @@ -24,8 +24,4 @@ public struct MessageFraming {

return (name, value)
}

public static func dataChannel(stdin: FileHandle, stdout: FileHandle, stderr: FileHandle, object: AnyObject?) -> DataChannel {
DataChannel(writeHandler: { _ in }, dataSequence: AsyncStream<Data>(unfolding: { nil }))
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import XCTest
import LanguageServerProtocol
@testable import LanguageServerProtocol

struct AsyncByteSequence : AsyncSequence {
struct AsyncDataSequence : AsyncSequence {
typealias Element = UInt8

struct AsyncIterator : AsyncIteratorProtocol {
Expand Down Expand Up @@ -44,7 +44,7 @@ final class AsyncMessageFramingSequenceTests: XCTestCase {
func testBasicMessageDecode() async throws {
let content = "{\"jsonrpc\":\"2.0\",\"params\":\"Something\"}"
let message = "Content-Length: \(content.utf8.count)\r\n\r\n\(content)"
let sequence = AsyncByteSequence(data: Data(message.utf8))
let sequence = AsyncDataSequence(data: Data(message.utf8))

let contentSequence = AsyncMessageFramingSequence(base: sequence)
var iterator = contentSequence.makeAsyncIterator()
Expand All @@ -64,7 +64,7 @@ final class AsyncMessageFramingSequenceTests: XCTestCase {
let header2 = "Another-Header: Something\r\n"
let header3 = "And-Another: third\r\n"
let message = header1 + header2 + header3 + "\r\n" + content
let sequence = AsyncByteSequence(data: Data(message.utf8))
let sequence = AsyncDataSequence(data: Data(message.utf8))

let contentSequence = AsyncMessageFramingSequence(base: sequence)
var iterator = contentSequence.makeAsyncIterator()
Expand Down

0 comments on commit af169ca

Please sign in to comment.