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 delivery-x-domain-request #2255

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface LoggerConfig {
debug: (msg: any) => void
info: (msg: any) => void
warn: (msg: any) => void
error: (msg: any) => void
error: (msg: any, err?: unknown) => void
}

interface Notifier {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/lib/json-payload.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type RedactedKey = string | RegExp

interface JsonPayload {
event: (event: Object, redactedKeys?: RedactedKey[]) => string
session: (session: Object, redactedKeys?: RedactedKey[]) => string
}

declare const jsonPayload: JsonPayload

export default jsonPayload
11 changes: 0 additions & 11 deletions packages/delivery-x-domain-request/delivery.d.ts

This file was deleted.

70 changes: 0 additions & 70 deletions packages/delivery-x-domain-request/delivery.js

This file was deleted.

15 changes: 14 additions & 1 deletion packages/delivery-x-domain-request/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "@bugsnag/delivery-x-domain-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 IE 8, 9 and 10",
"homepage": "https://www.bugsnag.com/",
"repository": {
Expand All @@ -16,6 +24,11 @@
],
AnastasiiaSvietlova marked this conversation as resolved.
Show resolved Hide resolved
"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-x-domain-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']
});
74 changes: 74 additions & 0 deletions packages/delivery-x-domain-request/src/delivery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable import/no-duplicates */
import payload from '@bugsnag/core/lib/json-payload'

import { Client, Config, Event, Session } from '@bugsnag/core'
import getApiUrl from './get-api-url'
import matchPageProtocol from './match-page-protocol'

import type ClientWithInternals from 'packages/core/client'
import type { Delivery } from 'packages/core/client'

const delivery = (client: Client, win = window): Delivery => ({
sendEvent: (event, cb = () => {}) => {
if ((client as ClientWithInternals)._config.endpoints?.notify === null) {
const err = new Error('Event not sent due to incomplete endpoint configuration')
return cb(err)
}

const url = getApiUrl((client as ClientWithInternals<Required<Config>>)._config, 'notify', '4', win)
const body = payload.event(event as unknown as Event, (client as ClientWithInternals<Required<Config>>)._config.redactedKeys)

// @ts-expect-error XDomainRequest is not defined in the Window interface
const req = new win.XDomainRequest()
req.onload = function () {
cb(null)
}
req.onerror = function () {
const err = new Error('Event failed to send');
(client as ClientWithInternals)._logger.error('Event failed to send…', err)
if (body.length > 10e5) {
(client as ClientWithInternals)._logger.warn(`Event oversized (${(body.length / 10e5).toFixed(2)} MB)`)
}
cb(err)
}
req.open('POST', url)
setTimeout(() => {
try {
req.send(body)
} catch (e) {
(client as ClientWithInternals)._logger.error(e)
if (e instanceof Error || e === undefined || e === null) {
cb(e)
}
}
}, 0)
},
sendSession: (session, cb = () => {}) => {
if ((client as ClientWithInternals<Required<Config>>)._config.endpoints.sessions === null) {
const err = new Error('Session not sent due to incomplete endpoint configuration')
return cb(err)
}

const url = getApiUrl((client as ClientWithInternals<Required<Config>>)._config, 'sessions', '1', win)
// @ts-expect-error XDomainRequest is not defined in the Window interface
const req = new win.XDomainRequest()
req.onload = function () {
cb(null)
}
req.open('POST', url)
setTimeout(() => {
try {
req.send(payload.session(session as Session, (client as ClientWithInternals<Required<Config>>)._config.redactedKeys))
} catch (e) {
(client as ClientWithInternals)._logger.error(e)
if (e instanceof Error || e === undefined || e === null) {
cb(e)
}
}
}, 0)
}
})

delivery._matchPageProtocol = matchPageProtocol

export default delivery
13 changes: 13 additions & 0 deletions packages/delivery-x-domain-request/src/get-api-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Config } from '@bugsnag/core'
import matchPageProtocol from './match-page-protocol'

const getApiUrl = (config: Required<Config>, endpoint: 'notify' | 'sessions', version: string, win: Window & typeof globalThis) => {
// IE8 doesn't support Date.prototype.toISOstring(), but it does convert a date
// to an ISO string when you use JSON stringify. Simply parsing the result of
// JSON.stringify is smaller than using a toISOstring() polyfill.
const isoDate = JSON.parse(JSON.stringify(new Date()))
const url = matchPageProtocol(config.endpoints[endpoint], win.location.protocol)
return `${url}?apiKey=${encodeURIComponent(config.apiKey)}&payloadVersion=${version}&sentAt=${encodeURIComponent(isoDate)}`
}

export default getApiUrl
6 changes: 6 additions & 0 deletions packages/delivery-x-domain-request/src/match-page-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const matchPageProtocol = (endpoint: string, pageProtocol: string) =>
pageProtocol === 'http:'
? endpoint.replace(/^https:/, 'http:')
: endpoint

export default matchPageProtocol
4 changes: 4 additions & 0 deletions packages/delivery-x-domain-request/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*.ts"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ android {
}

dependencies {
api "com.bugsnag:bugsnag-android:6.+"
compileOnly "com.bugsnag:bugsnag-android:6.+"
implementation 'com.facebook.react:react-native:+'
}

Expand Down
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-x-domain-request",
"packages/delivery-xml-http-request",
"packages/in-flight",
"packages/plugin-aws-lambda",
Expand Down
Loading