From 84af2d54fe24620a34ab611c5d4a824ab64bd644 Mon Sep 17 00:00:00 2001 From: Matt Lavin Date: Thu, 18 May 2023 17:18:04 -0400 Subject: [PATCH] Allow specifying alternate bin and cache directories when downloading --- download.d.ts | 12 +++++++----- download.js | 24 ++++++++++++++---------- index.d.ts | 4 ++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/download.d.ts b/download.d.ts index 0ee3075..6c66dea 100644 --- a/download.d.ts +++ b/download.d.ts @@ -2,11 +2,13 @@ declare module "ngrok/download" { export default function downloadNgrok( callback: (err?: Error) => void, options?: { - cafilePath: string; - arch: string; - cdnUrl: string; - cdnPath: string; - ignoreCache: boolean; + cafilePath?: string; + arch?: string; + cdnUrl?: string; + cdnPath?: string; + ignoreCache?: boolean; + cacheDir?: string; + binDir?: string; } ): void; } \ No newline at end of file diff --git a/download.js b/download.js index ea8df19..23b3083 100644 --- a/download.js +++ b/download.js @@ -61,16 +61,20 @@ function downloadNgrok(callback, options) { function getCacheUrl() { let dir; - try { - dir = - os.platform() === "win32" && process.env.APPDATA - ? path.join(process.env.APPDATA, "ngrok") - : path.join(os.homedir(), ".ngrok"); - if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) { - fs.mkdirSync(dir); + + if (options.cacheDir) dir = options.cacheDir; + else { + try { + dir = + os.platform() === "win32" && process.env.APPDATA + ? path.join(process.env.APPDATA, "ngrok") + : path.join(os.homedir(), ".ngrok"); + if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) { + fs.mkdirSync(dir); + } + } catch (err) { + dir = path.join(__dirname, "bin"); } - } catch (err) { - dir = path.join(__dirname, "bin"); } const name = Buffer.from(cdnUrl).toString("base64"); return path.join(dir, name + ".zip"); @@ -147,7 +151,7 @@ function downloadNgrok(callback, options) { function extract(cb) { console.error("ngrok - unpacking binary"); - const moduleBinPath = path.join(__dirname, "bin"); + const moduleBinPath = options.binDir || path.join(__dirname, "bin"); extract_zip(cacheUrl, { dir: moduleBinPath }) .then(() => { const suffix = os.platform() === "win32" ? ".exe" : ""; diff --git a/index.d.ts b/index.d.ts index 0da0acc..065461f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -179,7 +179,7 @@ declare module "ngrok" { gauge: number; } - interface HTTPRequests extends Metrics {} + interface HTTPRequests extends Metrics { } interface Tunnel { name: string; @@ -203,7 +203,7 @@ declare module "ngrok" { interface CapturedRequestOptions { limit: number; - tunnel_name: string; + tunnel_name?: string; } interface Request {