Skip to content

Commit

Permalink
fix: Internal encoding of non-html files upon serving to client
Browse files Browse the repository at this point in the history
  • Loading branch information
00Fjongl committed Dec 25, 2024
1 parent 94f87dc commit 0dce909
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/randomization.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ const randomListItem = (lis) => () => lis[(Math.random() * lis.length) | 0],
tryReadFile = (file, baseUrl) => {
file = fileURLToPath(new URL(file, baseUrl));
return existsSync(file + '')
? readFileSync(file + '', 'utf8')
? readFileSync(
file + '',
/\.(?:ico|png|jpg|jpeg)$/.test(file) ? undefined : 'utf8'
)
: preloaded404;
};

Expand Down
22 changes: 13 additions & 9 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,21 @@ app.get('/:path', (req, reply) => {
default: 'text/html',
html: 'text/html',
txt: 'text/plain',
xml: "application/xml",
xml: 'application/xml',
ico: 'image/vnd.microsoft.icon',
},
type = supportedTypes[fileName.slice(fileName.lastIndexOf('.') + 1)];

reply.type(type || supportedTypes.default);
reply.send(
paintSource(
loadTemplates(tryReadFile('../views/' + fileName, import.meta.url))
)
);
type =
supportedTypes[fileName.slice(fileName.lastIndexOf('.') + 1)] ||
supportedTypes.default;

reply.type(type);
if (type === supportedTypes.default)
reply.send(
paintSource(
loadTemplates(tryReadFile('../views/' + fileName, import.meta.url))
)
);
else reply.send(tryReadFile('../views/' + fileName, import.meta.url));
});

app.get('/github/:redirect', (req, reply) => {
Expand Down

0 comments on commit 0dce909

Please sign in to comment.