Skip to content

Commit b24e5d5

Browse files
committed
ref(node): Remove require calls
1 parent a1dab3b commit b24e5d5

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

packages/node/src/declarations.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
declare module 'https-proxy-agent';
21
declare module 'async-limiter';

packages/node/src/integrations/http.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
parseSemver,
99
stringMatchesSomePattern,
1010
} from '@sentry/utils';
11-
import type * as http from 'http';
12-
import type * as https from 'https';
11+
import * as http from 'http';
12+
import * as https from 'https';
1313
import { LRUMap } from 'lru_map';
1414

1515
import type { NodeClient } from '../client';
@@ -101,25 +101,17 @@ export class Http implements Integration {
101101
// and we will no longer have to do this optional merge, we can just pass `this._tracing` directly.
102102
const tracingOptions = this._tracing ? { ...clientOptions, ...this._tracing } : undefined;
103103

104-
// eslint-disable-next-line @typescript-eslint/no-var-requires
105-
const httpModule = require('http');
106-
const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, httpModule);
107-
fill(httpModule, 'get', wrappedHttpHandlerMaker);
108-
fill(httpModule, 'request', wrappedHttpHandlerMaker);
104+
const wrappedHttpHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, http);
105+
fill(http, 'get', wrappedHttpHandlerMaker);
106+
fill(http, 'request', wrappedHttpHandlerMaker);
109107

110108
// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
111109
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
112110
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
113111
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
114-
// eslint-disable-next-line @typescript-eslint/no-var-requires
115-
const httpsModule = require('https');
116-
const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(
117-
this._breadcrumbs,
118-
tracingOptions,
119-
httpsModule,
120-
);
121-
fill(httpsModule, 'get', wrappedHttpsHandlerMaker);
122-
fill(httpsModule, 'request', wrappedHttpsHandlerMaker);
112+
const wrappedHttpsHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, tracingOptions, https);
113+
fill(https, 'get', wrappedHttpsHandlerMaker);
114+
fill(https, 'request', wrappedHttpsHandlerMaker);
123115
}
124116
}
125117
}

packages/node/src/integrations/localvariables.ts

+22-9
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,35 @@ export interface DebugSession {
2424
* https://nodejs.org/docs/latest-v14.x/api/inspector.html
2525
*/
2626
class AsyncSession implements DebugSession {
27-
private readonly _session: Session;
27+
private _session: Session | undefined = undefined;
28+
29+
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
30+
private readonly _sessionModulePromise: Promise<typeof import('inspector')>;
2831

2932
/** Throws is inspector API is not available */
3033
public constructor() {
3134
// Node can be build without inspector support so this can throw
32-
// eslint-disable-next-line @typescript-eslint/no-var-requires
33-
const { Session } = require('inspector');
34-
this._session = new Session();
35+
// @ts-ignore eslint-disable-next-line @typescript-eslint/no-var-requires
36+
this._sessionModulePromise = import('inspector');
3537
}
3638

3739
/** @inheritdoc */
3840
public configureAndConnect(
3941
onPause: (message: InspectorNotification<Debugger.PausedEventDataType>) => void,
4042
captureAll: boolean,
4143
): void {
42-
this._session.connect();
43-
this._session.on('Debugger.paused', onPause);
44-
this._session.post('Debugger.enable');
45-
// We only want to pause on uncaught exceptions
46-
this._session.post('Debugger.setPauseOnExceptions', { state: captureAll ? 'all' : 'uncaught' });
44+
this._sessionModulePromise
45+
.then(sessionModuleConstructor => {
46+
this._session = new sessionModuleConstructor.Session();
47+
this._session.connect();
48+
this._session.on('Debugger.paused', onPause);
49+
this._session.post('Debugger.enable');
50+
// We only want to pause on uncaught exceptions
51+
this._session.post('Debugger.setPauseOnExceptions', { state: captureAll ? 'all' : 'uncaught' });
52+
})
53+
.catch(_ => {
54+
/* ignoring, `inspector` isn't always available */
55+
});
4756
}
4857

4958
/** @inheritdoc */
@@ -69,6 +78,10 @@ class AsyncSession implements DebugSession {
6978
*/
7079
private _getProperties(objectId: string): Promise<Runtime.PropertyDescriptor[]> {
7180
return new Promise((resolve, reject) => {
81+
if (!this._session) {
82+
reject(new Error('Session is not available'));
83+
return;
84+
}
7285
this._session.post(
7386
'Runtime.getProperties',
7487
{

packages/node/src/transports/http.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
} from '@sentry/types';
99
import * as http from 'http';
1010
import * as https from 'https';
11+
import * as createHttpsProxyAgent from 'https-proxy-agent';
1112
import { Readable } from 'stream';
1213
import { URL } from 'url';
1314
import { createGzip } from 'zlib';
@@ -74,8 +75,7 @@ export function makeNodeTransport(options: NodeTransportOptions): Transport {
7475
// TODO(v7): Evaluate if we can set keepAlive to true. This would involve testing for memory leaks in older node
7576
// versions(>= 8) as they had memory leaks when using it: #2555
7677
const agent = proxy
77-
? // eslint-disable-next-line @typescript-eslint/no-var-requires
78-
(new (require('https-proxy-agent'))(proxy) as http.Agent)
78+
? createHttpsProxyAgent(proxy)
7979
: new nativeHttpModule.Agent({ keepAlive, maxSockets: 30, timeout: 2000 });
8080

8181
const requestExecutor = createRequestExecutor(options, options.httpModule ?? nativeHttpModule, agent);

0 commit comments

Comments
 (0)