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

feat/refactor(server): move socket handling into server.listen #2061

Merged
merged 7 commits into from
Aug 22, 2019

Conversation

knagaitsev
Copy link
Collaborator

@knagaitsev knagaitsev commented Jun 24, 2019

  • This is a bugfix
  • This is a feature
  • This is a code refactor
  • This is a test update
  • This is a docs update
  • This is a metadata update

For Bugs and Features; did you add new tests?

Yes

Motivation / Use-Case

This is the socket option handling portion of: #2028. The goal is to move socket handling out of the CLI and into the API listen method.

Breaking Changes

API users will now get different behavior when they put a socket path in place of port in the server.listen(port, hostname, fn) method.

Additional Info

This should be merged before I start work on the findPort portion of #2028

@codecov
Copy link

codecov bot commented Jun 24, 2019

Codecov Report

Merging #2061 into next will increase coverage by 0.14%.
The diff coverage is 97.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##             next    #2061      +/-   ##
==========================================
+ Coverage   96.05%   96.19%   +0.14%     
==========================================
  Files          34       35       +1     
  Lines        1191     1236      +45     
  Branches      346      349       +3     
==========================================
+ Hits         1144     1189      +45     
  Misses         46       46              
  Partials        1        1
Impacted Files Coverage Δ
lib/Server.js 97.21% <100%> (+0.09%) ⬆️
lib/utils/startUnixSocket.js 96.55% <96.55%> (ø)
lib/utils/status.js 95% <0%> (+5%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf6ea3f...8577191. Read the comment docs.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Good job, just question

lib/Server.js Outdated
userCallback(err);
});
}
});
Copy link
Member

Choose a reason for hiding this comment

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

Can we refactor this on utils? Maybe multiple utils

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll try

lib/Server.js Outdated
socket,
setupAndListenCallback,
userCallback
);
Copy link
Member

Choose a reason for hiding this comment

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

Let rewrite this in style (pseudo code):

startUnixSocket(
  this.listeningApp,
  socket,
  (error) => { 
    // Here logic for callback
  }
);

Idea is not pass a lot of argument in util, just use closure for this

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@evilebottnawi I was passing in 2 callbacks because I didn't want options.onListening to be called if an error occurred before the server starts listening. But maybe that is not important. If you think it is important, maybe we could have:

startUnixSocket(
  this.listeningApp,
  socket,
  (error, startedListening) => { 
    if (startedListening) {
      this.options.onListening(this);
    }
    // other functions that always get called, regardless of if server started listening
  }
);

@hiroppy
Copy link
Member

hiroppy commented Jun 25, 2019

This pr has breaking changes so I think we should change from master to next.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

hm, after investigation, looks like yes, it is breaking change, yes we should use next branch for this, anyway thanks for the PR

@knagaitsev
Copy link
Collaborator Author

Should I switch it over to next branch?

@alexander-akait
Copy link
Member

Yes

@knagaitsev knagaitsev changed the base branch from master to next June 27, 2019 16:40
@hiroppy
Copy link
Member

hiroppy commented Jun 27, 2019

@Loonride Could you rebase?

@hiroppy
Copy link
Member

hiroppy commented Jul 3, 2019

@Loonride Do you need commits of the latest master before merging? If you need, I'll rebase the next branch from master.

@knagaitsev
Copy link
Collaborator Author

@hiroppy For this PR the latest master commits should not affect it

@hiroppy
Copy link
Member

hiroppy commented Jul 3, 2019

After all, I'll rebase from the master to the next branch every time. master...next
I just want to ask if you want to check if these changes can run on the latest master commits branch before merging. However, if you don't need the latest master commits, it's ok.

});
};

fs.access(socket, fs.constants.F_OK, (e) => {
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to use a callback. Just use promisify.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yes, thanks!

setTimeout(() => {
expect(userCallback).toBeCalledTimes(1);
done();
}, 10000);
Copy link
Member

Choose a reason for hiding this comment

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

I think this value is big but do we need 10000?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think 3000 should be enough.

@hiroppy
Copy link
Member

hiroppy commented Aug 10, 2019

Need to rebase.

setupCallback();
userCallback(err);
onListeningCallback();
};
Copy link
Member

Choose a reason for hiding this comment

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

I think we should refactor this in future, looks very misleading

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Really those 3 functions can all be in fullCallback, since they are only called by fullCallback. Originally I was doing some other things with these individual functions to avoid a few breaking changes and stay organized, but do you think just putting all their functionality in that one callback will be cleaner?

Copy link
Member

Choose a reason for hiding this comment

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

@Loonride i think we should allow userCallback to be async, also ehre good questions should userCallback will be called before or after onListeningCallback, potentially user callback should be called after all operation, it is allow do something with server (or not with server) when webpack-dev-server starts, for example for other web server, (for example if you use php project, you should use this callback to run php dev server)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i think we should allow userCallback to be async

Maybe we should do this in a separate PR?

potentially user callback should be called after all operation

So this is a good order the way I have above, because userCallback is after setupCallback, right?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should do this in a separate PR?

Yes, just thoughts

So this is a good order the way I have above, because userCallback is after setupCallback, right?

oh, it is interesting, i think userCallback should be after all callbacks, because i want to get socket information after starts server

Copy link
Member

Choose a reason for hiding this comment

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

/cc @hiroppy what do you think about this too

Copy link
Member

Choose a reason for hiding this comment

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

I want to change onListening(err, server). In fact, this format is correct as Node.js theory.

Copy link
Member

Choose a reason for hiding this comment

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

👍 agree

Copy link
Collaborator Author

@knagaitsev knagaitsev Aug 16, 2019

Choose a reason for hiding this comment

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

So can we get this merged, and then I'll do 2 PRs:

  1. async userCallback
  2. onListening(err, server)

But @evilebottnawi can you clarify the async one? A user can already do this without problems:

server.listen(port, host, async (err) => {
  await something();
  // ...
});

And I think there are no things before userCallback that are asynchronous. Maybe you mean we should do:

setupCallback();
await userCallback(err);
onListeningCallback();

But the only thing this would do is delay onListeningCallback until a potentially async userCallback is complete.

Is this what you meant?

Copy link
Member

Choose a reason for hiding this comment

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

But @evilebottnawi can you clarify the async one? A user can already do this without problems:

server.listen(port, host, async (err) => {
  await something();
  // ...
});

Yep, but it is require to create own util for running webpack-dev-server, in some cases it is unnecessary, because you want run one simple task when webpack-dev-server started

But the only thing this would do is delay onListeningCallback until a potentially async userCallback is complete.

Is this what you meant?

It is expected and can be used for some features

@knagaitsev knagaitsev added gsoc Google Summer of Code scope: cli labels Aug 13, 2019
@alexander-akait alexander-akait merged commit 7ebb26a into webpack:next Aug 22, 2019
@alexander-akait
Copy link
Member

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gsoc Google Summer of Code pr: next scope: cli
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants