diff --git a/__test__/connect.spec.mjs b/__test__/connect.spec.mjs index 99ab0d5..f50cafd 100644 --- a/__test__/connect.spec.mjs +++ b/__test__/connect.spec.mjs @@ -142,7 +142,7 @@ test("forward number", async (t) => { await validateShutdown(t, httpServer, url); }); -test("forward port string", async (t) => { +test("forward just string as port", async (t) => { ngrok.consoleLog(); const httpServer = await makeHttp(); ngrok.authtoken(process.env["NGROK_AUTHTOKEN"]); @@ -166,6 +166,18 @@ test("forward addr port string", async (t) => { await validateShutdown(t, httpServer, url); }); +test("forward port string", async (t) => { + ngrok.consoleLog(); + const httpServer = await makeHttp(); + ngrok.authtoken(process.env["NGROK_AUTHTOKEN"]); + const listener = await ngrok.forward({ port: httpServer.listenTo.split(":")[1] }); + const url = listener.url(); + + t.truthy(url); + t.truthy(url.startsWith("https://"), url); + await validateShutdown(t, httpServer, url); +}); + test("forward string", async (t) => { const httpServer = await makeHttp(); ngrok.authtoken(process.env["NGROK_AUTHTOKEN"]); diff --git a/index.js b/index.js index 7954a77..2737613 100644 --- a/index.js +++ b/index.js @@ -545,6 +545,13 @@ async function ngrokForward(config) { } config = { addr: address }; } + if (typeof config["port"] === "string" || config["port"] instanceof String) { + const num = parseInt(config["port"]); + if (isNaN(num)) { + throw new Error(`port must be a number: '${config["port"]}'`); + } + config["port"] = num; + } // Convert addr to string to allow for numeric port numbers const addr = config["addr"]; if (Number.isInteger(addr)) config["addr"] = "localhost:" + String(config["addr"]); diff --git a/trailer.js b/trailer.js index d98180d..e3f7df8 100644 --- a/trailer.js +++ b/trailer.js @@ -272,6 +272,13 @@ async function ngrokForward(config) { } config = { addr: address }; } + if (typeof config["port"] === "string" || config["port"] instanceof String) { + const num = parseInt(config["port"]); + if (isNaN(num)) { + throw new Error(`port must be a number: '${config["port"]}'`); + } + config["port"] = num; + } // Convert addr to string to allow for numeric port numbers const addr = config["addr"]; if (Number.isInteger(addr)) config["addr"] = "localhost:" + String(config["addr"]);