-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·140 lines (138 loc) · 3.68 KB
/
index.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
#!/usr/bin/env node
import { ProgressLogger } from "progress-logger-js";
import fetch from "node-fetch";
import fs from "fs";
import meow from "meow";
const progress = new ProgressLogger({
label: "infinite-wget",
logInterval: 1000
});
const cli = meow(`
Usage
$ infinite-wget <url>
Options
--parallelism, -p Parallel calls, default 1
--sleep, -s Sleep ms, default 0
--method, -m HTTP method, default GET
--logResponse, -l Log HTTP response, default false
--body, -b Body to send, default to no body
--header, -h Headers in form "key=value", default to no headers
Examples
$ infinite-wget http://httpbin.org/get -p 2
$ infinite-wget http://httpbin.org/post -l -m POST -b ./my-body.txt
`, {
importMeta: import.meta,
flags: {
parallelism: {
type: 'string',
alias: 'p',
default: '1'
},
sleep: {
type: 'string',
alias: 's',
default: '0'
},
method: {
type: 'string',
alias: 'm',
default: 'GET'
},
logResponse: {
type: 'boolean',
alias: 'l',
default: false
},
body: {
type: 'string',
alias: 'b',
isRequired: false
},
header: {
type: 'string',
alias: 'h',
default: ''
}
}
});
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(() => resolve(""), ms);
});
}
async function wget(wgetUrl, fetchOptions, logResponse) {
try {
const fetchResponse = await fetch(wgetUrl, fetchOptions);
if (!fetchResponse.ok) {
throw new Error("Invalid response " + fetchResponse.status);
}
const response = await fetchResponse.buffer();
if (logResponse) {
console.log(response.toString());
}
}
catch (err) {
console.error(err);
throw err;
}
}
async function runTask(wgetUrl, options) {
const headers = {};
for (const h of options.headers) {
const hK = h.split("=")[0];
const hv = h.split("=")[1] || "";
headers[hK] = hv;
}
const fetchOptions = {
method: options.method,
body: options.body,
headers
};
while (true) {
await progress.incrementPromise(wget(wgetUrl, fetchOptions, options.logResponse));
if (options.sleep > 0) {
await sleep(options.sleep);
}
}
}
async function run(wgetUrl, options) {
if (!wgetUrl) {
throw new Error("url not provided");
}
const pSleep = parseInt(options.sleep, 10);
if (isNaN(pSleep)) {
throw new Error("Invalid sleep parameter");
}
const pParallelism = parseInt(options.parallelism, 10);
if (isNaN(pParallelism)) {
throw new Error("Invalid parallelism parameter");
}
const pBody = options.body && fs.readFileSync(options.body);
let pHeaders = [];
if (Array.isArray(options.header)) {
pHeaders = options.header;
}
else if (options.header) {
pHeaders = [options.header];
}
const optionsParser = {
sleep: pSleep,
parallelism: pParallelism,
method: options.method,
logResponse: options.logResponse,
body: pBody,
headers: pHeaders
};
const tasks = Array.from(Array(optionsParser.parallelism))
.map(() => runTask(wgetUrl, optionsParser));
return tasks;
}
run(cli.input[0], cli.flags)
.catch(console.error.bind(console));
process.on('SIGINT', function () {
progress.end();
for (const err of progress.stats().errors) {
console.log(err);
}
process.exit();
});