diff --git a/packages/node/src/transports/base/http-module.ts b/packages/node/src/transports/base/http-module.ts new file mode 100644 index 000000000000..e6921a4cb82e --- /dev/null +++ b/packages/node/src/transports/base/http-module.ts @@ -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; +} diff --git a/packages/node/src/transports/base.ts b/packages/node/src/transports/base/index.ts similarity index 90% rename from packages/node/src/transports/base.ts rename to packages/node/src/transports/base/index.ts index 3413cbb8ef9f..14eb6aeb5f22 100644 --- a/packages/node/src/transports/base.ts +++ b/packages/node/src/transports/base/index.ts @@ -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; export type UrlParser = (url: string) => URLParts; @@ -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);