forked from oven-sh/bun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall_optimizer.rs
More file actions
268 lines (238 loc) · 8.56 KB
/
Copy pathpostinstall_optimizer.rs
File metadata and controls
268 lines (238 loc) · 8.56 KB
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
use std::sync::LazyLock;
use bun_collections::{ArrayHashMap, ArrayIdentityContextU64};
// `Expr` here is the T2 `bun_ast::Expr` (re-exported via
// `crate::bun_json`), not the T4 `bun_ast::Expr`. The sole caller
// (`lockfile::Package::parse_with_json`) holds a JSON-parsed `bun_json::Expr`,
// so binding to the lower-tier type avoids a cross-tier mismatch.
use bun_ast as js_ast;
use bun_semver as semver;
use crate::lockfile::package::Meta;
use crate::lockfile::tree::Id as TreeId;
use crate::npm;
use crate::{PackageID, PackageNameHash};
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum PostinstallOptimizer {
NativeBinlink,
Ignore,
}
// `string_hash` is `Wyhash11` (not `const fn`; only the std-Wyhash final4
// variant has a const implementation), so this is a `LazyLock` rather than a
// `const` array.
static DEFAULT_NATIVE_BINLINKS_NAME_HASHES: LazyLock<[PackageNameHash; 2]> = LazyLock::new(|| {
[
semver::string::Builder::string_hash(b"esbuild"),
semver::string::Builder::string_hash(b"@anthropic-ai/claude-code"),
]
});
struct DefaultIgnore {
name_hash: PackageNameHash,
minimum_version: semver::Version,
}
// `Version::parse_utf8` is not `const fn`, so this is a `LazyLock`.
static DEFAULT_IGNORE: LazyLock<[DefaultIgnore; 1]> = LazyLock::new(|| {
[DefaultIgnore {
name_hash: semver::string::Builder::string_hash(b"sharp"),
minimum_version: semver::Version::parse_utf8(b"0.33.0").version.min(),
}]
});
impl PostinstallOptimizer {
fn from_string_array_group(
list: &mut List,
expr: &js_ast::Expr,
value: PostinstallOptimizer,
) -> Result<bool, bun_alloc::AllocError> {
let Some(mut array) = expr.as_array() else {
return Ok(false);
};
while let Some(entry) = array.next() {
let js_ast::ExprData::EString(s) = &entry.data else {
continue;
};
debug_assert!(s.next.is_none());
debug_assert!(s.is_utf8());
let str = s.slice8();
if str.is_empty() {
continue;
}
list.dynamic
.put(semver::string::Builder::string_hash(str), value)?;
}
Ok(true)
}
pub fn from_package_json(
list: &mut List,
expr: &js_ast::Expr,
) -> Result<(), bun_alloc::AllocError> {
if let Some(native_deps_expr) = expr.get(b"nativeDependencies") {
list.disable_default_native_binlinks = Self::from_string_array_group(
list,
&native_deps_expr,
PostinstallOptimizer::NativeBinlink,
)?;
}
if let Some(ignored_scripts_expr) = expr.get(b"ignoreScripts") {
list.disable_default_ignore = Self::from_string_array_group(
list,
&ignored_scripts_expr,
PostinstallOptimizer::Ignore,
)?;
}
Ok(())
}
pub fn get_native_binlink_replacement_package_id(
resolutions: &[PackageID],
metas: &[Meta],
target_cpu: npm::Architecture,
target_os: npm::OperatingSystem,
) -> Option<PackageID> {
// Windows needs file extensions.
// Wrap the raw bit in the newtype since `WIN32` is exported as the
// underlying `u16` repr, not `Self`.
if target_os.is_match(npm::OperatingSystem(npm::OperatingSystem::WIN32)) {
return None;
}
// Loop through the list of optional dependencies with platform-specific constraints
// Find a matching target-specific dependency.
for &resolution in resolutions {
if (resolution as usize) >= metas.len() {
continue;
}
let meta: &Meta = &metas[resolution as usize];
if meta.arch == npm::Architecture::ALL || meta.os == npm::OperatingSystem::ALL {
continue;
}
if meta.arch.is_match(target_cpu) && meta.os.is_match(target_os) {
return Some(resolution);
}
}
None
}
}
// The key is already a hash, so use the identity context rather than
// re-hashing it.
pub type Map = ArrayHashMap<PackageNameHash, PostinstallOptimizer, ArrayIdentityContextU64>;
#[derive(Default)]
pub struct List {
pub dynamic: Map,
pub disable_default_native_binlinks: bool,
pub disable_default_ignore: bool,
}
#[derive(Clone, Copy)]
pub struct PkgInfo<'a> {
pub name_hash: PackageNameHash,
pub version: Option<semver::Version>,
// Borrows the lockfile string buffer at call sites; only used to resolve
// pre/build tags inside `Version::order`, never stored.
pub version_buf: &'a [u8],
}
impl Default for PkgInfo<'_> {
fn default() -> Self {
Self {
name_hash: 0,
version: None,
version_buf: b"",
}
}
}
impl List {
pub fn is_native_binlink_enabled(&self) -> bool {
if self.dynamic.len() == 0 {
if self.disable_default_native_binlinks {
return true;
}
}
// The feature flag defaults to false; `env_var` returns `Option<bool>`,
// so unwrap_or(false) preserves the default.
if bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_DISABLE_NATIVE_DEPENDENCY_LINKER
.get()
.unwrap_or(false)
{
return false;
}
true
}
pub fn should_ignore_lifecycle_scripts(
&self,
pkg_info: &PkgInfo<'_>,
resolutions: &[PackageID],
metas: &[Meta],
target_cpu: npm::Architecture,
target_os: npm::OperatingSystem,
tree_id: Option<TreeId>,
) -> bool {
// The feature flag defaults to false; see note on the binlinker flag above.
if bun_core::env_var::feature_flag::BUN_FEATURE_FLAG_DISABLE_IGNORE_SCRIPTS
.get()
.unwrap_or(false)
{
return false;
}
let Some(mode) = self.get(pkg_info) else {
return false;
};
match mode {
PostinstallOptimizer::NativeBinlink => {
// TODO: support hoisted.
(tree_id.is_none() || tree_id.unwrap() == 0)
// It's not as simple as checking `get(name_hash) != null` because if the
// specific versions of the package do not have optional
// dependencies then we cannot do this optimization without
// breaking the code.
//
// This shows up in test/integration/esbuild/esbuild.test.ts
&& PostinstallOptimizer::get_native_binlink_replacement_package_id(
resolutions,
metas,
target_cpu,
target_os,
)
.is_some()
}
PostinstallOptimizer::Ignore => true,
}
}
fn from_default(pkg_info: &PkgInfo<'_>) -> Option<PostinstallOptimizer> {
for &hash in DEFAULT_NATIVE_BINLINKS_NAME_HASHES.iter() {
if hash == pkg_info.name_hash {
return Some(PostinstallOptimizer::NativeBinlink);
}
}
for default in DEFAULT_IGNORE.iter() {
if default.name_hash == pkg_info.name_hash {
if let Some(version) = pkg_info.version {
if version.order(
default.minimum_version,
pkg_info.version_buf,
// minimum version doesn't need a string_buf because
// it doesn't use pre/build tags
b"",
) == core::cmp::Ordering::Less
{
return None;
}
}
return Some(PostinstallOptimizer::Ignore);
}
}
None
}
pub fn get(&self, pkg_info: &PkgInfo<'_>) -> Option<PostinstallOptimizer> {
if let Some(optimize) = self.dynamic.get(&pkg_info.name_hash) {
return Some(*optimize);
}
let default = Self::from_default(pkg_info)?;
match default {
PostinstallOptimizer::NativeBinlink => {
if !self.disable_default_native_binlinks {
return Some(PostinstallOptimizer::NativeBinlink);
}
}
PostinstallOptimizer::Ignore => {
if !self.disable_default_ignore {
return Some(PostinstallOptimizer::Ignore);
}
}
}
None
}
}