summaryrefslogtreecommitdiff
path: root/cli/tools/registry/tar.rs
blob: 1dcfe2949b0e14e7bd70d0edcf30344b5392bb1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use bytes::Bytes;
use deno_config::glob::FilePatterns;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::url::Url;
use sha2::Digest;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::fmt::Write as FmtWrite;
use std::io::Write;
use std::path::Path;
use tar::Header;

use crate::tools::registry::paths::PackagePath;
use crate::util::import_map::ImportMapUnfurler;

use super::diagnostics::PublishDiagnostic;
use super::diagnostics::PublishDiagnosticsCollector;

#[derive(Debug, Clone, PartialEq)]
pub struct PublishableTarballFile {
  pub specifier: Url,
  pub size: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub struct PublishableTarball {
  pub files: Vec<PublishableTarballFile>,
  pub hash: String,
  pub bytes: Bytes,
}

pub fn create_gzipped_tarball(
  dir: &Path,
  source_cache: &dyn deno_graph::ParsedSourceStore,
  diagnostics_collector: &PublishDiagnosticsCollector,
  unfurler: &ImportMapUnfurler,
  file_patterns: Option<FilePatterns>,
) -> Result<PublishableTarball, AnyError> {
  let mut tar = TarGzArchive::new();
  let mut files = vec![];

  let mut paths = HashSet::new();

  let mut iterator = walkdir::WalkDir::new(dir).follow_links(false).into_iter();
  while let Some(entry) = iterator.next() {
    let entry = entry?;

    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();
      }
      continue;
    }

    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(|| {
        format!("Unable to read file '{}'", entry.path().display())
      })?;
      files.push(PublishableTarballFile {
        specifier: specifier.clone(),
        size: data.len(),
      });
      let content = match source_cache.get_parsed_source(&specifier) {
        Some(parsed_source) => {
          let mut reporter = |diagnostic| {
            diagnostics_collector
              .push(PublishDiagnostic::ImportMapUnfurl(diagnostic));
          };
          let content =
            unfurler.unfurl(&specifier, &parsed_source, &mut reporter);
          content.into_bytes()
        }
        None => data,
      };
      tar
        .add_file(format!(".{}", path_str), &content)
        .with_context(|| {
          format!("Unable to add file to tarball '{}'", entry.path().display())
        })?;
    } 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:?}")
        },
      });
    }
  }

  let v = tar.finish().context("Unable to finish tarball")?;
  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),
  })
}

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)
  }
}