Skip to content

Commit

Permalink
feat - discrete cookie return
Browse files Browse the repository at this point in the history
  • Loading branch information
rumblefrog authored and KishanBagaria committed Nov 16, 2021
1 parent e1bd87a commit 7cf2051
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 15 deletions.
191 changes: 190 additions & 1 deletion Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-fetch"
version = "0.6.18"
version = "0.6.19"
authors = ["rumblefrog <[email protected]>"]
edition = "2018"

Expand All @@ -22,7 +22,8 @@ features = ["rt-multi-thread"]
git = "https://github.com/TextsHQ/reqwest"
branch = "master"
default-features = false
features = ["native-tls-alpn", "gzip", "brotli"]
# Cookies is used for the time_jar in order to invoke pre-redirect headers sets in reqwest.
features = ["native-tls-alpn", "gzip", "brotli", "cookies"]

[dependencies.neon]
git = "https://github.com/neon-bindings/neon"
Expand Down
21 changes: 12 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ export interface Response<T> {
* Each header may have more than one value in the value array.
*/
headers: Record<string, string | string[]>;

/**
* New cookies present since request time.
*
* URL => cookies[]
*/
newCookies: Record<string, string[]>;
}

export class Client {
Expand Down Expand Up @@ -179,16 +186,12 @@ export class Client {
args.body = (<FormData> args.body).getBuffer();
}

const res = await requestPromise.call(this.#client, url, args);
const res: Response<T> = await requestPromise.call(this.#client, url, args);

for (const [k, v] of Object.entries(res.headers)) {
if (args.cookieJar && k === 'set-cookie') {
if (Array.isArray(v)) {
for (const item of v as string[]) {
args.cookieJar.setCookieSync(item, url);
}
} else {
args.cookieJar.setCookieSync(v as string, url);
if (args.cookieJar) {
for (const [k, v] of Object.entries(res.newCookies)) {
for (const item of v) {
args.cookieJar.setCookieSync(item, k);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rust-fetch",
"version": "0.6.18",
"version": "0.6.19",
"description": "Rust HTTP wrapper for JS",
"main": "dist/index.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use reqwest::redirect::Policy;
use reqwest::ClientBuilder;

use crate::client::Client;
use crate::time_jar::TimeJar;

pub struct Builder(Option<BuilderInner>);

Expand Down Expand Up @@ -177,13 +178,18 @@ impl Builder {

cb.client = cb.client.http2_initial_stream_window_size(1024 * 256 * 24);

let time_jar = std::sync::Arc::new(TimeJar::default());

cb.client = cb.client.cookie_provider(time_jar.clone());

let client = cb.client.build().unwrap();

Ok(JsBox::new(
&mut cx,
Client {
runtime: Runtime::new().unwrap(),
client,
time_jar,
},
))
}
Expand Down
Loading

0 comments on commit 7cf2051

Please sign in to comment.