Repository: denoland/deno
File: cli/rt/binary.rs (Rust)

Code before:
      // check in FastString::from_static
      Self::String(s) => ModuleSourceCode::String(FastString::from_static(s)),
      Self::Bytes(b) => into_bytes(b),
    }
  }
}

#[derive(Debug, Error, JsError)]
#[class(generic)]
#[error("Too many redirects resolving: {0}")]
pub struct TooManyRedirectsError(Url);

pub struct RemoteModulesStore {
  specifiers: SpecifierStore,
  redirects: SpecifierDataStore<SpecifierId>,
  remote_modules: SpecifierDataStore<RemoteModuleEntry<'static>>,
}

impl RemoteModulesStore {
  fn new(
    specifiers: SpecifierStore,
    redirects: SpecifierDataStore<SpecifierId>,
    remote_modules: SpecifierDataStore<RemoteModuleEntry<'static>>,
  ) -> Self {
    Self {
      specifiers,
      redirects,
      remote_modules,
    }
  }

  pub fn resolve_specifier<'a>(
    &'a self,
    specifier: &'a Url,
  ) -> Result<Option<&'a Url>, TooManyRedirectsError> {
    let Some(mut current) = self.specifiers.get_id(specifier) else {
      return Ok(None);
    };
    let mut count = 0;
    loop {
      if count > 10 {
        return Err(TooManyRedirectsError(specifier.clone()));
      }
      match self.redirects.get(current) {
        Some(to) => {
          current = *to;
          count += 1;
        }
        None => {
          if count == 0 {
            return Ok(Some(specifier));

Review comment (performance):
Not related to this PR, but I'm not a fan that `ModuleCodeBytes` implements `From`... I have a suspicion that in some cases we're not using the most efficient way of passing data to `deno_core` which incurs some clones. Anyway, not important for this PR per se.

Code after:
#[derive(Debug)]
pub enum DenoCompileModuleSource {
  String(&'static str),
  Bytes(Cow<'static, [u8]>),
}

impl DenoCompileModuleSource {
  pub fn into_for_v8(self) -> ModuleSourceCode {
    match self {
      // todo(https://github.com/denoland/deno_core/pull/943): store whether
      // the string is ascii or not ahead of time so we can avoid the is_ascii()
      // check in FastString::from_static
      Self::String(s) => ModuleSourceCode::String(FastString::from_static(s)),
      Self::Bytes(b) => ModuleSourceCode::Bytes(module_source_into_bytes(b)),
    }
  }

  pub fn into_bytes_for_v8(self) -> ModuleCodeBytes {
    match self {
      DenoCompileModuleSource::String(text) => text.as_bytes().into(),
      DenoCompileModuleSource::Bytes(b) => module_source_into_bytes(b),
    }
  }
}

fn module_source_into_bytes(data: Cow<'static, [u8]>) -> ModuleCodeBytes {
  match data {
    Cow::Borrowed(d) => d.into(),
    Cow::Owned(d) => d.into_boxed_slice().into(),
  }
}

#[derive(Debug, Error, JsError)]
#[class(generic)]
#[error("Too many redirects resolving: {0}")]
pub struct TooManyRedirectsError(Url);

pub struct RemoteModulesStore {
  specifiers: SpecifierStore,
  redirects: SpecifierDataStore<SpecifierId>,
  remote_modules: SpecifierDataStore<RemoteModuleEntry<'static>>,
}

impl RemoteModulesStore {
  fn new(
    specifiers: SpecifierStore,
    redirects: SpecifierDataStore<SpecifierId>,
    remote_modules: SpecifierDataStore<RemoteModuleEntry<'static>>,
  ) -> Self {
    Self {
      specifiers,