surrealdb-sql 1.1.0

Full type definitions for the SurrealQL query language
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
//! Request class implementation

use js::{class::Trace, prelude::Coerced, Class, Ctx, Exception, FromJs, Object, Result, Value};
use reqwest::Method;

use crate::fnc::script::fetch::{body::Body, RequestError};

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum RequestMode {
	Navigate,
	SameOrigin,
	NoCors,
	Cors,
}

impl<'js> FromJs<'js> for RequestMode {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let res = if let Some(Coerced(x)) = <Option<Coerced<String>>>::from_js(ctx, value)? {
			match x.as_str() {
				"navigate" => RequestMode::Navigate,
				"same-origin" => RequestMode::SameOrigin,
				"no-cors" => RequestMode::NoCors,
				"cors" => RequestMode::Cors,
				x => {
					return Err(Exception::throw_type(
						ctx,
						&format!(
							"unexpected request mode `{}`, expected one of \
							`navigate`,\
							`same-origin`,\
							`no-cors`,\
							or `cors`",
							x
						),
					))
				}
			}
		} else {
			RequestMode::Cors
		};
		Ok(res)
	}
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum RequestCredentials {
	Omit,
	SameOrigin,
	Include,
}

impl<'js> FromJs<'js> for RequestCredentials {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let res = if let Some(Coerced(x)) = <Option<Coerced<String>>>::from_js(ctx, value)? {
			match x.as_str() {
				"omit" => RequestCredentials::Omit,
				"same-origin" => RequestCredentials::SameOrigin,
				"include" => RequestCredentials::Include,
				x => {
					return Err(Exception::throw_type(
						ctx,
						&format!(
							"unexpected request credentials `{}`, expected one of \
								`omit`\
								, `same-oring`\
								, or `include`",
							x
						),
					))
				}
			}
		} else {
			RequestCredentials::Omit
		};
		Ok(res)
	}
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum RequestCache {
	Default,
	NoStore,
	Reload,
	NoCache,
	ForceCache,
	OnlyIfCached,
}

impl<'js> FromJs<'js> for RequestCache {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let res = if let Some(Coerced(x)) = <Option<Coerced<String>>>::from_js(ctx, value)? {
			match x.as_str() {
				"default" => RequestCache::Default,
				"no-store" => RequestCache::NoStore,
				"reload" => RequestCache::Reload,
				"no-cache" => RequestCache::NoCache,
				"force-cache" => RequestCache::ForceCache,
				"only-if-cached" => RequestCache::OnlyIfCached,
				x => {
					return Err(Exception::throw_type(
						ctx,
						&format!(
							"unexpected request cache `{}`, expected one of \
								`default`\
								, `no-store`\
								, `reload`\
								, `no-cache`\
								, `force-cache`\
								, or `only-if-cached`",
							x
						),
					))
				}
			}
		} else {
			RequestCache::Default
		};
		Ok(res)
	}
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum RequestRedirect {
	Follow,
	Error,
	Manual,
}

impl<'js> FromJs<'js> for RequestRedirect {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let res = if let Some(Coerced(x)) = <Option<Coerced<String>>>::from_js(ctx, value)? {
			match x.as_str() {
				"follow" => RequestRedirect::Follow,
				"error" => RequestRedirect::Error,
				"manual" => RequestRedirect::Manual,
				x => {
					return Err(Exception::throw_type(
						ctx,
						&format!(
							"unexpected request redirect `{}`, expected one of \
							`follow`,\
							`error`,\
							or `manual`",
							x
						),
					))
				}
			}
		} else {
			RequestRedirect::Follow
		};
		Ok(res)
	}
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub enum ReferrerPolicy {
	Empty,
	NoReferrer,
	NoReferrerWhenDowngrade,
	SameOrigin,
	Origin,
	StrictOrigin,
	OriginWhenCrossOrigin,
	StrictOriginWhenCrossOrigin,
	UnsafeUrl,
}

impl<'js> FromJs<'js> for ReferrerPolicy {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let res = if let Some(Coerced(x)) = <Option<Coerced<String>>>::from_js(ctx, value)? {
			match x.as_str() {
				"" => ReferrerPolicy::Empty,
				"no-referrer" => ReferrerPolicy::NoReferrer,
				"no-referrer-when-downgrade" => ReferrerPolicy::NoReferrerWhenDowngrade,
				"same-origin" => ReferrerPolicy::SameOrigin,
				"origin" => ReferrerPolicy::Origin,
				"strict-origin" => ReferrerPolicy::StrictOrigin,
				"origin-when-cross-origin" => ReferrerPolicy::OriginWhenCrossOrigin,
				"strict-origin-when-cross-origin" => ReferrerPolicy::StrictOriginWhenCrossOrigin,
				"unsafe-url" => ReferrerPolicy::UnsafeUrl,
				x => {
					return Err(Exception::throw_type(
						ctx,
						&format!(
							"unexpected referrer policy `{}`, expected one of \
							, ``\
							, `no-referrer`\
							, `no-referrer-when-downgrade`\
							, `same-origin`\
							, `strict-origin`\
							, `origin-when-cross-origin`\
							, `strict-origin-when-cross-origin`\
							, or `unsafe-url1`",
							x
						),
					))
				}
			}
		} else {
			ReferrerPolicy::Empty
		};
		Ok(res)
	}
}

pub struct RequestInit<'js> {
	pub method: Method,
	pub headers: Class<'js, Headers>,
	pub body: Option<Body>,
	pub referrer: String,
	pub referrer_policy: ReferrerPolicy,
	pub request_mode: RequestMode,
	pub request_credentials: RequestCredentials,
	pub request_cache: RequestCache,
	pub request_redirect: RequestRedirect,
	pub integrity: String,
	pub keep_alive: bool,
}

impl<'js> Trace<'js> for RequestInit<'js> {
	fn trace<'a>(&self, tracer: js::class::Tracer<'a, 'js>) {
		self.headers.trace(tracer);
	}
}

impl<'js> RequestInit<'js> {
	pub fn default(ctx: Ctx<'js>) -> Result<Self> {
		let headers = Class::instance(ctx, Headers::new_empty())?;
		Ok(RequestInit {
			method: Method::GET,
			headers,
			body: None,
			referrer: "client".to_string(),
			referrer_policy: ReferrerPolicy::Empty,
			request_mode: RequestMode::Cors,
			request_credentials: RequestCredentials::SameOrigin,
			request_cache: RequestCache::Default,
			request_redirect: RequestRedirect::Follow,
			integrity: String::new(),
			keep_alive: false,
		})
	}

	pub fn clone_js(&self, ctx: Ctx<'js>) -> Result<Self> {
		let headers = self.headers.clone();
		let headers = Class::instance(ctx.clone(), headers.borrow().clone())?;

		let body = self.body.as_ref().map(|x| x.clone_js(ctx));

		Ok(RequestInit {
			method: self.method.clone(),
			headers,
			body,
			referrer: self.referrer.clone(),
			referrer_policy: self.referrer_policy,
			request_mode: self.request_mode,
			request_credentials: self.request_credentials,
			request_cache: self.request_cache,
			request_redirect: self.request_redirect,
			integrity: self.integrity.clone(),
			keep_alive: self.keep_alive,
		})
	}
}

// Normalize method string according to spec.
fn normalize_method(ctx: &Ctx<'_>, m: String) -> Result<Method> {
	if m.as_bytes().eq_ignore_ascii_case(b"CONNECT")
		|| m.as_bytes().eq_ignore_ascii_case(b"TRACE")
		|| m.as_bytes().eq_ignore_ascii_case(b"TRACK")
	{
		//methods that are not allowed [`https://fetch.spec.whatwg.org/#methods`]
		return Err(Exception::throw_type(ctx, &format!("method {m} is forbidden")));
	}

	// The following methods must be uppercased to the default case insensitive equivalent.
	if m.as_bytes().eq_ignore_ascii_case(b"DELETE") {
		return Ok(Method::DELETE);
	}
	if m.as_bytes().eq_ignore_ascii_case(b"GET") {
		return Ok(Method::GET);
	}
	if m.as_bytes().eq_ignore_ascii_case(b"HEAD") {
		return Ok(Method::HEAD);
	}
	if m.as_bytes().eq_ignore_ascii_case(b"OPTIONS") {
		return Ok(Method::OPTIONS);
	}
	if m.as_bytes().eq_ignore_ascii_case(b"POST") {
		return Ok(Method::POST);
	}
	if m.as_bytes().eq_ignore_ascii_case(b"PUT") {
		return Ok(Method::PUT);
	}

	match Method::from_bytes(m.as_bytes()) {
		Ok(x) => Ok(x),
		Err(e) => Err(Exception::throw_type(ctx, &format!("invalid method: {e}"))),
	}
}

impl<'js> FromJs<'js> for RequestInit<'js> {
	fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self> {
		let object = Object::from_js(ctx, value)?;

		let referrer = object
			.get::<_, Option<Coerced<String>>>("referrer")?
			.map(|x| x.0)
			.unwrap_or_else(|| "client".to_owned());
		let method = object
			.get::<_, Option<String>>("method")?
			.map(|m| normalize_method(ctx, m))
			.transpose()?
			.unwrap_or(Method::GET);

		let referrer_policy = object.get("referrerPolicy")?;
		let request_mode = object.get("mode")?;
		let request_redirect = object.get("redirect")?;
		let request_cache = object.get("cache")?;
		let request_credentials = object.get("credentials")?;
		let integrity = object
			.get::<_, Option<Coerced<String>>>("integrity")?
			.map(|x| x.0)
			.unwrap_or_else(String::new);
		let keep_alive =
			object.get::<_, Option<Coerced<bool>>>("keep_alive")?.map(|x| x.0).unwrap_or_default();

		// duplex can only be `half`
		if let Some(Coerced(x)) = object.get::<_, Option<Coerced<String>>>("duplex")? {
			if x != "half" {
				return Err(Exception::throw_type(
					ctx,
					&format!("unexpected request duplex `{}` expected `half`", x),
				));
			}
		}

		let headers = if let Some(hdrs) = object.get::<_, Option<Object>>("headers")? {
			if let Some(cls) = Class::<Headers>::from_object(hdrs.clone()) {
				cls
			} else {
				Class::instance(ctx.clone(), Headers::new_inner(ctx, hdrs.into_value())?)?
			}
		} else {
			Class::instance(ctx.clone(), Headers::new_empty())?
		};

		let body = object.get::<_, Option<Body>>("body")?;

		Ok(Self {
			method,
			headers,
			body,
			referrer,
			referrer_policy,
			request_mode,
			request_credentials,
			request_cache,
			request_redirect,
			integrity,
			keep_alive,
		})
	}
}

pub use super::*;

use bytes::Bytes;
use js::function::Opt;
// TODO: change implementation based on features.
use reqwest::{header::HeaderName, Url};

#[allow(dead_code)]
#[js::class]
#[derive(Trace)]
pub struct Request<'js> {
	#[qjs(skip_trace)]
	pub(crate) url: Url,
	pub(crate) init: RequestInit<'js>,
}

#[js::methods]
impl<'js> Request<'js> {
	// ------------------------------
	// Constructor
	// ------------------------------

	#[qjs(constructor)]
	pub fn new(ctx: Ctx<'js>, input: Value<'js>, init: Opt<RequestInit<'js>>) -> Result<Self> {
		if let Some(url) = input.as_string() {
			// url string
			let url_str = url.to_string()?;
			let url = Url::parse(&url_str)
				.map_err(|e| Exception::throw_type(&ctx, &format!("failed to parse url: {e}")))?;

			if !url.username().is_empty() || !url.password().map(str::is_empty).unwrap_or(true) {
				// url cannot contain non empty username and passwords
				return Err(Exception::throw_type(&ctx, "Url contained credentials."));
			}
			let init = init.into_inner().map_or_else(|| RequestInit::default(ctx.clone()), Ok)?;
			// HEAD and GET methods can't have a body
			if init.body.is_some() && init.method == Method::GET || init.method == Method::HEAD {
				return Err(Exception::throw_type(
					&ctx,
					&format!("Request with method `{}` cannot have a body", init.method),
				));
			}

			Ok(Self {
				url,
				init,
			})
		} else if let Some(request) = input.into_object().and_then(Class::<Self>::from_object) {
			// existing request object, just return it
			request.try_borrow()?.clone_js(ctx.clone())
		} else {
			Err(Exception::throw_type(
				&ctx,
				"request `init` parameter must either be a request object or a string",
			))
		}
	}

	/// Clone the response, teeing any possible underlying streams.
	#[qjs(rename = "clone")]
	pub fn clone_js(&self, ctx: Ctx<'js>) -> Result<Self> {
		Ok(Self {
			url: self.url.clone(),
			init: self.init.clone_js(ctx)?,
		})
	}

	// ------------------------------
	// Instance properties
	// ------------------------------
	#[qjs(get, rename = "body_used")]
	pub fn body_used(&self) -> bool {
		self.init.body.as_ref().map(Body::used).unwrap_or(true)
	}

	#[qjs(get)]
	pub fn method(&self) -> String {
		self.init.method.to_string()
	}

	#[qjs(get)]
	pub fn url(&self) -> String {
		self.url.to_string()
	}

	#[qjs(get)]
	pub fn headers(&self) -> Class<'js, Headers> {
		self.init.headers.clone()
	}

	#[qjs(get)]
	pub fn referrer(&self) -> String {
		self.init.referrer.clone()
	}
	// TODO

	// ------------------------------
	// Instance methods
	// ------------------------------

	// Convert the object to a string
	#[qjs(rename = "toString")]
	pub fn js_to_string(&self) -> String {
		String::from("[object Request]")
	}

	/// Takes the buffer from the body leaving it used.
	#[qjs(skip)]
	async fn take_buffer(&self, ctx: &Ctx<'js>) -> Result<Bytes> {
		let Some(body) = self.init.body.as_ref() else {
			return Ok(Bytes::new());
		};
		match body.to_buffer().await {
			Ok(Some(x)) => Ok(x),
			Ok(None) => Err(Exception::throw_type(ctx, "Body unusable")),
			Err(e) => match e {
				RequestError::Reqwest(e) => {
					Err(Exception::throw_type(ctx, &format!("stream failed: {e}")))
				}
			},
		}
	}

	// Returns a promise with the request body as a Blob
	pub async fn blob(&self, ctx: Ctx<'js>) -> Result<Blob> {
		let headers = self.init.headers.clone();
		let mime = {
			let headers = headers.borrow();
			let headers = &headers.inner;
			let key = HeaderName::from_static("content-type");
			let types = headers.get_all(key);
			// TODO: This is not according to spec.
			types
				.iter()
				.next()
				.map(|x| x.to_str().unwrap_or("text/html"))
				.unwrap_or("text/html")
				.to_owned()
		};

		let data = self.take_buffer(&ctx).await?;
		Ok(Blob {
			mime,
			data,
		})
	}

	// Returns a promise with the request body as FormData
	#[qjs(rename = "formData")]
	pub async fn form_data(&self, ctx: Ctx<'js>) -> Result<Value<'js>> {
		Err(Exception::throw_internal(&ctx, "Not yet implemented"))
	}

	// Returns a promise with the request body as JSON
	pub async fn json(&self, ctx: Ctx<'js>) -> Result<Value<'js>> {
		let text = self.text(ctx.clone()).await?;
		ctx.json_parse(text)
	}

	// Returns a promise with the request body as text
	pub async fn text(&self, ctx: Ctx<'js>) -> Result<String> {
		let data = self.take_buffer(&ctx).await?;

		// Skip UTF-BOM
		if data.starts_with(&[0xEF, 0xBB, 0xBF]) {
			Ok(String::from_utf8_lossy(&data[3..]).into_owned())
		} else {
			Ok(String::from_utf8_lossy(&data).into_owned())
		}
	}
}

#[cfg(test)]
mod test {
	use crate::fnc::script::fetch::test::create_test_context;
	use js::{promise::Promise, CatchResultExt};

	#[tokio::test]
	async fn basic_request_use() {
		create_test_context!(ctx => {
			ctx.eval::<Promise<()>,_>(r#"
				(async () => {
					assert.mustThrow(() => {
						new Request("invalid url")
					});
					assert.mustThrow(() => {
						new Request("http://invalid url")
					});
					// no credentials
					assert.mustThrow(() => {
						new Request("http://username:password@some_url.com")
					});
					// invalid option value
					assert.mustThrow(() => {
						new Request("http://a",{ referrerPolicy: "invalid value"})
					});
					assert.mustThrow(() => {
						new Request("http://a",{ mode: "invalid value"})
					});
					assert.mustThrow(() => {
						new Request("http://a",{ redirect: "invalid value"})
					});
					assert.mustThrow(() => {
						new Request("http://a",{ cache: "invalid value"})
					});
					assert.mustThrow(() => {
						new Request("http://a",{ credentials: "invalid value"})
					});
					assert.mustThrow(() => {
						new Request("http://a",{ duplex: "invalid value"})
					});

					let req = new Request("http://a",{ method: "PUT", body: "some text" });
					assert.seq(await req.text(),"some text");

					req = new Request("http://a",{ method: "PUT", body: JSON.stringify({ a: 1, b: [2], c: { d: 3} })});
					let res = await req.json();
					assert.seq(res.a,1);
					assert(Array.isArray(res.b));
					assert.seq(res.b[0],2);
					assert.seq(typeof res.c,"object");
					assert.seq(res.c.d,3);

					// some methods must be uppercased.
					req = new Request("http://a",{ method: "gEt" });
					assert.seq(req.method,"GET");

					// get requests can't have a body.
					assert.mustThrow(() => {
						new Request("http://a",{ body: "a"})
					})
					// head requests can't have a body.
					assert.mustThrow(() => {
						new Request("http://a",{ method: "HEAD", body: "a"})
					})

					// use body twice
					await assert.mustThrowAsync(async () => {
						let req = new Request("http://a",{ method: "PUT",body: "some text" });
						await req.text();
						await req.text();
					});

					// clone request
					req = new Request("http://a",{ method: "PUT", body: "some text" });
					let req_2 = req.clone()
					assert.seq(await req.text(),"some text");
					assert.seq(await req_2.text(),"some text");

				})()
			"#).catch(&ctx).unwrap().await.catch(&ctx).unwrap();
		})
		.await;
	}
}