0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-15 01:57:09 -05:00

Merge branch 'main' into fix/webgpu-adapter-info

This commit is contained in:
Kenta Moriuchi 2024-12-03 11:53:09 +09:00 committed by GitHub
commit d4909eb084
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 1725 additions and 508 deletions

47
Cargo.lock generated
View file

@ -1220,6 +1220,7 @@ dependencies = [
"deno_lint", "deno_lint",
"deno_lockfile", "deno_lockfile",
"deno_npm", "deno_npm",
"deno_npm_cache",
"deno_package_json", "deno_package_json",
"deno_path_util", "deno_path_util",
"deno_resolver", "deno_resolver",
@ -1803,9 +1804,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_lint" name = "deno_lint"
version = "0.68.1" version = "0.68.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce2a040657166e39c7d59ad34230f0cc829f8ea8b7b2377038cc012ec1a1ef16" checksum = "ce713d564f76efd90535061113210bdc6b942ed6327b33eb1d5f76a5daf8e7a5"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"deno_ast", "deno_ast",
@ -1981,9 +1982,9 @@ dependencies = [
[[package]] [[package]]
name = "deno_npm" name = "deno_npm"
version = "0.25.5" version = "0.26.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89ded7af9db5d9f2986a739d1b5fbe1c57f498e4f996ae4114728e7c6dad213f" checksum = "f2f125a5dba7839c46394a0a9c835da9fe60f5f412587ab4956a76492a1cc6a8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1998,6 +1999,35 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "deno_npm_cache"
version = "0.0.1"
dependencies = [
"anyhow",
"async-trait",
"base64 0.21.7",
"boxed_error",
"deno_cache_dir",
"deno_core",
"deno_npm",
"deno_semver",
"deno_unsync",
"faster-hex",
"flate2",
"futures",
"http 1.1.0",
"log",
"parking_lot",
"percent-encoding",
"rand",
"ring",
"serde_json",
"tar",
"tempfile",
"thiserror 1.0.64",
"url",
]
[[package]] [[package]]
name = "deno_ops" name = "deno_ops"
version = "0.199.0" version = "0.199.0"
@ -2260,10 +2290,11 @@ dependencies = [
[[package]] [[package]]
name = "deno_unsync" name = "deno_unsync"
version = "0.4.1" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f36b4ef61a04ce201b925a5dffa90f88437d37fee4836c758470dd15ba7f05e" checksum = "d774fd83f26b24f0805a6ab8b26834a0d06ceac0db517b769b1e4633c96a2057"
dependencies = [ dependencies = [
"futures",
"parking_lot", "parking_lot",
"tokio", "tokio",
] ]
@ -4682,9 +4713,9 @@ dependencies = [
[[package]] [[package]]
name = "markup_fmt" name = "markup_fmt"
version = "0.16.0" version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f303c36143671ac6c54112eb5aa95649b169dae783fdb6ead2c0e88b408c425c" checksum = "fa7605bb4ad755a9ab5c96f2ce3bfd4eb8acd559b842c041fc8a5f84d63aed3a"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"css_dataset", "css_dataset",

View file

@ -30,6 +30,7 @@ members = [
"ext/webstorage", "ext/webstorage",
"resolvers/deno", "resolvers/deno",
"resolvers/node", "resolvers/node",
"resolvers/npm_cache",
"runtime", "runtime",
"runtime/permissions", "runtime/permissions",
"tests", "tests",
@ -53,7 +54,7 @@ deno_bench_util = { version = "0.174.0", path = "./bench_util" }
deno_config = { version = "=0.39.3", features = ["workspace", "sync"] } deno_config = { version = "=0.39.3", features = ["workspace", "sync"] }
deno_lockfile = "=0.23.2" deno_lockfile = "=0.23.2"
deno_media_type = { version = "0.2.0", features = ["module_specifier"] } deno_media_type = { version = "0.2.0", features = ["module_specifier"] }
deno_npm = "=0.25.5" deno_npm = "=0.26.0"
deno_path_util = "=0.2.1" deno_path_util = "=0.2.1"
deno_permissions = { version = "0.40.0", path = "./runtime/permissions" } deno_permissions = { version = "0.40.0", path = "./runtime/permissions" }
deno_runtime = { version = "0.189.0", path = "./runtime" } deno_runtime = { version = "0.189.0", path = "./runtime" }
@ -93,6 +94,7 @@ deno_websocket = { version = "0.185.0", path = "./ext/websocket" }
deno_webstorage = { version = "0.175.0", path = "./ext/webstorage" } deno_webstorage = { version = "0.175.0", path = "./ext/webstorage" }
# resolvers # resolvers
deno_npm_cache = { version = "0.0.1", path = "./resolvers/npm_cache" }
deno_resolver = { version = "0.12.0", path = "./resolvers/deno" } deno_resolver = { version = "0.12.0", path = "./resolvers/deno" }
node_resolver = { version = "0.19.0", path = "./resolvers/node" } node_resolver = { version = "0.19.0", path = "./resolvers/node" }
@ -117,6 +119,7 @@ data-encoding = "2.3.3"
data-url = "=0.3.0" data-url = "=0.3.0"
deno_cache_dir = "=0.14.0" deno_cache_dir = "=0.14.0"
deno_package_json = { version = "0.2.1", default-features = false } deno_package_json = { version = "0.2.1", default-features = false }
deno_unsync = "0.4.2"
dlopen2 = "0.6.1" dlopen2 = "0.6.1"
ecb = "=0.1.2" ecb = "=0.1.2"
elliptic-curve = { version = "0.13.4", features = ["alloc", "arithmetic", "ecdh", "std", "pem", "jwk"] } elliptic-curve = { version = "0.13.4", features = ["alloc", "arithmetic", "ecdh", "std", "pem", "jwk"] }

View file

@ -74,9 +74,10 @@ deno_config.workspace = true
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] } deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = { version = "=0.161.2", features = ["rust", "comrak"] } deno_doc = { version = "=0.161.2", features = ["rust", "comrak"] }
deno_graph = { version = "=0.86.3" } deno_graph = { version = "=0.86.3" }
deno_lint = { version = "=0.68.1", features = ["docs"] } deno_lint = { version = "=0.68.2", features = ["docs"] }
deno_lockfile.workspace = true deno_lockfile.workspace = true
deno_npm.workspace = true deno_npm.workspace = true
deno_npm_cache.workspace = true
deno_package_json.workspace = true deno_package_json.workspace = true
deno_path_util.workspace = true deno_path_util.workspace = true
deno_resolver.workspace = true deno_resolver.workspace = true
@ -130,7 +131,7 @@ libz-sys.workspace = true
log = { workspace = true, features = ["serde"] } log = { workspace = true, features = ["serde"] }
lsp-types.workspace = true lsp-types.workspace = true
malva = "=0.11.0" malva = "=0.11.0"
markup_fmt = "=0.16.0" markup_fmt = "=0.18.0"
memmem.workspace = true memmem.workspace = true
monch.workspace = true monch.workspace = true
notify.workspace = true notify.workspace = true

View file

@ -27,6 +27,7 @@ use deno_npm::npm_rc::NpmRc;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot; use deno_npm::resolution::ValidSerializedNpmResolutionSnapshot;
use deno_npm::NpmSystemInfo; use deno_npm::NpmSystemInfo;
use deno_npm_cache::NpmCacheSetting;
use deno_path_util::normalize_path; use deno_path_util::normalize_path;
use deno_semver::npm::NpmPackageReqReference; use deno_semver::npm::NpmPackageReqReference;
use deno_telemetry::OtelConfig; use deno_telemetry::OtelConfig;
@ -238,20 +239,25 @@ pub enum CacheSetting {
} }
impl CacheSetting { impl CacheSetting {
pub fn should_use_for_npm_package(&self, package_name: &str) -> bool { pub fn as_npm_cache_setting(&self) -> NpmCacheSetting {
match self { match self {
CacheSetting::ReloadAll => false, CacheSetting::Only => NpmCacheSetting::Only,
CacheSetting::ReloadSome(list) => { CacheSetting::ReloadAll => NpmCacheSetting::ReloadAll,
if list.iter().any(|i| i == "npm:") { CacheSetting::ReloadSome(values) => {
return false; if values.iter().any(|v| v == "npm:") {
NpmCacheSetting::ReloadAll
} else {
NpmCacheSetting::ReloadSome {
npm_package_names: values
.iter()
.filter_map(|v| v.strip_prefix("npm:"))
.map(|n| n.to_string())
.collect(),
}
} }
let specifier = format!("npm:{package_name}");
if list.contains(&specifier) {
return false;
}
true
} }
_ => true, CacheSetting::RespectHeaders => unreachable!(), // not supported
CacheSetting::Use => NpmCacheSetting::Use,
} }
} }
} }

View file

@ -5,8 +5,6 @@ use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use cache::RegistryInfoDownloader;
use cache::TarballCache;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_cache_dir::npm::NpmCacheDir; use deno_cache_dir::npm::NpmCacheDir;
use deno_core::anyhow::Context; use deno_core::anyhow::Context;
@ -42,22 +40,23 @@ use crate::args::NpmProcessState;
use crate::args::NpmProcessStateKind; use crate::args::NpmProcessStateKind;
use crate::args::PackageJsonDepValueParseWithLocationError; use crate::args::PackageJsonDepValueParseWithLocationError;
use crate::cache::FastInsecureHasher; use crate::cache::FastInsecureHasher;
use crate::http_util::HttpClientProvider;
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs; use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBar;
use crate::util::sync::AtomicFlag; use crate::util::sync::AtomicFlag;
use self::cache::NpmCache;
use self::registry::CliNpmRegistryApi; use self::registry::CliNpmRegistryApi;
use self::resolution::NpmResolution; use self::resolution::NpmResolution;
use self::resolvers::create_npm_fs_resolver; use self::resolvers::create_npm_fs_resolver;
use self::resolvers::NpmPackageFsResolver; use self::resolvers::NpmPackageFsResolver;
use super::CliNpmCache;
use super::CliNpmCacheEnv;
use super::CliNpmRegistryInfoProvider;
use super::CliNpmResolver; use super::CliNpmResolver;
use super::CliNpmTarballCache;
use super::InnerCliNpmResolverRef; use super::InnerCliNpmResolverRef;
use super::ResolvePkgFolderFromDenoReqError; use super::ResolvePkgFolderFromDenoReqError;
pub mod cache;
mod registry; mod registry;
mod resolution; mod resolution;
mod resolvers; mod resolvers;
@ -85,8 +84,9 @@ pub struct CliManagedNpmResolverCreateOptions {
pub async fn create_managed_npm_resolver_for_lsp( pub async fn create_managed_npm_resolver_for_lsp(
options: CliManagedNpmResolverCreateOptions, options: CliManagedNpmResolverCreateOptions,
) -> Arc<dyn CliNpmResolver> { ) -> Arc<dyn CliNpmResolver> {
let npm_cache = create_cache(&options); let cache_env = create_cache_env(&options);
let npm_api = create_api(&options, npm_cache.clone()); let npm_cache = create_cache(cache_env.clone(), &options);
let npm_api = create_api(npm_cache.clone(), cache_env.clone(), &options);
// spawn due to the lsp's `Send` requirement // spawn due to the lsp's `Send` requirement
deno_core::unsync::spawn(async move { deno_core::unsync::spawn(async move {
let snapshot = match resolve_snapshot(&npm_api, options.snapshot).await { let snapshot = match resolve_snapshot(&npm_api, options.snapshot).await {
@ -97,8 +97,8 @@ pub async fn create_managed_npm_resolver_for_lsp(
} }
}; };
create_inner( create_inner(
cache_env,
options.fs, options.fs,
options.http_client_provider,
options.maybe_lockfile, options.maybe_lockfile,
npm_api, npm_api,
npm_cache, npm_cache,
@ -118,12 +118,13 @@ pub async fn create_managed_npm_resolver_for_lsp(
pub async fn create_managed_npm_resolver( pub async fn create_managed_npm_resolver(
options: CliManagedNpmResolverCreateOptions, options: CliManagedNpmResolverCreateOptions,
) -> Result<Arc<dyn CliNpmResolver>, AnyError> { ) -> Result<Arc<dyn CliNpmResolver>, AnyError> {
let npm_cache = create_cache(&options); let npm_cache_env = create_cache_env(&options);
let npm_api = create_api(&options, npm_cache.clone()); let npm_cache = create_cache(npm_cache_env.clone(), &options);
let npm_api = create_api(npm_cache.clone(), npm_cache_env.clone(), &options);
let snapshot = resolve_snapshot(&npm_api, options.snapshot).await?; let snapshot = resolve_snapshot(&npm_api, options.snapshot).await?;
Ok(create_inner( Ok(create_inner(
npm_cache_env,
options.fs, options.fs,
options.http_client_provider,
options.maybe_lockfile, options.maybe_lockfile,
npm_api, npm_api,
npm_cache, npm_cache,
@ -139,11 +140,11 @@ pub async fn create_managed_npm_resolver(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
fn create_inner( fn create_inner(
env: Arc<CliNpmCacheEnv>,
fs: Arc<dyn deno_runtime::deno_fs::FileSystem>, fs: Arc<dyn deno_runtime::deno_fs::FileSystem>,
http_client_provider: Arc<HttpClientProvider>,
maybe_lockfile: Option<Arc<CliLockfile>>, maybe_lockfile: Option<Arc<CliLockfile>>,
npm_api: Arc<CliNpmRegistryApi>, npm_api: Arc<CliNpmRegistryApi>,
npm_cache: Arc<NpmCache>, npm_cache: Arc<CliNpmCache>,
npm_rc: Arc<ResolvedNpmRc>, npm_rc: Arc<ResolvedNpmRc>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
text_only_progress_bar: crate::util::progress_bar::ProgressBar, text_only_progress_bar: crate::util::progress_bar::ProgressBar,
@ -157,12 +158,10 @@ fn create_inner(
snapshot, snapshot,
maybe_lockfile.clone(), maybe_lockfile.clone(),
)); ));
let tarball_cache = Arc::new(TarballCache::new( let tarball_cache = Arc::new(CliNpmTarballCache::new(
npm_cache.clone(), npm_cache.clone(),
fs.clone(), env,
http_client_provider.clone(),
npm_rc.clone(), npm_rc.clone(),
text_only_progress_bar.clone(),
)); ));
let fs_resolver = create_npm_fs_resolver( let fs_resolver = create_npm_fs_resolver(
fs.clone(), fs.clone(),
@ -190,25 +189,39 @@ fn create_inner(
)) ))
} }
fn create_cache(options: &CliManagedNpmResolverCreateOptions) -> Arc<NpmCache> { fn create_cache_env(
Arc::new(NpmCache::new( options: &CliManagedNpmResolverCreateOptions,
) -> Arc<CliNpmCacheEnv> {
Arc::new(CliNpmCacheEnv::new(
options.fs.clone(),
options.http_client_provider.clone(),
options.text_only_progress_bar.clone(),
))
}
fn create_cache(
env: Arc<CliNpmCacheEnv>,
options: &CliManagedNpmResolverCreateOptions,
) -> Arc<CliNpmCache> {
Arc::new(CliNpmCache::new(
options.npm_cache_dir.clone(), options.npm_cache_dir.clone(),
options.cache_setting.clone(), options.cache_setting.as_npm_cache_setting(),
env,
options.npmrc.clone(), options.npmrc.clone(),
)) ))
} }
fn create_api( fn create_api(
cache: Arc<CliNpmCache>,
env: Arc<CliNpmCacheEnv>,
options: &CliManagedNpmResolverCreateOptions, options: &CliManagedNpmResolverCreateOptions,
npm_cache: Arc<NpmCache>,
) -> Arc<CliNpmRegistryApi> { ) -> Arc<CliNpmRegistryApi> {
Arc::new(CliNpmRegistryApi::new( Arc::new(CliNpmRegistryApi::new(
npm_cache.clone(), cache.clone(),
Arc::new(RegistryInfoDownloader::new( Arc::new(CliNpmRegistryInfoProvider::new(
npm_cache, cache,
options.http_client_provider.clone(), env,
options.npmrc.clone(), options.npmrc.clone(),
options.text_only_progress_bar.clone(),
)), )),
)) ))
} }
@ -292,10 +305,10 @@ pub struct ManagedCliNpmResolver {
fs_resolver: Arc<dyn NpmPackageFsResolver>, fs_resolver: Arc<dyn NpmPackageFsResolver>,
maybe_lockfile: Option<Arc<CliLockfile>>, maybe_lockfile: Option<Arc<CliLockfile>>,
npm_api: Arc<CliNpmRegistryApi>, npm_api: Arc<CliNpmRegistryApi>,
npm_cache: Arc<NpmCache>, npm_cache: Arc<CliNpmCache>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
text_only_progress_bar: ProgressBar, text_only_progress_bar: ProgressBar,
npm_system_info: NpmSystemInfo, npm_system_info: NpmSystemInfo,
top_level_install_flag: AtomicFlag, top_level_install_flag: AtomicFlag,
@ -317,10 +330,10 @@ impl ManagedCliNpmResolver {
fs_resolver: Arc<dyn NpmPackageFsResolver>, fs_resolver: Arc<dyn NpmPackageFsResolver>,
maybe_lockfile: Option<Arc<CliLockfile>>, maybe_lockfile: Option<Arc<CliLockfile>>,
npm_api: Arc<CliNpmRegistryApi>, npm_api: Arc<CliNpmRegistryApi>,
npm_cache: Arc<NpmCache>, npm_cache: Arc<CliNpmCache>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
text_only_progress_bar: ProgressBar, text_only_progress_bar: ProgressBar,
npm_system_info: NpmSystemInfo, npm_system_info: NpmSystemInfo,
lifecycle_scripts: LifecycleScriptsConfig, lifecycle_scripts: LifecycleScriptsConfig,

View file

@ -14,27 +14,28 @@ use deno_core::parking_lot::Mutex;
use deno_npm::registry::NpmPackageInfo; use deno_npm::registry::NpmPackageInfo;
use deno_npm::registry::NpmRegistryApi; use deno_npm::registry::NpmRegistryApi;
use deno_npm::registry::NpmRegistryPackageInfoLoadError; use deno_npm::registry::NpmRegistryPackageInfoLoadError;
use deno_npm_cache::NpmCacheSetting;
use crate::args::CacheSetting; use crate::npm::CliNpmCache;
use crate::npm::CliNpmRegistryInfoProvider;
use crate::util::sync::AtomicFlag; use crate::util::sync::AtomicFlag;
use super::cache::NpmCache; // todo(#27198): Remove this and move functionality down into
use super::cache::RegistryInfoDownloader; // RegistryInfoProvider, which already does most of this.
#[derive(Debug)] #[derive(Debug)]
pub struct CliNpmRegistryApi(Option<Arc<CliNpmRegistryApiInner>>); pub struct CliNpmRegistryApi(Option<Arc<CliNpmRegistryApiInner>>);
impl CliNpmRegistryApi { impl CliNpmRegistryApi {
pub fn new( pub fn new(
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
registry_info_downloader: Arc<RegistryInfoDownloader>, registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
) -> Self { ) -> Self {
Self(Some(Arc::new(CliNpmRegistryApiInner { Self(Some(Arc::new(CliNpmRegistryApiInner {
cache, cache,
force_reload_flag: Default::default(), force_reload_flag: Default::default(),
mem_cache: Default::default(), mem_cache: Default::default(),
previously_reloaded_packages: Default::default(), previously_reloaded_packages: Default::default(),
registry_info_downloader, registry_info_provider,
}))) })))
} }
@ -83,11 +84,11 @@ enum CacheItem {
#[derive(Debug)] #[derive(Debug)]
struct CliNpmRegistryApiInner { struct CliNpmRegistryApiInner {
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
force_reload_flag: AtomicFlag, force_reload_flag: AtomicFlag,
mem_cache: Mutex<HashMap<String, CacheItem>>, mem_cache: Mutex<HashMap<String, CacheItem>>,
previously_reloaded_packages: Mutex<HashSet<String>>, previously_reloaded_packages: Mutex<HashSet<String>>,
registry_info_downloader: Arc<RegistryInfoDownloader>, registry_info_provider: Arc<CliNpmRegistryInfoProvider>,
} }
impl CliNpmRegistryApiInner { impl CliNpmRegistryApiInner {
@ -118,7 +119,7 @@ impl CliNpmRegistryApiInner {
return Ok(result); return Ok(result);
} }
} }
api.registry_info_downloader api.registry_info_provider
.load_package_info(&name) .load_package_info(&name)
.await .await
.map_err(Arc::new) .map_err(Arc::new)
@ -159,7 +160,7 @@ impl CliNpmRegistryApiInner {
// is disabled or if we're already reloading // is disabled or if we're already reloading
if matches!( if matches!(
self.cache.cache_setting(), self.cache.cache_setting(),
CacheSetting::Only | CacheSetting::ReloadAll NpmCacheSetting::Only | NpmCacheSetting::ReloadAll
) { ) {
return false; return false;
} }

View file

@ -8,11 +8,10 @@ use deno_core::error::AnyError;
use deno_lockfile::NpmPackageDependencyLockfileInfo; use deno_lockfile::NpmPackageDependencyLockfileInfo;
use deno_lockfile::NpmPackageLockfileInfo; use deno_lockfile::NpmPackageLockfileInfo;
use deno_npm::registry::NpmRegistryApi; use deno_npm::registry::NpmRegistryApi;
use deno_npm::resolution::AddPkgReqsOptions;
use deno_npm::resolution::NpmPackagesPartitioned; use deno_npm::resolution::NpmPackagesPartitioned;
use deno_npm::resolution::NpmResolutionError; use deno_npm::resolution::NpmResolutionError;
use deno_npm::resolution::NpmResolutionSnapshot; use deno_npm::resolution::NpmResolutionSnapshot;
use deno_npm::resolution::NpmResolutionSnapshotPendingResolver;
use deno_npm::resolution::NpmResolutionSnapshotPendingResolverOptions;
use deno_npm::resolution::PackageCacheFolderIdNotFoundError; use deno_npm::resolution::PackageCacheFolderIdNotFoundError;
use deno_npm::resolution::PackageNotFoundFromReferrerError; use deno_npm::resolution::PackageNotFoundFromReferrerError;
use deno_npm::resolution::PackageNvNotFoundError; use deno_npm::resolution::PackageNvNotFoundError;
@ -283,8 +282,9 @@ async fn add_package_reqs_to_snapshot(
/* this string is used in tests */ /* this string is used in tests */
"Running npm resolution." "Running npm resolution."
); );
let pending_resolver = get_npm_pending_resolver(api); let result = snapshot
let result = pending_resolver.add_pkg_reqs(snapshot, package_reqs).await; .add_pkg_reqs(api, get_add_pkg_reqs_options(package_reqs))
.await;
api.clear_memory_cache(); api.clear_memory_cache();
let result = match &result.dep_graph_result { let result = match &result.dep_graph_result {
Err(NpmResolutionError::Resolution(err)) if api.mark_force_reload() => { Err(NpmResolutionError::Resolution(err)) if api.mark_force_reload() => {
@ -293,7 +293,9 @@ async fn add_package_reqs_to_snapshot(
// try again // try again
let snapshot = get_new_snapshot(); let snapshot = get_new_snapshot();
let result = pending_resolver.add_pkg_reqs(snapshot, package_reqs).await; let result = snapshot
.add_pkg_reqs(api, get_add_pkg_reqs_options(package_reqs))
.await;
api.clear_memory_cache(); api.clear_memory_cache();
result result
} }
@ -309,19 +311,15 @@ async fn add_package_reqs_to_snapshot(
result result
} }
fn get_npm_pending_resolver( fn get_add_pkg_reqs_options(package_reqs: &[PackageReq]) -> AddPkgReqsOptions {
api: &CliNpmRegistryApi, AddPkgReqsOptions {
) -> NpmResolutionSnapshotPendingResolver<CliNpmRegistryApi> { package_reqs,
NpmResolutionSnapshotPendingResolver::new( // WARNING: When bumping this version, check if anything needs to be
NpmResolutionSnapshotPendingResolverOptions { // updated in the `setNodeOnlyGlobalNames` call in 99_main_compiler.js
api, types_node_version_req: Some(
// WARNING: When bumping this version, check if anything needs to be VersionReq::parse_from_npm("22.0.0 - 22.5.4").unwrap(),
// updated in the `setNodeOnlyGlobalNames` call in 99_main_compiler.js ),
types_node_version_req: Some( }
VersionReq::parse_from_npm("22.0.0 - 22.5.4").unwrap(),
),
},
)
} }
fn populate_lockfile_from_snapshot( fn populate_lockfile_from_snapshot(

View file

@ -24,7 +24,7 @@ use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_node::NodePermissions; use deno_runtime::deno_node::NodePermissions;
use node_resolver::errors::PackageFolderResolveError; use node_resolver::errors::PackageFolderResolveError;
use crate::npm::managed::cache::TarballCache; use crate::npm::CliNpmTarballCache;
/// Part of the resolution that interacts with the file system. /// Part of the resolution that interacts with the file system.
#[async_trait(?Send)] #[async_trait(?Send)]
@ -140,7 +140,7 @@ impl RegistryReadPermissionChecker {
/// Caches all the packages in parallel. /// Caches all the packages in parallel.
pub async fn cache_packages( pub async fn cache_packages(
packages: &[NpmResolutionPackage], packages: &[NpmResolutionPackage],
tarball_cache: &Arc<TarballCache>, tarball_cache: &Arc<CliNpmTarballCache>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let mut futures_unordered = futures::stream::FuturesUnordered::new(); let mut futures_unordered = futures::stream::FuturesUnordered::new();
for package in packages { for package in packages {

View file

@ -8,6 +8,8 @@ use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use crate::colors; use crate::colors;
use crate::npm::CliNpmCache;
use crate::npm::CliNpmTarballCache;
use async_trait::async_trait; use async_trait::async_trait;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_core::error::AnyError; use deno_core::error::AnyError;
@ -24,8 +26,6 @@ use node_resolver::errors::ReferrerNotFoundError;
use crate::args::LifecycleScriptsConfig; use crate::args::LifecycleScriptsConfig;
use crate::cache::FastInsecureHasher; use crate::cache::FastInsecureHasher;
use super::super::cache::NpmCache;
use super::super::cache::TarballCache;
use super::super::resolution::NpmResolution; use super::super::resolution::NpmResolution;
use super::common::cache_packages; use super::common::cache_packages;
use super::common::lifecycle_scripts::LifecycleScriptsStrategy; use super::common::lifecycle_scripts::LifecycleScriptsStrategy;
@ -35,8 +35,8 @@ use super::common::RegistryReadPermissionChecker;
/// Resolves packages from the global npm cache. /// Resolves packages from the global npm cache.
#[derive(Debug)] #[derive(Debug)]
pub struct GlobalNpmPackageResolver { pub struct GlobalNpmPackageResolver {
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
system_info: NpmSystemInfo, system_info: NpmSystemInfo,
registry_read_permission_checker: RegistryReadPermissionChecker, registry_read_permission_checker: RegistryReadPermissionChecker,
@ -45,9 +45,9 @@ pub struct GlobalNpmPackageResolver {
impl GlobalNpmPackageResolver { impl GlobalNpmPackageResolver {
pub fn new( pub fn new(
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
fs: Arc<dyn FileSystem>, fs: Arc<dyn FileSystem>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
system_info: NpmSystemInfo, system_info: NpmSystemInfo,
lifecycle_scripts: LifecycleScriptsConfig, lifecycle_scripts: LifecycleScriptsConfig,

View file

@ -17,6 +17,8 @@ use std::sync::Arc;
use crate::args::LifecycleScriptsConfig; use crate::args::LifecycleScriptsConfig;
use crate::colors; use crate::colors;
use crate::npm::CliNpmCache;
use crate::npm::CliNpmTarballCache;
use async_trait::async_trait; use async_trait::async_trait;
use deno_ast::ModuleSpecifier; use deno_ast::ModuleSpecifier;
use deno_cache_dir::npm::mixed_case_package_name_decode; use deno_cache_dir::npm::mixed_case_package_name_decode;
@ -52,8 +54,6 @@ use crate::util::fs::LaxSingleProcessFsFlag;
use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBar;
use crate::util::progress_bar::ProgressMessagePrompt; use crate::util::progress_bar::ProgressMessagePrompt;
use super::super::cache::NpmCache;
use super::super::cache::TarballCache;
use super::super::resolution::NpmResolution; use super::super::resolution::NpmResolution;
use super::common::bin_entries; use super::common::bin_entries;
use super::common::NpmPackageFsResolver; use super::common::NpmPackageFsResolver;
@ -63,12 +63,12 @@ use super::common::RegistryReadPermissionChecker;
/// and resolves packages from it. /// and resolves packages from it.
#[derive(Debug)] #[derive(Debug)]
pub struct LocalNpmPackageResolver { pub struct LocalNpmPackageResolver {
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
fs: Arc<dyn deno_fs::FileSystem>, fs: Arc<dyn deno_fs::FileSystem>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
progress_bar: ProgressBar, progress_bar: ProgressBar,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
root_node_modules_path: PathBuf, root_node_modules_path: PathBuf,
root_node_modules_url: Url, root_node_modules_url: Url,
system_info: NpmSystemInfo, system_info: NpmSystemInfo,
@ -79,12 +79,12 @@ pub struct LocalNpmPackageResolver {
impl LocalNpmPackageResolver { impl LocalNpmPackageResolver {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
cache: Arc<NpmCache>, cache: Arc<CliNpmCache>,
fs: Arc<dyn deno_fs::FileSystem>, fs: Arc<dyn deno_fs::FileSystem>,
npm_install_deps_provider: Arc<NpmInstallDepsProvider>, npm_install_deps_provider: Arc<NpmInstallDepsProvider>,
progress_bar: ProgressBar, progress_bar: ProgressBar,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
node_modules_folder: PathBuf, node_modules_folder: PathBuf,
system_info: NpmSystemInfo, system_info: NpmSystemInfo,
lifecycle_scripts: LifecycleScriptsConfig, lifecycle_scripts: LifecycleScriptsConfig,
@ -284,10 +284,10 @@ fn local_node_modules_package_contents_path(
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn sync_resolution_with_fs( async fn sync_resolution_with_fs(
snapshot: &NpmResolutionSnapshot, snapshot: &NpmResolutionSnapshot,
cache: &Arc<NpmCache>, cache: &Arc<CliNpmCache>,
npm_install_deps_provider: &NpmInstallDepsProvider, npm_install_deps_provider: &NpmInstallDepsProvider,
progress_bar: &ProgressBar, progress_bar: &ProgressBar,
tarball_cache: &Arc<TarballCache>, tarball_cache: &Arc<CliNpmTarballCache>,
root_node_modules_dir_path: &Path, root_node_modules_dir_path: &Path,
system_info: &NpmSystemInfo, system_info: &NpmSystemInfo,
lifecycle_scripts: &LifecycleScriptsConfig, lifecycle_scripts: &LifecycleScriptsConfig,

View file

@ -12,6 +12,8 @@ use deno_runtime::deno_fs::FileSystem;
use crate::args::LifecycleScriptsConfig; use crate::args::LifecycleScriptsConfig;
use crate::args::NpmInstallDepsProvider; use crate::args::NpmInstallDepsProvider;
use crate::npm::CliNpmCache;
use crate::npm::CliNpmTarballCache;
use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBar;
pub use self::common::NpmPackageFsResolver; pub use self::common::NpmPackageFsResolver;
@ -19,18 +21,16 @@ pub use self::common::NpmPackageFsResolver;
use self::global::GlobalNpmPackageResolver; use self::global::GlobalNpmPackageResolver;
use self::local::LocalNpmPackageResolver; use self::local::LocalNpmPackageResolver;
use super::cache::NpmCache;
use super::cache::TarballCache;
use super::resolution::NpmResolution; use super::resolution::NpmResolution;
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn create_npm_fs_resolver( pub fn create_npm_fs_resolver(
fs: Arc<dyn FileSystem>, fs: Arc<dyn FileSystem>,
npm_cache: Arc<NpmCache>, npm_cache: Arc<CliNpmCache>,
npm_install_deps_provider: &Arc<NpmInstallDepsProvider>, npm_install_deps_provider: &Arc<NpmInstallDepsProvider>,
progress_bar: &ProgressBar, progress_bar: &ProgressBar,
resolution: Arc<NpmResolution>, resolution: Arc<NpmResolution>,
tarball_cache: Arc<TarballCache>, tarball_cache: Arc<CliNpmTarballCache>,
maybe_node_modules_path: Option<PathBuf>, maybe_node_modules_path: Option<PathBuf>,
system_info: NpmSystemInfo, system_info: NpmSystemInfo,
lifecycle_scripts: LifecycleScriptsConfig, lifecycle_scripts: LifecycleScriptsConfig,

View file

@ -1,33 +1,39 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
mod byonm; mod byonm;
mod common;
mod managed; mod managed;
use std::borrow::Cow; use std::borrow::Cow;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use common::maybe_auth_header_for_npm_registry;
use dashmap::DashMap; use dashmap::DashMap;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::url::Url;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo; use deno_npm::registry::NpmPackageInfo;
use deno_resolver::npm::ByonmInNpmPackageChecker; use deno_resolver::npm::ByonmInNpmPackageChecker;
use deno_resolver::npm::ByonmNpmResolver; use deno_resolver::npm::ByonmNpmResolver;
use deno_resolver::npm::CliNpmReqResolver; use deno_resolver::npm::CliNpmReqResolver;
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError; use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
use deno_runtime::deno_fs::FileSystem;
use deno_runtime::deno_node::NodePermissions; use deno_runtime::deno_node::NodePermissions;
use deno_runtime::ops::process::NpmProcessStateProvider; use deno_runtime::ops::process::NpmProcessStateProvider;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq; use deno_semver::package::PackageReq;
use managed::cache::registry_info::get_package_url; use http::HeaderName;
use http::HeaderValue;
use managed::create_managed_in_npm_pkg_checker; use managed::create_managed_in_npm_pkg_checker;
use node_resolver::InNpmPackageChecker; use node_resolver::InNpmPackageChecker;
use node_resolver::NpmPackageFolderResolver; use node_resolver::NpmPackageFolderResolver;
use crate::file_fetcher::FileFetcher; use crate::file_fetcher::FileFetcher;
use crate::http_util::HttpClientProvider;
use crate::util::fs::atomic_write_file_with_retries_and_fs;
use crate::util::fs::hard_link_dir_recursive;
use crate::util::fs::AtomicWriteFileFsAdapter;
use crate::util::progress_bar::ProgressBar;
pub use self::byonm::CliByonmNpmResolver; pub use self::byonm::CliByonmNpmResolver;
pub use self::byonm::CliByonmNpmResolverCreateOptions; pub use self::byonm::CliByonmNpmResolverCreateOptions;
@ -36,6 +42,99 @@ pub use self::managed::CliManagedNpmResolverCreateOptions;
pub use self::managed::CliNpmResolverManagedSnapshotOption; pub use self::managed::CliNpmResolverManagedSnapshotOption;
pub use self::managed::ManagedCliNpmResolver; pub use self::managed::ManagedCliNpmResolver;
pub type CliNpmTarballCache = deno_npm_cache::TarballCache<CliNpmCacheEnv>;
pub type CliNpmCache = deno_npm_cache::NpmCache<CliNpmCacheEnv>;
pub type CliNpmRegistryInfoProvider =
deno_npm_cache::RegistryInfoProvider<CliNpmCacheEnv>;
#[derive(Debug)]
pub struct CliNpmCacheEnv {
fs: Arc<dyn FileSystem>,
http_client_provider: Arc<HttpClientProvider>,
progress_bar: ProgressBar,
}
impl CliNpmCacheEnv {
pub fn new(
fs: Arc<dyn FileSystem>,
http_client_provider: Arc<HttpClientProvider>,
progress_bar: ProgressBar,
) -> Self {
Self {
fs,
http_client_provider,
progress_bar,
}
}
}
#[async_trait::async_trait(?Send)]
impl deno_npm_cache::NpmCacheEnv for CliNpmCacheEnv {
fn exists(&self, path: &Path) -> bool {
self.fs.exists_sync(path)
}
fn hard_link_dir_recursive(
&self,
from: &Path,
to: &Path,
) -> Result<(), AnyError> {
// todo(dsherret): use self.fs here instead
hard_link_dir_recursive(from, to)
}
fn atomic_write_file_with_retries(
&self,
file_path: &Path,
data: &[u8],
) -> std::io::Result<()> {
atomic_write_file_with_retries_and_fs(
&AtomicWriteFileFsAdapter {
fs: self.fs.as_ref(),
write_mode: crate::cache::CACHE_PERM,
},
file_path,
data,
)
}
async fn download_with_retries_on_any_tokio_runtime(
&self,
url: Url,
maybe_auth_header: Option<(HeaderName, HeaderValue)>,
) -> Result<Option<Vec<u8>>, deno_npm_cache::DownloadError> {
let guard = self.progress_bar.update(url.as_str());
let client = self.http_client_provider.get_or_create().map_err(|err| {
deno_npm_cache::DownloadError {
status_code: None,
error: err,
}
})?;
client
.download_with_progress_and_retries(url, maybe_auth_header, &guard)
.await
.map_err(|err| {
use crate::http_util::DownloadError::*;
let status_code = match &err {
Fetch { .. }
| UrlParse { .. }
| HttpParse { .. }
| Json { .. }
| ToStr { .. }
| NoRedirectHeader { .. }
| TooManyRedirects => None,
BadResponse(bad_response_error) => {
Some(bad_response_error.status_code)
}
};
deno_npm_cache::DownloadError {
status_code,
error: err.into(),
}
})
}
}
pub enum CliNpmResolverCreateOptions { pub enum CliNpmResolverCreateOptions {
Managed(CliManagedNpmResolverCreateOptions), Managed(CliManagedNpmResolverCreateOptions),
Byonm(CliByonmNpmResolverCreateOptions), Byonm(CliByonmNpmResolverCreateOptions),
@ -179,13 +278,15 @@ impl NpmFetchResolver {
if let Some(info) = self.info_by_name.get(name) { if let Some(info) = self.info_by_name.get(name) {
return info.value().clone(); return info.value().clone();
} }
// todo(#27198): use RegistryInfoProvider instead
let fetch_package_info = || async { let fetch_package_info = || async {
let info_url = get_package_url(&self.npmrc, name); let info_url = deno_npm_cache::get_package_url(&self.npmrc, name);
let file_fetcher = self.file_fetcher.clone(); let file_fetcher = self.file_fetcher.clone();
let registry_config = self.npmrc.get_registry_config(name); let registry_config = self.npmrc.get_registry_config(name);
// TODO(bartlomieju): this should error out, not use `.ok()`. // TODO(bartlomieju): this should error out, not use `.ok()`.
let maybe_auth_header = let maybe_auth_header =
maybe_auth_header_for_npm_registry(registry_config).ok()?; deno_npm_cache::maybe_auth_header_for_npm_registry(registry_config)
.ok()?;
// spawn due to the lsp's `Send` requirement // spawn due to the lsp's `Send` requirement
let file = deno_core::unsync::spawn(async move { let file = deno_core::unsync::spawn(async move {
file_fetcher file_fetcher

View file

@ -440,8 +440,10 @@ pub fn format_html(
) )
} }
_ => { _ => {
let mut typescript_config = let mut typescript_config_builder =
get_resolved_typescript_config(fmt_options); get_typescript_config_builder(fmt_options);
typescript_config_builder.file_indent_level(hints.indent_level);
let mut typescript_config = typescript_config_builder.build();
typescript_config.line_width = hints.print_width as u32; typescript_config.line_width = hints.print_width as u32;
dprint_plugin_typescript::format_text( dprint_plugin_typescript::format_text(
&path, &path,
@ -919,9 +921,9 @@ fn files_str(len: usize) -> &'static str {
} }
} }
fn get_resolved_typescript_config( fn get_typescript_config_builder(
options: &FmtOptionsConfig, options: &FmtOptionsConfig,
) -> dprint_plugin_typescript::configuration::Configuration { ) -> dprint_plugin_typescript::configuration::ConfigurationBuilder {
let mut builder = let mut builder =
dprint_plugin_typescript::configuration::ConfigurationBuilder::new(); dprint_plugin_typescript::configuration::ConfigurationBuilder::new();
builder.deno(); builder.deno();
@ -953,7 +955,13 @@ fn get_resolved_typescript_config(
}); });
} }
builder.build() builder
}
fn get_resolved_typescript_config(
options: &FmtOptionsConfig,
) -> dprint_plugin_typescript::configuration::Configuration {
get_typescript_config_builder(options).build()
} }
fn get_resolved_markdown_config( fn get_resolved_markdown_config(
@ -1075,6 +1083,7 @@ fn get_resolved_markup_fmt_config(
}; };
let language_options = LanguageOptions { let language_options = LanguageOptions {
script_formatter: Some(markup_fmt::config::ScriptFormatter::Dprint),
quotes: Quotes::Double, quotes: Quotes::Double,
format_comments: false, format_comments: false,
script_indent: true, script_indent: true,

View file

@ -51,19 +51,6 @@ pub fn get_extension(file_path: &Path) -> Option<String> {
.map(|e| e.to_lowercase()); .map(|e| e.to_lowercase());
} }
pub fn get_atomic_dir_path(file_path: &Path) -> PathBuf {
let rand = gen_rand_path_component();
let new_file_name = format!(
".{}_{}",
file_path
.file_name()
.map(|f| f.to_string_lossy())
.unwrap_or(Cow::Borrowed("")),
rand
);
file_path.with_file_name(new_file_name)
}
pub fn get_atomic_file_path(file_path: &Path) -> PathBuf { pub fn get_atomic_file_path(file_path: &Path) -> PathBuf {
let rand = gen_rand_path_component(); let rand = gen_rand_path_component();
let extension = format!("{rand}.tmp"); let extension = format!("{rand}.tmp");

View file

@ -3,11 +3,9 @@
mod async_flag; mod async_flag;
mod sync_read_async_write_lock; mod sync_read_async_write_lock;
mod task_queue; mod task_queue;
mod value_creator;
pub use async_flag::AsyncFlag; pub use async_flag::AsyncFlag;
pub use deno_core::unsync::sync::AtomicFlag; pub use deno_core::unsync::sync::AtomicFlag;
pub use sync_read_async_write_lock::SyncReadAsyncWriteLock; pub use sync_read_async_write_lock::SyncReadAsyncWriteLock;
pub use task_queue::TaskQueue; pub use task_queue::TaskQueue;
pub use task_queue::TaskQueuePermit; pub use task_queue::TaskQueuePermit;
pub use value_creator::MultiRuntimeAsyncValueCreator;

View file

@ -1,213 +0,0 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::sync::Arc;
use deno_core::futures::future::BoxFuture;
use deno_core::futures::future::LocalBoxFuture;
use deno_core::futures::future::Shared;
use deno_core::futures::FutureExt;
use deno_core::parking_lot::Mutex;
use tokio::task::JoinError;
type JoinResult<TResult> = Result<TResult, Arc<JoinError>>;
type CreateFutureFn<TResult> =
Box<dyn Fn() -> LocalBoxFuture<'static, TResult> + Send + Sync>;
#[derive(Debug)]
struct State<TResult> {
retry_index: usize,
future: Option<Shared<BoxFuture<'static, JoinResult<TResult>>>>,
}
/// Attempts to create a shared value asynchronously on one tokio runtime while
/// many runtimes are requesting the value.
///
/// This is only useful when the value needs to get created once across
/// many runtimes.
///
/// This handles the case where the tokio runtime creating the value goes down
/// while another one is waiting on the value.
pub struct MultiRuntimeAsyncValueCreator<TResult: Send + Clone + 'static> {
create_future: CreateFutureFn<TResult>,
state: Mutex<State<TResult>>,
}
impl<TResult: Send + Clone + 'static> std::fmt::Debug
for MultiRuntimeAsyncValueCreator<TResult>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MultiRuntimeAsyncValueCreator").finish()
}
}
impl<TResult: Send + Clone + 'static> MultiRuntimeAsyncValueCreator<TResult> {
pub fn new(create_future: CreateFutureFn<TResult>) -> Self {
Self {
state: Mutex::new(State {
retry_index: 0,
future: None,
}),
create_future,
}
}
pub async fn get(&self) -> TResult {
let (mut future, mut retry_index) = {
let mut state = self.state.lock();
let future = match &state.future {
Some(future) => future.clone(),
None => {
let future = self.create_shared_future();
state.future = Some(future.clone());
future
}
};
(future, state.retry_index)
};
loop {
let result = future.await;
match result {
Ok(result) => return result,
Err(join_error) => {
if join_error.is_cancelled() {
let mut state = self.state.lock();
if state.retry_index == retry_index {
// we were the first one to retry, so create a new future
// that we'll run from the current runtime
state.retry_index += 1;
state.future = Some(self.create_shared_future());
}
retry_index = state.retry_index;
future = state.future.as_ref().unwrap().clone();
// just in case we're stuck in a loop
if retry_index > 1000 {
panic!("Something went wrong.") // should never happen
}
} else {
panic!("{}", join_error);
}
}
}
}
}
fn create_shared_future(
&self,
) -> Shared<BoxFuture<'static, JoinResult<TResult>>> {
let future = (self.create_future)();
deno_core::unsync::spawn(future)
.map(|result| result.map_err(Arc::new))
.boxed()
.shared()
}
}
#[cfg(test)]
mod test {
use deno_core::unsync::spawn;
use super::*;
#[tokio::test]
async fn single_runtime() {
let value_creator = MultiRuntimeAsyncValueCreator::new(Box::new(|| {
async { 1 }.boxed_local()
}));
let value = value_creator.get().await;
assert_eq!(value, 1);
}
#[test]
fn multi_runtimes() {
let value_creator =
Arc::new(MultiRuntimeAsyncValueCreator::new(Box::new(|| {
async {
tokio::task::yield_now().await;
1
}
.boxed_local()
})));
let handles = (0..3)
.map(|_| {
let value_creator = value_creator.clone();
std::thread::spawn(|| {
create_runtime().block_on(async move { value_creator.get().await })
})
})
.collect::<Vec<_>>();
for handle in handles {
assert_eq!(handle.join().unwrap(), 1);
}
}
#[test]
fn multi_runtimes_first_never_finishes() {
let is_first_run = Arc::new(Mutex::new(true));
let (tx, rx) = std::sync::mpsc::channel::<()>();
let value_creator = Arc::new(MultiRuntimeAsyncValueCreator::new({
let is_first_run = is_first_run.clone();
Box::new(move || {
let is_first_run = is_first_run.clone();
let tx = tx.clone();
async move {
let is_first_run = {
let mut is_first_run = is_first_run.lock();
let initial_value = *is_first_run;
*is_first_run = false;
tx.send(()).unwrap();
initial_value
};
if is_first_run {
tokio::time::sleep(std::time::Duration::from_millis(30_000)).await;
panic!("TIMED OUT"); // should not happen
} else {
tokio::task::yield_now().await;
}
1
}
.boxed_local()
})
}));
std::thread::spawn({
let value_creator = value_creator.clone();
let is_first_run = is_first_run.clone();
move || {
create_runtime().block_on(async {
let value_creator = value_creator.clone();
// spawn a task that will never complete
spawn(async move { value_creator.get().await });
// wait for the task to set is_first_run to false
while *is_first_run.lock() {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
// now exit the runtime while the value_creator is still pending
})
}
});
let handle = {
let value_creator = value_creator.clone();
std::thread::spawn(|| {
create_runtime().block_on(async move {
let value_creator = value_creator.clone();
rx.recv().unwrap();
// even though the other runtime shutdown, this get() should
// recover and still get the value
value_creator.get().await
})
})
};
assert_eq!(handle.join().unwrap(), 1);
}
fn create_runtime() -> tokio::runtime::Runtime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
}
}

View file

@ -16,6 +16,7 @@ use once_cell::sync::OnceCell;
use opentelemetry::logs::AnyValue; use opentelemetry::logs::AnyValue;
use opentelemetry::logs::LogRecord as LogRecordTrait; use opentelemetry::logs::LogRecord as LogRecordTrait;
use opentelemetry::logs::Severity; use opentelemetry::logs::Severity;
use opentelemetry::otel_error;
use opentelemetry::trace::SpanContext; use opentelemetry::trace::SpanContext;
use opentelemetry::trace::SpanId; use opentelemetry::trace::SpanId;
use opentelemetry::trace::SpanKind; use opentelemetry::trace::SpanKind;
@ -27,15 +28,21 @@ use opentelemetry::KeyValue;
use opentelemetry::StringValue; use opentelemetry::StringValue;
use opentelemetry::Value; use opentelemetry::Value;
use opentelemetry_otlp::HttpExporterBuilder; use opentelemetry_otlp::HttpExporterBuilder;
use opentelemetry_otlp::MetricExporter;
use opentelemetry_otlp::Protocol; use opentelemetry_otlp::Protocol;
use opentelemetry_otlp::WithExportConfig; use opentelemetry_otlp::WithExportConfig;
use opentelemetry_otlp::WithHttpConfig; use opentelemetry_otlp::WithHttpConfig;
use opentelemetry_sdk::export::trace::SpanData; use opentelemetry_sdk::export::trace::SpanData;
use opentelemetry_sdk::logs::BatchLogProcessor; use opentelemetry_sdk::logs::BatchLogProcessor;
use opentelemetry_sdk::logs::LogProcessor as LogProcessorTrait; use opentelemetry_sdk::logs::LogProcessor;
use opentelemetry_sdk::logs::LogRecord; use opentelemetry_sdk::logs::LogRecord;
use opentelemetry_sdk::metrics::data::Metric;
use opentelemetry_sdk::metrics::data::ResourceMetrics;
use opentelemetry_sdk::metrics::data::ScopeMetrics;
use opentelemetry_sdk::metrics::exporter::PushMetricExporter;
use opentelemetry_sdk::metrics::Temporality;
use opentelemetry_sdk::trace::BatchSpanProcessor; use opentelemetry_sdk::trace::BatchSpanProcessor;
use opentelemetry_sdk::trace::SpanProcessor as SpanProcessorTrait; use opentelemetry_sdk::trace::SpanProcessor;
use opentelemetry_sdk::Resource; use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_NAME; use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_NAME;
use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_VERSION; use opentelemetry_semantic_conventions::resource::PROCESS_RUNTIME_VERSION;
@ -54,9 +61,6 @@ use std::thread;
use std::time::Duration; use std::time::Duration;
use std::time::SystemTime; use std::time::SystemTime;
type SpanProcessor = BatchSpanProcessor<OtelSharedRuntime>;
type LogProcessor = BatchLogProcessor<OtelSharedRuntime>;
deno_core::extension!( deno_core::extension!(
deno_telemetry, deno_telemetry,
ops = [ ops = [
@ -71,6 +75,23 @@ deno_core::extension!(
op_otel_span_attribute3, op_otel_span_attribute3,
op_otel_span_set_dropped, op_otel_span_set_dropped,
op_otel_span_flush, op_otel_span_flush,
op_otel_metrics_resource_attribute,
op_otel_metrics_resource_attribute2,
op_otel_metrics_resource_attribute3,
op_otel_metrics_scope,
op_otel_metrics_sum,
op_otel_metrics_gauge,
op_otel_metrics_sum_or_gauge_data_point,
op_otel_metrics_histogram,
op_otel_metrics_histogram_data_point,
op_otel_metrics_histogram_data_point_entry_final,
op_otel_metrics_histogram_data_point_entry1,
op_otel_metrics_histogram_data_point_entry2,
op_otel_metrics_histogram_data_point_entry3,
op_otel_metrics_data_point_attribute,
op_otel_metrics_data_point_attribute2,
op_otel_metrics_data_point_attribute3,
op_otel_metrics_submit,
], ],
esm = ["telemetry.ts", "util.ts"], esm = ["telemetry.ts", "util.ts"],
); );
@ -322,8 +343,69 @@ mod hyper_client {
} }
} }
static OTEL_PROCESSORS: OnceCell<(SpanProcessor, LogProcessor)> = enum MetricProcessorMessage {
OnceCell::new(); ResourceMetrics(ResourceMetrics),
Flush(tokio::sync::oneshot::Sender<()>),
}
struct MetricProcessor {
tx: tokio::sync::mpsc::Sender<MetricProcessorMessage>,
}
impl MetricProcessor {
fn new(exporter: MetricExporter) -> Self {
let (tx, mut rx) = tokio::sync::mpsc::channel(2048);
let future = async move {
while let Some(message) = rx.recv().await {
match message {
MetricProcessorMessage::ResourceMetrics(mut rm) => {
if let Err(err) = exporter.export(&mut rm).await {
otel_error!(
name: "MetricProcessor.Export.Error",
error = format!("{}", err)
);
}
}
MetricProcessorMessage::Flush(tx) => {
if let Err(()) = tx.send(()) {
otel_error!(
name: "MetricProcessor.Flush.SendResultError",
error = "()",
);
}
}
}
}
};
(*OTEL_SHARED_RUNTIME_SPAWN_TASK_TX)
.unbounded_send(Box::pin(future))
.expect("failed to send task to shared OpenTelemetry runtime");
Self { tx }
}
fn submit(&self, rm: ResourceMetrics) {
let _ = self
.tx
.try_send(MetricProcessorMessage::ResourceMetrics(rm));
}
fn force_flush(&self) -> Result<(), anyhow::Error> {
let (tx, rx) = tokio::sync::oneshot::channel();
self.tx.try_send(MetricProcessorMessage::Flush(tx))?;
deno_core::futures::executor::block_on(rx)?;
Ok(())
}
}
struct Processors {
spans: BatchSpanProcessor<OtelSharedRuntime>,
logs: BatchLogProcessor<OtelSharedRuntime>,
metrics: MetricProcessor,
}
static OTEL_PROCESSORS: OnceCell<Processors> = OnceCell::new();
static BUILT_IN_INSTRUMENTATION_SCOPE: OnceCell< static BUILT_IN_INSTRUMENTATION_SCOPE: OnceCell<
opentelemetry::InstrumentationScope, opentelemetry::InstrumentationScope,
@ -404,6 +486,12 @@ pub fn init(config: OtelConfig) -> anyhow::Result<()> {
BatchSpanProcessor::builder(span_exporter, OtelSharedRuntime).build(); BatchSpanProcessor::builder(span_exporter, OtelSharedRuntime).build();
span_processor.set_resource(&resource); span_processor.set_resource(&resource);
let metric_exporter = HttpExporterBuilder::default()
.with_http_client(client.clone())
.with_protocol(protocol)
.build_metrics_exporter(Temporality::Cumulative)?;
let metric_processor = MetricProcessor::new(metric_exporter);
let log_exporter = HttpExporterBuilder::default() let log_exporter = HttpExporterBuilder::default()
.with_http_client(client) .with_http_client(client)
.with_protocol(protocol) .with_protocol(protocol)
@ -413,7 +501,11 @@ pub fn init(config: OtelConfig) -> anyhow::Result<()> {
log_processor.set_resource(&resource); log_processor.set_resource(&resource);
OTEL_PROCESSORS OTEL_PROCESSORS
.set((span_processor, log_processor)) .set(Processors {
spans: span_processor,
logs: log_processor,
metrics: metric_processor,
})
.map_err(|_| anyhow!("failed to init otel"))?; .map_err(|_| anyhow!("failed to init otel"))?;
let builtin_instrumentation_scope = let builtin_instrumentation_scope =
@ -431,16 +523,22 @@ pub fn init(config: OtelConfig) -> anyhow::Result<()> {
/// `process::exit()`, to ensure that all OpenTelemetry logs are properly /// `process::exit()`, to ensure that all OpenTelemetry logs are properly
/// flushed before the process terminates. /// flushed before the process terminates.
pub fn flush() { pub fn flush() {
if let Some((span_processor, log_processor)) = OTEL_PROCESSORS.get() { if let Some(Processors {
let _ = span_processor.force_flush(); spans,
let _ = log_processor.force_flush(); logs,
metrics,
}) = OTEL_PROCESSORS.get()
{
let _ = spans.force_flush();
let _ = logs.force_flush();
let _ = metrics.force_flush();
} }
} }
pub fn handle_log(record: &log::Record) { pub fn handle_log(record: &log::Record) {
use log::Level; use log::Level;
let Some((_, log_processor)) = OTEL_PROCESSORS.get() else { let Some(Processors { logs, .. }) = OTEL_PROCESSORS.get() else {
return; return;
}; };
@ -490,7 +588,7 @@ pub fn handle_log(record: &log::Record) {
let _ = record.key_values().visit(&mut Visitor(&mut log_record)); let _ = record.key_values().visit(&mut Visitor(&mut log_record));
log_processor.emit( logs.emit(
&mut log_record, &mut log_record,
BUILT_IN_INSTRUMENTATION_SCOPE.get().unwrap(), BUILT_IN_INSTRUMENTATION_SCOPE.get().unwrap(),
); );
@ -648,7 +746,7 @@ fn op_otel_log(
span_id: v8::Local<'_, v8::Value>, span_id: v8::Local<'_, v8::Value>,
#[smi] trace_flags: u8, #[smi] trace_flags: u8,
) { ) {
let Some((_, log_processor)) = OTEL_PROCESSORS.get() else { let Some(Processors { logs, .. }) = OTEL_PROCESSORS.get() else {
return; return;
}; };
@ -678,12 +776,25 @@ fn op_otel_log(
); );
} }
log_processor.emit( logs.emit(
&mut log_record, &mut log_record,
BUILT_IN_INSTRUMENTATION_SCOPE.get().unwrap(), BUILT_IN_INSTRUMENTATION_SCOPE.get().unwrap(),
); );
} }
fn owned_string<'s>(
scope: &mut v8::HandleScope<'s>,
string: v8::Local<'s, v8::String>,
) -> String {
let x = v8::ValueView::new(scope, string);
match x.data() {
v8::ValueViewData::OneByte(bytes) => {
String::from_utf8_lossy(bytes).into_owned()
}
v8::ValueViewData::TwoByte(bytes) => String::from_utf16_lossy(bytes),
}
}
struct TemporarySpan(SpanData); struct TemporarySpan(SpanData);
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -700,10 +811,10 @@ fn op_otel_span_start<'s>(
end_time: f64, end_time: f64,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
if let Some(temporary_span) = state.try_take::<TemporarySpan>() { if let Some(temporary_span) = state.try_take::<TemporarySpan>() {
let Some((span_processor, _)) = OTEL_PROCESSORS.get() else { let Some(Processors { spans, .. }) = OTEL_PROCESSORS.get() else {
return Ok(()); return Ok(());
}; };
span_processor.on_end(temporary_span.0); spans.on_end(temporary_span.0);
}; };
let Some(InstrumentationScope(instrumentation_scope)) = let Some(InstrumentationScope(instrumentation_scope)) =
@ -724,15 +835,7 @@ fn op_otel_span_start<'s>(
let parent_span_id = parse_span_id(scope, parent_span_id); let parent_span_id = parse_span_id(scope, parent_span_id);
let name = { let name = owned_string(scope, name.try_cast()?);
let x = v8::ValueView::new(scope, name.try_cast()?);
match x.data() {
v8::ValueViewData::OneByte(bytes) => {
String::from_utf8_lossy(bytes).into_owned()
}
v8::ValueViewData::TwoByte(bytes) => String::from_utf16_lossy(bytes),
}
};
let temporary_span = TemporarySpan(SpanData { let temporary_span = TemporarySpan(SpanData {
span_context: SpanContext::new( span_context: SpanContext::new(
@ -866,9 +969,598 @@ fn op_otel_span_flush(state: &mut OpState) {
return; return;
}; };
let Some((span_processor, _)) = OTEL_PROCESSORS.get() else { let Some(Processors { spans, .. }) = OTEL_PROCESSORS.get() else {
return; return;
}; };
span_processor.on_end(temporary_span.0); spans.on_end(temporary_span.0);
}
// Holds data being built from JS before
// it is submitted to the rust processor.
struct TemporaryMetricsExport {
resource_attributes: Vec<KeyValue>,
scope_metrics: Vec<ScopeMetrics>,
metric: Option<TemporaryMetric>,
}
struct TemporaryMetric {
name: String,
description: String,
unit: String,
data: TemporaryMetricData,
}
enum TemporaryMetricData {
Sum(opentelemetry_sdk::metrics::data::Sum<f64>),
Gauge(opentelemetry_sdk::metrics::data::Gauge<f64>),
Histogram(opentelemetry_sdk::metrics::data::Histogram<f64>),
}
impl From<TemporaryMetric> for Metric {
fn from(value: TemporaryMetric) -> Self {
Metric {
name: Cow::Owned(value.name),
description: Cow::Owned(value.description),
unit: Cow::Owned(value.unit),
data: match value.data {
TemporaryMetricData::Sum(sum) => Box::new(sum),
TemporaryMetricData::Gauge(gauge) => Box::new(gauge),
TemporaryMetricData::Histogram(histogram) => Box::new(histogram),
},
}
}
}
#[op2(fast)]
fn op_otel_metrics_resource_attribute<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key: v8::Local<'s, v8::Value>,
value: v8::Local<'s, v8::Value>,
) {
let metrics_export = if let Some(metrics_export) =
state.try_borrow_mut::<TemporaryMetricsExport>()
{
metrics_export.resource_attributes.reserve_exact(
(capacity as usize) - metrics_export.resource_attributes.capacity(),
);
metrics_export
} else {
state.put(TemporaryMetricsExport {
resource_attributes: Vec::with_capacity(capacity as usize),
scope_metrics: vec![],
metric: None,
});
state.borrow_mut()
};
attr!(scope, metrics_export.resource_attributes, key, value);
}
#[op2(fast)]
fn op_otel_metrics_resource_attribute2<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
value2: v8::Local<'s, v8::Value>,
) {
let metrics_export = if let Some(metrics_export) =
state.try_borrow_mut::<TemporaryMetricsExport>()
{
metrics_export.resource_attributes.reserve_exact(
(capacity as usize) - metrics_export.resource_attributes.capacity(),
);
metrics_export
} else {
state.put(TemporaryMetricsExport {
resource_attributes: Vec::with_capacity(capacity as usize),
scope_metrics: vec![],
metric: None,
});
state.borrow_mut()
};
attr!(scope, metrics_export.resource_attributes, key1, value1);
attr!(scope, metrics_export.resource_attributes, key2, value2);
}
#[allow(clippy::too_many_arguments)]
#[op2(fast)]
fn op_otel_metrics_resource_attribute3<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
value2: v8::Local<'s, v8::Value>,
key3: v8::Local<'s, v8::Value>,
value3: v8::Local<'s, v8::Value>,
) {
let metrics_export = if let Some(metrics_export) =
state.try_borrow_mut::<TemporaryMetricsExport>()
{
metrics_export.resource_attributes.reserve_exact(
(capacity as usize) - metrics_export.resource_attributes.capacity(),
);
metrics_export
} else {
state.put(TemporaryMetricsExport {
resource_attributes: Vec::with_capacity(capacity as usize),
scope_metrics: vec![],
metric: None,
});
state.borrow_mut()
};
attr!(scope, metrics_export.resource_attributes, key1, value1);
attr!(scope, metrics_export.resource_attributes, key2, value2);
attr!(scope, metrics_export.resource_attributes, key3, value3);
}
#[op2(fast)]
fn op_otel_metrics_scope<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
name: v8::Local<'s, v8::Value>,
schema_url: v8::Local<'s, v8::Value>,
version: v8::Local<'s, v8::Value>,
) {
let name = owned_string(scope, name.cast());
let scope_builder = opentelemetry::InstrumentationScope::builder(name);
let scope_builder = if schema_url.is_null_or_undefined() {
scope_builder
} else {
scope_builder.with_schema_url(owned_string(scope, schema_url.cast()))
};
let scope_builder = if version.is_null_or_undefined() {
scope_builder
} else {
scope_builder.with_version(owned_string(scope, version.cast()))
};
let scope = scope_builder.build();
let scope_metric = ScopeMetrics {
scope,
metrics: vec![],
};
match state.try_borrow_mut::<TemporaryMetricsExport>() {
Some(temp) => {
if let Some(current_metric) = temp.metric.take() {
let metric = Metric::from(current_metric);
temp.scope_metrics.last_mut().unwrap().metrics.push(metric);
}
temp.scope_metrics.push(scope_metric);
}
None => {
state.put(TemporaryMetricsExport {
resource_attributes: vec![],
scope_metrics: vec![scope_metric],
metric: None,
});
}
}
}
#[op2(fast)]
fn op_otel_metrics_sum<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
name: v8::Local<'s, v8::Value>,
description: v8::Local<'s, v8::Value>,
unit: v8::Local<'s, v8::Value>,
#[smi] temporality: u8,
is_monotonic: bool,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(current_metric) = temp.metric.take() {
let metric = Metric::from(current_metric);
temp.scope_metrics.last_mut().unwrap().metrics.push(metric);
}
let name = owned_string(scope, name.cast());
let description = owned_string(scope, description.cast());
let unit = owned_string(scope, unit.cast());
let temporality = match temporality {
0 => Temporality::Delta,
1 => Temporality::Cumulative,
_ => return,
};
let sum = opentelemetry_sdk::metrics::data::Sum {
data_points: vec![],
temporality,
is_monotonic,
};
temp.metric = Some(TemporaryMetric {
name,
description,
unit,
data: TemporaryMetricData::Sum(sum),
});
}
#[op2(fast)]
fn op_otel_metrics_gauge<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
name: v8::Local<'s, v8::Value>,
description: v8::Local<'s, v8::Value>,
unit: v8::Local<'s, v8::Value>,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(current_metric) = temp.metric.take() {
let metric = Metric::from(current_metric);
temp.scope_metrics.last_mut().unwrap().metrics.push(metric);
}
let name = owned_string(scope, name.cast());
let description = owned_string(scope, description.cast());
let unit = owned_string(scope, unit.cast());
let gauge = opentelemetry_sdk::metrics::data::Gauge {
data_points: vec![],
};
temp.metric = Some(TemporaryMetric {
name,
description,
unit,
data: TemporaryMetricData::Gauge(gauge),
});
}
#[op2(fast)]
fn op_otel_metrics_sum_or_gauge_data_point(
state: &mut OpState,
value: f64,
start_time: f64,
time: f64,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
let start_time = SystemTime::UNIX_EPOCH
.checked_add(std::time::Duration::from_secs_f64(start_time))
.unwrap();
let time = SystemTime::UNIX_EPOCH
.checked_add(std::time::Duration::from_secs_f64(time))
.unwrap();
let data_point = opentelemetry_sdk::metrics::data::DataPoint {
value,
start_time: Some(start_time),
time: Some(time),
attributes: vec![],
exemplars: vec![],
};
match &mut temp.metric {
Some(TemporaryMetric {
data: TemporaryMetricData::Sum(sum),
..
}) => sum.data_points.push(data_point),
Some(TemporaryMetric {
data: TemporaryMetricData::Gauge(gauge),
..
}) => gauge.data_points.push(data_point),
_ => {}
}
}
#[op2(fast)]
fn op_otel_metrics_histogram<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
name: v8::Local<'s, v8::Value>,
description: v8::Local<'s, v8::Value>,
unit: v8::Local<'s, v8::Value>,
#[smi] temporality: u8,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(current_metric) = temp.metric.take() {
let metric = Metric::from(current_metric);
temp.scope_metrics.last_mut().unwrap().metrics.push(metric);
}
let name = owned_string(scope, name.cast());
let description = owned_string(scope, description.cast());
let unit = owned_string(scope, unit.cast());
let temporality = match temporality {
0 => Temporality::Delta,
1 => Temporality::Cumulative,
_ => return,
};
let histogram = opentelemetry_sdk::metrics::data::Histogram {
data_points: vec![],
temporality,
};
temp.metric = Some(TemporaryMetric {
name,
description,
unit,
data: TemporaryMetricData::Histogram(histogram),
});
}
#[allow(clippy::too_many_arguments)]
#[op2(fast)]
fn op_otel_metrics_histogram_data_point(
state: &mut OpState,
#[number] count: u64,
min: f64,
max: f64,
sum: f64,
start_time: f64,
time: f64,
#[smi] buckets: u32,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
let min = if min.is_nan() { None } else { Some(min) };
let max = if max.is_nan() { None } else { Some(max) };
let start_time = SystemTime::UNIX_EPOCH
.checked_add(std::time::Duration::from_secs_f64(start_time))
.unwrap();
let time = SystemTime::UNIX_EPOCH
.checked_add(std::time::Duration::from_secs_f64(time))
.unwrap();
let data_point = opentelemetry_sdk::metrics::data::HistogramDataPoint {
bounds: Vec::with_capacity(buckets as usize),
bucket_counts: Vec::with_capacity((buckets as usize) + 1),
count,
sum,
min,
max,
start_time,
time,
attributes: vec![],
exemplars: vec![],
};
if let Some(TemporaryMetric {
data: TemporaryMetricData::Histogram(histogram),
..
}) = &mut temp.metric
{
histogram.data_points.push(data_point);
}
}
#[op2(fast)]
fn op_otel_metrics_histogram_data_point_entry_final(
state: &mut OpState,
#[number] count1: u64,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(TemporaryMetric {
data: TemporaryMetricData::Histogram(histogram),
..
}) = &mut temp.metric
{
histogram
.data_points
.last_mut()
.unwrap()
.bucket_counts
.push(count1)
}
}
#[op2(fast)]
fn op_otel_metrics_histogram_data_point_entry1(
state: &mut OpState,
#[number] count1: u64,
bound1: f64,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(TemporaryMetric {
data: TemporaryMetricData::Histogram(histogram),
..
}) = &mut temp.metric
{
let data_point = histogram.data_points.last_mut().unwrap();
data_point.bucket_counts.push(count1);
data_point.bounds.push(bound1);
}
}
#[op2(fast)]
fn op_otel_metrics_histogram_data_point_entry2(
state: &mut OpState,
#[number] count1: u64,
bound1: f64,
#[number] count2: u64,
bound2: f64,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(TemporaryMetric {
data: TemporaryMetricData::Histogram(histogram),
..
}) = &mut temp.metric
{
let data_point = histogram.data_points.last_mut().unwrap();
data_point.bucket_counts.push(count1);
data_point.bounds.push(bound1);
data_point.bucket_counts.push(count2);
data_point.bounds.push(bound2);
}
}
#[op2(fast)]
fn op_otel_metrics_histogram_data_point_entry3(
state: &mut OpState,
#[number] count1: u64,
bound1: f64,
#[number] count2: u64,
bound2: f64,
#[number] count3: u64,
bound3: f64,
) {
let Some(temp) = state.try_borrow_mut::<TemporaryMetricsExport>() else {
return;
};
if let Some(TemporaryMetric {
data: TemporaryMetricData::Histogram(histogram),
..
}) = &mut temp.metric
{
let data_point = histogram.data_points.last_mut().unwrap();
data_point.bucket_counts.push(count1);
data_point.bounds.push(bound1);
data_point.bucket_counts.push(count2);
data_point.bounds.push(bound2);
data_point.bucket_counts.push(count3);
data_point.bounds.push(bound3);
}
}
#[op2(fast)]
fn op_otel_metrics_data_point_attribute<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key: v8::Local<'s, v8::Value>,
value: v8::Local<'s, v8::Value>,
) {
if let Some(TemporaryMetricsExport {
metric: Some(metric),
..
}) = state.try_borrow_mut::<TemporaryMetricsExport>()
{
let attributes = match &mut metric.data {
TemporaryMetricData::Sum(sum) => {
&mut sum.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Gauge(gauge) => {
&mut gauge.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Histogram(histogram) => {
&mut histogram.data_points.last_mut().unwrap().attributes
}
};
attributes.reserve_exact((capacity as usize) - attributes.capacity());
attr!(scope, attributes, key, value);
}
}
#[op2(fast)]
fn op_otel_metrics_data_point_attribute2<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
value2: v8::Local<'s, v8::Value>,
) {
if let Some(TemporaryMetricsExport {
metric: Some(metric),
..
}) = state.try_borrow_mut::<TemporaryMetricsExport>()
{
let attributes = match &mut metric.data {
TemporaryMetricData::Sum(sum) => {
&mut sum.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Gauge(gauge) => {
&mut gauge.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Histogram(histogram) => {
&mut histogram.data_points.last_mut().unwrap().attributes
}
};
attributes.reserve_exact((capacity as usize) - attributes.capacity());
attr!(scope, attributes, key1, value1);
attr!(scope, attributes, key2, value2);
}
}
#[allow(clippy::too_many_arguments)]
#[op2(fast)]
fn op_otel_metrics_data_point_attribute3<'s>(
scope: &mut v8::HandleScope<'s>,
state: &mut OpState,
#[smi] capacity: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
value2: v8::Local<'s, v8::Value>,
key3: v8::Local<'s, v8::Value>,
value3: v8::Local<'s, v8::Value>,
) {
if let Some(TemporaryMetricsExport {
metric: Some(metric),
..
}) = state.try_borrow_mut::<TemporaryMetricsExport>()
{
let attributes = match &mut metric.data {
TemporaryMetricData::Sum(sum) => {
&mut sum.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Gauge(gauge) => {
&mut gauge.data_points.last_mut().unwrap().attributes
}
TemporaryMetricData::Histogram(histogram) => {
&mut histogram.data_points.last_mut().unwrap().attributes
}
};
attributes.reserve_exact((capacity as usize) - attributes.capacity());
attr!(scope, attributes, key1, value1);
attr!(scope, attributes, key2, value2);
attr!(scope, attributes, key3, value3);
}
}
#[op2(fast)]
fn op_otel_metrics_submit(state: &mut OpState) {
let Some(mut temp) = state.try_take::<TemporaryMetricsExport>() else {
return;
};
let Some(Processors { metrics, .. }) = OTEL_PROCESSORS.get() else {
return;
};
if let Some(current_metric) = temp.metric {
let metric = Metric::from(current_metric);
temp.scope_metrics.last_mut().unwrap().metrics.push(metric);
}
let resource = Resource::new(temp.resource_attributes);
let scope_metrics = temp.scope_metrics;
metrics.submit(ResourceMetrics {
resource,
scope_metrics,
});
} }

View file

@ -7,6 +7,23 @@ import {
op_otel_instrumentation_scope_enter, op_otel_instrumentation_scope_enter,
op_otel_instrumentation_scope_enter_builtin, op_otel_instrumentation_scope_enter_builtin,
op_otel_log, op_otel_log,
op_otel_metrics_data_point_attribute,
op_otel_metrics_data_point_attribute2,
op_otel_metrics_data_point_attribute3,
op_otel_metrics_gauge,
op_otel_metrics_histogram,
op_otel_metrics_histogram_data_point,
op_otel_metrics_histogram_data_point_entry1,
op_otel_metrics_histogram_data_point_entry2,
op_otel_metrics_histogram_data_point_entry3,
op_otel_metrics_histogram_data_point_entry_final,
op_otel_metrics_resource_attribute,
op_otel_metrics_resource_attribute2,
op_otel_metrics_resource_attribute3,
op_otel_metrics_scope,
op_otel_metrics_submit,
op_otel_metrics_sum,
op_otel_metrics_sum_or_gauge_data_point,
op_otel_span_attribute, op_otel_span_attribute,
op_otel_span_attribute2, op_otel_span_attribute2,
op_otel_span_attribute3, op_otel_span_attribute3,
@ -186,7 +203,7 @@ const instrumentationScopes = new SafeWeakMap<
>(); >();
let activeInstrumentationLibrary: WeakRef<InstrumentationLibrary> | null = null; let activeInstrumentationLibrary: WeakRef<InstrumentationLibrary> | null = null;
function submit( function submitSpan(
spanId: string | Uint8Array, spanId: string | Uint8Array,
traceId: string | Uint8Array, traceId: string | Uint8Array,
traceFlags: number, traceFlags: number,
@ -411,7 +428,7 @@ export class Span {
endSpan = (span: Span) => { endSpan = (span: Span) => {
const endTime = now(); const endTime = now();
submit( submitSpan(
span.#spanId, span.#spanId,
span.#traceId, span.#traceId,
span.#traceFlags, span.#traceFlags,
@ -571,7 +588,7 @@ class SpanExporter {
for (let i = 0; i < spans.length; i += 1) { for (let i = 0; i < spans.length; i += 1) {
const span = spans[i]; const span = spans[i];
const context = span.spanContext(); const context = span.spanContext();
submit( submitSpan(
context.spanId, context.spanId,
context.traceId, context.traceId,
context.traceFlags, context.traceFlags,
@ -671,6 +688,262 @@ class ContextManager {
} }
} }
function attributeValue(value: IAnyValue) {
return value.boolValue ?? value.stringValue ?? value.doubleValue ??
value.intValue;
}
function submitMetrics(resource, scopeMetrics) {
let i = 0;
while (i < resource.attributes.length) {
if (i + 2 < resource.attributes.length) {
op_otel_metrics_resource_attribute3(
resource.attributes.length,
resource.attributes[i].key,
attributeValue(resource.attributes[i].value),
resource.attributes[i + 1].key,
attributeValue(resource.attributes[i + 1].value),
resource.attributes[i + 2].key,
attributeValue(resource.attributes[i + 2].value),
);
i += 3;
} else if (i + 1 < resource.attributes.length) {
op_otel_metrics_resource_attribute2(
resource.attributes.length,
resource.attributes[i].key,
attributeValue(resource.attributes[i].value),
resource.attributes[i + 1].key,
attributeValue(resource.attributes[i + 1].value),
);
i += 2;
} else {
op_otel_metrics_resource_attribute(
resource.attributes.length,
resource.attributes[i].key,
attributeValue(resource.attributes[i].value),
);
i += 1;
}
}
for (let smi = 0; smi < scopeMetrics.length; smi += 1) {
const { scope, metrics } = scopeMetrics[smi];
op_otel_metrics_scope(scope.name, scope.schemaUrl, scope.version);
for (let mi = 0; mi < metrics.length; mi += 1) {
const metric = metrics[mi];
switch (metric.dataPointType) {
case 3:
op_otel_metrics_sum(
metric.descriptor.name,
// deno-lint-ignore prefer-primordials
metric.descriptor.description,
metric.descriptor.unit,
metric.aggregationTemporality,
metric.isMonotonic,
);
for (let di = 0; di < metric.dataPoints.length; di += 1) {
const dataPoint = metric.dataPoints[di];
op_otel_metrics_sum_or_gauge_data_point(
dataPoint.value,
hrToSecs(dataPoint.startTime),
hrToSecs(dataPoint.endTime),
);
const attributes = ObjectEntries(dataPoint.attributes);
let i = 0;
while (i < attributes.length) {
if (i + 2 < attributes.length) {
op_otel_metrics_data_point_attribute3(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
attributes[i + 2][0],
attributes[i + 2][1],
);
i += 3;
} else if (i + 1 < attributes.length) {
op_otel_metrics_data_point_attribute2(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
);
i += 2;
} else {
op_otel_metrics_data_point_attribute(
attributes.length,
attributes[i][0],
attributes[i][1],
);
i += 1;
}
}
}
break;
case 2:
op_otel_metrics_gauge(
metric.descriptor.name,
// deno-lint-ignore prefer-primordials
metric.descriptor.description,
metric.descriptor.unit,
);
for (let di = 0; di < metric.dataPoints.length; di += 1) {
const dataPoint = metric.dataPoints[di];
op_otel_metrics_sum_or_gauge_data_point(
dataPoint.value,
hrToSecs(dataPoint.startTime),
hrToSecs(dataPoint.endTime),
);
const attributes = ObjectEntries(dataPoint.attributes);
let i = 0;
while (i < attributes.length) {
if (i + 2 < attributes.length) {
op_otel_metrics_data_point_attribute3(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
attributes[i + 2][0],
attributes[i + 2][1],
);
i += 3;
} else if (i + 1 < attributes.length) {
op_otel_metrics_data_point_attribute2(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
);
i += 2;
} else {
op_otel_metrics_data_point_attribute(
attributes.length,
attributes[i][0],
attributes[i][1],
);
i += 1;
}
}
}
break;
case 0:
op_otel_metrics_histogram(
metric.descriptor.name,
// deno-lint-ignore prefer-primordials
metric.descriptor.description,
metric.descriptor.unit,
metric.aggregationTemporality,
);
for (let di = 0; di < metric.dataPoints.length; di += 1) {
const dataPoint = metric.dataPoints[di];
const { boundaries, counts } = dataPoint.value.buckets;
op_otel_metrics_histogram_data_point(
dataPoint.value.count,
dataPoint.value.min ?? NaN,
dataPoint.value.max ?? NaN,
dataPoint.value.sum,
hrToSecs(dataPoint.startTime),
hrToSecs(dataPoint.endTime),
boundaries.length,
);
let j = 0;
while (j < boundaries.length) {
if (j + 3 < boundaries.length) {
op_otel_metrics_histogram_data_point_entry3(
counts[j],
boundaries[j],
counts[j + 1],
boundaries[j + 1],
counts[j + 2],
boundaries[j + 2],
);
j += 3;
} else if (j + 2 < boundaries.length) {
op_otel_metrics_histogram_data_point_entry2(
counts[j],
boundaries[j],
counts[j + 1],
boundaries[j + 1],
);
j += 2;
} else {
op_otel_metrics_histogram_data_point_entry1(
counts[j],
boundaries[j],
);
j += 1;
}
}
op_otel_metrics_histogram_data_point_entry_final(counts[j]);
const attributes = ObjectEntries(dataPoint.attributes);
let i = 0;
while (i < attributes.length) {
if (i + 2 < attributes.length) {
op_otel_metrics_data_point_attribute3(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
attributes[i + 2][0],
attributes[i + 2][1],
);
i += 3;
} else if (i + 1 < attributes.length) {
op_otel_metrics_data_point_attribute2(
attributes.length,
attributes[i][0],
attributes[i][1],
attributes[i + 1][0],
attributes[i + 1][1],
);
i += 2;
} else {
op_otel_metrics_data_point_attribute(
attributes.length,
attributes[i][0],
attributes[i][1],
);
i += 1;
}
}
}
break;
default:
continue;
}
}
}
op_otel_metrics_submit();
}
class MetricExporter {
export(metrics, resultCallback: (result: ExportResult) => void) {
try {
submitMetrics(metrics.resource, metrics.scopeMetrics);
resultCallback({ code: 0 });
} catch (error) {
resultCallback({
code: 1,
error: ObjectPrototypeIsPrototypeOf(error, Error)
? error as Error
: new Error(String(error)),
});
}
}
async forceFlush() {}
async shutdown() {}
}
const otelConsoleConfig = { const otelConsoleConfig = {
ignore: 0, ignore: 0,
capture: 1, capture: 1,
@ -708,4 +981,5 @@ export function bootstrap(
export const telemetry = { export const telemetry = {
SpanExporter, SpanExporter,
ContextManager, ContextManager,
MetricExporter,
}; };

View file

@ -0,0 +1,42 @@
# Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
[package]
name = "deno_npm_cache"
version = "0.0.1"
authors.workspace = true
edition.workspace = true
license.workspace = true
readme = "README.md"
repository.workspace = true
description = "Helpers for downloading and caching npm dependencies for Deno"
[lib]
path = "lib.rs"
[dependencies]
# todo(dsherret): remove this dependency
anyhow.workspace = true
# todo(dsherret): remove this dependency
deno_core.workspace = true
async-trait.workspace = true
base64.workspace = true
boxed_error.workspace = true
deno_cache_dir.workspace = true
deno_npm.workspace = true
deno_semver.workspace = true
deno_unsync = { workspace = true, features = ["tokio"] }
faster-hex.workspace = true
flate2 = { workspace = true, features = ["zlib-ng-compat"] }
futures.workspace = true
http.workspace = true
log.workspace = true
parking_lot.workspace = true
percent-encoding.workspace = true
rand.workspace = true
ring.workspace = true
serde_json.workspace = true
tar.workspace = true
tempfile = "3.4.0"
thiserror.workspace = true
url.workspace = true

View file

@ -0,0 +1,6 @@
# deno_npm_cache
[![crates](https://img.shields.io/crates/v/deno_npm_cache.svg)](https://crates.io/crates/deno_npm_cache)
[![docs](https://docs.rs/deno_npm_cache/badge.svg)](https://docs.rs/deno_npm_cache)
Helpers for downloading and caching npm dependencies for Deno.

View file

@ -1,63 +1,133 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::collections::HashSet; use std::collections::HashSet;
use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
use deno_ast::ModuleSpecifier; use anyhow::bail;
use anyhow::Context;
use anyhow::Error as AnyError;
use deno_cache_dir::npm::NpmCacheDir; use deno_cache_dir::npm::NpmCacheDir;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo; use deno_npm::registry::NpmPackageInfo;
use deno_npm::NpmPackageCacheFolderId; use deno_npm::NpmPackageCacheFolderId;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
use deno_semver::Version; use deno_semver::Version;
use http::HeaderName;
use http::HeaderValue;
use http::StatusCode;
use parking_lot::Mutex;
use url::Url;
use crate::args::CacheSetting; mod registry_info;
use crate::cache::CACHE_PERM; mod remote;
use crate::util::fs::atomic_write_file_with_retries;
use crate::util::fs::hard_link_dir_recursive;
pub mod registry_info;
mod tarball; mod tarball;
mod tarball_extract; mod tarball_extract;
pub use registry_info::RegistryInfoDownloader; pub use registry_info::RegistryInfoProvider;
pub use tarball::TarballCache; pub use tarball::TarballCache;
// todo(#27198): make both of these private and get the rest of the code
// using RegistryInfoProvider.
pub use registry_info::get_package_url;
pub use remote::maybe_auth_header_for_npm_registry;
#[derive(Debug)]
pub struct DownloadError {
pub status_code: Option<StatusCode>,
pub error: AnyError,
}
impl std::error::Error for DownloadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error.as_ref())
}
}
impl std::fmt::Display for DownloadError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.error.fmt(f)
}
}
#[async_trait::async_trait(?Send)]
pub trait NpmCacheEnv: Send + Sync + 'static {
fn exists(&self, path: &Path) -> bool;
fn hard_link_dir_recursive(
&self,
from: &Path,
to: &Path,
) -> Result<(), AnyError>;
fn atomic_write_file_with_retries(
&self,
file_path: &Path,
data: &[u8],
) -> std::io::Result<()>;
async fn download_with_retries_on_any_tokio_runtime(
&self,
url: Url,
maybe_auth_header: Option<(HeaderName, HeaderValue)>,
) -> Result<Option<Vec<u8>>, DownloadError>;
}
/// Indicates how cached source files should be handled.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum NpmCacheSetting {
/// Only the cached files should be used. Any files not in the cache will
/// error. This is the equivalent of `--cached-only` in the CLI.
Only,
/// No cached source files should be used, and all files should be reloaded.
/// This is the equivalent of `--reload` in the CLI.
ReloadAll,
/// Only some cached resources should be used. This is the equivalent of
/// `--reload=npm:chalk`
ReloadSome { npm_package_names: Vec<String> },
/// The cached source files should be used for local modules. This is the
/// default behavior of the CLI.
Use,
}
impl NpmCacheSetting {
pub fn should_use_for_npm_package(&self, package_name: &str) -> bool {
match self {
NpmCacheSetting::ReloadAll => false,
NpmCacheSetting::ReloadSome { npm_package_names } => {
!npm_package_names.iter().any(|n| n == package_name)
}
_ => true,
}
}
}
/// Stores a single copy of npm packages in a cache. /// Stores a single copy of npm packages in a cache.
#[derive(Debug)] #[derive(Debug)]
pub struct NpmCache { pub struct NpmCache<TEnv: NpmCacheEnv> {
env: Arc<TEnv>,
cache_dir: Arc<NpmCacheDir>, cache_dir: Arc<NpmCacheDir>,
cache_setting: CacheSetting, cache_setting: NpmCacheSetting,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
/// ensures a package is only downloaded once per run
previously_reloaded_packages: Mutex<HashSet<PackageNv>>, previously_reloaded_packages: Mutex<HashSet<PackageNv>>,
} }
impl NpmCache { impl<TEnv: NpmCacheEnv> NpmCache<TEnv> {
pub fn new( pub fn new(
cache_dir: Arc<NpmCacheDir>, cache_dir: Arc<NpmCacheDir>,
cache_setting: CacheSetting, cache_setting: NpmCacheSetting,
env: Arc<TEnv>,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
) -> Self { ) -> Self {
Self { Self {
cache_dir, cache_dir,
cache_setting, cache_setting,
env,
previously_reloaded_packages: Default::default(), previously_reloaded_packages: Default::default(),
npmrc, npmrc,
} }
} }
pub fn cache_setting(&self) -> &CacheSetting { pub fn cache_setting(&self) -> &NpmCacheSetting {
&self.cache_setting &self.cache_setting
} }
@ -118,7 +188,9 @@ impl NpmCache {
// it seems Windows does an "AccessDenied" error when moving a // it seems Windows does an "AccessDenied" error when moving a
// directory with hard links, so that's why this solution is done // directory with hard links, so that's why this solution is done
with_folder_sync_lock(&folder_id.nv, &package_folder, || { with_folder_sync_lock(&folder_id.nv, &package_folder, || {
hard_link_dir_recursive(&original_package_folder, &package_folder) self
.env
.hard_link_dir_recursive(&original_package_folder, &package_folder)
})?; })?;
Ok(()) Ok(())
} }
@ -158,7 +230,7 @@ impl NpmCache {
pub fn resolve_package_folder_id_from_specifier( pub fn resolve_package_folder_id_from_specifier(
&self, &self,
specifier: &ModuleSpecifier, specifier: &Url,
) -> Option<NpmPackageCacheFolderId> { ) -> Option<NpmPackageCacheFolderId> {
self self
.cache_dir .cache_dir
@ -180,7 +252,7 @@ impl NpmCache {
) -> Result<Option<NpmPackageInfo>, AnyError> { ) -> Result<Option<NpmPackageInfo>, AnyError> {
let file_cache_path = self.get_registry_package_info_file_cache_path(name); let file_cache_path = self.get_registry_package_info_file_cache_path(name);
let file_text = match fs::read_to_string(file_cache_path) { let file_text = match std::fs::read_to_string(file_cache_path) {
Ok(file_text) => file_text, Ok(file_text) => file_text,
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None), Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err.into()), Err(err) => return Err(err.into()),
@ -195,7 +267,9 @@ impl NpmCache {
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let file_cache_path = self.get_registry_package_info_file_cache_path(name); let file_cache_path = self.get_registry_package_info_file_cache_path(name);
let file_text = serde_json::to_string(&package_info)?; let file_text = serde_json::to_string(&package_info)?;
atomic_write_file_with_retries(&file_cache_path, file_text, CACHE_PERM)?; self
.env
.atomic_write_file_with_retries(&file_cache_path, file_text.as_bytes())?;
Ok(()) Ok(())
} }
@ -216,7 +290,7 @@ fn with_folder_sync_lock(
output_folder: &Path, output_folder: &Path,
action: impl FnOnce() -> Result<(), AnyError>, action: impl FnOnce() -> Result<(), AnyError>,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
fs::create_dir_all(output_folder).with_context(|| { std::fs::create_dir_all(output_folder).with_context(|| {
format!("Error creating '{}'.", output_folder.display()) format!("Error creating '{}'.", output_folder.display())
})?; })?;
@ -229,7 +303,7 @@ fn with_folder_sync_lock(
// then wait until the other process finishes with a timeout), but // then wait until the other process finishes with a timeout), but
// for now this is good enough. // for now this is good enough.
let sync_lock_path = output_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME); let sync_lock_path = output_folder.join(NPM_PACKAGE_SYNC_LOCK_FILENAME);
match fs::OpenOptions::new() match std::fs::OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
.truncate(false) .truncate(false)
@ -257,7 +331,7 @@ fn with_folder_sync_lock(
match inner(output_folder, action) { match inner(output_folder, action) {
Ok(()) => Ok(()), Ok(()) => Ok(()),
Err(err) => { Err(err) => {
if let Err(remove_err) = fs::remove_dir_all(output_folder) { if let Err(remove_err) = std::fs::remove_dir_all(output_folder) {
if remove_err.kind() != std::io::ErrorKind::NotFound { if remove_err.kind() != std::io::ErrorKind::NotFound {
bail!( bail!(
concat!( concat!(

View file

@ -3,28 +3,22 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use deno_core::anyhow::anyhow; use anyhow::anyhow;
use deno_core::anyhow::bail; use anyhow::bail;
use deno_core::anyhow::Context; use anyhow::Context;
use deno_core::error::custom_error; use anyhow::Error as AnyError;
use deno_core::error::AnyError;
use deno_core::futures::future::LocalBoxFuture;
use deno_core::futures::FutureExt;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageInfo; use deno_npm::registry::NpmPackageInfo;
use deno_unsync::sync::MultiRuntimeAsyncValueCreator;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use parking_lot::Mutex;
use url::Url;
use crate::args::CacheSetting; use crate::remote::maybe_auth_header_for_npm_registry;
use crate::http_util::HttpClientProvider; use crate::NpmCache;
use crate::npm::common::maybe_auth_header_for_npm_registry; use crate::NpmCacheEnv;
use crate::util::progress_bar::ProgressBar; use crate::NpmCacheSetting;
use crate::util::sync::MultiRuntimeAsyncValueCreator;
use super::NpmCache;
// todo(dsherret): create seams and unit test this
type LoadResult = Result<FutureResult, Arc<AnyError>>; type LoadResult = Result<FutureResult, Arc<AnyError>>;
type LoadFuture = LocalBoxFuture<'static, LoadResult>; type LoadFuture = LocalBoxFuture<'static, LoadResult>;
@ -49,30 +43,31 @@ enum MemoryCacheItem {
MemoryCached(Result<Option<Arc<NpmPackageInfo>>, Arc<AnyError>>), MemoryCached(Result<Option<Arc<NpmPackageInfo>>, Arc<AnyError>>),
} }
// todo(#27198): refactor to store this only in the http cache and also
// consolidate with CliNpmRegistryApi.
/// Downloads packuments from the npm registry. /// Downloads packuments from the npm registry.
/// ///
/// This is shared amongst all the workers. /// This is shared amongst all the workers.
#[derive(Debug)] #[derive(Debug)]
pub struct RegistryInfoDownloader { pub struct RegistryInfoProvider<TEnv: NpmCacheEnv> {
cache: Arc<NpmCache>, // todo(#27198): remove this
http_client_provider: Arc<HttpClientProvider>, cache: Arc<NpmCache<TEnv>>,
env: Arc<TEnv>,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
progress_bar: ProgressBar,
memory_cache: Mutex<HashMap<String, MemoryCacheItem>>, memory_cache: Mutex<HashMap<String, MemoryCacheItem>>,
} }
impl RegistryInfoDownloader { impl<TEnv: NpmCacheEnv> RegistryInfoProvider<TEnv> {
pub fn new( pub fn new(
cache: Arc<NpmCache>, cache: Arc<NpmCache<TEnv>>,
http_client_provider: Arc<HttpClientProvider>, env: Arc<TEnv>,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
Self { Self {
cache, cache,
http_client_provider, env,
npmrc, npmrc,
progress_bar,
memory_cache: Default::default(), memory_cache: Default::default(),
} }
} }
@ -94,8 +89,8 @@ impl RegistryInfoDownloader {
self: &Arc<Self>, self: &Arc<Self>,
name: &str, name: &str,
) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> { ) -> Result<Option<Arc<NpmPackageInfo>>, AnyError> {
if *self.cache.cache_setting() == CacheSetting::Only { if *self.cache.cache_setting() == NpmCacheSetting::Only {
return Err(custom_error( return Err(deno_core::error::custom_error(
"NotCached", "NotCached",
format!( format!(
"An npm specifier not found in cache: \"{name}\", --cached-only is specified." "An npm specifier not found in cache: \"{name}\", --cached-only is specified."
@ -167,7 +162,7 @@ impl RegistryInfoDownloader {
) -> Result<NpmPackageInfo, AnyError> { ) -> Result<NpmPackageInfo, AnyError> {
// this scenario failing should be exceptionally rare so let's // this scenario failing should be exceptionally rare so let's
// deal with improving it only when anyone runs into an issue // deal with improving it only when anyone runs into an issue
let maybe_package_info = deno_core::unsync::spawn_blocking({ let maybe_package_info = deno_unsync::spawn_blocking({
let cache = self.cache.clone(); let cache = self.cache.clone();
let name = name.to_string(); let name = name.to_string();
move || cache.load_package_info(&name) move || cache.load_package_info(&name)
@ -199,20 +194,18 @@ impl RegistryInfoDownloader {
return std::future::ready(Err(Arc::new(err))).boxed_local() return std::future::ready(Err(Arc::new(err))).boxed_local()
} }
}; };
let guard = self.progress_bar.update(package_url.as_str());
let name = name.to_string(); let name = name.to_string();
async move { async move {
let client = downloader.http_client_provider.get_or_create()?; let maybe_bytes = downloader
let maybe_bytes = client .env
.download_with_progress_and_retries( .download_with_retries_on_any_tokio_runtime(
package_url, package_url,
maybe_auth_header, maybe_auth_header,
&guard,
) )
.await?; .await?;
match maybe_bytes { match maybe_bytes {
Some(bytes) => { Some(bytes) => {
let future_result = deno_core::unsync::spawn_blocking( let future_result = deno_unsync::spawn_blocking(
move || -> Result<FutureResult, AnyError> { move || -> Result<FutureResult, AnyError> {
let package_info = serde_json::from_slice(&bytes)?; let package_info = serde_json::from_slice(&bytes)?;
match downloader.cache.save_package_info(&name, &package_info) { match downloader.cache.save_package_info(&name, &package_info) {
@ -241,6 +234,8 @@ impl RegistryInfoDownloader {
} }
} }
// todo(#27198): make this private and only use RegistryInfoProvider in the rest of
// the code
pub fn get_package_url(npmrc: &ResolvedNpmRc, name: &str) -> Url { pub fn get_package_url(npmrc: &ResolvedNpmRc, name: &str) -> Url {
let registry_url = npmrc.get_registry_url(name); let registry_url = npmrc.get_registry_url(name);
// The '/' character in scoped package names "@scope/name" must be // The '/' character in scoped package names "@scope/name" must be

View file

@ -1,10 +1,10 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use anyhow::bail;
use anyhow::Context;
use anyhow::Error as AnyError;
use base64::prelude::BASE64_STANDARD; use base64::prelude::BASE64_STANDARD;
use base64::Engine; use base64::Engine;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_npm::npm_rc::RegistryConfig; use deno_npm::npm_rc::RegistryConfig;
use http::header; use http::header;

View file

@ -3,33 +3,26 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use deno_core::anyhow::anyhow; use anyhow::anyhow;
use deno_core::anyhow::bail; use anyhow::bail;
use deno_core::anyhow::Context; use anyhow::Context;
use deno_core::error::custom_error; use anyhow::Error as AnyError;
use deno_core::error::AnyError;
use deno_core::futures::future::LocalBoxFuture;
use deno_core::futures::FutureExt;
use deno_core::parking_lot::Mutex;
use deno_core::url::Url;
use deno_npm::npm_rc::ResolvedNpmRc; use deno_npm::npm_rc::ResolvedNpmRc;
use deno_npm::registry::NpmPackageVersionDistInfo; use deno_npm::registry::NpmPackageVersionDistInfo;
use deno_runtime::deno_fs::FileSystem;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
use deno_unsync::sync::MultiRuntimeAsyncValueCreator;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use http::StatusCode; use http::StatusCode;
use parking_lot::Mutex;
use url::Url;
use crate::args::CacheSetting; use crate::remote::maybe_auth_header_for_npm_registry;
use crate::http_util::DownloadError; use crate::tarball_extract::verify_and_extract_tarball;
use crate::http_util::HttpClientProvider; use crate::tarball_extract::TarballExtractionMode;
use crate::npm::common::maybe_auth_header_for_npm_registry; use crate::NpmCache;
use crate::util::progress_bar::ProgressBar; use crate::NpmCacheEnv;
use crate::util::sync::MultiRuntimeAsyncValueCreator; use crate::NpmCacheSetting;
use super::tarball_extract::verify_and_extract_tarball;
use super::tarball_extract::TarballExtractionMode;
use super::NpmCache;
// todo(dsherret): create seams and unit test this
type LoadResult = Result<(), Arc<AnyError>>; type LoadResult = Result<(), Arc<AnyError>>;
type LoadFuture = LocalBoxFuture<'static, LoadResult>; type LoadFuture = LocalBoxFuture<'static, LoadResult>;
@ -49,29 +42,23 @@ enum MemoryCacheItem {
/// ///
/// This is shared amongst all the workers. /// This is shared amongst all the workers.
#[derive(Debug)] #[derive(Debug)]
pub struct TarballCache { pub struct TarballCache<TEnv: NpmCacheEnv> {
cache: Arc<NpmCache>, cache: Arc<NpmCache<TEnv>>,
fs: Arc<dyn FileSystem>, env: Arc<TEnv>,
http_client_provider: Arc<HttpClientProvider>,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
progress_bar: ProgressBar,
memory_cache: Mutex<HashMap<PackageNv, MemoryCacheItem>>, memory_cache: Mutex<HashMap<PackageNv, MemoryCacheItem>>,
} }
impl TarballCache { impl<TEnv: NpmCacheEnv> TarballCache<TEnv> {
pub fn new( pub fn new(
cache: Arc<NpmCache>, cache: Arc<NpmCache<TEnv>>,
fs: Arc<dyn FileSystem>, env: Arc<TEnv>,
http_client_provider: Arc<HttpClientProvider>,
npmrc: Arc<ResolvedNpmRc>, npmrc: Arc<ResolvedNpmRc>,
progress_bar: ProgressBar,
) -> Self { ) -> Self {
Self { Self {
cache, cache,
fs, env,
http_client_provider,
npmrc, npmrc,
progress_bar,
memory_cache: Default::default(), memory_cache: Default::default(),
} }
} }
@ -144,11 +131,11 @@ impl TarballCache {
let package_folder = let package_folder =
tarball_cache.cache.package_folder_for_nv_and_url(&package_nv, registry_url); tarball_cache.cache.package_folder_for_nv_and_url(&package_nv, registry_url);
let should_use_cache = tarball_cache.cache.should_use_cache_for_package(&package_nv); let should_use_cache = tarball_cache.cache.should_use_cache_for_package(&package_nv);
let package_folder_exists = tarball_cache.fs.exists_sync(&package_folder); let package_folder_exists = tarball_cache.env.exists(&package_folder);
if should_use_cache && package_folder_exists { if should_use_cache && package_folder_exists {
return Ok(()); return Ok(());
} else if tarball_cache.cache.cache_setting() == &CacheSetting::Only { } else if tarball_cache.cache.cache_setting() == &NpmCacheSetting::Only {
return Err(custom_error( return Err(deno_core::error::custom_error(
"NotCached", "NotCached",
format!( format!(
"An npm specifier not found in cache: \"{}\", --cached-only is specified.", "An npm specifier not found in cache: \"{}\", --cached-only is specified.",
@ -169,15 +156,13 @@ impl TarballCache {
tarball_cache.npmrc.tarball_config(&tarball_uri); tarball_cache.npmrc.tarball_config(&tarball_uri);
let maybe_auth_header = maybe_registry_config.and_then(|c| maybe_auth_header_for_npm_registry(c).ok()?); let maybe_auth_header = maybe_registry_config.and_then(|c| maybe_auth_header_for_npm_registry(c).ok()?);
let guard = tarball_cache.progress_bar.update(&dist.tarball); let result = tarball_cache.env
let result = tarball_cache.http_client_provider .download_with_retries_on_any_tokio_runtime(tarball_uri, maybe_auth_header)
.get_or_create()?
.download_with_progress_and_retries(tarball_uri, maybe_auth_header, &guard)
.await; .await;
let maybe_bytes = match result { let maybe_bytes = match result {
Ok(maybe_bytes) => maybe_bytes, Ok(maybe_bytes) => maybe_bytes,
Err(DownloadError::BadResponse(err)) => { Err(err) => {
if err.status_code == StatusCode::UNAUTHORIZED if err.status_code == Some(StatusCode::UNAUTHORIZED)
&& maybe_registry_config.is_none() && maybe_registry_config.is_none()
&& tarball_cache.npmrc.get_registry_config(&package_nv.name).auth_token.is_some() && tarball_cache.npmrc.get_registry_config(&package_nv.name).auth_token.is_some()
{ {
@ -194,7 +179,6 @@ impl TarballCache {
} }
return Err(err.into()) return Err(err.into())
}, },
Err(err) => return Err(err.into()),
}; };
match maybe_bytes { match maybe_bytes {
Some(bytes) => { Some(bytes) => {
@ -213,7 +197,7 @@ impl TarballCache {
}; };
let dist = dist.clone(); let dist = dist.clone();
let package_nv = package_nv.clone(); let package_nv = package_nv.clone();
deno_core::unsync::spawn_blocking(move || { deno_unsync::spawn_blocking(move || {
verify_and_extract_tarball( verify_and_extract_tarball(
&package_nv, &package_nv,
&bytes, &bytes,

View file

@ -1,16 +1,17 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::borrow::Cow;
use std::collections::HashSet; use std::collections::HashSet;
use std::fs; use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::bail;
use anyhow::Context;
use anyhow::Error as AnyError;
use base64::prelude::BASE64_STANDARD; use base64::prelude::BASE64_STANDARD;
use base64::Engine; use base64::Engine;
use deno_core::anyhow::bail;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_npm::registry::NpmPackageVersionDistInfo; use deno_npm::registry::NpmPackageVersionDistInfo;
use deno_npm::registry::NpmPackageVersionDistInfoIntegrity; use deno_npm::registry::NpmPackageVersionDistInfoIntegrity;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
@ -18,8 +19,6 @@ use flate2::read::GzDecoder;
use tar::Archive; use tar::Archive;
use tar::EntryType; use tar::EntryType;
use crate::util::path::get_atomic_dir_path;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum TarballExtractionMode { pub enum TarballExtractionMode {
/// Overwrites the destination directory without deleting any files. /// Overwrites the destination directory without deleting any files.
@ -206,10 +205,30 @@ fn extract_tarball(data: &[u8], output_folder: &Path) -> Result<(), AnyError> {
Ok(()) Ok(())
} }
fn get_atomic_dir_path(file_path: &Path) -> PathBuf {
let rand = gen_rand_path_component();
let new_file_name = format!(
".{}_{}",
file_path
.file_name()
.map(|f| f.to_string_lossy())
.unwrap_or(Cow::Borrowed("")),
rand
);
file_path.with_file_name(new_file_name)
}
fn gen_rand_path_component() -> String {
(0..4).fold(String::new(), |mut output, _| {
output.push_str(&format!("{:02x}", rand::random::<u8>()));
output
})
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use deno_semver::Version; use deno_semver::Version;
use test_util::TempDir; use tempfile::TempDir;
use super::*; use super::*;
@ -303,21 +322,21 @@ mod test {
#[test] #[test]
fn rename_with_retries_succeeds_exists() { fn rename_with_retries_succeeds_exists() {
let temp_dir = TempDir::new(); let temp_dir = TempDir::new().unwrap();
let folder_1 = temp_dir.path().join("folder_1"); let folder_1 = temp_dir.path().join("folder_1");
let folder_2 = temp_dir.path().join("folder_2"); let folder_2 = temp_dir.path().join("folder_2");
folder_1.create_dir_all(); std::fs::create_dir_all(&folder_1).unwrap();
folder_1.join("a.txt").write("test"); std::fs::write(folder_1.join("a.txt"), "test").unwrap();
folder_2.create_dir_all(); std::fs::create_dir_all(&folder_2).unwrap();
// this will not end up in the output as rename_with_retries assumes // this will not end up in the output as rename_with_retries assumes
// the folders ending up at the destination are the same // the folders ending up at the destination are the same
folder_2.join("b.txt").write("test2"); std::fs::write(folder_2.join("b.txt"), "test2").unwrap();
let dest_folder = temp_dir.path().join("dest_folder"); let dest_folder = temp_dir.path().join("dest_folder");
rename_with_retries(folder_1.as_path(), dest_folder.as_path()).unwrap(); rename_with_retries(folder_1.as_path(), &dest_folder).unwrap();
rename_with_retries(folder_2.as_path(), dest_folder.as_path()).unwrap(); rename_with_retries(folder_2.as_path(), &dest_folder).unwrap();
assert!(dest_folder.join("a.txt").exists()); assert!(dest_folder.join("a.txt").exists());
assert!(!dest_folder.join("b.txt").exists()); assert!(!dest_folder.join("b.txt").exists());
} }

View file

@ -0,0 +1,9 @@
This crate is a work in progress:
1. Remove `deno_core` dependency.
1. Remove `anyhow` dependency.
1. Add a clippy.toml file that bans accessing the file system directory and
instead does it through a trait.
1. Make this crate work in Wasm.
1. Refactor to store npm packument in a single place:
https://github.com/denoland/deno/issues/27198

View file

@ -19,7 +19,7 @@ const encoder = new TextEncoder();
const NODE_VERSION = version; const NODE_VERSION = version;
const NODE_IGNORED_TEST_DIRS = [ export const NODE_IGNORED_TEST_DIRS = [
"addons", "addons",
"async-hooks", "async-hooks",
"cctest", "cctest",
@ -40,13 +40,13 @@ const NODE_IGNORED_TEST_DIRS = [
"wpt", "wpt",
]; ];
const VENDORED_NODE_TEST = new URL("./suite/test/", import.meta.url); export const VENDORED_NODE_TEST = new URL("./suite/test/", import.meta.url);
const NODE_COMPAT_TEST_DEST_URL = new URL( export const NODE_COMPAT_TEST_DEST_URL = new URL(
"../test/", "../test/",
import.meta.url, import.meta.url,
); );
async function getNodeTests(): Promise<string[]> { export async function getNodeTests(): Promise<string[]> {
const paths: string[] = []; const paths: string[] = [];
const rootPath = VENDORED_NODE_TEST.href.slice(7); const rootPath = VENDORED_NODE_TEST.href.slice(7);
for await ( for await (
@ -61,7 +61,7 @@ async function getNodeTests(): Promise<string[]> {
return paths.sort(); return paths.sort();
} }
function getDenoTests() { export function getDenoTests() {
return Object.entries(config.tests) return Object.entries(config.tests)
.filter(([testDir]) => !NODE_IGNORED_TEST_DIRS.includes(testDir)) .filter(([testDir]) => !NODE_IGNORED_TEST_DIRS.includes(testDir))
.flatMap(([testDir, tests]) => tests.map((test) => testDir + "/" + test)); .flatMap(([testDir, tests]) => tests.map((test) => testDir + "/" + test));

File diff suppressed because one or more lines are too long

View file

@ -15,6 +15,10 @@
{ {
"args": "run -A main.ts uncaught.ts", "args": "run -A main.ts uncaught.ts",
"output": "uncaught.out" "output": "uncaught.out"
},
{
"args": "run -A main.ts metric.ts",
"output": "metric.out"
} }
] ]
} }

View file

@ -188,5 +188,6 @@
"traceId": "00000000000000000000000000000003", "traceId": "00000000000000000000000000000003",
"spanId": "1000000000000002" "spanId": "1000000000000002"
} }
] ],
"metrics": []
} }

View file

@ -15,5 +15,6 @@
"traceId": "", "traceId": "",
"spanId": "" "spanId": ""
} }
] ],
"metrics": []
} }

View file

@ -3,6 +3,7 @@
const data = { const data = {
spans: [], spans: [],
logs: [], logs: [],
metrics: [],
}; };
const server = Deno.serve( const server = Deno.serve(
@ -45,6 +46,11 @@ const server = Deno.serve(
data.spans.push(...sSpans.spans); data.spans.push(...sSpans.spans);
}); });
}); });
body.resourceMetrics?.forEach((rMetrics) => {
rMetrics.scopeMetrics.forEach((sMetrics) => {
data.metrics.push(...sMetrics.metrics);
});
});
return Response.json({ partialSuccess: {} }, { status: 200 }); return Response.json({ partialSuccess: {} }, { status: 200 });
}, },
}, },

View file

@ -0,0 +1,124 @@
{
"spans": [],
"logs": [],
"metrics": [
{
"name": "counter",
"description": "Example of a Counter",
"unit": "",
"metadata": [],
"sum": {
"dataPoints": [
{
"attributes": [
{
"key": "attribute",
"value": {
"doubleValue": 1
}
}
],
"startTimeUnixNano": "[WILDCARD]",
"timeUnixNano": "[WILDCARD]",
"exemplars": [],
"flags": 0,
"asDouble": 1
}
],
"aggregationTemporality": 2,
"isMonotonic": true
}
},
{
"name": "up_down_counter",
"description": "Example of a UpDownCounter",
"unit": "",
"metadata": [],
"sum": {
"dataPoints": [
{
"attributes": [
{
"key": "attribute",
"value": {
"doubleValue": 1
}
}
],
"startTimeUnixNano": "[WILDCARD]",
"timeUnixNano": "[WILDCARD]",
"exemplars": [],
"flags": 0,
"asDouble": -1
}
],
"aggregationTemporality": 2,
"isMonotonic": false
}
},
{
"name": "histogram",
"description": "Example of a Histogram",
"unit": "",
"metadata": [],
"histogram": {
"dataPoints": [
{
"attributes": [
{
"key": "attribute",
"value": {
"doubleValue": 1
}
}
],
"startTimeUnixNano": "[WILDCARD]",
"timeUnixNano": "[WILDCARD]",
"count": 1,
"sum": 1,
"bucketCounts": [
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"explicitBounds": [
0,
5,
10,
25,
50,
75,
100,
250,
500,
750,
1000,
2500,
5000,
7500,
10000
],
"exemplars": [],
"flags": 0,
"min": 1,
"max": 1
}
],
"aggregationTemporality": 2
}
}
]
}

View file

@ -0,0 +1,34 @@
import {
MeterProvider,
PeriodicExportingMetricReader,
} from "npm:@opentelemetry/sdk-metrics@1.28.0";
const meterProvider = new MeterProvider();
meterProvider.addMetricReader(
new PeriodicExportingMetricReader({
exporter: new Deno.telemetry.MetricExporter(),
exportIntervalMillis: 100,
}),
);
const meter = meterProvider.getMeter("m");
const counter = meter.createCounter("counter", {
description: "Example of a Counter",
});
const upDownCounter = meter.createUpDownCounter("up_down_counter", {
description: "Example of a UpDownCounter",
});
const histogram = meter.createHistogram("histogram", {
description: "Example of a Histogram",
});
const attributes = { attribute: 1 };
counter.add(1, attributes);
upDownCounter.add(-1, attributes);
histogram.record(1, attributes);
await meterProvider.forceFlush();

View file

@ -15,5 +15,6 @@
"traceId": "", "traceId": "",
"spanId": "" "spanId": ""
} }
] ],
"metrics": []
} }

View file

@ -33,5 +33,6 @@ throw new Error("uncaught");
"traceId": "", "traceId": "",
"spanId": "" "spanId": ""
} }
] ],
"metrics": []
} }

View file

@ -12,6 +12,10 @@
"broken": { "broken": {
"args": "fmt broken.html", "args": "fmt broken.html",
"output": "broken.out" "output": "broken.out"
},
"with_js": {
"args": "fmt --check with_js.html",
"output": "Checked 1 file\n"
} }
} }
} }

View file

@ -0,0 +1,9 @@
<html>
<body>
<script>
/* some multi-line comment
with function below it */
someFunc();
</script>
</body>
</html>

View file

@ -1,4 +1,5 @@
{ {
"tempDir": true,
"tests": { "tests": {
"cjs_with_deps": { "cjs_with_deps": {
"args": "run --allow-read --allow-env main.js", "args": "run --allow-read --allow-env main.js",

View file

@ -3,7 +3,7 @@ type: JavaScript
dependencies: 14 unique dependencies: 14 unique
size: [WILDCARD] size: [WILDCARD]
file:///[WILDCARD]/cjs_with_deps/main.js ([WILDCARD]) file:///[WILDCARD]/main.js ([WILDCARD])
├─┬ npm:/chalk@4.1.2 ([WILDCARD]) ├─┬ npm:/chalk@4.1.2 ([WILDCARD])
│ ├─┬ npm:/ansi-styles@4.3.0 ([WILDCARD]) │ ├─┬ npm:/ansi-styles@4.3.0 ([WILDCARD])
│ │ └─┬ npm:/color-convert@2.0.1 ([WILDCARD]) │ │ └─┬ npm:/color-convert@2.0.1 ([WILDCARD])