srv-it 0.3.0 → 0.4.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 +2 -0
- package/license.md +20 -0
- package/package.json +1 -1
- package/src/cli.js +2 -1
- package/src/server.js +65 -8
package/README.md
CHANGED
|
@@ -97,3 +97,5 @@ Highlights:
|
|
|
97
97
|
- HTML pages get an auto-injected websocket client for live reload.
|
|
98
98
|
- CSS changes refresh styles without full page reload unless `--no-css-inject` is enabled.
|
|
99
99
|
- If a folder has `index.html`, that file is served; otherwise a styled directory listing is shown.
|
|
100
|
+
- Watch mode always ignores `.git` and `node_modules`, and also skips common home cache/config paths (`~/.cache`, `~/.local/share`).
|
|
101
|
+
- If the OS watcher limit is reached (`ENOSPC`), srv-it keeps serving files and disables live reload instead of crashing.
|
package/license.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Elouan Grimm.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
package/src/server.js
CHANGED
|
@@ -11,6 +11,32 @@ const os = require('node:os');
|
|
|
11
11
|
const chalk = require('chalk');
|
|
12
12
|
const { WebSocketServer } = require('ws');
|
|
13
13
|
|
|
14
|
+
const srvLogger = {
|
|
15
|
+
http: (...message) => console.info(chalk.bgBlue.bold(' HTTP '), ...message),
|
|
16
|
+
info: (...message) => console.info(chalk.bgMagenta.bold(' INFO '), ...message),
|
|
17
|
+
warn: (...message) => console.error(chalk.bgYellow.bold(' WARN '), ...message),
|
|
18
|
+
error: (...message) => console.error(chalk.bgRed.bold(' ERROR '), ...message),
|
|
19
|
+
log: console.log,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const BASE_WATCH_IGNORES = ['**/.git/**', '**/node_modules/**'];
|
|
23
|
+
|
|
24
|
+
const DIRECTORY_FAVICON_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#0c0a09" d="M3 5.5A2.5 2.5 0 0 1 5.5 3H10l2 2h6.5A2.5 2.5 0 0 1 21 7.5v9A2.5 2.5 0 0 1 18.5 19h-13A2.5 2.5 0 0 1 3 16.5z"/><path fill="#3b82f6" d="M3 8h18v8.5a2.5 2.5 0 0 1-2.5 2.5h-13A2.5 2.5 0 0 1 3 16.5z"/></svg>';
|
|
25
|
+
const DIRECTORY_FAVICON_DATA_URL = `data:image/svg+xml,${encodeURIComponent(DIRECTORY_FAVICON_SVG)}`;
|
|
26
|
+
|
|
27
|
+
function toGlobPath(value) {
|
|
28
|
+
return value.replace(/\\/g, '/');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getDefaultWatchIgnores() {
|
|
32
|
+
const home = toGlobPath(os.homedir());
|
|
33
|
+
return [
|
|
34
|
+
...BASE_WATCH_IGNORES,
|
|
35
|
+
`${home}/.cache/**`,
|
|
36
|
+
`${home}/.local/share/**`,
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
|
|
14
40
|
const LIVE_RELOAD_SNIPPET = `\n<script>\n(function(){\n if(!('WebSocket' in window)) return;\n function refreshCSS(){\n var links=[].slice.call(document.querySelectorAll('link[rel="stylesheet"],link:not([rel])'));\n links.forEach(function(link){\n var href=link.getAttribute('href');\n if(!href) return;\n var u=href.replace(/([?&])_srv=\\d+/,'').replace(/[?&]$/,'');\n var join=u.indexOf('?')>-1?'&':'?';\n link.setAttribute('href',u+join+'_srv='+Date.now());\n });\n }\n var proto=location.protocol==='https:'?'wss':'ws';\n var socket=new WebSocket(proto+'://'+location.host+'/__srv_ws');\n socket.onmessage=function(event){\n if(event.data==='refreshcss') refreshCSS();\n if(event.data==='reload') location.reload();\n };\n})();\n</script>\n`;
|
|
15
41
|
|
|
16
42
|
const STYLE_PRESETS = {
|
|
@@ -91,6 +117,7 @@ function renderDirListing({ pathnameValue, entries, style, customCss }) {
|
|
|
91
117
|
<meta charset="utf-8" />
|
|
92
118
|
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
93
119
|
<title>Index of ${pathnameValue}</title>
|
|
120
|
+
<link rel="icon" href="${DIRECTORY_FAVICON_DATA_URL}" />
|
|
94
121
|
<style>
|
|
95
122
|
:root {
|
|
96
123
|
--stone-050: #fafaf9;
|
|
@@ -370,7 +397,7 @@ async function createSrvServer(options) {
|
|
|
370
397
|
res.statusCode = 500;
|
|
371
398
|
res.end('Internal server error');
|
|
372
399
|
if (options.logLevel >= 1) {
|
|
373
|
-
|
|
400
|
+
srvLogger.error('[srv] request error:', error.message);
|
|
374
401
|
}
|
|
375
402
|
} finally {
|
|
376
403
|
if (!options.noRequestLogging) {
|
|
@@ -381,7 +408,7 @@ async function createSrvServer(options) {
|
|
|
381
408
|
const formattedTime = `${now.toLocaleDateString()} ${now.toLocaleTimeString()}`;
|
|
382
409
|
const methodColor = method === 'GET' ? 'cyan' : 'magenta';
|
|
383
410
|
|
|
384
|
-
|
|
411
|
+
srvLogger.http(
|
|
385
412
|
chalk.dim(formattedTime),
|
|
386
413
|
chalk.yellow(sourceIp),
|
|
387
414
|
chalk[methodColor](`${method} ${pathnameValue}`),
|
|
@@ -405,7 +432,7 @@ async function createSrvServer(options) {
|
|
|
405
432
|
res.statusCode = 500;
|
|
406
433
|
res.end('Internal server error');
|
|
407
434
|
if (options.logLevel >= 1) {
|
|
408
|
-
|
|
435
|
+
srvLogger.error('[srv] handler error:', error.message);
|
|
409
436
|
}
|
|
410
437
|
});
|
|
411
438
|
});
|
|
@@ -415,7 +442,7 @@ async function createSrvServer(options) {
|
|
|
415
442
|
res.statusCode = 500;
|
|
416
443
|
res.end('Internal server error');
|
|
417
444
|
if (options.logLevel >= 1) {
|
|
418
|
-
|
|
445
|
+
srvLogger.error('[srv] handler error:', error.message);
|
|
419
446
|
}
|
|
420
447
|
});
|
|
421
448
|
});
|
|
@@ -428,12 +455,29 @@ async function createSrvServer(options) {
|
|
|
428
455
|
});
|
|
429
456
|
|
|
430
457
|
const watchPaths = [root, ...(options.watch || []).map((x) => path.resolve(x))];
|
|
458
|
+
const ignore = [...getDefaultWatchIgnores(), ...((options.ignore || []).filter(Boolean))];
|
|
431
459
|
const watcher = chokidar.watch(watchPaths, {
|
|
432
460
|
ignoreInitial: true,
|
|
433
|
-
ignored:
|
|
461
|
+
ignored: Array.from(new Set(ignore)),
|
|
462
|
+
ignorePermissionErrors: true,
|
|
434
463
|
});
|
|
435
464
|
|
|
465
|
+
let liveReloadEnabled = true;
|
|
466
|
+
let watcherClosed = false;
|
|
467
|
+
|
|
468
|
+
const closeWatcher = async () => {
|
|
469
|
+
if (watcherClosed) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
watcherClosed = true;
|
|
473
|
+
await watcher.close();
|
|
474
|
+
};
|
|
475
|
+
|
|
436
476
|
const sendReload = (changePath) => {
|
|
477
|
+
if (!liveReloadEnabled) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
|
|
437
481
|
const isCss = path.extname(changePath).toLowerCase() === '.css' && !options.noCssInject;
|
|
438
482
|
const message = isCss ? 'refreshcss' : 'reload';
|
|
439
483
|
for (const client of clients) {
|
|
@@ -442,7 +486,7 @@ async function createSrvServer(options) {
|
|
|
442
486
|
}
|
|
443
487
|
}
|
|
444
488
|
if (options.logLevel >= 2) {
|
|
445
|
-
|
|
489
|
+
srvLogger.info(`[srv] ${isCss ? 'css refresh' : 'reload'}: ${changePath}`);
|
|
446
490
|
}
|
|
447
491
|
};
|
|
448
492
|
|
|
@@ -451,15 +495,28 @@ async function createSrvServer(options) {
|
|
|
451
495
|
watcher.on('unlink', sendReload);
|
|
452
496
|
watcher.on('addDir', sendReload);
|
|
453
497
|
watcher.on('unlinkDir', sendReload);
|
|
498
|
+
watcher.on('error', async (error) => {
|
|
499
|
+
if (error && error.code === 'ENOSPC') {
|
|
500
|
+
liveReloadEnabled = false;
|
|
501
|
+
await closeWatcher();
|
|
502
|
+
srvLogger.error('[srv] live reload disabled: file watcher limit reached (ENOSPC).');
|
|
503
|
+
srvLogger.warn('[srv] use --ignore to exclude noisy paths, or raise inotify limits on Linux.');
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (options.logLevel >= 1) {
|
|
508
|
+
srvLogger.error(`[srv] watcher error: ${error.message}`);
|
|
509
|
+
}
|
|
510
|
+
});
|
|
454
511
|
|
|
455
512
|
const closeAll = async () => {
|
|
456
|
-
await
|
|
513
|
+
await closeWatcher();
|
|
457
514
|
wss.close();
|
|
458
515
|
await new Promise((resolve) => server.close(resolve));
|
|
459
516
|
};
|
|
460
517
|
|
|
461
518
|
process.on('SIGINT', async () => {
|
|
462
|
-
|
|
519
|
+
srvLogger.info('\n[srv] shutting down...');
|
|
463
520
|
await closeAll();
|
|
464
521
|
process.exit(0);
|
|
465
522
|
});
|