Skip to content

Conversation

@torbs
Copy link

@torbs torbs commented Sep 28, 2022

Currently we will receive a url without a slash delimiter if we pass inn a relative url to the file method without a starting slash. With this fix the URL is correct even if the starting slash is missing from the argument. It does not break any of the current tests, but we should consider if this is a breaking change.

@torbs torbs requested a review from trygve-lie September 28, 2022 14:32
Copy link
Member

@digitalsadhu digitalsadhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that how it currently works is a bit of a footgun. Have run into it myself. Interested to hear @trygve-lie thoughts.

file(file = '') {
const base = this.base();
let value;
if (base && ABSOLUTE_URL_REGEX.test(base)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use path.isAbsolute here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked. isAbsolute is not doing the same thing as my test. my check is for an absolute URL, but path.isAbsolute checks for an absolute path. So isAbsolute('/test') is true but ABSOLUTE_URL_REGEX.test('/test') is false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point.

@torbs
Copy link
Author

torbs commented Sep 28, 2022

An alternative solution to this problem is:
value = `${base}${join('/', file)}`;
This is of course much simpler. But I believe using built-in methods to create the file path might help us avoid some edge cases

@trygve-lie
Copy link
Contributor

I am fine with this. This did though get me to realize that we have more or less forgot to apply our common pattern for building paths and URL in this module. We try to strive for a pattern where each URL / path part always start with a / and never ends with one. We then wash argument on public methods for this and we then kinda assume we can just join them together without checking what they are everywhere.

We already have methods for this in the common package: https://github.com/eik-lib/common/blob/next/packages/utils/src/path-slashes.js

If we followed this pattern we would end up with something like so;

file(file = '') {
    const sanitizedFile = utils.addLeadingSlash(file);
    return new Asset({
        value: `${this.base()}${sanitizedFile}`,
    });
}

In the above return value from this.base() is safe because a removal of any end / is dealt with in that method.

Though; I think that is food for a second PR so I am fine with this.

@trygve-lie trygve-lie merged commit 89253e0 into next Sep 30, 2022
@trygve-lie trygve-lie deleted the add_file_staring_slash_next branch September 30, 2022 15:01
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.

3 participants