Skip to content
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
25 changes: 13 additions & 12 deletions packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { getHubFromCarrier, Scope } from '@sentry/hub';
import { SentryEvent, Severity } from '@sentry/types';
import { forget } from '@sentry/utils/async';
import { serialize } from '@sentry/utils/object';
import { parse as parseCookie } from 'cookie';
import * as cookie from 'cookie';
import * as domain from 'domain';
import { hostname } from 'os';
import { parse as parseUrl } from 'url';
import * as http from 'http';
import * as os from 'os';
import * as url from 'url';
import { NodeClient } from './client';
import { getCurrentHub } from './hub';

Expand Down Expand Up @@ -45,10 +46,10 @@ function extractRequestData(req: { [key: string]: any }): { [key: string]: strin
// query string:
// node: req.url (raw)
// express, koa: req.query
const query = req.query || parseUrl(originalUrl || '', true).query;
const query = req.query || url.parse(originalUrl || '', true).query;
// cookies:
// node, express, koa: req.headers.cookie
const cookies = parseCookie(headers.cookie || '');
const cookies = cookie.parse(headers.cookie || '');
// body data:
// node, express, koa: req.body
let data = req.body;
Expand Down Expand Up @@ -121,7 +122,7 @@ function parseRequest(
...event.request,
...extractRequestData(req),
},
server_name: global.process.env.SENTRY_NAME || hostname(),
server_name: global.process.env.SENTRY_NAME || os.hostname(),
};

if (req.user) {
Expand All @@ -135,8 +136,8 @@ function parseRequest(
}

/** JSDoc */
export function requestHandler(): (req: Request, res: Response, next: () => void) => void {
return function sentryRequestMiddleware(req: Request, _res: Response, next: () => void): void {
export function requestHandler(): (req: http.ClientRequest, res: http.ServerResponse, next: () => void) => void {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a note, I don't exactly know what kind of style for imports you are following but you can import just import ClientRequest and ServerResponse to reduce verbosity or are you doing this to avoid type collissions in your builds?

return function sentryRequestMiddleware(req: http.ClientRequest, _res: http.ServerResponse, next: () => void): void {
const local = domain.create();
const hub = getHubFromCarrier(req);
hub.bindClient(getCurrentHub().getClient());
Expand Down Expand Up @@ -168,14 +169,14 @@ function getStatusCodeFromResponse(error: MiddlewareError): number {
/** JSDoc */
export function errorHandler(): (
error: MiddlewareError,
req: Request,
res: Response,
req: http.ClientRequest,
res: http.ServerResponse,
next: (error: MiddlewareError) => void,
) => void {
return function sentryErrorMiddleware(
error: MiddlewareError,
req: Request,
_res: Response,
req: http.ClientRequest,
_res: http.ServerResponse,
next: (error: MiddlewareError) => void,
): void {
const status = getStatusCodeFromResponse(error);
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/console.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Integration, Severity } from '@sentry/types';
import { fill } from '@sentry/utils/object';
import { format } from 'util';
import * as util from 'util';
import { getCurrentHub } from '../hub';

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ function consoleWrapper(originalModule: any): any {
{
category: 'console',
level: sentryLevel,
message: format.apply(undefined, arguments),
message: util.format.apply(undefined, arguments),
},
{
input: [...arguments],
Expand Down
24 changes: 12 additions & 12 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Integration } from '@sentry/types';
import { fill } from '@sentry/utils/object';
import { ClientRequest, ClientRequestArgs, ServerResponse } from 'http';
import { inherits } from 'util';
import * as http from 'http';
import * as util from 'util';
import { getCurrentHub } from '../hub';

let lastResponse: ServerResponse | undefined;
let lastResponse: http.ServerResponse | undefined;

/**
* Request interface which can carry around unified url
* independently of used framework
*/
interface SentryRequest extends Request {
interface SentryRequest extends http.IncomingMessage {
__ravenBreadcrumbUrl?: string;
}

Expand All @@ -20,7 +20,7 @@ interface SentryRequest extends Request {
* @param options url that should be returned or an object containing it's parts.
* @returns constructed url
*/
function createBreadcrumbUrl(options: string | ClientRequestArgs): string {
function createBreadcrumbUrl(options: string | http.ClientRequestArgs): string {
// We could just always reconstruct this from this.agent, this._headers, this.path, etc
// but certain other http-instrumenting libraries (like nock, which we use for tests) fail to
// maintain the guarantee that after calling origClientRequest, those fields will be populated
Expand Down Expand Up @@ -54,7 +54,7 @@ function loadWrapper(nativeModule: any): any {
const origClientRequest = originalModule.ClientRequest;
const clientRequest = function(
this: SentryRequest,
options: ClientRequestArgs | string,
options: http.ClientRequestArgs | string,
callback: () => void,
): any {
// Note: this won't capture a breadcrumb if a response never comes
Expand All @@ -68,7 +68,7 @@ function loadWrapper(nativeModule: any): any {
this.__ravenBreadcrumbUrl = createBreadcrumbUrl(options);
};

inherits(clientRequest, origClientRequest);
util.inherits(clientRequest, origClientRequest);

fill(clientRequest.prototype, 'emit', emitWrapper);

Expand All @@ -80,13 +80,13 @@ function loadWrapper(nativeModule: any): any {
// it still points at orig ClientRequest after our monkeypatch; these reimpls
// just get that reference updated to use our new ClientRequest
fill(originalModule, 'request', function(): any {
return function(options: ClientRequestArgs, callback: () => void): any {
return new originalModule.ClientRequest(options, callback) as ClientRequest;
return function(options: http.ClientRequestArgs, callback: () => void): any {
return new originalModule.ClientRequest(options, callback) as http.ClientRequest;
};
});

fill(originalModule, 'get', function(): any {
return function(options: ClientRequestArgs, callback: () => void): any {
return function(options: http.ClientRequestArgs, callback: () => void): any {
const req = originalModule.request(options, callback);
req.end();
return req;
Expand All @@ -101,8 +101,8 @@ function loadWrapper(nativeModule: any): any {
/**
* Wrapper function for request's `emit` calls
*/
function emitWrapper(origEmit: EventListener): (event: string, response: ServerResponse) => EventListener {
return function(this: SentryRequest, event: string, response: ServerResponse): any {
function emitWrapper(origEmit: EventListener): (event: string, response: http.ServerResponse) => EventListener {
return function(this: SentryRequest, event: string, response: http.ServerResponse): any {
// I'm not sure why but Node.js (at least in v8.X)
// is emitting all events twice :|
if (lastResponse === undefined || lastResponse !== response) {
Expand Down
8 changes: 4 additions & 4 deletions packages/node/src/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SentryEvent, SentryException, StackFrame } from '@sentry/types';
import { readFileAsync } from '@sentry/utils/fs';
import { snipLine } from '@sentry/utils/string';
import { basename, dirname } from 'path';
import * as path from 'path';
import * as stacktrace from 'stack-trace';

const LINES_OF_CONTEXT: number = 7;
Expand Down Expand Up @@ -30,7 +30,7 @@ function getTransaction(frame: StackFrame): string {
return frame.module || frame.function ? `${frame.module || '?'} at ${frame.function || '?'}` : '<unknown>';
}

const mainModule: string = `${(require.main && require.main.filename && dirname(require.main.filename)) ||
const mainModule: string = `${(require.main && require.main.filename && path.dirname(require.main.filename)) ||
global.process.cwd()}/`;

/** JSDoc */
Expand All @@ -40,8 +40,8 @@ function getModule(filename: string, base?: string): string {
}

// It's specifically a module
const file = basename(filename, '.js');
filename = dirname(filename); // tslint:disable-line:no-parameter-reassignment
const file = path.basename(filename, '.js');
filename = path.dirname(filename); // tslint:disable-line:no-parameter-reassignment
let n = filename.lastIndexOf('/node_modules/');
if (n > -1) {
// /node_modules/ is 14 chars
Expand Down
5 changes: 3 additions & 2 deletions packages/node/src/transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { SentryEvent, SentryResponse, Status, Transport, TransportOptions } from
import { serialize } from '@sentry/utils/object';
import * as http from 'http';
import * as https from 'https';
import * as url from 'url';
import { SDK_NAME, SDK_VERSION } from '../version';

/** Internal used interface for typescript */
export interface HTTPRequest {
request(
options: http.RequestOptions | string | URL,
options: http.RequestOptions | https.RequestOptions | string | url.URL,
callback?: (res: http.IncomingMessage) => void,
): http.ClientRequest;
}
Expand All @@ -27,7 +28,7 @@ export abstract class BaseTransport implements Transport {
}

/** Returns a build request option object used by request */
protected getRequestOptions(): http.RequestOptions {
protected getRequestOptions(): http.RequestOptions | https.RequestOptions {
const headers = {
...this.api.getRequestHeaders(SDK_NAME, SDK_VERSION),
...this.options.headers,
Expand Down