-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathRequestSender.ts
561 lines (496 loc) · 17.6 KB
/
RequestSender.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import {
StripeAPIError,
StripeAuthenticationError,
StripeConnectionError,
StripeError,
StripePermissionError,
StripeRateLimitError,
} from './Error.js';
import {
emitWarning,
normalizeHeaders,
removeNullish,
stringifyRequestData,
} from './utils.js';
import {HttpClient, HttpClientResponseInterface} from './net/HttpClient.js';
import {
StripeObject,
RequestHeaders,
RequestEvent,
ResponseEvent,
RequestCallback,
RequestCallbackReturn,
RequestSettings,
RequestData,
RequestOptions,
RequestDataProcessor,
} from './Types.js';
export type HttpClientResponseError = {code: string};
const MAX_RETRY_AFTER_WAIT = 60;
export class RequestSender {
protected _stripe: StripeObject;
private readonly _maxBufferedRequestMetric: number;
constructor(stripe: StripeObject, maxBufferedRequestMetric: number) {
this._stripe = stripe;
this._maxBufferedRequestMetric = maxBufferedRequestMetric;
}
_addHeadersDirectlyToObject(obj: any, headers: RequestHeaders): void {
// For convenience, make some headers easily accessible on
// lastResponse.
// NOTE: Stripe responds with lowercase header names/keys.
obj.requestId = headers['request-id'];
obj.stripeAccount = obj.stripeAccount || headers['stripe-account'];
obj.apiVersion = obj.apiVersion || headers['stripe-version'];
obj.idempotencyKey = obj.idempotencyKey || headers['idempotency-key'];
}
_makeResponseEvent(
requestEvent: RequestEvent,
statusCode: number,
headers: RequestHeaders
): ResponseEvent {
const requestEndTime = Date.now();
const requestDurationMs = requestEndTime - requestEvent.request_start_time;
return removeNullish({
api_version: headers['stripe-version'] as string,
account: headers['stripe-account'] as string,
idempotency_key: headers['idempotency-key'] as string,
method: requestEvent.method,
path: requestEvent.path,
status: statusCode,
request_id: this._getRequestId(headers),
elapsed: requestDurationMs,
request_start_time: requestEvent.request_start_time,
request_end_time: requestEndTime,
});
}
_getRequestId(headers: RequestHeaders): string {
return headers['request-id'] as string;
}
/**
* Used by methods with spec.streaming === true. For these methods, we do not
* buffer successful responses into memory or do parse them into stripe
* objects, we delegate that all of that to the user and pass back the raw
* http.Response object to the callback.
*
* (Unsuccessful responses shouldn't make it here, they should
* still be buffered/parsed and handled by _jsonResponseHandler -- see
* makeRequest)
*/
_streamingResponseHandler(
requestEvent: RequestEvent,
callback: RequestCallback
): (res: HttpClientResponseInterface) => RequestCallbackReturn {
return (res: HttpClientResponseInterface): RequestCallbackReturn => {
const headers = res.getHeaders();
const streamCompleteCallback = (): void => {
const responseEvent = this._makeResponseEvent(
requestEvent,
res.getStatusCode(),
headers
);
this._stripe._emitter.emit('response', responseEvent);
this._recordRequestMetrics(
this._getRequestId(headers),
responseEvent.elapsed
);
};
const stream = res.toStream(streamCompleteCallback);
// This is here for backwards compatibility, as the stream is a raw
// HTTP response in Node and the legacy behavior was to mutate this
// response.
this._addHeadersDirectlyToObject(stream, headers);
return callback(null, stream);
};
}
/**
* Default handler for Stripe responses. Buffers the response into memory,
* parses the JSON and returns it (i.e. passes it to the callback) if there
* is no "error" field. Otherwise constructs/passes an appropriate Error.
*/
_jsonResponseHandler(requestEvent: RequestEvent, callback: RequestCallback) {
return (res: HttpClientResponseInterface): void => {
const headers = res.getHeaders();
const requestId = this._getRequestId(headers);
const statusCode = res.getStatusCode();
const responseEvent = this._makeResponseEvent(
requestEvent,
statusCode,
headers
);
this._stripe._emitter.emit('response', responseEvent);
res
.toJSON()
.then(
(jsonResponse) => {
if (jsonResponse.error) {
let err;
// Convert OAuth error responses into a standard format
// so that the rest of the error logic can be shared
if (typeof jsonResponse.error === 'string') {
jsonResponse.error = {
type: jsonResponse.error,
message: jsonResponse.error_description,
};
}
jsonResponse.error.headers = headers;
jsonResponse.error.statusCode = statusCode;
jsonResponse.error.requestId = requestId;
if (statusCode === 401) {
err = new StripeAuthenticationError(jsonResponse.error);
} else if (statusCode === 403) {
err = new StripePermissionError(jsonResponse.error);
} else if (statusCode === 429) {
err = new StripeRateLimitError(jsonResponse.error);
} else {
err = StripeError.generate(jsonResponse.error);
}
throw err;
}
return jsonResponse;
},
(e: Error) => {
throw new StripeAPIError({
message: 'Invalid JSON received from the Stripe API',
exception: e,
requestId: headers['request-id'] as string,
});
}
)
.then(
(jsonResponse) => {
this._recordRequestMetrics(requestId, responseEvent.elapsed);
// Expose raw response object.
const rawResponse = res.getRawResponse();
this._addHeadersDirectlyToObject(rawResponse, headers);
Object.defineProperty(jsonResponse, 'lastResponse', {
enumerable: false,
writable: false,
value: rawResponse,
});
callback(null, jsonResponse);
},
(e) => callback(e, null)
);
};
}
static _generateConnectionErrorMessage(requestRetries: number): string {
return `An error occurred with our connection to Stripe.${
requestRetries > 0 ? ` Request was retried ${requestRetries} times.` : ''
}`;
}
// For more on when and how to retry API requests, see https://stripe.com/docs/error-handling#safely-retrying-requests-with-idempotency
static _shouldRetry(
res: null | HttpClientResponseInterface,
numRetries: number,
maxRetries: number,
error?: HttpClientResponseError
): boolean {
if (
error &&
numRetries === 0 &&
HttpClient.CONNECTION_CLOSED_ERROR_CODES.includes(error.code)
) {
return true;
}
// Do not retry if we are out of retries.
if (numRetries >= maxRetries) {
return false;
}
// Retry on connection error.
if (!res) {
return true;
}
// The API may ask us not to retry (e.g., if doing so would be a no-op)
// or advise us to retry (e.g., in cases of lock timeouts); we defer to that.
if (res.getHeaders()['stripe-should-retry'] === 'false') {
return false;
}
if (res.getHeaders()['stripe-should-retry'] === 'true') {
return true;
}
// Retry on conflict errors.
if (res.getStatusCode() === 409) {
return true;
}
// Retry on 500, 503, and other internal errors.
//
// Note that we expect the stripe-should-retry header to be false
// in most cases when a 500 is returned, since our idempotency framework
// would typically replay it anyway.
if (res.getStatusCode() >= 500) {
return true;
}
return false;
}
_getSleepTimeInMS(
numRetries: number,
retryAfter: number | null = null
): number {
const initialNetworkRetryDelay = this._stripe.getInitialNetworkRetryDelay();
const maxNetworkRetryDelay = this._stripe.getMaxNetworkRetryDelay();
// Apply exponential backoff with initialNetworkRetryDelay on the
// number of numRetries so far as inputs. Do not allow the number to exceed
// maxNetworkRetryDelay.
let sleepSeconds = Math.min(
initialNetworkRetryDelay * Math.pow(numRetries - 1, 2),
maxNetworkRetryDelay
);
// Apply some jitter by randomizing the value in the range of
// (sleepSeconds / 2) to (sleepSeconds).
sleepSeconds *= 0.5 * (1 + Math.random());
// But never sleep less than the base sleep seconds.
sleepSeconds = Math.max(initialNetworkRetryDelay, sleepSeconds);
// And never sleep less than the time the API asks us to wait, assuming it's a reasonable ask.
if (Number.isInteger(retryAfter) && retryAfter! <= MAX_RETRY_AFTER_WAIT) {
sleepSeconds = Math.max(sleepSeconds, retryAfter!);
}
return sleepSeconds * 1000;
}
// Max retries can be set on a per request basis. Favor those over the global setting
_getMaxNetworkRetries(settings: RequestSettings = {}): number {
return settings.maxNetworkRetries &&
Number.isInteger(settings.maxNetworkRetries)
? settings.maxNetworkRetries
: this._stripe.getMaxNetworkRetries();
}
_defaultIdempotencyKey(
method: string,
settings: RequestSettings
): string | null {
// If this is a POST and we allow multiple retries, ensure an idempotency key.
const maxRetries = this._getMaxNetworkRetries(settings);
if (method === 'POST' && maxRetries > 0) {
return `stripe-node-retry-${this._stripe._platformFunctions.uuid4()}`;
}
return null;
}
_makeHeaders(
auth: string | null,
contentLength: number,
apiVersion: string,
clientUserAgent: string,
method: string,
userSuppliedHeaders: RequestHeaders | null,
userSuppliedSettings: RequestSettings
): RequestHeaders {
const defaultHeaders = {
// Use specified auth token or use default from this stripe instance:
Authorization: auth ? `Bearer ${auth}` : this._stripe.getApiField('auth'),
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': this._getUserAgentString(),
'X-Stripe-Client-User-Agent': clientUserAgent,
'X-Stripe-Client-Telemetry': this._getTelemetryHeader(),
'Stripe-Version': apiVersion,
'Stripe-Account': this._stripe.getApiField('stripeAccount'),
'Idempotency-Key': this._defaultIdempotencyKey(
method,
userSuppliedSettings
),
} as RequestHeaders;
// As per https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2:
// A user agent SHOULD send a Content-Length in a request message when
// no Transfer-Encoding is sent and the request method defines a meaning
// for an enclosed payload body. For example, a Content-Length header
// field is normally sent in a POST request even when the value is 0
// (indicating an empty payload body). A user agent SHOULD NOT send a
// Content-Length header field when the request message does not contain
// a payload body and the method semantics do not anticipate such a
// body.
//
// These method types are expected to have bodies and so we should always
// include a Content-Length.
const methodHasPayload =
method == 'POST' || method == 'PUT' || method == 'PATCH';
// If a content length was specified, we always include it regardless of
// whether the method semantics anticipate such a body. This keeps us
// consistent with historical behavior. We do however want to warn on this
// and fix these cases as they are semantically incorrect.
if (methodHasPayload || contentLength) {
if (!methodHasPayload) {
emitWarning(
`${method} method had non-zero contentLength but no payload is expected for this verb`
);
}
defaultHeaders['Content-Length'] = contentLength;
}
return Object.assign(
removeNullish(defaultHeaders),
// If the user supplied, say 'idempotency-key', override instead of appending by ensuring caps are the same.
normalizeHeaders(userSuppliedHeaders)
);
}
_getUserAgentString(): string {
const packageVersion = this._stripe.getConstant('PACKAGE_VERSION');
const appInfo = this._stripe._appInfo
? this._stripe.getAppInfoAsString()
: '';
return `Stripe/v1 NodeBindings/${packageVersion} ${appInfo}`.trim();
}
_getTelemetryHeader(): string | undefined {
if (
this._stripe.getTelemetryEnabled() &&
this._stripe._prevRequestMetrics.length > 0
) {
const metrics = this._stripe._prevRequestMetrics.shift();
return JSON.stringify({
last_request_metrics: metrics,
});
}
}
_recordRequestMetrics(requestId: string, requestDurationMs: number): void {
if (this._stripe.getTelemetryEnabled() && requestId) {
if (
this._stripe._prevRequestMetrics.length > this._maxBufferedRequestMetric
) {
emitWarning(
'Request metrics buffer is full, dropping telemetry message.'
);
} else {
this._stripe._prevRequestMetrics.push({
request_id: requestId,
request_duration_ms: requestDurationMs,
});
}
}
}
_request(
method: string,
host: string | null,
path: string,
data: RequestData,
auth: string | null,
options: RequestOptions = {},
callback: RequestCallback,
requestDataProcessor: RequestDataProcessor | null = null
): void {
let requestData: string;
const retryRequest = (
requestFn: typeof makeRequest,
apiVersion: string,
headers: RequestHeaders,
requestRetries: number,
retryAfter: number | null
): NodeJS.Timeout => {
return setTimeout(
requestFn,
this._getSleepTimeInMS(requestRetries, retryAfter),
apiVersion,
headers,
requestRetries + 1
);
};
const makeRequest = (
apiVersion: string,
headers: RequestHeaders,
numRetries: number
): void => {
// timeout can be set on a per-request basis. Favor that over the global setting
const timeout =
options.settings &&
options.settings.timeout &&
Number.isInteger(options.settings.timeout) &&
options.settings.timeout >= 0
? options.settings.timeout
: this._stripe.getApiField('timeout');
const req = this._stripe
.getApiField('httpClient')
.makeRequest(
host || this._stripe.getApiField('host'),
this._stripe.getApiField('port'),
path,
method,
headers,
requestData,
this._stripe.getApiField('protocol'),
timeout
);
const requestStartTime = Date.now();
// @ts-ignore
const requestEvent: RequestEvent = removeNullish({
api_version: apiVersion,
account: headers['Stripe-Account'],
idempotency_key: headers['Idempotency-Key'],
method,
path,
request_start_time: requestStartTime,
});
const requestRetries = numRetries || 0;
const maxRetries = this._getMaxNetworkRetries(options.settings || {});
this._stripe._emitter.emit('request', requestEvent);
req
.then((res: HttpClientResponseInterface) => {
if (RequestSender._shouldRetry(res, requestRetries, maxRetries)) {
return retryRequest(
makeRequest,
apiVersion,
headers,
requestRetries,
// @ts-ignore
res.getHeaders()['retry-after']
);
} else if (options.streaming && res.getStatusCode() < 400) {
return this._streamingResponseHandler(requestEvent, callback)(res);
} else {
return this._jsonResponseHandler(requestEvent, callback)(res);
}
})
.catch((error: HttpClientResponseError) => {
if (
RequestSender._shouldRetry(null, requestRetries, maxRetries, error)
) {
return retryRequest(
makeRequest,
apiVersion,
headers,
requestRetries,
null
);
} else {
const isTimeoutError =
error.code && error.code === HttpClient.TIMEOUT_ERROR_CODE;
return callback(
new StripeConnectionError({
message: isTimeoutError
? `Request aborted due to timeout being reached (${timeout}ms)`
: RequestSender._generateConnectionErrorMessage(
requestRetries
),
// @ts-ignore
detail: error,
})
);
}
});
};
const prepareAndMakeRequest = (error: Error | null, data: string): void => {
if (error) {
return callback(error);
}
requestData = data;
this._stripe.getClientUserAgent((clientUserAgent: string) => {
const apiVersion = this._stripe.getApiField('version');
const headers = this._makeHeaders(
auth,
requestData.length,
apiVersion,
clientUserAgent,
method,
options.headers ?? null,
options.settings ?? {}
);
makeRequest(apiVersion, headers, 0);
});
};
if (requestDataProcessor) {
requestDataProcessor(
method,
data,
options.headers,
prepareAndMakeRequest
);
} else {
prepareAndMakeRequest(null, stringifyRequestData(data || {}));
}
}
}