/
nodeHttpClient.ts
326 lines (291 loc) · 9.44 KB
/
nodeHttpClient.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as http from "http";
import * as https from "https";
import * as zlib from "zlib";
import { Transform } from "stream";
import { AbortController, AbortError } from "@azure/abort-controller";
import {
HttpClient,
PipelineRequest,
PipelineResponse,
TransferProgressEvent,
HttpHeaders,
RequestBodyType
} from "./interfaces";
import { createHttpHeaders } from "./httpHeaders";
import { RestError } from "./restError";
import { URL } from "./util/url";
import { IncomingMessage } from "http";
import { logger } from "./log";
function isReadableStream(body: any): body is NodeJS.ReadableStream {
return body && typeof body.pipe === "function";
}
function isStreamComplete(stream: NodeJS.ReadableStream): Promise<void> {
return new Promise((resolve) => {
stream.on("close", resolve);
stream.on("end", resolve);
stream.on("error", resolve);
});
}
function isArrayBuffer(body: any): body is ArrayBuffer | ArrayBufferView {
return body && typeof body.byteLength === "number";
}
class ReportTransform extends Transform {
private loadedBytes = 0;
private progressCallback: (progress: TransferProgressEvent) => void;
// eslint-disable-next-line @typescript-eslint/ban-types
_transform(chunk: string | Buffer, _encoding: string, callback: Function): void {
this.push(chunk);
this.loadedBytes += chunk.length;
try {
this.progressCallback({ loadedBytes: this.loadedBytes });
callback();
} catch (e) {
callback(e);
}
}
constructor(progressCallback: (progress: TransferProgressEvent) => void) {
super();
this.progressCallback = progressCallback;
}
}
/**
* A HttpClient implementation that uses Node's "https" module to send HTTPS requests.
* @internal
*/
class NodeHttpClient implements HttpClient {
private httpsKeepAliveAgent?: https.Agent;
private httpKeepAliveAgent?: http.Agent;
/**
* Makes a request over an underlying transport layer and returns the response.
* @param request - The request to be made.
*/
public async sendRequest(request: PipelineRequest): Promise<PipelineResponse> {
const abortController = new AbortController();
let abortListener: ((event: any) => void) | undefined;
if (request.abortSignal) {
if (request.abortSignal.aborted) {
throw new AbortError("The operation was aborted.");
}
abortListener = (event: Event) => {
if (event.type === "abort") {
abortController.abort();
}
};
request.abortSignal.addEventListener("abort", abortListener);
}
if (request.timeout > 0) {
setTimeout(() => {
abortController.abort();
}, request.timeout);
}
const acceptEncoding = request.headers.get("Accept-Encoding");
const shouldDecompress =
acceptEncoding?.includes("gzip") || acceptEncoding?.includes("deflate");
let body = request.body;
if (body && !request.headers.has("Content-Length")) {
const bodyLength = getBodyLength(body);
if (bodyLength !== null) {
request.headers.set("Content-Length", bodyLength);
}
}
let responseStream: NodeJS.ReadableStream | undefined;
try {
const result = await new Promise<PipelineResponse>((resolve, reject) => {
if (body && request.onUploadProgress) {
const onUploadProgress = request.onUploadProgress;
const uploadReportStream = new ReportTransform(onUploadProgress);
uploadReportStream.on("error", reject);
if (isReadableStream(body)) {
body.pipe(uploadReportStream);
} else {
uploadReportStream.end(body);
}
body = uploadReportStream;
}
const req = this.makeRequest(request, async (res) => {
const headers = getResponseHeaders(res);
const status = res.statusCode ?? 0;
const response: PipelineResponse = {
status,
headers,
request
};
responseStream = shouldDecompress ? getDecodedResponseStream(res, headers) : res;
const onDownloadProgress = request.onDownloadProgress;
if (onDownloadProgress) {
const downloadReportStream = new ReportTransform(onDownloadProgress);
downloadReportStream.on("error", reject);
responseStream.pipe(downloadReportStream);
responseStream = downloadReportStream;
}
if (request.streamResponseStatusCodes?.has(response.status)) {
response.readableStreamBody = responseStream;
} else {
response.bodyAsText = await streamToText(responseStream);
}
resolve(response);
});
req.on("error", (err) => {
reject(new RestError(err.message, { code: RestError.REQUEST_SEND_ERROR, request }));
});
abortController.signal.addEventListener("abort", () => {
req.abort();
reject(new AbortError("The operation was aborted."));
});
if (body && isReadableStream(body)) {
body.pipe(req);
} else if (body) {
req.end(body);
} else {
// streams don't like "undefined" being passed as data
req.end();
}
});
return result;
} finally {
// clean up event listener
if (request.abortSignal && abortListener) {
let uploadStreamDone = Promise.resolve();
if (isReadableStream(body)) {
uploadStreamDone = isStreamComplete(body as NodeJS.ReadableStream);
}
let downloadStreamDone = Promise.resolve();
if (isReadableStream(responseStream)) {
downloadStreamDone = isStreamComplete(responseStream);
}
Promise.all([uploadStreamDone, downloadStreamDone])
.then(() => {
request.abortSignal?.removeEventListener("abort", abortListener!);
return;
})
.catch((e) => {
logger.warning("Error when cleaning up abortListener on httpRequest", e);
});
}
}
}
private makeRequest(
request: PipelineRequest,
callback: (res: http.IncomingMessage) => void
): http.ClientRequest {
const url = new URL(request.url);
const isInsecure = url.protocol !== "https:";
if (isInsecure && !request.allowInsecureConnection) {
throw new Error(`Cannot connect to ${request.url} while allowInsecureConnection is false.`);
}
const agent = request.agent ?? this.getOrCreateAgent(request, isInsecure);
const options: http.RequestOptions = {
agent,
hostname: url.hostname,
path: `${url.pathname}${url.search}`,
port: url.port,
method: request.method,
headers: request.headers.toJSON()
};
if (isInsecure) {
return http.request(options, callback);
} else {
return https.request(options, callback);
}
}
private getOrCreateAgent(request: PipelineRequest, isInsecure: boolean): http.Agent {
if (!request.disableKeepAlive) {
if (isInsecure) {
if (!this.httpKeepAliveAgent) {
this.httpKeepAliveAgent = new http.Agent({
keepAlive: true
});
}
return this.httpKeepAliveAgent;
} else {
if (!this.httpsKeepAliveAgent) {
this.httpsKeepAliveAgent = new https.Agent({
keepAlive: true
});
}
return this.httpsKeepAliveAgent;
}
} else if (isInsecure) {
return http.globalAgent;
} else {
return https.globalAgent;
}
}
}
function getResponseHeaders(res: IncomingMessage): HttpHeaders {
const headers = createHttpHeaders();
for (const header of Object.keys(res.headers)) {
const value = res.headers[header];
if (Array.isArray(value)) {
if (value.length > 0) {
headers.set(header, value[0]);
}
} else if (value) {
headers.set(header, value);
}
}
return headers;
}
function getDecodedResponseStream(
stream: IncomingMessage,
headers: HttpHeaders
): NodeJS.ReadableStream {
const contentEncoding = headers.get("Content-Encoding");
if (contentEncoding === "gzip") {
const unzip = zlib.createGunzip();
stream.pipe(unzip);
return unzip;
} else if (contentEncoding === "deflate") {
const inflate = zlib.createInflate();
stream.pipe(inflate);
return inflate;
}
return stream;
}
function streamToText(stream: NodeJS.ReadableStream): Promise<string> {
return new Promise<string>((resolve, reject) => {
const buffer: Buffer[] = [];
stream.on("data", (chunk) => {
if (Buffer.isBuffer(chunk)) {
buffer.push(chunk);
} else {
buffer.push(Buffer.from(chunk));
}
});
stream.on("end", () => {
resolve(Buffer.concat(buffer).toString("utf8"));
});
stream.on("error", (e) => {
reject(
new RestError(`Error reading response as text: ${e.message}`, {
code: RestError.PARSE_ERROR
})
);
});
});
}
/** @internal */
export function getBodyLength(body: RequestBodyType): number | null {
if (!body) {
return 0;
} else if (Buffer.isBuffer(body)) {
return body.length;
} else if (isReadableStream(body)) {
return null;
} else if (isArrayBuffer(body)) {
return body.byteLength;
} else if (typeof body === "string") {
return Buffer.from(body).length;
} else {
return null;
}
}
/**
* Create a new HttpClient instance for the NodeJS environment.
* @internal
*/
export function createNodeHttpClient(): HttpClient {
return new NodeHttpClient();
}