Skip to content

fix(uploads): settle GridFS removal callback on the MongoDB 7 driver - #4271

Merged
PierreBrisorgueil merged 1 commit into
masterfrom
fix/gridfs-remove-callback
Nov 12, 2026
Merged

PierreBrisorgueil merged 1 commit into
masterfrom
fix/gridfs-remove-callback

Conversation

@PierreBrisorgueil

@PierreBrisorgueil PierreBrisorgueil commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Scope

  • Module(s) impacted: uploads (GridFS storage engine, list)
  • Cross-module impact: none
  • Risk level: low

Technical detail

  • _removeFile settles the multer callback from the bucket.delete() promise.
  • contentType moved to metadata.contentType, since driver 7 dropped the top-level option.
  • list() reads metadata.contentType with a legacy top-level fallback; response shape unchanged.
  • Test mock is now driver-shaped; it fails on master.
  • Pre-push review OK, security pass: no HIGH/MEDIUM.

Validation

  • npm run lint
  • npm test
  • Manual checks done (if applicable)

Guardrails check

  • No secrets or credentials introduced (.env*, secrets/**, keys, tokens)
  • No risky rename/move of core stack paths
  • Changes remain merge-friendly for downstream projects
  • Tests added or updated when behavior changed

Summary by CodeRabbit

  • Bug Fixes
    • Improved file type handling for uploads, including files that store their content type in metadata. Existing uploads that store the content type in the legacy location remain supported.
    • Improved upload removal handling so completion and deletion errors are reported correctly.

GridFSBucket.delete() returns a promise and never calls a callback, so
multer's _removeFile cb was never called and a request could hang on an
upload error path. Settle cb from the promise.

The driver drops the top-level contentType option of openUploadStream:
createFromBuffer now stores it in metadata.contentType, and the
repository list() reads it from there (top-level fallback for older
files), keeping the contentType field in its result.

Closes #4268
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GridFS deletion now settles Multer’s callback from the bucket’s promise. Buffer uploads store content type in metadata. The uploads repository reads metadata content type when the top-level value is falsy.

Changes

GridFS upload behavior

Layer / File(s) Summary
GridFS storage and cleanup
lib/services/gridfs.js, lib/services/tests/gridfs.unit.tests.js
GridFS deletion calls back on promise fulfillment or rejection. Buffer uploads store content type in metadata alongside supplied metadata. Tests cover both deletion outcomes and upload options.
Upload content type retrieval
modules/uploads/repositories/uploads.repository.js, modules/uploads/tests/uploads.repository.unit.tests.js
list returns lean results, selects both content-type fields, and uses metadata.contentType when the top-level value is falsy. Tests cover the fallback and the lean query chain.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • pierreb-devkit/Node#4269 — The PR updates callback JSDoc types in lib/services/gridfs.js, but does not add the checkJs configuration or script described by the issue.

Suggested labels: Fix

Merge Risk: Minimal · up to 13591

This change fixes GridFS file removal so the callback completes when the MongoDB driver's delete returns a promise. It also moves content type into upload metadata while still reading older records that store it at the top level. No concrete merge-blocking risk remains in the reviewed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the GridFS removal callback is settled on the MongoDB 7 driver, under the uploads scope. The raw summary confirms the removal path now uses the bucket.delete() promise…
Description check Passed The description follows the template's main sections: Summary, Scope, Validation, and Guardrails. It states what changed, why, the related issue (Closes #4268), affected modules, cross-module impact, …
Linked Issues check Passed Issue #4268 coding requirements are implemented at the reviewed head.

1. Removal callback: GridFsStorage._removeFile in lib/services/gridfs.js calls `this.bucket.delete(file.id).then(() => cb(null)…
Out of Scope Changes check Passed The changes stay within the scope of #4268 and the PR description.

- lib/services/gridfs.js: changes are limited to _removeFile, createFromBuffer content type placement, and JSDoc types. All of t…
Full details: Title check

Explanation

The title names the main change: the GridFS removal callback is settled on the MongoDB 7 driver, under the uploads scope. The raw summary confirms the removal path now uses the bucket.delete() promise. The title does not mention the contentType move or the list() fallback, but those are secondary.

Full details: Description check

Explanation

The description follows the template's main sections: Summary, Scope, Validation, and Guardrails. It states what changed, why, the related issue (Closes #4268), affected modules, cross-module impact, and risk level. It adds a Technical detail section that the template does not list, but this does not reduce its usefulness. The Optional Infra/Stack section and Notes for reviewers are absent. Both are optional or non-critical. The Validation and Guardrails checkboxes are all ticked.

Full details: Linked Issues check

Explanation

Issue #4268 coding requirements are implemented at the reviewed head.

  1. Removal callback: GridFsStorage._removeFile in lib/services/gridfs.js calls this.bucket.delete(file.id).then(() => cb(null), cb). It passes no callback to the driver and settles multer's cb on both success and rejection. This matches the issue fix.
  2. Content type storage: _handleFile already writes contentType inside metadata. createFromBuffer now writes { ...metadata, contentType } as metadata and drops the removed top-level option. The JSDoc states metadata.contentType.
  3. Content type read: list() in modules/uploads/repositories/uploads.repository.js selects metadata.contentType. It returns contentType from the top-level field when truthy, otherwise from metadata.contentType. The returned fields stay _id, filename, uploadDate, and contentType, so the response shape is unchanged.
  4. Regression test: according to the diff summary, lib/services/tests/gridfs.unit.tests.js adds a driver-shaped mock where delete returns a promise. It asserts cb(null) on resolve and cb(error) on reject. Adds list tests for the metadata fallback. I did not open the test files, and I did not run the suite. The claim that the test fails on master is not independently verified.
  5. checkJs: the JSDoc types for _removeFile and _handleFile were updated. I did not run tsc, so the claim that TS2345/TS2353/TS2769 are gone is not independently verified.

Unverified items are test outcomes and tooling output. The implementation evidence supports compliance.

Full details: Out of Scope Changes check

Explanation

The changes stay within the scope of #4268 and the PR description.

  • lib/services/gridfs.js: changes are limited to _removeFile, createFromBuffer content type placement, and JSDoc types. All of these are named in the issue.
  • modules/uploads/repositories/uploads.repository.js: the diff summary reports changes only to list() and its JSDoc. list() is the listing behavior the issue names. Adding .lean() changes the returned objects to plain objects. This does not change the client-facing fields. The deleteMany test chain update (.lean() before .exec()) supports the list() change.
  • Test files for gridfs and the uploads repository support the changed behavior.

The uploads repository file also contains other functions (remove, purgeUnreferenced, and others). The diff summary does not list them as changed in this PR, so they are treated as existing code. I did not compare them against the merge base, so this relies on the diff summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between af599c0 and 13591dc.

Files selected for processing: 4

  • lib/services/gridfs.js
  • lib/services/tests/gridfs.unit.tests.js
  • modules/uploads/repositories/uploads.repository.js
  • modules/uploads/tests/uploads.repository.unit.tests.js

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.64%. Comparing base (4132fa9) to head (13591dc).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4271      +/-   ##
==========================================
+ Coverage   94.61%   94.64%   +0.03%     
==========================================
  Files         175      175              
  Lines        6216     6218       +2     
  Branches     2019     2020       +1     
==========================================
+ Hits         5881     5885       +4     
+ Misses        272      270       -2     
  Partials       63       63              
Flag Coverage Δ
integration 64.81% <80.00%> (+0.01%) ⬆️
unit 80.33% <100.00%> (+0.29%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af599c0...13591dc. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@PierreBrisorgueil
PierreBrisorgueil merged commit 8a6a23e into master Nov 12, 2026
11 checks passed
@PierreBrisorgueil
PierreBrisorgueil deleted the fix/gridfs-remove-callback branch November 12, 2026 13:52
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.

🐛 GridFS file removal never calls back on the MongoDB 7 driver

1 participant