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

fix: edge runtime support for crypto #184

Merged
merged 5 commits into from
May 31, 2024
Merged

fix: edge runtime support for crypto #184

merged 5 commits into from
May 31, 2024

Conversation

nadilas
Copy link
Member

@nadilas nadilas commented May 31, 2024

Summary by CodeRabbit

  • New Features

    • Improved environment detection to enhance functionality across different platforms.
  • Bug Fixes

    • Updated digest function to simplify its usage and improve reliability.
  • Chores

    • Adjusted code coverage workflow to ensure proper token usage for public repositories.
📦 Published PR as canary version: Canary Versions

✨ Test out this PR locally via:

npm install @dotinc/ogre@0.10.2-canary.184.9317496655.0
npm install @dotinc/ogre-react@0.10.2-canary.184.9317496655.0
# or 
yarn add @dotinc/ogre@0.10.2-canary.184.9317496655.0
yarn add @dotinc/ogre-react@0.10.2-canary.184.9317496655.0

Copy link

vercel bot commented May 31, 2024

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

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
ogre ⬜️ Ignored (Inspect) Visit Preview May 31, 2024 11:33am

Copy link

coderabbitai bot commented May 31, 2024

Warning

Rate limit exceeded

@nadilas has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 40 minutes and 55 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 23a317c and 30800c7.

Walkthrough

The recent changes streamline the digest function in the ogre package by leveraging a new utility to check the environment. Additionally, the cleanAuthor function now returns more detailed information, and the GitHub workflow for test coverage now requires a token for public repositories.

Changes

File Path Change Summary
packages/ogre/src/hash.ts Simplified digest function signature and updated usage of isBrowser.
packages/ogre/src/utils.ts Added isBrowser constant and updated cleanAuthor to return a name-email tuple.
.github/workflows/coverage.yml Made CODECOV_TOKEN required by uncommenting the token field.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant DigestFunction
    participant Utils

    User->>DigestFunction: Call digest(obj, algorithm)
    DigestFunction->>Utils: Call isBrowser()
    Utils-->>DigestFunction: Return true/false
    DigestFunction-->>User: Return hash string
Loading

Poem

In the land of code where changes bloom,
A digest now with simpler room,
isBrowser checks with ease and grace,
Authors' names and emails in place.
Coverage tokens now required,
Our workflow's strength newly inspired.
🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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
Member Author

nadilas commented May 31, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @nadilas and the rest of your teammates on Graphite Graphite

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: 3

Outside diff range and nitpick comments (3)
packages/ogre/src/hash.ts (2)

Line range hint 47-47: Import Node.js built-in modules using the node: protocol for clarity and consistency.

digest = (await import("node:crypto"))
  .createHash(nodeAlg)
  .update(Buffer.from(hashInput))
  .digest("hex");

Line range hint 34-54: Refactor the anonymous function to an arrow function for better readability and to follow modern JavaScript practices.

return (async (obj, algorithm) => {
  // function body remains the same
})(obj, algorithm);
packages/ogre/src/utils.ts (1)

Line range hint 53-53: Specify a more precise type than any for the tree parameter to enhance type safety.

export const treeToObject = <T = Record<string, unknown>>(tree: string): T => {
  // function body remains the same
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fbc5fb7 and ee1d091.

Files selected for processing (2)
  • packages/ogre/src/hash.ts (3 hunks)
  • packages/ogre/src/utils.ts (1 hunks)
Additional context used
Biome
packages/ogre/src/hash.ts

[error] 26-26: Unexpected any. Specify a different type.


[error] 42-44: Prefer for...of instead of forEach.


[error] 47-47: A Node.js builtin module should be imported with the node: protocol.


[error] 34-54: This function expression can be turned into an arrow function.


[error] 57-57: Unexpected any. Specify a different type.


[error] 61-61: Unexpected any. Specify a different type.


[error] 61-61: Unexpected any. Specify a different type.

packages/ogre/src/utils.ts

[error] 13-13: Invalid typeof comparison value: this expression is not a string literal


[error] 16-16: Do not use template literals if interpolation and special-character handling are not needed.


[error] 36-36: Do not use template literals if interpolation and special-character handling are not needed.


[error] 37-37: Do not use template literals if interpolation and special-character handling are not needed.


[error] 53-53: Unexpected any. Specify a different type.


[error] 106-106: This variable implicitly has the any type.


[error] 177-177: Do not use template literals if interpolation and special-character handling are not needed.


[error] 196-196: Do not use template literals if interpolation and special-character handling are not needed.


[error] 199-199: This type annotation is trivially inferred from its initialization.


[error] 201-201: Do not use template literals if interpolation and special-character handling are not needed.


[error] 208-208: Unexpected any. Specify a different type.


[error] 217-217: Do not use template literals if interpolation and special-character handling are not needed.


[error] 265-265: Constraining a type parameter to any or unknown is useless.


[error] 1-2: All these imports are only used as types.


[error] 2-3: All these imports are only used as types.


[error] 5-6: Some named imports are only used as types.


[error] 6-7: All these imports are only used as types.

Additional comments not posted (4)
packages/ogre/src/hash.ts (2)

10-10: Import statement updated to include isBrowser from utils.js.


26-26: The function signature has been simplified by removing the isBrowser parameter.

packages/ogre/src/utils.ts (2)

12-13: The isBrowser function has been introduced to determine if the runtime environment is a browser.


12-12: The cleanAuthor function now returns a tuple with the author's name and email, enhancing the function's utility by providing more structured data.

packages/ogre/src/hash.ts Show resolved Hide resolved
packages/ogre/src/utils.ts Outdated Show resolved Hide resolved
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: 2

Outside diff range and nitpick comments (1)
packages/ogre/src/utils.ts (1)

14-14: Avoid using template literals where not necessary.

// Replace template literals with simple strings where interpolation is not used.
return [strings[0], strings[1].replace(">", "")];
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ee1d091 and 23a317c.

Files selected for processing (3)
  • .github/workflows/coverage.yml (1 hunks)
  • packages/ogre/src/hash.ts (3 hunks)
  • packages/ogre/src/utils.ts (1 hunks)
Additional context used
Biome
packages/ogre/src/hash.ts

[error] 26-26: Unexpected any. Specify a different type.


[error] 34-54: This function expression can be turned into an arrow function.


[error] 57-57: Unexpected any. Specify a different type.


[error] 61-61: Unexpected any. Specify a different type.


[error] 61-61: Unexpected any. Specify a different type.

packages/ogre/src/utils.ts

[error] 17-17: Do not use template literals if interpolation and special-character handling are not needed.


[error] 37-37: Do not use template literals if interpolation and special-character handling are not needed.


[error] 38-38: Do not use template literals if interpolation and special-character handling are not needed.


[error] 54-54: Unexpected any. Specify a different type.


[error] 107-107: This variable implicitly has the any type.


[error] 178-178: Do not use template literals if interpolation and special-character handling are not needed.


[error] 197-197: Do not use template literals if interpolation and special-character handling are not needed.


[error] 200-200: This type annotation is trivially inferred from its initialization.


[error] 202-202: Do not use template literals if interpolation and special-character handling are not needed.


[error] 209-209: Unexpected any. Specify a different type.


[error] 218-218: Do not use template literals if interpolation and special-character handling are not needed.


[error] 266-266: Constraining a type parameter to any or unknown is useless.


[error] 1-2: All these imports are only used as types.


[error] 2-3: All these imports are only used as types.


[error] 5-6: Some named imports are only used as types.


[error] 6-7: All these imports are only used as types.

Additional comments not posted (4)
packages/ogre/src/hash.ts (2)

10-10: Import addition looks good.


39-47: Ensure correct environment handling in the digest function.

Verification successful

The isBrowser function is correctly implemented in packages/ogre/src/utils.ts to identify the browser environment by checking for the presence of window and window.document. The environment handling in the digest function in packages/ogre/src/hash.ts appears to be correctly switching between browser and Node.js environments based on this function.

  • isBrowser function implementation:

    export const isBrowser = () =>
      typeof window !== "undefined" && typeof window.document !== "undefined";
  • Environment handling in digest function:

    if (isBrowser()) {
      const buf = await crypto.subtle.digest(algorithm, hashInput);
      const h = "0123456789abcdef";
      for (const v of new Uint8Array(buf)) {
        digest += h[v >> 4] + h[v & 15];
      }
    } else {
      const nodeAlg = algorithm.toLowerCase().replace("-", "");
      digest = (await import("node:crypto"))
        .createHash(nodeAlg)
        .update(Buffer.from(hashInput))
        .digest("hex");
    }

The code correctly handles different environments, ensuring the appropriate cryptographic operations are performed based on whether the code is running in a browser or Node.js environment.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the environment handling in the `digest` function correctly switches between browser and Node.js environments.

# Test: Search for environment-specific code paths. Expect: Correct usage of `isBrowser()` and appropriate handling in both environments.
ast-grep --lang typescript --pattern $'if (isBrowser()) { $$$ } else { $$$ }' packages/ogre/src/hash.ts

Length of output: 946



Script:

#!/bin/bash
# Description: Locate and inspect the implementation of the `isBrowser()` function to ensure it correctly identifies the environment.

# Find the implementation of `isBrowser` function
ast-grep --lang typescript --pattern 'function isBrowser() { $$$ }' 

Length of output: 69



Script:

#!/bin/bash
# Description: Search for the `isBrowser` keyword to locate its definition or import statement.

# Search for `isBrowser` keyword in the codebase
rg 'isBrowser' 

Length of output: 233



Script:

#!/bin/bash
# Description: Display the content of `packages/ogre/src/utils.ts` to verify the `isBrowser` function implementation.

# Display the content of `packages/ogre/src/utils.ts`
cat packages/ogre/src/utils.ts

Length of output: 8854

packages/ogre/src/utils.ts (2)

12-13: Addition of isBrowser function is appropriate for environment checks.


14-14: Change in cleanAuthor function to return a tuple enhances data structure.

.github/workflows/coverage.yml Show resolved Hide resolved
packages/ogre/src/hash.ts Show resolved Hide resolved
@nadilas nadilas merged commit 4ac334d into main May 31, 2024
6 of 8 checks passed
@nadilas nadilas deleted the fix/edge_crypto branch May 31, 2024 11:35
Copy link

🚀 PR was released in @dotinc/ogre-cli@0.7.2, @dotinc/ogre-react@0.10.2, @dotinc/ogre@0.10.2 🚀

@github-actions github-actions bot added the released This issue/pull request has been released. label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This issue/pull request has been released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant