Skip to content

Commit 81e3c6f

Browse files
authored
Merge pull request #184 from sebj/reduce-feedtype-inspection-prefix-length
FeedType: reduce the inspection prefix length & throw early for insufficient data
2 parents 2cd0835 + f32d749 commit 81e3c6f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Sources/FeedKit/FeedType.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public extension FeedType {
8484
}
8585

8686
/// The number of bytes to inspect when determining the feed type.
87-
private let inspectionPrefixLength = 200
87+
private let inspectionPrefixLength = 128
8888

8989
// MARK: - FeedInitializable
9090

@@ -94,8 +94,12 @@ extension FeedType: FeedInitializable {
9494
/// - Parameter data: A `Data` object representing a feed to be inspected.
9595
/// - Returns: A `FeedType` if the data matches a known feed format, otherwise `nil`.
9696
public init(data: Data) throws {
97-
// Inspect only the first 200 bytes. This helps improve performance while
98-
// still providing enough data to reliably detect the feed format.
97+
guard data.count >= inspectionPrefixLength else {
98+
throw FeedError.unknownFeedFormat
99+
}
100+
101+
// Inspect only the first `inspectionPrefixLength` bytes. This helps improve performance
102+
// while still providing enough data to reliably detect the feed format.
99103
let string: String = .init(decoding: data.prefix(inspectionPrefixLength), as: UTF8.self)
100104

101105
// Determine the feed type

0 commit comments

Comments
 (0)