This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wget-fromscratch.js
69 lines (62 loc) · 2.11 KB
/
wget-fromscratch.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
const events = require('events');
class wget {
static download(url3, output) {
// const https = require("https");
const fs = require("fs");
// const url = require("url");
// const path = require("path");
const url2 = url3;
const myWgetInstance = new events.EventEmitter();
// const file_name = path.basename(url.parse(url2).pathname);
const file_name = output;
const writeStream = fs.createWriteStream(file_name);
// require("https").get(url2, (res) => {
// res.statusCode
// });
const request = adapterFor(url2).get(url2, (res) => {
res.pipe(writeStream);
let error = false;
if (res.statusCode == 404) {
// request.emit("error", 404);
fs.unlink(file_name, () => {
myWgetInstance.emit("error", 404);
});
error = true;
return;
}
writeStream.on("finish", function () {
writeStream.close(() => {
if (!error)
myWgetInstance.emit("end");
else
myWgetInstance.emit("error", 404);
});
});
});
// check for request error too
request.on('error', (err) => {
fs.unlink(file_name, () => {
myWgetInstance.emit("error", err);
});
// delete the (partial) file and then return the error
});
writeStream.on('error', (err) => {
// Handle errors
myWgetInstance.emit("fs_error", err);
// delete the (partial) file and then return the error
});
return myWgetInstance;
}
}
// https://stackoverflow.com/a/38465918
const adapterFor = (function () {
const url = require('url'),
adapters = {
'http:': require('http'),
'https:': require('https'),
};
return function (inputUrl) {
return adapters[url.parse(inputUrl).protocol];
};
}());
exports.download = wget.download;