-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
·312 lines (290 loc) · 12.7 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env node
import fs from 'fs';
import url from 'url';
import tty from 'tty';
import path from 'path';
import util from 'util';
import xbytes from 'xbytes';
import mime from 'mime-types';
import commander from 'commander';
import cStringd from 'stringd-colors';
import contentType from 'content-type';
import contentDisposition from 'content-disposition';
import xget from './lib/index.js';
import {XgetException} from './lib/xgetception.js';
import ProgressBar, {getPersistentStdout} from 'xprogress';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const [log, error] = [, ,].fill(
(function ninjaLoggers() {
let output;
if (!process.stdout.isTTY && ['linux', 'android', 'darwin'].includes(process.platform))
(output = new tty.WriteStream(fs.openSync('/dev/tty', 'w'))), process.on('beforeExit', () => output.destroy());
else output = process.stdout;
return function ninjaLogger(...args) {
output.write(`${util.format(...args)}\n`);
};
})(),
);
function getRetryMessage({meta, index, retryCount, maxRetries, bytesRead, totalBytes, lastErr}) {
return cStringd(
':{color(red)}{⯈}:{color:close(red)} :{color(cyan)}@:{metaIndex}:{color:close(cyan)}{:{color(yellow)}:{retryCount}:{color:close(yellow)}:{maxRetries}}: :{error}:{bytes}',
{
metaIndex: meta ? 'meta' : index + 1,
retryCount,
maxRetries: Number.isFinite(maxRetries) ? `/:{color(yellow)}${maxRetries}:{color:close(yellow)}` : '',
error: lastErr ? ':{lastErrCode}(:{color(yellow)}:{lastErr}:{color:close(yellow)}) ' : '',
lastErr,
lastErrCode: lastErr && lastErr.code ? `[:{color(yellow)}${lastErr.code}:{color:close(yellow)}] ` : '',
bytes: totalBytes ? '(:{color(cyan)}:{bytesRead}:{color:close(cyan)}:{totalBytes})' : '',
bytesRead: Number.isFinite(totalBytes) ? `${bytesRead}`.padStart(`${totalBytes}`.length, ' ') : bytesRead,
totalBytes: Number.isFinite(totalBytes) ? `/:{color(cyan)}${totalBytes}:{color:close(cyan)}` : '',
},
);
}
function getEndMessage(request) {
return [
`• Download Complete at ${request.bytesRead} (${xbytes(request.bytesRead)})`,
...(request.getHash() ? [`• Hash(${request.getHashAlgorithm()}): ${request.getHash('hex')}`] : []),
];
}
function parseExt(npath, ext) {
const rext = path.extname(npath);
return rext ? npath : path.join(path.dirname(npath), path.basename(npath, rext) + ext);
}
function processArgs(_url, outputFile, options) {
const parsedUrl = new URL(_url);
if (!['protocol', 'hostname'].every(item => parsedUrl[item]))
error('\x1b[31m[i]\x1b[0m Please enter a valid URL'), process.exit(1);
function CHECK_FLAG_VAL(variable, flagref, untype) {
if (![null, undefined].includes(variable) && typeof variable !== untype)
if (!(parseFloat(variable).toString() === variable && parseFloat(variable) >= 0))
throw new XgetException(`\`${flagref}\` if specified, must be given a valid positive \`${untype}\` datatype`);
else variable = parseInt(variable, 10);
return variable;
}
let barWriteStream;
try {
options.tries = CHECK_FLAG_VAL(options.tries === 'inf' ? Infinity : options.tries, '-t, --tries', 'number');
options.chunks = CHECK_FLAG_VAL(options.chunks, '-n, --chunks', 'number');
options.startPos = CHECK_FLAG_VAL(options.startPos, '--start-pos', 'number');
options.timeout = CHECK_FLAG_VAL(options.timeout, '--timeout', 'number');
options.cacheSize = CHECK_FLAG_VAL(options.cacheSize, '--cache-size', 'number');
// options.verbose = options.verbose || false;
options.continue = options.continue || false;
options.singleBar = options.singleBar || false;
options.pulsateBar = options.pulsateBar || false;
if (options.bar && null === (barWriteStream = getPersistentStdout())) options.bar = false;
} catch (er) {
error('\x1b[31m[i]\x1b[0m', er.message);
process.exit(1);
}
log(`URL:`, _url);
const opts = {
chunks: options.chunks,
hash: options.hash === true ? 'md5' : options.hash,
timeout: options.timeout,
start: options.startPos,
retries: options.tries,
auto: false,
cacheSize: 'cacheSize' in options ? options.cacheSize : 209715200,
cache: options.cache,
};
const request = xget(_url, opts);
if (options.bar)
request
.with('progressBar', ({size, chunkStack}) =>
ProgressBar.stream(
size,
chunkStack.map(chunk => chunk.size),
{
barWriteStream,
label: outputFile || '<stdout>',
forceFirst: options.singleBar || chunkStack.length > 20,
length: 40,
pulsate: options.pulsateBar || !Number.isFinite(size),
bar: {separator: '|', header: ''},
template: ['[:{label}]', ':{bars}'],
variables: {
bars: ({total}) =>
(Number.isFinite(total) && !options.singleBar && chunkStack.length > 1 && chunkStack.length < 20
? [' •|:{bar:complete}| [:3{percentage}%] [:{speed}] (:{eta})', ' •[:{bar}] [:{size}]']
: [` •|:{bar}|${Number.isFinite(total) ? ' [:3{percentage}%]' : ''} [:{speed}] (:{eta}) [:{size}]`]
).join('\n'),
size: (stack, _size, total) => (
(total = stack.total), `${stack.size()}${total !== Infinity ? `/:{size:total}` : ''}`
),
},
},
),
)
.use('progressBar', (dataSlice, store) => store.get('progressBar').next(dataSlice.size));
request
.on('retry', data => {
if (request.store.has('progressBar')) data.store.get('progressBar').print(getRetryMessage(data));
else log(getRetryMessage(data));
})
.on('end', () => {
const message = getEndMessage(request);
if (request.store.has('progressBar')) request.store.get('progressBar').end(message.concat('').join('\n'));
else log(message.join('\n'));
})
.on('error', err => {
const message = cStringd(`:{color(red)}[!]:{color:close} :{error}`, {
error: 'index' in err ? 'An error occurred [:{errMessage}]' : ':{errMessage}',
errMessage: err && (err.message || err.stack),
});
if (request.store.has('progressBar')) request.store.get('progressBar').end(message, '\n');
else error(message);
});
function hasPermissions(file, mode) {
try {
fs.accessSync(file, mode);
} catch {
return false;
}
return true;
}
function ensureWritableFile(filename) {
const dirName = path.dirname(filename);
if (!fs.existsSync(dirName)) throw new Error(`No such file or directory: ${dirName}`);
if (!hasPermissions(dirName, fs.constants.R_OK)) throw new Error(`Permission denied: ${dirName}`);
if (fs.existsSync(filename) && !hasPermissions(filename, fs.constants.W_OK))
throw new Error(`Permission denied: ${filename}`);
}
request.setHeadHandler(({headers, acceptsRanges, start, chunks, totalSize}) => {
const {type} = contentType.parse(headers['content-type'] || 'application/octet-stream');
const ext = mime.extension(type);
const {filename} = headers['content-disposition'] ? contentDisposition.parse(headers['content-disposition']).parameters : {};
let [offset, isSameResumeFile, outputFileExists, outputFileStat] = [start, false, , ,];
outputFile = process.stdout.isTTY
? (_path =>
path.join(
options.directoryPrefix || (path.isAbsolute(_path) ? '/' : '.'),
!options.directories ? path.basename(_path) : _path,
))(
outputFile ||
decodeURIComponent(
filename ||
parseExt(
parsedUrl.pathname && parsedUrl.pathname === '/' ? `index` : path.basename(parsedUrl.pathname),
`.${ext}` || '.html',
),
),
)
: null;
if (outputFile) ensureWritableFile(outputFile);
if ((outputFileExists = fs.existsSync(outputFile))) outputFileStat = fs.statSync(outputFile);
if (options.continue) {
if (options.overwrite) {
error(
cStringd(
`:{color(red)}[i]:{color:close(red)} :{color(cyan)}\`--continue\`:{color:close(cyan)} and :{color(cyan)}\`--overwrite\`:{color:close(cyan)} cannot be used together; exiting...`,
),
);
process.exit();
}
const resumeFile = options.continue === true ? outputFile : options.continue;
if (fs.existsSync(resumeFile)) {
if (resumeFile) ensureWritableFile(resumeFile);
const resumeFileStat = fs.statSync(resumeFile);
({size: offset} = resumeFileStat);
log(cStringd(`:{color(yellow)}[i]:{color:close(yellow)} Attempting to resume file ${resumeFile} at ${offset}`));
isSameResumeFile = outputFileExists && resumeFileStat.ino === outputFileStat.ino;
if (!acceptsRanges)
log(
cStringd(
":{color(yellow)}[i]:{color:close(yellow)} Server doesn't support byteRanges. :{color(cyan)}`--continue`:{color:close(cyan)} ignored",
),
);
} else {
error(
cStringd(
':{color(red)}[i]:{color:close(red)} :{color(cyan)}`--continue`:{color:close(cyan)}: No such file or directory: :{resumeFile}',
{resumeFile},
),
);
process.exit();
}
}
if (!acceptsRanges && options.startPos > 0)
log(
cStringd(
":{color(yellow)}[i]:{color:close(yellow)} Server doesn't support byteRanges. :{color(cyan)}`--start-pos`:{color:close(cyan)} ignored.",
),
);
const hasOffset = offset !== undefined && acceptsRanges;
log(`Chunks: ${chunks}`);
log(
`Length: ${
Number.isFinite(totalSize)
? `${hasOffset ? `${totalSize - offset}/` : ''}${totalSize} (${
hasOffset ? `${xbytes(totalSize - offset)}/` : ''
}${xbytes(totalSize)})`
: 'unspecified'
} ${type ? `[${type}]` : ''}`,
);
log(`Saving to: ‘${outputFile || '<stdout>'}’...`);
if (totalSize - offset <= 0) {
log(cStringd(`:{color(green)}[i]:{color:close(green)} The file is already fully retrieved; exiting...`));
process.exit();
}
if (offset === undefined && outputFileExists && outputFileStat.isFile())
if (!options.overwrite) {
error(cStringd(':{color(red)}[!]:{color:close(red)} File exists. Use `--overwrite` to overwrite'));
process.exit();
} else log(cStringd(':{color(yellow)}[i]:{color:close(yellow)} File exists. Overwriting...'));
request.pipe(
outputFile
? fs.createWriteStream(outputFile, {flags: (hasOffset && isSameResumeFile) || options.forceAppend ? 'a' : 'w'})
: process.stdout,
);
return offset;
});
request.start();
}
let packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json')).toString());
const command = commander
.name('xget')
.usage('[options] <url> [outputFile]')
.arguments('<url> [outputFile]')
.description(packageJson.description)
.option('-n, --chunks <N>', 'maximum number of concurrent chunk connections', 5)
.option('-c, --continue [FILE]', `resume getting a partially downloaded file`)
.option('-i, --start-pos <OFFSET>', 'start downloading from zero-based position OFFSET', 0)
.option('-t, --tries <N>', 'set number of retries for each chunk to N. `inf` for infinite', 5)
.option('-s, --hash [ALGORITHM]', 'calculate hash sum for the requested content using the specified algorithm (default: md5)')
.option('-D, --directory-prefix <PREFIX>', 'save files to PREFIX/..')
.option('-f, --overwrite', 'forcefully overwrite existing files')
.option('--timeout <N>', 'network inactivity timeout (ms)', 10000)
.option('--no-cache', 'disable in-memory caching')
.option('--cache-size <BYTES>', 'max memory capacity for the streaming process (default: 209715200 (200 MiB))')
.option('--force-append', 'whether or not to force append the downloaded content to the output file')
.option('--no-directories', "don't create directories")
.option('--no-bar', "don't show the ProgressBar")
.option('--pulsate-bar', 'show a pulsating bar')
.option(
'--single-bar',
[
'show a single bar for the download, hide chunk-view',
'(default when number of chunks/segments exceed printable space)',
].join('\n'),
)
// .option('-q, --quiet', 'be stealth')
// .option('-v, --verbose', 'be verbose')
.version(`v${packageJson.version}`, '-v, --version')
// Add header config
// Authentication
// POST Request
// Proxies
// Cookiefile
.action(processArgs);
function main(argv) {
if (!argv.includes('-V')) {
const credits = `libxget v${packageJson.version} - (c) ${packageJson.author.name} <${packageJson.author.email}>`;
log(credits);
log('-'.repeat(credits.length));
if (!argv.slice(2).filter(v => v !== '-').length) commander.outputHelp();
}
command.parse(argv);
}
main(process.argv);