forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSubmissionAdapter.ts
More file actions
120 lines (101 loc) · 4.08 KB
/
DefaultSubmissionAdapter.ts
File metadata and controls
120 lines (101 loc) · 4.08 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
import { ISubmissionAdapter } from './ISubmissionAdapter';
import { SubmissionCallback } from './SubmissionCallback';
import { SubmissionRequest } from './SubmissionRequest';
// tslint:disable-next-line:prefer-const
declare var XDomainRequest: { new (); create(); };
export class DefaultSubmissionAdapter implements ISubmissionAdapter {
public sendRequest(request: SubmissionRequest, callback?: SubmissionCallback, isAppExiting?: boolean) {
// TODO: Handle sending events when app is exiting with send beacon.
const TIMEOUT: string = 'timeout'; // optimization for minifier.
const LOADED: string = 'loaded'; // optimization for minifier.
const WITH_CREDENTIALS: string = 'withCredentials'; // optimization for minifier.
let isCompleted: boolean = false;
let useSetTimeout: boolean = false;
function complete(mode: string, xhr: XMLHttpRequest) {
function parseResponseHeaders(headerStr) {
function trim(value) {
return value.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}
const headers = {};
const headerPairs = (headerStr || '').split('\u000d\u000a');
for (const headerPair of headerPairs) {
// Can't use split() here because it does the wrong thing
// if the header value has the string ": " in it.
const separator = headerPair.indexOf('\u003a\u0020');
if (separator > 0) {
headers[trim(headerPair.substring(0, separator).toLowerCase())] = headerPair.substring(separator + 2);
}
}
return headers;
}
if (isCompleted) {
return;
}
isCompleted = true;
let message: string = xhr.statusText;
const responseText: string = xhr.responseText;
let status: number = xhr.status;
if (mode === TIMEOUT || status === 0) {
message = 'Unable to connect to server.';
status = 0;
} else if (mode === LOADED && !status) {
status = request.method === 'POST' ? 202 : 200;
} else if (status < 200 || status > 299) {
const responseBody: any = (xhr as any).responseBody;
if (!!responseBody && !!responseBody.message) {
message = responseBody.message;
} else if (!!responseText && responseText.indexOf('message') !== -1) {
try {
message = JSON.parse(responseText).message;
} catch (e) {
message = responseText;
}
}
}
callback && callback(status || 500, message || '', responseText, parseResponseHeaders(xhr.getAllResponseHeaders && xhr.getAllResponseHeaders()));
}
function createRequest(userAgent: string, method: string, url: string): XMLHttpRequest {
let xhr: any = new XMLHttpRequest();
if (WITH_CREDENTIALS in xhr) {
xhr.open(method, url, true);
xhr.setRequestHeader('X-Exceptionless-Client', userAgent);
if (method === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/json');
}
} else if (typeof XDomainRequest !== 'undefined') {
useSetTimeout = true;
xhr = new XDomainRequest();
xhr.open(method, location.protocol === 'http:' ? url.replace('https:', 'http:') : url);
} else {
xhr = null;
}
if (xhr) {
xhr.timeout = 10000;
}
return xhr;
}
const url = `${request.url}${(request.url.indexOf('?') === -1 ? '?' : '&')}access_token=${encodeURIComponent(request.apiKey)}`;
const xhr = createRequest(request.userAgent, request.method || 'POST', url);
if (!xhr) {
return (callback && callback(503, 'CORS not supported.'));
}
if (WITH_CREDENTIALS in xhr) {
xhr.onreadystatechange = () => {
// xhr not ready.
if (xhr.readyState !== 4) {
return;
}
complete(LOADED, xhr);
};
}
xhr.onprogress = () => { };
xhr.ontimeout = () => complete(TIMEOUT, xhr);
xhr.onerror = () => complete('error', xhr);
xhr.onload = () => complete(LOADED, xhr);
if (useSetTimeout) {
setTimeout(() => xhr.send(request.data), 500);
} else {
xhr.send(request.data);
}
}
}