Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: drop is-online #216

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"no-unused-vars": [
1,
{
"argsIgnorePattern": "^_"
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
]
}
Expand Down
43 changes: 40 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import xurl from 'url';
import xpath from 'path';
import crypto from 'crypto';
import {setTimeout} from 'timers/promises';
import {spawn, spawnSync} from 'child_process';

import Conf from 'conf';
Expand All @@ -16,15 +17,15 @@ import mkdirp from 'mkdirp';
import xbytes from 'xbytes';
import Promise from 'bluebird';
import cStringd from 'stringd-colors';
import isOnline from 'is-online';
import prettyMs from 'pretty-ms';
import {program as commander} from 'commander';
import minimatch from 'minimatch';
import filenamify from 'filenamify';
import TimeFormat from 'hh-mm-ss';
import ProgressBar from 'xprogress';
import countryData from 'country-data';
import {publicIpv4} from 'public-ip';
import {isBinaryFile} from 'isbinaryfile';
import {program as commander} from 'commander';
import {decode as entityDecode} from 'html-entities';

import symbols from './src/symbols.js';
Expand All @@ -40,6 +41,43 @@ import parseSearchFilter from './src/filter_parser.js';

const __dirname = xurl.fileURLToPath(new URL('.', import.meta.url));

async function pTimeout(timeout, fn) {
let timeoutSignal = Symbol('TimedOutSignal');
let f = fn();
let result = await Promise.race([f, setTimeout(timeout, timeoutSignal)]);
if (result == timeoutSignal) {
if (typeof f.cancel == 'function') f.cancel();
throw new Error('Promise timed out');
}
return result;
}

async function pRetry(tries, fn) {
let result;
for (let _ in Array.apply(null, {length: tries})) {
try {
result = await fn();
} catch (err) {
(result = Promise.reject(err)).catch(() => {});
}
}
return result;
}

async function isOnline() {
try {
let _publicIp = await pRetry(2, () =>
pTimeout(2000, async ip => {
if ((ip = await publicIpv4()) == undefined) throw new Error('unable to get public ip');
return ip;
}),
);
return true;
} catch {
return false;
}
}

function parseMeta(params) {
return Object.entries(params || {})
.filter(([, value]) => ![undefined, null].includes(value))
Expand Down Expand Up @@ -1355,7 +1393,6 @@ async function init(packageJson, queries, options) {
stackLogger.log(` [\u2022] Output bitrate: ${options.bitrate}`);
stackLogger.log('===============================');
}
setTimeout(process.exit, 1000);
}

function prepCli(packageJson) {
Expand Down
156 changes: 36 additions & 120 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"got": "^12.1.0",
"hh-mm-ss": "^1.2.0",
"html-entities": "^2.3.3",
"is-online": "^10.0.0",
"isbinaryfile": "^5.0.0",
"libxget": "^0.9.2",
"lodash": "^4.17.21",
Expand All @@ -78,6 +77,7 @@
"node-cache": "^5.1.2",
"open": "^8.4.0",
"pretty-ms": "^8.0.0",
"public-ip": "^6.0.1",
"spotify-uri": "^3.0.2",
"spotify-web-api-node": "^5.0.2",
"stringd": "^2.1.0",
Expand Down
Loading