-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
Copy pathlib.rs
222 lines (194 loc) · 6.18 KB
/
lib.rs
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
#![feature(min_specialization)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
use anyhow::Result;
use turbo_rcstr::RcStr;
use turbo_tasks::{duration_span, mark_session_dependent, ResolvedVc, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::issue::{Issue, IssueSeverity, IssueStage, OptionStyledString, StyledString};
pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
turbopack_core::register();
include!(concat!(env!("OUT_DIR"), "/register.rs"));
}
#[turbo_tasks::value(transparent)]
pub struct FetchResult(Result<ResolvedVc<HttpResponse>, ResolvedVc<FetchError>>);
#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub struct HttpResponse {
pub status: u16,
pub body: ResolvedVc<HttpResponseBody>,
}
#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub struct HttpResponseBody(pub Vec<u8>);
#[turbo_tasks::value_impl]
impl HttpResponseBody {
#[turbo_tasks::function]
pub async fn to_string(self: Vc<Self>) -> Result<Vc<RcStr>> {
let this = &*self.await?;
Ok(Vc::cell(std::str::from_utf8(&this.0)?.into()))
}
}
#[turbo_tasks::value(shared)]
#[derive(Debug)]
pub enum ProxyConfig {
Http(String),
Https(String),
}
#[turbo_tasks::value(transparent)]
pub struct OptionProxyConfig(Option<ProxyConfig>);
#[turbo_tasks::function(network)]
pub async fn fetch(
url: Vc<RcStr>,
user_agent: Vc<Option<RcStr>>,
proxy_option: Vc<OptionProxyConfig>,
) -> Result<Vc<FetchResult>> {
let url = &*url.await?;
let user_agent = &*user_agent.await?;
let proxy_option = &*proxy_option.await?;
let client_builder = reqwest::Client::builder();
let client_builder = match proxy_option {
Some(ProxyConfig::Http(proxy)) => client_builder.proxy(reqwest::Proxy::http(proxy)?),
Some(ProxyConfig::Https(proxy)) => client_builder.proxy(reqwest::Proxy::https(proxy)?),
_ => client_builder,
};
let client = client_builder.build()?;
let mut builder = client.get(url.as_str());
if let Some(user_agent) = user_agent {
builder = builder.header("User-Agent", user_agent.as_str());
}
let response = {
let _span = duration_span!("fetch request", url = url.as_str());
builder.send().await
}
.and_then(|r| r.error_for_status());
match response {
Ok(response) => {
let status = response.status().as_u16();
let body = {
let _span = duration_span!("fetch response", url = url.as_str());
response.bytes().await?
}
.to_vec();
Ok(Vc::cell(Ok(HttpResponse {
status,
body: HttpResponseBody::resolved_cell(HttpResponseBody(body)),
}
.resolved_cell())))
}
Err(err) => {
mark_session_dependent();
Ok(Vc::cell(Err(
FetchError::from_reqwest_error(&err, url).resolved_cell()
)))
}
}
}
#[derive(Debug)]
#[turbo_tasks::value(shared)]
pub enum FetchErrorKind {
Connect,
Timeout,
Status(u16),
Other,
}
#[turbo_tasks::value(shared)]
pub struct FetchError {
pub url: ResolvedVc<RcStr>,
pub kind: ResolvedVc<FetchErrorKind>,
pub detail: ResolvedVc<StyledString>,
}
impl FetchError {
fn from_reqwest_error(error: &reqwest::Error, url: &str) -> FetchError {
let kind = if error.is_connect() {
FetchErrorKind::Connect
} else if error.is_timeout() {
FetchErrorKind::Timeout
} else if let Some(status) = error.status() {
FetchErrorKind::Status(status.as_u16())
} else {
FetchErrorKind::Other
};
FetchError {
detail: StyledString::Text(error.to_string().into()).resolved_cell(),
url: ResolvedVc::cell(url.into()),
kind: kind.resolved_cell(),
}
}
}
#[turbo_tasks::value_impl]
impl FetchError {
#[turbo_tasks::function]
pub async fn to_issue(
self: Vc<Self>,
severity: ResolvedVc<IssueSeverity>,
issue_context: ResolvedVc<FileSystemPath>,
) -> Result<Vc<FetchIssue>> {
let this = &*self.await?;
Ok(FetchIssue {
issue_context,
severity,
url: this.url,
kind: this.kind,
detail: this.detail,
}
.into())
}
}
#[turbo_tasks::value(shared)]
pub struct FetchIssue {
pub issue_context: ResolvedVc<FileSystemPath>,
pub severity: ResolvedVc<IssueSeverity>,
pub url: ResolvedVc<RcStr>,
pub kind: ResolvedVc<FetchErrorKind>,
pub detail: ResolvedVc<StyledString>,
}
#[turbo_tasks::value_impl]
impl Issue for FetchIssue {
#[turbo_tasks::function]
fn file_path(&self) -> Vc<FileSystemPath> {
*self.issue_context
}
#[turbo_tasks::function]
fn severity(&self) -> Vc<IssueSeverity> {
*self.severity
}
#[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> {
StyledString::Text("Error while requesting resource".into()).cell()
}
#[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> {
IssueStage::Load.into()
}
#[turbo_tasks::function]
async fn description(&self) -> Result<Vc<OptionStyledString>> {
let url = &*self.url.await?;
let kind = &*self.kind.await?;
Ok(Vc::cell(Some(
StyledString::Text(match kind {
FetchErrorKind::Connect => format!(
"There was an issue establishing a connection while requesting {}.",
url
)
.into(),
FetchErrorKind::Status(status) => format!(
"Received response with status {} when requesting {}",
status, url
)
.into(),
FetchErrorKind::Timeout => {
format!("Connection timed out when requesting {}", url).into()
}
FetchErrorKind::Other => format!("There was an issue requesting {}", url).into(),
})
.resolved_cell(),
)))
}
#[turbo_tasks::function]
fn detail(&self) -> Vc<OptionStyledString> {
Vc::cell(Some(self.detail))
}
}