Skip to content

Commit

Permalink
Fixed issue with blocked port #671
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Feb 8, 2024
1 parent ad82f15 commit 2619f43
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed error when importing `once` without context (#664)
- Fixed issue with monorepos using Rush (#667)
- Fixed issue with blocked port in case of full debug reload (#671)
- Removed legacy option in `piral-cli-webpack5` to support IE8
- Removed pilet-related options in debug settings when running `piral debug` (#670)
- Improved internal navigation Blazor pilets using `piral-blazor`
Expand Down
2 changes: 1 addition & 1 deletion src/tooling/piral-cli-webpack5/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const debugPiral: DebugPiralBundlerDefinition = {
.default('config', defaultWebpackConfig)
.number('hmr-port')
.describe('hmr-port', 'Sets the port to be used for HMR for reloading the application.')
.default('hmr-port', 62123);
.default('hmr-port', undefined);
},
path: resolve(__dirname, 'webpack', 'piral.js'),
};
Expand Down
9 changes: 8 additions & 1 deletion src/tooling/piral-cli-webpack5/src/webpack/piral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ async function getConfig(
];
}

function getRandomPort() {
const min = 60000;
const max = 65536;
const rng = max - min;
return ~~(Math.random() * rng) + min;
}

const handler: PiralBuildHandler = {
async create(options) {
const { 'hmr-port': defaultHmrPort = 62123, config = defaultWebpackConfig } = options.args._;
const { 'hmr-port': defaultHmrPort = getRandomPort(), config = defaultWebpackConfig } = options.args._;
const hmrPort = options.hmr ? await getFreePort(defaultHmrPort) : 0;
const otherConfigPath = resolve(options.root, config);
const baseConfig = await getConfig(
Expand Down
13 changes: 10 additions & 3 deletions src/tooling/piral-cli/src/apps/debug-pilet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, dirname, resolve, relative, basename } from 'path';
import { callDebugPiralFromMonoRepo, callPiletDebug } from '../bundler';
import { AppDefinition, LogLevels, PiletSchemaVersion } from '../types';
import { AppDefinition, LogLevels, NetworkSpec, PiletSchemaVersion } from '../types';
import {
checkExistingDirectory,
retrievePiletData,
Expand Down Expand Up @@ -230,6 +230,7 @@ export async function debugPilet(baseDir = process.cwd(), options: DebugPiletOpt
} = options;
const publicUrl = normalizePublicUrl(originalPublicUrl);
const fullBase = resolve(process.cwd(), baseDir);
const networks: Array<NetworkSpec> = [];
setLogLevel(logLevel);

await hooks.onBegin?.({ options, fullBase });
Expand Down Expand Up @@ -341,7 +342,13 @@ export async function debugPilet(baseDir = process.cwd(), options: DebugPiletOpt
await Promise.all(
appInstances.sort(byPort).map(async ([appDir, appPort], i) => {
const platform = configurePlatform();
const suggestedPort = appPort || originalPort + i;

if (networks.length === i) {
networks.push({
port: appPort || originalPort + i,
type: 'proposed',
});
}

await platform.startModule({
appDir,
Expand All @@ -351,7 +358,7 @@ export async function debugPilet(baseDir = process.cwd(), options: DebugPiletOpt
fullBase,
hooks,
open,
originalPort: suggestedPort,
network: networks[i],
publicUrl,
maxListeners,
registerEnd(cb) {
Expand Down
8 changes: 6 additions & 2 deletions src/tooling/piral-cli/src/apps/debug-piral.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, join, resolve } from 'path';
import { callPiralDebug } from '../bundler';
import { LogLevels } from '../types';
import { LogLevels, NetworkSpec } from '../types';
import {
retrievePiletsInfo,
retrievePiralRoot,
Expand Down Expand Up @@ -124,6 +124,10 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
} = options;
const publicUrl = normalizePublicUrl(originalPublicUrl);
const fullBase = resolve(process.cwd(), baseDir);
const network: NetworkSpec = {
port: originalPort,
type: 'proposed',
};
setLogLevel(logLevel);

await hooks.onBegin?.({ options, fullBase });
Expand Down Expand Up @@ -187,7 +191,7 @@ export async function debugPiral(baseDir = process.cwd(), options: DebugPiralOpt
fullBase,
hooks,
open,
originalPort,
network,
publicUrl,
root,
targetDir,
Expand Down
5 changes: 4 additions & 1 deletion src/tooling/piral-cli/src/build/bundler-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function createBundler(cwd: string, ps: ChildProcess, args: any) {
}
},
stop() {
ps.kill();
return new Promise<void>(resolve => {
ps.on('exit', resolve);
ps.kill();
});
},
on(cb: BundleListener) {
listeners.push(cb);
Expand Down
33 changes: 25 additions & 8 deletions src/tooling/piral-cli/src/platforms/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import { config } from '../common/config';
import { openBrowser } from '../common/browser';
import { checkExistingDirectory, findFile } from '../common/io';
import { getAvailablePort } from '../common/port';
import { PlatformStartShellOptions, PlatformStartModuleOptions } from '../types';
import { PlatformStartShellOptions, PlatformStartModuleOptions, NetworkSpec } from '../types';

async function getPort(network: NetworkSpec) {
if (network.type === 'proposed') {
network.port = await getAvailablePort(network.port);
network.type = 'fixed';
}

return network.port;
}

async function startModule(options: PlatformStartModuleOptions) {
const {
Expand All @@ -16,7 +25,7 @@ async function startModule(options: PlatformStartModuleOptions) {
feed,
publicUrl,
customkrasrc,
originalPort,
network,
hooks,
registerWatcher,
registerEnd,
Expand Down Expand Up @@ -60,15 +69,19 @@ async function startModule(options: PlatformStartModuleOptions) {

configs.forEach(registerWatcher);

const port = await getAvailablePort(originalPort);
const shouldNotify = network.type === 'proposed';
const port = await getPort(network);
const krasConfig = readKrasConfig({ port, initial, required }, ...configs);

log('generalVerbose_0004', `Using kras with configuration: ${JSON.stringify(krasConfig, undefined, 2)}`);

const krasServer = buildKrasWithCli(krasConfig);
krasServer.setMaxListeners(maxListeners);
krasServer.removeAllListeners('open');
krasServer.on('open', notifyServerOnline(publicUrl, krasConfig.api));

if (shouldNotify) {
krasServer.on('open', notifyServerOnline(publicUrl, krasConfig.api));
}

await hooks.beforeOnline?.({ krasServer, krasConfig, open, port, api, feed, pilets, publicUrl });
await krasServer.start();
Expand All @@ -88,7 +101,7 @@ async function startShell(options: PlatformStartShellOptions) {
publicUrl,
bundler,
customkrasrc,
originalPort,
network,
hooks,
registerWatcher,
registerEnd,
Expand Down Expand Up @@ -123,21 +136,25 @@ async function startShell(options: PlatformStartShellOptions) {

configs.forEach(registerWatcher);

const port = await getAvailablePort(originalPort);
const shouldNotify = network.type === 'proposed';
const port = await getPort(network);
const krasConfig = readKrasConfig({ port, initial, required }, ...configs);
log('generalVerbose_0004', `Using kras with configuration: ${JSON.stringify(krasConfig, undefined, 2)}`);

const krasServer = buildKrasWithCli(krasConfig);
krasServer.setMaxListeners(16);
krasServer.removeAllListeners('open');
krasServer.on('open', notifyServerOnline(publicUrl, krasConfig.api));

if (shouldNotify) {
krasServer.on('open', notifyServerOnline(publicUrl, krasConfig.api));
}

await hooks.beforeOnline?.({ krasServer, krasConfig, open, port, publicUrl });
await krasServer.start();
openBrowser(open, port, publicUrl, !!krasConfig.ssl);
await hooks.afterOnline?.({ krasServer, krasConfig, open, port, publicUrl });

registerEnd(() => krasServer.stop());
registerEnd(async () => krasServer.stop());
}

export function setup() {
Expand Down
13 changes: 9 additions & 4 deletions src/tooling/piral-cli/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,25 @@ export interface BundleDetails {
export interface Bundler {
readonly bundle: BundleDetails;
start(): void;
stop(): void;
stop(): Promise<void>;
on(cb: (args: any) => void): void;
off(cb: (args: any) => void): void;
ready(): Promise<void>;
}

export interface NetworkSpec {
port: number;
type: 'proposed' | 'fixed';
}

export interface PlatformStartModuleOptions {
appDir?: string;
open: boolean;
fullBase: string;
feed: string | Array<string>;
publicUrl: string;
customkrasrc: string;
originalPort: number;
network: NetworkSpec;
hooks: Record<string, Function>;
registerWatcher(file: string): void;
registerEnd(cb: () => void): void;
Expand All @@ -143,10 +148,10 @@ export interface PlatformStartShellOptions {
publicUrl: string;
bundler: Bundler;
customkrasrc: string;
originalPort: number;
network: NetworkSpec;
hooks: Record<string, Function>;
registerWatcher(file: string): void;
registerEnd(cb: () => void): void;
registerEnd(cb: () => void | Promise<void>): void;
}

export interface ReleaseProvider {
Expand Down

0 comments on commit 2619f43

Please sign in to comment.