@stacktrace-lite/core
Version:
> Parse, filter, and format JavaScript stack traces with plugins for browsers and Node.js.
44 lines (43 loc) • 1.79 kB
JavaScript
/**
* A plugin that masks any detected email addresses in `StackFrame` properties.
*
* @remarks
* This plugin replaces any substring matching a simple email regular expression
* (like `user@example.com`) with a `[email]` placeholder. It operates on:
* - `fileName`
* - `functionName`
*
* Use this to help prevent accidentally logging sensitive email identifiers
* when reporting or transmitting stack frames. While this is a basic approach,
* it’s often sufficient to reduce direct exposure of personal data.
*
* **Note:** Masking PII (Personally Identifiable Information) is part of good
* data handling hygiene, especially when logs may be collected or shared.
* Industry resources confirm that masking PII helps prevent unintended disclosure
* while preserving useful structure.:contentReference[oaicite:1]{index=1}
*
* @param frames - The array of parsed {@link StackFrame} entries.
* @returns A new array of `StackFrame` where any email addresses in the
* `fileName` or `functionName` fields are replaced with `[email]`.
*
* @example
* ```ts
* import { parseStack } from './parser';
* import { piiMaskPlugin } from './plugins';
*
* const frames = parseStack(someError);
* const masked = piiMaskPlugin(frames);
*
* // Example: If a frame.fileName was "src/user/john.doe@example.com.ts",
* // after plugin it becomes "src/user/[email].ts"
* console.log(masked.map(f => f.fileName));
* ```
*/
export function piiMaskPlugin(frames) {
const EMAIL_RE = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g;
return frames.map((f) => ({
...f,
fileName: f.fileName ? f.fileName.replace(EMAIL_RE, '[email]') : f.fileName,
functionName: f.functionName ? f.functionName.replace(EMAIL_RE, '[email]') : f.functionName,
}));
}