Skip to content

Commit 7339c78

Browse files
authored
chore: restore XhrPlugin var names (#455)
1 parent 1380511 commit 7339c78

File tree

2 files changed

+197
-114
lines changed

2 files changed

+197
-114
lines changed

src/plugins/event-plugins/XhrPlugin.ts

Lines changed: 80 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ export const XHR_PLUGIN_ID = 'xhr';
9595
*/
9696
export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
9797
private config: HttpPluginConfig;
98-
private map: Map<XMLHttpRequest, XhrDetails>;
98+
private xhrMap: Map<XMLHttpRequest, XhrDetails>;
9999
private isSyntheticsUA: boolean;
100100

101101
constructor(config?: PartialHttpPluginConfig) {
102102
super(XHR_PLUGIN_ID);
103103
this.config = { ...defaultConfig, ...config };
104-
this.map = new Map<XMLHttpRequest, XhrDetails>();
104+
this.xhrMap = new Map<XMLHttpRequest, XhrDetails>();
105105
this.isSyntheticsUA = navigator.userAgent.includes(
106106
'CloudWatchSynthetics'
107107
);
@@ -111,10 +111,6 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
111111
this.enable();
112112
}
113113

114-
get cacheSize() {
115-
return this.map.size;
116-
}
117-
118114
protected get patches() {
119115
return [
120116
{
@@ -143,110 +139,110 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
143139
};
144140

145141
private handleXhrLoadEvent = (e: Event) => {
146-
const request = e.target as XMLHttpRequest;
147-
const details = this.map.get(request);
148-
if (details) {
149-
const endTime = epochTime();
150-
details.trace!.end_time = endTime;
151-
details.trace!.subsegments![0].end_time = endTime;
152-
details.trace!.subsegments![0].http!.response = {
153-
status: request.status
142+
const xhr: XMLHttpRequest = e.target as XMLHttpRequest;
143+
const xhrDetails: XhrDetails = this.xhrMap.get(xhr) as XhrDetails;
144+
if (xhrDetails) {
145+
const endTimee = epochTime();
146+
xhrDetails.trace!.end_time = endTimee;
147+
xhrDetails.trace!.subsegments![0].end_time = endTimee;
148+
xhrDetails.trace!.subsegments![0].http!.response = {
149+
status: xhr.status
154150
};
155151

156-
if (is429(request.status)) {
157-
details.trace!.subsegments![0].throttle = true;
158-
details.trace!.throttle = true;
159-
} else if (is4xx(request.status)) {
160-
details.trace!.subsegments![0].error = true;
161-
details.trace!.error = true;
162-
} else if (is5xx(request.status)) {
163-
details.trace!.subsegments![0].fault = true;
164-
details.trace!.fault = true;
152+
if (is429(xhr.status)) {
153+
xhrDetails.trace!.subsegments![0].throttle = true;
154+
xhrDetails.trace!.throttle = true;
155+
} else if (is4xx(xhr.status)) {
156+
xhrDetails.trace!.subsegments![0].error = true;
157+
xhrDetails.trace!.error = true;
158+
} else if (is5xx(xhr.status)) {
159+
xhrDetails.trace!.subsegments![0].fault = true;
160+
xhrDetails.trace!.fault = true;
165161
}
166162

167-
const clStr = request.getResponseHeader('Content-Length');
163+
const clStr = xhr.getResponseHeader('Content-Length');
168164
const cl = clStr ? parseInt(clStr, 10) : NaN;
169165
if (!isNaN(cl)) {
170-
details.trace!.subsegments![0].http!.response.content_length =
166+
xhrDetails.trace!.subsegments![0].http!.response.content_length =
171167
cl;
172168
}
173-
this.recordTraceEvent(details.trace!);
174-
this.recordHttpEventWithResponse(details, request);
169+
this.recordTraceEvent(xhrDetails.trace!);
170+
this.recordHttpEventWithResponse(xhrDetails, xhr);
175171
}
176172
};
177173

178174
private handleXhrErrorEvent = (e: Event) => {
179-
const request = e.target as XMLHttpRequest;
180-
const details = this.map.get(request);
175+
const xhr: XMLHttpRequest = e.target as XMLHttpRequest;
176+
const xhrDetails = this.xhrMap.get(xhr);
181177
const errorName = 'XMLHttpRequest error';
182-
const errorMessage: string = request.statusText
183-
? request.status.toString() + ': ' + request.statusText
184-
: request.status.toString();
185-
if (details) {
178+
const errorMessage: string = xhr.statusText
179+
? xhr.status.toString() + ': ' + xhr.statusText
180+
: xhr.status.toString();
181+
if (xhrDetails) {
186182
const endTime = epochTime();
187183
// Guidance from X-Ray documentation:
188184
// > Record errors in segments when your application returns an
189185
// > error to the user, and in subsegments when a downstream call
190186
// > returns an error.
191-
details.trace!.fault = true;
192-
details.trace!.end_time = endTime;
193-
details.trace!.subsegments![0].end_time = endTime;
194-
details.trace!.subsegments![0].fault = true;
195-
details.trace!.subsegments![0].cause = {
187+
xhrDetails.trace!.fault = true;
188+
xhrDetails.trace!.end_time = endTime;
189+
xhrDetails.trace!.subsegments![0].end_time = endTime;
190+
xhrDetails.trace!.subsegments![0].fault = true;
191+
xhrDetails.trace!.subsegments![0].cause = {
196192
exceptions: [
197193
{
198194
type: errorName,
199195
message: errorMessage
200196
}
201197
]
202198
};
203-
this.recordTraceEvent(details.trace!);
199+
this.recordTraceEvent(xhrDetails.trace!);
204200
this.recordHttpEventWithError(
205-
details,
206-
request,
201+
xhrDetails,
202+
xhr,
207203
new XhrError(errorMessage)
208204
);
209205
}
210206
};
211207

212208
private handleXhrAbortEvent = (e: Event) => {
213-
const request = e.target as XMLHttpRequest;
214-
const details = this.map.get(request);
215-
if (details) {
209+
const xhr: XMLHttpRequest = e.target as XMLHttpRequest;
210+
const xhrDetails = this.xhrMap.get(xhr);
211+
if (xhrDetails) {
216212
this.handleXhrDetailsOnError(
217-
details,
218-
request,
213+
xhrDetails,
214+
xhr,
219215
'XMLHttpRequest abort'
220216
);
221217
}
222218
};
223219

224220
private handleXhrTimeoutEvent = (e: Event) => {
225-
const request = e.target as XMLHttpRequest;
226-
const details = this.map.get(request);
221+
const xhr: XMLHttpRequest = e.target as XMLHttpRequest;
222+
const xhrDetails = this.xhrMap.get(xhr);
227223
const errorName = 'XMLHttpRequest timeout';
228-
this.handleXhrDetailsOnError(details, request, errorName);
224+
this.handleXhrDetailsOnError(xhrDetails, xhr, errorName);
229225
};
230226

231227
private handleXhrDetailsOnError(
232-
details: XhrDetails | undefined,
233-
request: XMLHttpRequest,
228+
xhrDetails: XhrDetails | undefined,
229+
xhr: XMLHttpRequest,
234230
errorName: string
235231
) {
236-
if (details) {
232+
if (xhrDetails) {
237233
const endTime = epochTime();
238-
details.trace!.end_time = endTime;
239-
details.trace!.subsegments![0].end_time = endTime;
240-
details.trace!.subsegments![0].error = true;
241-
details.trace!.subsegments![0].cause = {
234+
xhrDetails.trace!.end_time = endTime;
235+
xhrDetails.trace!.subsegments![0].end_time = endTime;
236+
xhrDetails.trace!.subsegments![0].error = true;
237+
xhrDetails.trace!.subsegments![0].cause = {
242238
exceptions: [
243239
{
244240
type: errorName
245241
}
246242
]
247243
};
248-
this.recordTraceEvent(details.trace!);
249-
this.recordHttpEventWithError(details, request, errorName);
244+
this.recordTraceEvent(xhrDetails.trace!);
245+
this.recordHttpEventWithError(xhrDetails, xhr, errorName);
250246
}
251247
}
252248

@@ -255,36 +251,33 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
255251
}
256252

257253
private recordHttpEventWithResponse(
258-
details: XhrDetails,
259-
request: XMLHttpRequest
254+
xhrDetails: XhrDetails,
255+
xhr: XMLHttpRequest
260256
) {
261-
this.map.delete(request);
257+
this.xhrMap.delete(xhr);
262258
const httpEvent: HttpEvent = {
263259
version: '1.0.0',
264-
request: { method: details.method, url: details.url },
265-
response: {
266-
status: request.status,
267-
statusText: request.statusText
268-
}
260+
request: { method: xhrDetails.method, url: xhrDetails.url },
261+
response: { status: xhr.status, statusText: xhr.statusText }
269262
};
270263
if (this.isTracingEnabled()) {
271-
httpEvent.trace_id = details.trace!.trace_id;
272-
httpEvent.segment_id = details.trace!.subsegments![0].id;
264+
httpEvent.trace_id = xhrDetails.trace!.trace_id;
265+
httpEvent.segment_id = xhrDetails.trace!.subsegments![0].id;
273266
}
274-
if (this.config.recordAllRequests || !this.statusOk(request.status)) {
267+
if (this.config.recordAllRequests || !this.statusOk(xhr.status)) {
275268
this.context.record(HTTP_EVENT_TYPE, httpEvent);
276269
}
277270
}
278271

279272
private recordHttpEventWithError(
280-
details: XhrDetails,
281-
request: XMLHttpRequest,
273+
xhrDetails: XhrDetails,
274+
xhr: XMLHttpRequest,
282275
error: Error | string | number | boolean | undefined | null
283276
) {
284-
this.map.delete(request);
277+
this.xhrMap.delete(xhr);
285278
const httpEvent: HttpEvent = {
286279
version: '1.0.0',
287-
request: { method: details.method, url: details.url },
280+
request: { method: xhrDetails.method, url: xhrDetails.url },
288281
error: errorEventToJsErrorEvent(
289282
{
290283
type: 'error',
@@ -294,8 +287,8 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
294287
)
295288
};
296289
if (this.isTracingEnabled()) {
297-
httpEvent.trace_id = details.trace!.trace_id;
298-
httpEvent.segment_id = details.trace!.subsegments![0].id;
290+
httpEvent.trace_id = xhrDetails.trace!.trace_id;
291+
httpEvent.segment_id = xhrDetails.trace!.subsegments![0].id;
299292
}
300293
this.context.record(HTTP_EVENT_TYPE, httpEvent);
301294
}
@@ -310,20 +303,20 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
310303
}
311304
}
312305

313-
private initializeTrace = (details: XhrDetails) => {
306+
private initializeTrace = (xhrDetails: XhrDetails) => {
314307
const startTime = epochTime();
315-
details.trace = createXRayTraceEvent(
308+
xhrDetails.trace = createXRayTraceEvent(
316309
this.config.logicalServiceName,
317310
startTime
318311
);
319-
details.trace.subsegments!.push(
312+
xhrDetails.trace.subsegments!.push(
320313
createXRaySubsegment(
321-
requestInfoToHostname(details.url),
314+
requestInfoToHostname(xhrDetails.url),
322315
startTime,
323316
{
324317
request: {
325-
method: details.method,
326-
url: details.url,
318+
method: xhrDetails.method,
319+
url: xhrDetails.url,
327320
traced: true
328321
}
329322
}
@@ -335,8 +328,8 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
335328
const self = this;
336329
return (original: any) => {
337330
return function (this: XMLHttpRequest): void {
338-
const details = self.map.get(this);
339-
if (details) {
331+
const xhrDetails = self.xhrMap.get(this);
332+
if (xhrDetails) {
340333
this.addEventListener('load', self.handleXhrLoadEvent);
341334
this.addEventListener('error', self.handleXhrErrorEvent);
342335
this.addEventListener('abort', self.handleXhrAbortEvent);
@@ -345,7 +338,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
345338
self.handleXhrTimeoutEvent
346339
);
347340

348-
self.initializeTrace(details);
341+
self.initializeTrace(xhrDetails);
349342

350343
if (
351344
!self.isSyntheticsUA &&
@@ -356,8 +349,8 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
356349
this.setRequestHeader(
357350
X_AMZN_TRACE_ID,
358351
getAmznTraceIdHeaderValue(
359-
details.trace!.trace_id,
360-
details.trace!.subsegments![0].id
352+
xhrDetails.trace!.trace_id,
353+
xhrDetails.trace!.subsegments![0].id
361354
)
362355
);
363356
}
@@ -377,7 +370,7 @@ export class XhrPlugin extends MonkeyPatched<XMLHttpRequest, 'send' | 'open'> {
377370
async: boolean
378371
): void {
379372
if (isUrlAllowed(url, self.config)) {
380-
self.map.set(this, { url, method, async });
373+
self.xhrMap.set(this, { url, method, async });
381374
}
382375
return original.apply(this, arguments);
383376
};

0 commit comments

Comments
 (0)