Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(actions): internal symbol check #12424

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" });
---
Loading