Skip to content

Commit d67af6b

Browse files
committed
fix(remix): isolate node-specific imports to their own paths
1 parent c773b89 commit d67af6b

File tree

3 files changed

+47
-44
lines changed

3 files changed

+47
-44
lines changed

packages/remix/src/server/instrumentServer.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
hasSpansEnabled,
2525
httpHeadersToSpanAttributes,
2626
isNodeEnv,
27-
loadModule,
2827
SEMANTIC_ATTRIBUTE_SENTRY_OP,
2928
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
3029
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
@@ -35,7 +34,6 @@ import {
3534
winterCGRequestToRequestData,
3635
withIsolationScope,
3736
} from '@sentry/core';
38-
import { createRequire } from 'module';
3937
import { DEBUG_BUILD } from '../utils/debug-build';
4038
import { createRoutes, getTransactionName } from '../utils/utils';
4139
import { extractData, isResponse, json } from '../utils/vendor/response';
@@ -477,44 +475,3 @@ export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?:
477475
return wrapRequestHandler(requestHandler, newBuild, options);
478476
};
479477
};
480-
481-
/**
482-
* Helper to load a module in both CJS and ESM contexts.
483-
* In ESM, we use createRequire to create a require function.
484-
* In CJS, we use the standard loadModule.
485-
*/
486-
function loadModuleCompat<T>(moduleName: string): T | undefined {
487-
// Check if we're in ESM context (module doesn't exist)
488-
if (typeof module === 'undefined') {
489-
// ESM context - use createRequire to get a require function
490-
try {
491-
// eslint-disable-next-line @typescript-eslint/no-var-requires
492-
const require = createRequire(import.meta.url);
493-
return require(moduleName) as T;
494-
} catch {
495-
return undefined;
496-
}
497-
} else {
498-
// CJS context - use loadModule with module reference
499-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
500-
return loadModule<T>(moduleName, module as any);
501-
}
502-
}
503-
504-
/**
505-
* Monkey-patch Remix's `createRequestHandler` from `@remix-run/server-runtime`
506-
* which Remix Adapters (https://remix.run/docs/en/v1/api/remix) use underneath.
507-
*/
508-
export function instrumentServer(options?: { instrumentTracing?: boolean }): void {
509-
const pkg = loadModuleCompat<{
510-
createRequestHandler: CreateRequestHandlerFunction;
511-
}>('@remix-run/server-runtime');
512-
513-
if (!pkg) {
514-
DEBUG_BUILD && debug.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');
515-
516-
return;
517-
}
518-
519-
fill(pkg, 'createRequestHandler', makeWrappedCreateRequestHandler(options));
520-
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { CreateRequestHandlerFunction } from '@remix-run/server-runtime';
2+
import { debug, fill, loadModule } from '@sentry/core';
3+
import { createRequire } from 'module';
4+
import { DEBUG_BUILD } from '../utils/debug-build';
5+
import { makeWrappedCreateRequestHandler } from './instrumentServer';
6+
7+
/**
8+
* Helper to load a module in both CJS and ESM contexts.
9+
* In ESM, we use createRequire to create a require function.
10+
* In CJS, we use the standard loadModule.
11+
*/
12+
function loadModuleCompat<T>(moduleName: string): T | undefined {
13+
// Check if we're in ESM context (module doesn't exist)
14+
if (typeof module === 'undefined') {
15+
// ESM context - use createRequire to get a require function
16+
try {
17+
// eslint-disable-next-line @typescript-eslint/no-var-requires
18+
const require = createRequire(import.meta.url);
19+
return require(moduleName) as T;
20+
} catch {
21+
return undefined;
22+
}
23+
} else {
24+
// CJS context - use loadModule with module reference
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
return loadModule<T>(moduleName, module as any);
27+
}
28+
}
29+
30+
/**
31+
* Monkey-patch Remix's `createRequestHandler` from `@remix-run/server-runtime`
32+
* which Remix Adapters (https://remix.run/docs/en/v1/api/remix) use underneath.
33+
*/
34+
export function instrumentServer(options?: { instrumentTracing?: boolean }): void {
35+
const pkg = loadModuleCompat<{
36+
createRequestHandler: CreateRequestHandlerFunction;
37+
}>('@remix-run/server-runtime');
38+
39+
if (!pkg) {
40+
DEBUG_BUILD && debug.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');
41+
42+
return;
43+
}
44+
45+
fill(pkg, 'createRequestHandler', makeWrappedCreateRequestHandler(options));
46+
}

packages/remix/src/server/sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import type { NodeClient, NodeOptions } from '@sentry/node';
44
import { getDefaultIntegrations as getDefaultNodeIntegrations, init as nodeInit, isInitialized } from '@sentry/node';
55
import { DEBUG_BUILD } from '../utils/debug-build';
66
import type { RemixOptions } from '../utils/remixOptions';
7-
import { instrumentServer } from './instrumentServer';
87
import { httpIntegration } from './integrations/http';
98
import { remixIntegration } from './integrations/opentelemetry';
9+
import { instrumentServer } from './patchServerRuntime';
1010

1111
/**
1212
* Returns the default Remix integrations.

0 commit comments

Comments
 (0)