Skip to content

Commit f361730

Browse files
feat(sessions): drivers (#15006)
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
1 parent 31074fc commit f361730

29 files changed

Lines changed: 428 additions & 238 deletions

File tree

.changeset/bright-parrots-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Removes session `test` driver - ([v6 upgrade guidance](https://v6.docs.astro.build/en/guides/upgrade-to/v6/#removed-session-test-driver))

.changeset/clever-clubs-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Deprecates session driver string signature - ([v6 upgrade guidance](https://v6.docs.astro.build/en/guides/upgrade-to/v6/#deprecated-session-driver-string-signature))

.changeset/solid-fans-jam.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
'@astrojs/cloudflare': minor
3+
'@astrojs/netlify': minor
4+
'@astrojs/node': minor
5+
'astro': minor
6+
---
7+
8+
Adds new session driver object shape
9+
10+
For greater flexibility and improved consistency with other Astro code, session drivers are now specified as an object:
11+
12+
```diff
13+
-import { defineConfig } from 'astro/config'
14+
+import { defineConfig, sessionDrivers } from 'astro/config'
15+
16+
export default defineConfig({
17+
session: {
18+
- driver: 'redis',
19+
- options: {
20+
- url: process.env.REDIS_URL
21+
- },
22+
+ driver: sessionDrivers.redis({
23+
+ url: process.env.REDIS_URL
24+
+ }),
25+
}
26+
})
27+
```
28+
29+
Specifying the session driver as a string has been deprecated, but will continue to work until this feature is removed completely in a future major version. The object shape is the current recommended and documented way to configure a session driver.

packages/astro/src/config/entrypoint.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { mergeConfig } from '../core/config/merge.js';
1010
export { validateConfig } from '../core/config/validate.js';
1111
export { envField } from '../env/config.js';
1212
export { defineConfig, getViteConfig } from './index.js';
13+
export { sessionDrivers } from '../core/session/drivers.js';
1314

1415
/**
1516
* Return the configuration needed to use the Sharp-based image service

packages/astro/src/config/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import type { UserConfig as ViteUserConfig, UserConfigFn as ViteUserConfigFn } from 'vite';
22
import { createRoutesList } from '../core/routing/index.js';
3-
import type {
4-
AstroInlineConfig,
5-
AstroUserConfig,
6-
Locales,
7-
SessionDriverName,
8-
} from '../types/public/config.js';
3+
import type { AstroInlineConfig, AstroUserConfig, Locales } from '../types/public/config.js';
4+
import type { SessionDriverConfig, SessionDriverName } from '../core/session/types.js';
95

106
/**
117
* See the full Astro Configuration API Documentation
128
* https://astro.build/config
139
*/
1410
export function defineConfig<
1511
const TLocales extends Locales = never,
16-
const TDriver extends SessionDriverName = never,
12+
const TDriver extends SessionDriverName | SessionDriverConfig = never,
1713
>(config: AstroUserConfig<TLocales, TDriver>) {
1814
return config;
1915
}

packages/astro/src/core/app/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { type CreateRenderContext, RenderContext } from '../render-context.js';
2727
import { redirectTemplate } from '../routing/3xx.js';
2828
import { ensure404Route } from '../routing/astro-designed-error-pages.js';
2929
import { matchRoute } from '../routing/match.js';
30-
import { type AstroSession, PERSIST_SYMBOL } from '../session.js';
30+
import { type AstroSession, PERSIST_SYMBOL } from '../session/runtime.js';
3131
import type { AppPipeline } from './pipeline.js';
3232
import type { SSRManifest } from './types.js';
3333

packages/astro/src/core/app/types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type {
66
CspAlgorithm,
77
Locales,
88
RemotePattern,
9-
ResolvedSessionConfig,
109
} from '../../types/public/config.js';
1110
import type {
1211
RouteData,
@@ -17,8 +16,8 @@ import type {
1716
import type { SinglePageBuiltModule } from '../build/types.js';
1817
import type { CspDirective } from '../csp/config.js';
1918
import type { LoggerLevel } from '../logger/core.js';
20-
import type { SessionDriver } from '../session.js';
2119
import type { RoutingStrategies } from './common.js';
20+
import type { BaseSessionConfig, SessionDriverFactory } from '../session/types.js';
2221

2322
type ComponentPath = string;
2423

@@ -96,10 +95,10 @@ export type SSRManifest = {
9695
i18n: SSRManifestI18n | undefined;
9796
middleware?: () => Promise<AstroMiddlewareInstance> | AstroMiddlewareInstance;
9897
actions?: () => Promise<SSRActions> | SSRActions;
99-
sessionDriver?: () => Promise<{ default: SessionDriver | null }>;
98+
sessionDriver?: () => Promise<{ default: SessionDriverFactory | null }>;
10099
checkOrigin: boolean;
101100
allowedDomains?: Partial<RemotePattern>[];
102-
sessionConfig?: ResolvedSessionConfig<any>;
101+
sessionConfig?: SSRManifestSession;
103102
cacheDir: URL;
104103
srcDir: URL;
105104
outDir: URL;
@@ -152,6 +151,11 @@ export type SSRManifestCSP = {
152151
directives: CspDirective[];
153152
};
154153

154+
export interface SSRManifestSession extends BaseSessionConfig {
155+
driver: string;
156+
options?: Record<string, any> | undefined;
157+
}
158+
155159
/** Public type exposed through the `astro:build:ssr` integration hook */
156160
export type SerializedSSRManifest = Omit<
157161
SSRManifest,

packages/astro/src/core/base-pipeline.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { sequence } from './middleware/sequence.js';
2323
import { RedirectSinglePageBuiltModule } from './redirects/index.js';
2424
import { RouteCache } from './render/route-cache.js';
2525
import { createDefaultRoutes } from './routing/default.js';
26-
import type { SessionDriver } from './session.js';
26+
import type { SessionDriverFactory } from './session/types.js';
2727

2828
/**
2929
* The `Pipeline` represents the static parts of rendering that do not change between requests.
@@ -35,7 +35,7 @@ export abstract class Pipeline {
3535
readonly internalMiddleware: MiddlewareHandler[];
3636
resolvedMiddleware: MiddlewareHandler | undefined = undefined;
3737
resolvedActions: SSRActions | undefined = undefined;
38-
resolvedSessionDriver: SessionDriver | null | undefined = undefined;
38+
resolvedSessionDriver: SessionDriverFactory | null | undefined = undefined;
3939

4040
constructor(
4141
readonly logger: Logger,
@@ -143,7 +143,7 @@ export abstract class Pipeline {
143143
return NOOP_ACTIONS_MOD;
144144
}
145145

146-
async getSessionDriver(): Promise<SessionDriver | null> {
146+
async getSessionDriver(): Promise<SessionDriverFactory | null> {
147147
// Return cached value if already resolved (including null)
148148
if (this.resolvedSessionDriver !== undefined) {
149149
return this.resolvedSessionDriver;

packages/astro/src/core/build/plugins/plugin-manifest.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import type { BuildInternals } from '../internal.js';
3535
import { cssOrder, mergeInlineCss } from '../runtime.js';
3636
import type { StaticBuildOptions } from '../types.js';
3737
import { makePageDataKey } from './util.js';
38+
import { sessionConfigToManifest } from '../../session/utils.js';
3839

3940
/**
4041
* Unified manifest system architecture:
@@ -369,7 +370,7 @@ async function buildManifest(
369370
(settings.config.security?.checkOrigin && settings.buildOutput === 'server') ?? false,
370371
allowedDomains: settings.config.security?.allowedDomains,
371372
key: encodedKey,
372-
sessionConfig: settings.config.session,
373+
sessionConfig: sessionConfigToManifest(settings.config.session),
373374
csp,
374375
devToolbar: {
375376
enabled: false,

packages/astro/src/core/config/schemas/base.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { localFontFamilySchema, remoteFontFamilySchema } from '../../../assets/f
1313
import { EnvSchema } from '../../../env/schema.js';
1414
import type { AstroUserConfig, ViteUserConfig } from '../../../types/public/config.js';
1515
import { allowedDirectivesSchema, cspAlgorithmSchema, cspHashSchema } from '../../csp/config.js';
16+
import { SessionSchema } from '../../session/config.js';
1617

1718
// The below types are required boilerplate to workaround a Zod issue since v3.21.2. Since that version,
1819
// Zod's compiled TypeScript would "simplify" certain values to their base representation, causing references
@@ -95,7 +96,6 @@ export const ASTRO_CONFIG_DEFAULTS = {
9596
schema: {},
9697
validateSecrets: false,
9798
},
98-
session: undefined,
9999
prerenderConflictBehavior: 'warn',
100100
experimental: {
101101
clientPrerender: false,
@@ -466,30 +466,7 @@ export const AstroConfigSchema = z.object({
466466
.strict()
467467
.optional()
468468
.default(ASTRO_CONFIG_DEFAULTS.env),
469-
session: z
470-
.object({
471-
driver: z.string().optional(),
472-
options: z.record(z.string(), z.any()).optional(),
473-
cookie: z
474-
.object({
475-
name: z.string().optional(),
476-
domain: z.string().optional(),
477-
path: z.string().optional(),
478-
maxAge: z.number().optional(),
479-
sameSite: z.union([z.enum(['strict', 'lax', 'none']), z.boolean()]).optional(),
480-
secure: z.boolean().optional(),
481-
})
482-
.or(z.string())
483-
.transform((val) => {
484-
if (typeof val === 'string') {
485-
return { name: val };
486-
}
487-
return val;
488-
})
489-
.optional(),
490-
ttl: z.number().optional(),
491-
})
492-
.optional(),
469+
session: SessionSchema.optional(),
493470
prerenderConflictBehavior: z
494471
.enum(['error', 'warn', 'ignore'])
495472
.optional()

0 commit comments

Comments
 (0)