Skip to content

Commit f06b1ff

Browse files
committed
refactor: better comments and remove duplication
1 parent 39975b3 commit f06b1ff

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,5 @@ export {
517517
runInRandomSafeContext as _INTERNAL_runInRandomSafeContext,
518518
safeDateNow as _INTERNAL_safeDateNow,
519519
safeMathRandom as _INTERNAL_safeMathRandom,
520+
type RandomSafeContextRunner as _INTERNAL_RandomSafeContextRunner,
520521
} from './utils/safeRandomGeneratorRunner';

packages/core/src/utils/ratelimit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { DataCategory } from '../types-hoist/datacategory';
22
import type { TransportMakeRequestResponse } from '../types-hoist/transport';
3-
import { runInRandomSafeContext, safeDateNow } from './safeRandomGeneratorRunner';
3+
import { safeDateNow } from './safeRandomGeneratorRunner';
44

55
// Intentionally keeping the key broad, as we don't know for sure what rate limit headers get returned from backend
66
export type RateLimits = Record<string, number>;
@@ -13,7 +13,7 @@ export const DEFAULT_RETRY_AFTER = 60 * 1000; // 60 seconds
1313
* @param now current unix timestamp
1414
*
1515
*/
16-
export function parseRetryAfterHeader(header: string, now: number = runInRandomSafeContext(() => Date.now())): number {
16+
export function parseRetryAfterHeader(header: string, now: number = safeDateNow()): number {
1717
const headerDelay = parseInt(`${header}`, 10);
1818
if (!isNaN(headerDelay)) {
1919
return headerDelay * 1000;

packages/core/src/utils/safeRandomGeneratorRunner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { GLOBAL_OBJ } from './worldwide';
22

3-
type SafeRandomContextRunner = <T>(callback: () => T) => T;
3+
export type RandomSafeContextRunner = <T>(callback: () => T) => T;
44

5-
let RESOLVED_RUNNER: SafeRandomContextRunner | undefined;
5+
let RESOLVED_RUNNER: RandomSafeContextRunner | undefined;
66

77
/**
88
* Simple wrapper that allows SDKs to *secretly* set context wrapper to generate safe random IDs in cache components contexts
@@ -14,7 +14,7 @@ export function runInRandomSafeContext<T>(cb: () => T): T {
1414
}
1515

1616
const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__');
17-
const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: SafeRandomContextRunner } = GLOBAL_OBJ;
17+
const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: RandomSafeContextRunner } = GLOBAL_OBJ;
1818
if (!(sym in globalWithSymbol) || typeof globalWithSymbol[sym] !== 'function') {
1919
return cb();
2020
}

packages/nextjs/src/server/prepareSafeIdGeneratorContext.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { debug, GLOBAL_OBJ } from '@sentry/core';
1+
import { type _INTERNAL_RandomSafeContextRunner as RandomSafeContextRunner, debug, GLOBAL_OBJ } from '@sentry/core';
22
import { DEBUG_BUILD } from '../common/debug-build';
33

4-
type SafeRandomContextRunner = <T>(callback: () => T) => T;
5-
4+
// Inline AsyncLocalStorage interface from current types
5+
// Avoids conflict with resolving it from getBuiltinModule
66
type OriginalAsyncLocalStorage = typeof AsyncLocalStorage;
77

88
/**
@@ -11,7 +11,7 @@ type OriginalAsyncLocalStorage = typeof AsyncLocalStorage;
1111
*/
1212
export function prepareSafeIdGeneratorContext(): void {
1313
const sym = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__');
14-
const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: SafeRandomContextRunner } = GLOBAL_OBJ;
14+
const globalWithSymbol: typeof GLOBAL_OBJ & { [sym]?: RandomSafeContextRunner } = GLOBAL_OBJ;
1515
const als = getAsyncLocalStorage();
1616
if (!als) {
1717
DEBUG_BUILD &&
@@ -27,11 +27,14 @@ export function prepareSafeIdGeneratorContext(): void {
2727

2828
function getAsyncLocalStorage(): OriginalAsyncLocalStorage | undefined {
2929
// May exist in the Next.js runtime globals
30-
if ('AsyncLocalStorage' in GLOBAL_OBJ) {
30+
// Doesn't exist in some of our tests
31+
if (typeof AsyncLocalStorage !== 'undefined') {
3132
return AsyncLocalStorage;
3233
}
3334

3435
// Try to resolve it dynamically without synchronously importing the module
36+
// This is done to avoid importing the module synchronously at the top
37+
// which means this is safe across runtimes
3538
if ('getBuiltinModule' in process && typeof process.getBuiltinModule === 'function') {
3639
const { AsyncLocalStorage } = process.getBuiltinModule('async_hooks') ?? {};
3740

0 commit comments

Comments
 (0)