Skip to content

Commit

Permalink
fetch optimizations (denoland#9402)
Browse files Browse the repository at this point in the history
Release deno_fetch 0.20.2
  • Loading branch information
ry committed Feb 4, 2021
1 parent 25b35be commit 644a7ff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ path = "./bench/main.rs"

[build-dependencies]
deno_core = { path = "../core", version = "0.77.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.2" }
deno_web = { path = "../op_crates/web", version = "0.28.1" }
deno_websocket = { path = "../op_crates/websocket", version = "0.3.1" }
regex = "1.4.3"
Expand Down
31 changes: 27 additions & 4 deletions op_crates/fetch/26_fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@

const teeBody = Symbol("Body#tee");

// fastBody and dontValidateUrl allow users to opt out of certain behaviors
const fastBody = Symbol("Body#fast");
const dontValidateUrl = Symbol("dontValidateUrl");

class Body {
#contentType = "";
#size;
Expand Down Expand Up @@ -730,6 +734,17 @@
return this.#stream;
}

// Optimization that allows caller to bypass expensive ReadableStream.
[fastBody]() {
if (!this.#bodySource) {
return null;
} else if (!(this.#bodySource instanceof ReadableStream)) {
return bodyToArrayBuffer(this.#bodySource);
} else {
return this.body;
}
}

/** @returns {BodyInit | null} */
[teeBody]() {
if (this.#stream || this.#bodySource instanceof ReadableStream) {
Expand Down Expand Up @@ -991,10 +1006,16 @@
this.#headers = new Headers(input.headers);
this.#credentials = input.credentials;
} else {
const baseUrl = getLocationHref();
this.#url = baseUrl != null
? new URL(String(input), baseUrl).href
: new URL(String(input)).href;
// Constructing a URL just for validation is known to be expensive.
// dontValidateUrl allows one to opt out.
if (init[dontValidateUrl]) {
this.#url = input;
} else {
const baseUrl = getLocationHref();
this.#url = baseUrl != null
? new URL(String(input), baseUrl).href
: new URL(String(input)).href;
}
}

if (init && "method" in init && init.method) {
Expand Down Expand Up @@ -1477,5 +1498,7 @@
Response,
HttpClient,
createHttpClient,
fastBody,
dontValidateUrl,
};
})(this);
2 changes: 1 addition & 1 deletion op_crates/fetch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "deno_fetch"
version = "0.20.1"
version = "0.20.2"
edition = "2018"
description = "provides fetch Web API to deno_core"
authors = ["the Deno authors"]
Expand Down
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ path = "examples/hello_runtime.rs"
[build-dependencies]
deno_core = { path = "../core", version = "0.77.1" }
deno_crypto = { path = "../op_crates/crypto", version = "0.11.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.2" }
deno_web = { path = "../op_crates/web", version = "0.28.1" }
deno_websocket = { path = "../op_crates/websocket", version = "0.3.1" }

Expand All @@ -31,7 +31,7 @@ winapi = "0.3.9"
[dependencies]
deno_core = { path = "../core", version = "0.77.1" }
deno_crypto = { path = "../op_crates/crypto", version = "0.11.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.1" }
deno_fetch = { path = "../op_crates/fetch", version = "0.20.2" }
deno_web = { path = "../op_crates/web", version = "0.28.1" }
deno_websocket = { path = "../op_crates/websocket", version = "0.3.1" }

Expand Down

0 comments on commit 644a7ff

Please sign in to comment.