11import fs from "node:fs/promises" ;
22import path from "node:path" ;
33import { normalizeEnvVarKey } from "../infra/host-env-security.js" ;
4- import { parseStrictPositiveInteger } from "../infra/parse-finite-number .js" ;
4+ import { parseTcpPort } from "../infra/tcp-port .js" ;
55import {
66 normalizeLowercaseStringOrEmpty ,
77 normalizeOptionalString ,
@@ -249,14 +249,25 @@ function auditGatewayCommand(programArguments: string[] | undefined, issues: Ser
249249 }
250250}
251251
252- function parseGatewayPortArg ( value : string | undefined ) : number | undefined {
253- const parsed = parseStrictPositiveInteger ( value ?? "" ) ;
254- return parsed !== undefined && parsed <= 65535 ? parsed : undefined ;
252+ type GatewayServiceCommandPort =
253+ | { kind : "missing" }
254+ | { kind : "valid" ; port : number }
255+ | { kind : "invalid" ; raw : string } ;
256+
257+ function parseGatewayPortArg ( value : string | undefined ) : GatewayServiceCommandPort {
258+ const raw = value ?. trim ( ) ?? "" ;
259+ const port = parseTcpPort ( raw ) ;
260+ if ( port !== null ) {
261+ return { kind : "valid" , port } ;
262+ }
263+ return raw ? { kind : "invalid" , raw } : { kind : "missing" } ;
255264}
256265
257- export function readGatewayServiceCommandPort ( programArguments ?: string [ ] ) : number | undefined {
266+ function readGatewayServiceCommandPortState (
267+ programArguments ?: string [ ] ,
268+ ) : GatewayServiceCommandPort {
258269 if ( ! programArguments || programArguments . length === 0 ) {
259- return undefined ;
270+ return { kind : "missing" } ;
260271 }
261272 for ( let index = 0 ; index < programArguments . length ; index += 1 ) {
262273 const arg = programArguments [ index ] ;
@@ -267,7 +278,12 @@ export function readGatewayServiceCommandPort(programArguments?: string[]): numb
267278 return parseGatewayPortArg ( arg . slice ( "--port=" . length ) ) ;
268279 }
269280 }
270- return undefined ;
281+ return { kind : "missing" } ;
282+ }
283+
284+ export function readGatewayServiceCommandPort ( programArguments ?: string [ ] ) : number | undefined {
285+ const servicePort = readGatewayServiceCommandPortState ( programArguments ) ;
286+ return servicePort . kind === "valid" ? servicePort . port : undefined ;
271287}
272288
273289function auditGatewayServicePort ( params : {
@@ -283,14 +299,21 @@ function auditGatewayServicePort(params: {
283299 ) {
284300 return ;
285301 }
286- const servicePort = readGatewayServiceCommandPort ( params . programArguments ) ;
287- if ( servicePort === undefined || servicePort === params . expectedPort ) {
302+ const servicePort = readGatewayServiceCommandPortState ( params . programArguments ) ;
303+ if ( servicePort . kind === "missing" ) {
304+ return ;
305+ }
306+ if ( servicePort . kind === "valid" && servicePort . port === params . expectedPort ) {
288307 return ;
289308 }
309+ const detail =
310+ servicePort . kind === "valid"
311+ ? `${ servicePort . port } -> ${ params . expectedPort } `
312+ : `${ servicePort . raw } -> ${ params . expectedPort } ` ;
290313 params . issues . push ( {
291314 code : SERVICE_AUDIT_CODES . gatewayPortMismatch ,
292315 message : "Gateway service port does not match current gateway config." ,
293- detail : ` ${ servicePort } -> ${ params . expectedPort } ` ,
316+ detail,
294317 level : "recommended" ,
295318 } ) ;
296319}
0 commit comments