Skip to content

Commit caba353

Browse files
committed
fix: avoid logging range errors to console; fixes #185 ; fixes #212
1 parent 4a13f21 commit caba353

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- Fix: Ensure package `version` stays up to date
2727
- Fix: path should be more generous in unescaping anything valid in a
2828
path (such as a hash)
29+
- Fix: Avoid logging range errors to console
2930
- Fix: change `fs.createReadStream()` mode to integer (@pixcai)
3031
- Enhancement: TypeScript support
3132
- Enhancement: Allow access with local ip (@flyingsky)

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,16 @@ A request to '/myFile' would check for a `myFile` folder (first) then a
225225

226226
example: `{ defaultExtension: "html" }`
227227

228+
### Listening for emitted warnings
229+
230+
Certain warnings are not logged to console but instead may be listened for.
231+
232+
```js
233+
fileServer.on('warn', (msg) => {
234+
console.log(msg);
235+
});
236+
```
237+
228238
## Command Line Interface
229239

230240
`node-static` also provides a CLI.

lib/node-static.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ function tryStat(p, callback) {
5656
* }} ServerOptions
5757
*/
5858

59-
class Server {
59+
class Server extends events.EventEmitter {
6060
/**
6161
* @param {string|ServerOptions|null} [root]
6262
* @param {ServerOptions} [options]
6363
*/
6464
constructor (root, options) {
65+
super();
6566
if (root && typeof root === 'object') { options = root; root = null }
6667

6768
// resolve() doesn't normalize (to lowercase) drive letters on Windows
@@ -422,10 +423,10 @@ class Server {
422423
if (!isNaN(byteRange.from) && !isNaN(byteRange.to) && 0 <= byteRange.from && byteRange.from <= byteRange.to) {
423424
byteRange.valid = true;
424425
} else {
425-
console.warn('Request contains invalid range header: ', rangeHeaderArr);
426+
this.emit('warn', 'Request contains invalid range header: ' + rangeHeaderArr.join(', '));
426427
}
427428
} else {
428-
console.warn('Request contains unsupported range header: ', rangeHeader);
429+
this.emit('warn', 'Request contains unsupported range header: ' + rangeHeader);
429430
}
430431
}
431432
return byteRange;
@@ -468,13 +469,13 @@ class Server {
468469

469470
} else {
470471
byteRange.valid = false;
471-
console.warn('Range request exceeds file boundaries, goes until byte no', byteRange.to, 'against file size of', length, 'bytes');
472+
this.emit('warn', 'Range request exceeds file boundaries, goes until byte no ' + byteRange.to + ' against file size of ' + length + ' bytes');
472473
}
473474
}
474475

475476
/* In any case, check for unhandled byte range headers */
476477
if (!byteRange.valid && req.headers['range']) {
477-
console.error(new Error('Range request present but invalid, might serve whole file instead'));
478+
this.emit('warn', 'Range request present but invalid, might serve whole file instead');
478479
}
479480

480481
// Copy default headers

test/integration/node-static-test.js

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -432,12 +432,11 @@ describe('node-static', function () {
432432

433433
it('serving full bytes of hello.txt with bad range', async function () {
434434
fileServer = new statik.Server(__dirname + '/../fixtures');
435-
const _consoleError = console.error;
436-
/** @type {Error} */
437-
let loggedErr;
438-
console.error = (err) => {
439-
loggedErr = err;
440-
};
435+
let emittedWarning;
436+
fileServer.on('warn', (warning) => {
437+
emittedWarning = warning;
438+
});
439+
441440
const options = {
442441
headers: {
443442
'Range': 'bytes=1000-1004'
@@ -451,20 +450,15 @@ describe('node-static', function () {
451450

452451
assert.equal(response.headers.get('content-range'), null);
453452
assert.equal(await response.text(), 'hello world', 'should respond with hello world');
454-
// @ts-expect-error Ok
455-
assert.equal(loggedErr?.message, 'Range request present but invalid, might serve whole file instead')
456-
console.error = _consoleError;
453+
assert.equal(emittedWarning, 'Range request present but invalid, might serve whole file instead')
457454
});
458455

459456
it('serving full bytes of hello.txt with unsupported range flavor', async function () {
460457
fileServer = new statik.Server(__dirname + '/../fixtures');
461-
const _consoleWarn = console.warn;
462-
let loggedWarning;
463-
let loggedHeader;
464-
console.warn = (warning, warning2) => {
465-
loggedWarning = warning;
466-
loggedHeader = warning2;
467-
};
458+
let emittedWarning;
459+
fileServer.once('warn', (warning) => {
460+
emittedWarning = warning;
461+
});
468462
const options = {
469463
headers: {
470464
'Range': 'qubits=1-5'
@@ -478,20 +472,15 @@ describe('node-static', function () {
478472

479473
assert.equal(response.headers.get('content-range'), null);
480474
assert.equal(await response.text(), 'hello world', 'should respond with hello world');
481-
assert.equal(loggedWarning, 'Request contains unsupported range header: ')
482-
assert.equal(loggedHeader, 'qubits=1-5');
483-
console.warn = _consoleWarn;
475+
assert.equal(emittedWarning, 'Request contains unsupported range header: qubits=1-5');
484476
});
485477

486478
it('serving full bytes of hello.txt with invalid range header', async function () {
487479
fileServer = new statik.Server(__dirname + '/../fixtures');
488-
const _consoleWarn = console.warn;
489-
let loggedWarning;
490-
let loggedHeader;
491-
console.warn = (warning, warning2) => {
492-
loggedWarning = warning;
493-
loggedHeader = warning2;
494-
};
480+
let emittedWarning;
481+
fileServer.once('warn', (warning) => {
482+
emittedWarning = warning;
483+
});
495484
const options = {
496485
headers: {
497486
'Range': 'bytes=a-b'
@@ -505,9 +494,7 @@ describe('node-static', function () {
505494

506495
assert.equal(response.headers.get('content-range'), null);
507496
assert.equal(await response.text(), 'hello world', 'should respond with hello world');
508-
assert.equal(loggedWarning, 'Request contains invalid range header: ')
509-
assert.deepEqual(loggedHeader, ['a', 'b']);
510-
console.warn = _consoleWarn;
497+
assert.equal(emittedWarning, "Request contains invalid range header: a, b")
511498
});
512499

513500
it('serving directory index', async function (){

0 commit comments

Comments
 (0)