chore(portless): upgrade local dev URLs

This commit is contained in:
Tommaso Casaburi
2026-04-28 14:25:16 +07:00
parent 8ba1761d07
commit 34b60c7d53
23 changed files with 92 additions and 88 deletions
+1 -1
View File
@@ -4,7 +4,7 @@ These rules apply to `scripts/**`. Follow the repo-root `AGENTS.md` first, then
- Keep scripts non-interactive and idempotent. Print the command, URL, branch, or path being acted on so failures are diagnosable.
- Use repo-relative paths and environment variables instead of user-specific absolute paths.
- For dev-server helpers, default to `http://5chan.localhost:1355`, but allow a branch-scoped `*.5chan.localhost:1355` route when the launcher is avoiding a Portless name collision. Respect the existing `PORTLESS=0` fallback instead of hard-coding alternate ports. For USB Android preview, `scripts/start-android-usb.mjs` mirrors bitsocial-web: `adb reverse` plus Vite on `127.0.0.1`, then `am start` VIEW to open the default browser when the port is listening (disable with `ANDROID_USB_OPEN_BROWSER=0`).
- For dev-server helpers, default to `https://5chan.localhost`, but allow a branch-scoped `*.5chan.localhost` route when the launcher is avoiding a Portless name collision. Respect the existing `PORTLESS=0` fallback instead of hard-coding alternate ports. For USB Android preview, `scripts/start-android-usb.mjs` mirrors bitsocial-web: `adb reverse` plus Vite on `127.0.0.1`, then `am start` VIEW to open the default browser when the port is listening (disable with `ANDROID_USB_OPEN_BROWSER=0`).
- Keep shell helpers thin. When logic becomes stateful or cross-platform, prefer a Node script.
- Git and worktree helpers must validate input and default to safe operations.
- If a helper deletes local branches automatically, document the exact eligibility checks and keep the behavior conservative.
+4 -4
View File
@@ -7,7 +7,7 @@ wait_timeout="${AGENT_INIT_TIMEOUT_SECONDS:-60}"
get_default_app_url() {
if [ "${PORTLESS:-}" = "0" ]; then
echo "http://5chan.localhost:1355"
echo "http://localhost:3000"
return
fi
@@ -23,12 +23,12 @@ get_default_app_url() {
)"
if [ -n "$branch_label" ]; then
echo "http://${branch_label}.5chan.localhost:1355"
echo "https://${branch_label}.5chan.localhost"
return
fi
fi
echo "http://5chan.localhost:1355"
echo "https://5chan.localhost"
}
app_url="${AGENT_APP_URL:-$(get_default_app_url)}"
@@ -55,7 +55,7 @@ mkdir -p "$log_dir"
cd "$repo_root"
is_server_up() {
curl -fsS "$app_url" >/dev/null 2>&1
curl -fsSk "$app_url" >/dev/null 2>&1
}
wait_for_server() {
+1 -1
View File
@@ -6,7 +6,7 @@ import { isWindows, repoRoot, resolvePort, startVite, waitForPort } from './dev-
const adbBin = isWindows ? 'adb.exe' : 'adb';
const host = '127.0.0.1';
const requestedPort = Number(process.env.ANDROID_USB_PORT || 1355);
const requestedPort = Number(process.env.ANDROID_USB_PORT || 3000);
const openBrowser = process.env.ANDROID_USB_OPEN_BROWSER !== '0' && process.env.ANDROID_USB_OPEN_BROWSER !== 'false';
function fail(message) {
+28 -22
View File
@@ -1,7 +1,8 @@
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { spawn, spawnSync } from 'node:child_process';
import { get } from 'node:http';
import { get as httpGet } from 'node:http';
import { get as httpsGet } from 'node:https';
import { resolvePort } from './dev-server-utils.mjs';
const isWindows = process.platform === 'win32';
@@ -11,8 +12,8 @@ const executableSuffix = isWindows ? '.cmd' : '';
const portlessBin = join(binDir, `portless${executableSuffix}`);
const viteBin = join(binDir, `vite${executableSuffix}`);
const fallbackHost = '127.0.0.1';
const fallbackUrlHost = '5chan.localhost';
const fallbackRequestedPort = 1355;
const fallbackUrlHost = 'localhost';
const fallbackRequestedPort = Number(process.env.PORT) || 3000;
function sanitizeLabel(value) {
return value
@@ -37,7 +38,7 @@ function getCurrentBranch() {
return branch || null;
}
function getActivePortlessRoutes() {
function getActivePortlessRouteHosts() {
const result = spawnSync(portlessBin, ['list'], {
cwd: process.cwd(),
encoding: 'utf8',
@@ -48,16 +49,16 @@ function getActivePortlessRoutes() {
return new Set();
}
const matches = result.stdout.match(/http:\/\/[a-z0-9.-]+\.localhost:1355/g) || [];
const matches = result.stdout.match(/https?:\/\/[a-z0-9.-]+\.localhost(?::\d+)?/g) || [];
return new Set(matches);
return new Set(matches.map((url) => new URL(url).hostname));
}
function isRouteBusy(activeRoutes, appName) {
return activeRoutes.has(`http://${appName}.localhost:1355`);
function isRouteBusy(activeRouteHosts, appName) {
return activeRouteHosts.has(`${appName}.localhost`);
}
function getPreferredPortlessAppName(activeRoutes) {
function getPreferredPortlessAppName(activeRouteHosts) {
const branch = getCurrentBranch();
const branchLabel = sanitizeLabel(branch || 'current');
@@ -65,7 +66,7 @@ function getPreferredPortlessAppName(activeRoutes) {
return `${branchLabel}.5chan`;
}
if (isRouteBusy(activeRoutes, '5chan')) {
if (isRouteBusy(activeRouteHosts, '5chan')) {
return `${branchLabel}.5chan`;
}
@@ -73,17 +74,17 @@ function getPreferredPortlessAppName(activeRoutes) {
}
function getPortlessAppName() {
const activeRoutes = getActivePortlessRoutes();
const preferredAppName = getPreferredPortlessAppName(activeRoutes);
const activeRouteHosts = getActivePortlessRouteHosts();
const preferredAppName = getPreferredPortlessAppName(activeRouteHosts);
if (!isRouteBusy(activeRoutes, preferredAppName)) {
if (!isRouteBusy(activeRouteHosts, preferredAppName)) {
return preferredAppName;
}
for (let suffix = 2; suffix < 1000; suffix += 1) {
const candidate = `${preferredAppName}-${suffix}`;
if (!isRouteBusy(activeRoutes, candidate)) {
if (!isRouteBusy(activeRouteHosts, candidate)) {
return candidate;
}
}
@@ -98,7 +99,7 @@ let publicUrl = null;
if (command === portlessBin) {
const appName = getPortlessAppName();
publicUrl = `http://${appName}.localhost:1355`;
publicUrl = `https://${appName}.localhost`;
args = [appName, 'vite'];
if (appName !== '5chan') {
@@ -127,7 +128,7 @@ const child = spawn(command, args, {
});
if (publicUrl && process.env.BROWSER !== 'none') {
waitForHttpReady(publicUrl, 30_000)
waitForUrlReady(publicUrl, 30_000)
.then(() => {
console.log(`Opening ${publicUrl} in browser...`);
openInBrowser(publicUrl);
@@ -146,16 +147,19 @@ child.on('exit', (code, signal) => {
process.exit(code ?? 0);
});
async function waitForHttpReady(url, timeoutMs) {
async function waitForUrlReady(url, timeoutMs) {
const startedAt = Date.now();
while (Date.now() - startedAt < timeoutMs) {
const ready = await new Promise((resolve) => {
const request = get(url, (response) => {
const parsedUrl = new URL(url);
const getUrl = parsedUrl.protocol === 'https:' ? httpsGet : httpGet;
const onResponse = (response) => {
response.resume();
const statusCode = response.statusCode ?? 500;
resolve(statusCode >= 200 && statusCode < 400);
});
};
const request = parsedUrl.protocol === 'https:' ? getUrl(parsedUrl, { rejectUnauthorized: false }, onResponse) : getUrl(parsedUrl, onResponse);
request.on('error', () => resolve(false));
request.setTimeout(2_000, () => {
@@ -176,9 +180,11 @@ async function waitForHttpReady(url, timeoutMs) {
function openInBrowser(url) {
const opener =
process.platform === 'darwin' ? { cmd: 'open', args: [url] }
: process.platform === 'win32' ? { cmd: 'cmd', args: ['/c', 'start', '""', url] }
: { cmd: 'xdg-open', args: [url] };
process.platform === 'darwin'
? { cmd: 'open', args: [url] }
: process.platform === 'win32'
? { cmd: 'cmd', args: ['/c', 'start', '""', url] }
: { cmd: 'xdg-open', args: [url] };
spawn(opener.cmd, opener.args, { stdio: 'ignore', detached: true }).unref();
}