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

feat(data-handlers): add digest, etag, stable and verified headers #216

Merged
merged 2 commits into from
Oct 2, 2024

Conversation

karlprieb
Copy link
Collaborator

No description provided.

@karlprieb karlprieb requested a review from djwhitt October 1, 2024 22:03
Copy link

codecov bot commented Oct 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.72%. Comparing base (9317970) to head (0171a22).
Report is 3 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #216      +/-   ##
===========================================
+ Coverage    68.70%   68.72%   +0.01%     
===========================================
  Files           32       32              
  Lines         7916     7919       +3     
  Branches       431      431              
===========================================
+ Hits          5439     5442       +3     
  Misses        2476     2476              
  Partials         1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@karlprieb karlprieb marked this pull request as ready for review October 2, 2024 14:48
Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request introduce new properties to the headerNames constant in src/constants.ts, specifically digest, stable, and verified, which represent HTTP header names. Additionally, a new function, setDigestStableVerifiedHeaders, is added in src/routes/data/handlers.ts to set these headers in HTTP responses based on data attributes. The test files are updated to include new assertions for these headers, enhancing the verification of HTTP responses in various tests.

Changes

File Path Change Summary
src/constants.ts Added new properties to headerNames: digest: 'X-AR-IO-Digest', stable: 'X-AR-IO-Stable', verified: 'X-AR-IO-Verified'.
src/routes/data/handlers.ts Added function setDigestStableVerifiedHeaders to set new HTTP response headers based on dataAttributes. Modified setDataHeaders and handleRangeRequest to include this new function.
src/routes/data/handlers.test.ts Added methods getDataItemAttributes and getTransactionAttributes to the dataIndex object for enhanced mock testing.
test/end-to-end/data.test.ts Added assertions to check for x-ar-io-digest, etag, x-ar-io-stable, and x-ar-io-verified headers in HTTP responses for various endpoints.

Possibly related PRs

Suggested reviewers

  • djwhitt

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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 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
Contributor

@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

🧹 Outside diff range and nitpick comments (5)
src/routes/data/handlers.test.ts (2)

43-47: Approve new methods and suggest improvements

The addition of getDataItemAttributes and getTransactionAttributes methods to the dataIndex object is approved. These new methods align with the changes in the createDataHandler function.

However, I have a few suggestions to improve the test coverage:

  1. Verify if returning undefined is the intended behavior for these mock methods. If specific attributes are expected in the actual implementation, consider mocking realistic return values.

  2. Add tests that explicitly check the behavior of createDataHandler when these methods are called. This will ensure that the handler correctly utilizes the new attributes.

Would you like assistance in drafting additional test cases for these new methods?


Line range hint 1-478: Overall assessment and suggestion for improvement

The changes to this test file are minimal and focused. The addition of getDataItemAttributes and getTransactionAttributes methods to the mock dataIndex object suggests an expansion of functionality in the createDataHandler function.

While the existing tests remain intact and should continue to pass, there's an opportunity to enhance the test coverage:

  1. Consider adding new test cases that specifically exercise the newly added methods. This will ensure that the createDataHandler function correctly utilizes these new attributes in various scenarios.

  2. Review the createDataHandler implementation to identify any new behaviors or edge cases introduced by these attributes, and add corresponding tests.

  3. If these new attributes are critical to the function's behavior, consider adding negative test cases (e.g., when these methods return null or throw errors) to ensure robust error handling.

Would you like assistance in outlining some additional test cases to cover these new methods?

src/routes/data/handlers.ts (1)

44-63: LGTM! Consider minor readability improvement.

The implementation of setDigestStableVerifiedHeaders looks good. It correctly handles the case when dataAttributes is undefined and sets appropriate headers based on the data attributes.

Consider using object destructuring for dataAttributes to improve readability:

 const setDigestStableVerifiedHeaders = ({
   res,
   dataAttributes,
 }: {
   res: Response;
   dataAttributes: ContiguousDataAttributes | undefined;
 }) => {
-  if (dataAttributes !== undefined) {
+  if (dataAttributes) {
+    const { stable, verified, hash } = dataAttributes;
-    res.setHeader(headerNames.stable, dataAttributes.stable ? 'true' : 'false');
+    res.setHeader(headerNames.stable, stable ? 'true' : 'false');
     res.setHeader(
       headerNames.verified,
-      dataAttributes.verified ? 'true' : 'false',
+      verified ? 'true' : 'false',
     );

-    if (dataAttributes.hash !== undefined) {
+    if (hash) {
-      res.setHeader(headerNames.digest, dataAttributes.hash);
-      res.setHeader('ETag', dataAttributes.hash);
+      res.setHeader(headerNames.digest, hash);
+      res.setHeader('ETag', hash);
     }
   }
 };
test/end-to-end/data.test.ts (2)

332-339: Consider extracting repeated constants into variables

The value 'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA' is used multiple times in these assertions. To improve maintainability and avoid duplication, consider defining it as a constant at the beginning of the test case.


467-474: Reuse constants for repeated header values

Similar to earlier, the constant 'gkOH8JBTdKr_wD9SriwYwCM6p7saQAJFU60AREREQLA' is repeated in multiple assertions. Extracting this value into a constant can enhance readability and maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 3942636 and 0171a22.

📒 Files selected for processing (4)
  • src/constants.ts (1 hunks)
  • src/routes/data/handlers.test.ts (1 hunks)
  • src/routes/data/handlers.ts (3 hunks)
  • test/end-to-end/data.test.ts (2 hunks)
🔇 Additional comments (7)
src/constants.ts (2)

23-25: LGTM! New headers added correctly.

The new header names (digest, stable, and verified) have been added correctly, following the existing naming convention with the 'X-AR-IO-' prefix. This maintains consistency with the current codebase structure.


23-25: ⚠️ Potential issue

Consider adding the etag header mentioned in the PR objectives.

The PR objectives mention adding an etag header, but it's not present in the current changes. Consider adding it to align with the stated objectives:

export const headerNames = {
  hops: 'X-AR-IO-Hops',
  origin: 'X-AR-IO-Origin',
  originNodeRelease: 'X-AR-IO-Origin-Node-Release',
  digest: 'X-AR-IO-Digest',
  stable: 'X-AR-IO-Stable',
  verified: 'X-AR-IO-Verified',
+ etag: 'ETag',
  cache: 'X-Cache',
  arnsTtlSeconds: 'X-ArNS-TTL-Seconds',
  arnsResolvedId: 'X-ArNS-Resolved-Id',
  arnsProcessId: 'X-ArNS-Process-Id',
};

Note that 'ETag' is the standard HTTP header name for entity tags, so it doesn't follow the 'X-AR-IO-' prefix convention.

To ensure we're not duplicating an existing etag header definition, let's check the codebase:

✅ Verification successful

Adding the ETag header aligns with the PR objectives and does not duplicate existing headers.

  • Include the etag: 'ETag' line in src/constants.ts as suggested.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any existing etag header definitions
rg --type typescript "etag.*:.*['\"](ETag|etag)['\"]" --ignore-case

Length of output: 108


Script:

#!/bin/bash
# Search for any existing etag header definitions in TypeScript files by specifying the file extensions
rg "etag\s*:\s*['\"](ETag|etag)['\"]" --ignore-case --glob "*.ts" --glob "*.tsx"

Length of output: 86

src/routes/data/handlers.ts (3)

124-124: LGTM! Good integration of new header-setting function.

The addition of setDigestStableVerifiedHeaders call in the setDataHeaders function ensures consistent setting of digest, stable, and verified headers for all data responses. This change aligns well with the PR objective.


173-174: LGTM! Consistent header setting for range requests.

The addition of setDigestStableVerifiedHeaders call in the handleRangeRequest function ensures that digest, stable, and verified headers are consistently set for range requests as well. This change aligns with the PR objective and maintains consistency with the non-range request handling.


Line range hint 1-724: Overall assessment: Well-implemented feature with consistent header handling.

The changes in this PR successfully implement the addition of digest, etag, stable, and verified headers across different request handling scenarios. The new setDigestStableVerifiedHeaders function is well-designed and consistently used in both setDataHeaders and handleRangeRequest functions. This ensures that these headers are set for all types of responses, including range requests.

The implementation is clean, consistent, and aligns well with the existing code structure. It effectively achieves the PR objective of adding these new headers to enhance the information provided in the HTTP responses.

test/end-to-end/data.test.ts (2)

340-341: Verify header value types for x-ar-io-stable and x-ar-io-verified

You are asserting the headers x-ar-io-stable and x-ar-io-verified against the string 'false'. If these headers are meant to represent boolean values, ensure that they are consistently returned as strings. Otherwise, consider parsing them to boolean before assertion.


475-476: Confirm consistency of header value types

The assertions for x-ar-io-stable and x-ar-io-verified compare against the string 'false'. Verify that the API consistently returns these header values as strings. If they represent boolean values, consider converting them accordingly in your tests.

src/routes/data/handlers.ts Show resolved Hide resolved
@djwhitt djwhitt merged commit 6ce5678 into develop Oct 2, 2024
4 checks passed
@djwhitt djwhitt deleted the PE-6857-digest-stable-verified-headers branch October 2, 2024 16:02
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