serve

    Minimal static HTTP file server for MoonX.

    command-line
    moonx
    wasm
    Download zip
    Author
    Version
    0.1.3
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    46

    #serve

    Minimal static HTTP file server, implemented in pure MoonBit with moonbitlang/async@0.22.9. Run the latest published release with MoonX:

    moonx cli/serve@latest -- public

    Version: cli/serve@0.1.3, reporting serve 0.1.3 with --version. This version keeps successful file-response connections available for reuse. The immutable 0.1.2 release closes those connections; the earlier 0.1.1 package incorrectly reports version 0.1.0.

    Usage: serve [--host ADDRESS] [-p PORT|--port PORT] [DIRECTORY]. The directory defaults to ., the address to 127.0.0.1, and the port to 8000. Use a numeric IPv4/IPv6 address; port 0 selects an available port. The actual address is printed to stderr. --port=8000 and -p8000 are also accepted, each option may be supplied once, and -- ends option parsing. Use -h or --help for usage and -V or --version for version information. Metadata requests exit without reading stdin or starting a listener. Argument syntax errors print the standard parser diagnostic with usage to stderr and exit 1.

    GET streams regular files with Content-Length and an extension-based MIME type; HEAD sends the same headers without a body. Unknown extensions use application/octet-stream; responses disable MIME sniffing. Directories serve index.html, redirecting to a trailing slash when needed; absent files/indexes return 404. Other methods return 405. There is no directory listing, SPA fallback, upload, range, cache, compression or TLS support. Every readable file under the selected root, including hidden files, is eligible for serving.

    Paths are percent-decoded once as UTF-8; queries do not select files. Traversal segments, malformed escapes, control bytes, backslashes, drive/stream colons and Windows device/alias names are rejected with 400 on all platforms. Canonical targets must remain under the canonical root, including symlink targets and index.html; forbidden/unreadable/nonregular targets return 404. Symlinks within the root are permitted. Open files stream through @http's fixed-length body path in 64 KiB chunks, with at most 64 simultaneous connections. moonbitlang/async@0.22.9 writes large fixed-length chunks directly to the socket, so successful GET/HEAD file responses keep the connection open; redirects and error responses also keep it open. Files should remain stable while served; a shortened file aborts its response, and appended bytes are excluded.

    Wasm policy adds process permissions; it is supplied by the launcher, not installed by this command. For example, save serve-policy.json beside public and launch from that parent directory:

    { "fs": { "read": ["public"] }, "net": { "bind": ["127.0.0.1:8000"] } }

    moonx --experimental-policy serve-policy.json cli/serve@latest -- public

    Keep write/process/network-connect permissions absent. Neither path-based checks nor the current host policy establish race-free confinement against hostile concurrent filesystem replacement. Serve a trusted, stable directory; descriptor-relative confined opening on Wasm is the missing unlock condition. See compatibility and the filesystem decision.