Skip to content

Commit

Permalink
Make HTTPModule more abstract to be able to use it in non-Node.JS env…
Browse files Browse the repository at this point in the history
…ironments (#3655)

Co-authored-by: Kamil Ogórek <[email protected]>
  • Loading branch information
1999 and kamilogorek authored Jun 10, 2021
1 parent 888c14a commit 4f401ab
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
51 changes: 51 additions & 0 deletions packages/node/src/transports/base/http-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import http, { IncomingHttpHeaders } from 'http';
import https from 'https';
import { URL } from 'url';

export type HTTPModuleRequestOptions = http.RequestOptions | https.RequestOptions | string | URL;

/**
* Cut version of http.IncomingMessage.
* Some transports work in a special Javascript environment where http.IncomingMessage is not available.
*/
export interface HTTPModuleRequestIncomingMessage {
headers: IncomingHttpHeaders;
statusCode?: number;
on(event: 'data' | 'end', listener: () => void): void;
setEncoding(encoding: string): void;
}

/**
* Cut version of http.ClientRequest.
* Some transports work in a special Javascript environment where http.IncomingMessage is not available.
*/
export interface HTTPModuleClientRequest {
end(chunk: string): void;
on(event: 'error', listener: () => void): void;
}

/**
* Internal used interface for typescript.
* @hidden
*/
export interface HTTPModule {
/**
* Request wrapper
* @param options These are {@see TransportOptions}
* @param callback Callback when request is finished
*/
request(
options: HTTPModuleRequestOptions,
callback?: (res: HTTPModuleRequestIncomingMessage) => void,
): HTTPModuleClientRequest;

// This is the type for nodejs versions that handle the URL argument
// (v10.9.0+), but we do not use it just yet because we support older node
// versions:

// request(
// url: string | URL,
// options: http.RequestOptions | https.RequestOptions,
// callback?: (res: http.IncomingMessage) => void,
// ): http.ClientRequest;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,8 @@ import * as http from 'http';
import * as https from 'https';
import { URL } from 'url';

import { SDK_NAME } from '../version';

/**
* Internal used interface for typescript.
* @hidden
*/
export interface HTTPModule {
/**
* Request wrapper
* @param options These are {@see TransportOptions}
* @param callback Callback when request is finished
*/
request(
options: http.RequestOptions | https.RequestOptions | string | URL,
callback?: (res: http.IncomingMessage) => void,
): http.ClientRequest;

// This is the type for nodejs versions that handle the URL argument
// (v10.9.0+), but we do not use it just yet because we support older node
// versions:

// request(
// url: string | URL,
// options: http.RequestOptions | https.RequestOptions,
// callback?: (res: http.IncomingMessage) => void,
// ): http.ClientRequest;
}
import { SDK_NAME } from '../../version';
import { HTTPModule } from './http-module';

export type URLParts = Pick<URL, 'hostname' | 'pathname' | 'port' | 'protocol'>;
export type UrlParser = (url: string) => URLParts;
Expand Down Expand Up @@ -231,7 +206,7 @@ export abstract class BaseTransport implements Transport {
throw new SentryError('No module available');
}
const options = this._getRequestOptions(this.urlParser(sentryReq.url));
const req = this.module.request(options, (res: http.IncomingMessage) => {
const req = this.module.request(options, res => {
const statusCode = res.statusCode || 500;
const status = Status.fromHttpCode(statusCode);

Expand Down

0 comments on commit 4f401ab

Please sign in to comment.