1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use crate::client::Config;
use crate::proto;
use anyhow::Context;
use worker::*;

use crate::{BatchResult, ResultSet, Statement};

#[derive(Debug)]
pub struct WebSocketClient {
    socket: WebSocket,
    next_reqid: std::sync::atomic::AtomicI32,
}

#[derive(Debug)]
pub struct HttpClient {
    url: String,
    auth: String,
}

#[derive(Debug)]
pub enum ClientInner {
    WebSocket(WebSocketClient),
    Http(HttpClient),
}

impl WebSocketClient {
    fn send_request(&self, request: proto::Request) -> Result<()> {
        // NOTICE: we effective allow concurrency of 1 here, until we implement
        // id allocation andfMe request tracking
        let request_id = self
            .next_reqid
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let msg = proto::ClientMsg::Request {
            request_id,
            request,
        };

        self.socket.send(&msg)
    }

    async fn recv_response(event_stream: &mut EventStream<'_>) -> Result<proto::ServerMsg> {
        use futures_util::StreamExt;

        // NOTICE: we're effectively synchronously waiting for the response here
        if let Some(event) = event_stream.next().await {
            match event? {
                WebsocketEvent::Message(msg) => {
                    let stmt_result: proto::ServerMsg = msg.json::<proto::ServerMsg>()?;
                    Ok(stmt_result)
                }
                WebsocketEvent::Close(msg) => {
                    Err(Error::RustError(format!("connection closed: {msg:?}")))
                }
            }
        } else {
            Err(Error::RustError("no response".to_string()))
        }
    }
}

/// Database client. This is the main structure used to
/// communicate with the database.
#[derive(Debug)]
pub struct Client {
    pub inner: ClientInner,
}

impl Client {
    /// Creates a database client with JWT authentication.
    ///
    /// # Arguments
    /// * `url` - URL of the database endpoint
    /// * `token` - auth token
    pub async fn new(url: impl Into<String>, token: impl Into<String>) -> Result<Self> {
        let token = token.into();
        let url = url.into();
        // Auto-update the URL to start with https://.
        // It will get updated to wss via Workers API automatically
        let (url, is_websocket) = if !url.contains("://") {
            (format!("https://{}", &url), true)
        } else if let Some(url) = url.strip_prefix("libsql://") {
            ("https://".to_owned() + url, true)
        } else if let Some(url) = url.strip_prefix("wss://") {
            ("https://".to_owned() + url, true)
        } else if let Some(url) = url.strip_prefix("ws://") {
            ("https://".to_owned() + url, true)
        } else {
            (url, false)
        };

        if !is_websocket {
            let inner = ClientInner::Http(HttpClient {
                url: url.clone(),
                auth: format!("Bearer {token}"),
            });
            return Ok(Self { inner });
        }

        let url = url::Url::parse(&url)
            .context("Failed to parse URL")
            .map_err(|e| Error::from(format!("{e}")))?;

        let mut req = Request::new(url.as_str(), Method::Get)?;
        let headers = req.headers_mut()?;
        headers.set("upgrade", "websocket")?;
        headers.set("Authentication", &format!("Bearer {token}"))?;

        let res = Fetch::Request(req).send().await?;

        let socket = match res.websocket() {
            Some(ws) => ws,
            None => {
                return Err(Error::RustError(
                    "Failed to upgrade to websocket".to_string(),
                ))
            }
        };

        let mut event_stream = socket.events()?;

        socket.accept()?;

        let jwt = if token.is_empty() { None } else { Some(token) };

        socket.send(&proto::ClientMsg::Hello { jwt })?;

        // NOTICE: only a single stream id is used for now
        socket.send(&proto::ClientMsg::Request {
            request_id: 0,
            request: proto::Request::OpenStream(proto::OpenStreamReq { stream_id: 0 }),
        })?;

        // Wait for Hello and OpenStream responses
        // TODO: they could be pipelined with the first request to save latency.
        // For that, we need to keep the event stream open in the Client,
        // but that's tricky with the borrow checker.
        WebSocketClient::recv_response(&mut event_stream).await?;
        WebSocketClient::recv_response(&mut event_stream).await?;

        tracing::debug!("Stream opened");
        drop(event_stream);
        let inner = ClientInner::WebSocket(WebSocketClient {
            socket,
            next_reqid: std::sync::atomic::AtomicI32::new(1),
        });
        Ok(Self { inner })
    }

    /// Creates a database client from a `Config` object.
    pub async fn from_config(config: Config) -> Result<Self> {
        Self::new(config.url, config.auth_token.unwrap_or_default()).await
    }

    /// Creates a database client, given a `Url`
    ///
    /// # Arguments
    /// * `url` - `Url` object of the database endpoint. This cannot be a relative URL;
    ///
    /// # Examples
    ///
    /// ```
    /// # use libsql_client::reqwest::Client;
    /// use url::Url;
    ///
    /// let url  = Url::parse("https://foo:bar@localhost:8080").unwrap();
    /// let db = Client::from_url(url).unwrap();
    /// ```
    pub async fn from_url<T: TryInto<url::Url>>(url: T) -> anyhow::Result<Client>
    where
        <T as TryInto<url::Url>>::Error: std::fmt::Display,
    {
        let url = url
            .try_into()
            .map_err(|e| anyhow::anyhow!(format!("{e}")))?;
        let mut params = url.query_pairs();
        // Try if a jwt=XXX parameter exists
        if let Some((_, token)) = params.find(|(param_key, _)| param_key == "jwt") {
            Client::new(url.as_str(), token).await
        } else {
            Client::new(url, "").await
        }
        .map_err(|e| anyhow::anyhow!(format!("{e}")))
    }

    /// Establishes a database client from Cloudflare Workers context.
    /// Expects the context to contain the following secrets defined:
    /// * `LIBSQL_CLIENT_URL`
    /// * `LIBSQL_CLIENT_USER`
    /// * `LIBSQL_CLIENT_PASS`
    /// # Arguments
    /// * `ctx` - Cloudflare Workers route context
    pub async fn from_ctx<D>(ctx: &worker::RouteContext<D>) -> Result<Self> {
        let token = ctx.secret("LIBSQL_CLIENT_TOKEN")?;
        Self::new(
            ctx.secret("LIBSQL_CLIENT_URL")?.to_string(),
            token.to_string(),
        )
        .await
    }

    async fn raw_batch_internal(
        &self,
        stmts: impl IntoIterator<Item = impl Into<Statement>>,
    ) -> Result<BatchResult> {
        match &self.inner {
            ClientInner::WebSocket(ws) => {
                let mut batch = proto::Batch::new();

                for stmt in stmts.into_iter() {
                    let stmt: Statement = stmt.into();
                    let mut hrana_stmt = proto::Stmt::new(stmt.sql, true);
                    for param in stmt.args {
                        hrana_stmt.bind(param);
                    }
                    batch.step(None, hrana_stmt);
                }

                let mut event_stream = ws.socket.events()?;

                // NOTICE: if we want to support concurrent requests, we need to
                // actually start managing stream ids
                ws.send_request(proto::Request::Batch(proto::BatchReq {
                    stream_id: 0,
                    batch,
                }))?;

                match WebSocketClient::recv_response(&mut event_stream).await? {
                    proto::ServerMsg::ResponseOk {
                        request_id: _,
                        response: proto::Response::Batch(proto::BatchResp { result }),
                    } => Ok(result),
                    proto::ServerMsg::ResponseError {
                        request_id: _,
                        error,
                    } => Err(Error::RustError(format!("{error}"))),
                    _ => Err(Error::RustError("unexpected response".to_string())),
                }
            }
            ClientInner::Http(http) => {
                let mut headers = Headers::new();
                headers.append("Authorization", &http.auth).ok();
                let (body, stmts_count) = crate::client::statements_to_string(stmts);
                let request_init = RequestInit {
                    body: Some(wasm_bindgen::JsValue::from_str(&body)),
                    headers,
                    cf: CfProperties::new(),
                    method: Method::Post,
                    redirect: RequestRedirect::Follow,
                };
                let req = Request::new_with_init(&http.url, &request_init)?;
                let mut response = Fetch::Request(req).send().await?;
                if response.status_code() != 200 {
                    return Err(worker::Error::from(format!("{}", response.status_code())));
                }
                let resp: String = response.text().await?;
                let response_json: serde_json::Value = serde_json::from_str(&resp)?;
                crate::client::http_json_to_batch_result(response_json, stmts_count).map_err(|e| {
                    worker::Error::from(format!("Error: {} ({:?})", e, request_init.body))
                })
            }
        }
    }

    async fn execute_internal(&self, stmt: impl Into<Statement>) -> Result<ResultSet> {
        match &self.inner {
            ClientInner::WebSocket(ws) => {
                let stmt: Statement = stmt.into();
                let mut hrana_stmt = proto::Stmt::new(stmt.sql, true);
                for param in stmt.args {
                    hrana_stmt.bind(param);
                }

                let mut event_stream = ws.socket.events()?;

                ws.send_request(proto::Request::Execute(proto::ExecuteReq {
                    stream_id: 0,
                    stmt: hrana_stmt,
                }))?;
                match WebSocketClient::recv_response(&mut event_stream).await? {
                    proto::ServerMsg::ResponseOk {
                        request_id: _,
                        response: proto::Response::Execute(proto::ExecuteResp { result }),
                    } => Ok(ResultSet::from(result)),
                    proto::ServerMsg::ResponseError {
                        request_id: _,
                        error,
                    } => Err(Error::RustError(format!("{error}"))),
                    _ => Err(Error::RustError("unexpected response".to_string())),
                }
            }
            ClientInner::Http(_) => {
                let results = self.raw_batch_internal(std::iter::once(stmt)).await?;
                match (results.step_results.first(), results.step_errors.first()) {
                    (Some(Some(result)), Some(None)) => Ok(ResultSet::from(result.clone())),
                    (Some(None), Some(Some(err))) => Err(Error::RustError(err.message.clone())),
                    _ => unreachable!(),
                }
            }
        }
    }
}

impl Client {
    pub async fn raw_batch(
        &self,
        stmts: impl IntoIterator<Item = impl Into<Statement>>,
    ) -> anyhow::Result<BatchResult> {
        self.raw_batch_internal(stmts)
            .await
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    pub async fn execute(&self, stmt: impl Into<Statement>) -> anyhow::Result<ResultSet> {
        self.execute_internal(stmt)
            .await
            .map_err(|e| anyhow::anyhow!("{e}"))
    }

    pub async fn execute_in_transaction(
        &self,
        _tx_id: u64,
        stmt: Statement,
    ) -> anyhow::Result<ResultSet> {
        self.execute(stmt).await
    }

    pub async fn commit_transaction(&self, _tx_id: u64) -> anyhow::Result<()> {
        self.execute("COMMIT").await.map(|_| ())
    }

    pub async fn rollback_transaction(&self, _tx_id: u64) -> anyhow::Result<()> {
        self.execute("ROLLBACK").await.map(|_| ())
    }
}