• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / supabase-swift / 17238365990

26 Aug 2025 12:38PM UTC coverage: 48.936% (-28.5%) from 77.386%
17238365990

Pull #781

github

web-flow
Merge 95ac7642e into e4d8c3718
Pull Request #781: RFC: Migrate HTTP networking from URLSession to Alamofire

287 of 986 new or added lines in 26 files covered. (29.11%)

1397 existing lines in 30 files now uncovered.

3448 of 7046 relevant lines covered (48.94%)

5.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.74
/Sources/Realtime/WebSocket/WebSocket.swift
1
import Foundation
2

3
/// Represents events that can occur on a WebSocket connection.
4
enum WebSocketEvent: Sendable, Hashable {
5
  case text(String)
6
  case binary(Data)
7
  case close(code: Int?, reason: String)
8
}
9

10
/// Represents errors that can occur on a WebSocket connection.
11
enum WebSocketError: Error, LocalizedError {
12
  /// An error occurred while connecting to the peer.
13
  case connection(message: String, error: any Error)
14

15
  var errorDescription: String? {
2✔
16
    switch self {
2✔
17
    case .connection(let message, let error): "\(message) \(error.localizedDescription)"
2✔
18
    }
2✔
19
  }
2✔
20
}
21

22
/// The interface for WebSocket connection.
23
protocol WebSocket: Sendable, AnyObject {
24
  var closeCode: Int? { get }
25
  var closeReason: String? { get }
26

27
  /// Sends text data to the connected peer.
28
  /// - Parameter text: The text data to send.
29
  func send(_ text: String)
30

31
  /// Sends binary data to the connected peer.
32
  /// - Parameter binary: The binary data to send.
33
  func send(_ binary: Data)
34

35
  /// Closes the WebSocket connection and the ``events`` `AsyncStream`.
36
  ///
37
  /// Sends a Close frame to the peer. If the optional `code` and `reason` arguments are given, they will be included in the Close frame. If no `code` is set then the peer will see a 1005 status code. If no `reason` is set then the peer will not receive a reason string.
38
  /// - Parameters:
39
  ///   - code: The close code to send to the peer.
40
  ///   - reason: The reason for closing the connection.
41
  func close(code: Int?, reason: String?)
42

43
  /// Listen for event messages in the connection.
44
  var onEvent: (@Sendable (WebSocketEvent) -> Void)? { get set }
45

46
  /// The WebSocket subprotocol negotiated with the peer.
47
  ///
48
  /// Will be the empty string if no subprotocol was negotiated.
49
  ///
50
  /// See [RFC-6455 1.9](https://datatracker.ietf.org/doc/html/rfc6455#section-1.9).
51
  var `protocol`: String { get }
52

53
  /// Whether connection is closed.
54
  var isClosed: Bool { get }
55
}
56

57
extension WebSocket {
58
  /// Closes the WebSocket connection and the ``events`` `AsyncStream`.
59
  ///
60
  /// Sends a Close frame to the peer. If the optional `code` and `reason` arguments are given, they will be included in the Close frame. If no `code` is set then the peer will see a 1005 status code. If no `reason` is set then the peer will not receive a reason string.
61
  func close() {
×
62
    self.close(code: nil, reason: nil)
×
63
  }
×
64

65
  /// An `AsyncStream` of ``WebSocketEvent`` received from the peer.
66
  ///
67
  /// Data received by the peer will be delivered as a ``WebSocketEvent/text(_:)`` or ``WebSocketEvent/binary(_:)``.
68
  ///
69
  /// If a ``WebSocketEvent/close(code:reason:)`` event is received then the `AsyncStream` will be closed. A ``WebSocketEvent/close(code:reason:)`` event indicates either that:
70
  ///
71
  /// - A close frame was received from the peer. `code` and `reason` will be set by the peer.
72
  /// - A failure occurred (e.g. the peer disconnected). `code` and `reason` will be a failure code defined by [RFC-6455](https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1) (e.g. 1006).
73
  ///
74
  /// Errors will never appear in this `AsyncStream`.
UNCOV
75
  var events: AsyncStream<WebSocketEvent> {
×
UNCOV
76
    let (stream, continuation) = AsyncStream<WebSocketEvent>.makeStream()
×
UNCOV
77
    self.onEvent = { event in
×
UNCOV
78
      continuation.yield(event)
×
UNCOV
79

×
UNCOV
80
      if case .close = event {
×
81
        continuation.finish()
×
82
      }
×
UNCOV
83
    }
×
UNCOV
84

×
UNCOV
85
    continuation.onTermination = { _ in
×
UNCOV
86
      self.onEvent = nil
×
UNCOV
87
    }
×
UNCOV
88
    return stream
×
UNCOV
89
  }
×
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc