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

supabase / supabase-swift / 38879475867

16 Oct 2026 09:17AM UTC coverage: 91.479% (-0.04%) from 91.523%
38879475867

push

github

web-flow
docs: document the macOS test flags for swift test and fix them for zsh (#1573)

* docs: document the macOS test flags for swift test and fix them for zsh

swift-snapshot-testing 1.19+ needs Swift Testing's cross-import overlays,
which SwiftPM leaves off, so swift test fails on macOS without extra flags.
Document the flags once in DEVELOPMENT.md and point agents to them from
AGENTS.md. Use a shell array: zsh does not split a plain string, so the old
swift test $SWIFT_FLAGS recipe failed with "Unknown option".

* chore(scripts): derive the integration-test triple from the host architecture

* docs: pass the macOS test flags in the general swift test commands

13623 of 14892 relevant lines covered (91.48%)

140.94 hits per line

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

89.32
/Sources/Helpers/HTTP/HTTPBodyOutputStreamBridge.swift
1
//
2
//  HTTPBodyOutputStreamBridge.swift
3
//  Helpers
4
//
5
//  Created by Guilherme Souza on 15/09/26.
6
//
7

8
#if !canImport(FoundationNetworking)
9
  import Foundation
10

11
  /// Pumps an ``HTTPBody`` into the write end of a bound stream pair so `URLSession` can read
12
  /// the other end as a streamed request body.
13
  ///
14
  /// The bridge pulls one chunk at a time and writes only while the stream reports space, so
15
  /// the body is never read further ahead than the pair's buffer plus one chunk. Stream events
16
  /// and all mutable state live on one private queue. A ``HTTPBody/Length/known(_:)`` body that
17
  /// yields a different number of bytes fails with ``HTTPBodyLengthMismatchError``: the declared
18
  /// count has already gone out as `Content-Length`, and a short body would otherwise leave
19
  /// `URLSession` waiting for bytes that never come.
20
  final class HTTPBodyOutputStreamBridge: NSObject, StreamDelegate, @unchecked Sendable {
21
    private let queue = DispatchQueue(label: "supabase.HTTPBodyOutputStreamBridge")
21 ✔
22

23
    private let output: OutputStream
24
    private let declaredLength: Int64?
25
    private let onFailure: @Sendable (any Error) -> Void
26
    // Touched only on `queue`.
27
    private var iterator: HTTPBody.Iterator
28
    private var pending: ArraySlice<UInt8> = []
21 ✔
29
    private var pulled: Int64 = 0
21 ✔
30
    private var fetch: Task<Void, Never>?
31
    private var isClosed = false
21 ✔
32

33
    /// Starts pumping `body` into `output`. Calls `onFailure` at most once, when the body throws,
34
    /// the stream fails, or the body's byte count disagrees with its declared length; reaching
35
    /// the end of the body just closes the stream.
36
    init(
37
      body: HTTPBody, output: OutputStream, onFailure: @escaping @Sendable (any Error) -> Void
38
    ) {
21 ✔
39
      self.output = output
21 ✔
40
      self.onFailure = onFailure
21 ✔
41
      self.iterator = body.makeAsyncIterator()
21 ✔
42
      if case .known(let count) = body.length {
21 ✔
43
        declaredLength = count
17 ✔
44
      } else {
17 ✔
45
        declaredLength = nil
4 ✔
46
      }
4 ✔
47
      super.init()
21 ✔
48
      output.delegate = self
21 ✔
49
      CFWriteStreamSetDispatchQueue(output as CFWriteStream, queue)
21 ✔
50
      output.open()
21 ✔
51
    }
21 ✔
52

53
    deinit {
21 ✔
54
      output.delegate = nil
21 ✔
55
    }
21 ✔
56

57
    /// Stops pumping and closes the write end. Safe to call more than once.
58
    func cancel() {
21 ✔
59
      queue.async { self.close() }
21 ✔
60
    }
21 ✔
61

62
    func stream(_ stream: Stream, handle event: Stream.Event) {
68 ✔
63
      switch event {
68 ✔
64
      case .openCompleted, .hasSpaceAvailable:
68 ✔
65
        pump()
67 ✔
66
      case .errorOccurred:
68 ✔
67
        fail(stream.streamError ?? URLError(.unknown))
×
68
      case .endEncountered:
68 ✔
69
        close()
1 ✔
70
      default:
68 ✔
71
        break
×
72
      }
68 ✔
73
    }
68 ✔
74

75
    /// Writes what it holds while the stream has space, then pulls the next chunk. Idle while a
76
    /// pull is in flight; the pull's completion re-enters here.
77
    private func pump() {
87 ✔
78
      guard !isClosed, fetch == nil else { return }
87 ✔
79
      if pending.isEmpty {
67 ✔
80
        fetch = Task { [self] in
68 ✔
81
          let result: Result<ArraySlice<UInt8>?, any Error>
68 ✔
82
          do {
68 ✔
83
            result = .success(try await iterator.next())
68 ✔
84
          } catch {
64 ✔
85
            result = .failure(error)
4 ✔
86
          }
68 ✔
87
          queue.async { self.resume(with: result) }
68 ✔
88
        }
68 ✔
89
        return
35 ✔
90
      }
35 ✔
91
      guard output.hasSpaceAvailable else { return }
32 ✔
92
      let written = pending.withUnsafeBytes { buffer -> Int in
52 ✔
93
        guard let base = buffer.baseAddress else { return 0 }
52 ✔
94
        return output.write(base, maxLength: buffer.count)
52 ✔
95
      }
52 ✔
96
      guard written > 0 else {
28 ✔
97
        // `.atEnd` means URLSession closed the read end, usually because the server answered
×
98
        // before reading the whole body (an early 413). The task still delivers that response;
×
99
        // failing here would cancel it and surface a bare `URLError(.unknown)` instead.
×
100
        if output.streamStatus == .atEnd {
×
101
          close()
×
102
        } else if written < 0 {
×
103
          fail(output.streamError ?? URLError(.unknown))
×
104
        }
×
105
        return
×
106
      }
28 ✔
107
      pending = pending.dropFirst(written)
28 ✔
108
      pump()
28 ✔
109
    }
28 ✔
110

111
    private func resume(with result: Result<ArraySlice<UInt8>?, any Error>) {
68 ✔
112
      fetch = nil
68 ✔
113
      switch result {
68 ✔
114
      case .success(let chunk?):
68 ✔
115
        pulled += Int64(chunk.count)
50 ✔
116
        if let declaredLength, pulled > declaredLength {
50 ✔
117
          fail(HTTPBodyLengthMismatchError(declared: declaredLength, actual: pulled))
2 ✔
118
          return
2 ✔
119
        }
48 ✔
120
        pending = chunk
48 ✔
121
        pump()
48 ✔
122
      case .success(nil):
68 ✔
123
        // `pump` only pulls once `pending` is empty, so everything pulled has been written.
14 ✔
124
        if let declaredLength, pulled != declaredLength {
14 ✔
125
          fail(HTTPBodyLengthMismatchError(declared: declaredLength, actual: pulled))
2 ✔
126
          return
2 ✔
127
        }
12 ✔
128
        close()
12 ✔
129
      case .failure(let error):
68 ✔
130
        fail(error)
4 ✔
131
      }
68 ✔
132
    }
64 ✔
133

134
    private func fail(_ error: any Error) {
8 ✔
135
      guard !isClosed else { return }
8 ✔
136
      close()
8 ✔
137
      onFailure(error)
8 ✔
138
    }
8 ✔
139

140
    private func close() {
42 ✔
141
      guard !isClosed else { return }
42 ✔
142
      isClosed = true
21 ✔
143
      fetch?.cancel()
21 ✔
144
      output.close()
21 ✔
145
      output.delegate = nil
21 ✔
146
    }
21 ✔
147
  }
148
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc