@@ -288,19 +288,43 @@ function parseArg(args, flagName) {
288288 return undefined ;
289289} ;
290290
291+ /**
292+ * Parse a valid TCP port from arguments.
293+ * Returns a number in the range 1–65535, or undefined if not provided.
294+ * Throws if the value is present but invalid.
295+ * @param {string[] } args
296+ * @param {string } flagName
297+ * @returns {number | undefined }
298+ */
299+ function parsePortArg ( args , flagName ) {
300+ const raw = parseArg ( args , flagName ) ;
301+ if ( raw === undefined ) {
302+ return undefined ;
303+ }
304+
305+ const port = Number . parseInt ( raw , 10 ) ;
306+ if ( ! Number . isFinite ( port ) || port <= 0 || port > 65535 ) {
307+ throw new Error (
308+ `Invalid value for ${ flagName } : ${ raw } . Expected an integer between 1 and 65535.` ,
309+ ) ;
310+ }
311+
312+ return port ;
313+ }
314+
291315async function main ( ) {
292316 const args = process . argv . slice ( 2 ) ;
293317 const specificUdid = parseArg ( args , '--udid' ) ;
294- const packetStreamBasePort = parseArg ( args , '--packet-stream-base-port' ) ;
295- const tunnelRegistryPort = parseArg ( args , '--tunnel-registry-port' ) ;
318+ const packetStreamBasePort = parsePortArg ( args , '--packet-stream-base-port' ) ;
319+ const tunnelRegistryPort = parsePortArg ( args , '--tunnel-registry-port' ) ;
296320
297321 const tunnelCreator = new TunnelCreator ( ) ;
298322 try {
299323 if ( packetStreamBasePort !== undefined ) {
300- tunnelCreator . packetStreamBasePort = parseInt ( packetStreamBasePort , 10 ) ;
324+ tunnelCreator . packetStreamBasePort = packetStreamBasePort ;
301325 }
302326 if ( tunnelRegistryPort !== undefined ) {
303- tunnelCreator . tunnelRegistryPort = parseInt ( tunnelRegistryPort , 10 ) ;
327+ tunnelCreator . tunnelRegistryPort = tunnelRegistryPort ;
304328 }
305329 const moduleRoot = node . getModuleRootSync ( 'appium-xcuitest-driver' , import . meta. url ) ;
306330 if ( ! moduleRoot ) {
0 commit comments