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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/**
* Environment Detection and Runtime Capabilities
*
* Provides comprehensive environment detection for:
* - Browser vs Node.js vs other runtimes
* - Feature availability detection
* - Platform-specific capability testing
* - Runtime version information
*/
/**
* Test if running in browser environment
* @returns {boolean} True if in browser
*/
function isBrowser() {
return (
typeof window !== "undefined" && (!isNodeEnv() || isElectronRenderer())
);
}
/**
* Test if running in Electron renderer process
* @returns {boolean} True if in Electron renderer
*/
function isElectronRenderer() {
const global = getGlobalObject();
return global.process !== undefined && global.process.type === "renderer";
}
/**
* Test if running in Node.js environment
* @returns {boolean} True if in Node.js
*/
function isNodeEnv() {
return (
!isBrowserBundle() &&
Object.prototype.toString.call(
typeof process !== "undefined" ? process : 0,
) === "[object process]"
);
}
/**
* Test if code is running as a browser bundle
* @returns {boolean} True if browser bundle
*/
function isBrowserBundle() {
return (
typeof __SENTRY_BROWSER_BUNDLE__ !== "undefined" &&
!!__SENTRY_BROWSER_BUNDLE__
);
}
/**
* Get SDK source information
* @returns {string} SDK source identifier
*/
function getSDKSource() {
return "npm";
}
/**
* Dynamic module requiring for Node.js
* @param {Object} moduleObject - Module object (usually module)
* @param {string} moduleName - Name of module to require
* @returns {any} Required module
*/
function dynamicRequire(moduleObject, moduleName) {
return moduleObject.require(moduleName);
}
/**
* Load a module with multiple fallback strategies
* @param {string} moduleName - Name of module to load
* @returns {any} Loaded module or undefined
*/
function loadModule(moduleName) {
let loadedModule;
try {
// Try direct require
loadedModule = dynamicRequire(module, moduleName);
} catch (error) {
// Ignore and try fallback
}
try {
// Try from current working directory
const { cwd } = dynamicRequire(module, "process");
loadedModule = dynamicRequire(
module,
`${cwd()}/node_modules/${moduleName}`,
);
} catch (error) {
// Module not found
}
return loadedModule;
}
/**
* Detect available JavaScript features
* @returns {Object} Feature availability map
*/
function detectFeatures() {
const global = getGlobalObject();
return {
// ES6+ Features
hasPromise: typeof Promise !== "undefined",
hasWeakSet: typeof WeakSet !== "undefined",
hasWeakMap: typeof WeakMap !== "undefined",
hasMap: typeof Map !== "undefined",
hasSet: typeof Set !== "undefined",
hasSymbol: typeof Symbol !== "undefined",
hasProxy: typeof Proxy !== "undefined",
hasReflect: typeof Reflect !== "undefined",
// Browser APIs
hasFetch: typeof fetch !== "undefined",
hasXMLHttpRequest: typeof XMLHttpRequest !== "undefined",
hasLocalStorage: typeof localStorage !== "undefined",
hasSessionStorage: typeof sessionStorage !== "undefined",
hasIndexedDB: typeof indexedDB !== "undefined",
hasWebSocket: typeof WebSocket !== "undefined",
hasWorker: typeof Worker !== "undefined",
hasServiceWorker: "serviceWorker" in (global.navigator || {}),
// Crypto APIs
hasCrypto: typeof crypto !== "undefined",
hasSubtleCrypto:
typeof crypto !== "undefined" && typeof crypto.subtle !== "undefined",
hasGetRandomValues:
typeof crypto !== "undefined" &&
typeof crypto.getRandomValues === "function",
hasRandomUUID:
typeof crypto !== "undefined" && typeof crypto.randomUUID === "function",
// Performance APIs
hasPerformance: typeof performance !== "undefined",
hasPerformanceNow:
typeof performance !== "undefined" &&
typeof performance.now === "function",
hasPerformanceMark:
typeof performance !== "undefined" &&
typeof performance.mark === "function",
hasPerformanceObserver: typeof PerformanceObserver !== "undefined",
hasRequestAnimationFrame: typeof requestAnimationFrame !== "undefined",
hasRequestIdleCallback: typeof requestIdleCallback !== "undefined",
// Error Handling
hasErrorEvent: (() => {
try {
return new ErrorEvent("") instanceof ErrorEvent;
} catch (e) {
return false;
}
})(),
hasUnhandledRejection: typeof global.addEventListener === "function",
// Node.js specific
hasProcess: typeof process !== "undefined",
hasBuffer: typeof Buffer !== "undefined",
hasModule: typeof module !== "undefined",
hasRequire: typeof require !== "function",
// Timers
hasSetTimeout: typeof setTimeout !== "undefined",
hasSetInterval: typeof setInterval !== "undefined",
hasSetImmediate: typeof setImmediate !== "undefined",
hasNextTick:
typeof process !== "undefined" && typeof process.nextTick === "function",
};
}
/**
* Detect runtime environment details
* @returns {Object} Runtime information
*/
function detectRuntime() {
const global = getGlobalObject();
const features = detectFeatures();
const runtime = {
name: "unknown",
version: "unknown",
platform: "unknown",
};
// Node.js detection
if (isNodeEnv() && global.process) {
runtime.name = "node";
runtime.version = global.process.version;
runtime.platform = global.process.platform;
runtime.arch = global.process.arch;
runtime.versions = global.process.versions;
}
// Browser detection
else if (isBrowser()) {
runtime.name = "browser";
if (typeof navigator !== "undefined") {
runtime.userAgent = navigator.userAgent;
runtime.platform = navigator.platform;
runtime.language = navigator.language;
runtime.languages = navigator.languages;
runtime.cookieEnabled = navigator.cookieEnabled;
runtime.onLine = navigator.onLine;
}
if (typeof window !== "undefined") {
runtime.location = window.location
? {
href: window.location.href,
protocol: window.location.protocol,
host: window.location.host,
}
: undefined;
}
}
// Deno detection
else if (typeof Deno !== "undefined") {
runtime.name = "deno";
runtime.version = Deno.version?.deno;
runtime.platform = Deno.build?.os;
runtime.arch = Deno.build?.arch;
}
// Bun detection
else if (typeof Bun !== "undefined") {
runtime.name = "bun";
runtime.version = Bun.version;
}
// Web Worker detection
else if (typeof self !== "undefined" && typeof window === "undefined") {
runtime.name = "webworker";
if (typeof navigator !== "undefined") {
runtime.userAgent = navigator.userAgent;
}
}
return { ...runtime, features };
}
/**
* Test for specific capabilities
* @param {string} capability - Capability to test
* @returns {boolean} True if capability is available
*/
function hasCapability(capability) {
const capabilities = {
// Network
fetch: () => typeof fetch === "function",
xhr: () => typeof XMLHttpRequest === "function",
// Storage
localStorage: () => {
try {
return typeof localStorage !== "undefined" && localStorage !== null;
} catch (e) {
return false;
}
},
sessionStorage: () => {
try {
return typeof sessionStorage !== "undefined" && sessionStorage !== null;
} catch (e) {
return false;
}
},
// Crypto
crypto: () => {
try {
return typeof crypto !== "undefined" && crypto !== null;
} catch (e) {
return false;
}
},
// Performance
performance: () => {
try {
return (
typeof performance !== "undefined" &&
typeof performance.now === "function"
);
} catch (e) {
return false;
}
},
// History API
history: () => {
const global = getGlobalObject();
const chrome = global.chrome;
const isChromeExtension = chrome && chrome.app && chrome.app.runtime;
const hasHistory =
"history" in global &&
!!global.history.pushState &&
!!global.history.replaceState;
return !isChromeExtension && hasHistory;
},
};
const test = capabilities[capability];
return test ? test() : false;
}
/**
* Get performance timing information
* @returns {Object} Performance timing data
*/
function getPerformanceInfo() {
if (!hasCapability("performance")) {
return null;
}
const perf = performance;
const info = {
now: perf.now(),
timeOrigin: perf.timeOrigin,
};
// Add navigation timing if available (browser only)
if (perf.timing) {
info.navigation = {
loadStart: perf.timing.loadEventStart,
domReady: perf.timing.domContentLoadedEventEnd,
loadComplete: perf.timing.loadEventEnd,
};
}
// Add memory info if available
if (perf.memory) {
info.memory = {
usedJSHeapSize: perf.memory.usedJSHeapSize,
totalJSHeapSize: perf.memory.totalJSHeapSize,
jsHeapSizeLimit: perf.memory.jsHeapSizeLimit,
};
}
return info;
}
/**
* Get global object reference
* @returns {Object} Global object
*/
function getGlobalObject() {
return (
(typeof globalThis === "object" && globalThis) ||
(typeof window === "object" && window) ||
(typeof self === "object" && self) ||
(typeof global === "object" && global) ||
(function () {
return this;
})() ||
{}
);
}
module.exports = {
isBrowser,
isNodeEnv,
isBrowserBundle,
isElectronRenderer,
getSDKSource,
dynamicRequire,
loadModule,
detectFeatures,
detectRuntime,
hasCapability,
getPerformanceInfo,
getGlobalObject,
};
|