Skip to content

Commit

Permalink
docs and fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
kflansburg committed Oct 23, 2023
1 parent 22ca933 commit 5d38fd7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 74 deletions.
61 changes: 5 additions & 56 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/tokio-postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "tokio-postgres-on-workers"
version = "0.1.0"
edition = "2021"
resolver = "2"

# https://github.com/rustwasm/wasm-pack/issues/1247
[package.metadata.wasm-pack.profile.release]
Expand All @@ -12,4 +13,4 @@ crate-type = ["cdylib"]

[dependencies]
worker = { workspace=true, features=["tokio-postgres"] }
tokio-postgres = { git="https://github.com/sfackler/rust-postgres", branch="master", features=['js'], default-features=false }
tokio-postgres = { version="0.7", features=['js'], default-features=false }
35 changes: 18 additions & 17 deletions worker/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,26 @@ fn handle_data(buf: &mut ReadBuf<'_>, mut data: Vec<u8>) -> (Reading, Poll<IoRes
}

#[cfg(feature = "tokio-postgres")]
/// Implements [`TlsConnect`](tokio_postgres::TlsConnect) for
/// [`Socket`](crate::Socket) to enable `tokio_postgres` connections
/// to databases using TLS.
pub mod postgres_tls {
use super::Socket;
use futures_util::future::{ready, Ready};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use tokio_postgres::tls::{ChannelBinding, TlsConnect, TlsStream};

/// Represents a stream which can start tls itself.
pub trait StartTlsStream: TlsStream {
/// Switch from plaintext to TLS
fn start_tls(self) -> Self;
}

/// Use TLS when socket already originates TLS connection
/// Supply this to `connect_raw` in place of `NoTls` to specify TLS
/// when using Workers.
///
/// ```rust
/// let config = tokio_postgres::config::Config::new();
/// let socket = Socket::builder()
/// .secure_transport(SecureTransport::StartTls)
/// .connect("database_url", 5432)?;
/// let _ = config.connect_raw(socket, PassthroughTls).await?;
/// ```
pub struct PassthroughTls;

#[derive(Debug)]
Expand All @@ -402,31 +408,26 @@ pub mod postgres_tls {

impl Display for PassthroughTlsError {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
fmt.write_str("PassthroughTlsError: future already polled")
fmt.write_str("PassthroughTlsError")
}
}

impl<S: StartTlsStream + Unpin> TlsConnect<S> for PassthroughTls {
type Stream = S;
impl TlsConnect<Socket> for PassthroughTls {
type Stream = Socket;
type Error = PassthroughTlsError;
type Future = Ready<Result<S, PassthroughTlsError>>;
type Future = Ready<Result<Socket, PassthroughTlsError>>;

fn connect(self, s: Self::Stream) -> Self::Future {
let tls = s.start_tls();
ready(Ok(tls))
}
}

impl TlsStream for Socket {
fn channel_binding(&self) -> ChannelBinding {
ChannelBinding::none()
}
}

impl StartTlsStream for Socket {
fn start_tls(self) -> Self {
self.start_tls()
}
}
}

#[cfg(test)]
Expand Down

0 comments on commit 5d38fd7

Please sign in to comment.