Skip to content

feat: node transports #1414

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 7 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"lerna": "2.9.0",
"lerna": "2.11.0",
"version": "0.5.4",
"packages": [
"packages/*"
],
"packages": "packages/*",
"ignore": "raven-*",
"npmClient": "yarn",
"useWorkspaces": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"codecov": "^3.0.2",
"jest": "^22.4.3",
"karma-sinon": "^1.0.5",
"lerna": "^2.11.0",
"lerna": "3.0.0-beta.20",
"mocha": "^5.1.1",
"npm-run-all": "^4.1.2",
"prettier": "^1.12.1",
Expand Down
17 changes: 9 additions & 8 deletions packages/browser/src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Backend, DSN, Options, SentryError } from '@sentry/core';
import { addBreadcrumb, captureEvent } from '@sentry/minimal';
import { SentryEvent } from '@sentry/types';
import { SentryEvent, SentryResponse } from '@sentry/types';
import { supportsFetch } from '@sentry/utils/supports';
import { Raven } from './raven';
import { FetchTransport, XHRTransport } from './transports';
Expand Down Expand Up @@ -115,7 +115,7 @@ export class BrowserBackend implements Backend {
/**
* @inheritDoc
*/
public async sendEvent(event: SentryEvent): Promise<number> {
public async sendEvent(event: SentryEvent): Promise<SentryResponse> {
let dsn: DSN;

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

const transportOptions = this.options.transportOptions
? this.options.transportOptions
: { dsn };

const transport = this.options.transport
? new this.options.transport({ dsn })
: supportsFetch()
? new FetchTransport({ dsn })
: new XHRTransport({ dsn });
? new FetchTransport(transportOptions)
: new XHRTransport(transportOptions);

return transport
.send(event)
.then((response: Response | XMLHttpRequest) => response.status)
.catch((error: Response | XMLHttpRequest) => error.status);
return transport.send(event);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
/**
* @inheritDoc
*/
protected getSdkInfo(): SdkInfo {
public getSdkInfo(): SdkInfo {
return {
name: 'sentry-browser',
version: Raven.VERSION,
Expand All @@ -32,6 +32,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
/**
* Instruments the given function and sends an event to Sentry every time the
* function throws an exception.
* TODO remove this
*
* @param fn A function to wrap.
* @returns The wrapped function.
Expand Down
8 changes: 6 additions & 2 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ export {
SdkInfo,
SentryEvent,
SentryException,
SentryResponse,
Severity,
StackFrame,
Stacktrace,
Status,
Thread,
User,
} from '@sentry/types';
Expand All @@ -19,11 +21,13 @@ export {
configureScope,
} from '@sentry/minimal';

export { getDefaultHub, Hub, Scope } from '@sentry/hub';
export { getHubFromCarrier, getDefaultHub, Hub, Scope } from '@sentry/hub';

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

import * as Integrations from './integrations';
export { Integrations };
import * as Transports from './transports';

export { Integrations, Transports };
3 changes: 2 additions & 1 deletion packages/browser/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DSN, SentryError } from '@sentry/core';
import {
DSNComponents,
SentryEvent,
SentryResponse,
Transport,
TransportOptions,
} from '@sentry/types';
Expand Down Expand Up @@ -49,7 +50,7 @@ export abstract class BaseTransport implements Transport {
/**
* @inheritDoc
*/
public async send(_: SentryEvent): Promise<Response | XMLHttpRequest> {
public async send(_: SentryEvent): Promise<SentryResponse> {
throw new SentryError('Transport Class has to implement `send` method');
}
}
12 changes: 9 additions & 3 deletions packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SentryEvent } from '@sentry/types';
import { SentryEvent, SentryResponse, Status } from '@sentry/types';
import { getGlobalObject } from '@sentry/utils/misc';
import { serialize } from '@sentry/utils/object';
import { supportsReferrerPolicy } from '@sentry/utils/supports';
Expand All @@ -11,7 +11,7 @@ export class FetchTransport extends BaseTransport {
/**
* @inheritDoc
*/
public async send(event: SentryEvent): Promise<Response> {
public async send(event: SentryEvent): Promise<SentryResponse> {
const defaultOptions: RequestInit = {
body: serialize(event),
keepalive: true,
Expand All @@ -25,6 +25,12 @@ export class FetchTransport extends BaseTransport {
: '') as ReferrerPolicy,
};

return (global as Window).fetch(this.url, defaultOptions);
const response = await (global as Window).fetch(this.url, defaultOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'll break if fetch throws. We should put it in try/catch clause I guess

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or will async wrap it in reject call? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async handles everything 💪


return {
code: response.status,
event_id: event.event_id,
status: Status.fromHttpCode(response.status),
};
}
}
12 changes: 8 additions & 4 deletions packages/browser/src/transports/xhr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SentryEvent } from '@sentry/types';
import { SentryEvent, SentryResponse, Status } from '@sentry/types';
import { serialize } from '@sentry/utils/object';
import { BaseTransport } from './base';

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

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

if (request.status === 200) {
resolve(request);
resolve({
code: request.status,
event_id: event.event_id,
status: Status.fromHttpCode(request.status),
});
}

reject(request);
Expand Down
14 changes: 8 additions & 6 deletions packages/browser/test/backend.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { expect } from 'chai';
import { SentryEvent } from '../src';
import { SentryEvent, SentryResponse, Status } from '../src';
import { BrowserBackend } from '../src/backend';
import { BaseTransport } from '../src/transports';

class SimpleTransport extends BaseTransport {
public async send(event: SentryEvent): Promise<Response> {
return new Response(event.event_id, {
status: 200,
});
public async send(event: SentryEvent): Promise<SentryResponse> {
return {
code: 200,
event_id: event.event_id,
status: Status.fromHttpCode(200),
};
}
}

Expand Down Expand Up @@ -37,7 +39,7 @@ describe('BrowserBackend', () => {
it('should call send() on provided transport', async () => {
backend = new BrowserBackend({ dsn, transport: SimpleTransport });
const status = await backend.sendEvent(testEvent);
expect(status).equal(200);
expect(status.code).equal(200);
});
});
});
18 changes: 7 additions & 11 deletions packages/browser/test/transports/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Transport } from '@sentry/types';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { FetchTransport } from '../../src/transports/fetch';
import { Status, Transports } from '../../src';

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

let fetch: SinonStub;
let transport: Transport;
let transport: Transports.BaseTransport;

describe('FetchTransport', () => {
beforeEach(() => {
fetch = stub(window, 'fetch');
transport = new FetchTransport({ dsn: testDSN });
transport = new Transports.FetchTransport({ dsn: testDSN });
});

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

describe('send()', async () => {
it('sends a request to Sentry servers', async () => {
const response = new Response('', {
status: 200,
});
const response = { status: 200 };

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

return transport.send(payload).then(res => {
expect(res.status).equal(200);
expect(res.code).equal(200);
expect(res.status).equal(Status.Success);
expect(fetch.calledOnce).equal(true);
expect(
fetch.calledWith(transportUrl, {
Expand All @@ -54,9 +52,7 @@ describe('FetchTransport', () => {
});

it('rejects with non-200 status code', async () => {
const response = new Response('', {
status: 403,
});
const response = { status: 403 };

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

Expand Down
14 changes: 7 additions & 7 deletions packages/browser/test/transports/xhr.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Transport } from '@sentry/types';
import { expect } from 'chai';
import { fakeServer, SinonFakeServer } from 'sinon';
import { XHRTransport } from '../../src/transports/xhr';
import { Transports } from '../../src';

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

let server: SinonFakeServer;
let transport: Transport;
let transport: Transports.BaseTransport;

describe('XHRTransport', () => {
beforeEach(() => {
server = fakeServer.create();
server.respondImmediately = true;
transport = new XHRTransport({ dsn: testDSN });
transport = new Transports.XHRTransport({ dsn: testDSN });
});

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

return transport.send(payload).then(res => {
expect(res.status).equal(200);
expect(res.code).equal(200);

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

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

return transport.send(payload).catch(res => {
transport.send(payload).catch(res => {
expect(res.status).equal(403);

const request = server.requests[0];
expect(server.requests.length).equal(1);
expect(request.method).equal('POST');
expect(JSON.parse(request.requestBody)).deep.equal(payload);
done();
});
});
});
Expand Down
Loading