11import { createRequire } from "module" ;
22import { dirname , join } from "path" ;
33
4- // Import rollbar's truncation module using CommonJS require
4+ //
5+ // -- Import rollbar's truncation module using CommonJS require --
6+ //
7+ // TODO - remove this workaround once https://github.com/rollbar/rollbar.js/issues/1283 is fixed.
58const require = createRequire ( import . meta. url ) ;
69
710// Define types for the rollbar truncation module
@@ -13,21 +16,72 @@ interface RollbarTruncation {
1316 ) => { value : string } ;
1417}
1518
16- // Load rollbar/src/truncation using require.
17- // TODO - remove this workaround once https://github.com/rollbar/rollbar.js/issues/1283 is fixed.
18- let truncation : RollbarTruncation ;
19- try {
20- // First try the direct import (works when running from source)
21- truncation = require ( "rollbar/src/truncation" ) as RollbarTruncation ;
22- } catch {
23- // If that fails, resolve through the main module path
24- // The main module is at rollbar/src/server/rollbar.js
25- // So we need to go up to rollbar/src/ and then find truncation.js
26- const rollbarPath = require . resolve ( "rollbar" ) ;
27- const rollbarSrcDir = dirname ( dirname ( rollbarPath ) ) ; // Go up from src/server to src
28- const truncationPath = join ( rollbarSrcDir , "truncation.js" ) ;
29- truncation = require ( truncationPath ) as RollbarTruncation ;
19+ // Type for the loaded module which might be in various formats
20+ interface LoadedModule {
21+ truncate ?: unknown ;
22+ default ?: unknown ;
23+ __esModule ?: boolean ;
24+ [ key : string ] : unknown ;
25+ }
26+
27+ // Since rollbar 3.0.0-alpha.2 doesn't export the truncation subpath,
28+ // we need to resolve through the main module path
29+ // The main module is at rollbar/src/server/rollbar.js
30+ // So we need to go up to rollbar/src/ and then find truncation.js
31+ const rollbarPath = require . resolve ( "rollbar" ) ;
32+ const rollbarSrcDir = dirname ( dirname ( rollbarPath ) ) ; // Go up from src/server to src
33+ const truncationPath = join ( rollbarSrcDir , "truncation.js" ) ;
34+
35+ // Use dynamic import to handle the module properly
36+ // This is wrapped in an IIFE to handle the async nature
37+ /* c8 ignore start */
38+ let truncation : RollbarTruncation = {
39+ truncate : ( ) => {
40+ throw new Error ( "Truncation not yet initialized" ) ;
41+ } ,
42+ } ;
43+ /* c8 ignore stop */
44+
45+ // Load the module synchronously using require
46+ const truncationModule = require ( truncationPath ) as LoadedModule ;
47+
48+ // Handle different module formats
49+ // When TypeScript compiles and the output is loaded, we might get:
50+ // 1. Direct CommonJS exports: { truncate: fn, ... }
51+ // 2. Wrapped ES module: { __esModule: true, default: { truncate: fn, ... } }
52+ // 3. Other wrapper: { default: { truncate: fn, ... } }
53+ /* c8 ignore start */
54+ if ( typeof truncationModule . truncate === "function" ) {
55+ // Direct CommonJS export
56+ truncation = truncationModule as RollbarTruncation ;
57+ } else if (
58+ truncationModule . default &&
59+ typeof ( truncationModule . default as LoadedModule ) . truncate === "function"
60+ ) {
61+ // Wrapped with default export
62+ truncation = truncationModule . default as RollbarTruncation ;
63+ } else if ( truncationModule . __esModule && truncationModule . default ) {
64+ // ES module interop
65+ truncation = truncationModule . default as RollbarTruncation ;
66+ } else {
67+ // Last resort - try to find truncate function anywhere in the module
68+ for ( const key of Object . keys ( truncationModule ) ) {
69+ const value = truncationModule [ key ] ;
70+ if (
71+ value &&
72+ typeof value === "object" &&
73+ typeof ( value as LoadedModule ) . truncate === "function"
74+ ) {
75+ truncation = value as RollbarTruncation ;
76+ break ;
77+ }
78+ }
3079}
80+ /* c8 ignore stop */
81+
82+ //
83+ // -- End workaround --
84+ //
3185
3286// Token estimation constants
3387const CHARS_PER_TOKEN = 4 ; // Rough estimate: 1 token ≈ 4 characters
0 commit comments