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: call fs.createReadStream lazily #2357

Merged
merged 6 commits into from Feb 26, 2024

Conversation

mbargiel
Copy link
Contributor

When using replyWithFile, a ReadStream is immediately created wih the file path. When the mock is hit, another ReadStream is created immediately if the interceptor is configured to be set again or if its scope has _persist set. When calling nock.removeInterceptor, the ReadStream isn't closed.

As a result, it's not possible to delete the folder where the file exists on Windows unless implementing a workaround like multiple fs.rm calls.

This PR removes the eager fs.createReadStream call when setting up the interceptor and instead configures the route to create
the stream lazily, in a callback invoked when intercepting a call. There is no longer any need for replyWithFile-related logic in Interceptor#markConsumed().

I also added an extra test for replyWithFile + persist(), which I think makes coverage a bit more complete.

Fixes #2354

@mbargiel
Copy link
Contributor Author

Hello dear nock maintainers! I'd like to know if there's any particular reason this PR (or either of the other two PRs I opened at the same time, #2355 and #2356) have not been looked at yet.

For starters, my branch is now out of date, and the CONTRIBUTING.md file says I should be targeting a beta branch (but there's none). But aside from that, is there anything wrong or missing? Updating merging the latest changes to the branch is something that can be done orthogonally from the review.

@mbargiel mbargiel changed the title fix: call fs.createReadStream lazily fix: call fs.createReadStream lazily Feb 17, 2024
@mbargiel mbargiel force-pushed the feature/fix-2354-lazy-replywithfile branch from aed6c64 to a6490c3 Compare February 17, 2024 16:10
@mbargiel mbargiel force-pushed the feature/fix-2354-lazy-replywithfile branch from a6490c3 to e344eb5 Compare February 17, 2024 16:16
@mbargiel
Copy link
Contributor Author

@mikicho Branch updated - rebased on top of main, and I also split my one commit into 3 (separated individual test additions from the fix itself)

Copy link
Contributor

@mikicho mikicho left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

lib/interceptor.js Outdated Show resolved Hide resolved
Copy link
Member

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

LGTM

@Uzlopak Uzlopak force-pushed the feature/fix-2354-lazy-replywithfile branch from a09e296 to 73757d4 Compare February 17, 2024 22:01
@mikicho mikicho enabled auto-merge (squash) February 26, 2024 21:45
@mikicho mikicho merged commit ba9fc42 into nock:main Feb 26, 2024
23 checks passed
Copy link

🎉 This PR is included in version 13.5.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

replyWithFile prevents deleting folder when persist is used or interceptor is not hit
3 participants