Skip to content

chore(node): Extract node version constant #7734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2023
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
11 changes: 2 additions & 9 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import type { Hub } from '@sentry/core';
import { getCurrentHub } from '@sentry/core';
import type { EventProcessor, Integration, Span, TracePropagationTargets } from '@sentry/types';
import {
dynamicSamplingContextToSentryBaggageHeader,
fill,
logger,
parseSemver,
stringMatchesSomePattern,
} from '@sentry/utils';
import { dynamicSamplingContextToSentryBaggageHeader, fill, logger, stringMatchesSomePattern } from '@sentry/utils';
import type * as http from 'http';
import type * as https from 'https';
import { LRUMap } from 'lru_map';

import type { NodeClient } from '../client';
import { NODE_VERSION } from '../nodeVersion';
import type { RequestMethod, RequestMethodArgs } from './utils/http';
import { cleanSpanDescription, extractRawUrl, extractUrl, isSentryRequest, normalizeRequestArgs } from './utils/http';

const NODE_VERSION = parseSemver(process.versions.node);

interface TracingOptions {
/**
* List of strings/regex controlling to which outgoing requests
Expand Down
4 changes: 1 addition & 3 deletions packages/node/src/integrations/undici/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import type { EventProcessor, Integration } from '@sentry/types';
import {
dynamicRequire,
dynamicSamplingContextToSentryBaggageHeader,
parseSemver,
stringMatchesSomePattern,
stripUrlQueryAndFragment,
} from '@sentry/utils';

import type { NodeClient } from '../../client';
import { NODE_VERSION } from '../../nodeVersion';
import { isSentryRequest } from '../utils/http';
import type { DiagnosticsChannel, RequestCreateMessage, RequestEndMessage, RequestErrorMessage } from './types';

const NODE_VERSION = parseSemver(process.versions.node);

export enum ChannelName {
// https://github.com/nodejs/undici/blob/e6fc80f809d1217814c044f52ed40ef13f21e43c/docs/api/DiagnosticsChannel.md#undicirequestcreate
RequestCreate = 'undici:request:create',
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/integrations/utils/http.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { getCurrentHub } from '@sentry/core';
import { parseSemver } from '@sentry/utils';
import type * as http from 'http';
import type * as https from 'https';
import { URL } from 'url';

const NODE_VERSION = parseSemver(process.versions.node);
import { NODE_VERSION } from '../../nodeVersion';

/**
* Checks whether given url points to Sentry server
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/nodeVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { parseSemver } from '@sentry/utils';

export const NODE_VERSION: ReturnType<typeof parseSemver> = parseSemver(process.versions.node);
Copy link
Member

Choose a reason for hiding this comment

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

l: Should we not put our own type here (not sure what parseSemver actually returns, a string??)? Seems a bit redundant and if the return type changes we won't actually find out and it will just be different from before?

Copy link
Member Author

Choose a reason for hiding this comment

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

So I had to include this because otherwise I was getting a build error:

created build/esm in 355ms
src/nodeVersion.ts:3:14 - error TS4023: Exported variable 'NODE_VERSION' has or is using name 'SemVer' from external module "/Users/abhijeetprasad/workspace/sentry-javascript/packages/utils/build/types/misc" but cannot be named.

3 export const NODE_VERSION = parseSemver(process.versions.node);

It seems because SemVer is not exported, the type breaks?

5 changes: 2 additions & 3 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Span, Transaction } from '@sentry/core';
import * as sentryCore from '@sentry/core';
import { addTracingExtensions, Hub } from '@sentry/core';
import type { TransactionContext } from '@sentry/types';
import { logger, parseSemver, TRACEPARENT_REGEXP } from '@sentry/utils';
import { logger, TRACEPARENT_REGEXP } from '@sentry/utils';
import * as http from 'http';
import * as https from 'https';
import * as HttpsProxyAgent from 'https-proxy-agent';
Expand All @@ -11,11 +11,10 @@ import * as nock from 'nock';
import type { Breadcrumb } from '../../src';
import { NodeClient } from '../../src/client';
import { Http as HttpIntegration } from '../../src/integrations/http';
import { NODE_VERSION } from '../../src/nodeVersion';
import type { NodeClientOptions } from '../../src/types';
import { getDefaultNodeClientOptions } from '../helper/node-client-options';

const NODE_VERSION = parseSemver(process.versions.node);

const originalHttpGet = http.get;
const originalHttpRequest = http.request;

Expand Down
8 changes: 4 additions & 4 deletions packages/node/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseSemver } from '@sentry/utils';
import { NODE_VERSION } from '../src/nodeVersion';

/**
* Returns`describe` or `describe.skip` depending on allowed major versions of Node.
Expand All @@ -7,12 +7,12 @@ import { parseSemver } from '@sentry/utils';
* @return {*} {jest.Describe}
*/
export const conditionalTest = (allowedVersion: { min?: number; max?: number }): jest.Describe => {
const NODE_VERSION = parseSemver(process.versions.node).major;
if (!NODE_VERSION) {
const major = NODE_VERSION.major;
if (!major) {
return describe.skip as jest.Describe;
}

return NODE_VERSION < (allowedVersion.min || -Infinity) || NODE_VERSION > (allowedVersion.max || Infinity)
return major < (allowedVersion.min || -Infinity) || major > (allowedVersion.max || Infinity)
? (describe.skip as jest.Describe)
: (describe as any);
};