-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2255 from bugsnag/PLAT-13092/delivery-x-domain-re…
…quest Refactor delivery-x-domain-request
- Loading branch information
Showing
12 changed files
with
130 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
packages/delivery-x-domain-request/src/match-page-protocol.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters