aube 1.38.1

Aube — a fast Node.js package manager
Documentation
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use miette::miette;

/// How an install presents user-facing output.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum InstallOutputMode {
    /// Render the normal CLI progress display and human-readable summaries.
    #[default]
    Human,
    /// Suppress direct output and deliver structured events to the reporter.
    Events,
    /// Suppress both direct output and structured output events.
    Silent,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallPhase {
    Resolving,
    Fetching,
    Linking,
    Complete,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallOutputLevel {
    Info,
    Warning,
    Error,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InstallProgressSnapshot {
    pub phase: Option<InstallPhase>,
    pub resolved: usize,
    pub total: usize,
    pub reused: usize,
    pub downloaded: usize,
    pub downloaded_bytes: u64,
    pub estimated_bytes: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InstallEvent {
    Phase(InstallPhase),
    Progress(InstallProgressSnapshot),
    Output {
        level: InstallOutputLevel,
        code: Option<String>,
        message: String,
    },
}

/// Non-blocking destination for structured install events.
///
/// Implementations that cross a runtime boundary should enqueue and return;
/// they must not wait for the consumer while holding an install worker.
pub trait InstallReporter: Send + Sync + 'static {
    fn report(&self, event: InstallEvent);
}

/// A confirmation requested by an embedded aube operation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum InstallPrompt {
    SimilarPackageName {
        package: String,
        suggested_package: String,
        popularity_rank: usize,
        edit_distance: u8,
    },
    LowDownloadPackage {
        package: String,
        weekly_downloads: u64,
        threshold: u64,
    },
    NewPackageName {
        package: String,
        created_at: String,
        minimum_age_minutes: u64,
    },
}

/// Future returned by an [`InstallPromptHandler`].
pub type InstallPromptFuture<'a> = Pin<Box<dyn Future<Output = miette::Result<bool>> + Send + 'a>>;

/// Host-provided destination for interactive confirmation requests.
///
/// Embedded aube operations never read process stdin themselves. When no
/// handler is configured, an operation that needs confirmation fails with
/// its normal structured error.
pub trait InstallPromptHandler: Send + Sync + 'static {
    fn confirm(&self, prompt: InstallPrompt) -> InstallPromptFuture<'_>;
}

#[derive(Clone)]
pub struct InstallControl {
    output: InstallOutputMode,
    reporter: Option<Arc<dyn InstallReporter>>,
    prompt_handler: Option<Arc<dyn InstallPromptHandler>>,
    cancellation: tokio_util::sync::CancellationToken,
}

impl std::fmt::Debug for InstallControl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InstallControl")
            .field("output", &self.output)
            .field("has_reporter", &self.reporter.is_some())
            .field("has_prompt_handler", &self.prompt_handler.is_some())
            .field("cancelled", &self.is_cancelled())
            .finish()
    }
}

impl Default for InstallControl {
    fn default() -> Self {
        Self {
            output: InstallOutputMode::Human,
            reporter: None,
            prompt_handler: None,
            cancellation: tokio_util::sync::CancellationToken::new(),
        }
    }
}

impl InstallControl {
    pub fn events(reporter: Arc<dyn InstallReporter>) -> Self {
        Self {
            output: InstallOutputMode::Events,
            reporter: Some(reporter),
            prompt_handler: None,
            cancellation: tokio_util::sync::CancellationToken::new(),
        }
    }

    pub fn silent() -> Self {
        Self {
            output: InstallOutputMode::Silent,
            ..Self::default()
        }
    }

    pub fn output_mode(&self) -> InstallOutputMode {
        self.output
    }

    pub fn with_prompt_handler(mut self, handler: Arc<dyn InstallPromptHandler>) -> Self {
        self.prompt_handler = Some(handler);
        self
    }

    pub fn cancel(&self) {
        self.cancellation.cancel();
    }

    pub fn is_cancelled(&self) -> bool {
        self.cancellation.is_cancelled()
    }

    pub async fn cancelled(&self) {
        self.cancellation.cancelled().await;
    }

    pub(crate) fn reporter(&self) -> Option<Arc<dyn InstallReporter>> {
        self.reporter.clone()
    }

    pub(crate) async fn confirm(&self, prompt: InstallPrompt) -> Option<miette::Result<bool>> {
        let handler = self.prompt_handler.clone()?;
        Some(tokio::select! {
            biased;
            _ = self.cancelled() => self.check_cancelled().map(|()| false),
            result = handler.confirm(prompt) => result,
        })
    }

    pub(crate) fn check_cancelled(&self) -> miette::Result<()> {
        if self.is_cancelled() {
            return Err(miette!(
                code = aube_codes::errors::ERR_AUBE_INSTALL_CANCELLED,
                "install cancelled"
            ));
        }
        Ok(())
    }

    pub(crate) fn report(&self, event: InstallEvent) {
        if let Some(reporter) = &self.reporter {
            reporter.report(event);
        }
    }

    pub(crate) fn output(
        &self,
        level: InstallOutputLevel,
        code: Option<&str>,
        message: impl Into<String>,
    ) {
        let message = message.into();
        match self.output {
            InstallOutputMode::Human => {
                let message = match level {
                    InstallOutputLevel::Info => message,
                    InstallOutputLevel::Warning => format!("warn: {message}"),
                    InstallOutputLevel::Error => format!("error: {message}"),
                };
                crate::progress::safe_eprintln(&message);
            }
            InstallOutputMode::Events => self.report(InstallEvent::Output {
                level,
                code: code.map(str::to_owned),
                message,
            }),
            InstallOutputMode::Silent => {}
        }
    }

    fn complete(&self, total: usize) {
        if self.output != InstallOutputMode::Events {
            return;
        }
        self.report(InstallEvent::Phase(InstallPhase::Complete));
        self.report(InstallEvent::Progress(InstallProgressSnapshot {
            phase: Some(InstallPhase::Complete),
            resolved: total,
            total,
            reused: total,
            downloaded: 0,
            downloaded_bytes: 0,
            estimated_bytes: 0,
        }));
    }
}

tokio::task_local! {
    static ACTIVE: InstallControl;
}

pub(crate) async fn scope<F: Future>(control: InstallControl, future: F) -> F::Output {
    ACTIVE.scope(control, future).await
}

pub(crate) fn current() -> InstallControl {
    ACTIVE.try_with(Clone::clone).unwrap_or_default()
}

pub(crate) fn check_cancelled() -> miette::Result<()> {
    current().check_cancelled()
}

pub(crate) fn output(level: InstallOutputLevel, code: Option<&str>, message: impl Into<String>) {
    current().output(level, code, message);
}

pub(crate) fn complete(total: usize) {
    current().complete(total);
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    #[derive(Default)]
    struct RecordingReporter(Mutex<Vec<InstallEvent>>);

    impl InstallReporter for RecordingReporter {
        fn report(&self, event: InstallEvent) {
            self.0.lock().unwrap().push(event);
        }
    }

    #[derive(Default)]
    struct CancelOnResolvingReporter(Mutex<Option<InstallControl>>);

    impl InstallReporter for CancelOnResolvingReporter {
        fn report(&self, event: InstallEvent) {
            if event == InstallEvent::Phase(InstallPhase::Resolving)
                && let Some(control) = self.0.lock().unwrap().as_ref()
            {
                control.cancel();
            }
        }
    }

    #[test]
    fn cloned_controls_share_cancellation_without_leaking_to_other_invocations() {
        let first = InstallControl::silent();
        let first_clone = first.clone();
        let second = InstallControl::silent();

        first.cancel();

        assert!(first_clone.is_cancelled());
        assert!(!second.is_cancelled());
    }

    #[tokio::test]
    async fn task_local_output_is_isolated_between_parallel_invocations() {
        let first = Arc::new(RecordingReporter::default());
        let second = Arc::new(RecordingReporter::default());
        let first_control = InstallControl::events(first.clone());
        let second_control = InstallControl::events(second.clone());

        let ((), ()) = tokio::join!(
            scope(first_control, async {
                output(InstallOutputLevel::Info, None, "first");
                tokio::task::yield_now().await;
                output(
                    InstallOutputLevel::Warning,
                    Some("WARN_FIRST"),
                    "first warning",
                );
            }),
            scope(second_control, async {
                tokio::task::yield_now().await;
                output(InstallOutputLevel::Info, None, "second");
            }),
        );

        let first_events = first.0.lock().unwrap();
        let second_events = second.0.lock().unwrap();
        assert_eq!(first_events.len(), 2);
        assert_eq!(second_events.len(), 1);
        assert!(matches!(
            &first_events[0],
            InstallEvent::Output { message, .. } if message == "first"
        ));
        assert!(matches!(
            &first_events[1],
            InstallEvent::Output { level: InstallOutputLevel::Warning, message, .. }
                if message == "first warning"
        ));
        assert!(matches!(
            &second_events[0],
            InstallEvent::Output { message, .. } if message == "second"
        ));
    }

    #[test]
    fn cancelled_control_returns_stable_error_code() {
        let control = InstallControl::silent();
        control.cancel();
        let error = control.check_cancelled().unwrap_err();

        assert_eq!(
            error.code().map(|code| code.to_string()).as_deref(),
            Some(aube_codes::errors::ERR_AUBE_INSTALL_CANCELLED)
        );
    }

    #[tokio::test]
    async fn reporter_can_cancel_an_install_before_resolution_work_starts() {
        let project = tempfile::tempdir().unwrap();
        std::fs::write(
            project.path().join("package.json"),
            r#"{"name":"cancel-test","version":"1.0.0","dependencies":{"dep":"file:./dep"}}"#,
        )
        .unwrap();
        std::fs::create_dir(project.path().join("dep")).unwrap();
        std::fs::write(
            project.path().join("dep/package.json"),
            r#"{"name":"dep","version":"1.0.0"}"#,
        )
        .unwrap();

        let reporter = Arc::new(CancelOnResolvingReporter::default());
        let control = InstallControl::events(reporter.clone());
        *reporter.0.lock().unwrap() = Some(control.clone());
        let mut options = super::super::InstallOptions::with_mode(super::super::FrozenMode::No);
        options.project_dir = Some(project.path().to_path_buf());
        options.dry_run = true;
        options.network_mode = aube_registry::NetworkMode::Offline;
        options.control = control;

        let error = super::super::run(options).await.unwrap_err();

        assert_eq!(
            error.code().map(|code| code.to_string()).as_deref(),
            Some(aube_codes::errors::ERR_AUBE_INSTALL_CANCELLED)
        );
        assert!(!project.path().join("node_modules").exists());
    }

    #[tokio::test]
    async fn warm_path_reports_cancellation_or_completion() {
        let project = tempfile::tempdir().unwrap();
        std::fs::write(
            project.path().join("package.json"),
            r#"{"name":"warm-test","version":"1.0.0","dependencies":{"dep":"file:./dep"}}"#,
        )
        .unwrap();
        std::fs::create_dir(project.path().join("dep")).unwrap();
        std::fs::write(
            project.path().join("dep/package.json"),
            r#"{"name":"dep","version":"1.0.0"}"#,
        )
        .unwrap();

        let mut initial = super::super::InstallOptions::with_mode(super::super::FrozenMode::No);
        initial.project_dir = Some(project.path().to_path_buf());
        initial.network_mode = aube_registry::NetworkMode::Offline;
        initial.control = InstallControl::silent();
        super::super::run(initial).await.unwrap();

        let state_path = project.path().join("node_modules/.aube-state/state.json");
        let mut state: serde_json::Value =
            serde_json::from_slice(&std::fs::read(&state_path).unwrap()).unwrap();
        state
            .as_object_mut()
            .unwrap()
            .remove("package_content_hashes");
        std::fs::write(&state_path, serde_json::to_vec(&state).unwrap()).unwrap();

        let cancelling_reporter = Arc::new(RecordingReporter::default());
        let cancelling_control = InstallControl::events(cancelling_reporter.clone());
        cancelling_control.cancel();
        let mut cancelled =
            super::super::InstallOptions::with_mode(super::super::FrozenMode::Prefer);
        cancelled.project_dir = Some(project.path().to_path_buf());
        cancelled.network_mode = aube_registry::NetworkMode::Offline;
        cancelled.control = cancelling_control;

        let error = super::super::run(cancelled).await.unwrap_err();
        assert_eq!(
            error.code().map(|code| code.to_string()).as_deref(),
            Some(aube_codes::errors::ERR_AUBE_INSTALL_CANCELLED)
        );
        assert!(cancelling_reporter.0.lock().unwrap().is_empty());

        let reporter = Arc::new(RecordingReporter::default());
        let mut completed =
            super::super::InstallOptions::with_mode(super::super::FrozenMode::Prefer);
        completed.project_dir = Some(project.path().to_path_buf());
        completed.network_mode = aube_registry::NetworkMode::Offline;
        completed.control = InstallControl::events(reporter.clone());
        super::super::run(completed).await.unwrap();

        let events = reporter.0.lock().unwrap();
        assert!(events.contains(&InstallEvent::Phase(InstallPhase::Complete)));
        assert!(events.iter().any(|event| matches!(
            event,
            InstallEvent::Progress(snapshot)
                if snapshot.phase == Some(InstallPhase::Complete)
                    && snapshot.resolved == 1
                    && snapshot.total == 1
                    && snapshot.reused == 1
        )));
    }
}