Skip to content

Commit 4af65a8

Browse files
committed
clean up use of regexes
1 parent c037941 commit 4af65a8

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

packages/tracing/src/utils.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { getCurrentHub, Hub } from '@sentry/hub';
22
import { Options, TraceparentData, Transaction } from '@sentry/types';
3-
import { BASE64_REGEX_PATTERN } from '@sentry/utils';
43

54
export const TRACEPARENT_REGEXP = new RegExp(
65
'^[ \\t]*' + // whitespace
@@ -10,11 +9,6 @@ export const TRACEPARENT_REGEXP = new RegExp(
109
'[ \\t]*$', // whitespace
1110
);
1211

13-
export const TRACESTATE_HEADER_REGEX = new RegExp(
14-
`sentry=(${BASE64_REGEX_PATTERN})` + // our part of the header - should be the only part or at least the first part
15-
`(,\\w+=\\w+)*`, // any number of copies of a comma followed by `name=value`
16-
);
17-
1812
/**
1913
* Determines if tracing is currently enabled.
2014
*

packages/tracing/test/browser/request.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ import {
1313
import { addExtensionMethods } from '../../src/hubextensions';
1414
import * as tracingUtils from '../../src/utils';
1515

16+
// This is a normal base64 regex, modified to reflect that fact that we strip the trailing = or == off
17+
const BASE64_MISSING_EQUALS = '([a-zA-Z0-9+/]{4})*(|[a-zA-Z0-9+/]{2,3})';
18+
19+
const TRACESTATE_HEADER_REGEX = new RegExp(
20+
`sentry=(${BASE64_MISSING_EQUALS})` + // our part of the header - should be the only part or at least the first part
21+
`(,\\w+=\\w+)*`, // any number of copies of a comma followed by `name=value`
22+
);
23+
1624
beforeAll(() => {
1725
addExtensionMethods();
1826
// @ts-ignore need to override global Request because it's not in the jest environment (even with an
@@ -235,10 +243,7 @@ describe('callbacks', () => {
235243
'sentry-trace',
236244
expect.stringMatching(tracingUtils.TRACEPARENT_REGEXP),
237245
);
238-
expect(setRequestHeader).toHaveBeenCalledWith(
239-
'tracestate',
240-
expect.stringMatching(tracingUtils.TRACESTATE_HEADER_REGEX),
241-
);
246+
expect(setRequestHeader).toHaveBeenCalledWith('tracestate', expect.stringMatching(TRACESTATE_HEADER_REGEX));
242247
});
243248

244249
it('creates and finishes XHR span on active transaction', () => {

packages/utils/src/string.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import { getGlobalObject } from './compat';
22
import { SentryError } from './error';
33
import { isRegExp, isString } from './is';
44

5-
// The regex is constructed this way because then it's possible to include the base64 pattern as part of other regexes
6-
// See https://tools.ietf.org/html/rfc4648#section-4 for base64 spec
7-
// eslint-disable-next-line no-useless-escape
8-
export const BASE64_REGEX_PATTERN = '(?:[a-zA-Z0-9+/]{4})*(?:|(?:[a-zA-Z0-9+/]{3}=)|(?:[a-zA-Z0-9+/]{2}==))';
9-
export const BASE64_REGEX = new RegExp(BASE64_REGEX_PATTERN);
10-
115
/**
126
* Truncates given string to the maximum characters count
137
*

packages/utils/test/string.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { BASE64_REGEX, base64ToUnicode, isMatchingPattern, truncate, unicodeToBase64 } from '../src/string';
1+
import { base64ToUnicode, isMatchingPattern, truncate, unicodeToBase64 } from '../src/string';
2+
3+
// See https://tools.ietf.org/html/rfc4648#section-4 for base64 spec
4+
// eslint-disable-next-line no-useless-escape
5+
const BASE64_REGEX = /([a-zA-Z0-9+/]{4})*(|([a-zA-Z0-9+/]{3}=)|([a-zA-Z0-9+/]{2}==))/;
26

37
describe('truncate()', () => {
48
test('it works as expected', () => {

0 commit comments

Comments
 (0)