-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix port conflicts #24
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26067b7
Update port handling and configuration
truemiller f3708e5
remove package lock
truemiller c4562c9
update yarn dev:backend
truemiller 13600c9
export port range
truemiller 32e0ef4
Fix appConfig bug and add console logs
truemiller 674f116
Merge remote-tracking branch 'origin/main' into fix-port-conflicts
truemiller 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 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,4 +1,6 @@ | ||
const PORT_RANGE = { startPort: 39152, endPort: 65535 }; | ||
const ERROR_ADDRESS_IN_USE = 'EADDRINUSE'; | ||
module.exports = { | ||
PORT_RANGE, | ||
ERROR_ADDRESS_IN_USE, | ||
}; |
This file contains 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 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,56 +1,79 @@ | ||
const net = require('net'); | ||
const { ERROR_ADDRESS_IN_USE } = require('./constants'); | ||
|
||
const portRange = { startPort: 39152, endPort: 65535 }; //only source dynamic and private ports https://www.arubanetworks.com/techdocs/AOS-S/16.10/MRG/YC/content/common%20files/tcp-por-num-ran.htm | ||
|
||
const isPortAvailable = async (port) => { | ||
/** | ||
* Finds an available port within the specified range, excluding specified ports. | ||
* @param {number} startPort - The start of the port range. | ||
* @param {number} endPort - The end of the port range. | ||
* @param {Array<number>} excludePorts - An array of ports to be skipped. | ||
* @returns {Promise<number>} The first available port found within the range that's not excluded. | ||
*/ | ||
function findAvailablePort({ startPort, endPort, excludePorts = [] }) { | ||
return new Promise((resolve, reject) => { | ||
const server = net.createServer(); | ||
let currentPort = startPort; | ||
|
||
server.once('error', (err) => { | ||
if (err.code === ERROR_ADDRESS_IN_USE) { | ||
resolve(false); | ||
} else { | ||
reject(err); | ||
const tryPort = (port) => { | ||
if (excludePorts.includes(port)) { | ||
if (currentPort < endPort) { | ||
tryPort(++currentPort); | ||
} else { | ||
reject( | ||
new Error( | ||
`Unable to find an available port between ${startPort} and ${endPort} excluding specified ports.`, | ||
), | ||
); | ||
} | ||
return; | ||
} | ||
}); | ||
|
||
server.once('listening', () => { | ||
server.close(); | ||
resolve(true); | ||
}); | ||
const server = net.createServer(); | ||
|
||
server.listen(port, 'localhost'); | ||
}); | ||
}; | ||
|
||
const findAvailablePort = async (startPort, endPort) => { | ||
return new Promise((resolve, reject) => { | ||
const server = net.createServer(); | ||
server.listen(port, () => { | ||
server.close(() => { | ||
resolve(port); | ||
}); | ||
}); | ||
|
||
server.on('error', (err) => { | ||
if (err.code === ERROR_ADDRESS_IN_USE) { | ||
if (startPort < endPort) { | ||
findAvailablePort(startPort + 1, endPort) | ||
.then(resolve) | ||
.catch(reject); | ||
server.on('error', (err) => { | ||
if (err.code === ERROR_ADDRESS_IN_USE && currentPort < endPort) { | ||
// Try the next port if the current one is in use or excluded | ||
tryPort(++currentPort); | ||
} else { | ||
reject(new Error('No available ports in the specified range')); | ||
reject( | ||
new Error( | ||
`Unable to find an available port between ${startPort} and ${endPort} excluding specified ports.`, | ||
), | ||
); | ||
} | ||
} else { | ||
reject(err); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
tryPort(currentPort); | ||
}); | ||
} | ||
|
||
server.on('listening', () => { | ||
const port = server.address().port; | ||
/** | ||
* Checks if a port is available. | ||
* @param {number} port - The port to check. | ||
* @returns {Promise<boolean>} Whether the port is available. | ||
*/ | ||
function isPortAvailable(port) { | ||
return new Promise((resolve) => { | ||
const server = net.createServer(); | ||
|
||
server.listen(port, () => { | ||
server.close(() => { | ||
resolve(port); | ||
resolve(true); | ||
}); | ||
}); | ||
|
||
server.listen(startPort, 'localhost'); | ||
server.on('error', () => { | ||
resolve(false); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
module.exports = { isPortAvailable, portRange, findAvailablePort }; | ||
module.exports = { | ||
findAvailablePort, | ||
isPortAvailable, | ||
}; |
Oops, something went wrong.
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.
is it possible to create a common function as they seem to be repeated?
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.
will resolve later today