Skip to content

Commit

Permalink
Refactor @bugsnag/plugin-client-ip (#2260)
Browse files Browse the repository at this point in the history
* refactor @bugsnag/plugin-client-ip

* fix rollup config

* update with internal client

* update extended plugin interface

* fix value type to unknown
  • Loading branch information
AnastasiiaSvietlova authored Nov 26, 2024
1 parent 4605afc commit af49fb0
Show file tree
Hide file tree
Showing 6 changed files with 71 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/es-utils/assign"]
});
46 changes: 46 additions & 0 deletions packages/plugin-client-ip/src/client-ip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Client, Plugin } from 'packages/core/types'
import assign from '@bugsnag/core/lib/es-utils/assign'

interface InternalClient extends Client {
_config: {
collectUserIp: boolean
}
}

interface ExtendedPlugin extends Plugin {
configSchema: Record<string, ValidationOption>
}

interface ValidationOption {
validate: (value: unknown) => boolean
defaultValue: () => unknown
message: string
}

/*
* Prevent collection of user IPs
*/
const plugin: ExtendedPlugin = {
load: client => {
if ((client as InternalClient)._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)
})
},
configSchema: {
collectUserIp: {
defaultValue: () => true,
message: 'should be true|false',
validate: (value: unknown) => 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 @@ -74,7 +74,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 af49fb0

Please sign in to comment.