Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 726x 4x 136x 136x 47x 47x 88x 88x 84x 84x 1x 1x 5x 5x 5x 1x 4x 1x 3x 1x 2x 2x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 42x 42x 42x 42x 42x 42x 42x 42x 39x 3x 3x 3x 3x 2x 2x 2x 2x 2x 1x 3x 3x | import type {
BrowserSupport,
SupportLevel,
AvailableMetric,
UnsupportedInfo,
UnsupportedReason,
FallbackStrategy,
} from "../types";
/**
* Check if running in a server-side environment
*/
export function isServer(): boolean {
return typeof window === "undefined";
}
/**
* Check if running in a client-side environment
*/
export function isClient(): boolean {
return typeof window !== "undefined";
}
/**
* Check if the legacy performance.memory API is available (Chrome/Edge)
*/
export function hasLegacyMemoryAPI(): boolean {
Iif (isServer()) return false;
return "memory" in performance;
}
/**
* Check if the modern measureUserAgentSpecificMemory API is available
*/
export function hasPreciseMemoryAPI(): boolean {
Iif (isServer()) return false;
return "measureUserAgentSpecificMemory" in performance;
}
/**
* Check if running in a secure context (HTTPS or localhost)
*/
export function isSecureContext(): boolean {
Iif (isServer()) return false;
return window.isSecureContext ?? false;
}
/**
* Check if cross-origin isolated (required for precise memory API)
*/
export function isCrossOriginIsolated(): boolean {
Iif (isServer()) return false;
return window.crossOriginIsolated ?? false;
}
/**
* Check if MutationObserver is available for DOM tracking
*/
export function hasMutationObserver(): boolean {
Iif (isServer()) return false;
return "MutationObserver" in window;
}
/**
* Attempt to detect the browser name
*/
export function detectBrowser(): string | undefined {
Iif (isServer()) return undefined;
const userAgent = navigator.userAgent;
if (userAgent.includes("Chrome") && !userAgent.includes("Edg")) {
return "Chrome";
}
if (userAgent.includes("Edg")) {
return "Edge";
}
if (userAgent.includes("Firefox")) {
return "Firefox";
}
Eif (userAgent.includes("Safari") && !userAgent.includes("Chrome")) {
return "Safari";
}
return undefined;
}
/**
* Determine the support level based on available APIs
*/
export function determineSupportLevel(): SupportLevel {
Iif (isServer()) return "none";
Eif (hasLegacyMemoryAPI()) {
return "full";
}
// DOM-only tracking is always available in browser
return "partial";
}
/**
* Get the list of available metrics based on browser support
*/
export function getAvailableMetrics(): AvailableMetric[] {
Iif (isServer()) return [];
const metrics: AvailableMetric[] = [];
Eif (hasLegacyMemoryAPI()) {
metrics.push("heapUsed", "heapTotal", "heapLimit");
}
// DOM tracking is always available in browser
metrics.push("domNodes");
// Event listener estimation is always available
metrics.push("eventListeners");
return metrics;
}
/**
* Get limitations based on current browser/context
*/
export function getLimitations(): string[] {
const limitations: string[] = [];
Iif (isServer()) {
limitations.push("Running in server-side environment");
return limitations;
}
Iif (!hasLegacyMemoryAPI()) {
limitations.push("performance.memory API not available (not Chrome/Edge)");
}
Eif (!isSecureContext()) {
limitations.push("Not running in secure context (HTTPS required for precise API)");
}
Eif (!isCrossOriginIsolated()) {
limitations.push(
"Not cross-origin isolated (COOP/COEP headers required for precise API)"
);
}
return limitations;
}
/**
* Comprehensive browser support detection
*/
export function detectSupport(): BrowserSupport {
return {
level: determineSupportLevel(),
availableMetrics: getAvailableMetrics(),
limitations: getLimitations(),
isSecureContext: isSecureContext(),
isCrossOriginIsolated: isCrossOriginIsolated(),
hasPreciseMemoryAPI: hasPreciseMemoryAPI(),
};
}
/**
* Determine the reason why memory monitoring is not supported
*/
export function getUnsupportedReason(): UnsupportedReason {
Iif (isServer()) {
return "server-side";
}
Iif (!isSecureContext() && hasPreciseMemoryAPI()) {
return "insecure-context";
}
Iif (!hasLegacyMemoryAPI() && !hasPreciseMemoryAPI()) {
return "no-api";
}
return "browser-restriction";
}
/**
* Get available fallback strategies based on support
*/
export function getAvailableFallbacks(): FallbackStrategy[] {
Iif (isServer()) {
return ["none"];
}
const fallbacks: FallbackStrategy[] = ["none"];
// DOM-only is always available in browser
fallbacks.push("dom-only");
// Estimation can work in any browser
fallbacks.push("estimation");
return fallbacks;
}
/**
* Create UnsupportedInfo object for callbacks
*/
export function createUnsupportedInfo(): UnsupportedInfo {
return {
reason: getUnsupportedReason(),
browser: detectBrowser(),
availableFallbacks: getAvailableFallbacks(),
};
}
/**
* Check if the precise memory API can be used
* Requires secure context AND cross-origin isolation
*/
export function canUsePreciseMemoryAPI(): boolean {
return hasPreciseMemoryAPI() && isSecureContext() && isCrossOriginIsolated();
}
/**
* Check if any memory monitoring is possible
*/
export function canMonitorMemory(): boolean {
return isClient() && (hasLegacyMemoryAPI() || hasPreciseMemoryAPI());
}
|