Skip to content

Experimental functions._invokeWithStreamedResponse never yields data #523

Closed
@kirkbyo

Description

@kirkbyo

Bug report

Describe the bug

When utilizing the experimental functions. _invokeWithStreamedResponse method (introduced in #346), the returned AsyncThrowingStream never yields any data to the consumer.

let events = supabase.functions._invokeWithStreamedResponse("some-sse-edge-function", options: options)
for try await data in events {
   // never executed
}

To Reproduce

Initialize _invokeWithStreamedResponse with an edge-function configured to send a text stream (e.g. the Deno SSE template should suffice).

Expected behavior

As bytes are decoded from the underlying NSURLSession they are yielded to the consumer.

System information

  • OS: macOS 14.4.1
  • Version of supabase-swift: 2.13.2
  • iOS version: 17.0.1

Additional context

The current implementation of _invokeWithStreamedResponse creates a session.dataTask with a callback:

let task = session.dataTask(with: urlRequest) { data, response, _ in
guard let httpResponse = response as? HTTPURLResponse else {
continuation.finish(throwing: URLError(.badServerResponse))
return
}
guard 200 ..< 300 ~= httpResponse.statusCode else {
let error = FunctionsError.httpError(code: httpResponse.statusCode, data: data ?? Data())
continuation.finish(throwing: error)
return
}
let isRelayError = httpResponse.value(forHTTPHeaderField: "x-relay-error") == "true"
if isRelayError {
continuation.finish(throwing: FunctionsError.relayError)
}
}

however, it is also specifying a URLSession delegate which is responsible for yielding data:

let session = URLSession(configuration: .default, delegate: delegate, delegateQueue: nil)

According to the docs, if a dataTask is specified with a callback, then the delegate is never used, which would explain why values are never yielded:

By using the completion handler, the task bypasses calls to delegate methods for response and data delivery, and instead provides any resulting NSData, URLResponse, and NSError objects inside the completion handler. Delegate methods for handling authentication challenges, however, are still called.

Patching the library to no longer use the callback resolved my issue: kirkbyo@70959a3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions