From: Justin Wind Date: Fri, 29 Apr 2022 00:54:13 +0000 (-0700) Subject: minor rearrangement in serveFile X-Git-Tag: v1.2.6^2~2 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=3254d4bd8914ab48cb12c5039c87e2beeca55f7a;p=squeep-api-dingus minor rearrangement in serveFile --- diff --git a/lib/dingus.js b/lib/dingus.js index 3ca710e..79cee2c 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -565,18 +565,20 @@ class Dingus { break; } const suffix = Enum.EncodingTypeSuffix[encoding]; - if (suffix) { - const encodedFilePath = `${filePath}${suffix}`; - const [ encodedStat, encodedData ] = await this._readFileInfo(encodedFilePath); - if (encodedStat) { - ([ stat, data ] = [ encodedStat, encodedData ]); - ctx.selectedEncoding = encoding; - Dingus.addEncodingHeader(res, encoding); - res.setHeader(Enum.Header.Vary, Enum.Header.AcceptEncoding); - this.logger.debug(_scope, 'serving encoded version', { ctx, encodedFilePath }); - } - break; + if (!suffix) { + this.logger.error(_scope, 'supported encoding missing mapped suffix', { ctx, encoding }); + continue; + } + const encodedFilePath = `${filePath}${suffix}`; + const [ encodedStat, encodedData ] = await this._readFileInfo(encodedFilePath); + if (encodedStat) { + ([ stat, data ] = [ encodedStat, encodedData ]); + ctx.selectedEncoding = encoding; + Dingus.addEncodingHeader(res, encoding); + res.setHeader(Enum.Header.Vary, Enum.Header.AcceptEncoding); + this.logger.debug(_scope, 'serving encoded version', { ctx, encodedFilePath }); } + break; } const lastModifiedDate = new Date(stat.mtimeMs); diff --git a/test/lib/dingus.js b/test/lib/dingus.js index c889215..ab61256 100644 --- a/test/lib/dingus.js +++ b/test/lib/dingus.js @@ -821,6 +821,13 @@ Content-Type: image/sgi await dingus.serveFile(req, res, ctx, directory, fileName); assert(res.end.called); }); + it('handles misconfigured encoding', async function () { + Enum.EncodingType.Flarp = 'flarp'; + req._headers[Enum.Header.AcceptEncoding] = 'flarp, gzip'; + await dingus.serveFile(req, res, ctx, directory, fileName); + delete Enum.EncodingType.Flarp; + assert(res.end.called); + }); }); // serveFile describe('renderError', function () {