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/dist/index.d.cts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import * as http0 from "http";
|
|
2
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
import "fs";
|
|
4
|
+
import { EventEmitter } from "node:events";
|
|
5
|
+
|
|
6
|
+
//#region src/types/index.d.mts
|
|
7
|
+
interface ServerConfig {
|
|
8
|
+
port: number;
|
|
9
|
+
rootPath: string;
|
|
10
|
+
reload: boolean;
|
|
11
|
+
ignorePatterns?: string[];
|
|
12
|
+
enableDirectoryListing?: boolean;
|
|
13
|
+
restartOnChange?: boolean;
|
|
14
|
+
logLevel?: LogLevel;
|
|
15
|
+
debounceMs?: number;
|
|
16
|
+
}
|
|
17
|
+
interface HttpRequest extends IncomingMessage {
|
|
18
|
+
url: string;
|
|
19
|
+
method: string;
|
|
20
|
+
}
|
|
21
|
+
interface HttpResponse extends ServerResponse {
|
|
22
|
+
writeHead(statusCode: number, headers?: Record<string, string>): void;
|
|
23
|
+
end(chunk?: string): void;
|
|
24
|
+
write(chunk: string): void;
|
|
25
|
+
}
|
|
26
|
+
interface HotReloadOptions {
|
|
27
|
+
watchPath: string;
|
|
28
|
+
ignored?: string[];
|
|
29
|
+
debounceMs?: number;
|
|
30
|
+
restartOnChange?: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface ReloadEvent {
|
|
33
|
+
type: "file-changed" | "directory-changed" | "file-added" | "file-removed";
|
|
34
|
+
path: string;
|
|
35
|
+
timestamp: Date;
|
|
36
|
+
}
|
|
37
|
+
interface ServerInstance {
|
|
38
|
+
port: number;
|
|
39
|
+
server: http0.Server;
|
|
40
|
+
config: ServerConfig;
|
|
41
|
+
stop: () => Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
interface PortFinderOptions {
|
|
44
|
+
startPort: number;
|
|
45
|
+
endPort?: number;
|
|
46
|
+
timeout?: number;
|
|
47
|
+
}
|
|
48
|
+
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
49
|
+
interface LogEntry {
|
|
50
|
+
level: LogLevel;
|
|
51
|
+
message: string;
|
|
52
|
+
timestamp: Date;
|
|
53
|
+
method?: string;
|
|
54
|
+
url?: string;
|
|
55
|
+
statusCode?: number;
|
|
56
|
+
responseTime?: number;
|
|
57
|
+
}
|
|
58
|
+
interface LoggerOptions {
|
|
59
|
+
level: LogLevel;
|
|
60
|
+
format?: "simple" | "json" | "detailed";
|
|
61
|
+
includeTimestamp?: boolean;
|
|
62
|
+
colorize?: boolean;
|
|
63
|
+
}
|
|
64
|
+
interface Success<T> {
|
|
65
|
+
readonly success: true;
|
|
66
|
+
readonly data: T;
|
|
67
|
+
readonly error?: never;
|
|
68
|
+
}
|
|
69
|
+
interface Failure<E = Error> {
|
|
70
|
+
readonly success: false;
|
|
71
|
+
readonly data?: never;
|
|
72
|
+
readonly error: E;
|
|
73
|
+
}
|
|
74
|
+
type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/config/cli.d.mts
|
|
77
|
+
/**
|
|
78
|
+
* Version information
|
|
79
|
+
*/
|
|
80
|
+
declare const VERSION_INFO: {
|
|
81
|
+
readonly name: "HTTPath";
|
|
82
|
+
readonly version: "0.1.0";
|
|
83
|
+
readonly description: "A minimalist Node.js file server with hot-reload capabilities";
|
|
84
|
+
readonly author: "José Martínez Santana";
|
|
85
|
+
readonly license: "MIT";
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Parse CLI arguments into ServerConfig
|
|
89
|
+
* @param argv - Array of command-line arguments
|
|
90
|
+
* @returns Result containing ServerConfig or error
|
|
91
|
+
* @description
|
|
92
|
+
* This function uses Node.js's built-in parseArgs utility to parse command-line arguments based on predefined options.
|
|
93
|
+
*/
|
|
94
|
+
declare const parseCliArgs: (argv?: string[]) => Result<ServerConfig>;
|
|
95
|
+
/**
|
|
96
|
+
* Validate server configuration
|
|
97
|
+
* @param config - Server configuration to validate
|
|
98
|
+
* @returns boolean indicating if the configuration is valid
|
|
99
|
+
* @description
|
|
100
|
+
* This function checks the validity of the server configuration. It ensures that the port number is within the valid range (1-65535),
|
|
101
|
+
* the root path is a non-empty string, and the reload flag is a boolean. If any of these checks fail, it logs an error message and returns false. Otherwise, it returns true.
|
|
102
|
+
*/
|
|
103
|
+
declare const validateConfig: (config: ServerConfig) => boolean;
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/services/server.d.mts
|
|
106
|
+
/**
|
|
107
|
+
* HTTP Server service class
|
|
108
|
+
*/
|
|
109
|
+
declare class HTTPServer {
|
|
110
|
+
private server;
|
|
111
|
+
private config;
|
|
112
|
+
private hotReload;
|
|
113
|
+
private logger;
|
|
114
|
+
private isRunning;
|
|
115
|
+
constructor(config: ServerConfig);
|
|
116
|
+
/**
|
|
117
|
+
* Start the HTTP server
|
|
118
|
+
* @returns ServerInstance object with server details
|
|
119
|
+
*/
|
|
120
|
+
start(): Promise<ServerInstance>;
|
|
121
|
+
/**
|
|
122
|
+
* Stop the HTTP server
|
|
123
|
+
* @returns Promise that resolves when the server is stopped
|
|
124
|
+
*/
|
|
125
|
+
stop(): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Handle incoming HTTP requests
|
|
128
|
+
* @param req - Incoming HTTP request
|
|
129
|
+
* @param res - HTTP response to send data
|
|
130
|
+
* @returns Promise that resolves when the request is handled
|
|
131
|
+
*/
|
|
132
|
+
private handleRequest;
|
|
133
|
+
/**
|
|
134
|
+
* Handle directory requests
|
|
135
|
+
* @param dirPath - Filesystem path of the directory
|
|
136
|
+
* @param urlPath - URL path requested
|
|
137
|
+
* @param res - HTTP response to send data
|
|
138
|
+
* @returns Promise that resolves when the directory request is handled
|
|
139
|
+
*/
|
|
140
|
+
private handleDirectoryRequest;
|
|
141
|
+
/**
|
|
142
|
+
* Handle file requests
|
|
143
|
+
* @param filePath - Filesystem path of the file
|
|
144
|
+
* @param res - HTTP response to send data
|
|
145
|
+
* @returns Promise that resolves when the file request is handled
|
|
146
|
+
*/
|
|
147
|
+
private handleFileRequest;
|
|
148
|
+
/**
|
|
149
|
+
* Send error response
|
|
150
|
+
* @param res - HTTP response
|
|
151
|
+
* @param statusCode - HTTP status code
|
|
152
|
+
* @param statusText - HTTP status text
|
|
153
|
+
* @returns {void}
|
|
154
|
+
*/
|
|
155
|
+
private sendError;
|
|
156
|
+
/**
|
|
157
|
+
* Generate error page HTML
|
|
158
|
+
* @param statusCode - HTTP status code
|
|
159
|
+
* @param statusText - HTTP status text
|
|
160
|
+
* @returns HTML string for the error page
|
|
161
|
+
*/
|
|
162
|
+
private generateErrorPage;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Create HTTP server instance
|
|
166
|
+
* @param config - Server configuration
|
|
167
|
+
* @returns HTTPServer instance
|
|
168
|
+
*/
|
|
169
|
+
declare const createHTTPServer: (config: ServerConfig) => HTTPServer;
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/utils/logger.d.mts
|
|
172
|
+
/**
|
|
173
|
+
* Logger class for handling application logging
|
|
174
|
+
*/
|
|
175
|
+
declare class Logger {
|
|
176
|
+
private options;
|
|
177
|
+
private startTime;
|
|
178
|
+
constructor(options?: LoggerOptions);
|
|
179
|
+
/**
|
|
180
|
+
* Log debug message
|
|
181
|
+
*/
|
|
182
|
+
debug(message: string, ...args: any[]): void;
|
|
183
|
+
/**
|
|
184
|
+
* Log info message
|
|
185
|
+
*/
|
|
186
|
+
info(message: string, ...args: any[]): void;
|
|
187
|
+
/**
|
|
188
|
+
* Log warning message
|
|
189
|
+
*/
|
|
190
|
+
warn(message: string, ...args: any[]): void;
|
|
191
|
+
/**
|
|
192
|
+
* Log error message
|
|
193
|
+
*/
|
|
194
|
+
error(message: string, ...args: any[]): void;
|
|
195
|
+
/**
|
|
196
|
+
* Log HTTP request
|
|
197
|
+
*/
|
|
198
|
+
logRequest(method: string, url: string, userAgent?: string): void;
|
|
199
|
+
/**
|
|
200
|
+
* Log HTTP response
|
|
201
|
+
*/
|
|
202
|
+
logResponse(statusCode: number, responseTime?: number): void;
|
|
203
|
+
/**
|
|
204
|
+
* Create a log entry object
|
|
205
|
+
*/
|
|
206
|
+
createLogEntry(level: LogLevel, message: string, meta?: Record<string, any>): LogEntry;
|
|
207
|
+
/**
|
|
208
|
+
* Set log level
|
|
209
|
+
*/
|
|
210
|
+
setLevel(level: LogLevel): void;
|
|
211
|
+
/**
|
|
212
|
+
* Get current log level
|
|
213
|
+
*/
|
|
214
|
+
getLevel(): LogLevel;
|
|
215
|
+
/**
|
|
216
|
+
* Check if a log level should be output
|
|
217
|
+
*/
|
|
218
|
+
shouldLog(level: LogLevel): boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Get server uptime
|
|
221
|
+
*/
|
|
222
|
+
getUptime(): number;
|
|
223
|
+
/**
|
|
224
|
+
* Format uptime as human readable string
|
|
225
|
+
*/
|
|
226
|
+
formatUptime(): string;
|
|
227
|
+
/**
|
|
228
|
+
* Core logging method
|
|
229
|
+
*/
|
|
230
|
+
private log;
|
|
231
|
+
/**
|
|
232
|
+
* Write log to console
|
|
233
|
+
*/
|
|
234
|
+
private writeLog;
|
|
235
|
+
/**
|
|
236
|
+
* Format message with arguments
|
|
237
|
+
*/
|
|
238
|
+
private formatMessage;
|
|
239
|
+
/**
|
|
240
|
+
* Format timestamp
|
|
241
|
+
*/
|
|
242
|
+
private formatTimestamp;
|
|
243
|
+
/**
|
|
244
|
+
* Apply color to text if colorization is enabled
|
|
245
|
+
*/
|
|
246
|
+
private colorize;
|
|
247
|
+
/**
|
|
248
|
+
* Get color for HTTP status code
|
|
249
|
+
*/
|
|
250
|
+
private getStatusColor;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Create a new logger instance
|
|
254
|
+
*/
|
|
255
|
+
declare const createLogger: (options?: LoggerOptions) => Logger;
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/utils/port-finder.d.mts
|
|
258
|
+
/**
|
|
259
|
+
* Find a single available port within a specified range
|
|
260
|
+
* @param options - PortFinderOptions to specify range and timeout
|
|
261
|
+
* @returns Promise resolving to Result<number> with the available port number or error
|
|
262
|
+
* @description
|
|
263
|
+
* This function iterates through the specified port range, checking each port's availability.
|
|
264
|
+
* It returns the first available port found. If no ports are available in the range,
|
|
265
|
+
* it returns an error indicating the failure.
|
|
266
|
+
*/
|
|
267
|
+
declare const findAvailablePort: (options?: Partial<PortFinderOptions>) => Promise<Result<number>>;
|
|
268
|
+
//#endregion
|
|
269
|
+
//#region src/services/hot-reload.d.mts
|
|
270
|
+
/**
|
|
271
|
+
* HotReloadService class
|
|
272
|
+
* Manages hot-reload functionality for development environments
|
|
273
|
+
*/
|
|
274
|
+
declare class HotReloadService extends EventEmitter {
|
|
275
|
+
private watcher;
|
|
276
|
+
private clients;
|
|
277
|
+
private options;
|
|
278
|
+
private debounceTimer;
|
|
279
|
+
constructor(options?: HotReloadOptions);
|
|
280
|
+
/**
|
|
281
|
+
* Start watching for file changes
|
|
282
|
+
* @returns Result indicating success or failure of starting the watcher
|
|
283
|
+
*/
|
|
284
|
+
start(): Result<void>;
|
|
285
|
+
/**
|
|
286
|
+
* Stop watching for file changes
|
|
287
|
+
* @description Stops the file system watcher and cleans up resources
|
|
288
|
+
*/
|
|
289
|
+
stop(): void;
|
|
290
|
+
/**
|
|
291
|
+
* Handle new SSE connection from client
|
|
292
|
+
* @param req - HTTP request
|
|
293
|
+
* @param res - HTTP response
|
|
294
|
+
*/
|
|
295
|
+
handleSSEConnection(req: HttpRequest, res: HttpResponse): void;
|
|
296
|
+
/**
|
|
297
|
+
* Broadcast reload signal to all connected clients
|
|
298
|
+
* @param event - Optional reload event details
|
|
299
|
+
* @returns {void}
|
|
300
|
+
*/
|
|
301
|
+
broadcastReload(event?: ReloadEvent): void;
|
|
302
|
+
/**
|
|
303
|
+
* Inject hot-reload script into HTML content
|
|
304
|
+
* @param htmlContent - HTML content to inject script into
|
|
305
|
+
* @returns HTML content with injected hot-reload script
|
|
306
|
+
*/
|
|
307
|
+
injectScript(htmlContent: string): string;
|
|
308
|
+
/**
|
|
309
|
+
* Get number of connected clients
|
|
310
|
+
* @returns {number} Number of connected clients
|
|
311
|
+
*/
|
|
312
|
+
getClientCount(): number;
|
|
313
|
+
/**
|
|
314
|
+
* Get information about connected clients
|
|
315
|
+
* @returns Array of client info objects
|
|
316
|
+
*/
|
|
317
|
+
getClientInfo(): Array<{
|
|
318
|
+
id: string;
|
|
319
|
+
connectedAt: Date;
|
|
320
|
+
}>;
|
|
321
|
+
/**
|
|
322
|
+
* Handle file change events
|
|
323
|
+
* @param eventType - Type of file system event
|
|
324
|
+
* @param filename - Name of the changed file
|
|
325
|
+
* @returns {void}
|
|
326
|
+
*/
|
|
327
|
+
private handleFileChange;
|
|
328
|
+
/**
|
|
329
|
+
* Check if a file should be ignored based on configured ignored patterns
|
|
330
|
+
* @param filename - Name of the file to check
|
|
331
|
+
* @returns boolean indicating if file should be ignored
|
|
332
|
+
*/
|
|
333
|
+
private shouldIgnoreFile;
|
|
334
|
+
/**
|
|
335
|
+
* Map fs.watch event type to ReloadEvent type
|
|
336
|
+
* @param eventType - Event type from fs.watch
|
|
337
|
+
* @returns Corresponding ReloadEvent type
|
|
338
|
+
*/
|
|
339
|
+
private getEventType;
|
|
340
|
+
/**
|
|
341
|
+
* Decide if any of the given paths should trigger a server restart
|
|
342
|
+
* @param paths - Array of changed file paths
|
|
343
|
+
* @returns {boolean} indicating if server should restart
|
|
344
|
+
*/
|
|
345
|
+
private shouldRestartServer;
|
|
346
|
+
/**
|
|
347
|
+
* Decide if any of the given paths should trigger a browser reload
|
|
348
|
+
* @param paths - Array of changed file paths
|
|
349
|
+
* @returns {boolean} indicating if browser should reload
|
|
350
|
+
*/
|
|
351
|
+
private shouldTriggerBrowserReload;
|
|
352
|
+
/**
|
|
353
|
+
* Restart the current Node.js process
|
|
354
|
+
* @returns {void}
|
|
355
|
+
*/
|
|
356
|
+
private restartProcess;
|
|
357
|
+
/**
|
|
358
|
+
* Generate a unique client ID
|
|
359
|
+
* @returns string representing the unique client ID
|
|
360
|
+
*/
|
|
361
|
+
private generateClientId;
|
|
362
|
+
/**
|
|
363
|
+
* Remove a client by ID
|
|
364
|
+
* @param clientId - ID of the client to remove
|
|
365
|
+
* @returns {void}
|
|
366
|
+
*/
|
|
367
|
+
private removeClient;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Create and return a HotReloadService instance
|
|
371
|
+
* @param options - Hot-reload options
|
|
372
|
+
* @returns HotReloadService instance
|
|
373
|
+
*/
|
|
374
|
+
declare const createHotReloadService: (options?: HotReloadOptions) => HotReloadService;
|
|
375
|
+
//#endregion
|
|
376
|
+
//#region src/index.d.mts
|
|
377
|
+
/**
|
|
378
|
+
* Main application function
|
|
379
|
+
*/
|
|
380
|
+
declare const main: () => Promise<void>;
|
|
381
|
+
//#endregion
|
|
382
|
+
export { type HotReloadOptions, type LoggerOptions, type ServerConfig, type ServerInstance, VERSION_INFO, createHTTPServer, createHotReloadService, createLogger, findAvailablePort, main, parseCliArgs, validateConfig };
|