-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat/refactor(server): move socket handling into server.listen #2061
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22dfb29
refactor(server): move socket handling into server.listen
knagaitsev 4e52766
refactor(server): added startUnixSocket helper
knagaitsev 30c2d4f
test(server): added unix socket helper tests
knagaitsev bbdd01e
test(server): fixed unix socket helper close method
knagaitsev 8da9a8c
feat(server): made startUnixSocket take one callback
knagaitsev b352448
refactor(server): use promisify to make unix socket async await
knagaitsev 8577191
test(server): reduce time to wait for unix socket listening
knagaitsev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const net = require('net'); | ||
const { promisify } = require('util'); | ||
|
||
const accessAsync = promisify(fs.access); | ||
|
||
async function startUnixSocket(listeningApp, socket, cb) { | ||
const chmodSocket = (done) => { | ||
// chmod 666 (rw rw rw) - octal | ||
const READ_WRITE = 438; | ||
fs.chmod(socket, READ_WRITE, done); | ||
}; | ||
|
||
const startSocket = () => { | ||
listeningApp.on('error', (err) => { | ||
cb(err); | ||
}); | ||
|
||
// 511 is the default value for the server.listen backlog parameter | ||
// https://nodejs.org/api/net.html#net_server_listen | ||
listeningApp.listen(socket, 511, (err) => { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
chmodSocket(cb); | ||
} | ||
}); | ||
}; | ||
|
||
try { | ||
await accessAsync(socket, fs.constants.F_OK); | ||
} catch (e) { | ||
// file does not exist | ||
startSocket(); | ||
return; | ||
} | ||
|
||
// file exists | ||
|
||
const clientSocket = new net.Socket(); | ||
|
||
clientSocket.on('error', (err) => { | ||
if (err.code === 'ECONNREFUSED' || err.code === 'ENOTSOCK') { | ||
// No other server listening on this socket so it can be safely removed | ||
fs.unlinkSync(socket); | ||
|
||
startSocket(); | ||
} | ||
}); | ||
|
||
clientSocket.connect({ path: socket }, () => { | ||
// if a client socket successfully connects to the given socket path, | ||
// it means that the socket is in use | ||
const err = new Error('This socket is already used'); | ||
clientSocket.destroy(); | ||
cb(err); | ||
}); | ||
} | ||
|
||
module.exports = startUnixSocket; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
const http = require('http'); | ||
|
||
const TestUnixSocket = class TestUnixSocket { | ||
constructor() { | ||
this.server = http.createServer(); | ||
this.sockets = new Set(); | ||
this.server.on('connection', (socket) => { | ||
this.sockets.add(socket); | ||
socket.on('close', () => { | ||
this.sockets.delete(socket); | ||
}); | ||
}); | ||
} | ||
|
||
close(done) { | ||
if (this.server.listening) { | ||
// get rid of connected sockets | ||
for (const socket of this.sockets.values()) { | ||
socket.destroy(); | ||
} | ||
this.server.close(done); | ||
} else { | ||
done(); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = TestUnixSocket; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,71 @@ | ||
'use strict'; | ||
|
||
const { unlink } = require('fs'); | ||
const { join } = require('path'); | ||
const testServer = require('../helpers/test-server'); | ||
const config = require('../fixtures/simple-config/webpack.config'); | ||
const port = require('../ports-map')['onListening-option']; | ||
const { skipTestOnWindows } = require('../helpers/conditional-test'); | ||
|
||
describe('onListening option', () => { | ||
let onListeningIsRunning = false; | ||
|
||
beforeAll((done) => { | ||
testServer.start( | ||
config, | ||
{ | ||
onListening: (devServer) => { | ||
if (!devServer) { | ||
throw new Error('webpack-dev-server is not defined'); | ||
} | ||
|
||
onListeningIsRunning = true; | ||
describe('with host and port', () => { | ||
let onListeningIsRunning = false; | ||
|
||
beforeAll((done) => { | ||
testServer.start( | ||
config, | ||
{ | ||
onListening: (devServer) => { | ||
if (!devServer) { | ||
throw new Error('webpack-dev-server is not defined'); | ||
} | ||
|
||
onListeningIsRunning = true; | ||
}, | ||
port, | ||
}, | ||
port, | ||
}, | ||
done | ||
); | ||
done | ||
); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
it('should run onListening callback', () => { | ||
expect(onListeningIsRunning).toBe(true); | ||
}); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
describe('with Unix socket', () => { | ||
if (skipTestOnWindows('Unix sockets are not supported on Windows')) { | ||
return; | ||
} | ||
|
||
const socketPath = join('.', 'onListening.webpack.sock'); | ||
let onListeningIsRunning = false; | ||
|
||
beforeAll((done) => { | ||
testServer.start( | ||
config, | ||
{ | ||
onListening: (devServer) => { | ||
if (!devServer) { | ||
throw new Error('webpack-dev-server is not defined'); | ||
} | ||
|
||
onListeningIsRunning = true; | ||
}, | ||
socket: socketPath, | ||
}, | ||
done | ||
); | ||
}); | ||
|
||
afterAll(testServer.close); | ||
|
||
it('should run onListening callback', (done) => { | ||
expect(onListeningIsRunning).toBe(true); | ||
|
||
it('should runs onListening callback', () => { | ||
expect(onListeningIsRunning).toBe(true); | ||
unlink(socketPath, done); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we should refactor this in future, looks very misleading
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.
Really those 3 functions can all be in
fullCallback
, since they are only called byfullCallback
. 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?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.
@Loonride i think we should allow
userCallback
to be async, also ehre good questions shoulduserCallback
will be called before or afteronListeningCallback
, 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)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.
Maybe we should do this in a separate PR?
So this is a good order the way I have above, because
userCallback
is aftersetupCallback
, right?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.
Yes, just thoughts
oh, it is interesting, i think
userCallback
should be after all callbacks, because i want to get socket information after starts serverThere 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.
/cc @hiroppy what do you think about this too
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 want to change
onListening(err, server)
. In fact, this format is correct as Node.js theory.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.
👍 agree
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.
So can we get this merged, and then I'll do 2 PRs:
userCallback
onListening(err, server)
But @evilebottnawi can you clarify the async one? A user can already do this without problems:
And I think there are no things before
userCallback
that are asynchronous. Maybe you mean we should do:But the only thing this would do is delay
onListeningCallback
until a potentially asyncuserCallback
is complete.Is this what you meant?
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.
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
It is expected and can be used for some features