Skip to content

Commit bad42a0

Browse files
committed
create node wrapper for functions to pass node deps automatically
1 parent b56f999 commit bad42a0

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

packages/node/src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type {
1515
Thread,
1616
User,
1717
} from '@sentry/types';
18+
export type { AddRequestDataToEventOptions, CrossPlatformRequest } from '@sentry/utils';
1819

1920
export type { NodeOptions } from './types';
2021

@@ -44,7 +45,17 @@ export {
4445

4546
export { NodeClient } from './client';
4647
export { makeNodeTransport } from './transports';
47-
export { defaultIntegrations, init, defaultStackParser, lastEventId, flush, close, getSentryRelease } from './sdk';
48+
export {
49+
addRequestDataToEvent,
50+
extractRequestData,
51+
defaultIntegrations,
52+
init,
53+
defaultStackParser,
54+
lastEventId,
55+
flush,
56+
close,
57+
getSentryRelease,
58+
} from './sdk';
4859
export { deepReadDirSync } from './utils';
4960

5061
import { Integrations as CoreIntegrations } from '@sentry/core';

packages/node/src/sdk.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
/* eslint-disable max-lines */
12
import { getCurrentHub, getIntegrationsToSetup, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
23
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
3-
import { SessionStatus, StackParser } from '@sentry/types';
4-
import { createStackParser, getGlobalObject, logger, stackParserFromStackParserOptions } from '@sentry/utils';
4+
import { Event, ExtractedNodeRequestData, SessionStatus, StackParser } from '@sentry/types';
5+
import {
6+
addRequestDataToEvent as _addRequestDataToEvent,
7+
AddRequestDataToEventOptions,
8+
createStackParser,
9+
CrossPlatformRequest,
10+
extractRequestData as _extractRequestData,
11+
getGlobalObject,
12+
logger,
13+
stackParserFromStackParserOptions,
14+
} from '@sentry/utils';
15+
import * as cookie from 'cookie';
516
import * as domain from 'domain';
17+
import * as url from 'url';
618

719
import { NodeClient } from './client';
820
import { Console, ContextLines, Http, LinkedErrors, OnUncaughtException, OnUnhandledRejection } from './integrations';
@@ -255,3 +267,51 @@ function startSessionTracking(): void {
255267
if (session && !terminalStates.includes(session.status)) hub.endSession();
256268
});
257269
}
270+
271+
/**
272+
* Add data from the given request to the given event
273+
*
274+
* (Note that there is no sister function to this one in `@sentry/browser`, because the whole point of this wrapper is
275+
* to pass along injected dependencies, which isn't necessary in a browser context. Isomorphic packages like
276+
* `@sentry/nextjs` should export directly from `@sentry/utils` in their browser index file.)
277+
*
278+
* @param event The event to which the request data will be added
279+
* @param req Request object
280+
* @param options.include Flags to control what data is included
281+
* @hidden
282+
*/
283+
export function addRequestDataToEvent(
284+
event: Event,
285+
req: CrossPlatformRequest,
286+
options?: Omit<AddRequestDataToEventOptions, 'deps'>,
287+
): Event {
288+
return _addRequestDataToEvent(event, req, {
289+
...options,
290+
// We have to inject these node-only dependencies because we can't import them in `@sentry/utils`, where the
291+
// original function lives
292+
deps: { cookie, url },
293+
});
294+
}
295+
296+
/**
297+
* Normalize data from the request object, accounting for framework differences.
298+
*
299+
* (Note that there is no sister function to this one in `@sentry/browser`, because the whole point of this wrapper is
300+
* to inject dependencies, which isn't necessary in a browser context. Isomorphic packages like `@sentry/nextjs` should
301+
* export directly from `@sentry/utils` in their browser index file.)
302+
*
303+
* @param req The request object from which to extract data
304+
* @param options.keys An optional array of keys to include in the normalized data. Defaults to DEFAULT_REQUEST_KEYS if
305+
* not provided.
306+
* @returns An object containing normalized request data
307+
*/
308+
export function extractRequestData(
309+
req: CrossPlatformRequest,
310+
options?: {
311+
include?: string[];
312+
},
313+
): ExtractedNodeRequestData {
314+
// We have to inject these node-only dependencies because we can't import them in `@sentry/utils`, where the original
315+
// function lives
316+
return _extractRequestData(req, { ...options, deps: { cookie, url } });
317+
}

0 commit comments

Comments
 (0)