(entry: PerformanceEntry)
| 146 | // via the reducer. But it will only do so if the performance entry is |
| 147 | // a resource entry that we care about. |
| 148 | const dispatchProxyLatenciesGuarded = (entry: PerformanceEntry) => { |
| 149 | if (entry.entryType !== "resource") { |
| 150 | // We should never get these, but just in case. |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // The entry.name is the url of the request. |
| 155 | const check = proxyChecks[entry.name]; |
| 156 | if (!check) { |
| 157 | // This is not a proxy request, so ignore it. |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | // These docs are super useful. |
| 162 | // https://developer.mozilla.org/en-US/docs/Web/API/Performance_API/Resource_timing |
| 163 | let latencyMS = 0; |
| 164 | let accurate = false; |
| 165 | let nextHopProtocol: string | undefined; |
| 166 | if ( |
| 167 | "requestStart" in entry && |
| 168 | (entry as PerformanceResourceTiming).requestStart !== 0 |
| 169 | ) { |
| 170 | // This is the preferred logic to get the latency. |
| 171 | const timingEntry = entry as PerformanceResourceTiming; |
| 172 | latencyMS = timingEntry.responseStart - timingEntry.requestStart; |
| 173 | accurate = true; |
| 174 | nextHopProtocol = timingEntry.nextHopProtocol; |
| 175 | } else { |
| 176 | // This is the total duration of the request and will be off by a good margin. |
| 177 | // This is a fallback if the better timing is not available. |
| 178 | // We can remove this when we display the "accurate" bool on the UI |
| 179 | console.warn( |
| 180 | `Using fallback latency calculation for "${entry.name}". Latency will be incorrect and larger then actual.`, |
| 181 | ); |
| 182 | latencyMS = entry.duration; |
| 183 | } |
| 184 | const update = { |
| 185 | proxyID: check.id, |
| 186 | cached: false, |
| 187 | report: { |
| 188 | latencyMS, |
| 189 | accurate, |
| 190 | at: new Date(), |
| 191 | nextHopProtocol: nextHopProtocol, |
| 192 | } as ProxyLatencyReport, |
| 193 | }; |
| 194 | dispatchProxyLatencies(update); |
| 195 | // Also save to local storage to persist the latency across page refreshes. |
| 196 | updateStoredLatencies(update); |
| 197 | |
| 198 | return; |
| 199 | }; |
| 200 | |
| 201 | // Start a new performance observer to record of all the requests |
| 202 | // to the proxies. |
no test coverage detected