Skip to content

Conversation

@ThisIsMissEm
Copy link
Contributor

@ThisIsMissEm ThisIsMissEm commented Oct 26, 2024

I think I've gotten this change right, essentially the code would try to resolve http://schema.org/ be redirected to https://schema.org/ and get back a link alternate with:

link: </docs/jsonldcontext.jsonld>; rel="alternate"; type="application/ld+json"

Which Fedify would correctly discover, however, it would then try to fetch /docs/jsonldcontext.jsonld instead of https://schema.org/docs/jsonldcontext.jsonld, this fetch would result in an invalid URL error.

This change should fix that by resolving alternate URIs relative to the docUrl, as is done in HTML response handling.

The test change appears to assert this but I'm not 100% sure this is correct.

Summary by CodeRabbit

  • New Features

    • Enhanced handling of alternate document URLs for improved resolution.
    • Added support for relative URLs in link headers within document loading.
  • Bug Fixes

    • Expanded test coverage for document loading, specifically for relative URL handling.
  • Documentation

    • Updated logging for clarity regarding alternate document URLs.

@coderabbitai
Copy link

coderabbitai bot commented Oct 26, 2024

Walkthrough

The changes involve updates to the test suite in src/runtime/docloader.test.ts and modifications to the getRemoteDocument function in src/runtime/docloader.ts. The test suite now includes a new mock response and additional assertions for handling relative URLs in link headers. In getRemoteDocument, a new variable altUri is introduced to improve the handling of alternate document URLs, enhancing the clarity of logging statements without altering the overall functionality.

Changes

File Path Change Summary
src/runtime/docloader.test.ts Added a new mock response for GET@/link-obj-relative and expanded tests for fetchDocumentLoader to handle relative URLs.
src/runtime/docloader.ts Introduced altUri for handling alternate document URLs and updated logging statements accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant DocumentLoader
    participant DocumentService

    Client->>DocumentLoader: fetchDocumentLoader(url)
    DocumentLoader->>DocumentService: GET /link-obj-relative
    DocumentService-->>DocumentLoader: 200 OK (empty response, relative link)
    DocumentLoader->>Client: Return document structure with resolved relative link
Loading

🐰 "In the land of code where rabbits play,
New tests were added to brighten the day.
With links that are relative, oh what a sight,
Fetching documents now feels just right!
Hopping through changes, we cheer with glee,
For every new feature, there's joy, you see!" 🌼


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.

@ThisIsMissEm
Copy link
Contributor Author

@dahlia this bug affects Hollo and prevents federation with GoToSocial v0.16.0 & v0.16.0+git-f1cbf6f

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

🧹 Outside diff range and nitpick comments (3)
src/runtime/docloader.test.ts (2)

122-143: Improve test structure and remove duplication.

The test step has the following issues:

  1. The first assertion (lines 123-131) is duplicated from the previous test step and doesn't test relative URL handling.
  2. The test name could be more specific about what's being tested.

Consider applying these improvements:

-  await t.step("Link header relative url", async () => {
-    assertEquals(await fetchDocumentLoader("https://example.com/link-ctx"), {
-      contextUrl: "https://www.w3.org/ns/activitystreams",
-      documentUrl: "https://example.com/link-ctx",
-      document: {
-        id: "https://example.com/link-ctx",
-        name: "Fetched object",
-        type: "Object",
-      },
-    });
-
+  await t.step("resolves relative URLs in Link headers", async () => {
     assertEquals(await fetchDocumentLoader("https://example.com/link-obj-relative"), {
       contextUrl: null,
       documentUrl: "https://example.com/object",
       document: {
         "@context": "https://www.w3.org/ns/activitystreams",
         id: "https://example.com/object",
         name: "Fetched object",
         type: "Object",
       },
     });
   });

86-97: Add test cases for additional relative path scenarios.

The current test only covers a simple relative path case. Consider adding test cases for:

  1. Nested paths (e.g., '/docs/jsonldcontext.jsonld')
  2. Parent directory references (e.g., '../docs/schema.json')
  3. Same directory references (e.g., './schema.json')

This would provide better coverage for the relative path handling fix described in the PR objectives.

Example mock for nested paths:

mf.mock("GET@/link-obj-nested", (_req) =>
  new Response(
    "",
    {
      status: 200,
      headers: {
        "Content-Type": "text/html; charset=utf-8",
        Link: '</docs/jsonldcontext.jsonld>; rel="alternate"; ' +
          'type="application/activity+json"',
      },
    },
  ));

Would you like me to generate the complete set of test cases for these scenarios?

src/runtime/docloader.ts (1)

136-148: Consider expanding test coverage for URI resolution edge cases.

While the changes look good, consider adding test cases for:

  1. Different relative path formats (./path, ../path, /path)
  2. Protocol-relative URLs (//example.com)
  3. Error cases with malformed URIs

Would you like me to help generate these additional test cases?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2e6bbbe and d4ea71b.

📒 Files selected for processing (2)
  • src/runtime/docloader.test.ts (2 hunks)
  • src/runtime/docloader.ts (1 hunks)
🔇 Additional comments (2)
src/runtime/docloader.test.ts (1)

86-97: LGTM! Mock response correctly simulates relative path scenario.

The mock response accurately represents the use case where a Link header contains a relative path URI, which is essential for testing the fix described in the PR objectives.

src/runtime/docloader.ts (1)

136-148: LGTM! The changes correctly handle relative URIs in link alternate headers.

The introduction of altUri properly resolves relative URIs against the base URL (docUrl), fixing the issue where relative paths in link alternate headers were not being correctly handled. This change aligns with the existing HTML link handling approach in the codebase.

Let's verify that the HTML link handling uses the same URL resolution approach:

✅ Verification successful

The changes correctly align with existing HTML link handling approach

The verification confirms that HTML link handling in the same file uses identical URL resolution logic:

  • Line 186: new URL(attribs.href, docUrl).href !== docUrl.href for comparison
  • Line 192: fetch(new URL(attribs.href, docUrl).href) for fetching

The PR's changes to handle relative URIs in link alternate headers perfectly mirror this established pattern, ensuring consistent behavior across both HTML and alternate link processing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify HTML link handling uses similar URL resolution
# Test: Search for URL resolution in HTML link handling
rg -A 5 "new URL\(.+, docUrl\)" 

Length of output: 1122

Copy link
Member

@dahlia dahlia left a comment

Choose a reason for hiding this comment

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

Thank you! Everything looks good, except that it should be merged into previous versions. Instead of merging this pull request, I'm cherry-picking your commit to the 1.0-maintenance branch.

@codecov
Copy link

codecov bot commented Oct 27, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 79.32%. Comparing base (2e6bbbe) to head (d4ea71b).
Report is 11 commits behind head on main.

Files with missing lines Patch % Lines
src/runtime/docloader.ts 75.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #155   +/-   ##
=======================================
  Coverage   79.31%   79.32%           
=======================================
  Files          42       42           
  Lines        7925     7926    +1     
  Branches     1119     1119           
=======================================
+ Hits         6286     6287    +1     
  Misses       1618     1618           
  Partials       21       21           

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

@dahlia dahlia closed this in 78783d8 Oct 27, 2024
@dahlia
Copy link
Member

dahlia commented Oct 27, 2024

This patch was applied to 0.15.4, 1.0.6, and 1.1.2.

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