Skip to content

Commit 4f401ab

Browse files
1999kamilogorek
andauthored
Make HTTPModule more abstract to be able to use it in non-Node.JS environments (getsentry#3655)
Co-authored-by: Kamil Ogórek <[email protected]>
1 parent 888c14a commit 4f401ab

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import http, { IncomingHttpHeaders } from 'http';
2+
import https from 'https';
3+
import { URL } from 'url';
4+
5+
export type HTTPModuleRequestOptions = http.RequestOptions | https.RequestOptions | string | URL;
6+
7+
/**
8+
* Cut version of http.IncomingMessage.
9+
* Some transports work in a special Javascript environment where http.IncomingMessage is not available.
10+
*/
11+
export interface HTTPModuleRequestIncomingMessage {
12+
headers: IncomingHttpHeaders;
13+
statusCode?: number;
14+
on(event: 'data' | 'end', listener: () => void): void;
15+
setEncoding(encoding: string): void;
16+
}
17+
18+
/**
19+
* Cut version of http.ClientRequest.
20+
* Some transports work in a special Javascript environment where http.IncomingMessage is not available.
21+
*/
22+
export interface HTTPModuleClientRequest {
23+
end(chunk: string): void;
24+
on(event: 'error', listener: () => void): void;
25+
}
26+
27+
/**
28+
* Internal used interface for typescript.
29+
* @hidden
30+
*/
31+
export interface HTTPModule {
32+
/**
33+
* Request wrapper
34+
* @param options These are {@see TransportOptions}
35+
* @param callback Callback when request is finished
36+
*/
37+
request(
38+
options: HTTPModuleRequestOptions,
39+
callback?: (res: HTTPModuleRequestIncomingMessage) => void,
40+
): HTTPModuleClientRequest;
41+
42+
// This is the type for nodejs versions that handle the URL argument
43+
// (v10.9.0+), but we do not use it just yet because we support older node
44+
// versions:
45+
46+
// request(
47+
// url: string | URL,
48+
// options: http.RequestOptions | https.RequestOptions,
49+
// callback?: (res: http.IncomingMessage) => void,
50+
// ): http.ClientRequest;
51+
}

packages/node/src/transports/base.ts renamed to packages/node/src/transports/base/index.ts

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,8 @@ import * as http from 'http';
1717
import * as https from 'https';
1818
import { URL } from 'url';
1919

20-
import { SDK_NAME } from '../version';
21-
22-
/**
23-
* Internal used interface for typescript.
24-
* @hidden
25-
*/
26-
export interface HTTPModule {
27-
/**
28-
* Request wrapper
29-
* @param options These are {@see TransportOptions}
30-
* @param callback Callback when request is finished
31-
*/
32-
request(
33-
options: http.RequestOptions | https.RequestOptions | string | URL,
34-
callback?: (res: http.IncomingMessage) => void,
35-
): http.ClientRequest;
36-
37-
// This is the type for nodejs versions that handle the URL argument
38-
// (v10.9.0+), but we do not use it just yet because we support older node
39-
// versions:
40-
41-
// request(
42-
// url: string | URL,
43-
// options: http.RequestOptions | https.RequestOptions,
44-
// callback?: (res: http.IncomingMessage) => void,
45-
// ): http.ClientRequest;
46-
}
20+
import { SDK_NAME } from '../../version';
21+
import { HTTPModule } from './http-module';
4722

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

0 commit comments

Comments
 (0)