Skip to content

refactor(server): improve naming & types for hibernation apis - #685

Closed
dinwwwh wants to merge 4 commits into
mainfrom
refactor/server/improve-naming-and-types-for-hibernation-apis
Closed

refactor(server): improve naming & types for hibernation apis#685
dinwwwh wants to merge 4 commits into
mainfrom
refactor/server/improve-naming-and-types-for-hibernation-apis

Conversation

@dinwwwh

@dinwwwh dinwwwh commented Jun 25, 2025

Copy link
Copy Markdown
Member

refactor(standard-server): distinguish server/client HibernationEventIterator type by defining .then

The client await the result, so the reflected result type will be more accurate by utilize .then

Summary by CodeRabbit

  • New Features

    • Instances of hibernation event iterators can now be used with promise-like behavior, supporting the use of .then().
    • Added a function to selectively hide specified properties on objects, enhancing flexibility in object handling.
  • Refactor

    • The hibernation callback is now always required and invoked directly, simplifying usage.
    • Interface and property names related to hibernation context have been updated for consistency and clarity.
  • Tests

    • Added tests verifying the behavior of the new property-omitting function on plain objects and asynchronous iterator instances.

dinwwwh added 2 commits June 25, 2025 14:47
…Iterator type by defining .then

The client `await` the result, so the reflected result type will be more accurate by utilize .then
@vercel

vercel Bot commented Jun 25, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 8:38am

@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jun 25, 2025
@dinwwwh
dinwwwh requested a review from Copilot June 25, 2025 07:51
@coderabbitai

coderabbitai Bot commented Jun 25, 2025

Copy link
Copy Markdown

Walkthrough

The changes update the handling of the hibernationCallback property to enforce its presence, removing optional chaining in tests and server code. The experimental_HibernationEventIterator class is enhanced to implement PromiseLike, exposes hibernationCallback as a public readonly property, and adds a then method. Additionally, interface and symbol names in the hibernation plugin context are renamed for clarity. A new utility proxyOmit is introduced to create proxies that omit specified properties, with tests verifying its behavior on objects and async iterators.

Changes

File(s) Change Summary
packages/durable-event-iterator/src/durable-object/handler.test.ts Removed optional chaining when calling hibernationCallback in tests.
packages/standard-server/src/hibernation.test.ts Removed optional chaining from hibernationCallback invocation in test.
packages/standard-server-peer/src/server.ts Changed hibernationCallback invocation to be unconditional (no optional chaining).
packages/standard-server/src/hibernation.ts experimental_HibernationEventIterator now implements PromiseLike, exposes hibernationCallback as public readonly, and adds a then method.
packages/server/src/hibernation/plugin.ts Renamed interface and symbol for hibernation context; updated all references accordingly.
packages/shared/src/object.ts Added proxyOmit function to create proxies that omit specified properties, preserving method bindings.
packages/shared/src/object.test.ts Added tests for proxyOmit verifying property omission on plain objects and async iterator instances.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant HibernationEventIterator

    Client->>Server: Initiate request
    Server->>HibernationEventIterator: Create instance (with hibernationCallback)
    Server->>HibernationEventIterator: Call hibernationCallback(id)
    HibernationEventIterator-->>Server: Callback executed
    Server-->>Client: Respond
Loading

Possibly related PRs

  • unnoq/orpc#682: Changes ID types from number to string and modifies hibernationCallback usage in tests and modules, related to callback invocation style.

Poem

🐇 In code where hibernation dwells,
Callbacks now ring clear as bells.
No more "maybe"—they’re always there,
With PromiseLike we now prepare.
Renamed symbols, context neat—
This rabbit finds the change complete!
✨🐰


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a69d37d and 42d06be.

📒 Files selected for processing (3)
  • packages/shared/src/object.test.ts (2 hunks)
  • packages/shared/src/object.ts (1 hunks)
  • packages/standard-server/src/hibernation.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/shared/src/object.test.ts (2)
packages/shared/src/object.ts (1)
  • proxyOmit (95-124)
packages/shared/src/iterator.ts (1)
  • AsyncIteratorClass (23-96)
🪛 Biome (1.9.4)
packages/standard-server/src/hibernation.ts

[error] 22-22: Do not add then to a class.

(lint/suspicious/noThenProperty)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: publish-commit
  • GitHub Check: lint
🔇 Additional comments (4)
packages/shared/src/object.ts (1)

92-124: Well-implemented utility function with proper handling of function binding.

The proxyOmit function is correctly implemented with appropriate TypeScript types and handles the crucial function binding needed for async iterators to work properly when proxied.

packages/shared/src/object.test.ts (1)

122-147: Comprehensive test coverage for the new proxyOmit utility.

The tests properly cover both basic object property omission and the more complex async iterator use case, verifying type preservation and method functionality.

packages/standard-server/src/hibernation.ts (2)

22-28: Clever implementation using proxyOmit to prevent recursive calls.

The then method correctly implements the PromiseLike interface and uses proxyOmit to prevent recursive .then invocations. The static analysis warning about adding then to a class is a false positive since this class explicitly implements PromiseLike.


11-11: Good change making hibernationCallback required.

Making hibernationCallback a required public readonly property improves type safety and aligns with its usage patterns throughout the codebase.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @unnoq, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on refining the experimental_HibernationEventIterator API to enhance type safety and improve the client-side experience. The primary changes involve making the hibernationCallback property mandatory and enabling the iterator to be directly awaitable by implementing the PromiseLike interface. Additionally, several naming conventions within the hibernation plugin have been updated for improved code clarity and maintainability.

Highlights

  • Type Refinement for HibernationEventIterator: The hibernationCallback property on experimental_HibernationEventIterator is no longer optional (?). This indicates it's always expected to be present, simplifying usage and type checking across the codebase.
  • Promise-like HibernationEventIterator: The experimental_HibernationEventIterator now implements PromiseLike by providing a then method. This allows the iterator to be awaited directly by clients, which, as per the PR description, improves type accuracy for awaited results.
  • Naming Improvements in Hibernation Plugin: The experimental_HibernationContext interface was renamed to experimental_HibernationPluginContext, and the associated internal context symbol was also renamed for better clarity and distinction within the server's hibernation plugin.
  • Code Simplification: The removal of optional chaining (?.) for hibernationCallback calls in various files (tests, server peer) is a direct consequence of its new non-optional type, leading to cleaner code.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the hibernation APIs to improve naming and type safety, distinguishing server and client behavior by implementing a Promise-like then method. Key changes include:

  • Introducing a then method in experimental_HibernationEventIterator to support awaiting.
  • Removing optional chaining on hibernationCallback to enforce its presence.
  • Renaming the hibernation context interface and symbol in the Hibernation plugin for increased clarity and consistency.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/standard-server/src/hibernation.ts Adds a then method and changes hibernationCallback from optional to required.
packages/standard-server/src/hibernation.test.ts Updates tests to call hibernationCallback without optional chaining.
packages/standard-server-peer/src/server.ts Updates the usage of hibernationCallback to align with the new non-optional type.
packages/server/src/hibernation/plugin.ts Renames the context interface and symbol for better consistency.
packages/durable-event-iterator/src/durable-object/handler.test.ts Modifies tests to invoke hibernationCallback directly after the refactor.
Comments suppressed due to low confidence (5)

packages/standard-server/src/hibernation.ts:11

  • Since hibernationCallback is now required rather than optional, please update the documentation comments to reflect its non-optional nature and intended usage.
    public readonly hibernationCallback: experimental_HibernationEventIteratorCallback,

packages/standard-server/src/hibernation.ts:22

  • [nitpick] Consider adding an inline comment explaining the use of Promise.resolve(this) in the then method to clarify its intent and behavior for future maintainers.
  then<TResult1 = AsyncIteratorClass<T, TReturn, TNext>, TResult2 = never>(

packages/standard-server-peer/src/server.ts:94

  • Ensure that response.body always provides a valid hibernationCallback since the optional chaining has been removed and the callback is now mandatory.
            response.body.hibernationCallback(id)

packages/server/src/hibernation/plugin.ts:6

  • [nitpick] The renaming to experimental_HibernationPluginContext improves clarity; consider ensuring that all related documentation reflects this new naming convention.
export interface experimental_HibernationPluginContext {

packages/durable-event-iterator/src/durable-object/handler.test.ts:43

  • Since hibernationCallback is now required, verify that the tests consistently assume its presence and remove any redundant optional chaining checks.
      output.hibernationCallback(123)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the hibernation APIs to improve naming and type safety. The renaming of experimental_HibernationContext to experimental_HibernationPluginContext and the context symbol makes the code clearer and less prone to collisions.

The core change is making experimental_HibernationEventIterator implement PromiseLike. This is a clever approach to distinguish the server-side type (with hibernationCallback) from the type inferred by consumers (a standard AsyncIteratorClass), improving type accuracy for clients of the RPC framework.

The changes are well-implemented and consistent across the affected files. I have one suggestion to improve maintainability by adding a comment to explain the PromiseLike pattern in experimental_HibernationEventIterator.

Comment thread packages/standard-server/src/hibernation.ts
@pkg-pr-new

pkg-pr-new Bot commented Jun 25, 2025

Copy link
Copy Markdown
More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@685

@orpc/client

npm i https://pkg.pr.new/@orpc/client@685

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@685

@orpc/experimental-durable-event-iterator

npm i https://pkg.pr.new/@orpc/experimental-durable-event-iterator@685

@orpc/hey-api

npm i https://pkg.pr.new/@orpc/hey-api@685

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@685

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@685

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@685

@orpc/react

npm i https://pkg.pr.new/@orpc/react@685

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@685

@orpc/server

npm i https://pkg.pr.new/@orpc/server@685

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@685

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@685

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@685

@orpc/standard-server-aws-lambda

npm i https://pkg.pr.new/@orpc/standard-server-aws-lambda@685

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@685

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@685

@orpc/standard-server-peer

npm i https://pkg.pr.new/@orpc/standard-server-peer@685

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@685

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@685

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@685

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@685

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@685

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@685

commit: 42d06be

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Jun 25, 2025
@dinwwwh

dinwwwh commented Jun 25, 2025

Copy link
Copy Markdown
Member Author

Using proxyOmit(this, 'then') to prevent infinite awaits is not ideal because it removes .then from the HibernationEventIterator. If a user checks instanceof HibernationEventIterator and expects .then to exist, this can cause issues since .then not exists.

@dinwwwh dinwwwh closed this Jun 25, 2025
@codecov

codecov Bot commented Jun 25, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@dinwwwh
dinwwwh deleted the refactor/server/improve-naming-and-types-for-hibernation-apis branch June 22, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants