Repository navigation
feat!: Custom CacheControl header per file - #167
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 📝 WalkthroughWalkthroughPublic API updated to introduce FileInfo and per-file cache-control callbacks. Static file serving internals were refactored to operate on FileInfo, adjust MIME detection, and propagate FileInfo through range and header-building logic. Tests were updated to pass function-typed cacheControl arguments and a new per-file caching test was added. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Client
participant H as StaticHandler
participant L as LruCache
participant FS as FileSystem
participant CC as CacheControlCallback
participant M as MimeResolver
C->>H: HTTP GET/HEAD /path
H->>L: Lookup FileInfo by key
alt Cache miss or stale
H->>FS: stat(path), openRead(...)
FS-->>H: File, FileStat, header bytes
H->>M: resolve mimeType(header bytes, extension)
M-->>H: mimeType
H->>L: Put(FileInfo)
end
H->>CC: cacheControl(fileInfo)
CC-->>H: CacheControlHeader? (per-file)
alt Conditional headers (If-None-Match/If-Modified-Since)
H-->>C: 304 Not Modified + headers
else Range request
H-->>C: 206 Partial Content + headers + range body
else Full content
H-->>C: 200 OK + headers + file body
end
note over H: All read paths use fileInfo.file.openRead(...)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #167 +/- ##
==========================================
- Coverage 91.82% 91.77% -0.06%
==========================================
Files 85 85
Lines 3118 3123 +5
Branches 1616 1618 +2
==========================================
+ Hits 2863 2866 +3
- Misses 255 257 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
516aad6 to
d12c15b
Compare
940315e to
1496401
Compare
|
@CodeRabbit review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 29
♻️ Duplicate comments (1)
lib/src/io/static/static_handler.dart (1)
109-113: Breaking change in createFileHandler needs the same backward compatibility treatmentSimilar to
createStaticHandler, this function now requires thecacheControlparameter, creating a breaking change.
🧹 Nitpick comments (3)
lib/src/io/static/static_handler.dart (2)
449-456: Consider moving the Stream extension to a utility moduleThe
firstOrNullextension onStream<T>is useful but placing it at the end of this file makes it less discoverable and could lead to duplication if needed elsewhere.Consider moving this extension to a shared utility module (e.g.,
lib/src/utils/stream_extensions.dart) where it can be reused across the codebase:// In lib/src/utils/stream_extensions.dart extension StreamExtensions<T> on Stream<T> { Future<T?> get firstOrNull async { await for (final value in this) { return value; } return null; } }Then import it in this file:
import '../../utils/stream_extensions.dart';
25-29: Potential performance concern with frequent stat callsThe
isStalegetter callsfile.statSync()on every access, which could be expensive if called frequently. Consider caching the fresh stat temporarily or documenting the performance implications.Consider either:
- Document that
isStaleperforms file I/O and should be used judiciously- Or implement a time-based cache for the fresh stat to avoid excessive I/O:
class FileInfo { final File file; final MimeType? mimeType; final FileStat stat; final String etag; // Cache fresh stat for a short duration FileStat? _cachedFreshStat; DateTime? _cacheTime; static const _cacheExpiry = Duration(milliseconds: 100); bool get isStale { final now = DateTime.now(); if (_cachedFreshStat == null || _cacheTime == null || now.difference(_cacheTime!) > _cacheExpiry) { _cachedFreshStat = file.statSync(); _cacheTime = now; } return stat.size != _cachedFreshStat!.size || stat.changed.isBefore(_cachedFreshStat!.changed); } const FileInfo(this.file, this.mimeType, this.stat, this.etag); }test/static/cache_control_test.dart (1)
65-69: Tighten JPG match to avoid false positives.endsWith('jpg') can match unintended paths; include the dot.
- cacheControl: (final fileInfo) => - fileInfo.file.path.endsWith('jpg') ? cacheControl : null, + cacheControl: (final fileInfo) => + fileInfo.file.path.toLowerCase().endsWith('.jpg') ? cacheControl : null,
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (16)
lib/src/io/static/static_handler.dart(15 hunks)test/static/alternative_root_test.dart(5 hunks)test/static/basic_file_test.dart(14 hunks)test/static/cache_control_test.dart(3 hunks)test/static/content_type_test.dart(3 hunks)test/static/create_file_handler_test.dart(11 hunks)test/static/default_handler_test.dart(1 hunks)test/static/get_handler_test.dart(1 hunks)test/static/if_modified_since_test.dart(1 hunks)test/static/if_none_match_test.dart(1 hunks)test/static/if_range_test.dart(1 hunks)test/static/not_found_test.dart(1 hunks)test/static/range_edge_cases_test.dart(1 hunks)test/static/range_test.dart(1 hunks)test/static/symbolic_link_test.dart(4 hunks)test/static/unsupported_methods_test.dart(1 hunks)
🔇 Additional comments (6)
lib/src/io/static/static_handler.dart (2)
19-32: FileInfo class looks good with efficient staleness checkThe new public
FileInfoclass is well-designed. TheisStalegetter efficiently detects file changes by comparing size and modification time, which is appropriate for cache invalidation.
208-208: Good defensive programming with firstOrNull extensionThe change from
firsttofirstOrNullis a good defensive programming practice that prevents exceptions when dealing with empty streams.test/static/if_range_test.dart (1)
16-16: Test correctly updated for new APIThe test has been properly updated to pass a function for the
cacheControlparameter. The use of(final _) => nullclearly indicates that cache control is disabled for this test.test/static/unsupported_methods_test.dart (1)
15-15: Test properly migrated to new APIThe test correctly uses the new function-based
cacheControlparameter.test/static/if_modified_since_test.dart (1)
15-15: Consistent test API usageTest properly updated to use the new
cacheControlfunction parameter.test/static/not_found_test.dart (1)
16-16: Test migration completeThe test has been correctly updated to use the new function-based
cacheControlAPI.
SandPod
left a comment
There was a problem hiding this comment.
A couple of questions and a test fix requirement.
a8f3e11 to
2a7d78f
Compare
a50f10c to
7eb0fdd
Compare
|
@SandPod Please disregard the minor fall in code-coverage |
Description
Allow Cache-Control header to be set per file.
Related Issues
Pre-Launch Checklist
Please ensure that your PR meets the following requirements before submitting:
///), ensuring consistency with existing project documentation.Breaking Changes
Changes the interface of
createStaticHandlerandcreateFileHandler.Summary by CodeRabbit