-
Notifications
You must be signed in to change notification settings - Fork 27
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(path): correctly encode delimiters and relative paths #108
Conversation
Chore
Tests
Bug Fixes
Styles
ContributorsCommit-Lint commandsYou can trigger Commit-Lint actions by commenting on this PR:
|
900975e
to
e2f6c11
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! And I'm glad to see some much needed improvements to the test runner.
To try and address some of your other points and questions:
This library exposes a to_url method instead of a build_url method like some of the others for example.
Unfortunately, we can't do much about this inconsistency since it is a part of the library's public API. But for anything else (like helper and private methods), feel free to refactor them to make the naming consistent across the kit.
Moreover, the logic for path encoding is separate from the rest of the library, but that's not how the projects are organized elsewhere.
Fun fact, imgix-rb was the first of the imgix SDK libraries. As a result, it's a lot unlike the other projects in most cases (Client -> path -> to_url
). Like I just mentioned, we're open to reorganizing things so long as it's unnoticeable to end users.
lib/imgix/path.rb
Outdated
def to_url(opts = {}) | ||
@path = sanitize_path(@path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this cause multiple encoding issues if to_url
is called multiple times on the same Path
? Perhaps a different ivar name and ||= sanitize_path(@path)
could be used instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion 💯 , I'll try this out locally and push the change if all looks good.
Oh also another note, #105 #103 introduced some performance benchmarks which I think we should extend and take advantage of. For example, you can try running all benchmarks in |
# otherwise, encode only specific characters | ||
return encode_URI(path) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Variant PR, which shifts a lot of this escaping/encoding work to application boot, this would ideally be done in the Variant initializer: https://github.com/imgix/imgix-rb/pull/104/files#diff-f7f33d40dff4d8a40100ac8a68f7fab2e082a98a683b678c8ee04c1bc930102aR8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't had a chance to dig into #104 too much, but loved what I saw so far. Will definitely keep this in mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion @stevehodgkiss. I could not find any encoding issues when to_url
is called multiple times on the same Path
, but I’m sure there will be edge cases that I haven’t thought of. Also, I think the ivar for sanitized_path
creates a more predictable experience for the user.
I’ve implemented a solution based on your idea:
Lines 17 to 27 in bd5bdec
def to_url(opts = {}) | |
sanitized_path ||= sanitize_path(@path) | |
prev_options = @options.dup | |
@options.merge!(opts) | |
current_path_and_params = path_and_params(sanitized_path) | |
url = @prefix + current_path_and_params | |
if @secure_url_token | |
url += (has_query? ? "&" : "?") + "s=#{signature(current_path_and_params)}" | |
end |
Before these changes, @path
was being modified in a way that was not explicit. There may be a case in the future where we want an @sanitized_path
var or a santize_path!
func, but for the purposes of this PR that is out of scope.
There are a lot of improvements needed in this file and the library throughout, but I felt these changes have to least potential for producing breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I take back my previous comment about "this would ideally be done in the Variant initializer". This can't be done there since it's about the Path itself, not the Variant.
7315506
to
bd5bdec
Compare
My apols, blew up the commit history locally without realizing. Never push before your first cup of coffee ☕ 😴 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At minimum, I think we need to have the tests for those other two paths I mentioned, other than that, looks good 👍
end | ||
|
||
def to_url(opts = {}) | ||
sanitized_path ||= sanitize_path(@path) | ||
prev_options = @options.dup | ||
@options.merge!(opts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but it would be great if this state mutation could be removed by passing state in method calls, like you're doing with sanitized_path
.
Co-authored-by: Steve Hodgkiss <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Description
This PR adds a
sanitize_path
method topath.rb
, related tests, and build improvements.Tests
Using
gem 'minitest-reporters'
,miniTest
output logs now behave similarly to rSpec's, with color formatting and better diffing.Test suite logging example:
Diffing example:
Tests can also be run in VSCode as the default test task,
SHIFT + COMMAND + T
in my system for example.Up for discussion
One of the ways this code base could be improved would be to re-name a lot of the methods. This library exposes a
to_url
method instead of abuild_url
method like some of the others for example. Moreover, the logic for path encoding is separate from the rest of the library, but that's not how the projects are organized elsewhere.