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 +79 -34
- package/__tests__/caching-headers.test.js +556 -0
- package/__tests__/deprecation-warnings.test.js +217 -0
- package/__tests__/publicWwwTest/test-page.html +1 -0
- package/__tests__/useOriginalUrl.test.js +213 -0
- package/docs/CHANGELOG.md +125 -0
- package/index.cjs +55 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,37 +4,30 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/koa-classic-server)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
|
-
[]()
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
## 🎉 Version 2.
|
|
11
|
+
## 🎉 Version 2.X - Production-Ready Release
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
The 2.X series brings major performance improvements, enhanced security, and powerful new features while maintaining full backward compatibility.
|
|
14
14
|
|
|
15
|
-
###
|
|
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** -
|
|
23
|
+
✅ **HTTP Caching** - ETag and Last-Modified headers with 304 responses
|
|
27
24
|
✅ **Async/Await** - Non-blocking I/O for high performance
|
|
28
|
-
✅ **
|
|
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
|
|
37
|
-
✅ **Security
|
|
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
|
-
|
|
105
|
-
|
|
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
|
-
|
|
233
|
-
|
|
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 `
|
|
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
|
-
|
|
285
|
-
|
|
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
|
-
|
|
372
|
-
|
|
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
|
-
| `
|
|
388
|
-
| `
|
|
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
|
-
- ✅
|
|
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)
|