http-server-rs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # http-server: a simple static HTTP server 🚀🦀
2
+
3
+ Rewrite of the popular npm package [http-server](https://github.com/http-party/http-server/tree/master) in Rust with some extras included.
4
+
5
+ `http-server` is a simple, zero-configuration command-line static HTTP server. It is powerful enough for production usage, but it's simple and hackable enough to be used for testing, local development and learning.
6
+
7
+ https://github.com/user-attachments/assets/7b6bbee0-3428-4c9f-80a6-2804ddac6e01
8
+
9
+ ## Usage
10
+
11
+ ```bash
12
+ # Use default configuration
13
+ http-server
14
+
15
+ # Arguments
16
+ # Enable CORS, reroute requests to index.html and automatically compress to brotli
17
+ http-server --cors --spa -Z ./public
18
+
19
+ # Custom Headers
20
+ http-server -H X-Custom-Header:some-value
21
+ ```
22
+
23
+ ```
24
+ Usage: http-server [OPTIONS] [SERVE_DIR]
25
+
26
+ Arguments:
27
+ [SERVE_DIR] Target directory to serve [default: ./dist]
28
+
29
+ Options:
30
+ -a, --address <ADDRESS>
31
+ [default: 0.0.0.0]
32
+ -p, --port <PORT>
33
+ [default: 8080]
34
+ --spa
35
+ Redirect requests to /index.html for Single Page Applications
36
+ -c, --cache-time <CACHE_TIME>
37
+ Cache control time [default: 0]
38
+ -Z, --compress
39
+ Compress responses (JIT)
40
+ -H, --header <HEADERS>
41
+ Custom headers (Format "key:value")
42
+ --auth <BASIC_AUTH>
43
+ Put server behind basic auth (Format "username:password")
44
+ --cors
45
+ Enable CORS header
46
+ -S, --shared-array-buffer
47
+ Enable headers for SharedArrayBuffer
48
+ -Q, --quiet
49
+ Don't print any logs to terminal
50
+ -w, --watch
51
+ Watch folder for changes and trigger a browser reload
52
+ --watch-dir <WATCH_DIR>
53
+ Watch for changes [default: SERVE_DIR]
54
+ --no-watch-inject
55
+ Don't automatically inject watch listener into html
56
+ --stream-buffer-size <STREAM_BUFFER_SIZE>
57
+ Configure the buffer size when streaming files [default: 4000]
58
+ -h, --help
59
+ Print help
60
+ ```
61
+
62
+ ## Watch Mode
63
+
64
+ `http-server` under `--watch` mode can watch the served directory for changes and emit an event to the client notifying of a change. By default the server will intercept html files and inject a JavaScript script which subscribes to change events and triggers a page reload.
65
+
66
+ ```bash
67
+ http-server --watch ./dist
68
+ ```
69
+
70
+ To customize the reload functionality, disable the auto-inject script, manually subscribe to change events and trigger the desired functionality.
71
+
72
+ ```bash
73
+ http-server --watch --no-watch-inject ./dist
74
+ ```
75
+
76
+ ```html
77
+ <html>
78
+ <head>
79
+ <script>
80
+ new EventSource("/.http-server-rs/reload")
81
+ .onmessage = () => window.location.reload();
82
+ </script>
83
+ </head>
84
+ <body>
85
+ <script src="./app.js"></script>
86
+ </body>
87
+ </html>
88
+ ```
89
+
90
+ ## Installation
91
+
92
+ ### MacOS & Linux
93
+
94
+ Download the binary from the [latest GitHub release](https://github.com/alshdavid/http-server-rs/releases/latest) and add it to your `PATH`
95
+
96
+ ```shell
97
+ # Linux AMD64
98
+ curl -L --url https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-linux-amd64.tar.gz | tar -xvzf - -C $HOME/.local/bin
99
+
100
+ # Linux ARM64
101
+ curl -L --url https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-linux-arm64.tar.gz | tar -xvzf - -C $HOME/.local/bin
102
+
103
+ # MacOS ARM64 (Apple Silicon)
104
+ curl -L --url https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-macos-arm64.tar.gz | tar -xvzf - -C $HOME/.local/bin
105
+
106
+ # MacOS AMD64 (Intel)
107
+ curl -L --url https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-macos-amd64.tar.gz | tar -xvzf - -C $HOME/.local/bin
108
+
109
+ # Add to PATH if not already there:
110
+ echo "\nexport \PATH=\$PATH:\$HOME/.local/bin\n" >> $HOME/.zshrc
111
+ echo "\nexport \PATH=\$PATH:\$HOME/.local/bin\n" >> $HOME/.bashrc
112
+ ```
113
+
114
+ ### Windows
115
+
116
+ Download the binary from the [latest GitHub release](https://github.com/alshdavid/http-server-rs/releases/latest) and add it to your `PATH`
@@ -0,0 +1,11 @@
1
+ This is a placeholder file required because npm will only create
2
+ a symlink to a `package.json#bin` entry if the file exists.
3
+
4
+ This file is replaced in a postinstall step with the actual binary
5
+ downloaded from GitHub releases.
6
+
7
+ This file has an .exe extension for Windows compatibility because
8
+ Windows requires a file extension to know a file is executable.
9
+
10
+ Linux and MacOS will be able to run the file as a normal binary
11
+ regardless of the extension being present.
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "http-server-rs",
3
+ "version": "0.0.1",
4
+ "bin": {
5
+ "http-server-rs": "./http-server.exe"
6
+ },
7
+ "scripts": {
8
+ "postinstall": "node ./postinstall.mjs"
9
+ }
10
+ }
@@ -0,0 +1,88 @@
1
+ import * as process from "node:process";
2
+ import * as fs from "node:fs";
3
+ import * as path from "node:path";
4
+ import * as url from "node:url";
5
+ import * as stream from "node:stream";
6
+ import * as tar from "./tar.cjs";
7
+
8
+ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
9
+ const __bin = path.join(__dirname, "http-server.exe");
10
+ const __bin_unix = path.join(__dirname, "http-server");
11
+
12
+ const HTTP_SERVER_RS_BIN_PATH = process.env.HTTP_SERVER_RS_BIN_PATH;
13
+ const HTTP_SERVER_RS_SKIP_DOWNLOAD = process.env.HTTP_SERVER_RS_SKIP_DOWNLOAD;
14
+ const HTTP_SERVER_RS_FORCE_TAG = process.env.HTTP_SERVER_RS_FORCE_TAG;
15
+
16
+ const os = {
17
+ darwin: "macos",
18
+ linux: "linux",
19
+ win32: "windows",
20
+ }[process.platform];
21
+
22
+ const arch = {
23
+ arm64: "arm64",
24
+ x64: "amd64",
25
+ }[process.arch];
26
+
27
+ void (async function main() {
28
+ if (HTTP_SERVER_RS_SKIP_DOWNLOAD === "true") {
29
+ // Do nothing
30
+ } else if (
31
+ HTTP_SERVER_RS_BIN_PATH &&
32
+ fs.existsSync(HTTP_SERVER_RS_BIN_PATH)
33
+ ) {
34
+ await fs.promises.rm(__bin, { recursive: true, force: true });
35
+ await fs.promises.symlink(__bin, HTTP_SERVER_RS_BIN_PATH);
36
+ } else {
37
+ await downloadBin();
38
+ }
39
+ })();
40
+
41
+ async function downloadBin() {
42
+ if (!arch || !os) {
43
+ throw new Error(
44
+ "Unable to determine what Atlaspack Version Manager binary to download"
45
+ );
46
+ }
47
+
48
+ let tag = HTTP_SERVER_RS_FORCE_TAG || "latest";
49
+ if (fs.existsSync(path.join(__dirname, "tag"))) {
50
+ tag = (
51
+ await fs.promises.readFile(path.join(__dirname, "tag"), "utf8")
52
+ ).trim();
53
+ }
54
+
55
+ let url = `https://github.com/alshdavid/http-server-rs/releases/latest/download/http-server-${os}-${arch}.tar.gz`;
56
+ if (tag !== "latest") {
57
+ url = `https://github.com/alshdavid/http-server-rs/releases/download/${tag}/http-server-${os}-${arch}.tar.gz`;
58
+ }
59
+
60
+ const response = await globalThis.fetch(url);
61
+ if (!response.ok) {
62
+ throw new Error("Unable to fetch http-server-rs binary");
63
+ }
64
+
65
+ const body = await response.bytes();
66
+ const file = new stream.Duplex();
67
+
68
+ file.push(body);
69
+ file.push(null);
70
+
71
+ if (fs.existsSync(__bin)) {
72
+ fs.rmSync(__bin, { recursive: true, force: true });
73
+ }
74
+
75
+ let writable = tar.x({
76
+ C: __dirname,
77
+ chmod: true,
78
+ });
79
+
80
+ file.pipe(writable);
81
+ await new Promise((res) => writable.on("close", res));
82
+
83
+ await fs.promises.rm(__bin, { recursive: true, force: true });
84
+ if (fs.existsSync(__bin_unix)) {
85
+ await fs.promises.rename(__bin_unix, __bin);
86
+ }
87
+ await fs.promises.chmod(__bin, "755");
88
+ }
package/tag ADDED
@@ -0,0 +1 @@
1
+ v2025.06.22.0413.6e88