Skip to content

Make sure correct client files are served, fixes #3555 #3557

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ Server.prototype.serveClient = function(v){
if (!arguments.length) return this._serveClient;
this._serveClient = v;
var resolvePath = function(file){
var filepath = path.resolve(__dirname, './../../', file);
if (exists(filepath)) {
return filepath;
}
return require.resolve(file);
var clientPath = require.resolve('socket.io-client'); // resolve the client dep. end in lib/index
return path.normalize(path.join(path.dirname(clientPath), '..', '..', file));

Choose a reason for hiding this comment

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

For the sake of code legibility, I'd be great to have each function execution on a separate line, assigning the result to a variable with a readable+understandable variable name.

Copy link
Author

Choose a reason for hiding this comment

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

If needed I can do that, but is it not undrstandable?
Else my proposals would be

var clientLibDir = path.dirname(clientPath)
var clientFilePath = path.join(clientLibDir, '..', '..', file);

Choose a reason for hiding this comment

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

(Just to be clear, I don't think the original code is very legible either).

My intuition is something like:

var pathToClosestSocketIOClientMainFile = require.resolve('socket.io-client');
var pathToParentNodeModulesFolder = path.join(path.dirname(clientPath), '..', '..')
return path.normalize(path.join(pathToParentNodeModulesFolder, file));

Imo, ideally code is readable without comments.

Copy link

@dasilvacontin dasilvacontin May 6, 2020

Choose a reason for hiding this comment

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

"If needed I can do that, but is it not undrstandable?"

We have the context, but someone else might not know why not just do require.resolve(file). (Which I just realized isn't really addressed in my suggestion, and it should. Maybe a comment)

And 2), to someone just reading, path.join(clientLibDir, '..', '..', file) is quite confusing. Like, where are you trying to go?? Why .. .. ? etc

Copy link
Author

Choose a reason for hiding this comment

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

Now you know how long I needed to understand the problem ;-) But yes you were right ... so i "learned" the context the hard way.

I think your names (even veeery long) are ok and basically understandable. Maybe some more details in the JSDoc of the method would so help to understand what is done there. Then maybe good variable names are enough.

};
if (v && !clientSource) {
clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
Expand Down