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

Plat 12163/browser #2252

Draft
wants to merge 6 commits into
base: integration/typescript
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"version": "8.1.2",
"main": "dist/bugsnag.js",
"types": "types/bugsnag.d.ts",
"exports": {
".": {
"types": "./types/bugsnag.d.ts",
"default": "./dist/bugsnag.js",
"import": "./dist/bugsnag.mjs"
}
},
"description": "Bugsnag error reporter for browser JavaScript",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -22,7 +29,8 @@
"scripts": {
"size": "../../bin/size dist/bugsnag.min.js",
"clean": "rm -fr dist && mkdir dist",
"build": "npm run clean && npm run build:dist && npm run build:dist:min",
"build": "npm run clean && npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"build:dist": "cross-env NODE_ENV=production bash -c '../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/extract-source-map dist/bugsnag.js'",
"build:dist:min": "cross-env NODE_ENV=production bash -c '../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/minify dist/bugsnag.min.js'",
"cdn-upload": "../../bin/cdn-upload dist/*"
Expand Down
33 changes: 33 additions & 0 deletions packages/browser/rollup.config.npm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import createRollupConfig from "../../.rollup/index.mjs";

export default createRollupConfig({
input: "src/request.ts",
external: [
'@bugsnag/core/client',
'@bugsnag/core/event',
'@bugsnag/core/session',
'@bugsnag/core/breadcrumb',
'@bugsnag/core/config',
'@bugsnag/core/types',
'@bugsnag/core/lib/es-utils/map',
'@bugsnag/core/lib/es-utils/keys',
'@bugsnag/core/lib/es-utils/assign',
'@bugsnag/plugin-window-onerror',
'@bugsnag/plugin-window-unhandled-rejection',
'@bugsnag/plugin-app-duration',
'@bugsnag/plugin-browser-device',
'@bugsnag/plugin-browser-context',
'@bugsnag/plugin-browser-request',
'@bugsnag/plugin-simple-throttle',
'@bugsnag/plugin-console-breadcrumbs',
'@bugsnag/plugin-network-breadcrumbs',
'@bugsnag/plugin-navigation-breadcrumbs',
'@bugsnag/plugin-interaction-breadcrumbs',
'@bugsnag/plugin-inline-script-content',
'@bugsnag/plugin-browser-session',
'@bugsnag/plugin-client-ip',
'@bugsnag/plugin-strip-query-string',
'@bugsnag/delivery-x-domain-request',
'@bugsnag/delivery-xml-http-request'
]
});
35 changes: 0 additions & 35 deletions packages/browser/src/config.js

This file was deleted.

25 changes: 25 additions & 0 deletions packages/browser/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { schema } from '@bugsnag/core/config'
import assign from '@bugsnag/core/lib/es-utils/assign'
import getPrefixedConsole from './get-prefixed-console'

const config = {
releaseStage: assign({}, schema.releaseStage, {
defaultValue: () => {
if (/^localhost(:\d+)?$/.test(window.location.host)) return 'development'
return 'production'
}
}),
appType: {
...schema.appType,
defaultValue: () => 'browser'
},
logger: assign({}, schema.logger, {
defaultValue: () =>
// set logger based on browser capability
(typeof console !== 'undefined' && typeof console.debug === 'function')
? getPrefixedConsole()
: undefined
})
}

export default config
17 changes: 17 additions & 0 deletions packages/browser/src/get-prefixed-console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import map from 'packages/core/lib/es-utils/map'

type LoggerMethod = 'debug' | 'info' | 'warn' | 'error'

const getPrefixedConsole = () => {
const logger: Record<string, unknown> = {}
const consoleLog = console.log
map(['debug', 'info', 'warn', 'error'], (method: LoggerMethod) => {
const consoleMethod = console[method]
logger[method] = typeof consoleMethod === 'function'
? consoleMethod.bind(console, '[bugsnag]')
: consoleLog.bind(console, '[bugsnag]')
})
return logger
}

export default getPrefixedConsole
2 changes: 0 additions & 2 deletions packages/browser/src/notifier.d.ts

This file was deleted.

110 changes: 0 additions & 110 deletions packages/browser/src/notifier.js

This file was deleted.

124 changes: 124 additions & 0 deletions packages/browser/src/notifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import Client from '@bugsnag/core/client'
import Event from '@bugsnag/core/event'
import Session from '@bugsnag/core/session'
import Breadcrumb from '@bugsnag/core/breadcrumb'
import { Config } from '@bugsnag/core/types'

import map from '@bugsnag/core/lib/es-utils/map'
import keys from '@bugsnag/core/lib/es-utils/keys'
import assign from '@bugsnag/core/lib/es-utils/assign'

// extend the base config schema with some browser-specific options
import { schema as baseConfig } from '@bugsnag/core/config'
import browserConfig from './config'

import pluginWindowOnerror from '@bugsnag/plugin-window-onerror'
import pluginUnhandledRejection from '@bugsnag/plugin-window-unhandled-rejection'
import pluginApp from '@bugsnag/plugin-app-duration'
import pluginDevice from '@bugsnag/plugin-browser-device'
import pluginContext from '@bugsnag/plugin-browser-context'
import pluginRequest from '@bugsnag/plugin-browser-request'
import pluginThrottle from '@bugsnag/plugin-simple-throttle'
import pluginConsoleBreadcrumbs from '@bugsnag/plugin-console-breadcrumbs'
import pluginNetworkBreadcrumbs from '@bugsnag/plugin-network-breadcrumbs'
import pluginNavigationBreadcrumbs from '@bugsnag/plugin-navigation-breadcrumbs'
import pluginInteractionBreadcrumbs from '@bugsnag/plugin-interaction-breadcrumbs'
import pluginInlineScriptContent from '@bugsnag/plugin-inline-script-content'
import pluginSession from '@bugsnag/plugin-browser-session'
import pluginIp from '@bugsnag/plugin-client-ip'
import pluginStripQueryString from '@bugsnag/plugin-strip-query-string'

// delivery mechanisms
import dXDomainRequest from '@bugsnag/delivery-x-domain-request'
import dXMLHttpRequest from '@bugsnag/delivery-xml-http-request'

const name = 'Bugsnag JavaScript'
const version = '__VERSION__'
const url = 'https://github.com/bugsnag/bugsnag-js'

const schema = assign({}, baseConfig, browserConfig)

declare global {
interface Window {
XDomainRequest: unknown
}
}

type BrowserClient = Partial<Client> & {
_client: Client | null
createClient: (opts?: Config) => Client
start: (opts?: Config) => Client
isStarted: () => boolean
_setDelivery?: (handler: typeof dXDomainRequest | typeof dXMLHttpRequest) => void
}

const Bugsnag: BrowserClient = {
_client: null,
createClient: (opts) => {
// handle very simple use case where user supplies just the api key as a string
if (typeof opts === 'string') opts = { apiKey: opts }
if (!opts) opts = {} as unknown as Config

const internalPlugins = [
// add browser-specific plugins
pluginApp,
pluginDevice(),
pluginContext(),
pluginRequest(),
pluginThrottle,
pluginSession,
pluginIp,
pluginStripQueryString,
pluginWindowOnerror(),
pluginUnhandledRejection(),
pluginNavigationBreadcrumbs(),
pluginInteractionBreadcrumbs(),
pluginNetworkBreadcrumbs(),
pluginConsoleBreadcrumbs,

// this one added last to avoid wrapping functionality before bugsnag uses it
pluginInlineScriptContent()
]

// configure a client with user supplied options
const bugsnag = new Client(opts, schema, internalPlugins, { name, version, url });

// set delivery based on browser capability (IE 8+9 have an XDomainRequest object)
(bugsnag as BrowserClient)._setDelivery?.(window.XDomainRequest ? dXDomainRequest : dXMLHttpRequest)

bugsnag._logger.debug('Loaded!')
bugsnag.leaveBreadcrumb('Bugsnag loaded', {}, 'state')

return bugsnag._config.autoTrackSessions
? bugsnag.startSession()
: bugsnag
},
start: (opts) => {
if (Bugsnag._client) {
Bugsnag._client._logger.warn('Bugsnag.start() was called more than once. Ignoring.')
return Bugsnag._client
}
Bugsnag._client = Bugsnag.createClient(opts)
return Bugsnag._client
},
isStarted: () => {
return Bugsnag._client != null
}
}

type Method = keyof typeof Client.prototype

map(['resetEventCount'].concat(keys(Client.prototype)) as Method[], (m) => {
if (/^_/.test(m)) return
Bugsnag[m] = function () {
if (!Bugsnag._client) return console.log(`Bugsnag.${m}() was called before Bugsnag.start()`)
Bugsnag._client._depth += 1
const ret = Bugsnag._client[m].apply(Bugsnag._client, arguments)
Bugsnag._client._depth -= 1
return ret
}
})

export { Client, Event, Session, Breadcrumb, Bugsnag }

export default Bugsnag
4 changes: 4 additions & 0 deletions packages/browser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
}
Loading
Loading