Skip to content
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

[FEATURE] simplistic serveIndex with additional properties #23

Merged
merged 2 commits into from
Jul 9, 2018

Conversation

petermuessig
Copy link
Contributor

@RandomByte @matz3 please take a look into the new serveIndex alternative for the ui5-server.

There are still some internals from the resource used when creating the resource info object. Maybe some of the internals can also become something for a more official API of the Resource but we could do that in a second step and provide the simplistic serveIndex to avoid the dependency to a github repo.

@CLAassistant
Copy link

CLAassistant commented Jul 6, 2018

CLA assistant check
All committers have signed the CLA.

@petermuessig petermuessig force-pushed the feature-serveIndex branch 2 times, most recently from 2860b59 to f4bff11 Compare July 6, 2018 16:13
@coveralls
Copy link

coveralls commented Jul 6, 2018

Pull Request Test Coverage Report for Build 102

  • 35 of 44 (79.55%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+1.4%) to 73.75%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/middleware/serveIndex.js 33 42 78.57%
Totals Coverage Status
Change from base Build 97: 1.4%
Covered Lines: 255
Relevant Lines: 327

💛 - Coveralls

@petermuessig petermuessig force-pushed the feature-serveIndex branch 2 times, most recently from 90bb04f to 996baab Compare July 6, 2018 20:03
@petermuessig
Copy link
Contributor Author

The tests may be a bit primitive but at least the logic is executed and a lightweight check for the result has been implemented.

@petermuessig petermuessig added the enhancement New feature or request label Jul 7, 2018
@@ -102,7 +102,6 @@
"portscanner": "^2.1.1",
"prompt": "^1.0.0",
"replacestream": "^4.0.3",
"serve-index": "RandomByte/serve-index#fs-option",
Copy link
Member

Choose a reason for hiding this comment

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

I would have expected that when the package.json has changed, there also should be a new package-lock.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I did not check that the package-lock.json is submitted. I do not do this normally...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

size: formatSize(stat.size),
sizeInBytes: stat.size,
project: resource._project.id,
realPath: resource._project.path + resource.getPath()
Copy link
Member

Choose a reason for hiding this comment

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

path.join?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point!

} else if (!a.isDir && b.isDir) {
return 1;
} else {
return a.name.localeCompare(b.name);
Copy link
Member

Choose a reason for hiding this comment

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

localCompare doesn't do well when comparing numbers:
screen shot 2018-07-08 at 23 36 28

(Finder sorts correctly)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is like a filesystem sorts the numbers! ;-)

Copy link
Member

Choose a reason for hiding this comment

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

But not how most file system browsers (Finder, Explorer, etc.) sort numbers. I find this sorting rather confusing. Maybe we could also check how Apache and others do this.

Copy link
Member

Choose a reason for hiding this comment

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

Mhmm, what sorting do you expect?

The sorting in Windows Explorer is not more than a "nice try" from my POV - I really hate it. Did you ever try to find a GUID in a folder full of GUIDS (some starting with digits, others not? It's a mess. You easily miss a file just because of the sorting. Simple alphanumeric sorting would be much better.

"Smart" sorting only makes sense when a folder only contains a single kind of names (only alpha or only numeric). But that's not how it is implemented, at least not in Windows Explorer.

+1 for strict ASCII sorting...

size: formatSize(stat.size),
sizeInBytes: stat.size,
project: resource._project.id,
realPath: resource._project.path + resource.getPath()
Copy link
Member

Choose a reason for hiding this comment

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

This can't work. Appending the full virtual path of the resource to the physical root path of its project (not even the resources reader) can only produce valid paths by pure chance.

For /resources/sap-ui-core.js you currently get: [...]/node_modules/@openui5/sap.ui.core/resources/sap-ui-core.js instead of [...]/node_modules/@openui5/sap.ui.core/src/sap-ui-core.js.

There is currently no way to get the "physical" path of a resource. This is intentional as ui5-fs is explicitly not relying on physical file systems.

In general, I don't know whether we should expose full file paths here just for convenience. People could still use this for module on production systems and the file paths might represent security relevant information. How is this handled by Apache et.al.?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I spent a TODO comment to consider this but keep the logic for now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will just use the project path

lastModified: new Date(stat.mtime).toLocaleString(),
size: formatSize(stat.size),
sizeInBytes: stat.size,
project: resource._project.id,
Copy link
Member

Choose a reason for hiding this comment

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

The _project attribute is undocumented and commented as an Experimental, internal parameter. We should at least deal with it eventually being undefined (which it is for all dynamically generated resources).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As written in my comments before, we need to consider whether to expose some more additional information

* @returns {String} HTML content for the resource listing
*/
function createContent(path, resourceInfos) {
return `<!DOCTYPE html>
Copy link
Member

Choose a reason for hiding this comment

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

I would love to have this as a Handlebars template. All with syntax highlighting and stuff. I'm ok with merging this as a start but would offer myself to move it into a proper template later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather tend to do this as an enhancement in future

* @param {Resource} resource the resource
* @returns {String} mime type
*/
function getMimeType(resource) {
Copy link
Member

Choose a reason for hiding this comment

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

Unit test for this function would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you do this for the serveResource? We should better externalize this in a util function... ;-)

* @param {Number} bytes bytes
* @returns {String} human readable size
*/
function formatSize(bytes) {
Copy link
Member

Choose a reason for hiding this comment

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

Unit test for this function would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Util function would be better - but those kind of enhancements might be something for a general cleanup in a separate commit

* @param {Resource} resource the resource to convert
* @returns {Object} resource info object
*/
function createResourceInfo(resource) {
Copy link
Member

Choose a reason for hiding this comment

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

Unit test for this function would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Internally called by serveIndex which runs through that function!

* @param {Resource[]} resources an array of resources
* @returns {Object[]} sorted array of resource infos
*/
function createResourceInfos(resources) {
Copy link
Member

Choose a reason for hiding this comment

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

Unit test for this function would be great

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Internally called by serveIndex which runs through that function! See line coverage...

* @param {Object[]} resourceInfos an array of resource infos
* @returns {String} HTML content for the resource listing
*/
function createContent(path, resourceInfos) {
Copy link
Member

Choose a reason for hiding this comment

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

Unit test for this function would be great 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Internally called by serveIndex which runs through that function! See line coverage...

@petermuessig petermuessig force-pushed the feature-serveIndex branch 2 times, most recently from dcec1be to dcf5194 Compare July 9, 2018 08:06
@RandomByte
Copy link
Member

A rebase is necessary due to conflicts in package-lock.json

@petermuessig
Copy link
Contributor Author

Grml - again a merge conflicT!

@petermuessig
Copy link
Contributor Author

Rebase has been done

@matz3
Copy link
Member

matz3 commented Jul 9, 2018

I've added an export in index.js, so that it can be used directly.

@petermuessig
Copy link
Contributor Author

@matz3 thanks for adding the serveIndex to the export - I did not see that place...

@RandomByte what about a merge now?

Copy link
Member

@RandomByte RandomByte left a comment

Choose a reason for hiding this comment

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

Fair enough I guess

@RandomByte
Copy link
Member

RandomByte commented Jul 9, 2018

Things I would like to follow up on:

  • Unit tests for most functions (should be required for all new features of the tooling)
  • Exposing local paths via server - security relevant?
  • Check internal Resource attributes _name and _project
  • Use HTML template
  • Make it look attractive (like it's from 2018)

<td${info.isDir ? ">" : " title=\"" + info.sizeInBytes + " Bytes\">" + info.size}</td>
<td><a href="${info.path}">${info.name}</a></td>
<td>${info.mimetype}</td>
<td><a href="file:////${info.projectPath}" title="${info.projectPath}">${info.project}</a></td>
Copy link
Member

Choose a reason for hiding this comment

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

file:// URLs neither work in Chrome, nor Safari (didn't test others) for security reasons: https://support.google.com/gsa/answer/2664790?hl=en

Console error logged in both browsers after clicking such a link: Not allowed to load local resource. From what I found, only resources loaded via file:// can open such links.

I would opt for removing those links.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would rather keep it as the link would be a server absolute link on mac which would point to the wrong location. At least copy and paste can work partially with the file URL...

Copy link
Member

Choose a reason for hiding this comment

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

Imho this is confusing. A non-clickable link with no explanation whatsoever.

Also, what is the benefit of opening a projects directory in your Browser?
I'm not aware how to open file:// links directly in the Finder or Terminal.

@petermuessig petermuessig merged commit b96d937 into master Jul 9, 2018
@petermuessig petermuessig deleted the feature-serveIndex branch July 9, 2018 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants