diff --git a/.changeset/brown-numbers-prove.md b/.changeset/brown-numbers-prove.md new file mode 100644 index 000000000..96db75af0 --- /dev/null +++ b/.changeset/brown-numbers-prove.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Displays a new config error if `outDir` is placed within `publicDir`. diff --git a/.changeset/rich-tigers-march.md b/.changeset/rich-tigers-march.md new file mode 100644 index 000000000..fb698048f --- /dev/null +++ b/.changeset/rich-tigers-march.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': patch +--- + +Fix an issue where `express` couldn't use the `handler` in `middleware` mode. diff --git a/.changeset/unlucky-cougars-heal.md b/.changeset/unlucky-cougars-heal.md new file mode 100644 index 000000000..a6579499e --- /dev/null +++ b/.changeset/unlucky-cougars-heal.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Make typing of `defineCollection` more permissive to support advanced union and intersection types diff --git a/packages/astro/content-types.template.d.ts b/packages/astro/content-types.template.d.ts index bcd839f78..596764fe6 100644 --- a/packages/astro/content-types.template.d.ts +++ b/packages/astro/content-types.template.d.ts @@ -34,12 +34,9 @@ declare module 'astro:content' { type BaseSchemaWithoutEffects = | import('astro/zod').AnyZodObject - | import('astro/zod').ZodUnion + | import('astro/zod').ZodUnion<[BaseSchemaWithoutEffects, ...BaseSchemaWithoutEffects[]]> | import('astro/zod').ZodDiscriminatedUnion - | import('astro/zod').ZodIntersection< - import('astro/zod').AnyZodObject, - import('astro/zod').AnyZodObject - >; + | import('astro/zod').ZodIntersection; type BaseSchema = | BaseSchemaWithoutEffects diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 897ebabf6..0717e1a81 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -399,14 +399,18 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) { // Handle `base` trailing slash based on `trailingSlash` config if (config.trailingSlash === 'never') { config.base = prependForwardSlash(removeTrailingForwardSlash(config.base)); - } else if (config.trailingSlash === 'always') { + } else if (config.trailingSlash === 'always') { config.base = prependForwardSlash(appendForwardSlash(config.base)); } else { config.base = prependForwardSlash(config.base); - } + } - return config; - }); + return config; + }) + .refine((obj) => !obj.outDir.toString().startsWith(obj.publicDir.toString()), { + message: + '`outDir` must not be placed inside `publicDir` to prevent an infinite loop. Please adjust the directory configuration and try again', + }); return AstroConfigRelativeSchema; } diff --git a/packages/astro/test/units/config/config-validate.test.js b/packages/astro/test/units/config/config-validate.test.js index 49fd6b418..604836747 100644 --- a/packages/astro/test/units/config/config-validate.test.js +++ b/packages/astro/test/units/config/config-validate.test.js @@ -68,4 +68,14 @@ describe('Config Validation', () => { ).catch((err) => err); expect(configError).to.be.not.instanceOf(Error); }); + it('Error when outDir is placed within publicDir', async () => { + const configError = await validateConfig({ outDir: './public/dist' }, process.cwd()).catch( + (err) => err + ); + expect(configError instanceof z.ZodError).to.equal(true); + expect(configError.errors[0].message).to.equal( + '`outDir` must not be placed inside `publicDir` to prevent an infinite loop. \ +Please adjust the directory configuration and try again' + ); + }); }); diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index d5df644f8..37e1c6101 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -49,6 +49,7 @@ "cheerio": "1.0.0-rc.12", "mocha": "^9.2.2", "node-mocks-http": "^1.13.0", - "undici": "^5.22.1" + "undici": "^5.22.1", + "express": "^4.18.2" } } diff --git a/packages/integrations/node/src/nodeMiddleware.ts b/packages/integrations/node/src/nodeMiddleware.ts index fa0cdfda8..1e0aaea0f 100644 --- a/packages/integrations/node/src/nodeMiddleware.ts +++ b/packages/integrations/node/src/nodeMiddleware.ts @@ -6,15 +6,22 @@ import { responseIterator } from './response-iterator'; import type { ErrorHandlerParams, Options, RequestHandlerParams } from './types'; // Disable no-unused-vars to avoid breaking signature change -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export default function (app: NodeApp, _mode: Options['mode']) { +export default function (app: NodeApp, mode: Options['mode']) { return async function (...args: RequestHandlerParams | ErrorHandlerParams) { let error = null; - let [req, res, next, locals] = args as RequestHandlerParams; + let locals; + let [req, res, next] = args as RequestHandlerParams; + if (mode === 'middleware') { + let { [3]: _locals } = args; + locals = _locals; + } if (args[0] instanceof Error) { - [error, req, res, next, locals] = args as ErrorHandlerParams; - + [error, req, res, next] = args as ErrorHandlerParams; + if (mode === 'middleware') { + let { [4]: _locals } = args as ErrorHandlerParams; + locals = _locals; + } if (error) { if (next) { return next(error); diff --git a/packages/integrations/node/test/fixtures/node-middleware/src/pages/ssr.ts b/packages/integrations/node/test/fixtures/node-middleware/src/pages/ssr.ts new file mode 100644 index 000000000..93543190f --- /dev/null +++ b/packages/integrations/node/test/fixtures/node-middleware/src/pages/ssr.ts @@ -0,0 +1,9 @@ +export async function get() { + let number = Math.random(); + return { + body: JSON.stringify({ + number, + message: `Here's a random number: ${number}`, + }), + }; +} diff --git a/packages/integrations/node/test/node-middleware.test.js b/packages/integrations/node/test/node-middleware.test.js index d7ba79e4c..009f403c2 100644 --- a/packages/integrations/node/test/node-middleware.test.js +++ b/packages/integrations/node/test/node-middleware.test.js @@ -2,6 +2,7 @@ import nodejs from '../dist/index.js'; import { loadFixture } from './test-utils.js'; import { expect } from 'chai'; import * as cheerio from 'cheerio'; +import express from 'express'; /** * @typedef {import('../../../astro/test/test-utils').Fixture} Fixture @@ -14,7 +15,7 @@ async function load() { return mod; } -describe('behavior from middleware', () => { +describe('behavior from middleware, standalone', () => { /** @type {import('./test-utils').Fixture} */ let fixture; let server; @@ -53,3 +54,42 @@ describe('behavior from middleware', () => { }); }); }); + +describe('behavior from middleware, middleware', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + let server; + + before(async () => { + process.env.ASTRO_NODE_AUTOSTART = 'disabled'; + process.env.PRERENDER = false; + fixture = await loadFixture({ + root: './fixtures/node-middleware/', + output: 'server', + adapter: nodejs({ mode: 'middleware' }), + }); + await fixture.build(); + const { handler } = await load(); + const app = express(); + app.use(handler); + server = app.listen(8888); + }); + + after(async () => { + server.close(); + await fixture.clean(); + delete process.env.PRERENDER; + }); + + it('when mode is standalone', async () => { + const res = await fetch(`http://localhost:8888/ssr`); + + expect(res.status).to.equal(200); + + const html = await res.text(); + const $ = cheerio.load(html); + + const body = $('body'); + expect(body.text()).to.contain("Here's a random number"); + }); +});