@justanalyticsapp/node 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,15 +2,10 @@
2
2
  * @file packages/node-sdk/src/utils/safe-require.ts
3
3
  * @description Bundler-opaque dynamic require utility.
4
4
  *
5
- * Turbopack, Webpack, esbuild, and Rollup all statically analyze `require('...')`
6
- * calls and try to resolve the module at build time -- even inside try/catch.
7
- * This causes build errors for applications that don't have all of our optional
8
- * peer dependencies installed.
9
- *
10
- * Using `new Function('id', 'return require(id)')` creates an indirect require
11
- * call that no bundler can statically analyze, so the module path is resolved
12
- * only at runtime. If the module is not installed, `safeRequire` returns `null`
13
- * instead of throwing.
5
+ * Provides a require() wrapper that:
6
+ * 1. Prevents bundlers (Turbopack, Webpack) from statically analyzing the import
7
+ * 2. Falls back gracefully when the module is not installed
8
+ * 3. Works in both CJS and ESM environments
14
9
  */
15
10
  /**
16
11
  * Dynamically require a module without being traced by bundlers.
@@ -3,20 +3,41 @@
3
3
  * @file packages/node-sdk/src/utils/safe-require.ts
4
4
  * @description Bundler-opaque dynamic require utility.
5
5
  *
6
- * Turbopack, Webpack, esbuild, and Rollup all statically analyze `require('...')`
7
- * calls and try to resolve the module at build time -- even inside try/catch.
8
- * This causes build errors for applications that don't have all of our optional
9
- * peer dependencies installed.
10
- *
11
- * Using `new Function('id', 'return require(id)')` creates an indirect require
12
- * call that no bundler can statically analyze, so the module path is resolved
13
- * only at runtime. If the module is not installed, `safeRequire` returns `null`
14
- * instead of throwing.
6
+ * Provides a require() wrapper that:
7
+ * 1. Prevents bundlers (Turbopack, Webpack) from statically analyzing the import
8
+ * 2. Falls back gracefully when the module is not installed
9
+ * 3. Works in both CJS and ESM environments
15
10
  */
16
11
  Object.defineProperty(exports, "__esModule", { value: true });
17
12
  exports.safeRequire = safeRequire;
18
- // eslint-disable-next-line @typescript-eslint/no-implied-eval
19
- const dynamicRequire = new Function('id', 'return require(id)');
13
+ const module_1 = require("module");
14
+ // Build a require function that works in all contexts:
15
+ // - createRequire provides proper Node.js module resolution
16
+ // - Falls back to global require if available
17
+ // - Falls back to Function-based require as last resort
18
+ let _require = null;
19
+ try {
20
+ // createRequire gives us proper module resolution from the SDK's location
21
+ _require = (0, module_1.createRequire)(__filename);
22
+ }
23
+ catch {
24
+ try {
25
+ // Fallback: use global require if available (CJS context)
26
+ if (typeof require === 'function') {
27
+ _require = require;
28
+ }
29
+ }
30
+ catch {
31
+ // Last resort: Function-based require (may not resolve from correct location)
32
+ try {
33
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
34
+ _require = new Function('id', 'return require(id)');
35
+ }
36
+ catch {
37
+ _require = null;
38
+ }
39
+ }
40
+ }
20
41
  /**
21
42
  * Dynamically require a module without being traced by bundlers.
22
43
  * Returns `null` if the module is not installed or cannot be loaded.
@@ -26,8 +47,10 @@ const dynamicRequire = new Function('id', 'return require(id)');
26
47
  */
27
48
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
49
  function safeRequire(id) {
50
+ if (!_require)
51
+ return null;
29
52
  try {
30
- return dynamicRequire(id);
53
+ return _require(id);
31
54
  }
32
55
  catch {
33
56
  return null;
@@ -1 +1 @@
1
- {"version":3,"file":"safe-require.js","sourceRoot":"","sources":["../../src/utils/safe-require.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;AAaH,kCAMC;AAjBD,8DAA8D;AAC9D,MAAM,cAAc,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAwB,CAAC;AAEvF;;;;;;GAMG;AACH,8DAA8D;AAC9D,SAAgB,WAAW,CAAC,EAAU;IACpC,IAAI,CAAC;QACH,OAAO,cAAc,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"safe-require.js","sourceRoot":"","sources":["../../src/utils/safe-require.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AAsCH,kCAOC;AA3CD,mCAAuC;AAEvC,uDAAuD;AACvD,4DAA4D;AAC5D,8CAA8C;AAC9C,wDAAwD;AACxD,IAAI,QAAQ,GAAiC,IAAI,CAAC;AAElD,IAAI,CAAC;IACH,0EAA0E;IAC1E,QAAQ,GAAG,IAAA,sBAAa,EAAC,UAAU,CAAC,CAAC;AACvC,CAAC;AAAC,MAAM,CAAC;IACP,IAAI,CAAC;QACH,0DAA0D;QAC1D,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE,CAAC;YAClC,QAAQ,GAAG,OAAO,CAAC;QACrB,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,8EAA8E;QAC9E,IAAI,CAAC;YACH,8DAA8D;YAC9D,QAAQ,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAwB,CAAC;QAC7E,CAAC;QAAC,MAAM,CAAC;YACP,QAAQ,GAAG,IAAI,CAAC;QAClB,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,8DAA8D;AAC9D,SAAgB,WAAW,CAAC,EAAU;IACpC,IAAI,CAAC,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3B,IAAI,CAAC;QACH,OAAO,QAAQ,CAAC,EAAE,CAAC,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justanalyticsapp/node",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "JustAnalytics Node.js SDK for distributed tracing and observability",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",