-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
229 lines (185 loc) · 5.91 KB
/
index.js
File metadata and controls
229 lines (185 loc) · 5.91 KB
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
"use strict";
const { createLogger } = require("@xen-orchestra/log");
const { pipeline } = require("node:stream");
const assert = require("node:assert/strict");
const http = require("node:http");
const https = require("node:https");
const readStream = require("./_readStream.js");
function buffer() {
return new Promise(readStream.bind(this));
}
function json() {
return new Promise(readStream.bind(this)).then(JSON.parse);
}
function text() {
return new Promise(readStream.bind(this)).then(String);
}
const stack = [
function doRequest({ body, ...opts }) {
const { debug, url } = this;
const hasBody = body !== undefined;
let bodyIsStream = false;
if (hasBody) {
bodyIsStream = typeof body.pipe === "function";
const { headers } = opts;
if (headers["content-length"] === undefined) {
const length = bodyIsStream
? body.headers?.["content-length"] ?? body.length
: Buffer.byteLength(body);
if (length !== undefined) {
opts.headers["content-length"] = length;
}
}
}
const isSecure = url.protocol === "https:";
if (!isSecure) {
assert.equal(url.protocol, "http:");
}
debug("sending request", { url: url.href, hasBody, bodyIsStream, opts });
const req = (isSecure ? https.request : http.request)(url, opts);
return new Promise((resolve, reject) => {
let _sendError = reject;
const sendError = (error) => {
_sendError(error);
};
req
.on("error", sendError)
.on("timeout", () => {
const error = new Error("HTTP connection has timed out");
error.url = url.href;
req.destroy(error);
})
.on("response", (response) => {
const { headers, statusCode, statusMessage } = response;
debug("response received", { headers, statusCode, statusMessage });
response.buffer = buffer;
// augment response with classic helpers (similar to standard fetch())
response.json = json;
response.text = text;
_sendError = (error) => response.destroy(error);
resolve(response);
});
// if `Expect: 100-continue`, wait before sending the body
const sendBody = bodyIsStream
? () => {
// avoid duplicate error handling on req
req.off("error", sendError);
pipeline(body, req, (error) => {
if (error != null) {
sendError(error);
}
});
}
: () => req.end(body);
if (opts.headers.expect === "100-continue") {
req.on("continue", sendBody);
} else {
sendBody();
}
});
},
async function handleRedirects({ maxRedirects = 5, ...opts }, next) {
const { debug } = this;
while (true) {
const response = await next(opts);
const { statusCode } = response;
if (maxRedirects > 0 && ((statusCode / 100) | 0) === 3) {
--maxRedirects;
const { location } = response.headers;
if (location !== undefined) {
debug("redirection", { location });
response.req.destroy();
this.url = new URL(location, this.url);
// This implementation does not change method/body if 302
//
// 307 and 308 requires that method/body stay unchanged
if (
!(statusCode === 302 || statusCode === 307 || statusCode === 308)
) {
if (opts.method !== "GET") {
debug("changing method to GET");
opts.method = "GET";
}
const { body } = opts;
if (body !== undefined) {
debug("removing body");
const { headers } = opts;
if (headers["content-length"]) {
delete headers["content-length"];
}
if (typeof body.destroy === "function") {
body.destroy();
}
delete opts.body;
}
}
continue;
}
}
return response;
}
},
async function assertSuccess({ bypassStatusCheck = false, ...opts }, next) {
const response = await next(opts);
if (bypassStatusCheck) {
this.debug("bypassing status check");
return response;
}
const { statusCode } = response;
if (((statusCode / 100) | 0) === 2) {
return response;
}
const error = new Error(`${response.statusCode} ${response.statusMessage}`);
Object.defineProperty(error, "response", { value: response });
throw error;
},
];
function runStack(i, ...args) {
assert(i >= 0);
assert(i < stack.length);
return stack[i].call(this, ...args, (...args) =>
runStack.call(this, i - 1, ...args)
);
}
module.exports = async function httpRequestPlus(url, opts) {
url = url instanceof URL ? url : new URL(url);
const { debug } = createLogger(
"http-request-plus:" +
url.hostname +
url.pathname +
":" +
Math.random().toString(36).slice(2, 6)
);
const ctx = { debug, url };
opts = {
__proto__: null,
...opts,
};
const { headers, method } = opts;
// normalize headers: clone (to avoid mutate user object) and lowercase
const normalizedHeaders = { __proto__: null };
if (headers !== undefined) {
for (const key of Object.keys(headers)) {
const lcKey = key.toLowerCase();
assert.equal(
normalizedHeaders[lcKey],
undefined,
"duplicate header " + key
);
normalizedHeaders[lcKey] = headers[key];
}
}
opts.headers = normalizedHeaders;
// normalize method: default value and upper case
opts.method = method === undefined ? "GET" : method.toUpperCase();
try {
const response = await runStack.call(ctx, stack.length - 1, opts);
return response;
} catch (error) {
// augment error with useful info
error.originalUrl = url.href;
error.url = ctx.url.href;
debug(error);
throw error;
}
};