-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
169 lines (145 loc) · 3.59 KB
/
index.ts
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
#!/usr/bin/env node
import {ProgressLogger} from "progress-logger-js";
import fetch, { RequestInit } 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: ''
}
}
});
interface MyOptions {
sleep: number;
parallelism: number;
method: string;
logResponse: boolean;
body?: Buffer;
headers: string[];
}
function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(() => resolve(""), ms);
});
}
async function wget(wgetUrl: string, fetchOptions: RequestInit, logResponse: boolean) {
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: string, options: MyOptions) {
const headers: { [index: string]: string } = {};
for (const h of options.headers) {
const hK = h.split("=")[0];
const hv = h.split("=")[1] || "";
headers[hK] = hv;
}
const fetchOptions: RequestInit = {
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: string, options: any) {
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: MyOptions = {
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();
});