2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2023-12-26 08:32:21 -05:00
|
|
|
use bytes::Bytes;
|
2024-01-24 15:30:08 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::url::Url;
|
2024-01-08 18:51:49 -05:00
|
|
|
use sha2::Digest;
|
2024-01-24 16:24:52 -05:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::ffi::OsStr;
|
2024-01-08 18:51:49 -05:00
|
|
|
use std::fmt::Write as FmtWrite;
|
2023-11-23 18:38:07 -05:00
|
|
|
use std::io::Write;
|
2023-12-14 04:55:56 -05:00
|
|
|
use std::path::Path;
|
2023-11-23 18:38:07 -05:00
|
|
|
use tar::Header;
|
|
|
|
|
2024-01-24 16:24:52 -05:00
|
|
|
use crate::tools::registry::paths::PackagePath;
|
2023-11-23 18:38:07 -05:00
|
|
|
use crate::util::import_map::ImportMapUnfurler;
|
|
|
|
|
2024-01-24 08:49:33 -05:00
|
|
|
use super::diagnostics::PublishDiagnostic;
|
|
|
|
use super::diagnostics::PublishDiagnosticsCollector;
|
|
|
|
|
2024-01-08 18:51:49 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct PublishableTarballFile {
|
2024-01-24 16:24:52 -05:00
|
|
|
pub specifier: Url,
|
2024-01-08 18:51:49 -05:00
|
|
|
pub size: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct PublishableTarball {
|
|
|
|
pub files: Vec<PublishableTarballFile>,
|
|
|
|
pub hash: String,
|
|
|
|
pub bytes: Bytes,
|
|
|
|
}
|
|
|
|
|
2023-11-23 18:38:07 -05:00
|
|
|
pub fn create_gzipped_tarball(
|
2023-12-14 04:55:56 -05:00
|
|
|
dir: &Path,
|
2024-01-23 06:40:23 -05:00
|
|
|
source_cache: &dyn deno_graph::ParsedSourceStore,
|
2024-01-24 08:49:33 -05:00
|
|
|
diagnostics_collector: &PublishDiagnosticsCollector,
|
2024-01-08 18:51:49 -05:00
|
|
|
unfurler: &ImportMapUnfurler,
|
2024-01-24 15:30:08 -05:00
|
|
|
file_patterns: Option<FilePatterns>,
|
2024-01-08 18:51:49 -05:00
|
|
|
) -> Result<PublishableTarball, AnyError> {
|
2023-11-23 18:38:07 -05:00
|
|
|
let mut tar = TarGzArchive::new();
|
2024-01-08 18:51:49 -05:00
|
|
|
let mut files = vec![];
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2024-01-24 16:24:52 -05:00
|
|
|
let mut paths = HashSet::new();
|
|
|
|
|
2024-01-08 18:51:49 -05:00
|
|
|
let mut iterator = walkdir::WalkDir::new(dir).follow_links(false).into_iter();
|
|
|
|
while let Some(entry) = iterator.next() {
|
2023-11-23 18:38:07 -05:00
|
|
|
let entry = entry?;
|
|
|
|
|
2024-01-24 16:24:52 -05:00
|
|
|
let path = entry.path();
|
|
|
|
let file_type = entry.file_type();
|
|
|
|
|
|
|
|
let matches_pattern = file_patterns
|
|
|
|
.as_ref()
|
|
|
|
.map(|p| p.matches_path(path))
|
|
|
|
.unwrap_or(true);
|
|
|
|
if !matches_pattern
|
|
|
|
|| path.file_name() == Some(OsStr::new(".git"))
|
|
|
|
|| path.file_name() == Some(OsStr::new("node_modules"))
|
|
|
|
{
|
|
|
|
if file_type.is_dir() {
|
|
|
|
iterator.skip_current_dir();
|
2024-01-08 18:51:49 -05:00
|
|
|
}
|
2024-01-24 16:24:52 -05:00
|
|
|
continue;
|
2024-01-08 18:51:49 -05:00
|
|
|
}
|
|
|
|
|
2024-01-24 16:24:52 -05:00
|
|
|
let Ok(specifier) = Url::from_file_path(path) else {
|
|
|
|
diagnostics_collector
|
|
|
|
.to_owned()
|
|
|
|
.push(PublishDiagnostic::InvalidPath {
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
message: "unable to convert path to url".to_string(),
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if file_type.is_file() {
|
|
|
|
let Ok(relative_path) = path.strip_prefix(dir) else {
|
|
|
|
diagnostics_collector
|
|
|
|
.to_owned()
|
|
|
|
.push(PublishDiagnostic::InvalidPath {
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
message: "path is not in publish directory".to_string(),
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
let path_str = relative_path.components().fold(
|
|
|
|
"".to_string(),
|
|
|
|
|mut path, component| {
|
|
|
|
path.push('/');
|
|
|
|
match component {
|
|
|
|
std::path::Component::Normal(normal) => {
|
|
|
|
path.push_str(&normal.to_string_lossy())
|
|
|
|
}
|
|
|
|
std::path::Component::CurDir => path.push('.'),
|
|
|
|
std::path::Component::ParentDir => path.push_str(".."),
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
path
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
match PackagePath::new(path_str.clone()) {
|
|
|
|
Ok(package_path) => {
|
|
|
|
if !paths.insert(package_path) {
|
|
|
|
diagnostics_collector.to_owned().push(
|
|
|
|
PublishDiagnostic::DuplicatePath {
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
diagnostics_collector.to_owned().push(
|
|
|
|
PublishDiagnostic::InvalidPath {
|
|
|
|
path: path.to_path_buf(),
|
|
|
|
message: err.to_string(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = std::fs::read(path).with_context(|| {
|
2024-01-08 18:51:49 -05:00
|
|
|
format!("Unable to read file '{}'", entry.path().display())
|
2023-11-30 13:54:54 -05:00
|
|
|
})?;
|
2024-01-08 18:51:49 -05:00
|
|
|
files.push(PublishableTarballFile {
|
2024-01-24 16:24:52 -05:00
|
|
|
specifier: specifier.clone(),
|
2024-01-08 18:51:49 -05:00
|
|
|
size: data.len(),
|
|
|
|
});
|
2024-01-24 16:24:52 -05:00
|
|
|
let content = match source_cache.get_parsed_source(&specifier) {
|
2024-01-23 06:40:23 -05:00
|
|
|
Some(parsed_source) => {
|
2024-01-24 08:49:33 -05:00
|
|
|
let mut reporter = |diagnostic| {
|
|
|
|
diagnostics_collector
|
|
|
|
.push(PublishDiagnostic::ImportMapUnfurl(diagnostic));
|
|
|
|
};
|
2024-01-24 16:24:52 -05:00
|
|
|
let content =
|
|
|
|
unfurler.unfurl(&specifier, &parsed_source, &mut reporter);
|
2024-01-23 06:40:23 -05:00
|
|
|
content.into_bytes()
|
|
|
|
}
|
|
|
|
None => data,
|
|
|
|
};
|
2023-11-30 13:54:54 -05:00
|
|
|
tar
|
2024-01-24 16:24:52 -05:00
|
|
|
.add_file(format!(".{}", path_str), &content)
|
2023-11-30 13:54:54 -05:00
|
|
|
.with_context(|| {
|
2024-01-08 18:51:49 -05:00
|
|
|
format!("Unable to add file to tarball '{}'", entry.path().display())
|
2023-11-30 13:54:54 -05:00
|
|
|
})?;
|
2024-01-24 16:24:52 -05:00
|
|
|
} else if !file_type.is_dir() {
|
|
|
|
diagnostics_collector.push(PublishDiagnostic::UnsupportedFileType {
|
|
|
|
specifier,
|
|
|
|
kind: if file_type.is_symlink() {
|
|
|
|
"symlink".to_owned()
|
|
|
|
} else {
|
|
|
|
format!("{file_type:?}")
|
|
|
|
},
|
|
|
|
});
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = tar.finish().context("Unable to finish tarball")?;
|
2024-01-08 18:51:49 -05:00
|
|
|
let hash_bytes: Vec<u8> = sha2::Sha256::digest(&v).iter().cloned().collect();
|
|
|
|
let mut hash = "sha256-".to_string();
|
|
|
|
for byte in hash_bytes {
|
|
|
|
write!(&mut hash, "{:02x}", byte).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PublishableTarball {
|
|
|
|
files,
|
|
|
|
hash,
|
|
|
|
bytes: Bytes::from(v),
|
|
|
|
})
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TarGzArchive {
|
|
|
|
builder: tar::Builder<Vec<u8>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TarGzArchive {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
builder: tar::Builder::new(Vec::new()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_file(
|
|
|
|
&mut self,
|
|
|
|
path: String,
|
|
|
|
data: &[u8],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let mut header = Header::new_gnu();
|
|
|
|
header.set_size(data.len() as u64);
|
|
|
|
self.builder.append_data(&mut header, &path, data)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(mut self) -> Result<Vec<u8>, AnyError> {
|
|
|
|
self.builder.finish()?;
|
|
|
|
let bytes = self.builder.into_inner()?;
|
|
|
|
let mut gz_bytes = Vec::new();
|
|
|
|
let mut encoder = flate2::write::GzEncoder::new(
|
|
|
|
&mut gz_bytes,
|
|
|
|
flate2::Compression::default(),
|
|
|
|
);
|
|
|
|
encoder.write_all(&bytes)?;
|
|
|
|
encoder.finish()?;
|
|
|
|
Ok(gz_bytes)
|
|
|
|
}
|
|
|
|
}
|