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

Actions: Add devalue for serializing complex values #11593

Merged
merged 15 commits into from
Aug 5, 2024

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Aug 1, 2024

Changes

Standardize our Actions serialization strategy. This introduces devalue to allow complex values like Dates and Sets, and adds serialization to middleware results for edge middleware support.

  • Abstract serialization and deserialization (with REST response metadata) to serializeActionResult and deserializeActionResult utils
  • Pass serializable values from middleware locals to ensure edge middleware works as expected
  • Update frontend fetch to user deserializer

Testing

  • Add integration test for Date and Set
  • Update expected headers and value checks to use devalue

Docs

N/A - should continue to work as expected. The RFC did not specify supported values

Copy link

changeset-bot bot commented Aug 1, 2024

🦋 Changeset detected

Latest commit: c4130c7

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Aug 1, 2024
@bholmesdev bholmesdev marked this pull request as ready for review August 1, 2024 16:55
callAction: APIContext['callAction'];
actionResult?: ReturnType<APIContext['getActionResult']>;
actionResult: SerializedActionResult;
actionName: string;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the getActionResult constructor to render-context. This ensures only serializable values are sent through locals for edge middleware support. It also simplified our code in the process by removing that nextLocalsStub utility!

@Fryuni
Copy link
Member

Fryuni commented Aug 1, 2024

Would URLs be a problem due to Rich-Harris/devalue#79?

@bholmesdev
Copy link
Contributor Author

bholmesdev commented Aug 1, 2024

@Fryuni Yes it would be. I recall we special case this for content collections. I'll see if we can reuse that code.

Update: added! Pretty easy to do

Comment on lines 84 to 85
message: body.message,
code: ActionError.statusToCode(body.status),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also anticipate if body is not an object? Or if the property access returns undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good call. We should add a check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: I just realized this was the base case when an Action error code not be parsed. I decided to simply return INTERNAL_SERVER_ERROR for that case, since that is unexpected.

@ascorbic
Copy link
Contributor

ascorbic commented Aug 5, 2024

I wonder if we should abstract our special devalue usage so that it's consistent across the different places we use it.

@bholmesdev
Copy link
Contributor Author

@ascorbic By "special devalue usage," do you mean for Actions specifically or Astro in general? I tried to consolidate everything into those serialize and deserialize functions for Actions.

@ascorbic
Copy link
Contributor

ascorbic commented Aug 5, 2024

@bholmesdev I mean in general. We're now using it in actions, content collection schema and it will also be used in the content layer data store. Not a blocker, but it would be good to not re-implement it every time.

@bholmesdev
Copy link
Contributor Author

@ascorbic Got it, agreed. I noticed that we're using eval / uneval for content collections and stringify / parse for actions, which is a difference. This is because content collections outputs a JS module (so eval is safe) while actions needs to serialize over the wire (so eval is unsafe).

I'd prefer to keep using eval for content collections due to this note in the devalue readme:

Use uneval when you want the most compact possible output and don't want to include any code for parsing the serialized value.

Think it's best to keep separate for now. If you can abstract for content collections v2, please do!

@bholmesdev bholmesdev merged commit 81d7150 into main Aug 5, 2024
13 checks passed
@bholmesdev bholmesdev deleted the feat/actions-edge-middleware branch August 5, 2024 12:22
This was referenced Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants