Skip to content

Conversation

@jaipaljadeja
Copy link
Member

No description provided.

- Added a new function to serialize runtime configuration for use in plugins.
- Updated the appConfig plugin to utilize the serialized configuration and handle undefined cases.
- Adjusted logger initialization to prevent bundling issues with custom loggers.
@jaipaljadeja jaipaljadeja requested a review from fracek March 12, 2025 13:58
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new JSON file containing prerelease metadata for the "apibara" package and adds a new dependency (@rollup/plugin-replace) to the CLI package. It extends the CLI commands to accept a "preset" parameter, and updates configuration handling by incorporating preset support with error validations and runtime merging. Additionally, changes include modifications to the reporter variable initialization, adjustments to the config export type, and a shift from file-based configuration retrieval to using environment variables.

Changes

File(s) Change Summary
change/apibara-3c1e3504-...deb8cf9.json Introduced a new JSON metadata file for a prerelease, specifying fields such as type, comment, packageName, email, and dependentChangeType.
packages/cli/package.json Added dependency "@rollup/plugin-replace": "^6.0.2".
packages/cli/src/cli/commands/dev.ts
packages/cli/src/cli/commands/start.ts
Added a preset property/argument to the configuration object passed to createApibara, enabling user-defined presets for the development and start commands.
packages/cli/src/core/config/resolvers/runtime-config.resolver.ts
packages/cli/src/rolldown/config.ts
packages/cli/src/rolldown/plugins/config.ts
Enhanced runtime configuration: added preset and presets handling, merged runtime configs, implemented error handling, and shifted configuration retrieval from file import to environment variable access with serialization via a new helper function.
packages/cli/src/runtime/internal/app.ts Updated the reporter variable declaration from let to const and commented out custom logger support to avoid bundling issues with Rollup.
packages/cli/src/types/virtual/config.d.ts Modified the exported config type to include only "runtimeConfig", "preset", and "presets" properties from the original type.
examples/cli/apibara.config.ts Added new configuration objects for evm and starknet, each with a startingBlock property.
examples/cli/indexers/1-evm.indexer.ts
examples/cli/indexers/2-starknet.indexer.ts
Modified destructuring of runtimeConfig to include startingBlock from evm and starknet respectively.
examples/cli/test/ethereum.test.ts
examples/cli/test/starknet.test.ts
Updated createIndexer function calls to include evm and starknet properties with startingBlock values.
packages/cli/src/hooks/useRuntimeConfig.ts Changed return statement to use deserialize instead of JSON.parse for processing APIBARA_RUNTIME_CONFIG.
packages/cli/src/utils/helper.ts Introduced deserialize and serialize functions for handling configuration data.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant C as CLI Command
    participant A as createApibara
    participant R as RuntimeConfig Resolver
    participant E as Environment (APIBARA_RUNTIME_CONFIG)
    participant P as RollDown Plugin

    U->>C: Invoke command with preset option
    C->>A: Call createApibara(preset)
    A->>R: Resolve runtime configuration with preset handling
    R-->>A: Return merged runtime config or error
    A->>E: Serialize and store runtime config to env variable
    P->>E: Retrieve runtime config for plugin replacement
Loading

Possibly related PRs

  • cli: use default logger that includes indexer name #120: The changes in the main PR, which introduce a new JSON metadata file for the "apibara" package, are related to the retrieved PR as both involve the addition of a JSON configuration file for the same package, albeit with different purposes.
  • Update beta to 2.1.0-beta.x #146: The changes in the main PR, which introduce a new JSON file for the "apibara" package's prerelease metadata, are related to the retrieved PR that also introduces new JSON files for various "apibara" packages, indicating a similar focus on prerelease metadata updates.
  • Fix minor issues with CLI #115: The changes in the main PR, which introduce a new JSON metadata file for the "apibara" package, are related to the retrieved PR that also adds a JSON metadata file for the same package, indicating a prerelease version. Both PRs involve modifications to JSON files that specify metadata for the "apibara" package.

Suggested reviewers

  • fracek

Poem

I'm just a rabbit, hopping in code's meadow,
Dancing with presets that brighten the plateau.
Configs merge like streams in a joyful spree,
As plugins and loggers now run merrily.
With every line, I smile and bound with glee!
🐇✨

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/cli/src/rolldown/config.ts (1)

93-109: Well-structured serialization function with proper error handling.

The getSerializedRuntimeConfig function correctly serializes only the necessary configuration properties and includes appropriate error handling with a clear error message. This helps ensure that only serializable values are used in the runtime configuration.

Two small suggestions:

  1. Consider adding more specific error information by including the original error in the thrown Error message
  2. Consider using a more concise JSON.stringify (without formatting) for production builds
function getSerializedRuntimeConfig(apibara: Apibara) {
  try {
    return JSON.stringify(
      {
        runtimeConfig: apibara.options.runtimeConfig,
        preset: apibara.options.preset,
        presets: apibara.options.presets,
      },
-      null,
-      2,
+      null
    );
  } catch (error) {
    throw new Error(
-      "Failed to serialize runtime config, please only use serializable values for runtime config",
+      `Failed to serialize runtime config, please only use serializable values for runtime config: ${error}`
    );
  }
}
packages/cli/src/runtime/internal/app.ts (1)

65-80: Well-documented temporary disabling of custom logger support.

The thorough comments explaining why custom logger support is temporarily disabled are excellent. They clearly articulate how this change relates to the PR objective of preventing unwanted bundling while still providing a roadmap for future improvements.

In the future, you might want to consider alternatives for custom loggers that don't require bundling the entire configuration object, such as:

  1. A plugin-based approach where loggers are registered separately
  2. A factory function pattern that accepts the serialized config at runtime
packages/cli/src/core/config/resolvers/runtime-config.resolver.ts (2)

4-5: Validate options.runtimeConfig before spreading
If options.runtimeConfig is undefined or not an object, this spread operation could potentially throw an error. Consider defaulting to an empty object for safer merging.

- let runtimeConfig: Record<string, unknown> = { ...options.runtimeConfig };
+ let runtimeConfig: Record<string, unknown> = { ...(options.runtimeConfig ?? {}) };

14-16: Check for property existence with in
You can improve readability by verifying property existence with the in operator, which avoids referencing a potentially non-existent index.

- if (presets[preset] === undefined) {
+ if (!(preset in presets)) {
    throw new Error(`Specified preset "${preset}" but it was not defined`);
  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 78052ac and 2c68994.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • change/apibara-3c1e3504-7ac7-41b7-bb7f-02c09deb8cf9.json (1 hunks)
  • packages/cli/package.json (1 hunks)
  • packages/cli/src/cli/commands/dev.ts (1 hunks)
  • packages/cli/src/cli/commands/start.ts (1 hunks)
  • packages/cli/src/core/config/resolvers/runtime-config.resolver.ts (1 hunks)
  • packages/cli/src/rolldown/config.ts (2 hunks)
  • packages/cli/src/rolldown/plugins/config.ts (1 hunks)
  • packages/cli/src/runtime/internal/app.ts (1 hunks)
  • packages/cli/src/types/virtual/config.d.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (15)
packages/cli/package.json (1)

94-94: New dependency added for runtime config serialization.

The @rollup/plugin-replace dependency has been added to support the runtime configuration serialization in rolldown, which aligns with the PR objective of avoiding bundling apibara.config.

change/apibara-3c1e3504-7ac7-41b7-bb7f-02c09deb8cf9.json (1)

1-7: Change file looks good for documenting the feature.

This change file correctly documents the feature addition and marks it as a prerelease, which is appropriate for integrating runtime configuration serialization into rolldown.

packages/cli/src/cli/commands/dev.ts (2)

59-59: Preset integration looks good.

The change adds support for the preset parameter to be passed to the createApibara function, which aligns with the PR objective of integrating runtime config serialization.


114-114: Verify preset propagation through CLI.

The preset argument is correctly passed to the child process when starting indexers. This ensures that the preset configuration is consistently applied throughout the application lifecycle.

packages/cli/src/cli/commands/start.ts (4)

20-23: Added preset CLI argument.

The preset argument is properly defined with appropriate type and description, allowing users to specify a preset configuration when starting an indexer.


31-31: Preset integration in createApibara.

The preset parameter is correctly passed to the createApibara function, which enables runtime configuration based on the specified preset.


35-35: Log message includes preset information.

Good addition of the preset information in the log message, which improves visibility of which preset is being used when starting an indexer.


55-55: Preset propagation to child process.

The preset argument is properly passed to the child process, ensuring that the preset configuration is maintained throughout the application lifecycle.

packages/cli/src/types/virtual/config.d.ts (1)

3-6: Good type narrowing for config serialization.

The change from using the full ApibaraConfig type to a more specific Pick type with only the necessary properties (runtimeConfig, preset, and presets) aligns perfectly with the PR objective of avoiding bundling the entire apibara.config. This focused approach will reduce the bundle size and ensure only the essential configuration properties are serialized at runtime.

packages/cli/src/rolldown/config.ts (2)

3-3: Import added for the new plugin.

The @rollup/plugin-replace import is correctly added to support runtime config serialization.


79-86: Good implementation of the config serialization strategy.

The addition of the replace plugin with process.env.APIBARA_CONFIG replacement is a clean way to inject the serialized runtime configuration during the build process. This approach avoids bundling the entire apibara.config file as intended.

packages/cli/src/rolldown/plugins/config.ts (1)

8-14: Effective approach to integrate environment-based config.

The change from file-based configuration to an environment variable approach aligns with the PR objective. The error handling for when APIBARA_CONFIG is undefined is a good defensive programming practice.

This change works in conjunction with the @rollup/plugin-replace implementation in the rolldown config to effectively inject the serialized configuration at build time.

packages/cli/src/runtime/internal/app.ts (1)

59-59: Changed variable declaration to constant.

Changing from let to const for the reporter is appropriate since the custom logger functionality is being disabled, meaning the variable no longer needs to be reassignable.

packages/cli/src/core/config/resolvers/runtime-config.resolver.ts (2)

7-12: Good practice in error reporting
This clear error message ensures users know how to fix a missing presets definition when preset is specified.


18-21: Confirm the intended overwrite direction
Merging the preset’s runtimeConfig last means the preset overrides user-specified values. If you intended user-specified values to override the preset, reverse the merge order.

Copy link
Contributor

@fracek fracek left a comment

Choose a reason for hiding this comment

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

I think we just need to support serializing/deserializing bigint and we're good.

@jaipaljadeja jaipaljadeja requested a review from fracek March 12, 2025 19:12
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (6)
examples/cli/indexers/1-evm.indexer.ts (1)

18-21: Consider adding defensive programming for configuration values.

The code assumes that evm and startingBlock always exist in the runtime configuration. Consider adding default values or validation to handle cases where these properties might be missing.

  const {
    connectionString,
-   evm: { startingBlock },
+   evm: { startingBlock } = { startingBlock: 215_30_000n },
  } = runtimeConfig;

Or with more comprehensive error handling:

  const {
    connectionString,
-   evm: { startingBlock },
+   evm,
  } = runtimeConfig;
+ 
+ if (!evm || typeof evm.startingBlock === 'undefined') {
+   throw new Error('Missing required evm.startingBlock in runtime configuration');
+ }
+ 
+ const { startingBlock } = evm;
packages/cli/src/hooks/useRuntimeConfig.ts (1)

5-5: Consider adding fallback error handling for invalid JSON.

If process.env.APIBARA_RUNTIME_CONFIG is not valid JSON, JSON.parse will throw. Handling or logging parse errors here might improve resilience and debugging.

packages/cli/src/utils/helper.ts (1)

1-7: Support negative BigInts in the reviver.

Currently, the regex only matches positive integers ending with “n”. If negative BigInt strings (e.g., -42n) are needed, consider updating the regex pattern:

-    typeof value === "string" && value.match(/^\d+n$/)
+    typeof value === "string" && value.match(/^-?\d+n$/)
packages/cli/src/rolldown/plugins/config.ts (3)

10-12: Improve error handling for invalid configuration.

The current error handling only checks if the config is undefined or empty. Consider adding additional validation to handle cases where the environment variable exists but contains invalid JSON.

if (serializedConfig === undefined || serializedConfig === "") {
  throw new Error("APIBARA_CONFIG is not defined");
}

+try {
+  JSON.parse(serializedConfig);
+} catch (error) {
+  throw new Error("APIBARA_CONFIG contains invalid JSON: " + error.message);
+}

14-20: Enhance BigInt deserialization to support negative numbers.

The current regex pattern only matches positive BigInt values. Update it to also handle negative values.

function deserialize(str) {
  return JSON.parse(str, (_, value) =>
-    typeof value === "string" && value.match(/^\\d+n$/)
+    typeof value === "string" && value.match(/^-?\\d+n$/)
      ? BigInt(value.slice(0, -1))
      : value,
  );
}

22-22: Consider adding error handling for the deserialization process.

It's a good practice to wrap the deserialization in a try-catch block to provide clearer error messages when parsing fails.

-export const config = deserialize(serializedConfig);
+try {
+  export const config = deserialize(serializedConfig);
+} catch (error) {
+  throw new Error(`Failed to deserialize configuration: ${error.message}`);
+}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2c68994 and 24ce0fb.

📒 Files selected for processing (10)
  • examples/cli/apibara.config.ts (1 hunks)
  • examples/cli/indexers/1-evm.indexer.ts (2 hunks)
  • examples/cli/indexers/2-starknet.indexer.ts (2 hunks)
  • examples/cli/test/ethereum.test.ts (1 hunks)
  • examples/cli/test/starknet.test.ts (1 hunks)
  • packages/cli/src/core/config/resolvers/runtime-config.resolver.ts (1 hunks)
  • packages/cli/src/hooks/useRuntimeConfig.ts (1 hunks)
  • packages/cli/src/rolldown/config.ts (3 hunks)
  • packages/cli/src/rolldown/plugins/config.ts (1 hunks)
  • packages/cli/src/utils/helper.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/cli/src/rolldown/config.ts
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (12)
examples/cli/apibara.config.ts (1)

5-10: Good addition of structured configuration for EVM and StarkNet environments

The new configuration objects for both EVM and StarkNet with their respective starting block numbers are well-structured and improve configurability. Using bigint literals with underscore separators (215_30_000n and 10_30_000n) improves the readability of these large numbers.

examples/cli/test/starknet.test.ts (1)

14-18: Well-structured test configuration

The updated createIndexer call properly incorporates the new configuration parameters. Note that the test is using a different starting block value (800_000n) than the one in the main configuration (10_30_000n), which is appropriate for this test since it's only examining blocks 800_000 through 800_005.

examples/cli/indexers/2-starknet.indexer.ts (2)

13-16: Good refactoring to use configured starting block

The destructuring of the runtime configuration now properly extracts the nested startingBlock from the starknet object. This is a better approach than hardcoding values.


28-28: Clean implementation using the configured starting block

Using the dynamically configured startingBlock value here improves flexibility and maintainability.

examples/cli/test/ethereum.test.ts (1)

14-18: Well-structured test configuration with consistent pattern

The test properly includes both EVM and StarkNet configuration parameters, following the same pattern as the StarkNet test. The test uses a different starting block value for testing purposes, which is appropriate since it examines only a small block range (10_000_000 to 10_000_005).

examples/cli/indexers/1-evm.indexer.ts (2)

18-21: Good refactoring to use configuration instead of hardcoded values.

This change enhances configurability by extracting the startingBlock from the nested evm object in runtimeConfig instead of using a hardcoded value. This is a good practice that aligns with the PR objective of integrating runtime config serialization.


33-33: LGTM! Good use of the configured value.

Using the destructured startingBlock from configuration instead of a hardcoded value improves flexibility and maintainability.

packages/cli/src/hooks/useRuntimeConfig.ts (1)

2-2: LGTM for the new import.

The introduction of deserialize from ../utils/helper is a clean way to centralize parsing logic.

packages/cli/src/utils/helper.ts (1)

9-15: Validate unified handling of BigInts.

The serializer logic for BigInt is consistent, but ensure you also handle negative values equivalently (e.g., -42n). This approach maintains parity with the proposed regex update in deserialize.

packages/cli/src/core/config/resolvers/runtime-config.resolver.ts (2)

2-23: Robust preset merging logic.

Merging the specified preset's runtimeConfig into the existing runtimeConfig is well-structured. Throwing clear errors for undefined or missing presets ensures the user is aware of configuration mistakes. No specific concerns here.


24-24: Minimize reliance on process.env for local config.

Storing configuration directly in process.env may cause side effects in multi-step scripts or runtime re-entry. Consider returning the computed config instead of mutating environment variables.

packages/cli/src/rolldown/plugins/config.ts (1)

8-8:

✅ Verification successful

Review the environment variable replacement pattern.

The template literal syntax \process.env.APIBARA_CONFIG`` suggests this string will be processed by a build tool to replace it with the actual environment variable value. Make sure the string replacement is working as expected with your rollup configuration.


🏁 Script executed:

#!/bin/bash
# Check if @rollup/plugin-replace is correctly configured in your build setup
grep -r "@rollup/plugin-replace" packages/cli/

Length of output: 194


Environment Variable Replacement Config Looks Correct

The build setup includes the @rollup/plugin-replace dependency (as seen in packages/cli/package.json) and its import in packages/cli/src/rolldown/config.ts verifies it’s available for processing. The literal

const serializedConfig = `process.env.APIBARA_CONFIG`;

in packages/cli/src/rolldown/plugins/config.ts will be replaced at build time if your rollup configuration correctly targets that string. Please ensure through a full build/test cycle that the expected runtime value is injected as intended.

Copy link
Contributor

@fracek fracek left a comment

Choose a reason for hiding this comment

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

Looks good now!

@fracek fracek merged commit 9a26728 into apibara:main Mar 13, 2025
2 checks passed
@jaipaljadeja jaipaljadeja deleted the fix/apibara-config-bundler-fix branch March 13, 2025 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants