Skip to content

Commit

Permalink
Compress requests with zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
mgorven committed May 13, 2024
1 parent de061ab commit 08a094f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lightspark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ client = ["base", "objects", "dep:reqwest"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"], optional = true }
futures = "0.3"
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls", "zstd"], optional = true }
futures = "0.3"
base64 = "0.21.0"
serde_json = "1.0.94"
serde = { version = "1.0.155", features = ["derive"] }
Expand All @@ -40,6 +40,7 @@ pbkdf2 = "0.12.2"
rsa = { version ="0.9.2", features = ["sha2"] }
cbc = "0.1.2"
async-trait = "0.1.73"
zstd = "0.13"

[dev-dependencies]
tokio = { version = "1.12.0", features = ["full"] }
24 changes: 17 additions & 7 deletions lightspark/src/request/requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,38 @@ impl Requester {
variables: Option<Value>,
signing_key: Option<T>,
) -> Result<Value, Error> {
let body = build_graphql_request_body(operation, variables, signing_key.is_some())?;
let payload = build_graphql_request_body(operation, variables, signing_key.is_some())?;

let mut headers = HeaderMap::new();
headers.insert("Content-Type", HeaderValue::from_static("application/json"));

if let Some(op) = body["operationName"].as_str() {
if let Some(op) = payload["operationName"].as_str() {
if let Ok(value) = HeaderValue::from_str(op) {
headers.insert("X-GraphQL-Operation", value);
}
}

let json_string = to_string(&payload)
.map_err(|_| Error::GraphqlError("Failed to serialise payload".to_owned()))?;

if let Some(key) = signing_key {
let json_string =
to_string(&body).map_err(|_| Error::GraphqlError("Body malformat.".to_owned()))?;
let payload: Vec<u8> = json_string.into_bytes();
let signing = key.sign_payload(&payload).map_err(Error::CryptoError)?;
let signing = key
.sign_payload(&json_string.as_bytes())
.map_err(Error::CryptoError)?;
headers.insert(
"X-Lightspark-Signing",
HeaderValue::from_str(signing.as_str()).map_err(|_| Error::InvalidHeaderValue)?,
);
}

let body = if json_string.len() > 1024 {
headers.insert("Content-Encoding", HeaderValue::from_static("zstd"));
zstd::bulk::compress(json_string.as_bytes(), 0)
.map_err(|_| Error::GraphqlError("Failed to compress payload".to_owned()))?
} else {
json_string.into_bytes()
};

let url = match &self.base_url {
Some(base_url) => base_url.clone(),
None => DEFAULT_BASE_URL.to_owned(),
Expand All @@ -187,7 +197,7 @@ impl Requester {
.client
.post(url)
.headers(headers)
.json(&body)
.body(body)
.send()
.await
.map_err(|e| Error::ReqwestError(e.to_string()))?;
Expand Down

0 comments on commit 08a094f

Please sign in to comment.