@@ -30,7 +30,6 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
3030/**
3131 * @typedef {object } Extra
3232 * @property {import("fs").Stats= } stats stats
33- * @property {number= } errorCode error code
3433 * @property {boolean= } immutable true when immutable, otherwise false
3534 */
3635
@@ -42,30 +41,31 @@ const UP_PATH_REGEXP = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
4241 * @returns {string }
4342 */
4443
45- // TODO refactor me in the next major release, this function should return `{ filename, stats, error }`
4644// TODO fix redirect logic when `/` at the end, like https://github.com/pillarjs/send/blob/master/index.js#L586
4745/**
4846 * @template {IncomingMessage} Request
4947 * @template {ServerResponse} Response
5048 * @param {import("../index.js").FilledContext<Request, Response> } context context
5149 * @param {string } url url
5250 * @param {Extra= } extra extra
53- * @returns {string | undefined } filename
51+ * @returns {{ filename?: string, extra: Extra, errorCode?: number } } filename
5452 */
5553function getFilenameFromUrl ( context , url , extra = { } ) {
5654 const { options } = context ;
5755 const paths = getPaths ( context ) ;
5856
5957 /** @type {string | undefined } */
6058 let foundFilename ;
59+ /** @type {number | undefined } */
60+ let errorCode ;
6161 /** @type {import("node:url").Url } */
6262 let urlObject ;
6363
6464 try {
6565 // The `url` property of the `request` is contains only `pathname`, `search` and `hash`
6666 urlObject = memoizedParse ( url , false , true ) ;
6767 } catch {
68- return ;
68+ return { errorCode , filename : foundFilename , extra } ;
6969 }
7070
7171 for ( const { publicPath, outputPath, assetsInfo } of paths ) {
@@ -94,16 +94,16 @@ function getFilenameFromUrl(context, url, extra = {}) {
9494 ) {
9595 // Null byte(s)
9696 if ( pathname . includes ( "\0" ) ) {
97- extra . errorCode = 400 ;
97+ errorCode = 400 ;
9898
99- return ;
99+ return { errorCode , filename : foundFilename , extra } ;
100100 }
101101
102102 // ".." is malicious
103103 if ( UP_PATH_REGEXP . test ( path . normalize ( `./${ pathname } ` ) ) ) {
104- extra . errorCode = 403 ;
104+ errorCode = 403 ;
105105
106- return ;
106+ return { errorCode , filename : foundFilename , extra } ;
107107 }
108108
109109 // Strip the `pathname` property from the `publicPath` option from the start of requested url
@@ -161,7 +161,7 @@ function getFilenameFromUrl(context, url, extra = {}) {
161161 }
162162 }
163163
164- return foundFilename ;
164+ return { filename : foundFilename , extra , errorCode } ;
165165}
166166
167167module . exports = getFilenameFromUrl ;
0 commit comments