Repository: denoland/deno
File: ext/node/ops/sqlite/database.rs (Rust)

Code before:
  ) -> Result<Self, Self::Error> {
    let maybe_path = if value.is_string() {
      let path_str = value.to_rust_string_lossy(scope);
      Some(path_str)
    } else if value.is_uint8_array() {
      let buffer_view = value.cast::<v8::Uint8Array>();
      let buffer_view_data = buffer_view.data() as *mut u8;
      // SAFETY: `buffer_view_data` is valid as long as `buffer_view` is alive,
      let path_slice = unsafe {
        std::slice::from_raw_parts(buffer_view_data, buffer_view.byte_length())
      };

      let path_str = str::from_utf8(path_slice).unwrap().to_string();
      Some(path_str)
    } else if value.is_object() {
      v8_static_strings! {
        HREF = "href",
        URL = "URL"
      }

      let global = scope.get_current_context().global(scope);
      let url_str = URL.v8_string(scope).unwrap();
      let js_url: v8::Local<v8::Object> = {
        global
          .get(scope, url_str.into())
          .unwrap()
          .try_into()
          .unwrap()
      };

      let path_url = v8::Local::<v8::Object>::try_from(value).unwrap();
      if !path_url.instance_of(scope, js_url).unwrap() {
        None
      } else {
        let href_str = HREF.v8_string(scope).unwrap();
        let path_href = path_url
          .get(scope, href_str.into())
          .unwrap()
          .to_rust_string_lossy(scope);

        if !path_href.starts_with("file:") {
          return Err(validators::Error::InvalidUrlScheme);
        }
        Some(path_href)
      }
    } else {
      None
    };

    let maybe_path = maybe_path.filter(|path| {
      if path.find('\0').is_some() {

Review comment (suggestion):
I don't get it - are you trying to call `URL` object from JS here? Why not do it on JS side and only forward relevant string? This functionality will break if someone does `delete globalThis.URL` (yes, people really do that)


Code after:
  timeout: u64,
}

impl<'a> FromV8<'a> for DatabaseSyncOptions {
  type Error = validators::Error;

  fn from_v8(
    scope: &mut v8::PinScope<'a, '_>,
    value: v8::Local<'a, v8::Value>,
  ) -> Result<Self, Self::Error> {
    use validators::Error;

    if value.is_undefined() {
      return Ok(Self::default());
    }

    let Ok(obj) = v8::Local::<v8::Object>::try_from(value) else {
      return Err(Error::InvalidArgType(
        "The \"options\" argument must be an object.",
      ));
    };

    let mut options = Self::default();

    v8_static_strings! {
      OPEN_STRING = "open",
      ENABLE_FOREIGN_KEY_CONSTRAINTS_STRING = "enableForeignKeyConstraints",
      READ_ONLY_STRING = "readOnly",
      ALLOW_EXTENSION_STRING = "allowExtension",
      ENABLE_DOUBLE_QUOTED_STRING_LITERALS_STRING = "enableDoubleQuotedStringLiterals",
      TIMEOUT_STRING = "timeout",
    }

    let open_string = OPEN_STRING.v8_string(scope).unwrap();
    if let Some(open) = obj.get(scope, open_string.into())
      && !open.is_undefined()
    {
      options.open = v8::Local::<v8::Boolean>::try_from(open)
        .map_err(|_| {
          Error::InvalidArgType(
            "The \"options.open\" argument must be a boolean.",
          )
        })?
        .is_true();
    }

    let read_only_string = READ_ONLY_STRING.v8_string(scope).unwrap();
    if let Some(read_only) = obj.get(scope, read_only_string.into())
      && !read_only.is_undefined()
    {
      options.read_only = v8::Local::<v8::Boolean>::try_from(read_only)