MCPcopy Create free account
hub / github.com/awslabs/llrt / new

Method new

modules/llrt_fetch/src/request.rs:100–253  ·  view source on GitHub ↗
(
        ctx: Ctx<'js>,
        this: This<Value<'js>>,
        input: Value<'js>,
        options: Opt<Value<'js>>,
    )

Source from the content-addressed store, hash-verified

98impl<'js> Request<'js> {
99 #[qjs(constructor)]
100 pub fn new(
101 ctx: Ctx<'js>,
102 this: This<Value<'js>>,
103 input: Value<'js>,
104 options: Opt<Value<'js>>,
105 ) -> Result<Self> {
106 // When called with `new`, rquickjs passes the constructor function as
107 // `this`. When called without `new`, `this` is undefined (strict) or
108 // the global object (sloppy). WPT's request-error.any.js requires
109 // `Request("...")` to throw TypeError.
110 if this.as_function().is_none() {
111 return Err(Exception::throw_type(
112 &ctx,
113 "Failed to construct 'Request': Please use the 'new' operator",
114 ));
115 }
116 let mut request = Self {
117 url: "".into(),
118 method: Method::GET,
119 headers: None,
120 body: RwLock::new(BodyVariant::Empty),
121 body_stream: RwLock::new(None),
122 signal: None,
123 mode: RequestMode::Cors,
124 keepalive: false,
125 agent: None,
126 };
127
128 // If the input is a Request, we may need to tee its body at the very
129 // end so construction failures (e.g. GET with body) don't leave the
130 // input disturbed. This holds the input request and whether init.body
131 // overrides the body.
132 let mut input_request_to_disturb: Option<(Class<'js, Request<'js>>, bool)> = None;
133
134 if input.is_string() {
135 let s: String = input.get()?;
136 // Validate as a URL; accept relative URLs against a generic base.
137 if s.is_empty() {
138 return Err(Exception::throw_type(&ctx, "Invalid URL"));
139 }
140 let base = url::Url::parse("http://llrt.local/").expect("static base URL");
141 let parsed = base
142 .join(&s)
143 .map_err(|_| Exception::throw_type(&ctx, "Invalid URL"))?;
144 if !parsed.username().is_empty() || parsed.password().is_some() {
145 return Err(Exception::throw_type(&ctx, "URL must not have credentials"));
146 }
147 request.url = s;
148 } else if let Ok(url) = URL::from_js(&ctx, input.clone()) {
149 request.url = url.to_string();
150 } else if input.is_object() {
151 let obj = input.as_object().expect("input is an object");
152 // Check if input is a Request - if so, transfer body (mark original as used)
153 if let Some(input_request) = Class::<Request>::from_object(obj) {
154 let input_req = input_request.borrow();
155 let init_overrides_body = options
156 .0
157 .as_ref()

Callers

nothing calls this directly

Calls 15

parseFunction · 0.85
is_unusable_bodyFunction · 0.85
assign_requestFunction · 0.85
tee_readable_streamFunction · 0.85
joinMethod · 0.80
usernameMethod · 0.80
passwordMethod · 0.80
as_objectMethod · 0.80
okMethod · 0.80
writeMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected