koa-classic-server 4.2.0 → 4.2.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.
Files changed (2) hide show
  1. package/index.cjs +19 -17
  2. package/package.json +1 -1
package/index.cjs CHANGED
@@ -2042,8 +2042,11 @@ module.exports = function koaClassicServer(
2042
2042
  }
2043
2043
  }
2044
2044
 
2045
- // No index file found, show directory listing
2046
- ctx.body = await show_dir(toOpen, ctx);
2045
+ // No index file found, show directory listing. On a readdir
2046
+ // failure show_dir writes the 500 error page itself and returns
2047
+ // undefined — don't clobber that body with the listing assignment.
2048
+ const listing = await show_dir(toOpen, ctx);
2049
+ if (listing !== undefined) ctx.body = listing;
2047
2050
  } else {
2048
2051
  // Directory listing disabled
2049
2052
  await sendErrorPage(ctx, 404);
@@ -2534,21 +2537,13 @@ module.exports = function koaClassicServer(
2534
2537
  }
2535
2538
  } catch (error) {
2536
2539
  _logger.error('Directory read error:', error);
2537
- ctx.status = 500;
2538
- setGeneratedPageHeaders(ctx, NOT_FOUND_CSP);
2539
- return `
2540
- <!DOCTYPE html>
2541
- <html>
2542
- <head>
2543
- <meta charset="UTF-8">
2544
- <title>Error</title>
2545
- </head>
2546
- <body>
2547
- <h1>Error Reading Directory</h1>
2548
- <p>Unable to access directory contents.</p>
2549
- </body>
2550
- </html>
2551
- `;
2540
+ // Route through the unified writer so this 500 honors errorPages[500]
2541
+ // (custom page when configured), the no-store / header-scrub, and the
2542
+ // generated-page security headers — like every other 500. sendErrorPage
2543
+ // sets ctx.status/headers/body itself; returning undefined signals the
2544
+ // caller not to overwrite the body it just wrote.
2545
+ await sendErrorPage(ctx, 500);
2546
+ return undefined;
2552
2547
  }
2553
2548
 
2554
2549
  // Relative path of this directory from rootDir (used for alwaysHide path matching)
@@ -2868,4 +2863,11 @@ module.exports._internals = {
2868
2863
  singleFlight,
2869
2864
  refreshOrInsert,
2870
2865
  escapeHtml,
2866
+ // writeErrorPage is the shared output path for every middleware-generated error
2867
+ // response (sendErrorPage / sendErrorPageSync delegate to it). Exposed so its
2868
+ // contract — header scrub, no-store on >=500, Content-Type, custom-vs-built-in
2869
+ // body, CSP only for the built-in page — can be asserted deterministically: the
2870
+ // stream-failure branches that also use it can't be, because Koa 3 tears the
2871
+ // socket down on a mid-stream body error before the client sees the response.
2872
+ writeErrorPage,
2871
2873
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa-classic-server",
3
- "version": "4.2.0",
3
+ "version": "4.2.1",
4
4
  "description": "High-performance Koa middleware for serving static files with classic directory listing (similar, but not identical, to Apache 2), HTTP caching, template engine support, and comprehensive security fixes",
5
5
  "main": "index.cjs",
6
6
  "exports": {