Skip to content

Commit

Permalink
refactor @bugsnag/plugin-client-ip
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaSvietlova committed Nov 22, 2024
1 parent 97819cb commit 4937450
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
25 changes: 0 additions & 25 deletions packages/plugin-client-ip/client-ip.js

This file was deleted.

17 changes: 15 additions & 2 deletions packages/plugin-client-ip/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/plugin-client-ip",
"version": "8.1.1",
"main": "client-ip.js",
"main": "dist/client-ip.js",
"types": "dist/types/client-ip.d.ts",
"exports": {
".": {
"types": "./dist/types/client-ip.d.ts",
"default": "./dist/client-ip.js",
"import": "./dist/client-ip.mjs"
}
},
"description": "@bugsnag/js plugin to disable client IP from error reports",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,7 +20,7 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
Expand All @@ -21,5 +29,10 @@
},
"peerDependencies": {
"@bugsnag/core": "^8.0.0"
},
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
}
}
6 changes: 6 additions & 0 deletions packages/plugin-client-ip/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/client-ip.ts",
external: ["@bugsnag/core/lib/json-payload"]
});
32 changes: 32 additions & 0 deletions packages/plugin-client-ip/src/client-ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Plugin } from 'packages/core/types'
import assign from '@bugsnag/core/lib/es-utils/assign'

/*
* Prevent collection of user IPs
*/
const plugin: Plugin = {
load: client => {
// @ts-expect-error _config is private API
if (client._config.collectUserIp) return

client.addOnError(event => {
// If user.id is explicitly undefined, it will be missing from the payload. It needs
// removing so that the following line replaces it
if (event.getUser() && typeof event.getUser().id === 'undefined') {
const _user = event.getUser()
event.setUser('[REDACTED]', _user.email, _user.name)
}
event.request = assign({ clientIp: '[REDACTED]' }, event.request)
})
},
// @ts-expect-error _config is private API
configSchema: {
collectUserIp: {
defaultValue: () => true,
message: 'should be true|false',
validate: (value: boolean) => value === true || value === false
}
}
}

export default plugin
4 changes: 4 additions & 0 deletions packages/plugin-client-ip/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"packages/plugin-koa",
"packages/plugin-restify",
"packages/node",
"packages/plugin-client-ip",
"packages/plugin-strip-query-string",
"packages/plugin-strip-project-root",
"packages/plugin-interaction-breadcrumbs",
Expand Down

0 comments on commit 4937450

Please sign in to comment.