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: support fs.exists async function #65

Merged
merged 4 commits into from
Dec 22, 2024
Merged

feat: support fs.exists async function #65

merged 4 commits into from
Dec 22, 2024

Conversation

fengmk2
Copy link
Member

@fengmk2 fengmk2 commented Dec 22, 2024

Summary by CodeRabbit

  • New Features

    • Added a badge in the README.md to welcome pull requests.
    • Introduced a new asynchronous function exists to check for file existence.
    • Expanded the module's public API by exporting all functionalities from the fs.js module.
  • Tests

    • Added unit tests for the exists function, covering various scenarios including file existence checks and error handling.

Copy link
Contributor

coderabbitai bot commented Dec 22, 2024

Warning

Rate limit exceeded

@fengmk2 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 19 minutes and 4 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

Reviewing files that changed from the base of the PR and between 308fe3a and 03a78d2.

📒 Files selected for processing (2)
  • .github/workflows/nodejs.yml (0 hunks)
  • test/fs.test.ts (1 hunks)

Walkthrough

This pull request introduces a new file system utility function exists() in the project. The function checks whether a given file or directory exists by using Node.js's stat method from the fs/promises module. It returns file statistics when the path exists or false when it doesn't. The changes also include adding this new function to the module's exports and creating comprehensive test cases to validate its behavior across different scenarios.

Changes

File Change Summary
README.md Added PRs Welcome badge
src/fs.ts Added new async exists() function for file/directory existence checking
src/index.ts Exported fs module functionality
test/fs.test.ts Added unit tests for exists() function covering various scenarios

Sequence Diagram

sequenceDiagram
    participant Client
    participant exists() as exists Function
    participant fs as Node.js fs Module
    
    Client->>exists(): Check file path
    exists()->>fs: Call stat()
    alt Path Exists
        fs-->>exists(): Return Stats
        exists()-->>Client: Return Stats
    else Path Not Found
        fs-->>exists(): Throw ENOENT Error
        exists()-->>Client: Return false
    else Other Error
        fs-->>exists(): Throw Error
        exists()->>Client: Rethrow Error
    end
Loading

Poem

🐰 A rabbit's tale of file detection,
Checking paths with swift reflection,
exists() hops through filesystem trees,
Finding files with elegant ease,
No path escapes its watchful gaze! 🔍


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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

pkg-pr-new bot commented Dec 22, 2024

Open in Stackblitz

npm i https://pkg.pr.new/node-modules/utility@65

commit: 03a78d2

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

🧹 Nitpick comments (3)
src/fs.ts (1)

4-7: Enhance documentation with security considerations

The JSDoc comments should include:

  • Warning about potential race conditions
  • Note about path traversal risks
  • Example usage

Consider expanding the documentation:

 /**
  * Check if a file exists.
  * Returns the file stats if it exists, or `false` if it doesn't.
+ * 
+ * @param file - The path to check
+ * @returns Promise<Stats | false> - Stats object if file exists, false otherwise
+ * 
+ * @example
+ * ```ts
+ * const stats = await exists('./config.json');
+ * if (stats && stats.isFile()) {
+ *   // File exists and is a regular file
+ * }
+ * ```
+ * 
+ * @security This function may be subject to race conditions between check and usage.
+ * Ensure proper path sanitization to prevent path traversal attacks.
  */
test/fs.test.ts (2)

5-6: Consider importing exists only once

The file imports the same function twice through different paths.

 import * as utility from '../src/index.js';
-import { exists } from '../src/index.js';

13-28: Enhance test coverage

While the current tests cover basic functionality, consider adding tests for:

  1. Symlinks
  2. Permission denied errors
  3. Additional path traversal scenarios
  4. Unicode paths

Example additional test cases:

it('should handle symlinks', async () => {
  // Test both symlink to file and directory
});

it('should handle permission errors', async () => {
  // Test accessing a file without proper permissions
});

it('should handle special paths', async () => {
  // Test Unicode paths
  // Test relative path traversal attempts
});
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 153e3ff and 308fe3a.

📒 Files selected for processing (4)
  • README.md (1 hunks)
  • src/fs.ts (1 hunks)
  • src/index.ts (1 hunks)
  • test/fs.test.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • README.md
🔇 Additional comments (2)
src/index.ts (1)

12-12: LGTM!

The export statement follows the established pattern in the file and correctly exposes the new fs module.

src/fs.ts (1)

8-17: Consider adding path validation

The implementation correctly handles errors, but consider adding path validation to prevent path traversal attacks.

Let's check if there are any path validation utilities already in use:

Copy link

codecov bot commented Dec 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.12%. Comparing base (bc3e168) to head (03a78d2).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #65      +/-   ##
==========================================
+ Coverage   99.11%   99.12%   +0.01%     
==========================================
  Files          12       13       +1     
  Lines         899      917      +18     
  Branches      184      188       +4     
==========================================
+ Hits          891      909      +18     
  Misses          8        8              

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

@fengmk2 fengmk2 merged commit eb95f36 into master Dec 22, 2024
27 checks passed
@fengmk2 fengmk2 deleted the fs-exists branch December 22, 2024 16:56
fengmk2 pushed a commit that referenced this pull request Dec 22, 2024
[skip ci]

## [2.4.0](v2.3.0...v2.4.0) (2024-12-22)

### Features

* support fs.exists async function ([#65](#65)) ([eb95f36](eb95f36))
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.

1 participant