From f959990096cb89ba41746c27076c3f6ad5334977 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Thu, 20 Jan 2022 13:04:03 -0500 Subject: [PATCH 1/4] feat(rewriteframes): Introduce RewriteFrames package --- package.json | 1 + .../.eslintrc.js | 3 + .../.npmignore | 4 + .../integration-common-rewriteframes/LICENSE | 29 ++++ .../README.md | 57 ++++++++ .../package.json | 72 ++++++++++ .../src/index.ts | 132 +++++++++++++++++ .../test/index.test.ts} | 2 +- .../tsconfig.cjs.json | 8 ++ .../tsconfig.esm.json | 8 ++ .../tsconfig.json | 9 ++ .../tsconfig.test.json | 12 ++ packages/integrations/package.json | 1 + packages/integrations/src/rewriteframes.ts | 133 +----------------- 14 files changed, 338 insertions(+), 133 deletions(-) create mode 100644 packages/integration-common-rewriteframes/.eslintrc.js create mode 100644 packages/integration-common-rewriteframes/.npmignore create mode 100644 packages/integration-common-rewriteframes/LICENSE create mode 100644 packages/integration-common-rewriteframes/README.md create mode 100644 packages/integration-common-rewriteframes/package.json create mode 100644 packages/integration-common-rewriteframes/src/index.ts rename packages/{integrations/test/rewriteframes.test.ts => integration-common-rewriteframes/test/index.test.ts} (99%) create mode 100644 packages/integration-common-rewriteframes/tsconfig.cjs.json create mode 100644 packages/integration-common-rewriteframes/tsconfig.esm.json create mode 100644 packages/integration-common-rewriteframes/tsconfig.json create mode 100644 packages/integration-common-rewriteframes/tsconfig.test.json diff --git a/package.json b/package.json index 9d3710013bf2..6e86e8e3cf16 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "packages/eslint-plugin-sdk", "packages/gatsby", "packages/hub", + "packages/integration-common-rewriteframes", "packages/integration-tests", "packages/integrations", "packages/minimal", diff --git a/packages/integration-common-rewriteframes/.eslintrc.js b/packages/integration-common-rewriteframes/.eslintrc.js new file mode 100644 index 000000000000..5a2cc7f1ec08 --- /dev/null +++ b/packages/integration-common-rewriteframes/.eslintrc.js @@ -0,0 +1,3 @@ +module.exports = { + extends: ['../../.eslintrc.js'], +}; diff --git a/packages/integration-common-rewriteframes/.npmignore b/packages/integration-common-rewriteframes/.npmignore new file mode 100644 index 000000000000..14e80551ae7c --- /dev/null +++ b/packages/integration-common-rewriteframes/.npmignore @@ -0,0 +1,4 @@ +* +!/dist/**/* +!/esm/**/* +*.tsbuildinfo diff --git a/packages/integration-common-rewriteframes/LICENSE b/packages/integration-common-rewriteframes/LICENSE new file mode 100644 index 000000000000..3a6c98ad2910 --- /dev/null +++ b/packages/integration-common-rewriteframes/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2022, Sentry +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/packages/integration-common-rewriteframes/README.md b/packages/integration-common-rewriteframes/README.md new file mode 100644 index 000000000000..e19a2edfc763 --- /dev/null +++ b/packages/integration-common-rewriteframes/README.md @@ -0,0 +1,57 @@ +

+ + + +
+

+ +# Sentry JavaScript RewriteFrames integration + +[![npm version](https://img.shields.io/npm/v/@sentry/integration-common-rewriteframes.svg)](https://www.npmjs.com/package/@sentry/wasm) +[![npm dm](https://img.shields.io/npm/dm/@sentry/integration-common-rewriteframes.svg)](https://www.npmjs.com/package/@sentry/wasm) +[![npm dt](https://img.shields.io/npm/dt/@sentry/integration-common-rewriteframes.svg)](https://www.npmjs.com/package/@sentry/wasm) +[![typedoc](https://img.shields.io/badge/docs-typedoc-blue.svg)](http://getsentry.github.io/sentry-javascript/) + +## Links + +- [Official SDK Docs](https://docs.sentry.io/quickstart/) +- [TypeDoc](http://getsentry.github.io/sentry-javascript/) + +## General + +This integration allows you to apply a transformation to each frame of the stack trace. In the streamlined scenario, it can be used to change the name of the file frame it originates from, or it can be fed with an iterated function to apply any arbitrary transformation. + +On Windows machines, you have to use Unix paths and skip the volume letter in root option to enable. For example `C:\\Program Files\\Apache\\www` won’t work, however, `/Program Files/Apache/www` will. + +Available options: + +```js +import * as Sentry from "@sentry/browser"; +import { RewriteFrames } from "@sentry/integration-common-rewriteframes"; + +Sentry.init({ + dsn: "___PUBLIC_DSN___", + integrations: [new RewriteFrames( + { + // root path that will be stripped from the current frame's filename by the default iteratee if the filename is an absolute path + root: string; + + // a custom prefix that will be used by the default iteratee (default: `app://`) + prefix: string; + + // function that takes the frame, applies a transformation, and returns it + iteratee: (frame) => frame; + } + )], +}); +``` + +#### Usage Examples + +For example, if the full path to your file is `/www/src/app/file.js`: + +| Usage | Path in Stack Trace | Description | +| --------------------------------- | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | +| `RewriteFrames()` | `app:///file.js` | The default behavior is to replace the absolute path, except the filename, and prefix it with the default prefix (`app:///`). | +| `RewriteFrames({prefix: 'foo/'})` | `foo/file.js` | Prefix `foo/` is used instead of the default prefix `app:///`. | +| `RewriteFrames({root: '/www'})` | `app:///src/app/file.js` | `root` is defined as `/www`, so only that part is trimmed from beginning of the path. | diff --git a/packages/integration-common-rewriteframes/package.json b/packages/integration-common-rewriteframes/package.json new file mode 100644 index 000000000000..30986e86acac --- /dev/null +++ b/packages/integration-common-rewriteframes/package.json @@ -0,0 +1,72 @@ +{ + "name": "@sentry/integration-common-rewriteframes", + "version": "6.17.0-beta.0", + "description": "Integration that allows users to apply a transformation to each frame of the stack trace", + "repository": "git://github.com/getsentry/sentry-javascript.git", + "homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/integration-common-rewriteframes", + "author": "Sentry", + "license": "BSD-3-Clause", + "engines": { + "node": ">=6" + }, + "publishConfig": { + "access": "public" + }, + "main": "dist/index.js", + "module": "esm/index.js", + "types": "dist/index.d.ts", + "dependencies": { + "@sentry/types": "6.17.0-beta.0", + "@sentry/utils": "6.17.0-beta.0", + "tslib": "^1.9.3" + }, + "devDependencies": {}, + "scripts": { + "build": "run-p build:cjs build:esm", + "build:cjs": "tsc -p tsconfig.cjs.json", + "build:dev": "run-s build", + "build:es5": "yarn build:cjs # *** backwards compatibility - remove in v7 ***", + "build:esm": "tsc -p tsconfig.esm.json", + "build:watch": "run-p build:cjs:watch build:esm:watch", + "build:cjs:watch": "tsc -p tsconfig.cjs.json -w --preserveWatchOutput", + "build:dev:watch": "run-s build:watch", + "build:es5:watch": "yarn build:cjs:watch # *** backwards compatibility - remove in v7 ***", + "build:esm:watch": "tsc -p tsconfig.esm.json -w --preserveWatchOutput", + "circularDepCheck": "madge --circular src/index.ts", + "clean": "rimraf dist esm coverage", + "fix": "run-s fix:eslint fix:prettier", + "fix:eslint": "eslint . --format stylish --fix", + "fix:prettier": "prettier --write \"{src,test}/**/*.ts\"", + "link:yarn": "yarn link", + "lint": "run-s lint:prettier lint:eslint", + "lint:eslint": "eslint . --cache --cache-location '../../eslintcache/' --format stylish", + "lint:prettier": "prettier --check \"{src,test}/**/*.ts\"", + "pack": "npm pack", + "test": "jest", + "test:watch": "jest --watch" + }, + "volta": { + "extends": "../../package.json" + }, + "jest": { + "collectCoverage": true, + "transform": { + "^.+\\.ts$": "ts-jest" + }, + "moduleFileExtensions": [ + "js", + "ts" + ], + "testEnvironment": "node", + "testMatch": [ + "**/*.test.ts" + ], + "globals": { + "ts-jest": { + "tsConfig": "./tsconfig.json", + "diagnostics": false + } + } + }, + "sideEffects": false +} diff --git a/packages/integration-common-rewriteframes/src/index.ts b/packages/integration-common-rewriteframes/src/index.ts new file mode 100644 index 000000000000..9eb95e54f6d3 --- /dev/null +++ b/packages/integration-common-rewriteframes/src/index.ts @@ -0,0 +1,132 @@ +import { Event, EventProcessor, Hub, Integration, StackFrame, Stacktrace } from '@sentry/types'; +import { basename, relative } from '@sentry/utils'; + +type StackFrameIteratee = (frame: StackFrame) => StackFrame; + +/** Rewrite event frames paths */ +export class RewriteFrames implements Integration { + /** + * @inheritDoc + */ + public static id: string = 'RewriteFrames'; + + /** + * @inheritDoc + */ + public name: string = RewriteFrames.id; + + /** + * @inheritDoc + */ + private readonly _root?: string; + + /** + * @inheritDoc + */ + private readonly _prefix: string = 'app:///'; + + /** + * @inheritDoc + */ + public constructor(options: { root?: string; prefix?: string; iteratee?: StackFrameIteratee } = {}) { + if (options.root) { + this._root = options.root; + } + if (options.prefix) { + this._prefix = options.prefix; + } + if (options.iteratee) { + this._iteratee = options.iteratee; + } + } + + /** + * @inheritDoc + */ + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { + addGlobalEventProcessor(event => { + const self = getCurrentHub().getIntegration(RewriteFrames); + if (self) { + return self.process(event); + } + return event; + }); + } + + /** JSDoc */ + public process(originalEvent: Event): Event { + let processedEvent = originalEvent; + + if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) { + processedEvent = this._processExceptionsEvent(processedEvent); + } + + if (originalEvent.stacktrace) { + processedEvent = this._processStacktraceEvent(processedEvent); + } + + return processedEvent; + } + + /** + * @inheritDoc + */ + private readonly _iteratee: StackFrameIteratee = (frame: StackFrame) => { + if (!frame.filename) { + return frame; + } + // Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\` + const isWindowsFrame = /^[A-Z]:\\/.test(frame.filename); + const startsWithSlash = /^\//.test(frame.filename); + if (isWindowsFrame || startsWithSlash) { + const filename = isWindowsFrame + ? frame.filename + .replace(/^[A-Z]:/, '') // remove Windows-style prefix + .replace(/\\/g, '/') // replace all `\\` instances with `/` + : frame.filename; + const base = this._root ? relative(this._root, filename) : basename(filename); + frame.filename = `${this._prefix}${base}`; + } + return frame; + }; + + /** JSDoc */ + private _processExceptionsEvent(event: Event): Event { + try { + return { + ...event, + exception: { + ...event.exception, + // The check for this is performed inside `process` call itself, safe to skip here + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + values: event.exception!.values!.map(value => ({ + ...value, + stacktrace: this._processStacktrace(value.stacktrace), + })), + }, + }; + } catch (_oO) { + return event; + } + } + + /** JSDoc */ + private _processStacktraceEvent(event: Event): Event { + try { + return { + ...event, + stacktrace: this._processStacktrace(event.stacktrace), + }; + } catch (_oO) { + return event; + } + } + + /** JSDoc */ + private _processStacktrace(stacktrace?: Stacktrace): Stacktrace { + return { + ...stacktrace, + frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => this._iteratee(f)), + }; + } +} diff --git a/packages/integrations/test/rewriteframes.test.ts b/packages/integration-common-rewriteframes/test/index.test.ts similarity index 99% rename from packages/integrations/test/rewriteframes.test.ts rename to packages/integration-common-rewriteframes/test/index.test.ts index 92346d99ebcb..8e169cc46e1c 100644 --- a/packages/integrations/test/rewriteframes.test.ts +++ b/packages/integration-common-rewriteframes/test/index.test.ts @@ -1,6 +1,6 @@ import { Event, StackFrame } from '@sentry/types'; -import { RewriteFrames } from '../src/rewriteframes'; +import { RewriteFrames } from '../src'; let rewriteFrames: RewriteFrames; let messageEvent: Event; diff --git a/packages/integration-common-rewriteframes/tsconfig.cjs.json b/packages/integration-common-rewriteframes/tsconfig.cjs.json new file mode 100644 index 000000000000..abd80f77e1ff --- /dev/null +++ b/packages/integration-common-rewriteframes/tsconfig.cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + + "compilerOptions": { + "module": "commonjs", + "outDir": "dist" + } +} diff --git a/packages/integration-common-rewriteframes/tsconfig.esm.json b/packages/integration-common-rewriteframes/tsconfig.esm.json new file mode 100644 index 000000000000..b6ee3fa615c0 --- /dev/null +++ b/packages/integration-common-rewriteframes/tsconfig.esm.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + + "compilerOptions": { + "module": "es6", + "outDir": "esm" + } +} diff --git a/packages/integration-common-rewriteframes/tsconfig.json b/packages/integration-common-rewriteframes/tsconfig.json new file mode 100644 index 000000000000..bf45a09f2d71 --- /dev/null +++ b/packages/integration-common-rewriteframes/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + + "include": ["src/**/*"], + + "compilerOptions": { + // package-specific options + } +} diff --git a/packages/integration-common-rewriteframes/tsconfig.test.json b/packages/integration-common-rewriteframes/tsconfig.test.json new file mode 100644 index 000000000000..87f6afa06b86 --- /dev/null +++ b/packages/integration-common-rewriteframes/tsconfig.test.json @@ -0,0 +1,12 @@ +{ + "extends": "./tsconfig.json", + + "include": ["test/**/*"], + + "compilerOptions": { + // should include all types from `./tsconfig.json` plus types for all test frameworks used + "types": ["node", "jest"] + + // other package-specific, test-specific options + } +} diff --git a/packages/integrations/package.json b/packages/integrations/package.json index 98f75c54b0ca..76b1add566a6 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -16,6 +16,7 @@ "module": "esm/index.js", "types": "dist/index.d.ts", "dependencies": { + "@sentry/integration-common-rewriteframes": "6.17.0-beta.0", "@sentry/types": "6.17.0-beta.0", "@sentry/utils": "6.17.0-beta.0", "localforage": "^1.8.1", diff --git a/packages/integrations/src/rewriteframes.ts b/packages/integrations/src/rewriteframes.ts index 9eb95e54f6d3..661002b4f87e 100644 --- a/packages/integrations/src/rewriteframes.ts +++ b/packages/integrations/src/rewriteframes.ts @@ -1,132 +1 @@ -import { Event, EventProcessor, Hub, Integration, StackFrame, Stacktrace } from '@sentry/types'; -import { basename, relative } from '@sentry/utils'; - -type StackFrameIteratee = (frame: StackFrame) => StackFrame; - -/** Rewrite event frames paths */ -export class RewriteFrames implements Integration { - /** - * @inheritDoc - */ - public static id: string = 'RewriteFrames'; - - /** - * @inheritDoc - */ - public name: string = RewriteFrames.id; - - /** - * @inheritDoc - */ - private readonly _root?: string; - - /** - * @inheritDoc - */ - private readonly _prefix: string = 'app:///'; - - /** - * @inheritDoc - */ - public constructor(options: { root?: string; prefix?: string; iteratee?: StackFrameIteratee } = {}) { - if (options.root) { - this._root = options.root; - } - if (options.prefix) { - this._prefix = options.prefix; - } - if (options.iteratee) { - this._iteratee = options.iteratee; - } - } - - /** - * @inheritDoc - */ - public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { - addGlobalEventProcessor(event => { - const self = getCurrentHub().getIntegration(RewriteFrames); - if (self) { - return self.process(event); - } - return event; - }); - } - - /** JSDoc */ - public process(originalEvent: Event): Event { - let processedEvent = originalEvent; - - if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) { - processedEvent = this._processExceptionsEvent(processedEvent); - } - - if (originalEvent.stacktrace) { - processedEvent = this._processStacktraceEvent(processedEvent); - } - - return processedEvent; - } - - /** - * @inheritDoc - */ - private readonly _iteratee: StackFrameIteratee = (frame: StackFrame) => { - if (!frame.filename) { - return frame; - } - // Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\` - const isWindowsFrame = /^[A-Z]:\\/.test(frame.filename); - const startsWithSlash = /^\//.test(frame.filename); - if (isWindowsFrame || startsWithSlash) { - const filename = isWindowsFrame - ? frame.filename - .replace(/^[A-Z]:/, '') // remove Windows-style prefix - .replace(/\\/g, '/') // replace all `\\` instances with `/` - : frame.filename; - const base = this._root ? relative(this._root, filename) : basename(filename); - frame.filename = `${this._prefix}${base}`; - } - return frame; - }; - - /** JSDoc */ - private _processExceptionsEvent(event: Event): Event { - try { - return { - ...event, - exception: { - ...event.exception, - // The check for this is performed inside `process` call itself, safe to skip here - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - values: event.exception!.values!.map(value => ({ - ...value, - stacktrace: this._processStacktrace(value.stacktrace), - })), - }, - }; - } catch (_oO) { - return event; - } - } - - /** JSDoc */ - private _processStacktraceEvent(event: Event): Event { - try { - return { - ...event, - stacktrace: this._processStacktrace(event.stacktrace), - }; - } catch (_oO) { - return event; - } - } - - /** JSDoc */ - private _processStacktrace(stacktrace?: Stacktrace): Stacktrace { - return { - ...stacktrace, - frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => this._iteratee(f)), - }; - } -} +export { RewriteFrames } from '@sentry/integration-common-rewriteframes'; From 5c1f97c42a41565c9ad95ca5fccb8966ab5d474d Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 21 Jan 2022 09:26:01 -0500 Subject: [PATCH 2/4] testing --- packages/integrations/src/rewriteframes.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/integrations/src/rewriteframes.ts b/packages/integrations/src/rewriteframes.ts index 661002b4f87e..0578de7002ea 100644 --- a/packages/integrations/src/rewriteframes.ts +++ b/packages/integrations/src/rewriteframes.ts @@ -1 +1,2 @@ -export { RewriteFrames } from '@sentry/integration-common-rewriteframes'; +// export { RewriteFrames } from '@sentry/integration-common-rewriteframes'; +console.log('hello'); From 795db117e8740c396798be4c9f67267f1fd2db45 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 21 Jan 2022 09:33:05 -0500 Subject: [PATCH 3/4] ok --- packages/integrations/src/rewriteframes.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/integrations/src/rewriteframes.ts b/packages/integrations/src/rewriteframes.ts index 0578de7002ea..d85114cb5b80 100644 --- a/packages/integrations/src/rewriteframes.ts +++ b/packages/integrations/src/rewriteframes.ts @@ -1,2 +1,5 @@ // export { RewriteFrames } from '@sentry/integration-common-rewriteframes'; -console.log('hello'); +/** JSDoc */ +export function RewriteFrames(): void { + console.log('hello'); +} From 97cc43c4f45c800a415d990203f70987cf803bae Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Fri, 21 Jan 2022 09:39:56 -0500 Subject: [PATCH 4/4] add it back --- packages/integrations/package.json | 1 - packages/integrations/src/rewriteframes.ts | 135 ++++++++++++++++++++- 2 files changed, 131 insertions(+), 5 deletions(-) diff --git a/packages/integrations/package.json b/packages/integrations/package.json index 76b1add566a6..98f75c54b0ca 100644 --- a/packages/integrations/package.json +++ b/packages/integrations/package.json @@ -16,7 +16,6 @@ "module": "esm/index.js", "types": "dist/index.d.ts", "dependencies": { - "@sentry/integration-common-rewriteframes": "6.17.0-beta.0", "@sentry/types": "6.17.0-beta.0", "@sentry/utils": "6.17.0-beta.0", "localforage": "^1.8.1", diff --git a/packages/integrations/src/rewriteframes.ts b/packages/integrations/src/rewriteframes.ts index d85114cb5b80..9eb95e54f6d3 100644 --- a/packages/integrations/src/rewriteframes.ts +++ b/packages/integrations/src/rewriteframes.ts @@ -1,5 +1,132 @@ -// export { RewriteFrames } from '@sentry/integration-common-rewriteframes'; -/** JSDoc */ -export function RewriteFrames(): void { - console.log('hello'); +import { Event, EventProcessor, Hub, Integration, StackFrame, Stacktrace } from '@sentry/types'; +import { basename, relative } from '@sentry/utils'; + +type StackFrameIteratee = (frame: StackFrame) => StackFrame; + +/** Rewrite event frames paths */ +export class RewriteFrames implements Integration { + /** + * @inheritDoc + */ + public static id: string = 'RewriteFrames'; + + /** + * @inheritDoc + */ + public name: string = RewriteFrames.id; + + /** + * @inheritDoc + */ + private readonly _root?: string; + + /** + * @inheritDoc + */ + private readonly _prefix: string = 'app:///'; + + /** + * @inheritDoc + */ + public constructor(options: { root?: string; prefix?: string; iteratee?: StackFrameIteratee } = {}) { + if (options.root) { + this._root = options.root; + } + if (options.prefix) { + this._prefix = options.prefix; + } + if (options.iteratee) { + this._iteratee = options.iteratee; + } + } + + /** + * @inheritDoc + */ + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { + addGlobalEventProcessor(event => { + const self = getCurrentHub().getIntegration(RewriteFrames); + if (self) { + return self.process(event); + } + return event; + }); + } + + /** JSDoc */ + public process(originalEvent: Event): Event { + let processedEvent = originalEvent; + + if (originalEvent.exception && Array.isArray(originalEvent.exception.values)) { + processedEvent = this._processExceptionsEvent(processedEvent); + } + + if (originalEvent.stacktrace) { + processedEvent = this._processStacktraceEvent(processedEvent); + } + + return processedEvent; + } + + /** + * @inheritDoc + */ + private readonly _iteratee: StackFrameIteratee = (frame: StackFrame) => { + if (!frame.filename) { + return frame; + } + // Check if the frame filename begins with `/` or a Windows-style prefix such as `C:\` + const isWindowsFrame = /^[A-Z]:\\/.test(frame.filename); + const startsWithSlash = /^\//.test(frame.filename); + if (isWindowsFrame || startsWithSlash) { + const filename = isWindowsFrame + ? frame.filename + .replace(/^[A-Z]:/, '') // remove Windows-style prefix + .replace(/\\/g, '/') // replace all `\\` instances with `/` + : frame.filename; + const base = this._root ? relative(this._root, filename) : basename(filename); + frame.filename = `${this._prefix}${base}`; + } + return frame; + }; + + /** JSDoc */ + private _processExceptionsEvent(event: Event): Event { + try { + return { + ...event, + exception: { + ...event.exception, + // The check for this is performed inside `process` call itself, safe to skip here + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + values: event.exception!.values!.map(value => ({ + ...value, + stacktrace: this._processStacktrace(value.stacktrace), + })), + }, + }; + } catch (_oO) { + return event; + } + } + + /** JSDoc */ + private _processStacktraceEvent(event: Event): Event { + try { + return { + ...event, + stacktrace: this._processStacktrace(event.stacktrace), + }; + } catch (_oO) { + return event; + } + } + + /** JSDoc */ + private _processStacktrace(stacktrace?: Stacktrace): Stacktrace { + return { + ...stacktrace, + frames: stacktrace && stacktrace.frames && stacktrace.frames.map(f => this._iteratee(f)), + }; + } }