Skip to content

Commit 7eb6e08

Browse files
committed
[v7] Add @sentry/transport-http package
1 parent 7215834 commit 7eb6e08

File tree

10 files changed

+578
-0
lines changed

10 files changed

+578
-0
lines changed

packages/transport-http/.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-http/.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-http/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-http/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-http.svg)](https://www.npmjs.com/package/@sentry/transport-http)
11+
[![npm dm](https://img.shields.io/npm/dm/@sentry/transport-http.svg)](https://www.npmjs.com/package/@sentry/transport-http)
12+
[![npm dt](https://img.shields.io/npm/dt/@sentry/transport-http.svg)](https://www.npmjs.com/package/@sentry/transport-http)
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 `HTTPTransport` class that implements `BaseTransport` using Node HTTP module.

packages/transport-http/package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@sentry/transport-http",
3+
"version": "7.0.0-alpha.0",
4+
"description": "Sentry transport using Node HTTP module",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/transport-http",
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+
"https-proxy-agent": "5.0.0"
21+
},
22+
"scripts": {
23+
"build": "run-p build:es5 build:esm",
24+
"build:es5": "tsc -p tsconfig.build.json",
25+
"build:esm": "tsc -p tsconfig.esm.json",
26+
"build:watch": "run-p build:watch:es5 build:watch:esm",
27+
"build:watch:es5": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
28+
"build:watch:esm": "tsc -p tsconfig.esm.json -w --preserveWatchOutput",
29+
"clean": "rimraf dist coverage",
30+
"link:yarn": "yarn link",
31+
"lint": "run-s lint:prettier lint:eslint",
32+
"lint:prettier": "prettier --check \"{src,test}/**/*.ts\"",
33+
"lint:eslint": "eslint .",
34+
"fix": "run-s fix:eslint fix:prettier",
35+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
36+
"fix:eslint": "eslint . --fix",
37+
"test": "jest -c ../../jest.config.js",
38+
"test:watch": "npm run test -- --watch",
39+
"pack": "npm pack"
40+
},
41+
"volta": {
42+
"extends": "../../package.json"
43+
},
44+
"sideEffects": false
45+
}

packages/transport-http/src/index.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import * as http from 'http';
2+
import * as https from 'https';
3+
// import { readFileSync } from 'fs';
4+
import { URL } from 'url';
5+
6+
import {
7+
BaseTransport,
8+
Transport,
9+
TransportOptions,
10+
TransportRequest,
11+
TransportRequestMaker,
12+
TransportResponse,
13+
} from '@sentry/transport-base';
14+
15+
// TODO: Unify all transports options
16+
type HTTPTransportOptions = TransportOptions & {
17+
requestOptions?: https.RequestOptions;
18+
proxy?: string;
19+
caCerts?: string;
20+
};
21+
22+
// TODO: `x-sentry-error` header?
23+
24+
export class HTTPTransport extends BaseTransport implements Transport {
25+
constructor(private readonly _options: HTTPTransportOptions) {
26+
super(_options);
27+
}
28+
29+
public sendRequest<T>(request: TransportRequest<T>): PromiseLike<TransportResponse> {
30+
const requestMaker: TransportRequestMaker<T> = request => {
31+
return new Promise((resolve, reject) => {
32+
const { hostname, pathname, port, protocol } = new URL(this._dsn.getEnvelopeEndpoint());
33+
const httpModule = protocol === 'https:' ? https : http;
34+
const proxy = this._options.proxy || process.env.http_proxy;
35+
const agent = proxy
36+
? new (require('https-proxy-agent'))(proxy)
37+
: new httpModule.Agent({ keepAlive: false, maxSockets: 30, timeout: 2000 });
38+
39+
const requestOptions: https.RequestOptions = {
40+
agent,
41+
hostname,
42+
method: 'POST',
43+
path: pathname,
44+
port,
45+
protocol,
46+
headers: {
47+
...this._options.headers,
48+
...this._dsn.getEnvelopeEndpointAuthHeaders(),
49+
},
50+
// ...this._options.requestOptions,
51+
// TODO: Handle and cache CA certs?
52+
// ...(this._options.caCerts && {
53+
// ca: readFileSync(this._options.caCerts),
54+
// }),
55+
};
56+
57+
const req = httpModule.request(requestOptions, (res: http.IncomingMessage) => {
58+
res.setEncoding('utf8');
59+
60+
let body = '';
61+
res.on('data', chunk => {
62+
body += chunk;
63+
});
64+
res.on('end', () => {
65+
resolve({
66+
body,
67+
headers: {
68+
'x-sentry-rate-limits': res.headers['X-Sentry-Rate-Limits'] as string,
69+
'retry-after': res.headers['Retry-After'] as string,
70+
},
71+
reason: res.statusMessage,
72+
statusCode: res.statusCode || 500,
73+
});
74+
});
75+
});
76+
77+
req.on('error', error => reject(error));
78+
req.end(request.body);
79+
});
80+
};
81+
82+
return super.sendRequest(request, requestMaker);
83+
}
84+
}

0 commit comments

Comments
 (0)