[ci] format

This commit is contained in:
matthewp 2022-11-11 16:30:25 +00:00 committed by fredkbot
parent 9eee0f0166
commit 8aea2dea61
5 changed files with 129 additions and 98 deletions

View file

@ -74,10 +74,7 @@ export const AstroConfigSchema = z.object({
.url() .url()
.optional() .optional()
.transform((val) => (val ? appendForwardSlash(val) : val)), .transform((val) => (val ? appendForwardSlash(val) : val)),
base: z base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base),
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.base),
trailingSlash: z trailingSlash: z
.union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .union([z.literal('always'), z.literal('never'), z.literal('ignore')])
.optional() .optional()
@ -326,11 +323,11 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) {
config.build.client = new URL('./dist/client/', config.outDir); config.build.client = new URL('./dist/client/', config.outDir);
} }
const trimmedBase = trimSlashes(config.base); const trimmedBase = trimSlashes(config.base);
if(trimmedBase.length && config.trailingSlash === 'never') { if (trimmedBase.length && config.trailingSlash === 'never') {
config.base = prependForwardSlash(trimmedBase); config.base = prependForwardSlash(trimmedBase);
} else { } else {
config.base = prependForwardSlash(appendForwardSlash(trimmedBase)); config.base = prependForwardSlash(appendForwardSlash(trimmedBase));
} }
return config; return config;
}); });

View file

@ -61,7 +61,11 @@ function getParts(part: string, file: string) {
return result; return result;
} }
function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: AstroConfig['trailingSlash']) { function getPattern(
segments: RoutePart[][],
base: string,
addTrailingSlash: AstroConfig['trailingSlash']
) {
const pathname = segments const pathname = segments
.map((segment) => { .map((segment) => {
if (segment.length === 1 && segment[0].spread) { if (segment.length === 1 && segment[0].spread) {
@ -94,7 +98,7 @@ function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: Ast
const trailing = const trailing =
addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$'; addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$';
let initial = '\\/'; let initial = '\\/';
if(addTrailingSlash === 'never' && base !== '/') { if (addTrailingSlash === 'never' && base !== '/') {
initial = ''; initial = '';
} }
return new RegExp(`^${pathname || initial}${trailing}`); return new RegExp(`^${pathname || initial}${trailing}`);

View file

@ -27,7 +27,7 @@ export async function handleRequest(
const url = new URL(origin + req.url); const url = new URL(origin + req.url);
let pathname: string; let pathname: string;
if(config.trailingSlash === 'never' && !req.url) { if (config.trailingSlash === 'never' && !req.url) {
pathname = ''; pathname = '';
} else { } else {
pathname = decodeURI(url.pathname); pathname = decodeURI(url.pathname);

View file

@ -9,97 +9,121 @@ describe('base configuration', () => {
describe('with trailingSlash: "never"', () => { describe('with trailingSlash: "never"', () => {
describe('index route', () => { describe('index route', () => {
it('Requests that include a trailing slash 404', async () => { it('Requests that include a trailing slash 404', async () => {
const fs = createFs({ const fs = createFs(
'/src/pages/index.astro': `<h1>testing</h1>`, {
}, root); '/src/pages/index.astro': `<h1>testing</h1>`,
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
}, },
}, async (container) => { root
const { req, res, done } = createRequestAndResponse({ );
method: 'GET',
url: '/docs/', await runInContainer(
}); {
container.handle(req, res); fs,
await done; root,
expect(res.statusCode).to.equal(404); userConfig: {
}); base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
}
);
}); });
it('Requests that exclude a trailing slash 200', async () => { it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({ const fs = createFs(
'/src/pages/index.astro': `<h1>testing</h1>`, {
}, root); '/src/pages/index.astro': `<h1>testing</h1>`,
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
}, },
}, async (container) => { root
const { req, res, done } = createRequestAndResponse({ );
method: 'GET',
url: '/docs', await runInContainer(
}); {
container.handle(req, res); fs,
await done; root,
expect(res.statusCode).to.equal(200); userConfig: {
}); base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
}
);
}); });
}); });
describe('sub route', () => { describe('sub route', () => {
it('Requests that include a trailing slash 404', async () => { it('Requests that include a trailing slash 404', async () => {
const fs = createFs({ const fs = createFs(
'/src/pages/sub/index.astro': `<h1>testing</h1>`, {
}, root); '/src/pages/sub/index.astro': `<h1>testing</h1>`,
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
}, },
}, async (container) => { root
const { req, res, done } = createRequestAndResponse({ );
method: 'GET',
url: '/docs/sub/', await runInContainer(
}); {
container.handle(req, res); fs,
await done; root,
expect(res.statusCode).to.equal(404); userConfig: {
}); base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub/',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(404);
}
);
}); });
it('Requests that exclude a trailing slash 200', async () => { it('Requests that exclude a trailing slash 200', async () => {
const fs = createFs({ const fs = createFs(
'/src/pages/sub/index.astro': `<h1>testing</h1>`, {
}, root); '/src/pages/sub/index.astro': `<h1>testing</h1>`,
await runInContainer({
fs,
root,
userConfig: {
base: '/docs',
trailingSlash: 'never',
}, },
}, async (container) => { root
const { req, res, done } = createRequestAndResponse({ );
method: 'GET',
url: '/docs/sub', await runInContainer(
}); {
container.handle(req, res); fs,
await done; root,
expect(res.statusCode).to.equal(200); userConfig: {
}); base: '/docs',
trailingSlash: 'never',
},
},
async (container) => {
const { req, res, done } = createRequestAndResponse({
method: 'GET',
url: '/docs/sub',
});
container.handle(req, res);
await done;
expect(res.statusCode).to.equal(200);
}
);
}); });
}); });
}); });

View file

@ -9,17 +9,23 @@ const root = new URL('../../fixtures/alias/', import.meta.url);
describe('routing - createRouteManifest', () => { describe('routing - createRouteManifest', () => {
it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => { it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => {
const fs = createFs({ const fs = createFs(
'/src/pages/index.astro': `<h1>test</h1>`, {
}, root); '/src/pages/index.astro': `<h1>test</h1>`,
const settings = await createDefaultDevSettings({ },
base: '/search', root
trailingSlash: 'never' );
}, root); const settings = await createDefaultDevSettings(
{
base: '/search',
trailingSlash: 'never',
},
root
);
const manifest = createRouteManifest({ const manifest = createRouteManifest({
cwd: fileURLToPath(root), cwd: fileURLToPath(root),
settings, settings,
fsMod: fs fsMod: fs,
}); });
const [{ pattern }] = manifest.routes; const [{ pattern }] = manifest.routes;
expect(pattern.test('')).to.equal(true); expect(pattern.test('')).to.equal(true);