nsh-cli 0.8.0

The nsh command line tool: build, package and run apps.
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
//! The Cargo features of a native release runner that an app needs,
//! worked out from what its bundle imports ([`linked_gates`]): `nsh
//! package` builds an app's own runner with them (#1878).

use crate::web::linked_gates;

/// A runner feature and why it is kept or left out.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Choice {
    pub feature: &'static str,
    pub keep: bool,
    pub why: String,
}

/// The optional runner features and the gates ([`linked_gates`]) that
/// need each. An empty list: no import tells, so the feature is kept.
const BY_GATE: &[(&str, &[&str])] = &[
    ("shader", &["nsh_feature_shader"]),
    ("editor", &["nsh_part_editor", "nsh_part_diff"]),
    ("rich-editor", &["nsh_part_rich_editor"]),
    ("plot", &["nsh_part_plot"]),
    ("term", &["nsh_part_term"]),
    ("emoji-picker", &["nsh_part_emoji_picker"]),
    // Code blocks and formulas come with text the app shows, so any view
    // that renders Markdown or code keeps them.
    (
        "highlight",
        &[
            "nsh_part_text_view",
            "nsh_part_editor",
            "nsh_part_diff",
            "nsh_part_rich_editor",
        ],
    ),
    ("math", &["nsh_part_text_view", "nsh_part_rich_editor"]),
    ("zlib", &["nsh_std_zlib"]),
    ("crypto", &["nsh_std_crypto"]),
];

/// Features kept by the native modules an app imports by specifier.
const BY_MODULE: &[(&str, &str)] = &[("websocket", "websocket"), ("updater", "nsh/updater")];

/// Features no import tells about: an image URL or `fetch` call comes
/// from data, so nothing proves them unused.
const ALWAYS: &[(&str, &str)] = &[
    ("js", "the app has a JS bundle"),
    (
        "net",
        "`fetch` and remote images depend on data, not imports",
    ),
    ("svg", "an image's format depends on data, not imports"),
];

/// `nsh`'s exports that play sound: Linux's `sound` (GStreamer) decodes
/// and plays a `Sound`; `AudioPlayer` and `Video` play through GTK there.
const SOUND_EXPORTS: &[&str] = &["Sound"];

/// `nsh`'s exports that need Windows' `media` (Media Foundation and
/// XAudio2): sounds, players and videos all play through it.
const MEDIA_EXPORTS: &[&str] = &["Sound", "AudioPlayer", "Video"];

/// What the app's own Rust code may use that no bundle import tells
/// about ([`crate::rust_app::rust_code_use`]).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct RustCodeUse {
    /// It may make a web view (#2377).
    pub web_view: bool,
    /// It may load a sound.
    pub sound: bool,
    /// It may load a sound, or make an audio player or a video.
    pub media: bool,
}

/// The runner features for an app whose bundle uses `uses` (`None`: not
/// analyzed, keep everything), plus `keep`, features the app names
/// itself (dynamic access, or what its own Rust code needs), and those
/// `rust`, the app's own Rust code, may need.
pub(crate) fn choose(
    uses: Option<&nsh_bundler::NativeUse>,
    keep: &[String],
    rust: RustCodeUse,
) -> Vec<Choice> {
    let gates = linked_gates(uses);
    let named = |f: &str| keep.iter().any(|k| k == f);
    let mut out = Vec::new();
    for (feature, why) in ALWAYS {
        out.push(Choice {
            feature,
            keep: true,
            why: (*why).into(),
        });
    }
    for (feature, need) in BY_GATE {
        let hit = need.iter().find(|g| gates.iter().any(|x| x == *g));
        out.push(match (hit, named(feature), uses.is_none()) {
            (_, _, true) => Choice {
                feature,
                keep: true,
                why: "the bundle was not analyzed".into(),
            },
            (Some(g), _, _) => Choice {
                feature,
                keep: true,
                why: format!("the app links `{g}`"),
            },
            (None, true, _) => Choice {
                feature,
                keep: true,
                why: "the app keeps it".into(),
            },
            (None, false, _) => Choice {
                feature,
                keep: false,
                why: "nothing reachable uses it".into(),
            },
        });
    }
    for (feature, module) in BY_MODULE {
        let imported = uses.is_none_or(|u| u.computed_import || u.imports.contains_key(*module));
        // The global `WebSocket` is the `websocket` module's native side.
        let global = *feature == "websocket" && uses.is_some_and(|u| u.global_websocket);
        out.push(if global {
            Choice {
                feature,
                keep: true,
                why: "the app may use the global `WebSocket`".into(),
            }
        } else if imported {
            Choice {
                feature,
                keep: true,
                why: format!("the app may import `{module}`"),
            }
        } else if named(feature) {
            Choice {
                feature,
                keep: true,
                why: "the app keeps it".into(),
            }
        } else {
            Choice {
                feature,
                keep: false,
                why: format!("nothing imports `{module}`"),
            }
        });
    }
    // The system libraries below: kept by what the bundle cannot tell
    // apart, else by what the bundle (`found`) or the app's Rust code
    // (`by_rust`) may use, or by `nsh.json`.
    let unknown = match uses {
        None => Some("the bundle was not analyzed"),
        Some(u) if u.computed_import => Some("the app loads modules by computed names"),
        Some(_) => None,
    };
    let why = |feature: &str, found: Option<String>, by_rust: Option<&str>| {
        unknown
            .map(str::to_owned)
            .or(found)
            .or(by_rust.map(str::to_owned))
            .or_else(|| named(feature).then(|| "the app keeps it".to_owned()))
    };
    let imports = |names: &[&str]| {
        let imported = uses.and_then(|u| u.imports.get("nsh"))?;
        let name = names.iter().find(|n| imported.has(n))?;
        Some(format!("the app imports `{name}`"))
    };
    let made = |made: bool, what: &str| made.then(|| format!("the app makes a `{what}`"));
    // A web view (Linux: WebKitGTK, a large system library) only when the
    // app may make one (#2377).
    let web_view = why(
        "webview",
        made(uses.is_some_and(|u| u.web_view), "WebView"),
        rust.web_view
            .then_some("the app's Rust code may make a web view"),
    );
    // Linux's GStreamer only when the app may load a `Sound`; GTK plays
    // `AudioPlayer` and `Video` without it.
    let sound = why(
        "sound",
        imports(SOUND_EXPORTS),
        rust.sound.then_some("the app's Rust code may load a sound"),
    );
    // Windows' Media Foundation and XAudio2 only when the app may play a
    // sound, an audio player or a video.
    let media = why(
        "media",
        imports(MEDIA_EXPORTS).or_else(|| made(uses.is_some_and(|u| u.video), "Video")),
        rust.media
            .then_some("the app's Rust code may play a sound or a video"),
    );
    for (feature, why, unused) in [
        ("webview", web_view, "nothing makes a `WebView`"),
        ("sound", sound, "nothing imports `Sound`"),
        (
            "media",
            media,
            "nothing imports `Sound` or `AudioPlayer`, or makes a `Video`",
        ),
    ] {
        out.push(Choice {
            feature,
            keep: why.is_some(),
            why: why.unwrap_or_else(|| unused.into()),
        });
    }
    out
}

/// The features that are parts of `nsh/base` of their own: a Windows
/// or macOS runner linked for an app ([`crate::runner_link`]) has a part when
/// [`choose`] keeps its feature.
const PART_FEATURES: &[&str] = &["editor", "rich-editor", "plot", "term", "emoji-picker"];

/// The gates of the parts of `nsh/base` a runner linked for the app keeps:
/// those its bundle links ([`linked_gates`]), and those of the part
/// features [`choose`] keeps.
pub(crate) fn part_gates(
    uses: Option<&nsh_bundler::NativeUse>,
    keep: &[String],
    rust: RustCodeUse,
) -> Vec<String> {
    let mut gates = linked_gates(uses);
    for choice in choose(uses, keep, rust) {
        if !choice.keep || !PART_FEATURES.contains(&choice.feature) {
            continue;
        }
        let need = BY_GATE
            .iter()
            .find(|(feature, _)| *feature == choice.feature)
            .map_or(&[][..], |(_, need)| *need);
        for gate in need {
            if !gates.iter().any(|g| g == gate) {
                gates.push((*gate).to_owned());
            }
        }
    }
    gates
}

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

    fn bundle(code: &str) -> nsh_bundler::NativeUse {
        let mut uses = nsh_bundler::NativeUse::default();
        uses.add(code, |s| {
            s == "nsh" || s.starts_with("nsh/") || s == "websocket"
        });
        uses
    }

    fn web_view() -> RustCodeUse {
        RustCodeUse {
            web_view: true,
            ..Default::default()
        }
    }

    fn kept(choices: &[Choice]) -> Vec<&'static str> {
        choices
            .iter()
            .filter(|c| c.keep)
            .map(|c| c.feature)
            .collect()
    }

    #[test]
    fn a_button_app_leaves_the_large_parts_out() {
        let uses = bundle("import { Button } from 'nsh/base'; Button;");
        assert_eq!(
            kept(&choose(Some(&uses), &[], RustCodeUse::default())),
            ["js", "net", "svg"]
        );
    }

    #[test]
    fn an_import_keeps_its_feature_and_dropping_it_leaves_it_out() {
        let with = bundle("import { Editor } from 'nsh/base'; Editor;");
        let k = kept(&choose(Some(&with), &[], RustCodeUse::default()));
        assert!(k.contains(&"editor") && k.contains(&"highlight"), "{k:?}");
        let k = kept(&choose(
            Some(&bundle("import { Button } from 'nsh/base'; Button;")),
            &[],
            RustCodeUse::default(),
        ));
        assert!(!k.contains(&"editor"), "{k:?}");
    }

    #[test]
    fn a_linked_runner_keeps_the_parts_imported_or_named() {
        let button = bundle("import { Button } from 'nsh/base'; Button;");
        let gates = part_gates(Some(&button), &[], RustCodeUse::default());
        assert!(!gates.iter().any(|g| g == "nsh_part_editor"), "{gates:?}");
        let gates = part_gates(Some(&button), &["plot".into()], RustCodeUse::default());
        assert!(gates.iter().any(|g| g == "nsh_part_plot"), "{gates:?}");
        let editor = bundle("import { Editor } from 'nsh/base'; Editor;");
        let gates = part_gates(Some(&editor), &[], RustCodeUse::default());
        assert!(gates.iter().any(|g| g == "nsh_part_editor"), "{gates:?}");
        // Highlighting is kept with a `TextView`, but is not a part.
        let text = bundle("import { TextView } from 'nsh/base'; TextView;");
        let gates = part_gates(Some(&text), &[], RustCodeUse::default());
        assert!(!gates.iter().any(|g| g == "nsh_part_editor"), "{gates:?}");
    }

    #[test]
    fn text_view_keeps_highlight_and_math() {
        let k = kept(&choose(
            Some(&bundle("import { TextView } from 'nsh/base'; TextView;")),
            &[],
            RustCodeUse::default(),
        ));
        assert!(k.contains(&"highlight") && k.contains(&"math"), "{k:?}");
    }

    #[test]
    fn what_cannot_be_told_keeps_everything() {
        assert!(
            choose(None, &[], RustCodeUse::default())
                .iter()
                .all(|c| c.keep)
        );
        let k = choose(
            Some(&bundle("const m = 'nsh/' + x; import(m);")),
            &[],
            RustCodeUse::default(),
        );
        assert!(k.iter().all(|c| c.keep), "{k:?}");
    }

    #[test]
    fn the_global_websocket_keeps_websocket() {
        let uses = bundle("const ws = new WebSocket('wss://example.com'); ws;");
        assert!(uses.global_websocket);
        assert!(kept(&choose(Some(&uses), &[], RustCodeUse::default())).contains(&"websocket"));
        let uses = bundle("import { Button } from 'nsh/base'; Button;");
        assert!(!kept(&choose(Some(&uses), &[], RustCodeUse::default())).contains(&"websocket"));
    }

    #[test]
    fn modules_and_named_features_are_kept() {
        let uses = bundle("import { connect } from 'websocket'; connect;");
        let k = kept(&choose(
            Some(&uses),
            &["plot".into()],
            RustCodeUse::default(),
        ));
        assert!(
            k.contains(&"websocket") && k.contains(&"plot") && !k.contains(&"updater"),
            "{k:?}"
        );
    }

    #[test]
    fn web_view_only_when_something_makes_one() {
        let has = |c: &[Choice]| c.iter().any(|c| c.feature == "webview" && c.keep);
        let plain = bundle("import { Text } from 'nsh'; element('text');");
        assert!(!has(&choose(Some(&plain), &[], RustCodeUse::default())));
        assert!(has(&choose(Some(&plain), &[], web_view())));
        assert!(has(&choose(
            Some(&plain),
            &["webview".into()],
            RustCodeUse::default()
        )));
        assert!(has(&choose(
            Some(&bundle("element('webview', {});")),
            &[],
            RustCodeUse::default()
        )));
        assert!(has(&choose(None, &[], RustCodeUse::default())));
    }

    #[test]
    fn sound_and_media_only_when_the_app_plays_them() {
        let has = |c: &[Choice], f: &str| c.iter().any(|c| c.feature == f && c.keep);
        let none = RustCodeUse::default();
        // A button app, as sysmon's: neither GStreamer nor Media Foundation.
        let plain =
            bundle("import { Button } from 'nsh/base'; import { View } from 'nsh'; Button; View;");
        let c = choose(Some(&plain), &[], none);
        assert!(!has(&c, "sound") && !has(&c, "media"), "{c:?}");
        // `Sound` needs both.
        let c = choose(
            Some(&bundle("import { Sound } from 'nsh'; Sound.load('a.wav');")),
            &[],
            none,
        );
        assert!(has(&c, "sound") && has(&c, "media"), "{c:?}");
        // GTK plays `AudioPlayer` and `Video` on Linux: only Windows' `media`.
        for code in [
            "import { AudioPlayer } from 'nsh'; new AudioPlayer('a.mp3');",
            "import { Video } from 'nsh'; f(Video);",
            "_$element('video', { src: 'a.mp4' });",
        ] {
            let c = choose(Some(&bundle(code)), &[], none);
            assert!(!has(&c, "sound") && has(&c, "media"), "{code}: {c:?}");
        }
        // A namespace used whole may load a sound.
        let c = choose(
            Some(&bundle("import * as n from 'nsh'; use(n);")),
            &[],
            none,
        );
        assert!(has(&c, "sound") && has(&c, "media"), "{c:?}");
        // What cannot be told, the app's Rust code and `nsh.json` keep them.
        for c in [
            choose(None, &[], none),
            choose(Some(&bundle("import(name);")), &[], none),
            choose(
                Some(&plain),
                &[],
                RustCodeUse {
                    sound: true,
                    media: true,
                    ..Default::default()
                },
            ),
            choose(Some(&plain), &["sound".into(), "media".into()], none),
        ] {
            assert!(has(&c, "sound") && has(&c, "media"), "{c:?}");
        }
        let c = choose(
            Some(&plain),
            &[],
            RustCodeUse {
                media: true,
                ..Default::default()
            },
        );
        assert!(!has(&c, "sound") && has(&c, "media"), "{c:?}");
    }
}