Skip to content

Commit

Permalink
Merge branch 'master' into fix/release-after-update
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Aug 2, 2023
2 parents 5d5ac16 + ecd978a commit 2d553da
Show file tree
Hide file tree
Showing 24 changed files with 2,451 additions and 175 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## Unreleased

## 4.8.0

- feat: Update to [v7.58.0](https://github.com/getsentry/sentry-javascript/releases/tag/7.58.0) of JavaScript SDKs
(#699)
- fix: Normalize filename before parsing into module name (#699)

## 4.7.0

- feat: Update to [v7.57.0](https://github.com/getsentry/sentry-javascript/releases/tag/7.57.0) of JavaScript SDKs
(#690)

## 4.6.0

- feat: Update to [v7.50.0](https://github.com/getsentry/sentry-javascript/releases/tag/7.50.0) of JavaScript SDKs
Expand Down
18 changes: 9 additions & 9 deletions examples/electron-forge-webpack/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4491,19 +4491,19 @@ semver-compare@^1.0.0:
integrity sha512-YM3/ITh2MJ5MtzaM429anh+x2jiLVjqILF4m4oyQB18W7Ggea7BfqdH/wGMK7dDiMghv/6WG7znWMwUDzJiXow==

"semver@2 || 3 || 4 || 5", semver@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
version "5.7.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.2.tgz#48d55db737c3287cd4835e17fa13feace1c41ef8"
integrity sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==

semver@^6.2.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
version "6.3.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==

semver@^7.1.3, semver@^7.2.1, semver@^7.3.2, semver@^7.3.5:
version "7.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.1.tgz#c90c4d631cf74720e46b21c1d37ea07edfab91ec"
integrity sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==
version "7.5.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
dependencies:
lru-cache "^6.0.0"

Expand Down
12 changes: 6 additions & 6 deletions examples/electron-vite/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1204,14 +1204,14 @@ semver-compare@^1.0.0:
integrity sha512-YM3/ITh2MJ5MtzaM429anh+x2jiLVjqILF4m4oyQB18W7Ggea7BfqdH/wGMK7dDiMghv/6WG7znWMwUDzJiXow==

semver@^6.2.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
version "6.3.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==

semver@^7.3.2:
version "7.5.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.1.tgz#c90c4d631cf74720e46b21c1d37ea07edfab91ec"
integrity sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==
version "7.5.4"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.4.tgz#483986ec4ed38e1c6c48c34894a9182dbff68a6e"
integrity sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==
dependencies:
lru-cache "^6.0.0"

Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@sentry/electron",
"description": "Official Sentry SDK for Electron",
"version": "4.6.0",
"version": "4.8.0",
"main": "./index.js",
"module": "./esm/main/index.js",
"browser": "./esm/renderer/index.js",
Expand Down Expand Up @@ -58,17 +58,18 @@
"e2e": "cross-env TS_NODE_PROJECT=tsconfig.json xvfb-maybe mocha --require ts-node/register/transpile-only --retries 3 ./test/e2e/*.ts"
},
"dependencies": {
"@sentry/browser": "7.55.0",
"@sentry/core": "7.55.0",
"@sentry/node": "7.55.0",
"@sentry/types": "7.55.0",
"@sentry/utils": "7.55.0",
"@sentry/browser": "7.60.0",
"@sentry/core": "7.60.0",
"@sentry/node": "7.60.0",
"@sentry/types": "7.60.0",
"@sentry/utils": "7.60.0",
"deepmerge": "4.3.0",
"lru_map": "^0.3.3",
"tslib": "^2.5.0"
},
"devDependencies": {
"@sentry-internal/eslint-config-sdk": "7.55.0",
"@sentry-internal/typescript": "7.55.0",
"@sentry-internal/eslint-config-sdk": "7.60.0",
"@sentry-internal/typescript": "7.60.0",
"@types/busboy": "^0.2.3",
"@types/chai": "^4.2.10",
"@types/chai-as-promised": "^7.1.5",
Expand All @@ -83,7 +84,7 @@
"chai-as-promised": "^7.1.1",
"chai-subset": "^1.6.0",
"cross-env": "^7.0.3",
"electron": "23.0.0",
"electron": "25.3.0",
"electron-latest-versions": "^0.2.0",
"electron-mocha": "^11.0.2",
"eslint": "7.32.0",
Expand All @@ -106,4 +107,4 @@
"node": "18.12.1",
"yarn": "1.22.19"
}
}
}
2 changes: 1 addition & 1 deletion src/main/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,5 @@ export async function getEventDefaults(release?: string, environment?: string):
cachedDefaultsPromise = _getEventDefaults(release, environment);
}

return await cachedDefaultsPromise;
return cachedDefaultsPromise;
}
44 changes: 44 additions & 0 deletions src/main/electron-normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,47 @@ export function capturePage(window: BrowserWindow): Promise<NativeImage> {

return window.capturePage();
}

/**
* Electron >= 25 support `protocol.handle`
*/
function supportsProtocolHandle(): boolean {
return version.major >= 25;
}

interface InternalRequest {
url: string;
body?: Buffer;
}

/**
* Registers a custom protocol to receive events from the renderer
*
* Uses `protocol.handle` if available, otherwise falls back to `protocol.registerStringProtocol`
*/
export function registerProtocol(
protocol: Electron.Protocol,
scheme: string,
callback: (request: InternalRequest) => void,
): void {
if (supportsProtocolHandle()) {
protocol.handle(scheme, async (request) => {
callback({
url: request.url,
body: Buffer.from(await request.arrayBuffer()),
});

return new Response('');
});
} else {
// eslint-disable-next-line deprecation/deprecation
protocol.registerStringProtocol(scheme, (request, complete) => {
callback({
url: request.url,
body: request.uploadData?.[0]?.bytes,
});

complete('');
});
}
}
137 changes: 93 additions & 44 deletions src/main/integrations/net-breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable deprecation/deprecation */
import { getCurrentHub } from '@sentry/core';
import { Integration, Span } from '@sentry/types';
import { fill } from '@sentry/utils';
import { getCurrentHub } from '@sentry/node';
import { EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types';
import { fill, stringMatchesSomePattern } from '@sentry/utils';
import { ClientRequest, ClientRequestConstructorOptions, IncomingMessage, net } from 'electron';
import { LRUMap } from 'lru_map';
import * as urlModule from 'url';

import { OrBool, OrFalse } from '../../common/types';

type ShouldTraceFn = (method: string, url: string) => boolean;

interface NetOptions {
Expand All @@ -15,37 +14,23 @@ interface NetOptions {
*
* Defaults to: true
*/
breadcrumbs: boolean;
breadcrumbs?: boolean;
/**
* Whether to capture transaction spans for net requests
*
* true | false | (method: string, url: string) => boolean
* Defaults to: true
*/
tracing: ShouldTraceFn;
tracing?: ShouldTraceFn | boolean;

/**
* Whether to add 'sentry-trace' headers to outgoing requests
* @deprecated Use `tracePropagationTargets` client option instead.
*
* true | false | (method: string, url: string) => boolean
* Defaults to: true
* Sentry.init({
* tracePropagationTargets: ['api.site.com'],
* })
*/
tracingOrigins: ShouldTraceFn;
}

const DEFAULT_OPTIONS: NetOptions = {
breadcrumbs: true,
tracing: (_method, _url) => true,
tracingOrigins: (_method, _url) => true,
};

/** Converts all user supplied options to T | false */
export function normalizeOptions(options: Partial<OrBool<NetOptions>>): Partial<OrFalse<NetOptions>> {
return (Object.keys(options) as (keyof NetOptions)[]).reduce((obj, k) => {
if (typeof options[k] === 'function' || options[k] === false) {
obj[k] = options[k] as boolean & (false | ShouldTraceFn);
}
return obj;
}, {} as Partial<OrFalse<NetOptions>>);
tracingOrigins?: ShouldTraceFn | boolean;
}

/** http module integration */
Expand All @@ -56,22 +41,19 @@ export class Net implements Integration {
/** @inheritDoc */
public name: string = Net.id;

private readonly _options: OrFalse<NetOptions>;

/** @inheritDoc */
public constructor(options: Partial<OrBool<NetOptions>> = {}) {
this._options = {
...DEFAULT_OPTIONS,
...normalizeOptions(options),
};
}
public constructor(private readonly _options: NetOptions = {}) {}

/** @inheritDoc */
public setupOnce(): void {
public setupOnce(_addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
const clientOptions = getCurrentHub().getClient()?.getOptions();

// No need to instrument if we don't want to track anything
if (this._options.breadcrumbs || this._options.tracing) {
fill(net, 'request', createWrappedRequestFactory(this._options));
if (this._options.breadcrumbs === false && this._options.tracing === false) {
return;
}

fill(net, 'request', createWrappedRequestFactory(this._options, clientOptions?.tracePropagationTargets));
}
}

Expand Down Expand Up @@ -125,7 +107,73 @@ type RequestMethod = (opt: RequestOptions) => ClientRequest;
type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod;

/** */
function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedRequestMethodFactory {
function createWrappedRequestFactory(
options: NetOptions,
tracePropagationTargets: TracePropagationTargets | undefined,
): WrappedRequestMethodFactory {
// We're caching results so we don't have to recompute regexp every time we create a request.
const createSpanUrlMap = new LRUMap<string, boolean>(100);
const headersUrlMap = new LRUMap<string, boolean>(100);

const shouldCreateSpan = (method: string, url: string): boolean => {
if (options.tracing === undefined) {
return true;
}

if (options.tracing === false) {
return false;
}

const key = `${method}:${url}`;

const cachedDecision = createSpanUrlMap.get(key);
if (cachedDecision !== undefined) {
return cachedDecision;
}

const decision = options.tracing === true || options.tracing(method, url);
createSpanUrlMap.set(key, decision);
return decision;
};

// This will be considerably simpler once `tracingOrigins` is removed in the next major release
const shouldAttachTraceData = (method: string, url: string): boolean => {
if (options.tracingOrigins === false) {
return false;
}

// Neither integration nor client options are set or integration option is set to true
if (
(options.tracingOrigins === undefined && tracePropagationTargets === undefined) ||
options.tracingOrigins === true
) {
return true;
}

const key = `${method}:${url}`;

const cachedDecision = headersUrlMap.get(key);
if (cachedDecision !== undefined) {
return cachedDecision;
}

if (tracePropagationTargets) {
const decision = stringMatchesSomePattern(url, tracePropagationTargets);
headersUrlMap.set(key, decision);
return decision;
}

if (options.tracingOrigins) {
const decision = options.tracingOrigins(method, url);
headersUrlMap.set(key, decision);
return decision;
}

// We cannot reach here since either `tracePropagationTargets` or `tracingOrigins` will be defined but TypeScript
// cannot infer that
return true;
};

return function wrappedRequestMethodFactory(originalRequestMethod: RequestMethod): RequestMethod {
return function requestMethod(this: typeof net, reqOptions: RequestOptions): ClientRequest {
// eslint-disable-next-line @typescript-eslint/no-this-alias
Expand All @@ -140,8 +188,9 @@ function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedReque

let span: Span | undefined;

const scope = getCurrentHub().getScope();
if (scope && options.tracing && options.tracing(method, url)) {
const hub = getCurrentHub();
const scope = hub.getScope();
if (scope && shouldCreateSpan(method, url)) {
const parentSpan = scope.getSpan();

if (parentSpan) {
Expand All @@ -150,7 +199,7 @@ function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedReque
op: 'http.client',
});

if (options.tracingOrigins && options.tracingOrigins(method, url)) {
if (shouldAttachTraceData(method, url)) {
request.setHeader('sentry-trace', span.toTraceparent());
}
}
Expand All @@ -160,7 +209,7 @@ function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedReque
.once('response', function (this: ClientRequest, res: IncomingMessage): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const req = this;
if (options.breadcrumbs) {
if (options.breadcrumbs !== false) {
addRequestBreadcrumb('response', method, url, req, res);
}
if (span) {
Expand All @@ -174,7 +223,7 @@ function createWrappedRequestFactory(options: OrFalse<NetOptions>): WrappedReque
// eslint-disable-next-line @typescript-eslint/no-this-alias
const req = this;

if (options.breadcrumbs) {
if (options.breadcrumbs !== false) {
addRequestBreadcrumb('error', method, url, req, undefined);
}
if (span) {
Expand Down
Loading

0 comments on commit 2d553da

Please sign in to comment.