1
+ import { createRequire } from 'node:module' ;
1
2
import { arch as _arch , platform as _platform } from 'node:os' ;
2
3
import { join , resolve } from 'node:path' ;
4
+ import { dirname } from 'node:path' ;
3
5
import { env , versions } from 'node:process' ;
6
+ import { fileURLToPath , pathToFileURL } from 'node:url' ;
4
7
import { threadId } from 'node:worker_threads' ;
5
8
import { familySync } from 'detect-libc' ;
6
9
import { getAbi } from 'node-abi' ;
@@ -15,84 +18,89 @@ import type {
15
18
} from './types' ;
16
19
import type { ProfileFormat } from './types' ;
17
20
18
- // #START_SENTRY_ESM_SHIM
19
- // When building for ESM, we shim require to use createRequire and __dirname.
20
- // We need to do this because .node extensions in esm are not supported.
21
- // The comment below this line exists as a placeholder for where to insert the shim.
22
- // #END_SENTRY_ESM_SHIM
21
+ declare const __IMPORT_META_URL_REPLACEMENT__ : string ;
23
22
24
23
const stdlib = familySync ( ) ;
25
24
const platform = process . env [ 'BUILD_PLATFORM' ] || _platform ( ) ;
26
25
const arch = process . env [ 'BUILD_ARCH' ] || _arch ( ) ;
27
26
const abi = getAbi ( versions . node , 'node' ) ;
28
27
const identifier = [ platform , arch , stdlib , abi ] . filter ( c => c !== undefined && c !== null ) . join ( '-' ) ;
29
28
30
- const built_from_source_path = resolve ( __dirname , '..' , `./sentry_cpu_profiler-${ identifier } ` ) ;
31
-
32
29
/**
33
30
* Imports cpp bindings based on the current platform and architecture.
34
31
*/
35
32
// eslint-disable-next-line complexity
36
33
export function importCppBindingsModule ( ) : PrivateV8CpuProfilerBindings {
34
+ // We need to work around using import.meta.url directly with __IMPORT_META_URL_REPLACEMENT__ because jest complains about it.
35
+ const importMetaUrl =
36
+ typeof __IMPORT_META_URL_REPLACEMENT__ !== 'undefined'
37
+ ? // This case is always hit when the SDK is built
38
+ __IMPORT_META_URL_REPLACEMENT__
39
+ : // This case is hit when the tests are run
40
+ pathToFileURL ( __filename ) . href ;
41
+
42
+ const createdRequire = createRequire ( importMetaUrl ) ;
43
+ const esmCompatibleDirname = dirname ( fileURLToPath ( importMetaUrl ) ) ;
44
+
37
45
// If a binary path is specified, use that.
38
46
if ( env [ 'SENTRY_PROFILER_BINARY_PATH' ] ) {
39
47
const envPath = env [ 'SENTRY_PROFILER_BINARY_PATH' ] ;
40
- return require ( envPath ) ;
48
+ return createdRequire ( envPath ) ;
41
49
}
42
50
43
51
// If a user specifies a different binary dir, they are in control of the binaries being moved there
44
52
if ( env [ 'SENTRY_PROFILER_BINARY_DIR' ] ) {
45
53
const binaryPath = join ( resolve ( env [ 'SENTRY_PROFILER_BINARY_DIR' ] ) , `sentry_cpu_profiler-${ identifier } ` ) ;
46
- return require ( `${ binaryPath } .node` ) ;
54
+ return createdRequire ( `${ binaryPath } .node` ) ;
47
55
}
48
56
49
57
// We need the fallthrough so that in the end, we can fallback to the dynamic require.
50
58
// This is for cases where precompiled binaries were not provided, but may have been compiled from source.
51
59
if ( platform === 'darwin' ) {
52
60
if ( arch === 'x64' ) {
53
61
if ( abi === '93' ) {
54
- return require ( '../sentry_cpu_profiler-darwin-x64-93.node' ) ;
62
+ return createdRequire ( '../sentry_cpu_profiler-darwin-x64-93.node' ) ;
55
63
}
56
64
if ( abi === '108' ) {
57
- return require ( '../sentry_cpu_profiler-darwin-x64-108.node' ) ;
65
+ return createdRequire ( '../sentry_cpu_profiler-darwin-x64-108.node' ) ;
58
66
}
59
67
if ( abi === '115' ) {
60
- return require ( '../sentry_cpu_profiler-darwin-x64-115.node' ) ;
68
+ return createdRequire ( '../sentry_cpu_profiler-darwin-x64-115.node' ) ;
61
69
}
62
70
if ( abi === '127' ) {
63
- return require ( '../sentry_cpu_profiler-darwin-x64-127.node' ) ;
71
+ return createdRequire ( '../sentry_cpu_profiler-darwin-x64-127.node' ) ;
64
72
}
65
73
}
66
74
67
75
if ( arch === 'arm64' ) {
68
76
if ( abi === '93' ) {
69
- return require ( '../sentry_cpu_profiler-darwin-arm64-93.node' ) ;
77
+ return createdRequire ( '../sentry_cpu_profiler-darwin-arm64-93.node' ) ;
70
78
}
71
79
if ( abi === '108' ) {
72
- return require ( '../sentry_cpu_profiler-darwin-arm64-108.node' ) ;
80
+ return createdRequire ( '../sentry_cpu_profiler-darwin-arm64-108.node' ) ;
73
81
}
74
82
if ( abi === '115' ) {
75
- return require ( '../sentry_cpu_profiler-darwin-arm64-115.node' ) ;
83
+ return createdRequire ( '../sentry_cpu_profiler-darwin-arm64-115.node' ) ;
76
84
}
77
85
if ( abi === '127' ) {
78
- return require ( '../sentry_cpu_profiler-darwin-arm64-127.node' ) ;
86
+ return createdRequire ( '../sentry_cpu_profiler-darwin-arm64-127.node' ) ;
79
87
}
80
88
}
81
89
}
82
90
83
91
if ( platform === 'win32' ) {
84
92
if ( arch === 'x64' ) {
85
93
if ( abi === '93' ) {
86
- return require ( '../sentry_cpu_profiler-win32-x64-93.node' ) ;
94
+ return createdRequire ( '../sentry_cpu_profiler-win32-x64-93.node' ) ;
87
95
}
88
96
if ( abi === '108' ) {
89
- return require ( '../sentry_cpu_profiler-win32-x64-108.node' ) ;
97
+ return createdRequire ( '../sentry_cpu_profiler-win32-x64-108.node' ) ;
90
98
}
91
99
if ( abi === '115' ) {
92
- return require ( '../sentry_cpu_profiler-win32-x64-115.node' ) ;
100
+ return createdRequire ( '../sentry_cpu_profiler-win32-x64-115.node' ) ;
93
101
}
94
102
if ( abi === '127' ) {
95
- return require ( '../sentry_cpu_profiler-win32-x64-127.node' ) ;
103
+ return createdRequire ( '../sentry_cpu_profiler-win32-x64-127.node' ) ;
96
104
}
97
105
}
98
106
}
@@ -101,66 +109,68 @@ export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
101
109
if ( arch === 'x64' ) {
102
110
if ( stdlib === 'musl' ) {
103
111
if ( abi === '93' ) {
104
- return require ( '../sentry_cpu_profiler-linux-x64-musl-93.node' ) ;
112
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-musl-93.node' ) ;
105
113
}
106
114
if ( abi === '108' ) {
107
- return require ( '../sentry_cpu_profiler-linux-x64-musl-108.node' ) ;
115
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-musl-108.node' ) ;
108
116
}
109
117
if ( abi === '115' ) {
110
- return require ( '../sentry_cpu_profiler-linux-x64-musl-115.node' ) ;
118
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-musl-115.node' ) ;
111
119
}
112
120
if ( abi === '127' ) {
113
- return require ( '../sentry_cpu_profiler-linux-x64-musl-127.node' ) ;
121
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-musl-127.node' ) ;
114
122
}
115
123
}
116
124
if ( stdlib === 'glibc' ) {
117
125
if ( abi === '93' ) {
118
- return require ( '../sentry_cpu_profiler-linux-x64-glibc-93.node' ) ;
126
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-glibc-93.node' ) ;
119
127
}
120
128
if ( abi === '108' ) {
121
- return require ( '../sentry_cpu_profiler-linux-x64-glibc-108.node' ) ;
129
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-glibc-108.node' ) ;
122
130
}
123
131
if ( abi === '115' ) {
124
- return require ( '../sentry_cpu_profiler-linux-x64-glibc-115.node' ) ;
132
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-glibc-115.node' ) ;
125
133
}
126
134
if ( abi === '127' ) {
127
- return require ( '../sentry_cpu_profiler-linux-x64-glibc-127.node' ) ;
135
+ return createdRequire ( '../sentry_cpu_profiler-linux-x64-glibc-127.node' ) ;
128
136
}
129
137
}
130
138
}
131
139
if ( arch === 'arm64' ) {
132
140
if ( stdlib === 'musl' ) {
133
141
if ( abi === '93' ) {
134
- return require ( '../sentry_cpu_profiler-linux-arm64-musl-93.node' ) ;
142
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-musl-93.node' ) ;
135
143
}
136
144
if ( abi === '108' ) {
137
- return require ( '../sentry_cpu_profiler-linux-arm64-musl-108.node' ) ;
145
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-musl-108.node' ) ;
138
146
}
139
147
if ( abi === '115' ) {
140
- return require ( '../sentry_cpu_profiler-linux-arm64-musl-115.node' ) ;
148
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-musl-115.node' ) ;
141
149
}
142
150
if ( abi === '127' ) {
143
- return require ( '../sentry_cpu_profiler-linux-arm64-musl-127.node' ) ;
151
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-musl-127.node' ) ;
144
152
}
145
153
}
146
154
147
155
if ( stdlib === 'glibc' ) {
148
156
if ( abi === '93' ) {
149
- return require ( '../sentry_cpu_profiler-linux-arm64-glibc-93.node' ) ;
157
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-glibc-93.node' ) ;
150
158
}
151
159
if ( abi === '108' ) {
152
- return require ( '../sentry_cpu_profiler-linux-arm64-glibc-108.node' ) ;
160
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-glibc-108.node' ) ;
153
161
}
154
162
if ( abi === '115' ) {
155
- return require ( '../sentry_cpu_profiler-linux-arm64-glibc-115.node' ) ;
163
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-glibc-115.node' ) ;
156
164
}
157
165
if ( abi === '127' ) {
158
- return require ( '../sentry_cpu_profiler-linux-arm64-glibc-127.node' ) ;
166
+ return createdRequire ( '../sentry_cpu_profiler-linux-arm64-glibc-127.node' ) ;
159
167
}
160
168
}
161
169
}
162
170
}
163
- return require ( `${ built_from_source_path } .node` ) ;
171
+
172
+ const built_from_source_path = resolve ( esmCompatibleDirname , '..' , `sentry_cpu_profiler-${ identifier } ` ) ;
173
+ return createdRequire ( `${ built_from_source_path } .node` ) ;
164
174
}
165
175
166
176
const PrivateCpuProfilerBindings : PrivateV8CpuProfilerBindings = importCppBindingsModule ( ) ;
0 commit comments