xtask-wasm 0.6.0

Customizable subcommands to build your Wasm projects using xtask.
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
use crate::{
    anyhow::{bail, ensure, Context, Result},
    camino::Utf8Path,
    clap, xtask_command, Dist, Watch,
};
use derive_more::Debug;
use std::{
    ffi, fs,
    io::prelude::*,
    net::{IpAddr, Ipv4Addr, SocketAddr, TcpListener, TcpStream},
    path::{Path, PathBuf},
    process,
    sync::Arc,
    thread,
};

type RequestHandler = Arc<dyn Fn(Request) -> Result<()> + Send + Sync + 'static>;

/// A type that can produce a [`process::Command`] given the final [`DevServer`] configuration.
///
/// Implement this trait to build a command whose arguments or environment depend on the server's
/// configuration — for example to pass `--dist-dir`, `--port`, or other runtime values.
///
/// A blanket implementation is provided for [`process::Command`] itself, so existing call sites
/// that pass a plain command continue to work without any changes.
///
/// # Examples
///
/// ```rust,no_run
/// use std::process;
/// use xtask_wasm::{anyhow::Result, clap, DevServer, Hook};
///
/// struct NotifyOnPort;
///
/// impl Hook for NotifyOnPort {
///     fn build_command(self: Box<Self>, server: &DevServer) -> process::Command {
///         let mut cmd = process::Command::new("notify-send");
///         cmd.arg(format!("dev server on port {}", server.port));
///         cmd
///     }
/// }
///
/// #[derive(clap::Parser)]
/// enum Opt {
///     Start(xtask_wasm::DevServer),
/// }
///
/// fn main() -> Result<()> {
///     let opt: Opt = clap::Parser::parse();
///
///     match opt {
///         Opt::Start(dev_server) => {
///             dev_server
///                 .xtask("dist")
///                 .post(NotifyOnPort)
///                 .start()?;
///         }
///     }
///
///     Ok(())
/// }
/// ```
pub trait Hook {
    /// Construct the [`process::Command`] to run, using `server` as context.
    fn build_command(self: Box<Self>, server: &DevServer) -> process::Command;
}

impl Hook for process::Command {
    fn build_command(self: Box<Self>, _server: &DevServer) -> process::Command {
        *self
    }
}

/// Abstraction over an HTTP request.
#[derive(Debug)]
#[non_exhaustive]
pub struct Request<'a> {
    /// TCP stream of the request.
    pub stream: &'a mut TcpStream,
    /// Path of the request.
    pub path: &'a str,
    /// Request header.
    pub header: &'a str,
    /// Path to the distributed directory.
    pub dist_dir: &'a Path,
    /// Path to the file used when the requested file cannot be found for the default request
    /// handler.
    pub not_found_path: Option<&'a Path>,
}

/// A simple HTTP server useful during development.
///
/// It can watch the source code for changes and restart a provided [`command`](Self::command).
///
/// Serve the file from the provided [`dist_dir`](Self::dist_dir) at a given IP address
/// (127.0.0.1:8000 by default). An optional command can be provided to restart the build when
/// changes are detected using [`command`](Self::command), [`xtask`](Self::xtask) or
/// [`cargo`](Self::cargo).
///
/// # Usage
///
/// ```rust,no_run
/// use std::process;
/// use xtask_wasm::{
///     anyhow::Result,
///     clap,
/// };
///
/// #[derive(clap::Parser)]
/// enum Opt {
///     Start(xtask_wasm::DevServer),
///     Dist,
/// }
///
/// fn main() -> Result<()> {
///     let opt: Opt = clap::Parser::parse();
///
///     match opt {
///         Opt::Dist => todo!("build project"),
///         Opt::Start(dev_server) => {
///             log::info!("Starting the development server...");
///             dev_server
///                 .xtask("dist")
///                 .start()?;
///         }
///     }
///
///     Ok(())
/// }
/// ```
///
/// This adds a `start` subcommand that will run `cargo xtask dist`, watching for
/// changes in the workspace and serve the files in the default dist directory
/// (`target/debug/dist`) at the default IP address.
#[non_exhaustive]
#[derive(Debug, clap::Parser)]
#[clap(
    about = "A simple HTTP server useful during development.",
    long_about = "A simple HTTP server useful during development.\n\
        It can watch the source code for changes."
)]
pub struct DevServer {
    /// IP address to bind. Default to `127.0.0.1`.
    #[clap(long, default_value = "127.0.0.1")]
    pub ip: IpAddr,
    /// Port number. Default to `8000`.
    #[clap(long, default_value = "8000")]
    pub port: u16,

    /// Watch configuration for detecting file-system changes.
    ///
    /// Controls which paths are watched, debounce timing, and other watch
    /// behaviour. Watching is only active when at least one of `pre_hooks`,
    /// `command`, or `post_hooks` is set; if none are provided the watch
    /// thread is not started.
    #[clap(flatten)]
    pub watch: Watch,

    /// Directory of all generated artifacts.
    #[clap(skip)]
    pub dist_dir: Option<PathBuf>,

    /// Commands executed before the main command when a change is detected.
    #[clap(skip)]
    #[debug(skip)]
    pub pre_hooks: Vec<Box<dyn Hook>>,

    /// Main command executed when a change is detected.
    #[clap(skip)]
    pub command: Option<process::Command>,

    /// Commands executed after the main command when a change is detected.
    #[clap(skip)]
    #[debug(skip)]
    pub post_hooks: Vec<Box<dyn Hook>>,

    /// Use another file path when the URL is not found.
    #[clap(skip)]
    pub not_found_path: Option<PathBuf>,

    /// Pass a custom request handler.
    #[clap(skip)]
    #[debug(skip)]
    request_handler: Option<RequestHandler>,
}

impl DevServer {
    /// Set the dev-server binding address.
    pub fn address(mut self, ip: IpAddr, port: u16) -> Self {
        self.ip = ip;
        self.port = port;

        self
    }

    /// Set the directory for the generated artifacts.
    ///
    /// The default is `target/debug/dist`.
    pub fn dist_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.dist_dir = Some(path.into());
        self
    }

    /// Add a command to execute before the main command when a change is detected.
    pub fn pre(mut self, command: impl Hook + 'static) -> Self {
        self.pre_hooks.push(Box::new(command));
        self
    }

    /// Add multiple commands to execute before the main command when a change is detected.
    pub fn pres(mut self, commands: impl IntoIterator<Item = impl Hook + 'static>) -> Self {
        self.pre_hooks
            .extend(commands.into_iter().map(|c| Box::new(c) as Box<dyn Hook>));
        self
    }

    /// Add a command to execute after the main command when a change is detected.
    pub fn post(mut self, command: impl Hook + 'static) -> Self {
        self.post_hooks.push(Box::new(command));
        self
    }

    /// Add multiple commands to execute after the main command when a change is detected.
    pub fn posts(mut self, commands: impl IntoIterator<Item = impl Hook + 'static>) -> Self {
        self.post_hooks
            .extend(commands.into_iter().map(|c| Box::new(c) as Box<dyn Hook>));
        self
    }

    /// Main command executed when a change is detected.
    ///
    /// See [`xtask`](Self::xtask) if you want to use an `xtask` command.
    pub fn command(mut self, command: process::Command) -> Self {
        self.command = Some(command);
        self
    }

    /// Name of the main xtask command that is executed when a change is detected.
    ///
    /// See [`command`](Self::command) to use an arbitrary command.
    pub fn xtask(mut self, name: impl AsRef<str>) -> Self {
        let mut command = xtask_command();
        command.arg(name.as_ref());
        self.command = Some(command);
        self
    }

    /// Cargo subcommand executed as the main command when a change is detected.
    ///
    /// See [`xtask`](Self::xtask) for xtask commands or [`command`](Self::command) for arbitrary
    /// commands.
    pub fn cargo(mut self, subcommand: impl AsRef<str>) -> Self {
        let mut command = process::Command::new("cargo");
        command.arg(subcommand.as_ref());
        self.command = Some(command);
        self
    }

    /// Adds an argument to the main command executed when changes are detected.
    ///
    /// # Panics
    ///
    /// Panics if called before [`command`](Self::command), [`xtask`](Self::xtask) or
    /// [`cargo`](Self::cargo).
    pub fn arg<S: AsRef<ffi::OsStr>>(mut self, arg: S) -> Self {
        self.command
            .as_mut()
            .expect("`arg` called without a command set; call `command`, `xtask` or `cargo` first")
            .arg(arg);
        self
    }

    /// Adds multiple arguments to the main command executed when changes are detected.
    ///
    /// # Panics
    ///
    /// Panics if called before [`command`](Self::command), [`xtask`](Self::xtask) or
    /// [`cargo`](Self::cargo).
    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<ffi::OsStr>,
    {
        self.command
            .as_mut()
            .expect("`args` called without a command set; call `command`, `xtask` or `cargo` first")
            .args(args);
        self
    }

    /// Inserts or updates an environment variable for the main command executed when changes are
    /// detected.
    ///
    /// # Panics
    ///
    /// Panics if called before [`command`](Self::command), [`xtask`](Self::xtask) or
    /// [`cargo`](Self::cargo).
    pub fn env<K, V>(mut self, key: K, val: V) -> Self
    where
        K: AsRef<ffi::OsStr>,
        V: AsRef<ffi::OsStr>,
    {
        self.command
            .as_mut()
            .expect("`env` called without a command set; call `command`, `xtask` or `cargo` first")
            .env(key, val);
        self
    }

    /// Inserts or updates multiple environment variables for the main command executed when
    /// changes are detected.
    ///
    /// # Panics
    ///
    /// Panics if called before [`command`](Self::command), [`xtask`](Self::xtask) or
    /// [`cargo`](Self::cargo).
    pub fn envs<I, K, V>(mut self, vars: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<ffi::OsStr>,
        V: AsRef<ffi::OsStr>,
    {
        self.command
            .as_mut()
            .expect("`envs` called without a command set; call `command`, `xtask` or `cargo` first")
            .envs(vars);
        self
    }

    /// Use another file path when the URL is not found.
    pub fn not_found_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.not_found_path.replace(path.into());
        self
    }

    /// Pass a custom request handler to the dev server.
    pub fn request_handler<F>(mut self, handler: F) -> Self
    where
        F: Fn(Request) -> Result<()> + Send + Sync + 'static,
    {
        self.request_handler.replace(Arc::new(handler));
        self
    }

    /// Start the server, serving the files at [`dist_dir`](Self::dist_dir).
    ///
    /// If `dist_dir` has not been provided, [`Dist::default_debug_dir`] will be used.
    pub fn start(mut self) -> Result<()> {
        // Resolve dist_dir early so Hooks can observe the final value via &self.
        if self.dist_dir.is_none() {
            self.dist_dir = Some(Dist::default_debug_dir().into());
        }
        let dist_dir = self.dist_dir.clone().unwrap();

        let watch_process = {
            // mem::take so we can pass &self to build_command while the fields are empty.
            let pre_hooks = std::mem::take(&mut self.pre_hooks);
            let post_hooks = std::mem::take(&mut self.post_hooks);
            let main_command = self.command.take();

            let mut commands: Vec<process::Command> = pre_hooks
                .into_iter()
                .map(|p| p.build_command(&self))
                .collect();
            if let Some(command) = main_command {
                commands.push(command);
            }
            commands.extend(post_hooks.into_iter().map(|p| p.build_command(&self)));

            if !commands.is_empty() {
                // NOTE: the path needs to exists in order to be excluded because it is canonicalize
                std::fs::create_dir_all(&dist_dir).with_context(|| {
                    format!("cannot create dist directory `{}`", dist_dir.display())
                })?;
                let watch = self.watch.exclude_path(&dist_dir);
                let handle = std::thread::spawn(|| match watch.run(commands) {
                    Ok(()) => log::trace!("Starting to watch"),
                    Err(err) => log::error!("an error occurred when starting to watch: {err}"),
                });

                Some(handle)
            } else {
                None
            }
        };

        if let Some(handler) = self.request_handler {
            serve(self.ip, self.port, dist_dir, self.not_found_path, handler)
                .context("an error occurred when starting to serve")?;
        } else {
            serve(
                self.ip,
                self.port,
                dist_dir,
                self.not_found_path,
                Arc::new(default_request_handler),
            )
            .context("an error occurred when starting to serve")?;
        }

        if let Some(handle) = watch_process {
            handle.join().expect("an error occurred when exiting watch");
        }

        Ok(())
    }
}

impl Default for DevServer {
    fn default() -> DevServer {
        DevServer {
            ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
            port: 8000,
            watch: Default::default(),
            dist_dir: None,
            pre_hooks: Default::default(),
            command: None,
            post_hooks: Default::default(),
            not_found_path: None,
            request_handler: None,
        }
    }
}

fn serve(
    ip: IpAddr,
    port: u16,
    dist_dir: PathBuf,
    not_found_path: Option<PathBuf>,
    handler: RequestHandler,
) -> Result<()> {
    let address = SocketAddr::new(ip, port);
    let listener = TcpListener::bind(address).context("cannot bind to the given address")?;

    log::info!("Development server running at: http://{}", &address);

    macro_rules! warn_not_fail {
        ($expr:expr) => {{
            match $expr {
                Ok(res) => res,
                Err(err) => {
                    log::warn!("Malformed request's header: {}", err);
                    return;
                }
            }
        }};
    }

    for mut stream in listener.incoming().filter_map(Result::ok) {
        let handler = handler.clone();
        let dist_dir = dist_dir.clone();
        let not_found_path = not_found_path.clone();
        thread::spawn(move || {
            let header = warn_not_fail!(read_header(&stream));
            let request = Request {
                stream: &mut stream,
                header: header.as_ref(),
                path: warn_not_fail!(parse_request_path(&header)),
                dist_dir: dist_dir.as_ref(),
                not_found_path: not_found_path.as_deref(),
            };

            (handler)(request).unwrap_or_else(|e| {
                let _ = stream.write("HTTP/1.1 500 INTERNAL SERVER ERROR\r\n\r\n".as_bytes());
                log::error!("an error occurred: {e}");
            });
        });
    }

    Ok(())
}

fn read_header(mut stream: &TcpStream) -> Result<String> {
    let mut header = Vec::with_capacity(64 * 1024);
    let mut peek_buffer = [0u8; 4096];

    loop {
        let n = stream.peek(&mut peek_buffer)?;
        ensure!(n > 0, "Unexpected EOF");

        let data = &mut peek_buffer[..n];
        if let Some(i) = data.windows(4).position(|x| x == b"\r\n\r\n") {
            let data = &mut peek_buffer[..(i + 4)];
            stream.read_exact(data)?;
            header.extend(&*data);
            break;
        } else {
            stream.read_exact(data)?;
            header.extend(&*data);
        }
    }

    Ok(String::from_utf8(header)?)
}

fn parse_request_path(header: &str) -> Result<&str> {
    let content = header.split('\r').next().unwrap();
    let requested_path = content
        .split_whitespace()
        .nth(1)
        .context("could not find path in request")?;
    Ok(requested_path
        .split_once('?')
        .map(|(prefix, _suffix)| prefix)
        .unwrap_or(requested_path))
}

/// Default request handler
pub fn default_request_handler(request: Request) -> Result<()> {
    let requested_path = request.path;

    log::debug!("<-- {requested_path}");

    let rel_path = Path::new(requested_path.trim_matches('/'));
    let mut full_path = request.dist_dir.join(rel_path);

    if full_path.is_dir() {
        if full_path.join("index.html").exists() {
            full_path = full_path.join("index.html")
        } else if full_path.join("index.htm").exists() {
            full_path = full_path.join("index.htm")
        } else {
            bail!("no index.html in {}", full_path.display());
        }
    }

    if let Some(path) = request.not_found_path {
        if !full_path.is_file() {
            full_path = request.dist_dir.join(path);
        }
    }

    if full_path.is_file() {
        log::debug!("--> {}", full_path.display());
        let full_path_extension = Utf8Path::from_path(&full_path)
            .context("request path contains non-utf8 characters")?
            .extension();

        let content_type = match full_path_extension {
            Some("html") => "text/html;charset=utf-8",
            Some("css") => "text/css;charset=utf-8",
            Some("js") => "application/javascript",
            Some("wasm") => "application/wasm",
            _ => "application/octet-stream",
        };

        request
            .stream
            .write(
                format!(
                    "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nContent-Type: {}\r\n\r\n",
                    full_path.metadata()?.len(),
                    content_type,
                )
                .as_bytes(),
            )
            .context("cannot write response")?;

        std::io::copy(&mut fs::File::open(&full_path)?, request.stream)?;
    } else {
        log::error!("--> {} (404 NOT FOUND)", full_path.display());
        request
            .stream
            .write("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes())
            .context("cannot write response")?;
    }

    Ok(())
}