Skip to content

Commit 7215834

Browse files
committed
[v7] Add @sentry/transport-xhr package
1 parent b6b2a9c commit 7215834

File tree

10 files changed

+585
-0
lines changed

10 files changed

+585
-0
lines changed

packages/transport-xhr/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['../../.eslintrc.js'],
3+
};

packages/transport-xhr/.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!/dist/**/*
3+
!/esm/**/*
4+
*.tsbuildinfo

packages/transport-xhr/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2019, Sentry
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/transport-xhr/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p align="center">
2+
<a href="https://sentry.io" target="_blank" align="center">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
4+
</a>
5+
<br />
6+
</p>
7+
8+
# Sentry JavaScript SDK Hub
9+
10+
[![npm version](https://img.shields.io/npm/v/@sentry/transport-xhr.svg)](https://www.npmjs.com/package/@sentry/transport-xhr)
11+
[![npm dm](https://img.shields.io/npm/dm/@sentry/transport-xhr.svg)](https://www.npmjs.com/package/@sentry/transport-xhr)
12+
[![npm dt](https://img.shields.io/npm/dt/@sentry/transport-xhr.svg)](https://www.npmjs.com/package/@sentry/transport-xhr)
13+
[![typedoc](https://img.shields.io/badge/docs-typedoc-blue.svg)](http://getsentry.github.io/sentry-javascript/)
14+
15+
## Links
16+
17+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)
18+
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
19+
20+
## General
21+
22+
This package provides the `XHRTransport` class that implements `BaseTransport` using Fetch API.

packages/transport-xhr/package.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "@sentry/transport-xhr",
3+
"version": "7.0.0-alpha.0",
4+
"description": "Sentry transport using XHR API",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/transport-xhr",
7+
"author": "Sentry",
8+
"license": "BSD-3-Clause",
9+
"publishConfig": {
10+
"access": "public"
11+
},
12+
"main": "dist/index.js",
13+
"module": "esm/index.js",
14+
"types": "dist/index.d.ts",
15+
"dependencies": {
16+
"@sentry/transport-base": "7.0.0-alpha.0",
17+
"@sentry/types": "6.1.0",
18+
"@sentry/utils": "6.1.0",
19+
"@sentry/core": "6.1.0"
20+
},
21+
"scripts": {
22+
"build": "run-p build:es5 build:esm",
23+
"build:es5": "tsc -p tsconfig.build.json",
24+
"build:esm": "tsc -p tsconfig.esm.json",
25+
"build:watch": "run-p build:watch:es5 build:watch:esm",
26+
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
27+
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
28+
"clean": "rimraf dist coverage",
29+
"link:yarn": "yarn link",
30+
"lint": "run-s lint:prettier lint:eslint",
31+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
32+
"lint:eslint": "eslint .",
33+
"fix": "run-s fix:eslint fix:prettier",
34+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
35+
"fix:eslint": "eslint . --fix",
36+
"test": "jest -c ../../jest.config.js",
37+
"test:watch": "npm run test -- --watch",
38+
"pack": "npm pack"
39+
},
40+
"volta": {
41+
"extends": "../../package.json"
42+
},
43+
"sideEffects": false
44+
}

packages/transport-xhr/src/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
BaseTransport,
3+
Transport,
4+
TransportOptions,
5+
TransportRequest,
6+
TransportRequestMaker,
7+
TransportResponse,
8+
} from '@sentry/transport-base';
9+
10+
// TODO: Unify all transports options
11+
type XHRTransportOptions = TransportOptions;
12+
13+
export class XHRTransport extends BaseTransport implements Transport {
14+
constructor(private readonly _options: XHRTransportOptions) {
15+
super(_options);
16+
}
17+
18+
public sendRequest<T>(request: TransportRequest<T>): PromiseLike<TransportResponse> {
19+
const requestMaker: TransportRequestMaker<T> = request => {
20+
return new Promise((resolve, reject) => {
21+
const xhr = new XMLHttpRequest();
22+
23+
xhr.onerror = error => reject(error);
24+
xhr.onreadystatechange = (): void => {
25+
if (xhr.readyState === 4) {
26+
resolve({
27+
body: xhr.responseText,
28+
headers: {
29+
'x-sentry-rate-limits': xhr.getResponseHeader('X-Sentry-Rate-Limits'),
30+
'retry-after': xhr.getResponseHeader('Retry-After'),
31+
},
32+
reason: xhr.statusText,
33+
statusCode: xhr.status,
34+
});
35+
}
36+
};
37+
38+
xhr.open('POST', this._dsn.getEnvelopeEndpoint());
39+
// "When using setRequestHeader(), you must call it after calling open(), but before calling send()."
40+
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
41+
for (const header in this._options.headers) {
42+
if (Object.prototype.hasOwnProperty.call(this._options.headers, header)) {
43+
xhr.setRequestHeader(header, this._options.headers[header]);
44+
}
45+
}
46+
const body = (request.body as unknown) as BodyInit;
47+
xhr.send(body);
48+
});
49+
};
50+
51+
return super.sendRequest(request, requestMaker);
52+
}
53+
}

0 commit comments

Comments
 (0)