@@ -5,12 +5,6 @@ import * as util from 'util';
5
5
import * as zlib from 'zlib' ;
6
6
import type { Envelope , EnvelopeItem } from '@sentry/types' ;
7
7
8
- // change to folder name of app to test
9
- const APP = 'express' ;
10
- const DIRECTORY = '../../payload-files' ;
11
-
12
- const TEMPORARY_FILE_PATH = `${ DIRECTORY } /${ APP } /temporary.json` ;
13
-
14
8
const readFile = util . promisify ( fs . readFile ) ;
15
9
const writeFile = util . promisify ( fs . writeFile ) ;
16
10
const unlink = util . promisify ( fs . unlink ) ;
@@ -19,7 +13,12 @@ interface EventProxyServerOptions {
19
13
/** Port to start the event proxy server at. */
20
14
port : number ;
21
15
/** The name for the proxy server used for referencing it with listener functions */
22
- proxyServerName : string ;
16
+ /* The folder name of the app to test (e.g. 'nextjs-13_2_0' or 'express') */
17
+ appName : string ;
18
+ /** Change to `url` or `transactionName` depending on what you want to use as filename
19
+ /* Using transaction name as filename is useful when testing frameworks (such as Next.js) as
20
+ /* the API routes are often called from the client and `url` would just be 'localhost:3030' */
21
+ filenameOrigin : 'url' | 'transactionName' ;
23
22
}
24
23
25
24
interface SentryRequestCallbackData {
@@ -158,9 +157,14 @@ function replaceDynamicValues(jsonData: object[]): object[] {
158
157
* The new content is saved into a new file with the url as the filename.
159
158
* The temporary file is deleted in the end.
160
159
*/
161
- async function transformSavedJSON ( ) {
160
+ async function transformSavedJSON (
161
+ directory : string ,
162
+ appName : string ,
163
+ temporaryFilePath : string ,
164
+ filenameOrigin : 'url' | 'transactionName' ,
165
+ ) : Promise < void > {
162
166
try {
163
- const data = await readFile ( TEMPORARY_FILE_PATH , 'utf8' ) ;
167
+ const data = await readFile ( temporaryFilePath , 'utf8' ) ;
164
168
165
169
const jsonData = addCommaAfterEachLine ( data ) ;
166
170
const sortedJSON = sortObjectKeys ( JSON . parse ( jsonData ) ) ;
@@ -178,20 +182,18 @@ async function transformSavedJSON() {
178
182
const transactionName = objData ?. transaction ;
179
183
const url = objData ?. request ?. url || objData . contexts ?. trace ?. data ?. url ;
180
184
181
- // Change to `url` or `transactionName` depending on what you want to use as filename
182
- // Using transaction name as filename is useful when testing frameworks (such as Next.js) as the API routes are often called from the client and `url` would just be 'localhost:3030'
183
- const filename = url ; // transactionName;
185
+ const filename = filenameOrigin === 'transactionName' ? transactionName : url ;
184
186
185
187
if ( filename ) {
186
188
const replaceForwardSlashes = ( str : string ) => str . split ( '/' ) . join ( '_' ) ;
187
189
188
- const filepath = `${ DIRECTORY } /${ APP } /${ replaceForwardSlashes ( extractRelevantFileName ( filename ) ) } --${ type } .json` ;
190
+ const filepath = `${ directory } /${ appName } /${ replaceForwardSlashes ( extractRelevantFileName ( filename ) ) } --${ type } .json` ;
189
191
190
192
writeFile ( filepath , JSON . stringify ( transformedJSON , null , 2 ) ) . then ( ( ) => {
191
193
console . log ( `Successfully replaced data and saved file in ${ filepath } ` ) ;
192
194
193
- unlink ( TEMPORARY_FILE_PATH ) . then ( ( ) =>
194
- console . log ( `Successfully deleted ${ TEMPORARY_FILE_PATH } ` ) ,
195
+ unlink ( temporaryFilePath ) . then ( ( ) =>
196
+ console . log ( `Successfully deleted ${ temporaryFilePath } ` ) ,
195
197
) ;
196
198
} ) ;
197
199
} else {
@@ -209,7 +211,11 @@ async function transformSavedJSON() {
209
211
*
210
212
*/
211
213
export async function startEventProxyServer ( options : EventProxyServerOptions ) : Promise < void > {
212
- console . log ( `Proxy server "${ options . proxyServerName } " running. Waiting for events...` ) ;
214
+ const APP = options . appName ;
215
+ const DIRECTORY = '../../payload-files' ;
216
+ const TEMPORARY_FILE_PATH = `${ DIRECTORY } /${ APP } /temporary.json` ;
217
+
218
+ console . log ( `Proxy server for "${ APP } " running. Waiting for events...` ) ;
213
219
214
220
const proxyServer = http . createServer ( ( proxyRequest , proxyResponse ) => {
215
221
const proxyRequestChunks : Uint8Array [ ] = [ ] ;
@@ -231,7 +237,7 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
231
237
// save the JSON payload into a file
232
238
try {
233
239
writeFile ( TEMPORARY_FILE_PATH , `[${ proxyRequestBody } ]` ) . then ( ( ) => {
234
- transformSavedJSON ( ) ;
240
+ transformSavedJSON ( DIRECTORY , APP , TEMPORARY_FILE_PATH , options . filenameOrigin ) ;
235
241
} ) ;
236
242
} catch ( err ) {
237
243
console . error ( `Error writing file ${ TEMPORARY_FILE_PATH } ` , err ) ;
0 commit comments