Skip to content

Commit

Permalink
Merge pull request #2256 from bugsnag/PLAT-13093/delivery-xml-http-re…
Browse files Browse the repository at this point in the history
…quest

Refactor delivery-xml-http-request
  • Loading branch information
gingerbenw authored Nov 22, 2024
2 parents 46873ef + fb59b53 commit 15622fb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
6 changes: 0 additions & 6 deletions packages/delivery-xml-http-request/delivery.d.ts

This file was deleted.

17 changes: 15 additions & 2 deletions packages/delivery-xml-http-request/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/delivery-xml-http-request",
"version": "8.1.1",
"main": "delivery.js",
"main": "dist/delivery.js",
"types": "dist/types/delivery.d.ts",
"exports": {
".": {
"types": "./dist/types/delivery.d.ts",
"default": "./dist/delivery.js",
"import": "./dist/delivery.mjs"
}
},
"description": "@bugsnag/js delivery mechanism for most browsers",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -12,10 +20,15 @@
"access": "public"
},
"files": [
"*.js"
"dist"
],
"author": "Bugsnag",
"license": "MIT",
"scripts": {
"build": "npm run build:npm",
"build:npm": "rollup --config rollup.config.npm.mjs",
"clean": "rm -rf dist/*"
},
"devDependencies": {
"@bugsnag/core": "^8.1.1"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/delivery-xml-http-request/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/delivery.ts",
external: ['@bugsnag/core/lib/json-payload']
});
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const payload = require('@bugsnag/core/lib/json-payload')
import type { Client, Config, Session } from '@bugsnag/core'

module.exports = (client, win = window) => ({
import payload from '@bugsnag/core/lib/json-payload'
import { Event } from 'packages/core'
import ClientWithInternals, { Delivery } from 'packages/core/client'

const delivery = (client: Client, win = window): Delivery => ({
sendEvent: (event, cb = () => {}) => {
try {
const url = client._config.endpoints.notify
const url = (client as ClientWithInternals<Required<Config>>)._config.endpoints.notify
if (url === null) {
const err = new Error('Event not sent due to incomplete endpoint configuration')
return cb(err)
}
const req = new win.XMLHttpRequest()
const body = payload.event(event, client._config.redactedKeys)
const body = payload.event(event as unknown as Event, (client as ClientWithInternals<Required<Config>>)._config.redactedKeys)

req.onreadystatechange = function () {
if (req.readyState === win.XMLHttpRequest.DONE) {
const status = req.status
if (status === 0 || status >= 400) {
const err = new Error(`Request failed with status ${status}`)
client._logger.error('Event failed to send…', err)
const err = new Error(`Request failed with status ${status}`);
(client as ClientWithInternals)._logger.error('Event failed to send…', err)
if (body.length > 10e5) {
client._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`)
(client as ClientWithInternals)._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`)
}
cb(err)
} else {
Expand All @@ -29,17 +33,17 @@ module.exports = (client, win = window) => ({

req.open('POST', url)
req.setRequestHeader('Content-Type', 'application/json')
req.setRequestHeader('Bugsnag-Api-Key', event.apiKey || client._config.apiKey)
req.setRequestHeader('Bugsnag-Api-Key', event.apiKey || (client as ClientWithInternals)._config.apiKey)
req.setRequestHeader('Bugsnag-Payload-Version', '4')
req.setRequestHeader('Bugsnag-Sent-At', (new Date()).toISOString())
req.send(body)
} catch (e) {
client._logger.error(e)
(client as ClientWithInternals)._logger.error(e)
}
},
sendSession: (session, cb = () => {}) => {
try {
const url = client._config.endpoints.sessions
const url = (client as ClientWithInternals<Required<Config>>)._config.endpoints.sessions
if (url === null) {
const err = new Error('Session not sent due to incomplete endpoint configuration')
return cb(err)
Expand All @@ -50,8 +54,8 @@ module.exports = (client, win = window) => ({
if (req.readyState === win.XMLHttpRequest.DONE) {
const status = req.status
if (status === 0 || status >= 400) {
const err = new Error(`Request failed with status ${status}`)
client._logger.error('Session failed to send…', err)
const err = new Error(`Request failed with status ${status}`);
(client as ClientWithInternals)._logger.error('Session failed to send…', err)
cb(err)
} else {
cb(null)
Expand All @@ -61,12 +65,14 @@ module.exports = (client, win = window) => ({

req.open('POST', url)
req.setRequestHeader('Content-Type', 'application/json')
req.setRequestHeader('Bugsnag-Api-Key', client._config.apiKey)
req.setRequestHeader('Bugsnag-Api-Key', (client as ClientWithInternals)._config.apiKey)
req.setRequestHeader('Bugsnag-Payload-Version', '1')
req.setRequestHeader('Bugsnag-Sent-At', (new Date()).toISOString())
req.send(payload.session(session, client._config.redactedKeys))
req.send(payload.session(session as Session, (client as ClientWithInternals<Required<Config>>)._config.redactedKeys))
} catch (e) {
client._logger.error(e)
(client as ClientWithInternals)._logger.error(e)
}
}
})

export default delivery
4 changes: 4 additions & 0 deletions packages/delivery-xml-http-request/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 @@ -63,7 +63,6 @@
"packages/core",
"packages/delivery-node",
"packages/delivery-react-native",
"packages/delivery-xml-http-request",
"packages/in-flight",
"packages/plugin-aws-lambda",
"packages/plugin-contextualize",
Expand Down

0 comments on commit 15622fb

Please sign in to comment.