Skip to content

Commit 274e088

Browse files
committed
improve proxy server
1 parent 84badbe commit 274e088

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ example applications can also be used as a reference for using the JS SDKs.
88

99
## How to test apps and save the payloads
1010

11-
1. Change the `APP` variable of `utils/event-proxy-server/src/event-proxy-server.ts` to the app
12-
folder name you want to test.
13-
- Example: `apps/express` -> `APP = 'express'`
14-
2. Make sure you have a folder named like the app in `utils/event-proxy-server/payload-files`.
15-
- Example: `apps/express` -> `utils/event-proxy-server/payload-files/express`
11+
1. Change the proxy-server options in `utils/event-proxy-server/start-event-proxy.ts`
12+
- `appName`: the app folder name you want to test (e.g. `apps/express` -> `appName: 'express'`)
13+
- `filenameOrigin`: where the filename comes from (usually `url` but sometimes `transactionName`)
14+
2. Make sure you have a folder named like the app in `payload-files`.
15+
- Example: `apps/express` -> `payload-files/express`
1616
3. Run `yarn start:proxy-server`.
1717
4. Run `yarn start:[app]` like `start:express`.
1818
5. Check the "Disable Cache" option in the DevTools Network tab of your browser.

utils/event-proxy-server/src/event-proxy-server.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import * as util from 'util';
55
import * as zlib from 'zlib';
66
import type { Envelope, EnvelopeItem } from '@sentry/types';
77

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-
148
const readFile = util.promisify(fs.readFile);
159
const writeFile = util.promisify(fs.writeFile);
1610
const unlink = util.promisify(fs.unlink);
@@ -19,7 +13,12 @@ interface EventProxyServerOptions {
1913
/** Port to start the event proxy server at. */
2014
port: number;
2115
/** 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';
2322
}
2423

2524
interface SentryRequestCallbackData {
@@ -158,9 +157,14 @@ function replaceDynamicValues(jsonData: object[]): object[] {
158157
* The new content is saved into a new file with the url as the filename.
159158
* The temporary file is deleted in the end.
160159
*/
161-
async function transformSavedJSON() {
160+
async function transformSavedJSON(
161+
directory: string,
162+
appName: string,
163+
temporaryFilePath: string,
164+
filenameOrigin: 'url' | 'transactionName',
165+
): Promise<void> {
162166
try {
163-
const data = await readFile(TEMPORARY_FILE_PATH, 'utf8');
167+
const data = await readFile(temporaryFilePath, 'utf8');
164168

165169
const jsonData = addCommaAfterEachLine(data);
166170
const sortedJSON = sortObjectKeys(JSON.parse(jsonData));
@@ -178,20 +182,18 @@ async function transformSavedJSON() {
178182
const transactionName = objData?.transaction;
179183
const url = objData?.request?.url || objData.contexts?.trace?.data?.url;
180184

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;
184186

185187
if (filename) {
186188
const replaceForwardSlashes = (str: string) => str.split('/').join('_');
187189

188-
const filepath = `${DIRECTORY}/${APP}/${replaceForwardSlashes(extractRelevantFileName(filename))}--${type}.json`;
190+
const filepath = `${directory}/${appName}/${replaceForwardSlashes(extractRelevantFileName(filename))}--${type}.json`;
189191

190192
writeFile(filepath, JSON.stringify(transformedJSON, null, 2)).then(() => {
191193
console.log(`Successfully replaced data and saved file in ${filepath}`);
192194

193-
unlink(TEMPORARY_FILE_PATH).then(() =>
194-
console.log(`Successfully deleted ${TEMPORARY_FILE_PATH}`),
195+
unlink(temporaryFilePath).then(() =>
196+
console.log(`Successfully deleted ${temporaryFilePath}`),
195197
);
196198
});
197199
} else {
@@ -209,7 +211,11 @@ async function transformSavedJSON() {
209211
*
210212
*/
211213
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...`);
213219

214220
const proxyServer = http.createServer((proxyRequest, proxyResponse) => {
215221
const proxyRequestChunks: Uint8Array[] = [];
@@ -231,7 +237,7 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
231237
// save the JSON payload into a file
232238
try {
233239
writeFile(TEMPORARY_FILE_PATH, `[${proxyRequestBody}]`).then(() => {
234-
transformSavedJSON();
240+
transformSavedJSON(DIRECTORY, APP, TEMPORARY_FILE_PATH, options.filenameOrigin);
235241
});
236242
} catch (err) {
237243
console.error(`Error writing file ${TEMPORARY_FILE_PATH}`, err);

utils/event-proxy-server/start-event-proxy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import { startEventProxyServer } from './src/event-proxy-server';
22

33
startEventProxyServer({
44
port: 3031,
5-
proxyServerName: 'event-proxy-server',
5+
appName: 'express',
6+
filenameOrigin: 'url',
67
});

0 commit comments

Comments
 (0)