From e245cf54e40fead960e8c157a6e11197cbd2f4b9 Mon Sep 17 00:00:00 2001 From: devance Date: Thu, 14 Mar 2024 18:27:04 +0530 Subject: [PATCH 01/15] feat/try other logger ports --- packages/plugin/webpack/src/WebpackPlugin.ts | 4 +- packages/utils/web-multi-logger/src/Logger.ts | 39 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index d41b6021ab..a8da608ca1 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -570,7 +570,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); const logger = new Logger(this.loggerPort); this.loggers.push(logger); - await logger.start(); + const currLoggerPort = await logger.start(); return { tasks: [ @@ -587,7 +587,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); title: 'Launching dev servers for renderer process code', task: async (_, task) => { await this.launchRendererDevServers(logger); - task.output = `Output Available: ${chalk.cyan(`http://localhost:${this.loggerPort}`)}\n`; + task.output = `Output Available: ${chalk.cyan(`http://localhost:${currLoggerPort}`)}\n`; }, options: { persistentOutput: true, diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index f9633e2759..b665835ba3 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -33,6 +33,36 @@ export default class Logger { // I assume this endpoint is just a no-op needed for some reason. }); } + /** + * Find an available port between 9000 and 9009 for web UI. + * @returns the port number. + */ + private async findAvailablePort(): Promise { + const maxPortAttempts = 10; // Maximum attempts to find an available port + let port = this.port; + let attempts = 0; + + while (attempts < maxPortAttempts) { + try { + await new Promise((resolve, reject) => { + const server = http.createServer(); + server.unref(); // Allows the program to exit if this is the only active server + server.on('error', reject); + server.listen(port, () => { + server.close(() => { + resolve(); + }); + }); + }); + return port; + } catch { + // Port is in use, try the next one + port++; + attempts++; + } + } + throw new Error('Could not find an available port between 9000 and 9009. Please free up a port and try again.'); + } /** * Creates a new tab with the given name, the name should be human readable @@ -49,12 +79,11 @@ export default class Logger { * * @returns the port number */ - start(): Promise { - return new Promise((resolve) => { - this.server = this.app.listen(this.port, () => resolve(this.port)); - }); + async start(): Promise { + this.port = await this.findAvailablePort(); + this.server = this.app.listen(this.port); + return this.port; } - /** * Stop the HTTP server hosting the web UI */ From 9b8e9e9132848c1bf209286a1b70594e7595a771 Mon Sep 17 00:00:00 2001 From: devance Date: Fri, 15 Mar 2024 12:52:54 +0530 Subject: [PATCH 02/15] made some changes as per the code review and added unit tests for Logger --- packages/utils/web-multi-logger/src/Logger.ts | 54 +++++++++++-------- .../web-multi-logger/test/Logger_spec.ts | 28 ++++++++++ 2 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 packages/utils/web-multi-logger/test/Logger_spec.ts diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index b665835ba3..cdb1df11f0 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -34,34 +34,42 @@ export default class Logger { }); } /** - * Find an available port between 9000 and 9009 for web UI. + * Check if a port is occupied. + * @returns boolean promise that resolves to true if the port is available, false otherwise. + */ + private static async portOccupied(port: number): Promise { + return new Promise((resolve, reject) => { + const server = http.createServer().listen(port); + server.on('listening', () => { + server.close(); + resolve(); + }); + + 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. */ - private async findAvailablePort(): Promise { - const maxPortAttempts = 10; // Maximum attempts to find an available port - let port = this.port; - let attempts = 0; + private static async findAvailablePort(initialPort: number): Promise { + const maxPort = initialPort + 10; - while (attempts < maxPortAttempts) { + for (let p = initialPort; p <= maxPort; p++) { try { - await new Promise((resolve, reject) => { - const server = http.createServer(); - server.unref(); // Allows the program to exit if this is the only active server - server.on('error', reject); - server.listen(port, () => { - server.close(() => { - resolve(); - }); - }); - }); - return port; - } catch { - // Port is in use, try the next one - port++; - attempts++; + await Logger.portOccupied(p); + return p; + } catch (_err) { + // Pass } } - throw new Error('Could not find an available port between 9000 and 9009. Please free up a port and try again.'); + throw new Error(`Could not find an available port between ${initialPort} and ${maxPort}. Please free up a port and try again.`); } /** @@ -80,7 +88,7 @@ export default class Logger { * @returns the port number */ async start(): Promise { - this.port = await this.findAvailablePort(); + this.port = await Logger.findAvailablePort(this.port); this.server = this.app.listen(this.port); return this.port; } diff --git a/packages/utils/web-multi-logger/test/Logger_spec.ts b/packages/utils/web-multi-logger/test/Logger_spec.ts new file mode 100644 index 0000000000..6626df0bfa --- /dev/null +++ b/packages/utils/web-multi-logger/test/Logger_spec.ts @@ -0,0 +1,28 @@ +import { expect } from 'chai'; + +import Logger from '../src/Logger'; +import Tab from '../src/Tab'; + +describe('Logger', () => { + let logger: Logger; + + beforeEach(() => { + logger = new Logger(); + }); + + it('should create a tab', () => { + const tabName = 'Test Tab'; + const tab = logger.createTab(tabName); + expect(tab).to.be.an.instanceOf(Tab); + expect(tab.name).to.equal(tabName); + }); + + it('should start the server', async () => { + const port = await logger.start(); // This will test the findAvailablePort method + expect(port).to.be.a('number'); + }); + + it('should stop the server', () => { + logger.stop(); + }); +}); From 6d5fd766ffc9f165c78de827d507f498ab14efa7 Mon Sep 17 00:00:00 2001 From: devance Date: Sat, 16 Mar 2024 00:28:23 +0530 Subject: [PATCH 03/15] transfered server functions to utils and added tests --- packages/utils/core-utils/src/index.ts | 1 + packages/utils/core-utils/src/port.ts | 39 +++++++++++++++++ packages/utils/core-utils/test/port_spec.ts | 35 ++++++++++++++++ packages/utils/web-multi-logger/package.json | 1 + packages/utils/web-multi-logger/src/Logger.ts | 42 +------------------ 5 files changed, 78 insertions(+), 40 deletions(-) create mode 100644 packages/utils/core-utils/src/port.ts create mode 100644 packages/utils/core-utils/test/port_spec.ts diff --git a/packages/utils/core-utils/src/index.ts b/packages/utils/core-utils/src/index.ts index 2b4f1876ef..40bf4b806a 100644 --- a/packages/utils/core-utils/src/index.ts +++ b/packages/utils/core-utils/src/index.ts @@ -1,3 +1,4 @@ export * from './rebuild'; export * from './electron-version'; export * from './yarn-or-npm'; +export * from './port'; diff --git a/packages/utils/core-utils/src/port.ts b/packages/utils/core-utils/src/port.ts new file mode 100644 index 0000000000..36b2db2804 --- /dev/null +++ b/packages/utils/core-utils/src/port.ts @@ -0,0 +1,39 @@ +import * as http from 'http'; +/** + * Check if a port is occupied. + * @returns boolean promise that resolves to true if the port is available, false otherwise. + */ +export const portOccupied = async (port: number): Promise => { + return new Promise((resolve, reject) => { + const server = http.createServer().listen(port); + server.on('listening', () => { + server.close(); + resolve(); + }); + + 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, portOccupied: (port: number) => Promise): Promise => { + 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.`); +}; diff --git a/packages/utils/core-utils/test/port_spec.ts b/packages/utils/core-utils/test/port_spec.ts new file mode 100644 index 0000000000..8f7874b965 --- /dev/null +++ b/packages/utils/core-utils/test/port_spec.ts @@ -0,0 +1,35 @@ +import { expect } from 'chai'; +import { stub } from 'sinon'; + +import { findAvailablePort } from '../src/port'; + +describe('Port tests', () => { + describe('Find available port', () => { + it('should find an available port', async () => { + const initialPort = 9000; + // reject initial port, accept 9001 + const portOccupiedStub = stub(); + portOccupiedStub.onFirstCall().throws(new Error('port: 9000 is occupied')); + portOccupiedStub.onSecondCall().returns(Promise.resolve()); + + const port = await findAvailablePort(initialPort, portOccupiedStub); + expect(port).to.equal(9001); + expect(portOccupiedStub.callCount).to.equal(2); + expect(portOccupiedStub.firstCall.args[0]).to.equal(9000); + expect(portOccupiedStub.secondCall.args[0]).to.equal(9001); + }); + it('should throw an error if no port is available', async () => { + const initialPort = 9000; + const portOccupiedStub = stub(); + for (let i = 0; i < 11; i++) { + portOccupiedStub.onCall(i).throws(new Error('port: ' + (9000 + i) + ' is occupied')); + } + try { + await findAvailablePort(initialPort, portOccupiedStub); + expect.fail('Should have thrown an error'); + } catch (err) { + expect((err as Error).message).to.equal('Could not find an available port between 9000 and 9010. Please free up a port and try again.'); + } + }); + }); +}); diff --git a/packages/utils/web-multi-logger/package.json b/packages/utils/web-multi-logger/package.json index 34829f66a3..da9d67a151 100644 --- a/packages/utils/web-multi-logger/package.json +++ b/packages/utils/web-multi-logger/package.json @@ -8,6 +8,7 @@ "main": "dist/Logger.js", "typings": "dist/Logger.d.ts", "dependencies": { + "@electron-forge/core-utils": "7.3.0", "express": "^4.17.1", "express-ws": "^5.0.2", "xterm": "^4.9.0", diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index cdb1df11f0..f65cb08f6d 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -1,6 +1,7 @@ import http from 'http'; import path from 'path'; +import { findAvailablePort, portOccupied } from '@electron-forge/core-utils'; import express from 'express'; import ews from 'express-ws'; @@ -33,45 +34,6 @@ export default class Logger { // I assume this endpoint is just a no-op needed for some reason. }); } - /** - * Check if a port is occupied. - * @returns boolean promise that resolves to true if the port is available, false otherwise. - */ - private static async portOccupied(port: number): Promise { - return new Promise((resolve, reject) => { - const server = http.createServer().listen(port); - server.on('listening', () => { - server.close(); - resolve(); - }); - - 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. - */ - private static async findAvailablePort(initialPort: number): Promise { - const maxPort = initialPort + 10; - - for (let p = initialPort; p <= maxPort; p++) { - try { - await Logger.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.`); - } - /** * Creates a new tab with the given name, the name should be human readable * it will be used as the tab title in the front end. @@ -88,7 +50,7 @@ export default class Logger { * @returns the port number */ async start(): Promise { - this.port = await Logger.findAvailablePort(this.port); + this.port = await findAvailablePort(this.port, portOccupied); this.server = this.app.listen(this.port); return this.port; } From 7c630c74bf30a909d44e9ff0abb769a496d4738c Mon Sep 17 00:00:00 2001 From: devance Date: Sat, 16 Mar 2024 00:47:32 +0530 Subject: [PATCH 04/15] removed old logger tests --- .../web-multi-logger/test/Logger_spec.ts | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 packages/utils/web-multi-logger/test/Logger_spec.ts diff --git a/packages/utils/web-multi-logger/test/Logger_spec.ts b/packages/utils/web-multi-logger/test/Logger_spec.ts deleted file mode 100644 index 6626df0bfa..0000000000 --- a/packages/utils/web-multi-logger/test/Logger_spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { expect } from 'chai'; - -import Logger from '../src/Logger'; -import Tab from '../src/Tab'; - -describe('Logger', () => { - let logger: Logger; - - beforeEach(() => { - logger = new Logger(); - }); - - it('should create a tab', () => { - const tabName = 'Test Tab'; - const tab = logger.createTab(tabName); - expect(tab).to.be.an.instanceOf(Tab); - expect(tab.name).to.equal(tabName); - }); - - it('should start the server', async () => { - const port = await logger.start(); // This will test the findAvailablePort method - expect(port).to.be.a('number'); - }); - - it('should stop the server', () => { - logger.stop(); - }); -}); From 1254b701fe50bdc3c011c2c502cac41ccbc11c96 Mon Sep 17 00:00:00 2001 From: devance Date: Mon, 18 Mar 2024 16:31:01 +0530 Subject: [PATCH 05/15] changed unit tests and got rid of stub --- packages/utils/core-utils/src/port.ts | 2 +- packages/utils/core-utils/test/port_spec.ts | 56 ++++++++++--------- packages/utils/web-multi-logger/src/Logger.ts | 4 +- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/utils/core-utils/src/port.ts b/packages/utils/core-utils/src/port.ts index 36b2db2804..cef19fb9b0 100644 --- a/packages/utils/core-utils/src/port.ts +++ b/packages/utils/core-utils/src/port.ts @@ -24,7 +24,7 @@ export const portOccupied = async (port: number): Promise => { * Find an available port for web UI. * @returns the port number. */ -export const findAvailablePort = async (initialPort: number, portOccupied: (port: number) => Promise): Promise => { +export const findAvailablePort = async (initialPort: number): Promise => { const maxPort = initialPort + 10; for (let p = initialPort; p <= maxPort; p++) { diff --git a/packages/utils/core-utils/test/port_spec.ts b/packages/utils/core-utils/test/port_spec.ts index 8f7874b965..ffe2f7cb3f 100644 --- a/packages/utils/core-utils/test/port_spec.ts +++ b/packages/utils/core-utils/test/port_spec.ts @@ -1,34 +1,40 @@ import { expect } from 'chai'; -import { stub } from 'sinon'; - -import { findAvailablePort } from '../src/port'; +import { findAvailablePort, portOccupied } from '../src/port'; describe('Port tests', () => { - describe('Find available port', () => { - it('should find an available port', async () => { - const initialPort = 9000; - // reject initial port, accept 9001 - const portOccupiedStub = stub(); - portOccupiedStub.onFirstCall().throws(new Error('port: 9000 is occupied')); - portOccupiedStub.onSecondCall().returns(Promise.resolve()); - - const port = await findAvailablePort(initialPort, portOccupiedStub); - expect(port).to.equal(9001); - expect(portOccupiedStub.callCount).to.equal(2); - expect(portOccupiedStub.firstCall.args[0]).to.equal(9000); - expect(portOccupiedStub.secondCall.args[0]).to.equal(9001); + 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 throw an error if no port is available', async () => { - const initialPort = 9000; - const portOccupiedStub = stub(); - for (let i = 0; i < 11; i++) { - portOccupiedStub.onCall(i).throws(new Error('port: ' + (9000 + i) + ' is occupied')); + + it('should reject if the port is occupied', async () => { + const port = 3000; + try { + await portOccupied(port); + } catch (error) { + expect((error as Error).message).to.equal(`port: ${port} is occupied`); } + }); + }); + + describe('findAvailablePort', () => { + it('should find an available port', async () => { + const initialPort = 49152; + const port = await findAvailablePort(initialPort); + expect(port).to.be.a('number'); + expect(port).to.be.within(initialPort, initialPort + 10); + }); + + it('should throw an error if no available port is found', async () => { + const initialPort = 3000; try { - await findAvailablePort(initialPort, portOccupiedStub); - expect.fail('Should have thrown an error'); - } catch (err) { - expect((err as Error).message).to.equal('Could not find an available port between 9000 and 9010. Please free up a port and try again.'); + 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.` + ); } }); }); diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index f65cb08f6d..c2a1297808 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -1,7 +1,7 @@ import http from 'http'; import path from 'path'; -import { findAvailablePort, portOccupied } from '@electron-forge/core-utils'; +import { findAvailablePort } from '@electron-forge/core-utils'; import express from 'express'; import ews from 'express-ws'; @@ -50,7 +50,7 @@ export default class Logger { * @returns the port number */ async start(): Promise { - this.port = await findAvailablePort(this.port, portOccupied); + this.port = await findAvailablePort(this.port); this.server = this.app.listen(this.port); return this.port; } From feee8fe4920afd8f40e6b04081eaa2f9cc36b1f8 Mon Sep 17 00:00:00 2001 From: devance Date: Tue, 19 Mar 2024 08:47:48 +0530 Subject: [PATCH 06/15] creating a server to make the tests more robust --- packages/utils/core-utils/src/port.ts | 4 +- packages/utils/core-utils/test/port_spec.ts | 41 +++++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/packages/utils/core-utils/src/port.ts b/packages/utils/core-utils/src/port.ts index cef19fb9b0..3ed3a797ec 100644 --- a/packages/utils/core-utils/src/port.ts +++ b/packages/utils/core-utils/src/port.ts @@ -1,4 +1,5 @@ -import * as http from 'http'; +import http from 'http'; + /** * Check if a port is occupied. * @returns boolean promise that resolves to true if the port is available, false otherwise. @@ -20,6 +21,7 @@ export const portOccupied = async (port: number): Promise => { }); }); }; + /** * Find an available port for web UI. * @returns the port number. diff --git a/packages/utils/core-utils/test/port_spec.ts b/packages/utils/core-utils/test/port_spec.ts index ffe2f7cb3f..e200471d51 100644 --- a/packages/utils/core-utils/test/port_spec.ts +++ b/packages/utils/core-utils/test/port_spec.ts @@ -1,6 +1,30 @@ +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[] = []; + 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 () => { @@ -8,33 +32,36 @@ describe('Port tests', () => { const result = await portOccupied(port); expect(result).to.not.throw; }); - it('should reject if the port is occupied', async () => { - const port = 3000; + 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 = 49152; + const initialPort = 51155; const port = await findAvailablePort(initialPort); - expect(port).to.be.a('number'); - expect(port).to.be.within(initialPort, initialPort + 10); + expect(port).gte(initialPort); }); - it('should throw an error if no available port is found', async () => { - const initialPort = 3000; + 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(); } }); }); From 32809e8c8c489a9866aa459040c6680fb2c6dd4a Mon Sep 17 00:00:00 2001 From: devance Date: Tue, 19 Mar 2024 17:07:30 +0530 Subject: [PATCH 07/15] updated logger class according new param passed to start --- packages/plugin/webpack/src/WebpackPlugin.ts | 6 +++--- packages/utils/web-multi-logger/src/Logger.ts | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index a8da608ca1..5f11bef879 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -568,9 +568,9 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); await fs.remove(this.baseDir); - const logger = new Logger(this.loggerPort); + const logger = new Logger(); this.loggers.push(logger); - const currLoggerPort = await logger.start(); + this.loggerPort = await logger.start(this.loggerPort); return { tasks: [ @@ -587,7 +587,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); title: 'Launching dev servers for renderer process code', task: async (_, task) => { await this.launchRendererDevServers(logger); - task.output = `Output Available: ${chalk.cyan(`http://localhost:${currLoggerPort}`)}\n`; + task.output = `Output Available: ${chalk.cyan(`http://localhost:${this.loggerPort}`)}\n`; }, options: { persistentOutput: true, diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index c2a1297808..5b24a156d1 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -49,11 +49,12 @@ export default class Logger { * * @returns the port number */ - async start(): Promise { - this.port = await findAvailablePort(this.port); - this.server = this.app.listen(this.port); - return this.port; + async start(loggerPort: number): Promise { + loggerPort = await findAvailablePort(loggerPort); + this.server = this.app.listen(loggerPort); + return loggerPort; } + /** * Stop the HTTP server hosting the web UI */ From 7df57bfe3b014a364327d16c7e9ff77cdff52361 Mon Sep 17 00:00:00 2001 From: devance Date: Thu, 21 Mar 2024 08:59:17 +0530 Subject: [PATCH 08/15] bumped dependency version of core/utils to 7.3.1 --- packages/utils/web-multi-logger/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/web-multi-logger/package.json b/packages/utils/web-multi-logger/package.json index b355c9b6f1..cce23cbf6f 100644 --- a/packages/utils/web-multi-logger/package.json +++ b/packages/utils/web-multi-logger/package.json @@ -8,7 +8,7 @@ "main": "dist/Logger.js", "typings": "dist/Logger.d.ts", "dependencies": { - "@electron-forge/core-utils": "7.3.0", + "@electron-forge/core-utils": "7.3.1", "express": "^4.17.1", "express-ws": "^5.0.2", "xterm": "^4.9.0", From c38e6ced92107cc7bb39702033a5deb57713bb94 Mon Sep 17 00:00:00 2001 From: devance Date: Tue, 16 Apr 2024 02:20:28 +0530 Subject: [PATCH 09/15] bumped the version to resolve tests --- packages/utils/web-multi-logger/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/web-multi-logger/package.json b/packages/utils/web-multi-logger/package.json index 57df392498..22c25ab90d 100644 --- a/packages/utils/web-multi-logger/package.json +++ b/packages/utils/web-multi-logger/package.json @@ -8,7 +8,7 @@ "main": "dist/Logger.js", "typings": "dist/Logger.d.ts", "dependencies": { - "@electron-forge/core-utils": "7.3.1", + "@electron-forge/core-utils": "7.4.0", "express": "^4.17.1", "express-ws": "^5.0.2", "xterm": "^4.9.0", From caae0a2ffb3ca8cc783d6499f1036f5ee9c38d8e Mon Sep 17 00:00:00 2001 From: devance Date: Sat, 20 Apr 2024 08:14:42 +0530 Subject: [PATCH 10/15] refactor portoccupied function and tests --- packages/utils/core-utils/src/port.ts | 16 +++++++--------- packages/utils/core-utils/test/port_spec.ts | 2 +- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/utils/core-utils/src/port.ts b/packages/utils/core-utils/src/port.ts index 3ed3a797ec..2e6e2abad7 100644 --- a/packages/utils/core-utils/src/port.ts +++ b/packages/utils/core-utils/src/port.ts @@ -4,20 +4,18 @@ import http from 'http'; * Check if a port is occupied. * @returns boolean promise that resolves to true if the port is available, false otherwise. */ -export const portOccupied = async (port: number): Promise => { - return new Promise((resolve, reject) => { +export const portOccupied = async (port: number): Promise => { + return new Promise((resolve, reject) => { const server = http.createServer().listen(port); server.on('listening', () => { server.close(); - resolve(); + server.on('close', () => { + resolve(true); + }); }); - server.on('error', (error) => { - if ('code' in error && (error as NodeJS.ErrnoException).code === 'EADDRINUSE') { - reject(new Error(`port: ${port} is occupied`)); - } else { - reject(error); - } + server.on('error', (_err) => { + reject(false); }); }); }; diff --git a/packages/utils/core-utils/test/port_spec.ts b/packages/utils/core-utils/test/port_spec.ts index e200471d51..67285544be 100644 --- a/packages/utils/core-utils/test/port_spec.ts +++ b/packages/utils/core-utils/test/port_spec.ts @@ -38,7 +38,7 @@ describe('Port tests', () => { try { await portOccupied(port); } catch (error) { - expect((error as Error).message).to.equal(`port: ${port} is occupied`); + expect(error).to.equal(false); } finally { releasePort(); } From 7a44cbbd330212e74a2a705ee9a70038b0610d1b Mon Sep 17 00:00:00 2001 From: devance Date: Tue, 23 Apr 2024 21:57:25 +0530 Subject: [PATCH 11/15] refactors webpack plugin to avoid any ts errors --- packages/plugin/webpack/src/WebpackPlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index b529198b44..1bab25b5d6 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -571,7 +571,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); const logger = new Logger(); this.loggers.push(logger); - this.loggerPort = await logger.start(this.loggerPort); + this.loggerPort = await logger.start(this.loggerPort as number); return { tasks: [ From 825a426ef616b216d3404d464dbe0fced5573ebb Mon Sep 17 00:00:00 2001 From: devance Date: Wed, 29 May 2024 13:12:51 +0530 Subject: [PATCH 12/15] fixes ts errors? --- packages/plugin/webpack/src/WebpackPlugin.ts | 4 ++-- packages/utils/web-multi-logger/src/Logger.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 1bab25b5d6..344d86ad5a 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -101,7 +101,7 @@ export default class WebpackPlugin extends PluginBase { this.servers = []; for (const logger of this.loggers) { d('stopping logger'); - logger.stop(); + logger.stopPort(); } this.loggers = []; } @@ -571,7 +571,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); const logger = new Logger(); this.loggers.push(logger); - this.loggerPort = await logger.start(this.loggerPort as number); + this.loggerPort = await logger.startPort(this.loggerPort); return { tasks: [ diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index 5b24a156d1..126f4dbe50 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -49,7 +49,7 @@ export default class Logger { * * @returns the port number */ - async start(loggerPort: number): Promise { + async startPort(loggerPort: number): Promise { loggerPort = await findAvailablePort(loggerPort); this.server = this.app.listen(loggerPort); return loggerPort; @@ -58,7 +58,7 @@ export default class Logger { /** * Stop the HTTP server hosting the web UI */ - stop(): void { + stopPort(): void { if (this.server) this.server.close(); } } From c4ca3b97027cc06fd67368c3a54323ffec1d8932 Mon Sep 17 00:00:00 2001 From: devance Date: Sat, 20 Jul 2024 01:33:23 +0530 Subject: [PATCH 13/15] reverted API names, removed loggerPort arg and kept this.port in constructor --- packages/plugin/webpack/src/Config.ts | 2 +- packages/plugin/webpack/src/WebpackPlugin.ts | 4 ++-- packages/utils/web-multi-logger/src/Logger.ts | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/plugin/webpack/src/Config.ts b/packages/plugin/webpack/src/Config.ts index 2a201af825..8fc22af351 100644 --- a/packages/plugin/webpack/src/Config.ts +++ b/packages/plugin/webpack/src/Config.ts @@ -151,7 +151,7 @@ export interface WebpackPluginConfig { */ port?: number; /** - * The TCP port for web-multi-logger. Defaults to 9000. + * The TCP port for web-multi-logger. Defaults to 9000. If the specified port is unavailable, the server will attempt to start on ports up to 10 higher (port + 10). */ loggerPort?: number; /** diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index 344d86ad5a..b9f9f4fe89 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -101,7 +101,7 @@ export default class WebpackPlugin extends PluginBase { this.servers = []; for (const logger of this.loggers) { d('stopping logger'); - logger.stopPort(); + logger.stop(); } this.loggers = []; } @@ -571,7 +571,7 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); const logger = new Logger(); this.loggers.push(logger); - this.loggerPort = await logger.startPort(this.loggerPort); + this.loggerPort = await logger.start(); return { tasks: [ diff --git a/packages/utils/web-multi-logger/src/Logger.ts b/packages/utils/web-multi-logger/src/Logger.ts index 126f4dbe50..68b487e455 100644 --- a/packages/utils/web-multi-logger/src/Logger.ts +++ b/packages/utils/web-multi-logger/src/Logger.ts @@ -49,16 +49,16 @@ export default class Logger { * * @returns the port number */ - async startPort(loggerPort: number): Promise { - loggerPort = await findAvailablePort(loggerPort); - this.server = this.app.listen(loggerPort); - return loggerPort; + async start(): Promise { + this.port = await findAvailablePort(this.port); + this.server = this.app.listen(this.port); + return this.port; } /** * Stop the HTTP server hosting the web UI */ - stopPort(): void { + stop(): void { if (this.server) this.server.close(); } } From 809c35aed98d44c922e12ef35a22aa327f7f54e7 Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Mon, 2 Dec 2024 15:40:47 -0800 Subject: [PATCH 14/15] fix lint --- packages/plugin/webpack/src/WebpackPlugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/plugin/webpack/src/WebpackPlugin.ts b/packages/plugin/webpack/src/WebpackPlugin.ts index b548dfe05e..c2381abf21 100644 --- a/packages/plugin/webpack/src/WebpackPlugin.ts +++ b/packages/plugin/webpack/src/WebpackPlugin.ts @@ -597,7 +597,6 @@ the generated files). Instead, it is ${JSON.stringify(pj.main)}`); } private alreadyStarted = false; - } export { WebpackPlugin, WebpackPluginConfig }; From 0861161b255ffba0873e8504ced8227ae99f8c8d Mon Sep 17 00:00:00 2001 From: Erick Zhao Date: Mon, 2 Dec 2024 15:54:53 -0800 Subject: [PATCH 15/15] make syncpack happy --- packages/utils/web-multi-logger/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/web-multi-logger/package.json b/packages/utils/web-multi-logger/package.json index 1813e807c9..6e6b954018 100644 --- a/packages/utils/web-multi-logger/package.json +++ b/packages/utils/web-multi-logger/package.json @@ -8,7 +8,7 @@ "main": "dist/Logger.js", "typings": "dist/Logger.d.ts", "dependencies": { - "@electron-forge/core-utils": "7.4.0", + "@electron-forge/core-utils": "7.5.0", "express": "^4.17.1", "express-ws": "^5.0.2", "xterm": "^4.9.0",