-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Invert "does this entry match the filter" logic
Before this change, we turned filters into a list of path prefixes and then checked entry paths against those prefixes. Rough pseudocode: ```javascript function doesEntryMatchFilter({ path }, filter) { const pathPrefixes = pathPrefixesFromFilter(filter) return pathPrefixes.some((prefix) => path.startsWith(prefix)) } ``` For performance and simplicity, I think it's cleaner if we "look up" entry paths in the existing filter object. Rough pseudocode: ```javascript function doesEntryMatchFilter({ path }, filter) { const { type, variant } = parsePath(path) return filter[type]?.includes(variant) } ``` I think this has two advantages: - It's less code. We don't need to worry about de-duping paths (e.g., `/photos/original` versus `/photos/`). We don't need to worry about sketchy paths (e.g., `/photos/garbage/../original`). - It's faster (at least, in theory). Rather than having to iterate over every path prefix, we only need to iterate over the variants of each path. (This could be further optimized with a `Set`.)
- Loading branch information
Showing
3 changed files
with
80 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import test from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import { filePathMatchesFilter } from '../../src/blob-store/utils.js' | ||
|
||
test('filePathMatchesFilter', () => { | ||
const filter = { | ||
photo: ['a', 'b'], | ||
video: [], | ||
} | ||
|
||
const shouldMatch = [ | ||
'/photo/a/foo.jpg', | ||
'/photo/b/foo.jpg', | ||
'/photo/a/', | ||
'/video/foo.mp4', | ||
'/video/foo/bar.mp4', | ||
'/video/', | ||
'/video///', | ||
] | ||
for (const filePath of shouldMatch) { | ||
assert( | ||
filePathMatchesFilter(filter, filePath), | ||
`${filePath} matches filter` | ||
) | ||
} | ||
|
||
const shouldntMatch = [ | ||
'/photo/c/foo.jpg', | ||
'/photo/c/', | ||
'/photo/a', | ||
'/photo/ax/foo.jpg', | ||
'/photo/c/../a/foo.jpg', | ||
'/photo', | ||
'/photo/', | ||
'/photo//', | ||
'/PHOTO/a/foo.jpg', | ||
'/audio/a/foo.mp3', | ||
'photo/a/foo.jpg', | ||
'//photo/a/foo.jpg', | ||
' /photo/a/foo.jpg', | ||
'/hasOwnProperty/', | ||
'/hasOwnProperty/a/foo.jpg', | ||
] | ||
for (const filePath of shouldntMatch) { | ||
assert( | ||
!filePathMatchesFilter(filter, filePath), | ||
`${filePath} doesn't match filter` | ||
) | ||
} | ||
}) |