nsh-cli 0.1.1

The nsh command line tool: build, package and run apps.
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use std::{
    cell::Cell,
    net::{Ipv4Addr, SocketAddr},
    path::{Path, PathBuf},
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, Ordering},
        mpsc,
    },
    time::{Duration, Instant},
};

use anyhow::Context as _;
use notify::{RecursiveMode, Watcher};
use nsh_dev::{
    ClientEvent, DevServer,
    protocol::{HostPlatform, WS_PATH},
};

use crate::{build, logger, ui};

/// File events arriving within this window are coalesced into one rebuild.
const DEBOUNCE: Duration = Duration::from_millis(30);
/// Ports tried after the requested one when it is taken.
const PORT_ATTEMPTS: u16 = 10;

/// A running dev server plus the source watcher feeding it.
pub(crate) struct DevSession {
    entry: PathBuf,
    src_dir: PathBuf,
    release: bool,
    server: DevServer,
    devices: Arc<Devices>,
    /// The modules of the last good development build: updates are
    /// computed against it.
    hot: std::cell::RefCell<Option<nsh_bundler::HotBundle>>,
    /// Good builds so far: the first one is "built", the next "rebuilt".
    builds: Cell<u32>,
}

impl DevSession {
    /// Starts the server and publishes the first build (or its error).
    pub(crate) fn start(entry: &Path, port: u16, release: bool) -> anyhow::Result<DevSession> {
        let entry = dunce::canonicalize(entry)
            .with_context(|| format!("entry not found: {}", entry.display()))?;
        let src_dir = entry
            .parent()
            .context("entry has no directory")?
            .to_path_buf();
        let devices = Arc::<Devices>::default();
        let server = start_server(port, &src_dir, devices.clone())?;
        server.serve_files(crate::build::crate_assets().map(|a| (a.path, a.source)));
        server.install_commands(install_commands(&src_dir));
        // Browsers too, when there is a web runtime to serve (`nsh run
        // --target web` builds it in a checkout).
        if let Ok(Some(runtime)) = crate::runtime::web(crate::runtime::WebKind::Dev, false) {
            let (page, files) = crate::web::dev_host(&runtime)?;
            server.serve_web(page, files);
        }
        let session = DevSession {
            entry,
            src_dir,
            release,
            server,
            devices,
            hot: Default::default(),
            builds: Cell::new(0),
        };
        session.rebuild();
        Ok(session)
    }

    /// The port the dev server listens on.
    pub(crate) fn port(&self) -> u16 {
        self.server.local_addr().port()
    }

    /// WebSocket URL for hosts on this machine.
    pub(crate) fn local_url(&self) -> String {
        format!(
            "ws://127.0.0.1:{}{WS_PATH}",
            self.server.local_addr().port()
        )
    }

    /// The web host's page for browsers on this machine.
    pub(crate) fn web_url(&self) -> String {
        format!("http://127.0.0.1:{}/", self.server.local_addr().port())
    }

    /// Prints where hosts can connect.
    pub(crate) fn print_urls(&self) {
        let port = self.server.local_addr().port();
        ui::note(format!("dev server {}", self.local_url()));
        let web = self.server.serves_web();
        if web {
            ui::detail(format!("browsers: {}", self.web_url()));
        }
        for ip in lan_ips() {
            ui::detail(format!(
                "devices on this network: ws://{ip}:{port}{WS_PATH}"
            ));
            if web {
                ui::detail(format!("browsers on this network: http://{ip}:{port}/"));
            }
        }
    }

    /// Rebuilds on source changes until `stop` returns true.
    pub(crate) fn watch(&self, stop: impl FnMut() -> anyhow::Result<bool>) -> anyhow::Result<()> {
        let result = self.watch_until(stop);
        // The session is over: its hosts going away (stopped by the
        // command, or with the server) is no news.
        self.devices.closing.store(true, Ordering::Relaxed);
        result
    }

    fn watch_until(&self, mut stop: impl FnMut() -> anyhow::Result<bool>) -> anyhow::Result<()> {
        let (tx, rx) = mpsc::channel();
        let mut watcher =
            notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
                if let Ok(event) = event {
                    let _ = tx.send(event);
                }
            })?;
        watcher.watch(&self.src_dir, RecursiveMode::Recursive)?;
        ui::debug(format!("watching {}", ui::path(&self.src_dir)));
        // The app's Rust code: a change builds the app's hosts again.
        let rust = crate::rust_app::current();
        if let Some(app) = &rust
            && !app.crate_dir().starts_with(&self.src_dir)
        {
            watcher.watch(app.crate_dir(), RecursiveMode::Recursive)?;
        }
        loop {
            if ui::stop_requested() || stop()? {
                return Ok(());
            }
            let Ok(event) = rx.recv_timeout(Duration::from_millis(200)) else {
                continue;
            };
            // inotify reports reads too (FSEvents does not): the build's
            // own would start the next one, forever.
            if matches!(event.kind, notify::EventKind::Access(_)) {
                continue;
            }
            if let Some(app) = &rust
                && event.paths.iter().any(|p| app.is_rust(p))
            {
                return Err(crate::rust_app::Restart.into());
            }
            if !is_source(&event) {
                continue;
            }
            let deadline = Instant::now() + DEBOUNCE;
            while rx
                .recv_timeout(deadline.saturating_duration_since(Instant::now()))
                .is_ok()
            {}
            self.rebuild();
        }
    }

    fn rebuild(&self) {
        self.server.building();
        if self.release {
            match build::compile(&self.entry, true) {
                Ok(built) => {
                    let capabilities = built.capabilities_json();
                    let summary = self.summary(&built);
                    self.server.publish(
                        built.bytecode,
                        built.code,
                        built.source_map,
                        capabilities,
                        None,
                        built.ui_sets,
                    );
                    self.published(summary);
                }
                Err(err) => self.failed(err),
            }
            return;
        }
        match build::compile_hot(&self.entry) {
            Ok((built, hot)) => {
                let summary = self.summary(&built);
                // Other capabilities: the app starts again under them.
                let patch = self
                    .hot
                    .borrow()
                    .as_ref()
                    .filter(|previous| previous.capabilities == hot.capabilities)
                    .map(|previous| hot.patch(previous));
                let patch = match patch {
                    Some(nsh_bundler::HotPatch::Update { code, source_map }) => {
                        Some((code, source_map))
                    }
                    // Nothing to replace; hosts still clear a build error.
                    Some(nsh_bundler::HotPatch::Unchanged) => Some(("true".to_owned(), None)),
                    Some(nsh_bundler::HotPatch::Reload) | None => None,
                };
                let capabilities = built.capabilities_json();
                self.server.publish(
                    built.bytecode,
                    built.code,
                    built.source_map,
                    capabilities,
                    patch,
                    built.ui_sets,
                );
                *self.hot.borrow_mut() = Some(hot);
                self.published(summary);
            }
            Err(err) => self.failed(err),
        }
    }

    /// What the line of a good build says after its path.
    fn summary(&self, built: &build::Built) -> String {
        if self.builds.get() == 0 {
            built.summary("")
        } else {
            ui::dim(format!("({})", ui::duration(built.elapsed)))
        }
    }

    /// `✓ built App.tsx (...)`, then `✓ rebuilt App.tsx (18ms), 2 devices`.
    fn published(&self, summary: String) {
        let entry = ui::path(&self.entry);
        let builds = self.builds.get();
        self.builds.set(builds + 1);
        if builds == 0 {
            return ui::done(format!("built {entry} {summary}"));
        }
        let devices = match self.server.clients() {
            0 => String::new(),
            1 => ", 1 device".to_owned(),
            n => format!(", {n} devices"),
        };
        ui::done(format!("rebuilt {entry} {summary}{}", ui::dim(devices)));
    }

    fn failed(&self, err: build::BuildError) {
        let plain = err.plain.clone();
        ui::report(&err.into_error(&self.entry));
        self.server.publish_error(plain);
    }
}

impl Drop for DevSession {
    fn drop(&mut self) {
        // The server stops next.
        self.devices.closing.store(true, Ordering::Relaxed);
    }
}

/// The hosts connected to the dev server: what they do is shown as `•`
/// lines, their log lines with their names.
#[derive(Default)]
struct Devices {
    connected: Mutex<Vec<String>>,
    /// The session ends.
    closing: AtomicBool,
}

impl Devices {
    fn connected(&self) -> std::sync::MutexGuard<'_, Vec<String>> {
        self.connected.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn on_event(&self, event: ClientEvent) {
        match event {
            ClientEvent::Connected { device, build } => {
                let build = build.map_or(String::new(), |b| format!(", build {b}"));
                ui::event(format!("{device} connected{build}"));
                self.connected().push(device);
            }
            ClientEvent::CannotRun {
                device,
                missing,
                install,
            } => {
                let label = short_name(&self.connected(), &device);
                let install =
                    install.map_or(String::new(), |c| format!(": {c} installs this app's host"));
                ui::warn(format!(
                    "{label}: its host has no {} (it was built for another app){install}",
                    missing.join(", ")
                ));
            }
            ClientEvent::Disconnected { device } => {
                {
                    let mut connected = self.connected();
                    if let Some(i) = connected.iter().position(|d| *d == device) {
                        connected.remove(i);
                    }
                }
                if !self.closing.load(Ordering::Relaxed) {
                    ui::event(ui::dim(format!("{device} disconnected")));
                }
            }
            ClientEvent::Log(log) => {
                let (label, several) = {
                    let connected = self.connected();
                    (short_name(&connected, &log.device), connected.len() > 1)
                };
                let device = logger::Device {
                    name: &log.device,
                    label: &label,
                    several,
                };
                logger::device(&device, log.level, &log.target, &log.message);
            }
        }
    }
}

/// `device` without its model ("iOS Simulator (iPhone 16 Plus)": "iOS
/// Simulator"), unless another connected device has that name too.
fn short_name(connected: &[String], device: &str) -> String {
    let short = |name: &str| name.split_once(" (").map_or(name, |(s, _)| s).to_owned();
    let name = short(device);
    let shared = connected
        .iter()
        .any(|other| other != device && short(other) == name);
    if shared { device.to_owned() } else { name }
}

/// The commands that install the app in `dir`'s host, for the platforms
/// `nsh run` has a target for.
fn install_commands(dir: &Path) -> Vec<(HostPlatform, String)> {
    let app = match ui::path(dir) {
        dot if dot == "." => String::new(),
        path if path.contains(' ') => format!(" '{path}'"),
        path => format!(" {path}"),
    };
    use crate::run::Target;
    // The target this machine's `nsh run` defaults to goes without a flag.
    let run = |target: Target, name: &str| {
        if target == Target::native() {
            format!("nsh run{app}")
        } else {
            format!("nsh run{app} --target {name}")
        }
    };
    vec![
        (HostPlatform::MacCatalyst, run(Target::Catalyst, "catalyst")),
        (HostPlatform::MacOs, run(Target::Macos, "macos")),
        (HostPlatform::Linux, run(Target::Linux, "linux")),
        (HostPlatform::Windows, run(Target::Windows, "windows")),
        (HostPlatform::Gpui, run(Target::Gpui, "gpui")),
        (HostPlatform::IosSimulator, run(Target::Ios, "ios")),
        (HostPlatform::Android, run(Target::Android, "android")),
        (HostPlatform::Web, run(Target::Web, "web")),
    ]
}

fn start_server(port: u16, assets_root: &Path, devices: Arc<Devices>) -> anyhow::Result<DevServer> {
    let mut last_err = None;
    for port in port..port.saturating_add(PORT_ATTEMPTS) {
        let addr = SocketAddr::from(([0, 0, 0, 0], port));
        let root = Some(assets_root.to_path_buf());
        let devices = devices.clone();
        match DevServer::start(addr, nsh_js::BYTECODE_VERSION, root, move |event| {
            devices.on_event(event)
        }) {
            Ok(server) => return Ok(server),
            Err(err) => last_err = Some(err),
        }
    }
    Err(last_err.expect("at least one attempt")).context("start dev server")
}

fn is_source(event: &notify::Event) -> bool {
    event.paths.iter().any(|p| {
        !p.components()
            .any(|c| c.as_os_str() == ".nsh" || c.as_os_str() == "node_modules")
            && matches!(
                p.extension().and_then(|e| e.to_str()),
                Some("ts" | "tsx" | "js" | "jsx" | "json")
            )
    })
}

/// Private IPv4 addresses of physical network interfaces (`en*`; on Linux
/// also `eth*` and Wi-Fi's `wl*`), where devices on the same Wi-Fi or
/// Ethernet can reach this machine. VPN and proxy tunnels are skipped.
#[cfg(unix)]
fn lan_ips() -> Vec<Ipv4Addr> {
    let mut ips = Vec::new();
    let mut list: *mut libc::ifaddrs = std::ptr::null_mut();
    // SAFETY: getifaddrs fills a linked list that we walk read-only and free
    // with freeifaddrs.
    unsafe {
        if libc::getifaddrs(&mut list) != 0 {
            return ips;
        }
        let mut cur = list;
        while let Some(ifa) = cur.as_ref() {
            cur = ifa.ifa_next;
            if ifa.ifa_addr.is_null() || i32::from((*ifa.ifa_addr).sa_family) != libc::AF_INET {
                continue;
            }
            let name = std::ffi::CStr::from_ptr(ifa.ifa_name).to_string_lossy();
            if !["en", "eth", "wl"].iter().any(|p| name.starts_with(p)) {
                continue;
            }
            let addr = &*(ifa.ifa_addr as *const libc::sockaddr_in);
            let ip = Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr));
            if ip.is_private() && !ips.contains(&ip) {
                ips.push(ip);
            }
        }
        libc::freeifaddrs(list);
    }
    ips
}

/// Private IPv4 addresses of the Ethernet and Wi-Fi adapters that are up
/// (by Windows' interface types: VPN, Hyper-V and WSL adapters are of
/// other types).
#[cfg(windows)]
fn lan_ips() -> Vec<Ipv4Addr> {
    use windows_sys::Win32::{
        Foundation::ERROR_BUFFER_OVERFLOW,
        NetworkManagement::{
            IpHelper::{
                GAA_FLAG_SKIP_ANYCAST, GAA_FLAG_SKIP_DNS_SERVER, GAA_FLAG_SKIP_MULTICAST,
                GetAdaptersAddresses, IF_TYPE_ETHERNET_CSMACD, IF_TYPE_IEEE80211,
                IP_ADAPTER_ADDRESSES_LH,
            },
            Ndis::IfOperStatusUp,
        },
        Networking::WinSock::{AF_INET, SOCKADDR_IN},
    };

    let mut ips = Vec::new();
    let flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
    // u64s keep the structures written into the buffer aligned.
    let mut buffer: Vec<u64> = vec![0; 2048];
    loop {
        let mut size = (buffer.len() * 8) as u32;
        // SAFETY: the buffer holds `size` writable bytes.
        let result = unsafe {
            GetAdaptersAddresses(
                u32::from(AF_INET),
                flags,
                std::ptr::null(),
                buffer.as_mut_ptr().cast(),
                &mut size,
            )
        };
        if result == ERROR_BUFFER_OVERFLOW {
            buffer = vec![0; (size as usize).div_ceil(8)];
            continue;
        }
        if result != 0 {
            return ips;
        }
        break;
    }
    let mut adapter = buffer.as_ptr().cast::<IP_ADAPTER_ADDRESSES_LH>();
    // SAFETY: the call filled the buffer with a linked list of adapters and
    // their addresses, all inside it, walked read-only while it lives.
    unsafe {
        while let Some(a) = adapter.as_ref() {
            adapter = a.Next;
            if a.OperStatus != IfOperStatusUp
                || !matches!(a.IfType, IF_TYPE_ETHERNET_CSMACD | IF_TYPE_IEEE80211)
            {
                continue;
            }
            let mut unicast = a.FirstUnicastAddress;
            while let Some(u) = unicast.as_ref() {
                unicast = u.Next;
                let addr = u.Address.lpSockaddr;
                if addr.is_null() || (*addr).sa_family != AF_INET {
                    continue;
                }
                let addr = &*(addr as *const SOCKADDR_IN);
                let ip = Ipv4Addr::from(u32::from_be(addr.sin_addr.S_un.S_addr));
                if ip.is_private() && !ips.contains(&ip) {
                    ips.push(ip);
                }
            }
        }
    }
    ips
}

#[cfg(test)]
mod tests {
    #[test]
    fn devices_go_by_short_names_unless_two_share_one() {
        let connected = [
            "iOS Simulator (iPhone 16 Plus)".to_owned(),
            "Mac Catalyst".to_owned(),
        ];
        assert_eq!(
            super::short_name(&connected, &connected[0]),
            "iOS Simulator"
        );
        assert_eq!(
            super::short_name(&connected, "Mac Catalyst"),
            "Mac Catalyst"
        );
        let two = [
            "iOS Simulator (iPhone 16 Plus)".to_owned(),
            "iOS Simulator (iPad Air)".to_owned(),
        ];
        assert_eq!(super::short_name(&two, &two[1]), "iOS Simulator (iPad Air)");
    }
}