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

feat: don't require ua-parser-js in lambda-context #170

Merged
merged 1 commit into from
Nov 21, 2024
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
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;