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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use bun_collections::HashMap;
use bun_collections::zig_hash_map::MapEntry as Entry;
use bun_semver::string::Builder as StringBuilder;
use bun_sys::Fd;
use crate::PackageNameHash;
use crate::npm;
#[derive(Default)]
pub struct PackageManifestMap {
pub hash_map: ManifestHashMap,
}
pub enum Value {
Expired(npm::PackageManifest),
Manifest(npm::PackageManifest),
// Avoid checking the filesystem again.
NotFound,
}
impl Value {
// Zig: `entry.value_ptr.manifest` field projection on the `.manifest` arm.
bun_core::enum_unwrap!(pub Value, Manifest => fn manifest / manifest_mut -> npm::PackageManifest);
}
// Zig: `std.HashMap(PackageNameHash, Value, IdentityContext(PackageNameHash), 80)`.
type ManifestHashMap =
HashMap<PackageNameHash, Value, bun_collections::IdentityContext<PackageNameHash>>;
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum CacheBehavior {
LoadFromMemory,
LoadFromMemoryFallbackToDisk,
}
/// By-value snapshot of the `PackageManager` fields the disk-fallback path of
/// [`PackageManifestMap::by_name_hash_allow_expired`] reads.
///
/// Zig threads `pm: *PackageManager` and reads these directly. In Rust every
/// caller is `pm.manifests.by_name…(pm, …)`, so accepting `&mut PackageManager`
/// (or `&mut *raw`) would alias the `&mut self` receiver — Stacked-Borrows UB
/// regardless of which fields the body touches. Capturing the four scalars by
/// value lets callers split `&mut pm.manifests` from `&pm.lockfile` /
/// `&pm.options` with safe disjoint-field borrows and keeps this map free of a
/// `PackageManager` dependency.
///
/// Construct via `PackageManager::manifest_disk_cache_ctx`.
#[derive(Clone, Copy)]
pub struct DiskCacheCtx {
pub enable_manifest_cache: bool,
pub enable_manifest_cache_control: bool,
/// `pm.getCacheDirectory()` — pre-opened so the lookup never needs `&mut
/// PackageManager`. `None` iff `enable_manifest_cache` is false (the only
/// branch that reads it is gated on that flag).
pub cache_directory: Option<Fd>,
pub timestamp_for_manifest_cache_control: u32,
}
impl DiskCacheCtx {
/// Context for the [`CacheBehavior::LoadFromMemory`] path, which never
/// reads any of these fields.
pub const MEMORY_ONLY: Self = Self {
enable_manifest_cache: false,
enable_manifest_cache_control: false,
cache_directory: None,
timestamp_for_manifest_cache_control: 0,
};
}
impl PackageManifestMap {
pub fn by_name(
&mut self,
ctx: DiskCacheCtx,
scope: &npm::registry::Scope,
name: &[u8],
cache_behavior: CacheBehavior,
needs_extended_manifest: bool,
) -> Option<&mut npm::PackageManifest> {
self.by_name_hash(
ctx,
scope,
StringBuilder::string_hash(name),
cache_behavior,
needs_extended_manifest,
)
}
pub fn insert(
&mut self,
name_hash: PackageNameHash,
manifest: npm::PackageManifest,
) -> Result<(), bun_alloc::AllocError> {
self.hash_map.insert(name_hash, Value::Manifest(manifest));
Ok(())
}
pub fn by_name_hash(
&mut self,
ctx: DiskCacheCtx,
scope: &npm::registry::Scope,
name_hash: PackageNameHash,
cache_behavior: CacheBehavior,
needs_extended_manifest: bool,
) -> Option<&mut npm::PackageManifest> {
self.by_name_hash_allow_expired(
ctx,
scope,
name_hash,
None,
cache_behavior,
needs_extended_manifest,
)
}
/// Memory-only lookup — equivalent to Zig
/// `byNameHash(this, pm, scope, hash, .load_from_memory, _)` with
/// `is_expired = null`, but without the `ctx`/`scope` parameters: the
/// `.load_from_memory` arm never reads them. Exposed separately so callers
/// holding `&mut PackageManager` can borrow only the disjoint
/// `pm.manifests` field.
pub fn by_name_hash_in_memory(
&mut self,
name_hash: PackageNameHash,
) -> Option<&mut npm::PackageManifest> {
match self.hash_map.get_mut(&name_hash)? {
Value::Manifest(m) => Some(m),
Value::Expired(_) | Value::NotFound => None,
}
}
pub fn by_name_allow_expired(
&mut self,
ctx: DiskCacheCtx,
scope: &npm::registry::Scope,
name: &[u8],
is_expired: Option<&mut bool>,
cache_behavior: CacheBehavior,
needs_extended_manifest: bool,
) -> Option<&mut npm::PackageManifest> {
self.by_name_hash_allow_expired(
ctx,
scope,
StringBuilder::string_hash(name),
is_expired,
cache_behavior,
needs_extended_manifest,
)
}
/// Zig: `byNameHashAllowExpired(this, pm: *PackageManager, ...)`.
///
/// PORT NOTE: reshaped for borrowck — Zig passes `pm` and reads
/// `pm.options.enable.*`, `pm.getCacheDirectory()`, and
/// `pm.timestamp_for_manifest_cache_control` on the disk-fallback arm.
/// Those scalars are hoisted into [`DiskCacheCtx`] so callers never hold
/// `&mut pm.manifests` and a `PackageManager` borrow simultaneously.
pub fn by_name_hash_allow_expired(
&mut self,
ctx: DiskCacheCtx,
scope: &npm::registry::Scope,
name_hash: PackageNameHash,
is_expired: Option<&mut bool>,
cache_behavior: CacheBehavior,
needs_extended_manifest: bool,
) -> Option<&mut npm::PackageManifest> {
if cache_behavior == CacheBehavior::LoadFromMemory {
let entry = self.hash_map.get_mut(&name_hash)?;
return match entry {
Value::Manifest(m) => Some(m),
Value::Expired(m) => {
if let Some(expiry) = is_expired {
*expiry = true;
Some(m)
} else {
None
}
}
Value::NotFound => None,
};
}
// PORT NOTE: reshaped for borrowck — Zig's `getOrPut` returns `{ found_existing, value_ptr }`;
// Rust splits into Occupied/Vacant arms.
match self.hash_map.entry(name_hash) {
Entry::Occupied(occ) => {
let value_ptr = occ.into_mut();
// PORT NOTE: reshaped for borrowck — Zig mutated `value_ptr.*` in
// place from `.manifest` to `.expired`. Compute the demote decision
// first without holding a borrow that escapes the fn.
let demote = matches!(
value_ptr,
Value::Manifest(m)
if needs_extended_manifest && !m.pkg.has_extended_manifest
);
if demote {
let Value::Manifest(m) = core::mem::replace(value_ptr, Value::NotFound) else {
unreachable!()
};
*value_ptr = Value::Expired(m);
} else if let Value::Manifest(m) = value_ptr {
return Some(m);
}
if let Some(expiry) = is_expired {
if let Value::Expired(m) = value_ptr {
*expiry = true;
return Some(m);
}
}
None
}
Entry::Vacant(vac) => {
if ctx.enable_manifest_cache {
// `ctx.cache_directory` is `Some` iff `enable_manifest_cache`
// (see `manifest_disk_cache_ctx`).
let cache_fd = ctx.cache_directory.expect("cache_directory");
if let Some(manifest) = npm::package_manifest::Serializer::load_by_file_id(
scope, cache_fd, name_hash,
)
.ok()
.flatten()
{
if needs_extended_manifest && !manifest.pkg.has_extended_manifest {
let value_ptr = vac.insert(Value::Expired(manifest));
if let Some(expiry) = is_expired {
*expiry = true;
let Value::Expired(m) = value_ptr else {
unreachable!()
};
return Some(m);
}
return None;
}
if ctx.enable_manifest_cache_control
&& manifest.pkg.public_max_age
> ctx.timestamp_for_manifest_cache_control
{
let value_ptr = vac.insert(Value::Manifest(manifest));
let Value::Manifest(m) = value_ptr else {
unreachable!()
};
return Some(m);
} else {
let value_ptr = vac.insert(Value::Expired(manifest));
if let Some(expiry) = is_expired {
*expiry = true;
let Value::Expired(m) = value_ptr else {
unreachable!()
};
return Some(m);
}
return None;
}
}
}
vac.insert(Value::NotFound);
None
}
}
}
}
// ported from: src/install/PackageManifestMap.zig