Skip to content

[WIP] HTTP Client Config #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

[WIP] HTTP Client Config #229

wants to merge 8 commits into from

Conversation

stevensJourney
Copy link
Contributor

@stevensJourney stevensJourney commented Jul 23, 2025

Overview

This PR allows configuring the Ktor networking client we use for PowerSync connections.

This currently allows for a broad range of configuration, including:

  • Installing additional Ktor plugins. E.g the Logging pluging or Custom plugins.
  • Customising request headers etc via the DefaultRequest plugin.

Todos And Open Questions

  • Verify Websocket logging - this seems to currently be quite limited Websockets are no longer used.

  • Gauge the impact of exposing and depending on Ktor in our public SyncOptions API for Swift

The addition of this work increased the release XCFramework by 6MB. The header file increased by 6KB.
This is relatively small compared to the current sizes.

Main Branch

❯  du -sh PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework
228M    PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework
❯  du -sh PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework/ios-arm64/PowerSyncKotlin.framework/Headers/PowerSyncKotlin.h
272K    PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework/ios-arm64/PowerSyncKotlin.framework/Headers/PowerSyncKotlin.h

This Branch

❯ du -sh PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework
234M    PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework
❯ du -sh PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework/ios-arm64/PowerSyncKotlin.framework/Headers/PowerSyncKotlin.h
276K    PowerSyncKotlin/build/XCFrameworks/release/PowerSyncKotlin.xcframework/ios-arm64/PowerSyncKotlin.framework/Headers/PowerSyncKotlin.h
  • Confirm if there is an actual use case for providing an existing Ktor client, versus hooking in to the configuration process of a new client.

The ability to use a provided client has been marked as experimental. We could mark this as a delicate API after an initial experimental release.

  • Investigate formatting changes (might be an issue with my dev setup)

Demo

The Ktor Logging plugin has been added to the Hello-PowerSync demo. A sample log of the HTTP request headers is below

2025-07-23 09:25:33.516 11804-11844 System.out              com.powersync.demos                  I  Authorization=[Token token]
2025-07-23 09:25:33.516 11804-11844 System.out              com.powersync.demos                  I  User-Agent=[PowerSync Kotlin SDK v1.3.0 (Android 29)]
2025-07-23 09:25:33.516 11804-11844 System.out              com.powersync.demos                  I  Accept-Charset=[UTF-8]
2025-07-23 09:25:33.516 11804-11844 System.out              com.powersync.demos                  I  Accept=[*/*]
2025-07-23 09:26:03.535 11804-11844 System.out              com.powersync.demos                  I  Response headers:
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  access-control-allow-origin=[*]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  access-control-expose-headers=[Content-Type]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  connection=[keep-alive]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  content-length=[2419]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  content-type=[application/json; charset=utf-8]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  date=[Wed, 23 Jul 2025 07:26:42 GMT]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  keep-alive=[timeout=72]
2025-07-23 09:26:03.536 11804-11844 System.out              com.powersync.demos                  I  vary=[Origin]

Swift

The Swift SDK now exposes a limited and controlled subset of plugin functionality by exposing logging PowerSync network requests. powersync-ja/powersync-swift#63

Copy link
Contributor

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks nice 👍

One thing I'm wondering about is if we can use this to remove the createClient field in SyncStream and PowerSyncDatabaseImpl (since we now have a proper option for this). I've added those for tests, but it looks like we can now pass the test engine via connection options instead.
(and then as a fallback when the engine option is null, use defaultHttpClient from the companion or just inline the HttpClient call)

/**
* Extends the default Ktor [HttpClient] configuration with the provided block.
*/
public class ExtendedConfig(public val block: HttpClientConfig<*>.() -> Unit) :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we should remember to make ktor an API dependency in Gradle for this.

(and carefully evaluate the impact that has on the generated Swift headers)

# Conflicts:
#	core/src/commonIntegrationTest/kotlin/com/powersync/testutils/TestUtils.kt
#	core/src/commonMain/kotlin/com/powersync/sync/SyncOptions.kt
#	core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants