summaryrefslogtreecommitdiff
path: root/ext/web/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'ext/web/lib.rs')
-rw-r--r--ext/web/lib.rs152
1 files changed, 52 insertions, 100 deletions
diff --git a/ext/web/lib.rs b/ext/web/lib.rs
index 3977379a5..af0fc2c27 100644
--- a/ext/web/lib.rs
+++ b/ext/web/lib.rs
@@ -6,9 +6,6 @@ mod message_port;
mod stream_resource;
mod timers;
-use deno_core::error::range_error;
-use deno_core::error::type_error;
-use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::url::Url;
use deno_core::v8;
@@ -22,10 +19,14 @@ use encoding_rs::DecoderResult;
use encoding_rs::Encoding;
use std::borrow::Cow;
use std::cell::RefCell;
-use std::fmt;
use std::path::PathBuf;
use std::sync::Arc;
+pub use blob::BlobError;
+pub use compression::CompressionError;
+pub use message_port::MessagePortError;
+pub use stream_resource::StreamResourceError;
+
use crate::blob::op_blob_create_object_url;
use crate::blob::op_blob_create_part;
use crate::blob::op_blob_from_object_url;
@@ -51,7 +52,8 @@ pub use crate::message_port::Transferable;
use crate::timers::op_defer;
use crate::timers::op_now;
-use crate::timers::StartTime;
+use crate::timers::op_time_origin;
+pub use crate::timers::StartTime;
pub use crate::timers::TimersPermission;
deno_core::extension!(deno_web,
@@ -83,6 +85,7 @@ deno_core::extension!(deno_web,
compression::op_compression_write,
compression::op_compression_finish,
op_now<P>,
+ op_time_origin<P>,
op_defer,
stream_resource::op_readable_stream_resource_allocate,
stream_resource::op_readable_stream_resource_allocate_sized,
@@ -122,13 +125,31 @@ deno_core::extension!(deno_web,
if let Some(location) = options.maybe_location {
state.put(Location(location));
}
- state.put(StartTime::now());
+ state.put(StartTime::default());
}
);
+#[derive(Debug, thiserror::Error)]
+pub enum WebError {
+ #[error("Failed to decode base64")]
+ Base64Decode,
+ #[error("The encoding label provided ('{0}') is invalid.")]
+ InvalidEncodingLabel(String),
+ #[error("buffer exceeds maximum length")]
+ BufferTooLong,
+ #[error("Value too large to decode")]
+ ValueTooLarge,
+ #[error("Provided buffer too small")]
+ BufferTooSmall,
+ #[error("The encoded data is not valid")]
+ DataInvalid,
+ #[error(transparent)]
+ DataError(#[from] v8::DataError),
+}
+
#[op2]
#[serde]
-fn op_base64_decode(#[string] input: String) -> Result<ToJsBuffer, AnyError> {
+fn op_base64_decode(#[string] input: String) -> Result<ToJsBuffer, WebError> {
let mut s = input.into_bytes();
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
@@ -137,7 +158,7 @@ fn op_base64_decode(#[string] input: String) -> Result<ToJsBuffer, AnyError> {
#[op2]
#[serde]
-fn op_base64_atob(#[serde] mut s: ByteString) -> Result<ByteString, AnyError> {
+fn op_base64_atob(#[serde] mut s: ByteString) -> Result<ByteString, WebError> {
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
Ok(s)
@@ -147,11 +168,9 @@ fn op_base64_atob(#[serde] mut s: ByteString) -> Result<ByteString, AnyError> {
#[inline]
fn forgiving_base64_decode_inplace(
input: &mut [u8],
-) -> Result<usize, AnyError> {
- let error =
- || DomExceptionInvalidCharacterError::new("Failed to decode base64");
- let decoded =
- base64_simd::forgiving_decode_inplace(input).map_err(|_| error())?;
+) -> Result<usize, WebError> {
+ let decoded = base64_simd::forgiving_decode_inplace(input)
+ .map_err(|_| WebError::Base64Decode)?;
Ok(decoded.len())
}
@@ -177,13 +196,9 @@ fn forgiving_base64_encode(s: &[u8]) -> String {
#[string]
fn op_encoding_normalize_label(
#[string] label: String,
-) -> Result<String, AnyError> {
+) -> Result<String, WebError> {
let encoding = Encoding::for_label_no_replacement(label.as_bytes())
- .ok_or_else(|| {
- range_error(format!(
- "The encoding label provided ('{label}') is invalid."
- ))
- })?;
+ .ok_or(WebError::InvalidEncodingLabel(label))?;
Ok(encoding.name().to_lowercase())
}
@@ -192,7 +207,7 @@ fn op_encoding_decode_utf8<'a>(
scope: &mut v8::HandleScope<'a>,
#[anybuffer] zero_copy: &[u8],
ignore_bom: bool,
-) -> Result<v8::Local<'a, v8::String>, AnyError> {
+) -> Result<v8::Local<'a, v8::String>, WebError> {
let buf = &zero_copy;
let buf = if !ignore_bom
@@ -216,7 +231,7 @@ fn op_encoding_decode_utf8<'a>(
// - https://github.com/v8/v8/blob/d68fb4733e39525f9ff0a9222107c02c28096e2a/include/v8.h#L3277-L3278
match v8::String::new_from_utf8(scope, buf, v8::NewStringType::Normal) {
Some(text) => Ok(text),
- None => Err(type_error("buffer exceeds maximum length")),
+ None => Err(WebError::BufferTooLong),
}
}
@@ -227,12 +242,9 @@ fn op_encoding_decode_single(
#[string] label: String,
fatal: bool,
ignore_bom: bool,
-) -> Result<U16String, AnyError> {
- let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| {
- range_error(format!(
- "The encoding label provided ('{label}') is invalid."
- ))
- })?;
+) -> Result<U16String, WebError> {
+ let encoding = Encoding::for_label(label.as_bytes())
+ .ok_or(WebError::InvalidEncodingLabel(label))?;
let mut decoder = if ignore_bom {
encoding.new_decoder_without_bom_handling()
@@ -242,7 +254,7 @@ fn op_encoding_decode_single(
let max_buffer_length = decoder
.max_utf16_buffer_length(data.len())
- .ok_or_else(|| range_error("Value too large to decode."))?;
+ .ok_or(WebError::ValueTooLarge)?;
let mut output = vec![0; max_buffer_length];
@@ -254,12 +266,8 @@ fn op_encoding_decode_single(
output.truncate(written);
Ok(output.into())
}
- DecoderResult::OutputFull => {
- Err(range_error("Provided buffer too small."))
- }
- DecoderResult::Malformed(_, _) => {
- Err(type_error("The encoded data is not valid."))
- }
+ DecoderResult::OutputFull => Err(WebError::BufferTooSmall),
+ DecoderResult::Malformed(_, _) => Err(WebError::DataInvalid),
}
} else {
let (result, _, written, _) =
@@ -269,7 +277,7 @@ fn op_encoding_decode_single(
output.truncate(written);
Ok(output.into())
}
- CoderResult::OutputFull => Err(range_error("Provided buffer too small.")),
+ CoderResult::OutputFull => Err(WebError::BufferTooSmall),
}
}
}
@@ -280,12 +288,9 @@ fn op_encoding_new_decoder(
#[string] label: &str,
fatal: bool,
ignore_bom: bool,
-) -> Result<TextDecoderResource, AnyError> {
- let encoding = Encoding::for_label(label.as_bytes()).ok_or_else(|| {
- range_error(format!(
- "The encoding label provided ('{label}') is invalid."
- ))
- })?;
+) -> Result<TextDecoderResource, WebError> {
+ let encoding = Encoding::for_label(label.as_bytes())
+ .ok_or_else(|| WebError::InvalidEncodingLabel(label.to_string()))?;
let decoder = if ignore_bom {
encoding.new_decoder_without_bom_handling()
@@ -305,13 +310,13 @@ fn op_encoding_decode(
#[anybuffer] data: &[u8],
#[cppgc] resource: &TextDecoderResource,
stream: bool,
-) -> Result<U16String, AnyError> {
+) -> Result<U16String, WebError> {
let mut decoder = resource.decoder.borrow_mut();
let fatal = resource.fatal;
let max_buffer_length = decoder
.max_utf16_buffer_length(data.len())
- .ok_or_else(|| range_error("Value too large to decode."))?;
+ .ok_or(WebError::ValueTooLarge)?;
let mut output = vec![0; max_buffer_length];
@@ -323,12 +328,8 @@ fn op_encoding_decode(
output.truncate(written);
Ok(output.into())
}
- DecoderResult::OutputFull => {
- Err(range_error("Provided buffer too small."))
- }
- DecoderResult::Malformed(_, _) => {
- Err(type_error("The encoded data is not valid."))
- }
+ DecoderResult::OutputFull => Err(WebError::BufferTooSmall),
+ DecoderResult::Malformed(_, _) => Err(WebError::DataInvalid),
}
} else {
let (result, _, written, _) =
@@ -338,7 +339,7 @@ fn op_encoding_decode(
output.truncate(written);
Ok(output.into())
}
- CoderResult::OutputFull => Err(range_error("Provided buffer too small.")),
+ CoderResult::OutputFull => Err(WebError::BufferTooSmall),
}
}
}
@@ -356,7 +357,7 @@ fn op_encoding_encode_into(
input: v8::Local<v8::Value>,
#[buffer] buffer: &mut [u8],
#[buffer] out_buf: &mut [u32],
-) -> Result<(), AnyError> {
+) -> Result<(), WebError> {
let s = v8::Local::<v8::String>::try_from(input)?;
let mut nchars = 0;
@@ -414,53 +415,4 @@ pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_web.d.ts")
}
-#[derive(Debug)]
-pub struct DomExceptionQuotaExceededError {
- pub msg: String,
-}
-
-impl DomExceptionQuotaExceededError {
- pub fn new(msg: &str) -> Self {
- DomExceptionQuotaExceededError {
- msg: msg.to_string(),
- }
- }
-}
-
-#[derive(Debug)]
-pub struct DomExceptionInvalidCharacterError {
- pub msg: String,
-}
-
-impl DomExceptionInvalidCharacterError {
- pub fn new(msg: &str) -> Self {
- DomExceptionInvalidCharacterError {
- msg: msg.to_string(),
- }
- }
-}
-
-impl fmt::Display for DomExceptionQuotaExceededError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.pad(&self.msg)
- }
-}
-impl fmt::Display for DomExceptionInvalidCharacterError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- f.pad(&self.msg)
- }
-}
-
-impl std::error::Error for DomExceptionQuotaExceededError {}
-
-impl std::error::Error for DomExceptionInvalidCharacterError {}
-
-pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
- e.downcast_ref::<DomExceptionQuotaExceededError>()
- .map(|_| "DOMExceptionQuotaExceededError")
- .or_else(|| {
- e.downcast_ref::<DomExceptionInvalidCharacterError>()
- .map(|_| "DOMExceptionInvalidCharacterError")
- })
-}
pub struct Location(pub Url);