Skip to content

Commit c5a0ca8

Browse files
authored
fix: dynamicRequire pass in module (#1687)
1 parent b701b28 commit c5a0ca8

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [utils]: `bundlerSafeRequire` renamed to `dynamicRequire` now takes two arguments, first is should be `module`, second `request` / `moduleName`.
6+
57
## 4.2.2
68

79
- [core]: Several internal fixes regarding integration, exports and domain.

packages/hub/src/hub.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Severity,
99
} from '@sentry/types';
1010
import { logger } from '@sentry/utils/logger';
11-
import { bundlerSafeRequire, getGlobalObject, uuid4 } from '@sentry/utils/misc';
11+
import { dynamicRequire, getGlobalObject, uuid4 } from '@sentry/utils/misc';
1212
import { Carrier, Layer } from './interfaces';
1313
import { Scope } from './scope';
1414

@@ -334,10 +334,10 @@ export function getCurrentHub(): Hub {
334334

335335
// Prefer domains over global if they are there
336336
try {
337-
// We need to use `bundlerSafeRequire` because `require` on it's own will be optimized by webpack.
338-
// We do not want this to happen, we need to try `require` the domain node module and fail if we are in browser
337+
// We need to use `dynamicRequire` because `require` on it's own will be optimized by webpack.
338+
// We do not want this to happen, we need to try to `require` the domain node module and fail if we are in browser
339339
// for example so we do not have to shim it and use `getCurrentHub` universally.
340-
const domain = bundlerSafeRequire('domain');
340+
const domain = dynamicRequire(module, 'domain');
341341
const activeDomain = domain.active;
342342

343343
// If there no active domain, just return global hub

packages/node/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
"fix": "run-s fix:tslint fix:prettier",
4949
"fix:prettier": "prettier --write '{src,test}/**/*.ts'",
5050
"fix:tslint": "tslint --fix -t stylish -p .",
51-
"test": "jest",
51+
"test": "run-s test:jest test:express",
52+
"test:jest": "jest",
5253
"test:watch": "jest --watch --notify",
54+
"test:express": "node test/manual/express.js",
5355
"version": "node ../../scripts/versionbump.js src/version.ts"
5456
},
5557
"jest": {

packages/utils/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919

2020
## General
2121

22-
Common utilities used by the Sentry JavaScript SDKs. **Warning, only submodule imports are allowed here.**
22+
Common utilities used by the Sentry JavaScript SDKs. **Warning, only submodule imports are allowed here.** Also note,
23+
this function shouldn't be used externally, we will break them from time to time. This package is not part of our public
24+
API contract, we use them only internally.

packages/utils/src/misc.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import { isString } from './is';
66
*
77
* @param request The module path to resolve
88
*/
9-
export function bundlerSafeRequire(request: string): any {
10-
// We need to do this check for global.module || module here because if we are calling this inside a
11-
// active domain, global.module is undefined.
12-
// tslint:disable-next-line
13-
return ((getGlobalObject() as any).module || module).require(request);
9+
export function dynamicRequire(mod: NodeModule, request: string): any {
10+
return mod.require(request);
1411
}
1512

1613
/**

0 commit comments

Comments
 (0)