Skip to content

Commit 523aff7

Browse files
authored
feat: Add native-tls feature (#119)
1 parent 0027cac commit 523aff7

12 files changed

Lines changed: 89 additions & 73 deletions

File tree

.github/actions/build-docs/action.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/actions/ci/action.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
name: CI Workflow
2-
description: 'Shared CI workflow.'
2+
description: "Shared CI workflow."
33

44
inputs:
5-
feature-flags:
6-
description: 'Cargo feature flags to pass to test and clippy commands'
5+
cargo-flags:
6+
description: "Flags to pass to cargo commands."
77
required: false
8-
default: ''
8+
default: ""
99

1010
runs:
1111
using: composite
@@ -16,24 +16,24 @@ runs:
1616

1717
- name: Run tests
1818
shell: bash
19-
run: cargo test ${{ inputs.feature-flags }} -p eventsource-client
19+
run: cargo test ${{ inputs.cargo-flags }} -p eventsource-client
2020

2121
- name: Run slower integration tests
2222
shell: bash
23-
run: cargo test ${{ inputs.feature-flags }} -p eventsource-client --lib -- --ignored
23+
run: cargo test ${{ inputs.cargo-flags }} -p eventsource-client --lib -- --ignored
2424

2525
- name: Run clippy checks
2626
shell: bash
27-
run: cargo clippy ${{ inputs.feature-flags }} -p eventsource-client -- -D warnings
27+
run: cargo clippy ${{ inputs.cargo-flags }} -p eventsource-client -- -D warnings
2828

2929
- name: Build contract tests
3030
shell: bash
31-
run: make build-contract-tests
31+
run: CARGO_FLAGS="${{ inputs.cargo-flags }}" make build-contract-tests
3232

3333
- name: Start contract test service
3434
shell: bash
35-
run: make start-contract-test-service-bg
35+
run: CARGO_FLAGS="${{ inputs.cargo-flags }}" make start-contract-test-service-bg
3636

3737
- name: Run contract tests
3838
shell: bash
39-
run: make run-contract-tests
39+
run: CARGO_FLAGS="${{ inputs.cargo-flags }}" make run-contract-tests

.github/workflows/ci.yml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
matrix:
2222
features:
2323
- name: "default"
24-
flags: ""
25-
- name: "no-features"
26-
flags: "--no-default-features"
27-
- name: "hyper"
28-
flags: "--no-default-features --features hyper"
29-
- name: "hyper-rustls"
30-
flags: "--no-default-features --features hyper-rustls"
24+
cargo-flags: ""
25+
- name: "hyper-rustls-native-roots"
26+
cargo-flags: "--no-default-features --features hyper-rustls-native-roots"
27+
- name: "hyper-rustls-webpki-roots"
28+
cargo-flags: "--no-default-features --features hyper-rustls-webpki-roots"
29+
- name: "native-tls"
30+
cargo-flags: "--no-default-features --features native-tls"
3131

3232
name: CI (${{ matrix.features.name }})
3333

@@ -47,7 +47,7 @@ jobs:
4747
4848
- uses: ./.github/actions/ci
4949
with:
50-
feature-flags: ${{ matrix.features.flags }}
50+
cargo-flags: ${{ matrix.features.cargo-flags }}
5151

5252
build-docs:
5353
runs-on: ubuntu-latest
@@ -58,12 +58,11 @@ jobs:
5858
with:
5959
fetch-depth: 0
6060

61-
- name: Get Rust version
62-
id: rust-version
63-
run: cat ./.github/variables/rust-versions.env >> $GITHUB_OUTPUT
64-
6561
- name: Setup rust tooling
66-
run: |
67-
rustup override set ${{ steps.rust-version.outputs.target }}
62+
run: rustup override set nightly
63+
64+
- name: Install cargo-docs-rs
65+
run: cargo install cargo-docs-rs
6866

69-
- uses: ./.github/actions/build-docs
67+
- name: Build documentation
68+
run: cargo docs-rs -p eventsource-client

.github/workflows/manual-publish.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ jobs:
2727
rustup component add rustfmt clippy
2828
2929
- uses: ./.github/actions/ci
30-
- uses: ./.github/actions/build-docs
3130

3231
- uses: launchdarkly/gh-actions/actions/release-secrets@release-secrets-v1.2.0
3332
name: "Get crates.io token"

.github/workflows/release-please.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ jobs:
4343
- uses: ./.github/actions/ci
4444
if: ${{ steps.release.outputs['eventsource-client--release_created'] == 'true' }}
4545

46-
- uses: ./.github/actions/build-docs
47-
if: ${{ steps.release.outputs['eventsource-client--release_created'] == 'true' }}
48-
4946
- uses: ./.github/actions/publish
5047
if: ${{ steps.release.outputs['eventsource-client--release_created'] == 'true' }}
5148
with:

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
TEMP_TEST_OUTPUT=/tmp/contract-test-service.log
2+
CARGO_FLAGS ?= ""
23

34
build-contract-tests:
4-
@cargo build
5+
@cargo build $(CARGO_FLAGS)
56

67
start-contract-test-service: build-contract-tests
78
@./target/debug/sse-test-api
89

910
start-contract-test-service-bg:
1011
@echo "Test service output will be captured in $(TEMP_TEST_OUTPUT)"
11-
@make start-contract-test-service >$(TEMP_TEST_OUTPUT) 2>&1 &
12+
@$(MAKE) start-contract-test-service >$(TEMP_TEST_OUTPUT) 2>&1 &
1213

1314
run-contract-tests:
1415
@curl -s https://raw.githubusercontent.com/launchdarkly/sse-contract-tests/main/downloader/run.sh \

contract-tests/Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "Apache-2.0"
77
[dependencies]
88
futures = { version = "0.3.21" }
99
serde = { version = "1.0", features = ["derive"] }
10-
eventsource-client = { path = "../eventsource-client", features = ["hyper-rustls"] }
10+
eventsource-client = { path = "../eventsource-client" }
1111
serde_json = { version = "1.0.39"}
1212
actix = { version = "0.13.1"}
1313
actix-web = { version = "4"}
@@ -18,7 +18,14 @@ log = "0.4.6"
1818
http = "1.0"
1919
bytes = "1.5"
2020

21-
launchdarkly-sdk-transport = { git = "https://github.com/launchdarkly/rust-sdk-transport.git", branch = "main" }
21+
launchdarkly-sdk-transport = { version = "0.1.0" }
2222

2323
[[bin]]
2424
name = "sse-test-api"
25+
26+
[features]
27+
default = ["hyper"]
28+
hyper = ["eventsource-client/hyper", "launchdarkly-sdk-transport/hyper"]
29+
native-tls = ["hyper", "eventsource-client/native-tls"]
30+
hyper-rustls-native-roots = ["hyper", "eventsource-client/hyper-rustls-native-roots"]
31+
hyper-rustls-webpki-roots = ["hyper", "eventsource-client/hyper-rustls-webpki-roots"]

contract-tests/src/bin/sse-test-api/stream_entity.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,22 @@ impl Inner {
131131
transport_builder = transport_builder.read_timeout(Duration::from_millis(timeout_ms));
132132
}
133133

134+
#[cfg(any(
135+
feature = "hyper-rustls-native-roots",
136+
feature = "hyper-rustls-webpki-roots",
137+
feature = "native-tls"
138+
))]
134139
let transport = transport_builder
135140
.build_https()
136141
.map_err(|e| format!("Failed to build HTTPS transport: {e:?}"))?;
142+
#[cfg(not(any(
143+
feature = "hyper-rustls-native-roots",
144+
feature = "hyper-rustls-webpki-roots",
145+
feature = "native-tls"
146+
)))]
147+
let transport = transport_builder
148+
.build_http()
149+
.map_err(|e| format!("Failed to build HTTP transport: {e:?}"))?;
137150

138151
Ok(Box::new(
139152
client_builder

eventsource-client/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tokio = { version = "1.17.0", features = ["time"] }
2020
rand = "0.8.5"
2121
base64 = "0.22.1"
2222

23-
launchdarkly-sdk-transport = { git = "https://github.com/launchdarkly/rust-sdk-transport.git", branch = "main" }
23+
launchdarkly-sdk-transport = { version = "0.1.0" }
2424

2525
[dev-dependencies]
2626
env_logger = "0.10.0"
@@ -38,4 +38,6 @@ required-features = ["hyper"]
3838
[features]
3939
default = ["hyper"]
4040
hyper = ["launchdarkly-sdk-transport/hyper"]
41-
hyper-rustls = ["hyper", "launchdarkly-sdk-transport/hyper-rustls"]
41+
native-tls = ["hyper", "launchdarkly-sdk-transport/native-tls"]
42+
hyper-rustls-native-roots = ["hyper", "launchdarkly-sdk-transport/hyper-rustls-native-roots"]
43+
hyper-rustls-webpki-roots = ["hyper", "launchdarkly-sdk-transport/hyper-rustls-webpki-roots"]

eventsource-client/README.md

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,26 @@ This library provides a complete SSE protocol implementation with a built-in HTT
99
[Server-Sent Events]: https://html.spec.whatwg.org/multipage/server-sent-events.html
1010
[EventSource]: https://developer.mozilla.org/en-US/docs/Web/API/EventSource
1111

12-
## Requirements
13-
14-
* Tokio async runtime
15-
* Enable the `hyper` feature for the built-in HTTP transport (enabled by default)
16-
* Optionally enable `hyper-rustls` for HTTPS support
17-
1812
## Quick Start
1913

2014
### 1. Add dependencies
2115

2216
```toml
2317
[dependencies]
24-
eventsource-client = { version = "0.17", features = ["hyper", "hyper-rustls"] }
18+
eventsource-client = { version = "0.17", features = ["hyper-rustls-native-roots"] }
2519
futures = "0.3"
2620
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
2721
```
2822

2923
**Features:**
3024
- `hyper` - Enables the built-in `HyperTransport` for HTTP support (enabled by default)
31-
- `hyper-rustls` - Adds HTTPS support via rustls (optional)
25+
- `hyper-rustls-native-roots`, `hyper-rustls-webpki-roots`, or `native-tls` - Adds HTTPS support via rustls (optional)
3226

3327
### 2. Use the client
3428

3529
```rust
36-
use eventsource_client::{ClientBuilder, HyperTransport, SSE};
30+
use eventsource_client::{ClientBuilder, SSE};
31+
use launchdarkly_sdk_transport::HyperTransport;
3732
use futures::TryStreamExt;
3833
use std::time::Duration;
3934

@@ -71,28 +66,30 @@ The `tail` example demonstrates a complete SSE client using the built-in `HyperT
7166

7267
**Run with HTTP:**
7368
```bash
74-
cargo run --example tail --features hyper -- http://sse.dev/test "Bearer token"
69+
cargo run --example tail --features hyper -- http://live-test-scores.herokuapp.com/scores "Bearer token"
7570
```
7671

7772
**Run with HTTPS:**
7873
```bash
79-
cargo run --example tail --features hyper,hyper-rustls -- https://sse.dev/test "Bearer token"
74+
cargo run --example tail --features hyper-rustls-native-roots -- https://live-test-scores.herokuapp.com/scores "Bearer token"
75+
cargo run --example tail --features hyper-rustls-webpki-roots -- https://live-test-scores.herokuapp.com/scores "Bearer token"
76+
cargo run --example tail --features native-tls -- https://live-test-scores.herokuapp.com/scores "Bearer token"
8077
```
8178

8279
The example shows:
8380
- Creating a `HyperTransport` with custom timeouts
8481
- Building an SSE client with authentication headers
8582
- Configuring automatic reconnection with exponential backoff
8683
- Handling different SSE event types (events, comments, connection status)
87-
- Proper error handling for HTTPS URLs without the `hyper-rustls` feature
84+
- Proper error handling for HTTPS URLs without the `hyper-rustls-native-roots` feature
8885

8986
See [`examples/tail.rs`](https://github.com/launchdarkly/rust-eventsource-client/tree/main/eventsource-client/examples/tail.rs) for the complete implementation.
9087

9188
## Features
9289

9390
* **Built-in HTTP transport** - Production-ready `HyperTransport` powered by hyper v1
9491
* **Configurable timeouts** - Connect, read, and write timeout support
95-
* **HTTPS support** - Optional rustls integration via the `hyper-rustls` feature
92+
* **HTTPS support** - Optional rustls integration via the `hyper-rustls-*` or `native-tls` features
9693
* **Pluggable transport** - Use a custom HTTP client if needed (reqwest, etc.)
9794
* **Tokio-based streaming** - Efficient async/await support
9895
* **Custom headers** - Full control over HTTP requests
@@ -106,20 +103,16 @@ See [`examples/tail.rs`](https://github.com/launchdarkly/rust-eventsource-client
106103
While the built-in `HyperTransport` works for most use cases, you can implement the `HttpTransport` trait to use your own HTTP client:
107104

108105
```rust
109-
use eventsource_client::{HttpTransport, ByteStream, TransportError};
110-
use std::pin::Pin;
111-
use std::future::Future;
106+
use launchdarkly_sdk_transport::{HttpTransport, Request, ResponseFuture};
107+
use bytes::Bytes;
112108

113109
#[derive(Clone)]
114110
struct MyTransport {
115111
// Your HTTP client here
116112
}
117113

118114
impl HttpTransport for MyTransport {
119-
fn request(
120-
&self,
121-
request: http::Request<Option<String>>,
122-
) -> Pin<Box<dyn Future<Output = Result<http::Response<ByteStream>, TransportError>> + Send + Sync + 'static>> {
115+
fn request(&self, request: Request<Option<Bytes>>) -> ResponseFuture {
123116
// Implement HTTP request handling
124117
// See the HttpTransport trait documentation for details
125118
todo!()

0 commit comments

Comments
 (0)