renew-ip
Version:
A module for renewing 4G/LTE IP addresses on tethered Android phones from Node JS.
1 lines • 2.73 kB
Source Map (JSON)
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;AAAA,iCAAuD;AACvD,kEAAgC;AAEhC,uCAA+B;AAG/B;;;GAGG;AACU,QAAA,KAAK,GAAa,aAAY,CAAC,UAAU,CAAC,CAAA;AAEvD;;;;;GAKG;AACI,MAAM,cAAc,GAAG,KAAK,EAAE,EAAU,EAAiB,EAAE;IAChE,aAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAA;IAC3C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,UAAU,CAAC,GAAG,EAAE;YACd,OAAO,EAAE,CAAA;QACX,CAAC,EAAE,EAAE,CAAC,CAAA;IACR,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAPY,QAAA,cAAc,kBAO1B;AAED;;;;;GAKG;AACI,MAAM,aAAa,GAAG,KAAK,EAChC,cAAsB,aAAG,CAAC,aAAa,EACrB,EAAE;IACpB,IAAI,aAAa,GAAY,KAAK,CAAA;IAClC,IAAI,OAAO,GAAW,CAAC,CAAA;IACvB,OAAO,CAAC,aAAa,IAAI,OAAO,GAAG,WAAW,EAAE;QAC9C,aAAK,CAAC,sCAAsC,OAAO,KAAK,CAAC,CAAA;QACzD,MAAM,sBAAc,CAAC,aAAG,CAAC,YAAY,CAAC,CAAA;QACtC,aAAa,GAAG,MAAM,mBAAQ,EAAE,CAAA;QAChC,OAAO,EAAE,CAAA;KACV;IACD,aAAK,CAAC,sBAAsB,OAAO,YAAY,EAAE,aAAa,CAAC,CAAA;IAC/D,OAAO,aAAa,CAAA;AACtB,CAAC,CAAA;AAbY,QAAA,aAAa,iBAazB;AAED;;;;;GAKG;AACI,MAAM,SAAS,GAAG,CAAC,OAAuB,EAAsB,EAAE;IACvE,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,EAAE,CAAA;AAChD,CAAC,CAAA;AAFY,QAAA,SAAS,aAErB","sourcesContent":["import { debug as debugFactory, Debugger } from \"debug\"\nimport isOnline from \"is-online\"\n\nimport { env } from \"./schemas\"\nimport { IpHistory } from \"./types\"\n\n/**\n * Export a debug module for the package.\n * @type {debug.Debugger}\n */\nexport const debug: Debugger = debugFactory(\"renew-ip\")\n\n/**\n * Pauses for a specified duration.\n *\n * @param {number} ms\n * @return {Promise<void>}\n */\nexport const waitForTimeout = async (ms: number): Promise<void> => {\n debug(`Waiting for timeout after ${ms}ms.`)\n return new Promise((resolve) => {\n setTimeout(() => {\n resolve()\n }, ms)\n })\n}\n\n/**\n * Slower devices can take a while to reconnect, so loop to try get a connection.\n *\n * @param {number} maxAttempts\n * @return {Promise<boolean>}\n */\nexport const hasConnection = async (\n maxAttempts: number = env.POLL_ATTEMPTS\n): Promise<boolean> => {\n let hasConnection: boolean = false\n let attempt: number = 0\n while (!hasConnection && attempt < maxAttempts) {\n debug(`Polling for connectivity. Attempt #${attempt}...`)\n await waitForTimeout(env.POLL_TIMEOUT)\n hasConnection = await isOnline()\n attempt++\n }\n debug(`Connectivity after ${attempt} attempts:`, hasConnection)\n return hasConnection\n}\n\n/**\n * Pops the last ip history from a history set and attempts to return the ip attribute.\n *\n * @param {Set<IpHistory>} history\n * @return {string | undefined}\n */\nexport const getLastIp = (history: Set<IpHistory>): string | undefined => {\n return [...(history.values() || [])].pop()?.ip\n}\n"]}