-
Notifications
You must be signed in to change notification settings - Fork 305
Removes acl files from list of files to match when mapping url to file #1109
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
Conversation
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'm thinking that it effectively does a greedy match, and I'm thinking it should be more modest... :-) In terms of a regexp, it did ^/index\..*/, and with this patch ^/index\..*[^\.acl]$/, but an alternative solution to this problem would have been ^/index.(.*?)/. In the latter case, it would have matched only one period after the filename, and so excluded the .acl, since that would have had two .s.
Not sure what would have been the best solution, though, so Approve.
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.
I would propose to not hardcode the .acl suffix, as this would result in the same problem for .meta files. Another solution may be to configure an array for all blacklisted suffixes in the constructor.
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.
Good point, @rubensworks . Yeah, I can see it being passed around as suffixAcl here and there. Doesn't seem to be very consistently applied, but I think it is a good idea to look into using that.
|
I'll push an update soon that doesn't hardcode it ^_^ |
|
@rubensworks @kjetilk I've pushed an update, hope it looks better ^_^ (Was uncertain how to include the knowledge of suffixes into resourceMapper, so hope the way I did it looks ok) |
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.
Yup, it does! @rubensworks ?
lib/resource-mapper.js
Outdated
|
|
||
| // Find a file with the same name (minus the dollar extension) | ||
| return (files.find(f => this._removeDollarExtension(f) === filename || | ||
| (isIndex && f.startsWith(this._indexFilename + '.') && !f.endsWith(this._suffixes.acl)))) |
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.
I would go one step further, and loop over all this._suffixes to exclude all file names that end with any of the defined suffixes. Right now, only index.html.acl will be blacklisted, but index.html.meta will still cause the same problem. A loop over this._suffixes would solve this.
For this purpose, it may be easier to refactor this._suffixes to be an array instead of a hash, as this is (AFAICS) only being used here.
|
@rubensworks then I've implemented your suggested changes (changing fileSuffixes from an object to an array). Could you have a look at the latest change, and see if you think it's good? |
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 to me now! :-)
Perhaps the fileSuffixes variable name can be changed to something more descriptive such as blacklistedFileSuffixes, but I have no strong opinion on this.
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.
I'm missing unit tests for ResourceMapper here, as well as in #1067 actually. It is one of the only unit-testable classes we have, and it used to have 100% coverage with many cases. Could we keep that up?
| indexFilename = 'index', | ||
| overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' } | ||
| overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' }, | ||
| fileSuffixes = ['.acl', '.meta'] |
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.
Unclear variable name. I would name it something like controlExtensions.
lib/resource-mapper.js
Outdated
| this._defaultContentType = defaultContentType | ||
| this._indexFilename = indexFilename | ||
| this._types = { ...types, ...overrideTypes } | ||
| this._suffixes = fileSuffixes |
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.
Idem. They're not just suffixes, they are suffixes for a specific kinds of files that needs special behavior.
| } | ||
| // Determine the path of an existing file | ||
| } else { | ||
| // Read all files in the corresponding folder |
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.
Outdated comment
| // Find a file with the same name (minus the dollar extension) | ||
| let match = !searchIndex ? '' : (files.find(f => this._removeDollarExtension(f) === filename || | ||
| (isIndex && f.startsWith(this._indexFilename + '.')))) | ||
| let match = searchIndex ? await this._getMatchingFile(folder, filename, isIndex) : '' |
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.
I'm not sure if I would move this specific bit to a new function. Is there potential for reuse?
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.
My thinking was to make it more readable by giving it a name. Don't know of any potential reuse, no.
lib/resource-mapper.js
Outdated
| const files = await this._readdir(folder) | ||
|
|
||
| // Find a file with the same name (minus the dollar extension) | ||
| return (files.find(f => this._removeDollarExtension(f) === filename || |
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.
We should probably expand this condition across multiple lines now, given the increased complexity.
lib/resource-mapper.js
Outdated
|
|
||
| // Find a file with the same name (minus the dollar extension) | ||
| return (files.find(f => this._removeDollarExtension(f) === filename || | ||
| (isIndex && f.startsWith(this._indexFilename + '.') && !this._suffixes.some(suffix => f.endsWith(suffix))))) |
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.
Seems wasteful to always iterate over the extensions.
How about, in the constructor, we instead use a regex to determine whether something is a control file? Then the default value is /.(?:acl|meta)$/, and this routine just does this._controlFile.test(f).
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.
That sounds reasonable - I'll write tests that make sure stuff works, and then try to optimize if per your suggestions 😸
|
Ah, yes, I've been lazy on tests here. I'll write them when I have time, hopefully during the next couple of days. |
Trying to optimise code, and improve abstraction for better readability Hopefully responding to the concerns of @RubenVerborgh: #1109 (comment) and #1109 (comment)
|
@RubenVerborgh think I have addressed your concerns; I'm uncertain of how to run coverage reports on this actually, could you check? (And perhaps drop a line on how you do it here? Could read up more on it, but am in bit of a hurry right now =P ) |
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 except for regexp.
lib/resource-mapper.js
Outdated
| this._defaultContentType = defaultContentType | ||
| this._indexFilename = indexFilename | ||
| this._types = { ...types, ...overrideTypes } | ||
| this._isControlFile = new RegExp(`.(?:${fileSuffixes.join('|')})$`) |
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 the right regex: a) fileSuffixes already has a leading dot, so there are two dots now b) dots need to be escaped in regexes (but, since you are creating the regex from a string, actually needs double backslash '\\.).
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.
Alternatively, you can just pass the regex itself instead of passing fileSuffixes.
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.
That makes it difficult to configure the suffixes though =/
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.
Hope the latest commit looks good.
|
If you're concerned about coverage... why not use a tool like CodeCov? |
| this._defaultContentType = defaultContentType | ||
| this._indexFilename = indexFilename | ||
| this._types = { ...types, ...overrideTypes } | ||
| this._isControlFile = new RegExp(`(?:${fileSuffixes.map(fs => fs.replace('.', '\\.')).join('|')})$`) |
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.
fs.replace(/\./g, '\\.') strictly, but good enough 😉
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.
Regex is so difficult =P
Coverage in general is not great yet; but it was for this particular class 😎 |
Should fix #1104