Skip to content

Commit 4854913

Browse files
committed
WIP...
1 parent 0dd0385 commit 4854913

File tree

3 files changed

+41
-52
lines changed

3 files changed

+41
-52
lines changed

packages/browser/src/sdk.ts

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ declare const __SENTRY_RELEASE__: string | undefined;
127127
* @see {@link BrowserOptions} for documentation on configuration options.
128128
*/
129129
export function init(browserOptions: BrowserOptions = {}): Client | undefined {
130+
if (_checkBailForBrowserExtension(browserOptions)) {
131+
return;
132+
}
130133
return _init(browserOptions, getDefaultIntegrations(browserOptions));
131134
}
132135

@@ -142,30 +145,18 @@ export function initWithDefaultIntegrations(
142145
browserOptions: BrowserOptions = {},
143146
getDefaultIntegrationsImpl: (options: BrowserOptions) => Integration[],
144147
): BrowserClient | undefined {
148+
if (_checkBailForBrowserExtension(browserOptions)) {
149+
return;
150+
}
151+
145152
return _init(browserOptions, getDefaultIntegrationsImpl(browserOptions));
146153
}
147154

148155
/**
149156
* Acutal implementation shared by init and initWithDefaultIntegrations.
150157
*/
151-
function _init(
152-
browserOptions: BrowserOptions = {},
153-
defaultIntegrations: Integration[],
154-
): BrowserClient | undefined {
158+
function _init(browserOptions: BrowserOptions = {}, defaultIntegrations: Integration[]): BrowserClient {
155159
const options = applyDefaultOptions(browserOptions);
156-
157-
const isBrowserExtension = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
158-
159-
/* if (DEBUG_BUILD) {
160-
logBrowserEnvironmentWarnings({
161-
browserExtension: isBrowserExtension,
162-
fetch: !supportsFetch(),
163-
});
164-
}
165-
*/
166-
if (isBrowserExtension) {
167-
return;
168-
}
169160
const clientOptions = getClientOptions(options, defaultIntegrations);
170161
return initAndBind(BrowserClient, clientOptions);
171162
}
@@ -253,52 +244,49 @@ export function onLoad(callback: () => void): void {
253244
}
254245

255246
function shouldShowBrowserExtensionError(): boolean {
256-
const windowWithMaybeExtension =
257-
typeof WINDOW.window !== 'undefined' && (WINDOW as typeof WINDOW & ExtensionProperties);
258-
if (!windowWithMaybeExtension) {
247+
if (typeof WINDOW.window === 'undefined') {
259248
// No need to show the error if we're not in a browser window environment (e.g. service workers)
260249
return false;
261250
}
262251

263-
const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser';
264-
const extensionObject = windowWithMaybeExtension[extensionKey];
252+
const _window = WINDOW as typeof WINDOW & ExtensionProperties;
265253

266-
const runtimeId = extensionObject?.runtime?.id;
267-
const href = getLocationHref() || '';
254+
// Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
255+
// see: https://github.com/getsentry/sentry-javascript/issues/12668
256+
if (_window.nw) {
257+
return false;
258+
}
259+
260+
const extensionObject = _window['chrome'] || _window['browser'];
268261

269-
const extensionProtocols = ['chrome-extension:', 'moz-extension:', 'ms-browser-extension:', 'safari-web-extension:'];
262+
if (!extensionObject?.runtime?.id) {
263+
return false;
264+
}
265+
266+
const href = getLocationHref();
267+
const extensionProtocols = ['chrome-extension', 'moz-extension', 'ms-browser-extension', 'safari-web-extension'];
270268

271269
// Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
272270
const isDedicatedExtensionPage =
273-
!!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`));
271+
WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}://`));
274272

275-
// Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
276-
// see: https://github.com/getsentry/sentry-javascript/issues/12668
277-
const isNWjs = typeof windowWithMaybeExtension.nw !== 'undefined';
278-
279-
return !!runtimeId && !isDedicatedExtensionPage && !isNWjs;
273+
return !isDedicatedExtensionPage;
280274
}
281275

282-
function logBrowserEnvironmentWarnings({
283-
fetch,
284-
browserExtension,
285-
}: {
286-
fetch: boolean;
287-
browserExtension: boolean;
288-
}): void {
289-
if (browserExtension) {
290-
consoleSandbox(() => {
291-
// eslint-disable-next-line no-console
292-
console.error(
293-
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
294-
);
295-
});
296-
}
276+
function _checkBailForBrowserExtension(options: BrowserOptions): true | void {
277+
const isBrowserExtension = !options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError();
278+
279+
if (isBrowserExtension) {
280+
if (DEBUG_BUILD) {
281+
consoleSandbox(() => {
282+
// eslint-disable-next-line no-console
283+
console.error(
284+
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/',
285+
);
286+
});
287+
}
297288

298-
if (fetch) {
299-
logger.warn(
300-
'No Fetch API detected. The Sentry SDK requires a Fetch API compatible environment to send events. Please add a Fetch API polyfill.',
301-
);
289+
return true;
302290
}
303291
}
304292

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ export {
206206
supportsFetch,
207207
supportsHistory,
208208
supportsNativeFetch,
209+
// eslint-disable-next-line deprecation/deprecation
209210
supportsReferrerPolicy,
210211
supportsReportingObserver,
211212
} from './utils-hoist/supports';

packages/core/src/utils-hoist/supports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ export function supportsFetch(): boolean {
7676
}
7777

7878
try {
79-
new Headers();
8079
new Request('http://www.example.com');
81-
new Response();
8280
return true;
8381
} catch (e) {
8482
return false;
@@ -153,6 +151,8 @@ export function supportsReportingObserver(): boolean {
153151
* {@link supportsReferrerPolicy}.
154152
*
155153
* @returns Answer to the given question.
154+
*
155+
* @deprecated This method will be removed in a future version.
156156
*/
157157
export function supportsReferrerPolicy(): boolean {
158158
// Despite all stars in the sky saying that Edge supports old draft syntax, aka 'never', 'always', 'origin' and 'default'

0 commit comments

Comments
 (0)