-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathhttpRequest.ts
More file actions
140 lines (124 loc) · 3.93 KB
/
Copy pathhttpRequest.ts
File metadata and controls
140 lines (124 loc) · 3.93 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
import type { EndpointBuilder, TransportRetryInfo } from '@datadog/js-core/transport'
import { ONE_KIBI_BYTE } from '@datadog/js-core/util'
import type { Context } from '../tools/serialisation/context'
import { fetch } from '../browser/fetch'
import { monitor, monitorError } from '../tools/monitor'
import { Observable } from '../tools/observable'
import { newRetryState, sendWithRetryStrategy } from './sendWithRetryStrategy'
/**
* beacon payload max queue size implementation is 64kb
* ensure that we leave room for logs, rum and potential other users
*/
export const RECOMMENDED_REQUEST_BYTES_LIMIT = 16 * ONE_KIBI_BYTE
/**
* Use POST request without content type to:
* - avoid CORS preflight requests
* - allow usage of sendBeacon
*
* multiple elements are sent separated by \n in order
* to be parsed correctly without content type header
*/
export interface HttpRequest<Body extends Payload = Payload> {
observable: Observable<HttpRequestEvent<Body>>
send(this: void, payload: Body): void
sendOnExit(this: void, payload: Body): void
}
export interface HttpResponse extends Context {
status: number
type?: ResponseType
}
export interface BandwidthStats {
ongoingByteCount: number
ongoingRequestCount: number
}
export type HttpRequestEvent<Body extends Payload = Payload> =
| {
// A request to send the given payload failed. (We may retry.)
type: 'failure'
bandwidth: BandwidthStats
payload: Body
}
| {
// The given payload was discarded because the request queue is full.
type: 'queue-full'
bandwidth: BandwidthStats
payload: Body
}
| {
// A request to send the given payload succeeded.
type: 'success'
bandwidth: BandwidthStats
payload: Body
}
export interface Payload {
data: string | FormData | Blob
bytesCount: number
retry?: TransportRetryInfo
encoding?: 'deflate'
}
export function createHttpRequest<Body extends Payload = Payload>(
endpointBuilders: EndpointBuilder[],
reportError: (message: string) => void,
bytesLimit: number = RECOMMENDED_REQUEST_BYTES_LIMIT
): HttpRequest<Body> {
const observable = new Observable<HttpRequestEvent<Body>>()
const retryState = newRetryState<Body>()
return {
observable,
send: (payload: Body) => {
for (const endpointBuilder of endpointBuilders) {
sendWithRetryStrategy(
payload,
retryState,
(payload, onResponse) => {
fetchStrategy(endpointBuilder, payload, onResponse)
},
endpointBuilder.trackType,
reportError,
observable
)
}
},
/**
* Since fetch keepalive behaves like regular fetch on Firefox,
* keep using sendBeaconStrategy on exit
*/
sendOnExit: (payload: Body) => {
for (const endpointBuilder of endpointBuilders) {
sendBeaconStrategy(endpointBuilder, bytesLimit, payload)
}
},
}
}
function sendBeaconStrategy(endpointBuilder: EndpointBuilder, bytesLimit: number, payload: Payload) {
const canUseBeacon = payload.bytesCount < bytesLimit
if (canUseBeacon) {
try {
const beaconUrl = endpointBuilder.build('beacon', payload)
const isQueued = navigator.sendBeacon(beaconUrl, payload.data)
if (isQueued) {
return
}
} catch (e) {
reportBeaconError(e)
}
}
fetchStrategy(endpointBuilder, payload)
}
let hasReportedBeaconError = false
function reportBeaconError(e: unknown) {
if (!hasReportedBeaconError) {
hasReportedBeaconError = true
monitorError(e)
}
}
export function fetchStrategy(
endpointBuilder: EndpointBuilder,
payload: Payload,
onResponse?: (r: HttpResponse) => void
) {
const fetchUrl = endpointBuilder.build('fetch', payload)
fetch(fetchUrl, { method: 'POST', body: payload.data, mode: 'cors' })
.then(monitor((response: Response) => onResponse?.({ status: response.status, type: response.type })))
.catch(monitor(() => onResponse?.({ status: 0 })))
}