-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): Add new v7 XHR Transport #4803
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9f20419
feat(browser) add new v7 xhr transport
Lms24 b28aae1
add first test
Lms24 98d9a3d
rework xhr transport test
Lms24 4ef11f0
refactor transports test to use async/await instead of `done()`
Lms24 da67dd9
add tests for rate limit and custom headers
Lms24 aed77c1
add newXhr creation in BrowserBackend
Lms24 4672d15
fix format error caught by linter
Lms24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
import { | ||
BaseTransportOptions, | ||
createTransport, | ||
NewTransport, | ||
TransportMakeRequestResponse, | ||
TransportRequest, | ||
} from '@sentry/core'; | ||
import { SyncPromise } from '@sentry/utils'; | ||
|
||
/** | ||
* The DONE ready state for XmlHttpRequest | ||
* | ||
* Defining it here as a constant b/c XMLHttpRequest.DONE is not always defined | ||
* (e.g. during testing, it is `undefined`) | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState} | ||
*/ | ||
const XHR_READYSTATE_DONE = 4; | ||
|
||
export interface XHRTransportOptions extends BaseTransportOptions { | ||
headers?: { [key: string]: string }; | ||
} | ||
|
||
/** | ||
* Creates a Transport that uses the XMLHttpRequest API to send events to Sentry. | ||
*/ | ||
export function makeNewXHRTransport(options: XHRTransportOptions): NewTransport { | ||
function makeRequest(request: TransportRequest): PromiseLike<TransportMakeRequestResponse> { | ||
return new SyncPromise<TransportMakeRequestResponse>((resolve, _reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.onreadystatechange = (): void => { | ||
if (xhr.readyState === XHR_READYSTATE_DONE) { | ||
const response = { | ||
body: xhr.response, | ||
headers: { | ||
'x-sentry-rate-limits': xhr.getResponseHeader('X-Sentry-Rate-Limits'), | ||
'retry-after': xhr.getResponseHeader('Retry-After'), | ||
}, | ||
reason: xhr.statusText, | ||
statusCode: xhr.status, | ||
}; | ||
resolve(response); | ||
} | ||
}; | ||
|
||
xhr.open('POST', options.url); | ||
|
||
for (const header in options.headers) { | ||
if (Object.prototype.hasOwnProperty.call(options.headers, header)) { | ||
xhr.setRequestHeader(header, options.headers[header]); | ||
} | ||
} | ||
|
||
xhr.send(request.body); | ||
}); | ||
} | ||
|
||
return createTransport({ bufferSize: options.bufferSize }, makeRequest); | ||
} |
This file contains hidden or 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,109 @@ | ||
import { EventEnvelope, EventItem } from '@sentry/types'; | ||
import { createEnvelope, serializeEnvelope } from '@sentry/utils'; | ||
|
||
import { makeNewXHRTransport, XHRTransportOptions } from '../../../src/transports/new-xhr'; | ||
|
||
const DEFAULT_XHR_TRANSPORT_OPTIONS: XHRTransportOptions = { | ||
url: 'https://sentry.io/api/42/store/?sentry_key=123&sentry_version=7', | ||
}; | ||
|
||
const ERROR_ENVELOPE = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [ | ||
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }] as EventItem, | ||
]); | ||
|
||
function createXHRMock() { | ||
const retryAfterSeconds = 10; | ||
|
||
const xhrMock: Partial<XMLHttpRequest> = { | ||
open: jest.fn(), | ||
send: jest.fn(), | ||
setRequestHeader: jest.fn(), | ||
readyState: 4, | ||
status: 200, | ||
response: 'Hello World!', | ||
onreadystatechange: () => {}, | ||
getResponseHeader: jest.fn((header: string) => { | ||
switch (header) { | ||
case 'Retry-After': | ||
return '10'; | ||
case `${retryAfterSeconds}`: | ||
return; | ||
default: | ||
return `${retryAfterSeconds}:error:scope`; | ||
} | ||
}), | ||
}; | ||
|
||
// casting `window` as `any` because XMLHttpRequest is missing in Window (TS-only) | ||
jest.spyOn(window as any, 'XMLHttpRequest').mockImplementation(() => xhrMock as XMLHttpRequest); | ||
|
||
return xhrMock; | ||
} | ||
|
||
describe('NewXHRTransport', () => { | ||
const xhrMock: Partial<XMLHttpRequest> = createXHRMock(); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('makes an XHR request to the given URL', async () => { | ||
const transport = makeNewXHRTransport(DEFAULT_XHR_TRANSPORT_OPTIONS); | ||
expect(xhrMock.open).toHaveBeenCalledTimes(0); | ||
expect(xhrMock.setRequestHeader).toHaveBeenCalledTimes(0); | ||
expect(xhrMock.send).toHaveBeenCalledTimes(0); | ||
|
||
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]); | ||
|
||
expect(xhrMock.open).toHaveBeenCalledTimes(1); | ||
expect(xhrMock.open).toHaveBeenCalledWith('POST', DEFAULT_XHR_TRANSPORT_OPTIONS.url); | ||
expect(xhrMock.send).toHaveBeenCalledTimes(1); | ||
expect(xhrMock.send).toHaveBeenCalledWith(serializeEnvelope(ERROR_ENVELOPE)); | ||
}); | ||
|
||
it('returns the correct response', async () => { | ||
const transport = makeNewXHRTransport(DEFAULT_XHR_TRANSPORT_OPTIONS); | ||
|
||
const [res] = await Promise.all([ | ||
transport.send(ERROR_ENVELOPE), | ||
(xhrMock as XMLHttpRequest).onreadystatechange(null), | ||
]); | ||
|
||
expect(res).toBeDefined(); | ||
expect(res.status).toEqual('success'); | ||
}); | ||
|
||
it('sets rate limit response headers', async () => { | ||
const transport = makeNewXHRTransport(DEFAULT_XHR_TRANSPORT_OPTIONS); | ||
|
||
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]); | ||
|
||
expect(xhrMock.getResponseHeader).toHaveBeenCalledTimes(2); | ||
expect(xhrMock.getResponseHeader).toHaveBeenCalledWith('X-Sentry-Rate-Limits'); | ||
expect(xhrMock.getResponseHeader).toHaveBeenCalledWith('Retry-After'); | ||
}); | ||
|
||
it('sets custom request headers', async () => { | ||
const headers = { | ||
referrerPolicy: 'strict-origin', | ||
keepalive: 'true', | ||
referrer: 'http://example.org', | ||
}; | ||
const options: XHRTransportOptions = { | ||
...DEFAULT_XHR_TRANSPORT_OPTIONS, | ||
headers, | ||
}; | ||
|
||
const transport = makeNewXHRTransport(options); | ||
await Promise.all([transport.send(ERROR_ENVELOPE), (xhrMock as XMLHttpRequest).onreadystatechange(null)]); | ||
|
||
expect(xhrMock.setRequestHeader).toHaveBeenCalledTimes(3); | ||
expect(xhrMock.setRequestHeader).toHaveBeenCalledWith('referrerPolicy', headers.referrerPolicy); | ||
expect(xhrMock.setRequestHeader).toHaveBeenCalledWith('keepalive', headers.keepalive); | ||
expect(xhrMock.setRequestHeader).toHaveBeenCalledWith('referrer', headers.referrer); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to go with
{[key: string]: string}
instead ofRecord
as it is used that way inTransportOptions
sentry-javascript/packages/types/src/transport.ts
Lines 52 to 56 in caba96e
which are passed to
makeNewXHRTransport
. Seems to be more consistent that way IMHO