From ba476e0df474e5c4bed40ec23e1381db36ed1fc6 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 17 Apr 2025 12:58:17 +0100 Subject: [PATCH] fix: clone actionAPIContext preserving accessors --- .changeset/dirty-dryers-rush.md | 5 +++++ .../src/actions/runtime/virtual/server.ts | 22 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .changeset/dirty-dryers-rush.md diff --git a/.changeset/dirty-dryers-rush.md b/.changeset/dirty-dryers-rush.md new file mode 100644 index 000000000000..5950bde791ea --- /dev/null +++ b/.changeset/dirty-dryers-rush.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug that caused a session error to be logged when using actions without sessions diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index fe046701dbf3..9fb560befd9d 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -300,13 +300,21 @@ export function getActionContext(context: APIContext): AstroActionContext { } throw e; } - const { - props: _props, - getActionResult: _getActionResult, - callAction: _callAction, - redirect: _redirect, - ...actionAPIContext - } = context; + + const omitKeys = ['props', 'getActionResult', 'callAction', 'redirect']; + + // Clones the context, preserving accessors and methods but omitting + // the properties that are not needed in the action handler. + const actionAPIContext = Object.create( + Object.getPrototypeOf(context), + Object.fromEntries( + Object.entries(Object.getOwnPropertyDescriptors(context)) + .filter(([key]) => + !omitKeys.includes(key) + ) + ) + ); + Reflect.set(actionAPIContext, ACTION_API_CONTEXT_SYMBOL, true); const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext); return handler(input);