Skip to content
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

Update Lighthouse CI start regex command #470

Merged
merged 3 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-hornets-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Fix potential infinite loop when searching for a free port
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ jobs:
- name: Build
run: yarn ci
- name: LHCI
run: lhci autorun --upload.target=temporary-public-storage --collect.url="http://localhost:8080" --collect.startServerCommand="yarn workspace @examples/demo serve" --upload.githubAppToken=${{ secrets.LHCI_GITHUB_APP_TOKEN }}
run: lhci autorun --upload.target=temporary-public-storage --collect.url="http://localhost:8080" --collect.startServerCommand="yarn workspace @examples/demo serve" --collect.startServerReadyPattern="server running at" --upload.githubAppToken=${{ secrets.LHCI_GITHUB_APP_TOKEN }}
27 changes: 23 additions & 4 deletions packages/wmr/src/lib/net-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,34 @@ export async function getFreePort(port) {

// Limit to 20 attempts for now
while (!found && attempts <= 20) {
if (!isPortFree(port)) {
port++;
attempts++;
}
if (isPortFree(port)) break;

port++;
attempts++;
}

return port;
}

/**
* Check if the user specified port is available and
* throw if it is taken. If the user didn't specify
* a port we'll try to find a free one
* @param {{port?: number | string}} options
*/
export async function getPort(options) {
const userPort = options.port || process.env.PORT;
if (userPort !== undefined) {
if (await isPortFree(+userPort)) {
return +userPort;
}

throw new Error(`Another process is already running on port ${userPort}. Please choose a different port.`);
}

return await getFreePort(8080);
}

/**
* Display local and network origins for a server's address.
* @param {net.AddressInfo|string} addr
Expand Down
3 changes: 3 additions & 0 deletions packages/wmr/src/lib/normalize-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import url from 'url';
import { readEnvFiles } from './environment.js';
import { compileSingleModule } from './compile-single-module.js';
import { debug } from './output-utils.js';
import { getPort } from './net-utils.js';

/**
* @param {Partial<Options>} options
Expand Down Expand Up @@ -50,6 +51,8 @@ export async function normalizeOptions(options, mode) {
options.publicPath += '/';
}

options.port = await getPort(options);

// If the CWD has a public/ directory, all files are assumed to be within it.
// From here, everything except node_modules and `out` are relative to public:
if (options.public !== '.' && (await isDirectory(join(options.cwd, options.public)))) {
Expand Down
4 changes: 2 additions & 2 deletions packages/wmr/src/serve.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as kl from 'kolorist';
import polka from 'polka';
import { normalizeOptions } from './lib/normalize-options.js';
import { getFreePort, getServerAddresses } from './lib/net-utils.js';
import { getPort, getServerAddresses } from './lib/net-utils.js';
import { createServer } from 'http';
import { createHttp2Server } from './lib/http2.js';
import compression from './lib/polkompress.js';
Expand Down Expand Up @@ -98,7 +98,7 @@ export default async function serve(options = {}) {
app.http2 = false;
}

const port = await getFreePort(options.port || process.env.PORT || 8080);
const port = await getPort(options);
const host = options.host || process.env.HOST;
app.listen(port, host, () => {
const addresses = getServerAddresses(app.server.address(), { https: app.http2 });
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module 'wmr' {
reload: boolean;
public: string;
publicPath: string;
port: number;
root: string;
out: string;
overlayDir: string;
Expand Down