Skip to content

Commit

Permalink
feat: don't require ua-parser-js in lambda-context
Browse files Browse the repository at this point in the history
BREAKING CHANGE: If you want to continue to use `ua-parser-js` in
`lambda-context` you will need to use `lambdaContextWithUaParser` and
pass in your own parser function as described here:
https://github.com/birchill/bugsnag-zero?tab=readme-ov-file#using-a-custom-user-agent-string-parser

This is because from version `ua-parser-js` version 2 onwards, users are
required to either use the AGPL version or license the library.

It's best to allow users of this package to decide how they want to
consume `ua-parser-js`.
  • Loading branch information
birtles committed Nov 21, 2024
1 parent f0e2d06 commit 3ab253c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,5 @@
"typescript": "5.6.3",
"vitest": "2.1.5",
"vitest-github-actions-reporter": "0.11.1"
},
"dependencies": {
"ua-parser-js": "^1.0.32"
}
}
10 changes: 0 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 1 addition & 12 deletions src/browser-context.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { ExtendedClientApi, Plugin } from './client';
import { BugsnagEvent } from './event';
import { parseUserAgent } from './simple-ua-parser';

export type UserAgentInfo = {
browserName?: string;
browserVersion?: string;
osName?: string;
osVersion?: string;
manufacturer?: string;
model?: string;
modelNumber?: string;
};

export type UserAgentParserFn = (userAgent: string) => UserAgentInfo;
import type { UserAgentParserFn } from './user-agent-types';

export const browserContextWithUaParser = (
uaParser: UserAgentParserFn
Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,7 @@ export { nodeNotifyUnhandledExceptions } from './node-unhandled-exceptions';

// Other plugins
export { appDuration } from './app-duration';
export {
browserContext,
browserContextWithUaParser,
UserAgentParserFn,
UserAgentInfo,
} from './browser-context';
export { browserContext, browserContextWithUaParser } from './browser-context';
export { deviceOrientation } from './deviceorientation';
export { limitEvents } from './limit-events';
export {
Expand All @@ -365,6 +360,7 @@ export {
redactObject,
} from './redact-keys';
export { stringifyValues } from './stringify-values';
export type { UserAgentParserFn, UserAgentInfo } from './user-agent-types';

// Delivery plugins
export { FetchDelivery } from './fetch-delivery';
Expand Down
32 changes: 23 additions & 9 deletions src/lambda-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
Context,
} from 'aws-lambda';
import { platform as osPlatform, release as osRelease } from 'os';
import UAParser from 'ua-parser-js';

import { ExtendedClientApi, Plugin } from './client';
import { BugsnagEvent } from './event';
import { parseUserAgent } from './simple-ua-parser';
import { toExceptions } from './to-exceptions';
import { UserAgentParserFn } from './user-agent-types';

export interface LambdaContextPlugin {
setContext(
Expand All @@ -22,7 +23,8 @@ export interface LambdaContextPlugin {
};
}

export const lambdaContext = (
export const lambdaContextWithUaParser = (
uaParser: UserAgentParserFn,
initialEvent?: Record<string, any>,
initialContext?: Context
): Plugin => {
Expand Down Expand Up @@ -157,9 +159,7 @@ export const lambdaContext = (
lambdaEvent.requestContext.identity.userAgent || undefined;
}

const browser = userAgent
? new UAParser(userAgent).getBrowser()
: undefined;
const browser = userAgent ? uaParser(userAgent) : undefined;
const hostname = lambdaEvent?.requestContext?.domainName;

let timeEpoch: number | undefined;
Expand All @@ -171,12 +171,15 @@ export const lambdaContext = (

event.device = {
hostname,
osName: osPlatform(),
osVersion: osRelease(),
osName: browser?.osName || osPlatform(),
osVersion: browser?.osVersion || osRelease(),
freeMemory,
totalMemory,
browserName: browser?.name,
browserVersion: browser?.version,
browserName: browser?.browserName,
browserVersion: browser?.browserVersion,
manufacturer: browser?.manufacturer,
model: browser?.model,
modelNumber: browser?.modelNumber,
runtimeVersions: {
node: process.version,
},
Expand Down Expand Up @@ -284,6 +287,17 @@ export const lambdaContext = (
};
};

export const lambdaContext = (
initialEvent?: Record<string, any>,
initialContext?: Context
): Plugin => {
return lambdaContextWithUaParser(
parseUserAgent,
initialEvent,
initialContext
);
};

function isGwV2Event(
event: Record<string, any> | undefined
): event is APIGatewayProxyEventV2 {
Expand Down
2 changes: 1 addition & 1 deletion src/simple-ua-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserAgentInfo } from './browser-context';
import type { UserAgentInfo } from './user-agent-types';

// The Bugsnag v5 API requires doing your own UA string parsing, requiring a
// `browserName`, `browserVersion`, `osName`, `osVersion`, etc.
Expand Down
11 changes: 11 additions & 0 deletions src/user-agent-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type UserAgentInfo = {
browserName?: string;
browserVersion?: string;
osName?: string;
osVersion?: string;
manufacturer?: string;
model?: string;
modelNumber?: string;
};

export type UserAgentParserFn = (userAgent: string) => UserAgentInfo;

0 comments on commit 3ab253c

Please sign in to comment.