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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::{
fs, io,
path::{Path, PathBuf},
};
use cfg_if::cfg_if;
#[cfg(feature = "yarn_pnp")]
use pnp::fs::{LruZipCache, VPath, VPathInfo, ZipCache};
use crate::ResolveError;
/// File System abstraction used for `ResolverGeneric`
pub trait FileSystem: Send + Sync {
#[cfg(feature = "yarn_pnp")]
fn new(yarn_pnp: bool) -> Self;
#[cfg(not(feature = "yarn_pnp"))]
fn new() -> Self;
/// See [std::fs::read_to_string]
///
/// # Errors
///
/// * See [std::fs::read_to_string]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn read_to_string(&self, path: &Path) -> io::Result<String>;
/// See [std::fs::metadata]
///
/// # Errors
/// See [std::fs::metadata]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn metadata(&self, path: &Path) -> io::Result<FileMetadata>;
/// See [std::fs::symlink_metadata]
///
/// # Errors
///
/// See [std::fs::symlink_metadata]
/// ## Warning
/// Use `&Path` instead of a generic `P: AsRef<Path>` here,
/// because object safety requirements, it is especially useful, when
/// you want to store multiple `dyn FileSystem` in a `Vec` or use a `ResolverGeneric<Fs>` in
/// napi env.
fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata>;
/// Returns the resolution of a symbolic link.
///
/// # Errors
/// * Returns an error of [`ResolveError::IOError`] kind if there is an IO error invoking [`std::fs::read_link`].
/// * Returns an error of [`ResolveError::PathNotSupported`] kind if the symlink target cannot be represented
/// as a path that can be consumed by the `import`/`require` syntax of Node.js.
/// Caller should not try to follow the symlink in this case.
///
/// See [std::fs::read_link]
fn read_link(&self, path: &Path) -> Result<PathBuf, ResolveError>;
}
/// Metadata information about a file
#[derive(Debug, Clone, Copy)]
pub struct FileMetadata {
pub(crate) is_file: bool,
pub(crate) is_dir: bool,
pub(crate) is_symlink: bool,
}
impl FileMetadata {
#[must_use]
pub const fn new(is_file: bool, is_dir: bool, is_symlink: bool) -> Self {
Self { is_file, is_dir, is_symlink }
}
#[must_use]
pub const fn is_file(self) -> bool {
self.is_file
}
#[must_use]
pub const fn is_dir(self) -> bool {
self.is_dir
}
#[must_use]
pub const fn is_symlink(self) -> bool {
self.is_symlink
}
}
#[cfg(feature = "yarn_pnp")]
impl From<pnp::fs::FileType> for FileMetadata {
fn from(value: pnp::fs::FileType) -> Self {
Self::new(value == pnp::fs::FileType::File, value == pnp::fs::FileType::Directory, false)
}
}
impl From<fs::Metadata> for FileMetadata {
fn from(metadata: fs::Metadata) -> Self {
Self::new(metadata.is_file(), metadata.is_dir(), metadata.is_symlink())
}
}
#[cfg(not(feature = "yarn_pnp"))]
pub struct FileSystemOs;
#[cfg(feature = "yarn_pnp")]
pub struct FileSystemOs {
pnp_lru: LruZipCache<Vec<u8>>,
yarn_pnp: bool,
}
impl FileSystemOs {
/// # Errors
///
/// See [std::fs::read_to_string]
pub fn read_to_string(path: &Path) -> io::Result<String> {
// `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally
let bytes = std::fs::read(path)?;
if simdutf8::basic::from_utf8(&bytes).is_err() {
// Same error as `fs::read_to_string` produces (`io::Error::INVALID_UTF8`)
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"stream did not contain valid UTF-8",
));
}
// SAFETY: `simdutf8` has ensured it's a valid UTF-8 string
Ok(unsafe { String::from_utf8_unchecked(bytes) })
}
/// # Errors
///
/// See [std::fs::metadata]
#[inline]
pub fn metadata(path: &Path) -> io::Result<FileMetadata> {
fs::metadata(path).map(FileMetadata::from)
}
/// # Errors
///
/// See [std::fs::symlink_metadata]
#[inline]
pub fn symlink_metadata(path: &Path) -> io::Result<FileMetadata> {
fs::symlink_metadata(path).map(FileMetadata::from)
}
/// # Errors
///
/// See [std::fs::read_link]
#[inline]
pub fn read_link(path: &Path) -> Result<PathBuf, ResolveError> {
let path = fs::read_link(path)?;
cfg_if! {
if #[cfg(target_os = "windows")] {
crate::windows::strip_windows_prefix(path)
} else {
Ok(path)
}
}
}
}
impl FileSystem for FileSystemOs {
#[cfg(feature = "yarn_pnp")]
fn new(yarn_pnp: bool) -> Self {
Self { pnp_lru: LruZipCache::new(50, pnp::fs::open_zip_via_read_p), yarn_pnp }
}
#[cfg(not(feature = "yarn_pnp"))]
fn new() -> Self {
Self
}
fn read_to_string(&self, path: &Path) -> io::Result<String> {
cfg_if! {
if #[cfg(feature = "yarn_pnp")] {
if self.yarn_pnp {
return match VPath::from(path)? {
VPath::Zip(info) => {
self.pnp_lru.read_to_string(info.physical_base_path(), info.zip_path)
}
VPath::Virtual(info) => Self::read_to_string(&info.physical_base_path()),
VPath::Native(path) => Self::read_to_string(&path),
}
}
}
}
Self::read_to_string(path)
}
fn metadata(&self, path: &Path) -> io::Result<FileMetadata> {
cfg_if! {
if #[cfg(feature = "yarn_pnp")] {
if self.yarn_pnp {
return match VPath::from(path)? {
VPath::Zip(info) => self
.pnp_lru
.file_type(info.physical_base_path(), info.zip_path)
.map(FileMetadata::from),
VPath::Virtual(info) => {
Self::metadata(&info.physical_base_path())
}
VPath::Native(path) => Self::metadata(&path),
}
}
}
}
Self::metadata(path)
}
fn symlink_metadata(&self, path: &Path) -> io::Result<FileMetadata> {
Self::symlink_metadata(path)
}
fn read_link(&self, path: &Path) -> Result<PathBuf, ResolveError> {
cfg_if! {
if #[cfg(feature = "yarn_pnp")] {
if self.yarn_pnp {
return match VPath::from(path)? {
VPath::Zip(info) => Self::read_link(&info.physical_base_path().join(info.zip_path)),
VPath::Virtual(info) => Self::read_link(&info.physical_base_path()),
VPath::Native(path) => Self::read_link(&path),
}
}
}
}
Self::read_link(path)
}
}
#[test]
fn metadata() {
let meta = FileMetadata { is_file: true, is_dir: true, is_symlink: true };
assert_eq!(
format!("{meta:?}"),
"FileMetadata { is_file: true, is_dir: true, is_symlink: true }"
);
let _ = meta;
}