koa-classic-server 2.1.3 → 2.3.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/README.md CHANGED
@@ -4,37 +4,30 @@
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/koa-classic-server.svg)](https://www.npmjs.com/package/koa-classic-server)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
- [![Tests](https://img.shields.io/badge/tests-153%20passing-brightgreen.svg)]()
7
+ [![Tests](https://img.shields.io/badge/tests-197%20passing-brightgreen.svg)]()
8
8
 
9
9
  ---
10
10
 
11
- ## 🎉 Version 2.1.3 - Configuration Update
11
+ ## 🎉 Version 2.X - Production-Ready Release
12
12
 
13
- Version 2.1.3 updates the default caching behavior for better development experience while maintaining production-ready performance.
13
+ The 2.X series brings major performance improvements, enhanced security, and powerful new features while maintaining full backward compatibility.
14
14
 
15
- ### What's New in 2.1.3
16
-
17
- ✅ **Development-Friendly Defaults** - `enableCaching` now defaults to `false` for easier development
18
- ✅ **Production Guidance** - Clear documentation on enabling caching for production environments
19
- ✅ **Enhanced Documentation** - Comprehensive notes on caching configuration and recommendations
20
-
21
- ### What's New in 2.1.2
15
+ ### Key Features in Version 2.X
22
16
 
17
+ ✅ **URL Rewriting Support** - Compatible with i18n and routing middleware via `useOriginalUrl` option
18
+ ✅ **Improved Caching Controls** - Clear `browserCacheEnabled` and `browserCacheMaxAge` options
19
+ ✅ **Development-Friendly Defaults** - Caching disabled by default for easier development
20
+ ✅ **Production Optimized** - Enable caching in production for 80-95% bandwidth reduction
23
21
  ✅ **Sortable Directory Columns** - Click Name/Type/Size to sort (Apache2-like)
24
- ✅ **Navigation Bug Fixed** - Directory navigation now works correctly after sorting
25
22
  ✅ **File Size Display** - Human-readable file sizes (B, KB, MB, GB, TB)
26
- ✅ **HTTP Caching** - 80-95% bandwidth reduction with ETag and Last-Modified
23
+ ✅ **HTTP Caching** - ETag and Last-Modified headers with 304 responses
27
24
  ✅ **Async/Await** - Non-blocking I/O for high performance
28
- ✅ **153 Tests Passing** - Comprehensive test coverage
29
- ✅ **Flow Documentation** - Complete execution flow diagrams
30
- ✅ **Code Review** - Standardized operators and best practices
31
-
32
- ### What's New in 2.0
33
-
34
- ✅ **Performance Optimizations** - 50-70% faster directory listings
25
+ ✅ **Performance Optimized** - 50-70% faster directory listings
35
26
  ✅ **Enhanced Index Option** - Array format with RegExp support
36
- ✅ **Template Engine Guide** - Complete documentation with examples
37
- ✅ **Security Hardened** - Path traversal, XSS, race condition fixes
27
+ ✅ **Template Engine Support** - EJS, Pug, Handlebars, Nunjucks, and more
28
+ ✅ **Enterprise Security** - Path traversal, XSS, race condition protection
29
+ ✅ **Comprehensive Testing** - 197 tests passing with extensive coverage
30
+ ✅ **Complete Documentation** - Detailed guides and examples
38
31
 
39
32
  [See full changelog →](./docs/CHANGELOG.md)
40
33
 
@@ -101,8 +94,8 @@ app.use(koaClassicServer(__dirname + '/public', {
101
94
  showDirContents: true,
102
95
  index: ['index.html', 'index.htm'],
103
96
  urlPrefix: '/static',
104
- cacheMaxAge: 3600,
105
- enableCaching: true
97
+ browserCacheMaxAge: 3600,
98
+ browserCacheEnabled: true
106
99
  }));
107
100
 
108
101
  app.listen(3000);
@@ -229,14 +222,14 @@ Enable aggressive caching for static files:
229
222
 
230
223
  ```javascript
231
224
  app.use(koaClassicServer(__dirname + '/public', {
232
- enableCaching: true, // Enable ETag and Last-Modified
233
- cacheMaxAge: 86400, // Cache for 24 hours (in seconds)
225
+ browserCacheEnabled: true, // Enable ETag and Last-Modified
226
+ browserCacheMaxAge: 86400, // Cache for 24 hours (in seconds)
234
227
  }));
235
228
  ```
236
229
 
237
230
  **⚠️ Important: Production Recommendation**
238
231
 
239
- The default value for `enableCaching` is `false` to facilitate development (where you want changes to be immediately visible). **For production environments, it is strongly recommended to set `enableCaching: true`** to benefit from:
232
+ The default value for `browserCacheEnabled` is `false` to facilitate development (where you want changes to be immediately visible). **For production environments, it is strongly recommended to set `browserCacheEnabled: true`** to benefit from:
240
233
 
241
234
  - 80-95% bandwidth reduction
242
235
  - 304 Not Modified responses for unchanged files
@@ -281,8 +274,8 @@ app.use(koaClassicServer(path.join(__dirname, 'public'), {
281
274
  index: ['index.html', 'index.htm'],
282
275
  urlPrefix: '/assets',
283
276
  urlsReserved: ['/admin', '/api', '/.git'],
284
- enableCaching: true,
285
- cacheMaxAge: 86400, // 24 hours
277
+ browserCacheEnabled: true,
278
+ browserCacheMaxAge: 86400, // 24 hours
286
279
  }));
287
280
 
288
281
  // Serve dynamic templates
@@ -366,10 +359,18 @@ Creates a Koa middleware for serving static files.
366
359
  ext: ['ejs', 'pug', 'hbs']
367
360
  },
368
361
 
369
- // HTTP caching configuration
362
+ // Browser HTTP caching configuration
370
363
  // NOTE: Default is false for development. Set to true in production for better performance!
371
- enableCaching: false, // Enable ETag & Last-Modified (default: false)
372
- cacheMaxAge: 3600, // Cache-Control max-age in seconds (default: 3600 = 1 hour)
364
+ browserCacheEnabled: false, // Enable ETag & Last-Modified (default: false)
365
+ browserCacheMaxAge: 3600, // Cache-Control max-age in seconds (default: 3600 = 1 hour)
366
+
367
+ // URL resolution
368
+ useOriginalUrl: true, // Use ctx.originalUrl (default) or ctx.url
369
+ // Set false for URL rewriting middleware (i18n, routing)
370
+
371
+ // DEPRECATED (use new names above):
372
+ // enableCaching: use browserCacheEnabled instead
373
+ // cacheMaxAge: use browserCacheMaxAge instead
373
374
  }
374
375
  ```
375
376
 
@@ -384,8 +385,52 @@ Creates a Koa middleware for serving static files.
384
385
  | `urlsReserved` | Array | `[]` | Reserved directory paths (first-level only) |
385
386
  | `template.render` | Function | `undefined` | Template rendering function |
386
387
  | `template.ext` | Array | `[]` | Extensions for template rendering |
387
- | `enableCaching` | Boolean | `false` | Enable HTTP caching headers (recommended: `true` in production) |
388
- | `cacheMaxAge` | Number | `3600` | Cache duration in seconds |
388
+ | `browserCacheEnabled` | Boolean | `false` | Enable browser HTTP caching headers (recommended: `true` in production) |
389
+ | `browserCacheMaxAge` | Number | `3600` | Browser cache duration in seconds |
390
+ | `useOriginalUrl` | Boolean | `true` | Use `ctx.originalUrl` (default) or `ctx.url` for URL resolution |
391
+ | ~~`enableCaching`~~ | Boolean | `false` | **DEPRECATED**: Use `browserCacheEnabled` instead |
392
+ | ~~`cacheMaxAge`~~ | Number | `3600` | **DEPRECATED**: Use `browserCacheMaxAge` instead |
393
+
394
+ #### useOriginalUrl (Boolean, default: true)
395
+
396
+ Controls which URL property is used for file resolution:
397
+ - **`true` (default)**: Uses `ctx.originalUrl` (immutable, reflects the original request)
398
+ - **`false`**: Uses `ctx.url` (mutable, can be modified by middleware)
399
+
400
+ **When to use `false`:**
401
+
402
+ Set `useOriginalUrl: false` when using URL rewriting middleware such as i18n routers or path rewriters that modify `ctx.url`. This allows koa-classic-server to serve files based on the rewritten URL instead of the original request URL.
403
+
404
+ **Example with i18n middleware:**
405
+
406
+ ```javascript
407
+ const Koa = require('koa');
408
+ const koaClassicServer = require('koa-classic-server');
409
+
410
+ const app = new Koa();
411
+
412
+ // i18n middleware that rewrites URLs
413
+ app.use(async (ctx, next) => {
414
+ if (ctx.path.match(/^\/it\//)) {
415
+ ctx.url = ctx.path.replace(/^\/it/, ''); // /it/page.html → /page.html
416
+ }
417
+ await next();
418
+ });
419
+
420
+ // Serve files using rewritten URL
421
+ app.use(koaClassicServer(__dirname + '/www', {
422
+ useOriginalUrl: false // Use ctx.url (rewritten) instead of ctx.originalUrl
423
+ }));
424
+
425
+ app.listen(3000);
426
+ ```
427
+
428
+ **How it works:**
429
+ - Request: `GET /it/page.html`
430
+ - `ctx.originalUrl`: `/it/page.html` (unchanged)
431
+ - `ctx.url`: `/page.html` (rewritten by middleware)
432
+ - With `useOriginalUrl: false`: Server looks for `/www/page.html` ✅
433
+ - With `useOriginalUrl: true` (default): Server looks for `/www/it/page.html` ❌ 404
389
434
 
390
435
  ---
391
436
 
@@ -511,7 +556,7 @@ npm run test:performance
511
556
  ```
512
557
 
513
558
  **Test Coverage:**
514
- - ✅ 153 tests passing
559
+ - ✅ 197 tests passing
515
560
  - ✅ Security tests (path traversal, XSS, race conditions)
516
561
  - ✅ EJS template integration tests
517
562
  - ✅ Index option tests (strings, arrays, RegExp)