2025-01-01 04:12:39 +09:00
|
|
|
// Copyright 2018-2025 the Deno authors. MIT license.
|
2023-11-24 00:38:07 +01:00
|
|
|
|
2024-12-31 12:13:39 -05:00
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2023-12-26 14:32:21 +01:00
|
|
|
use bytes::Bytes;
|
2023-11-24 00:38:07 +01:00
|
|
|
use deno_core::anyhow::Context;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::url::Url;
|
2025-02-19 14:58:10 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
2024-01-08 18:51:49 -05:00
|
|
|
use sha2::Digest;
|
2023-11-24 00:38:07 +01:00
|
|
|
use tar::Header;
|
|
|
|
|
2024-01-24 14:49:33 +01:00
|
|
|
use super::diagnostics::PublishDiagnosticsCollector;
|
2025-02-19 14:58:10 -05:00
|
|
|
use super::module_content::ModuleContentProvider;
|
2024-04-24 14:52:05 -04:00
|
|
|
use super::paths::CollectedPublishPath;
|
2024-01-24 14:49:33 +01:00
|
|
|
|
2024-01-08 18:51:49 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct PublishableTarballFile {
|
2024-02-28 07:58:02 +05:30
|
|
|
pub path_str: String,
|
2024-01-24 22:24:52 +01:00
|
|
|
pub specifier: Url,
|
2024-02-28 07:58:02 +05:30
|
|
|
pub hash: String,
|
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-24 00:38:07 +01:00
|
|
|
pub fn create_gzipped_tarball(
|
2025-02-19 14:58:10 -05:00
|
|
|
module_content_provider: &ModuleContentProvider,
|
|
|
|
graph: &ModuleGraph,
|
2024-01-24 14:49:33 +01:00
|
|
|
diagnostics_collector: &PublishDiagnosticsCollector,
|
2025-02-19 14:58:10 -05:00
|
|
|
publish_paths: Vec<CollectedPublishPath>,
|
2024-01-08 18:51:49 -05:00
|
|
|
) -> Result<PublishableTarball, AnyError> {
|
2023-11-24 00:38:07 +01:00
|
|
|
let mut tar = TarGzArchive::new();
|
2024-01-08 18:51:49 -05:00
|
|
|
let mut files = vec![];
|
2023-11-24 00:38:07 +01:00
|
|
|
|
2024-04-24 14:52:05 -04:00
|
|
|
for path in publish_paths {
|
|
|
|
let path_str = &path.relative_path;
|
|
|
|
let specifier = &path.specifier;
|
2024-02-29 11:54:57 +00:00
|
|
|
|
2024-07-24 21:43:30 -04:00
|
|
|
let content = match path.maybe_content {
|
|
|
|
Some(content) => content.clone(),
|
2025-02-19 14:58:10 -05:00
|
|
|
None => module_content_provider.resolve_content_maybe_unfurling(
|
|
|
|
graph,
|
|
|
|
diagnostics_collector,
|
2024-07-24 21:43:30 -04:00
|
|
|
&path.path,
|
|
|
|
specifier,
|
|
|
|
)?,
|
|
|
|
};
|
2024-03-07 20:16:32 -05:00
|
|
|
|
|
|
|
files.push(PublishableTarballFile {
|
|
|
|
path_str: path_str.clone(),
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
// This hash string matches the checksum computed by registry
|
|
|
|
hash: format!("sha256-{:x}", sha2::Sha256::digest(&content)),
|
|
|
|
size: content.len(),
|
|
|
|
});
|
2024-07-26 11:35:29 -04:00
|
|
|
assert!(path_str.starts_with('/'));
|
2024-03-07 20:16:32 -05:00
|
|
|
tar
|
|
|
|
.add_file(format!(".{}", path_str), &content)
|
|
|
|
.with_context(|| {
|
2024-07-24 21:43:30 -04:00
|
|
|
format!("Unable to add file to tarball '{}'", path.path.display())
|
2024-03-07 20:16:32 -05:00
|
|
|
})?;
|
2023-11-24 00:38:07 +01: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();
|
|
|
|
}
|
|
|
|
|
2024-02-20 13:30:34 +01:00
|
|
|
files.sort_by(|a, b| a.specifier.cmp(&b.specifier));
|
|
|
|
|
2024-01-08 18:51:49 -05:00
|
|
|
Ok(PublishableTarball {
|
|
|
|
files,
|
|
|
|
hash,
|
|
|
|
bytes: Bytes::from(v),
|
|
|
|
})
|
2023-11-24 00:38:07 +01: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)
|
|
|
|
}
|
|
|
|
}
|