From 6de1d741f6ea1cf4fd0de9d169cd35eac87a8c6f Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Wed, 1 Oct 2025 15:43:08 +0100 Subject: [PATCH] Move error handling from `startProxy` to `runWrapper` in `start-proxy` action --- lib/start-proxy-action.js | 134 ++++++++++++++++--------------- src/start-proxy-action.ts | 161 +++++++++++++++++++------------------- 2 files changed, 150 insertions(+), 145 deletions(-) diff --git a/lib/start-proxy-action.js b/lib/start-proxy-action.js index 1b934050d..43f1dfd7c 100644 --- a/lib/start-proxy-action.js +++ b/lib/start-proxy-action.js @@ -49322,6 +49322,9 @@ async function delay(milliseconds, opts) { } }); } +function wrapError(error2) { + return error2 instanceof Error ? error2 : new Error(String(error2)); +} function getErrorMessage(error2) { return error2 instanceof Error ? error2.message : String(error2); } @@ -49612,79 +49615,80 @@ function generateCertificateAuthority() { async function runWrapper() { persistInputs(); const logger = getActionsLogger(); - const tempDir = getTemporaryDirectory(); - const proxyLogFilePath = path.resolve(tempDir, "proxy.log"); - core8.saveState("proxy-log-file", proxyLogFilePath); - const credentials = getCredentials( - logger, - getOptionalInput("registry_secrets"), - getOptionalInput("registries_credentials"), - getOptionalInput("language") - ); - if (credentials.length === 0) { - logger.info("No credentials found, skipping proxy setup."); - return; - } - logger.info( - `Credentials loaded for the following registries: + try { + const tempDir = getTemporaryDirectory(); + const proxyLogFilePath = path.resolve(tempDir, "proxy.log"); + core8.saveState("proxy-log-file", proxyLogFilePath); + const credentials = getCredentials( + logger, + getOptionalInput("registry_secrets"), + getOptionalInput("registries_credentials"), + getOptionalInput("language") + ); + if (credentials.length === 0) { + logger.info("No credentials found, skipping proxy setup."); + return; + } + logger.info( + `Credentials loaded for the following registries: ${credentials.map((c) => credentialToStr(c)).join("\n")}` - ); - const ca = generateCertificateAuthority(); - const proxyConfig = { - all_credentials: credentials, - ca - }; - const proxyBin = await getProxyBinaryPath(logger); - await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger); + ); + const ca = generateCertificateAuthority(); + const proxyConfig = { + all_credentials: credentials, + ca + }; + const proxyBin = await getProxyBinaryPath(logger); + await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger); + } catch (unwrappedError) { + const error2 = wrapError(unwrappedError); + core8.setFailed(`start-proxy action failed: ${error2.message}`); + } } async function startProxy(binPath, config, logFilePath, logger) { const host = "127.0.0.1"; let port = 49152; - try { - let subprocess = void 0; - let tries = 5; - let subprocessError = void 0; - while (tries-- > 0 && !subprocess && !subprocessError) { - subprocess = (0, import_child_process.spawn)( - binPath, - ["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath], - { - detached: true, - stdio: ["pipe", "ignore", "ignore"] - } - ); - subprocess.unref(); - if (subprocess.pid) { - core8.saveState("proxy-process-pid", `${subprocess.pid}`); + let subprocess = void 0; + let tries = 5; + let subprocessError = void 0; + while (tries-- > 0 && !subprocess && !subprocessError) { + subprocess = (0, import_child_process.spawn)( + binPath, + ["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath], + { + detached: true, + stdio: ["pipe", "ignore", "ignore"] } - subprocess.on("error", (error2) => { - subprocessError = error2; - }); - subprocess.on("exit", (code) => { - if (code !== 0) { - port = Math.floor(Math.random() * (65535 - 49152) + 49152); - subprocess = void 0; - } - }); - subprocess.stdin?.write(JSON.stringify(config)); - subprocess.stdin?.end(); - await delay(1e3); + ); + subprocess.unref(); + if (subprocess.pid) { + core8.saveState("proxy-process-pid", `${subprocess.pid}`); } - if (subprocessError) { - throw subprocessError; - } - logger.info(`Proxy started on ${host}:${port}`); - core8.setOutput("proxy_host", host); - core8.setOutput("proxy_port", port.toString()); - core8.setOutput("proxy_ca_certificate", config.ca.cert); - const registry_urls = config.all_credentials.filter((credential) => credential.url !== void 0).map((credential) => ({ - type: credential.type, - url: credential.url - })); - core8.setOutput("proxy_urls", JSON.stringify(registry_urls)); - } catch (error2) { - core8.setFailed(`start-proxy action failed: ${getErrorMessage(error2)}`); + subprocess.on("error", (error2) => { + subprocessError = error2; + }); + subprocess.on("exit", (code) => { + if (code !== 0) { + port = Math.floor(Math.random() * (65535 - 49152) + 49152); + subprocess = void 0; + } + }); + subprocess.stdin?.write(JSON.stringify(config)); + subprocess.stdin?.end(); + await delay(1e3); } + if (subprocessError) { + throw subprocessError; + } + logger.info(`Proxy started on ${host}:${port}`); + core8.setOutput("proxy_host", host); + core8.setOutput("proxy_port", port.toString()); + core8.setOutput("proxy_ca_certificate", config.ca.cert); + const registry_urls = config.all_credentials.filter((credential) => credential.url !== void 0).map((credential) => ({ + type: credential.type, + url: credential.url + })); + core8.setOutput("proxy_urls", JSON.stringify(registry_urls)); } async function getProxyBinaryPath(logger) { const proxyFileName = process.platform === "win32" ? `${UPDATEJOB_PROXY}.exe` : UPDATEJOB_PROXY; diff --git a/src/start-proxy-action.ts b/src/start-proxy-action.ts index 9592b904b..763ab5f89 100644 --- a/src/start-proxy-action.ts +++ b/src/start-proxy-action.ts @@ -89,40 +89,45 @@ async function runWrapper() { const logger = getActionsLogger(); - // Setup logging for the proxy - const tempDir = actionsUtil.getTemporaryDirectory(); - const proxyLogFilePath = path.resolve(tempDir, "proxy.log"); - core.saveState("proxy-log-file", proxyLogFilePath); + try { + // Setup logging for the proxy + const tempDir = actionsUtil.getTemporaryDirectory(); + const proxyLogFilePath = path.resolve(tempDir, "proxy.log"); + core.saveState("proxy-log-file", proxyLogFilePath); - // Get the configuration options - const credentials = getCredentials( - logger, - actionsUtil.getOptionalInput("registry_secrets"), - actionsUtil.getOptionalInput("registries_credentials"), - actionsUtil.getOptionalInput("language"), - ); + // Get the configuration options + const credentials = getCredentials( + logger, + actionsUtil.getOptionalInput("registry_secrets"), + actionsUtil.getOptionalInput("registries_credentials"), + actionsUtil.getOptionalInput("language"), + ); - if (credentials.length === 0) { - logger.info("No credentials found, skipping proxy setup."); - return; + if (credentials.length === 0) { + logger.info("No credentials found, skipping proxy setup."); + return; + } + + logger.info( + `Credentials loaded for the following registries:\n ${credentials + .map((c) => credentialToStr(c)) + .join("\n")}`, + ); + + const ca = generateCertificateAuthority(); + + const proxyConfig: ProxyConfig = { + all_credentials: credentials, + ca, + }; + + // Start the Proxy + const proxyBin = await getProxyBinaryPath(logger); + await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger); + } catch (unwrappedError) { + const error = util.wrapError(unwrappedError); + core.setFailed(`start-proxy action failed: ${error.message}`); } - - logger.info( - `Credentials loaded for the following registries:\n ${credentials - .map((c) => credentialToStr(c)) - .join("\n")}`, - ); - - const ca = generateCertificateAuthority(); - - const proxyConfig: ProxyConfig = { - all_credentials: credentials, - ca, - }; - - // Start the Proxy - const proxyBin = await getProxyBinaryPath(logger); - await startProxy(proxyBin, proxyConfig, proxyLogFilePath, logger); } async function startProxy( @@ -133,57 +138,53 @@ async function startProxy( ) { const host = "127.0.0.1"; let port = 49152; - try { - let subprocess: ChildProcess | undefined = undefined; - let tries = 5; - let subprocessError: Error | undefined = undefined; - while (tries-- > 0 && !subprocess && !subprocessError) { - subprocess = spawn( - binPath, - ["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath], - { - detached: true, - stdio: ["pipe", "ignore", "ignore"], - }, - ); - subprocess.unref(); - if (subprocess.pid) { - core.saveState("proxy-process-pid", `${subprocess.pid}`); + let subprocess: ChildProcess | undefined = undefined; + let tries = 5; + let subprocessError: Error | undefined = undefined; + while (tries-- > 0 && !subprocess && !subprocessError) { + subprocess = spawn( + binPath, + ["-addr", `${host}:${port}`, "-config", "-", "-logfile", logFilePath], + { + detached: true, + stdio: ["pipe", "ignore", "ignore"], + }, + ); + subprocess.unref(); + if (subprocess.pid) { + core.saveState("proxy-process-pid", `${subprocess.pid}`); + } + subprocess.on("error", (error) => { + subprocessError = error; + }); + subprocess.on("exit", (code) => { + if (code !== 0) { + // If the proxy failed to start, try a different port from the ephemeral range [49152, 65535] + port = Math.floor(Math.random() * (65535 - 49152) + 49152); + subprocess = undefined; } - subprocess.on("error", (error) => { - subprocessError = error; - }); - subprocess.on("exit", (code) => { - if (code !== 0) { - // If the proxy failed to start, try a different port from the ephemeral range [49152, 65535] - port = Math.floor(Math.random() * (65535 - 49152) + 49152); - subprocess = undefined; - } - }); - subprocess.stdin?.write(JSON.stringify(config)); - subprocess.stdin?.end(); - // Wait a little to allow the proxy to start - await util.delay(1000); - } - if (subprocessError) { - // eslint-disable-next-line @typescript-eslint/only-throw-error - throw subprocessError; - } - logger.info(`Proxy started on ${host}:${port}`); - core.setOutput("proxy_host", host); - core.setOutput("proxy_port", port.toString()); - core.setOutput("proxy_ca_certificate", config.ca.cert); - - const registry_urls = config.all_credentials - .filter((credential) => credential.url !== undefined) - .map((credential) => ({ - type: credential.type, - url: credential.url, - })); - core.setOutput("proxy_urls", JSON.stringify(registry_urls)); - } catch (error) { - core.setFailed(`start-proxy action failed: ${util.getErrorMessage(error)}`); + }); + subprocess.stdin?.write(JSON.stringify(config)); + subprocess.stdin?.end(); + // Wait a little to allow the proxy to start + await util.delay(1000); } + if (subprocessError) { + // eslint-disable-next-line @typescript-eslint/only-throw-error + throw subprocessError; + } + logger.info(`Proxy started on ${host}:${port}`); + core.setOutput("proxy_host", host); + core.setOutput("proxy_port", port.toString()); + core.setOutput("proxy_ca_certificate", config.ca.cert); + + const registry_urls = config.all_credentials + .filter((credential) => credential.url !== undefined) + .map((credential) => ({ + type: credential.type, + url: credential.url, + })); + core.setOutput("proxy_urls", JSON.stringify(registry_urls)); } async function getProxyBinaryPath(logger: Logger): Promise {