-
-
Notifications
You must be signed in to change notification settings - Fork 572
fix(plugin-webpack): try other logger ports #3534
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
Open
DevanceJ
wants to merge
38
commits into
electron:main
Choose a base branch
from
DevanceJ:feat/try-other-ports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
e245cf5
feat/try other logger ports
DevanceJ 9b8e9e9
made some changes as per the code review and added unit tests for Logger
DevanceJ 6d5fd76
transfered server functions to utils and added tests
DevanceJ 7c630c7
removed old logger tests
DevanceJ 1254b70
changed unit tests and got rid of stub
DevanceJ feee8fe
creating a server to make the tests more robust
DevanceJ 32809e8
updated logger class according new param passed to start
DevanceJ a8f6a88
Merge branch 'main' into feat/try-other-ports
BlackHole1 59d6033
Merge branch 'main' into feat/try-other-ports
BlackHole1 7df57bf
bumped dependency version of core/utils to 7.3.1
DevanceJ 39e4961
Merge branch 'main' into feat/try-other-ports
DevanceJ dc4fe2f
Merge branch 'main' into feat/try-other-ports
DevanceJ 012bfe2
Merge branch 'main' into feat/try-other-ports
DevanceJ f5f9056
Merge branch 'main' into feat/try-other-ports
DevanceJ c6bc940
Merge branch 'main' into feat/try-other-ports
DevanceJ c38e6ce
bumped the version to resolve tests
DevanceJ 4aff4c7
Merge branch 'main' into feat/try-other-ports
DevanceJ caae0a2
refactor portoccupied function and tests
DevanceJ 57275fe
Merge branch 'main' into feat/try-other-ports
DevanceJ 7a44cbb
refactors webpack plugin to avoid any ts errors
DevanceJ 547acf9
Merge branch 'main' into feat/try-other-ports
DevanceJ bac9781
Merge branch 'main' into feat/try-other-ports
DevanceJ 86850d9
Merge branch 'main' into feat/try-other-ports
DevanceJ 2f15245
Merge branch 'main' into feat/try-other-ports
erickzhao 825a426
fixes ts errors?
DevanceJ dc8dacd
Merge branch 'main' into feat/try-other-ports
DevanceJ d372fb2
Merge branch 'main' into feat/try-other-ports
DevanceJ ead16ac
Merge branch 'main' into feat/try-other-ports
DevanceJ 5b411b0
Merge branch 'main' into feat/try-other-ports
DevanceJ fb06062
Merge branch 'main' into feat/try-other-ports
DevanceJ 1cd7bc8
Merge branch 'main' into feat/try-other-ports
DevanceJ c4ca3b9
reverted API names, removed loggerPort arg and kept this.port in cons…
DevanceJ eaf5703
Merge branch 'main' into feat/try-other-ports
DevanceJ 63d3e93
Merge branch 'main' into feat/try-other-ports
DevanceJ 4cbf812
Merge branch 'main' into feat/try-other-ports
DevanceJ 809c35a
fix lint
erickzhao 0861161
make syncpack happy
erickzhao 7aa2d5e
Merge branch 'main' into feat/try-other-ports
DevanceJ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './rebuild'; | ||
export * from './electron-version'; | ||
export * from './yarn-or-npm'; | ||
export * from './port'; |
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,41 @@ | ||
import http from 'http'; | ||
|
||
/** | ||
* Check if a port is occupied. | ||
* @returns boolean promise that resolves to true if the port is available, false otherwise. | ||
DevanceJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
export const portOccupied = async (port: number): Promise<void> => { | ||
return new Promise<void>((resolve, reject) => { | ||
const server = http.createServer().listen(port); | ||
server.on('listening', () => { | ||
server.close(); | ||
resolve(); | ||
DevanceJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
server.on('error', (error) => { | ||
if ('code' in error && (error as NodeJS.ErrnoException).code === 'EADDRINUSE') { | ||
reject(new Error(`port: ${port} is occupied`)); | ||
} else { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Find an available port for web UI. | ||
* @returns the port number. | ||
*/ | ||
export const findAvailablePort = async (initialPort: number): Promise<number> => { | ||
const maxPort = initialPort + 10; | ||
|
||
for (let p = initialPort; p <= maxPort; p++) { | ||
try { | ||
await portOccupied(p); | ||
return p; | ||
} catch (_err) { | ||
// Pass | ||
} | ||
} | ||
throw new Error(`Could not find an available port between ${initialPort} and ${maxPort}. Please free up a port and try again.`); | ||
}; |
BlackHole1 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,68 @@ | ||
import net from 'node:net'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { findAvailablePort, portOccupied } from '../src/port'; | ||
|
||
const usePort = (port: number) => { | ||
const server = net.createServer(); | ||
server.on('error', () => { | ||
// pass | ||
}); | ||
server.listen(port); | ||
return () => { | ||
server.close(); | ||
}; | ||
}; | ||
|
||
const usePorts = (port: number, endPort: number) => { | ||
const releases: ReturnType<typeof usePort>[] = []; | ||
for (let i = port; i <= endPort; i++) { | ||
releases.push(usePort(i)); | ||
} | ||
return () => { | ||
releases.forEach((release) => release()); | ||
}; | ||
}; | ||
|
||
describe('Port tests', () => { | ||
describe('portOccupied', () => { | ||
it('should resolve to true if the port is available', async () => { | ||
const port = 49152; | ||
const result = await portOccupied(port); | ||
expect(result).to.not.throw; | ||
}); | ||
it('should reject if the port is occupied', async () => { | ||
const port = 48143; | ||
const releasePort = usePort(port); | ||
try { | ||
await portOccupied(port); | ||
} catch (error) { | ||
expect((error as Error).message).to.equal(`port: ${port} is occupied`); | ||
} finally { | ||
releasePort(); | ||
} | ||
}); | ||
}); | ||
|
||
describe('findAvailablePort', () => { | ||
it('should find an available port', async () => { | ||
const initialPort = 51155; | ||
const port = await findAvailablePort(initialPort); | ||
expect(port).gte(initialPort); | ||
}); | ||
it('should throw an error if no available port is found', async () => { | ||
const initialPort = 53024; | ||
const releasePort = usePorts(initialPort, initialPort + 10); | ||
try { | ||
await findAvailablePort(initialPort); | ||
} catch (error) { | ||
expect((error as Error).message).to.equal( | ||
`Could not find an available port between ${initialPort} and ${initialPort + 10}. Please free up a port and try again.` | ||
); | ||
} finally { | ||
releasePort(); | ||
} | ||
}); | ||
}); | ||
}); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.