Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor @bugsnag/plugin-client-ip #2260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading