httpath 1.0.0
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/LICENSE +21 -0
- package/README.md +252 -0
- package/dist/index.cjs +532 -0
- package/dist/index.d.cts +382 -0
- package/dist/index.d.mts +382 -0
- package/dist/index.mjs +532 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JosΓ© MartΓnez Santana
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
# HTTPath
|
|
2
|
+
|
|
3
|
+
<div align="center">
|
|
4
|
+
π A minimalist HTTP file server for Node.js with hot-reload capabilities
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
> **HTTPath** - A lightweight, feature-rich static file server similar to Python's `python -m http.server` but with modern Node.js features.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+

|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## β¨ Features
|
|
14
|
+
|
|
15
|
+
- π **Static File Streaming** - Efficient file serving using Node.js streams
|
|
16
|
+
- π― **MIME Type Detection** - Automatic content-type detection for common file types
|
|
17
|
+
- π **Directory Indexing** - Beautiful directory listings with navigation
|
|
18
|
+
- π **Path Sanitization** - Built-in security against directory traversal attacks
|
|
19
|
+
- π **Hot-Reload** - Automatic browser refresh when files change (with `--reload` flag)
|
|
20
|
+
- β‘ **Auto Port Detection** - Automatically finds available ports if default is busy
|
|
21
|
+
- π₯οΈ **CLI Interface** - Easy command-line usage with intuitive flags
|
|
22
|
+
- π‘οΈ **Graceful Shutdown** - Clean server shutdown with Ctrl+C
|
|
23
|
+
|
|
24
|
+
## π Quick Start
|
|
25
|
+
|
|
26
|
+
### Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install -g httpath
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or use without installing:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npx httpath
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Basic Usage
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# Start server on default port (8080) in current directory
|
|
42
|
+
httpath
|
|
43
|
+
|
|
44
|
+
# Custom port
|
|
45
|
+
httpath --port 3000
|
|
46
|
+
|
|
47
|
+
# Serve different directory
|
|
48
|
+
httpath --path ./public
|
|
49
|
+
|
|
50
|
+
# Enable hot-reload for development
|
|
51
|
+
httpath --reload
|
|
52
|
+
|
|
53
|
+
# Combined options
|
|
54
|
+
httpath --port 3000 --path ./dist --reload
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Additional options (useful for development):
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Ignore specific patterns (comma-separated)
|
|
61
|
+
httpath --path ./demo --reload --ignore node_modules,.git
|
|
62
|
+
|
|
63
|
+
# Disable directory listing (serve index.html or return 403)
|
|
64
|
+
httpath --path ./demo --no-listing
|
|
65
|
+
|
|
66
|
+
# Restart the server process when server/config files change
|
|
67
|
+
httpath --path ./demo --reload --restart-on-change
|
|
68
|
+
|
|
69
|
+
# Set log level
|
|
70
|
+
httpath --log debug --reload
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## π API Reference
|
|
74
|
+
|
|
75
|
+
### Command Line Options
|
|
76
|
+
|
|
77
|
+
| Option | Short | Description | Default |
|
|
78
|
+
|--------|-------|-------------|---------|
|
|
79
|
+
| `--port` | `-p` | Port number to listen on | `8080` |
|
|
80
|
+
| `--path` | `-d` | Directory to serve files from | Current directory |
|
|
81
|
+
| `--reload` | `-r` | Enable hot-reload functionality | `false` |
|
|
82
|
+
| `--ignore` | `-i` | Comma-separated patterns to ignore (watch) | `node_modules,.git,.DS_Store` |
|
|
83
|
+
| `--no-listing` | | Disable directory listing when no `index.html` is present | `false` |
|
|
84
|
+
| `--restart-on-change` | | Restart Node process when server/config files change | `false` |
|
|
85
|
+
| `--log` | | Log level: `debug`, `info`, `warn`, `error` | `info` |
|
|
86
|
+
|
|
87
|
+
### Examples
|
|
88
|
+
|
|
89
|
+
#### Development Server with Hot-Reload
|
|
90
|
+
```bash
|
|
91
|
+
httpath --port 3000 --reload
|
|
92
|
+
```
|
|
93
|
+
Perfect for frontend development. Changes to HTML, CSS, JS files will automatically refresh the browser.
|
|
94
|
+
|
|
95
|
+
#### Production-like Static Server
|
|
96
|
+
```bash
|
|
97
|
+
httpath --port 8080 --path ./build
|
|
98
|
+
```
|
|
99
|
+
Serve built static assets without hot-reload overhead.
|
|
100
|
+
|
|
101
|
+
#### Custom Directory with Auto Port
|
|
102
|
+
```bash
|
|
103
|
+
httpath --path ./my-website
|
|
104
|
+
```
|
|
105
|
+
If port 8080 is busy, HTTPath will automatically try 8081, 8082, etc.
|
|
106
|
+
|
|
107
|
+
## π₯ Hot-Reload Feature
|
|
108
|
+
|
|
109
|
+
When enabled with `--reload`, HTTPath provides:
|
|
110
|
+
|
|
111
|
+
- **File Watching**: Monitors all files in the served directory recursively
|
|
112
|
+
- **Server-Sent Events**: Uses SSE for real-time browser communication
|
|
113
|
+
- **Auto-Injection**: Automatically injects reload script into HTML files
|
|
114
|
+
- **Smart Reconnection**: Handles connection drops gracefully
|
|
115
|
+
|
|
116
|
+
Notes on behavior:
|
|
117
|
+
|
|
118
|
+
- By default hot-reload broadcasts a reload signal to connected browsers when frontend assets change (HTML/CSS/JS/images/fonts/etc.).
|
|
119
|
+
- The server classifies file changes and will optionally restart the Node process when server or configuration files change (e.g., `.js`, `.mjs`, `.ts`, `package.json`, `.json`, `.yaml`). Enable this with `--restart-on-change`.
|
|
120
|
+
- Use `--ignore` to provide comma-separated patterns (e.g., `node_modules,.git`) so file-watching ignores them.
|
|
121
|
+
|
|
122
|
+
### How It Works
|
|
123
|
+
|
|
124
|
+
1. HTTPath watches for file changes using Node.js `fs.watch`
|
|
125
|
+
2. A tiny JavaScript snippet is injected into HTML files
|
|
126
|
+
3. The script opens a connection to `/__reload__` endpoint
|
|
127
|
+
4. When files change, the server sends a reload signal
|
|
128
|
+
5. The browser automatically refreshes
|
|
129
|
+
|
|
130
|
+
## π‘οΈ Security Features
|
|
131
|
+
|
|
132
|
+
HTTPath includes built-in protection against common web server vulnerabilities:
|
|
133
|
+
|
|
134
|
+
- **Directory Traversal Protection**: Prevents access to files outside the served directory
|
|
135
|
+
- **Path Normalization**: Safely resolves relative paths
|
|
136
|
+
- **Input Sanitization**: Cleans URL parameters
|
|
137
|
+
|
|
138
|
+
## π Directory Listings
|
|
139
|
+
|
|
140
|
+
When accessing a directory without an `index.html` file, HTTPath generates a clean, navigable listing:
|
|
141
|
+
|
|
142
|
+
- **Sorted Display**: Directories first, then files alphabetically
|
|
143
|
+
- **Parent Navigation**: Easy ".." links to go up directories
|
|
144
|
+
- **File Type Icons**: Visual indicators for different file types
|
|
145
|
+
- **Responsive Design**: Works well on mobile devices
|
|
146
|
+
|
|
147
|
+
## π― Supported MIME Types
|
|
148
|
+
|
|
149
|
+
HTTPath automatically detects and serves files with correct content types:
|
|
150
|
+
|
|
151
|
+
| Extension | MIME Type |
|
|
152
|
+
|-----------|-----------|
|
|
153
|
+
| `.html`, `.htm` | `text/html` |
|
|
154
|
+
| `.js`, `.mjs` | `text/javascript` |
|
|
155
|
+
| `.css` | `text/css` |
|
|
156
|
+
| `.json` | `application/json` |
|
|
157
|
+
| `.png` | `image/png` |
|
|
158
|
+
| `.jpg`, `.jpeg` | `image/jpeg` |
|
|
159
|
+
| `.gif` | `image/gif` |
|
|
160
|
+
| `.svg` | `image/svg+xml` |
|
|
161
|
+
| `.txt` | `text/plain` |
|
|
162
|
+
| `.pdf` | `application/pdf` |
|
|
163
|
+
| `.woff`, `.woff2` | `font/woff`, `font/woff2` |
|
|
164
|
+
|
|
165
|
+
And many more...
|
|
166
|
+
|
|
167
|
+
## π§ Development
|
|
168
|
+
|
|
169
|
+
### Building from Source
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
# Clone repository
|
|
173
|
+
git clone https://github.com/MetalbolicX/httpath.git
|
|
174
|
+
cd httpath
|
|
175
|
+
|
|
176
|
+
# Install dependencies
|
|
177
|
+
npm install
|
|
178
|
+
|
|
179
|
+
# Build the project
|
|
180
|
+
npm run build
|
|
181
|
+
|
|
182
|
+
# Test locally
|
|
183
|
+
npm start
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### Project Structure
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
src/
|
|
190
|
+
βββ index.mts # Main entry point and orchestration
|
|
191
|
+
βββ types/
|
|
192
|
+
β βββ index.mts # TypeScript type definitions
|
|
193
|
+
βββ config/
|
|
194
|
+
β βββ cli.mts # CLI argument parsing and configuration
|
|
195
|
+
βββ constants/
|
|
196
|
+
β βββ mime-types.mts # MIME type mappings and utilities
|
|
197
|
+
βββ security/
|
|
198
|
+
β βββ path-validator.mts # Path validation and security middleware
|
|
199
|
+
βββ services/
|
|
200
|
+
β βββ file-service.mts # File system operations and directory listing
|
|
201
|
+
β βββ hot-reload.mts # Hot-reload functionality with SSE
|
|
202
|
+
β βββ server.mts # HTTP server and request handling
|
|
203
|
+
βββ utils/
|
|
204
|
+
βββ logger.mts # Logging utilities and request logging
|
|
205
|
+
βββ port-finder.mts # Port availability checking
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## π§ͺ Testing
|
|
209
|
+
|
|
210
|
+
The project includes comprehensive tests for:
|
|
211
|
+
|
|
212
|
+
- Static file serving
|
|
213
|
+
- MIME type detection
|
|
214
|
+
- Directory traversal protection
|
|
215
|
+
- Hot-reload functionality
|
|
216
|
+
- CLI argument parsing
|
|
217
|
+
|
|
218
|
+
Run tests:
|
|
219
|
+
```bash
|
|
220
|
+
npm test
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## π Requirements
|
|
224
|
+
|
|
225
|
+
- **Node.js** >= 18.0.0
|
|
226
|
+
- **Operating System**: Windows, macOS, Linux
|
|
227
|
+
|
|
228
|
+
## π€ Contributing
|
|
229
|
+
|
|
230
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
231
|
+
|
|
232
|
+
1. Fork the repository
|
|
233
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
234
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
235
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
236
|
+
5. Open a Pull Request
|
|
237
|
+
|
|
238
|
+
## π License
|
|
239
|
+
|
|
240
|
+
Released under [MIT License](LICENSE) by [@MetalbolicX](https://github.com/MetalbolicX).
|
|
241
|
+
|
|
242
|
+
## π Related Projects
|
|
243
|
+
|
|
244
|
+
- [live-server](https://github.com/tapio/live-server) - Live reloading for development
|
|
245
|
+
- [http-server](https://github.com/http-party/http-server) - Simple HTTP server
|
|
246
|
+
- [serve](https://github.com/vercel/serve) - Static file serving and directory listing
|
|
247
|
+
|
|
248
|
+
---
|
|
249
|
+
|
|
250
|
+
<div align="center">
|
|
251
|
+
Made with β€οΈ by <a href="https://github.com/MetalbolicX">JosΓ© MartΓnez Santana</a>
|
|
252
|
+
</div>
|