Skip to content

Commit

Permalink
refactor: readability (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Dec 4, 2024
1 parent 26976ad commit 2c36ee5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
38 changes: 19 additions & 19 deletions packages/node/src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export function createStaticHandler(app: NodeApp, options: Options) {
const [urlPath, urlQuery] = req.url.split('?');
const filePath = path.join(client, app.removeBase(urlPath));

let pathname: string;
let isDirectory = false;
try {
isDirectory = fs.lstatSync(filePath).isDirectory();
Expand All @@ -34,38 +33,39 @@ export function createStaticHandler(app: NodeApp, options: Options) {
const { trailingSlash = 'ignore' } = options;

const hasSlash = urlPath.endsWith('/');
let pathname = urlPath;

switch (trailingSlash) {
case 'never':
// biome-ignore lint/suspicious/noDoubleEquals: <explanation>
if (isDirectory && urlPath != '/' && hasSlash) {
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/suspicious/noFallthroughSwitchClause: <explanation>
case 'never': {
if (isDirectory && urlPath !== '/' && hasSlash) {
// biome-ignore lint/style/useTemplate: more readable like this
pathname = urlPath.slice(0, -1) + (urlQuery ? '?' + urlQuery : '');
res.statusCode = 301;
res.setHeader('Location', pathname);
return res.end();
// biome-ignore lint/style/noUselessElse: <explanation>
} else pathname = urlPath;
// intentionally fall through
case 'ignore':
{
if (isDirectory && !hasSlash) {
// biome-ignore lint/style/useTemplate: <explanation>
pathname = urlPath + '/index.html';
} else pathname = urlPath;
}
if (isDirectory && !hasSlash) {
pathname = `${urlPath}/index.html`;
}
break;
}
case 'ignore': {
if (isDirectory && !hasSlash) {
pathname = `${urlPath}/index.html`;
}
break;
case 'always':
}
case 'always': {
// trailing slash is not added to "subresources"
if (!hasSlash && !isSubresourceRegex.test(urlPath)) {
// biome-ignore lint/style/useTemplate: <explanation>
// biome-ignore lint/style/useTemplate: more readable like this
pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');
res.statusCode = 301;
res.setHeader('Location', pathname);
return res.end();
// biome-ignore lint/style/noUselessElse: <explanation>
} else pathname = urlPath;
}
break;
}
}
// app.removeBase sometimes returns a path without a leading slash
pathname = prependForwardSlash(app.removeBase(pathname));
Expand Down
1 change: 0 additions & 1 deletion packages/node/test/node-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ describe('behavior from middleware, middleware', () => {
it('when mode is middleware', async () => {
const res = await fetch('http://localhost:8889/ssr');

console.log(res);
assert.equal(res.status, 200);

const html = await res.text();
Expand Down
2 changes: 0 additions & 2 deletions packages/vercel/test/edge-middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ describe('Vercel edge middleware', () => {
// this is abysmal...
'../.vercel/output/functions/render.func/www/withastro/astro/packages/vercel/test/fixtures/middleware-with-edge-file/dist/middleware.mjs'
);
console.log(contents);
// assert.equal(contents.includes('title:')).to.be.true;
// chaiJestSnapshot.setTestName('Middleware with handler file');
// assert.equal(contents).to.matchSnapshot(true);
Expand All @@ -68,7 +67,6 @@ describe('Vercel edge middleware', () => {
// this is abysmal...
'../.vercel/output/functions/render.func/www/withastro/astro/packages/vercel/test/fixtures/middleware-without-edge-file/dist/middleware.mjs'
);
console.log(contents);
// assert.equal(contents.includes('title:')).to.be.false;
// chaiJestSnapshot.setTestName('Middleware without handler file');
// assert.equal(contents).to.matchSnapshot(true);
Expand Down
6 changes: 3 additions & 3 deletions scripts/notify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async function run() {
}

if (message.length < 2000) {
console.log(message);
console.info(message);
} else {
const { name, version, url } = packages.find((pkg) => pkg.name === 'astro') ?? packages[0];
message = `${emoji} Some ${descriptor} ${pluralize(verb)}\n\n`;
Expand All @@ -145,13 +145,13 @@ async function run() {
}

if (message.length < 2000) {
console.log(message);
console.info(message);
} else {
message = `${emoji} Some ${descriptor} ${pluralize(verb)}\n\n`;
message += `• \`${name}@${version}\` Read the [release notes →](<${url}>)\n`;

message += `\n\nAlso ${item(extraVerbs)}: ${remainingPackages.length} other packages!`;
console.log(message);
console.info(message);
}
}
}
Expand Down

0 comments on commit 2c36ee5

Please sign in to comment.