Skip to content

Commit f4cd60d

Browse files
authored
feat: node transports (#1414)
1 parent 3a0ec5d commit f4cd60d

File tree

35 files changed

+2477
-1739
lines changed

35 files changed

+2477
-1739
lines changed

lerna.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
2-
"lerna": "2.9.0",
2+
"lerna": "2.11.0",
33
"version": "0.5.4",
4-
"packages": [
5-
"packages/*"
6-
],
4+
"packages": "packages/*",
75
"ignore": "raven-*",
86
"npmClient": "yarn",
97
"useWorkspaces": true

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"codecov": "^3.0.2",
3030
"jest": "^22.4.3",
3131
"karma-sinon": "^1.0.5",
32-
"lerna": "^2.11.0",
32+
"lerna": "3.0.0-beta.20",
3333
"mocha": "^5.1.1",
3434
"npm-run-all": "^4.1.2",
3535
"prettier": "^1.12.1",

packages/browser/src/backend.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Backend, DSN, Options, SentryError } from '@sentry/core';
22
import { addBreadcrumb, captureEvent } from '@sentry/minimal';
3-
import { SentryEvent } from '@sentry/types';
3+
import { SentryEvent, SentryResponse } from '@sentry/types';
44
import { supportsFetch } from '@sentry/utils/supports';
55
import { Raven } from './raven';
66
import { FetchTransport, XHRTransport } from './transports';
@@ -115,7 +115,7 @@ export class BrowserBackend implements Backend {
115115
/**
116116
* @inheritDoc
117117
*/
118-
public async sendEvent(event: SentryEvent): Promise<number> {
118+
public async sendEvent(event: SentryEvent): Promise<SentryResponse> {
119119
let dsn: DSN;
120120

121121
if (!this.options.dsn) {
@@ -124,16 +124,17 @@ export class BrowserBackend implements Backend {
124124
dsn = new DSN(this.options.dsn);
125125
}
126126

127+
const transportOptions = this.options.transportOptions
128+
? this.options.transportOptions
129+
: { dsn };
130+
127131
const transport = this.options.transport
128132
? new this.options.transport({ dsn })
129133
: supportsFetch()
130-
? new FetchTransport({ dsn })
131-
: new XHRTransport({ dsn });
134+
? new FetchTransport(transportOptions)
135+
: new XHRTransport(transportOptions);
132136

133-
return transport
134-
.send(event)
135-
.then((response: Response | XMLHttpRequest) => response.status)
136-
.catch((error: Response | XMLHttpRequest) => error.status);
137+
return transport.send(event);
137138
}
138139

139140
/**

packages/browser/src/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
2222
/**
2323
* @inheritDoc
2424
*/
25-
protected getSdkInfo(): SdkInfo {
25+
public getSdkInfo(): SdkInfo {
2626
return {
2727
name: 'sentry-browser',
2828
version: Raven.VERSION,
@@ -32,6 +32,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
3232
/**
3333
* Instruments the given function and sends an event to Sentry every time the
3434
* function throws an exception.
35+
* TODO remove this
3536
*
3637
* @param fn A function to wrap.
3738
* @returns The wrapped function.

packages/browser/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ export {
44
SdkInfo,
55
SentryEvent,
66
SentryException,
7+
SentryResponse,
78
Severity,
89
StackFrame,
910
Stacktrace,
11+
Status,
1012
Thread,
1113
User,
1214
} from '@sentry/types';
@@ -19,11 +21,13 @@ export {
1921
configureScope,
2022
} from '@sentry/minimal';
2123

22-
export { getDefaultHub, Hub, Scope } from '@sentry/hub';
24+
export { getHubFromCarrier, getDefaultHub, Hub, Scope } from '@sentry/hub';
2325

2426
export { BrowserBackend, BrowserOptions } from './backend';
2527
export { BrowserClient } from './client';
2628
export { init } from './sdk';
2729

2830
import * as Integrations from './integrations';
29-
export { Integrations };
31+
import * as Transports from './transports';
32+
33+
export { Integrations, Transports };

packages/browser/src/transports/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DSN, SentryError } from '@sentry/core';
22
import {
33
DSNComponents,
44
SentryEvent,
5+
SentryResponse,
56
Transport,
67
TransportOptions,
78
} from '@sentry/types';
@@ -49,7 +50,7 @@ export abstract class BaseTransport implements Transport {
4950
/**
5051
* @inheritDoc
5152
*/
52-
public async send(_: SentryEvent): Promise<Response | XMLHttpRequest> {
53+
public async send(_: SentryEvent): Promise<SentryResponse> {
5354
throw new SentryError('Transport Class has to implement `send` method');
5455
}
5556
}

packages/browser/src/transports/fetch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SentryEvent } from '@sentry/types';
1+
import { SentryEvent, SentryResponse, Status } from '@sentry/types';
22
import { getGlobalObject } from '@sentry/utils/misc';
33
import { serialize } from '@sentry/utils/object';
44
import { supportsReferrerPolicy } from '@sentry/utils/supports';
@@ -11,7 +11,7 @@ export class FetchTransport extends BaseTransport {
1111
/**
1212
* @inheritDoc
1313
*/
14-
public async send(event: SentryEvent): Promise<Response> {
14+
public async send(event: SentryEvent): Promise<SentryResponse> {
1515
const defaultOptions: RequestInit = {
1616
body: serialize(event),
1717
keepalive: true,
@@ -25,6 +25,12 @@ export class FetchTransport extends BaseTransport {
2525
: '') as ReferrerPolicy,
2626
};
2727

28-
return (global as Window).fetch(this.url, defaultOptions);
28+
const response = await (global as Window).fetch(this.url, defaultOptions);
29+
30+
return {
31+
code: response.status,
32+
event_id: event.event_id,
33+
status: Status.fromHttpCode(response.status),
34+
};
2935
}
3036
}

packages/browser/src/transports/xhr.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SentryEvent } from '@sentry/types';
1+
import { SentryEvent, SentryResponse, Status } from '@sentry/types';
22
import { serialize } from '@sentry/utils/object';
33
import { BaseTransport } from './base';
44

@@ -7,8 +7,8 @@ export class XHRTransport extends BaseTransport {
77
/**
88
* @inheritDoc
99
*/
10-
public async send(event: SentryEvent): Promise<XMLHttpRequest> {
11-
return new Promise<XMLHttpRequest>((resolve, reject) => {
10+
public async send(event: SentryEvent): Promise<SentryResponse> {
11+
return new Promise<SentryResponse>((resolve, reject) => {
1212
const request = new XMLHttpRequest();
1313

1414
request.onreadystatechange = () => {
@@ -17,7 +17,11 @@ export class XHRTransport extends BaseTransport {
1717
}
1818

1919
if (request.status === 200) {
20-
resolve(request);
20+
resolve({
21+
code: request.status,
22+
event_id: event.event_id,
23+
status: Status.fromHttpCode(request.status),
24+
});
2125
}
2226

2327
reject(request);

packages/browser/test/backend.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { expect } from 'chai';
2-
import { SentryEvent } from '../src';
2+
import { SentryEvent, SentryResponse, Status } from '../src';
33
import { BrowserBackend } from '../src/backend';
44
import { BaseTransport } from '../src/transports';
55

66
class SimpleTransport extends BaseTransport {
7-
public async send(event: SentryEvent): Promise<Response> {
8-
return new Response(event.event_id, {
9-
status: 200,
10-
});
7+
public async send(event: SentryEvent): Promise<SentryResponse> {
8+
return {
9+
code: 200,
10+
event_id: event.event_id,
11+
status: Status.fromHttpCode(200),
12+
};
1113
}
1214
}
1315

@@ -37,7 +39,7 @@ describe('BrowserBackend', () => {
3739
it('should call send() on provided transport', async () => {
3840
backend = new BrowserBackend({ dsn, transport: SimpleTransport });
3941
const status = await backend.sendEvent(testEvent);
40-
expect(status).equal(200);
42+
expect(status.code).equal(200);
4143
});
4244
});
4345
});

packages/browser/test/transports/fetch.test.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Transport } from '@sentry/types';
21
import { expect } from 'chai';
32
import { SinonStub, stub } from 'sinon';
4-
import { FetchTransport } from '../../src/transports/fetch';
3+
import { Status, Transports } from '../../src';
54

65
const testDSN = 'https://[email protected]/42';
76
const transportUrl =
@@ -15,12 +14,12 @@ const payload = {
1514
};
1615

1716
let fetch: SinonStub;
18-
let transport: Transport;
17+
let transport: Transports.BaseTransport;
1918

2019
describe('FetchTransport', () => {
2120
beforeEach(() => {
2221
fetch = stub(window, 'fetch');
23-
transport = new FetchTransport({ dsn: testDSN });
22+
transport = new Transports.FetchTransport({ dsn: testDSN });
2423
});
2524

2625
afterEach(() => {
@@ -33,14 +32,13 @@ describe('FetchTransport', () => {
3332

3433
describe('send()', async () => {
3534
it('sends a request to Sentry servers', async () => {
36-
const response = new Response('', {
37-
status: 200,
38-
});
35+
const response = { status: 200 };
3936

4037
fetch.returns(Promise.resolve(response));
4138

4239
return transport.send(payload).then(res => {
43-
expect(res.status).equal(200);
40+
expect(res.code).equal(200);
41+
expect(res.status).equal(Status.Success);
4442
expect(fetch.calledOnce).equal(true);
4543
expect(
4644
fetch.calledWith(transportUrl, {
@@ -54,9 +52,7 @@ describe('FetchTransport', () => {
5452
});
5553

5654
it('rejects with non-200 status code', async () => {
57-
const response = new Response('', {
58-
status: 403,
59-
});
55+
const response = { status: 403 };
6056

6157
fetch.returns(Promise.reject(response));
6258

packages/browser/test/transports/xhr.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Transport } from '@sentry/types';
21
import { expect } from 'chai';
32
import { fakeServer, SinonFakeServer } from 'sinon';
4-
import { XHRTransport } from '../../src/transports/xhr';
3+
import { Transports } from '../../src';
54

65
const testDSN = 'https://[email protected]/42';
76
const transportUrl =
@@ -15,13 +14,13 @@ const payload = {
1514
};
1615

1716
let server: SinonFakeServer;
18-
let transport: Transport;
17+
let transport: Transports.BaseTransport;
1918

2019
describe('XHRTransport', () => {
2120
beforeEach(() => {
2221
server = fakeServer.create();
2322
server.respondImmediately = true;
24-
transport = new XHRTransport({ dsn: testDSN });
23+
transport = new Transports.XHRTransport({ dsn: testDSN });
2524
});
2625

2726
afterEach(() => {
@@ -37,7 +36,7 @@ describe('XHRTransport', () => {
3736
server.respondWith('POST', transportUrl, [200, {}, '']);
3837

3938
return transport.send(payload).then(res => {
40-
expect(res.status).equal(200);
39+
expect(res.code).equal(200);
4140

4241
const request = server.requests[0];
4342
expect(server.requests.length).equal(1);
@@ -46,16 +45,17 @@ describe('XHRTransport', () => {
4645
});
4746
});
4847

49-
it('rejects with non-200 status code', async () => {
48+
it('rejects with non-200 status code', done => {
5049
server.respondWith('POST', transportUrl, [403, {}, '']);
5150

52-
return transport.send(payload).catch(res => {
51+
transport.send(payload).catch(res => {
5352
expect(res.status).equal(403);
5453

5554
const request = server.requests[0];
5655
expect(server.requests.length).equal(1);
5756
expect(request.method).equal('POST');
5857
expect(JSON.parse(request.requestBody)).deep.equal(payload);
58+
done();
5959
});
6060
});
6161
});

0 commit comments

Comments
 (0)