Skip to content

Commit

Permalink
fix(actions): internal symbol check (#12424)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Nov 14, 2024
1 parent b745e38 commit 4364bff
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-bulldogs-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where an incorrect usage of Astro actions was lost when porting the fix from v4 to v5
6 changes: 5 additions & 1 deletion packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { AstroError } from '../../../core/errors/errors.js';
import type { APIContext } from '../../../types/public/index.js';
import { ACTION_RPC_ROUTE_PATTERN } from '../../consts.js';
import {
ACTION_API_CONTEXT_SYMBOL,
type ActionAPIContext,
type ErrorInferenceObject,
type MaybePromise,
formContentTypes,
hasContentType,
isActionAPIContext,
} from '../utils.js';
import type { Locals } from '../utils.js';
import { getAction } from './get-action.js';
Expand Down Expand Up @@ -79,7 +81,8 @@ export function defineAction<
: getJsonServerHandler(handler, inputSchema);

async function safeServerHandler(this: ActionAPIContext, unparsedInput: unknown) {
if (typeof this === 'function') {
// The ActionAPIContext should always contain the `params` property
if (typeof this === 'function' || !isActionAPIContext(this)) {
throw new AstroError(ActionCalledFromServerError);
}
return callSafely(() => serverHandler(unparsedInput, this));
Expand Down Expand Up @@ -293,6 +296,7 @@ export function getActionContext(context: APIContext): ActionMiddlewareContext {
redirect: _redirect,
...actionAPIContext
} = context;
Reflect.set(actionAPIContext, ACTION_API_CONTEXT_SYMBOL, true);
const handler = baseAction.bind(actionAPIContext satisfies ActionAPIContext);
return handler(input);
},
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/actions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type fsMod from 'node:fs';
import * as eslexer from 'es-module-lexer';
import type { APIContext } from '../types/public/context.js';
import type { ActionAPIContext, Locals } from './runtime/utils.js';
import { ACTION_API_CONTEXT_SYMBOL, type ActionAPIContext, type Locals } from './runtime/utils.js';
import { deserializeActionResult, getActionQueryString } from './runtime/virtual/shared.js';

export function hasActionPayload(locals: APIContext['locals']): locals is Locals {
Expand All @@ -22,6 +22,7 @@ export function createGetActionResult(locals: APIContext['locals']): APIContext[

export function createCallAction(context: ActionAPIContext): APIContext['callAction'] {
return (baseAction, input) => {
Reflect.set(context, ACTION_API_CONTEXT_SYMBOL, true);
const action = baseAction.bind(context);
return action(input) as any;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
import { actions } from "astro:actions";
// this is invalid, it should fail
const result = await actions.imageUploadInChunks();
const result = await actions.subscribe({ channel: "hey" });
---

0 comments on commit 4364bff

Please sign in to comment.