From 99f498c232553d6494a76de0824f289bc543841f Mon Sep 17 00:00:00 2001 From: minsiam <80940731+minsiam@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:39:27 +0600 Subject: [PATCH] Fix a bug where changing the authtoken would not work when app is running (#322) --- index.js | 9 +++++++++ src/authtoken.js | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/index.js b/index.js index c47f1f0..300a6bb 100644 --- a/index.js +++ b/index.js @@ -29,10 +29,19 @@ let ngrokClient = null; * @param {Object | string} opts * @returns Promise */ + +let authtoken; + async function connect(opts) { const { tunnelOpts, globalOpts } = defaults(opts); validate(globalOpts); + if (globalOpts.authtoken) { + if (authtoken !== globalOpts.authtoken) { + await kill(); + } + authtoken = globalOpts.authtoken; + } processUrl = await getProcess(globalOpts); ngrokClient = new NgrokClient(processUrl); return connectRetry(tunnelOpts); diff --git a/src/authtoken.js b/src/authtoken.js index 5ce5923..dc71056 100644 --- a/src/authtoken.js +++ b/src/authtoken.js @@ -3,6 +3,10 @@ const { join } = require("path"); const { defaultDir, bin } = require("./constants"); +const { killProcess } = require('./process'); + +let authtoken; + function consolidateTokenAndOpts(optsOrToken) { const isOpts = typeof optsOrToken !== "string"; const opts = isOpts ? optsOrToken : {}; @@ -19,6 +23,7 @@ async function setAuthtoken(optsOrToken) { command.push("--config=" + opts.configPath); } + let dir = defaultDir; if (opts.binPath) { dir = opts.binPath(dir); @@ -32,6 +37,13 @@ async function setAuthtoken(optsOrToken) { process.stderr.once("data", () => reject(new Error("cant set authtoken"))); }); + if (opts.authtoken) { + if (authtoken !== opts.authtoken) { + await killProcess(); + } + authtoken = opts.authtoken; + } + try { return await killed; } finally {