@util.js/node-http 0.41.4 → 0.41.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Urls.js +19 -9
- package/package.json +3 -4
- package/test/UrlsTest.js +38 -11
package/lib/Urls.js
CHANGED
|
@@ -61,8 +61,8 @@ class Urls {
|
|
|
61
61
|
const port = parsedUrl.port
|
|
62
62
|
? parsedUrl.port
|
|
63
63
|
: parsedUrl.protocol === "https:"
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
? 443
|
|
65
|
+
: 80;
|
|
66
66
|
|
|
67
67
|
let options = Object.assign(
|
|
68
68
|
{},
|
|
@@ -70,7 +70,7 @@ class Urls {
|
|
|
70
70
|
? typeof callbackOrOptions === "function"
|
|
71
71
|
? {}
|
|
72
72
|
: callbackOrOptions
|
|
73
|
-
: {}
|
|
73
|
+
: {},
|
|
74
74
|
);
|
|
75
75
|
options = Object.assign(options, {
|
|
76
76
|
host: parsedUrl.host,
|
|
@@ -78,22 +78,32 @@ class Urls {
|
|
|
78
78
|
path: parsedUrl.path,
|
|
79
79
|
port,
|
|
80
80
|
});
|
|
81
|
+
let cbCalled = false;
|
|
81
82
|
const request = getProtocolObject(parsedUrl.protocol)
|
|
82
|
-
.request(options, (res) =>
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
.request(options, (res) => {
|
|
84
|
+
res.destroy();
|
|
85
|
+
if (!cbCalled) {
|
|
86
|
+
cbCalled = true;
|
|
87
|
+
cb(null, res.headers);
|
|
88
|
+
}
|
|
85
89
|
})
|
|
86
|
-
.
|
|
90
|
+
.on("error", (error) => {
|
|
91
|
+
if (!cbCalled) {
|
|
92
|
+
cbCalled = true;
|
|
93
|
+
cb(error);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
87
96
|
const timeout = determineTimeout(callbackOrOptions);
|
|
88
97
|
if (timeout !== -1) {
|
|
89
98
|
// https://stackoverflow.com/a/12783062
|
|
90
99
|
request.on("socket", (socket) => {
|
|
91
100
|
socket.setTimeout(timeout);
|
|
92
101
|
socket.on("timeout", () => {
|
|
93
|
-
request.
|
|
102
|
+
request.destroy(new Error("The request timed out."));
|
|
94
103
|
});
|
|
95
104
|
});
|
|
96
105
|
}
|
|
106
|
+
request.end();
|
|
97
107
|
}
|
|
98
108
|
}
|
|
99
109
|
|
|
@@ -138,7 +148,7 @@ function getProtocolObject(protocolStr) {
|
|
|
138
148
|
return http();
|
|
139
149
|
default:
|
|
140
150
|
throw new TypeError(
|
|
141
|
-
'The protocol must be "https:" or "http:", not "' + protocolStr + '"'
|
|
151
|
+
'The protocol must be "https:" or "http:", not "' + protocolStr + '"',
|
|
142
152
|
);
|
|
143
153
|
}
|
|
144
154
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Chris Topher <c@creemama.com>",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@util.js/node-files": "0.41.
|
|
5
|
-
"@util.js/promises": "0.41.
|
|
4
|
+
"@util.js/node-files": "0.41.5",
|
|
5
|
+
"@util.js/promises": "0.41.5"
|
|
6
6
|
},
|
|
7
7
|
"description": "JavaScript utility methods for URLs",
|
|
8
8
|
"keywords": [
|
|
@@ -27,6 +27,5 @@
|
|
|
27
27
|
"type": "git",
|
|
28
28
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-node-http"
|
|
29
29
|
},
|
|
30
|
-
"version": "0.41.
|
|
31
|
-
"gitHead": "2119863a175cf7095ca3a2417e61af32974add15"
|
|
30
|
+
"version": "0.41.5"
|
|
32
31
|
}
|
package/test/UrlsTest.js
CHANGED
|
@@ -43,7 +43,7 @@ describe("Urls", () => {
|
|
|
43
43
|
},
|
|
44
44
|
(error) => {
|
|
45
45
|
expect(error).to.be.an.instanceof(TypeError);
|
|
46
|
-
}
|
|
46
|
+
},
|
|
47
47
|
));
|
|
48
48
|
it("should throw an error if there is no protocol", () =>
|
|
49
49
|
downloadUrlDestination("www.google.com").then(
|
|
@@ -52,7 +52,7 @@ describe("Urls", () => {
|
|
|
52
52
|
},
|
|
53
53
|
(error) => {
|
|
54
54
|
expect(error).to.be.an.instanceof(TypeError);
|
|
55
|
-
}
|
|
55
|
+
},
|
|
56
56
|
));
|
|
57
57
|
it("handles HTTP errors", () =>
|
|
58
58
|
downloadUrlDestination("http://doesnotexist:8080", {
|
|
@@ -64,7 +64,7 @@ describe("Urls", () => {
|
|
|
64
64
|
(error) => {
|
|
65
65
|
if (!error) throw new Error("Unexpected");
|
|
66
66
|
/* expected */
|
|
67
|
-
}
|
|
67
|
+
},
|
|
68
68
|
));
|
|
69
69
|
it("handles HTTPS errors", () =>
|
|
70
70
|
downloadUrlDestination("https://doesnotexist:8443", {
|
|
@@ -76,7 +76,7 @@ describe("Urls", () => {
|
|
|
76
76
|
(error) => {
|
|
77
77
|
if (!error) throw new Error("Unexpected");
|
|
78
78
|
/* expected */
|
|
79
|
-
}
|
|
79
|
+
},
|
|
80
80
|
));
|
|
81
81
|
});
|
|
82
82
|
|
|
@@ -92,7 +92,7 @@ describe("Urls", () => {
|
|
|
92
92
|
(error) => {
|
|
93
93
|
if (error) return callback();
|
|
94
94
|
callback(new Error("Unexpected success"));
|
|
95
|
-
}
|
|
95
|
+
},
|
|
96
96
|
));
|
|
97
97
|
it("handles HTTPS errors", (callback) =>
|
|
98
98
|
downloadUrlDestinationCallback(
|
|
@@ -101,7 +101,7 @@ describe("Urls", () => {
|
|
|
101
101
|
(error) => {
|
|
102
102
|
if (error) return callback();
|
|
103
103
|
callback(new Error("Unexpected success"));
|
|
104
|
-
}
|
|
104
|
+
},
|
|
105
105
|
));
|
|
106
106
|
});
|
|
107
107
|
|
|
@@ -118,6 +118,14 @@ describe("Urls", () => {
|
|
|
118
118
|
urls.headers("https://www.google.com", null).then((headers) => {
|
|
119
119
|
expect(headers["content-type"].startsWith("text/html")).to.be.true;
|
|
120
120
|
}));
|
|
121
|
+
it("handles HTTP requests with explicit port", () =>
|
|
122
|
+
urls.headers("http://www.google.com:80").then((headers) => {
|
|
123
|
+
expect(headers["content-type"].startsWith("text/html")).to.be.true;
|
|
124
|
+
}));
|
|
125
|
+
it("handles HTTP requests with empty options object", () =>
|
|
126
|
+
urls.headers("http://www.google.com", {}).then((headers) => {
|
|
127
|
+
expect(headers["content-type"].startsWith("text/html")).to.be.true;
|
|
128
|
+
}));
|
|
121
129
|
it("handles HTTP errors", () =>
|
|
122
130
|
urls.headers("http://doesnotexist:8080", { timeout: 50 }).then(
|
|
123
131
|
(value) => {
|
|
@@ -126,7 +134,7 @@ describe("Urls", () => {
|
|
|
126
134
|
(error) => {
|
|
127
135
|
if (!error) throw new Error("Unexpected");
|
|
128
136
|
/* expected */
|
|
129
|
-
}
|
|
137
|
+
},
|
|
130
138
|
));
|
|
131
139
|
it("handles HTTPS errors", () =>
|
|
132
140
|
urls.headers("https://doesnotexist:8443", { timeout: 50 }).then(
|
|
@@ -136,7 +144,16 @@ describe("Urls", () => {
|
|
|
136
144
|
(error) => {
|
|
137
145
|
if (!error) throw new Error("Unexpected");
|
|
138
146
|
/* expected */
|
|
139
|
-
}
|
|
147
|
+
},
|
|
148
|
+
));
|
|
149
|
+
it("handles request timeouts", () =>
|
|
150
|
+
urls.headers("http://10.255.255.1", { timeout: 10 }).then(
|
|
151
|
+
(value) => {
|
|
152
|
+
throw new Error("Unexpected success");
|
|
153
|
+
},
|
|
154
|
+
(error) => {
|
|
155
|
+
expect(error).to.be.an.instanceof(Error);
|
|
156
|
+
},
|
|
140
157
|
));
|
|
141
158
|
});
|
|
142
159
|
|
|
@@ -151,6 +168,11 @@ describe("Urls", () => {
|
|
|
151
168
|
expect(headers["content-type"].startsWith("text/html")).to.be.true;
|
|
152
169
|
callback(error);
|
|
153
170
|
}));
|
|
171
|
+
it("handles HTTP requests with empty options object", (callback) =>
|
|
172
|
+
urls.headers("http://www.google.com", {}, (error, headers) => {
|
|
173
|
+
expect(headers["content-type"].startsWith("text/html")).to.be.true;
|
|
174
|
+
callback(error);
|
|
175
|
+
}));
|
|
154
176
|
it("handles HTTP errors", (callback) =>
|
|
155
177
|
urls.headers("http://doesnotexist:8080", { timeout: 50 }, (error) => {
|
|
156
178
|
if (error) return callback();
|
|
@@ -161,6 +183,11 @@ describe("Urls", () => {
|
|
|
161
183
|
if (error) return callback();
|
|
162
184
|
callback(new Error("Unexpected success"));
|
|
163
185
|
}));
|
|
186
|
+
it("handles request timeouts", (callback) =>
|
|
187
|
+
urls.headers("http://10.255.255.1", { timeout: 10 }, (error) => {
|
|
188
|
+
if (error) return callback();
|
|
189
|
+
callback(new Error("Unexpected success"));
|
|
190
|
+
}));
|
|
164
191
|
});
|
|
165
192
|
});
|
|
166
193
|
|
|
@@ -171,7 +198,7 @@ async function downloadUrlDestination(url, options) {
|
|
|
171
198
|
"/" +
|
|
172
199
|
files.sanitizeFilename(
|
|
173
200
|
fileNum++ + ".downloadUrlDestination." + url + ".html",
|
|
174
|
-
{ replacement: "_" }
|
|
201
|
+
{ replacement: "_" },
|
|
175
202
|
);
|
|
176
203
|
|
|
177
204
|
const readFile = (_) => files.readFile(destination, "utf8");
|
|
@@ -197,7 +224,7 @@ function downloadUrlDestinationCallback() {
|
|
|
197
224
|
async function asyncDownloadUrlDestinationCallback(
|
|
198
225
|
url,
|
|
199
226
|
callbackOrOptions,
|
|
200
|
-
callback
|
|
227
|
+
callback,
|
|
201
228
|
) {
|
|
202
229
|
await files.mkdirp(targetDir);
|
|
203
230
|
const cb = arguments[arguments.length - 1];
|
|
@@ -206,7 +233,7 @@ async function asyncDownloadUrlDestinationCallback(
|
|
|
206
233
|
"/" +
|
|
207
234
|
files.sanitizeFilename(
|
|
208
235
|
fileNum++ + ".asyncDownloadUrlDestinationCallback." + url + ".html",
|
|
209
|
-
{ replacement: "_" }
|
|
236
|
+
{ replacement: "_" },
|
|
210
237
|
);
|
|
211
238
|
const options = callbackOrOptions
|
|
212
239
|
? typeof callbackOrOptions === "function"
|