Skip to content

Commit 2eb3749

Browse files
committed
fix(remix): Lighten vendored types
1 parent 3a81041 commit 2eb3749

File tree

8 files changed

+84
-268
lines changed

8 files changed

+84
-268
lines changed

packages/remix/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"devDependencies": {
7979
"@remix-run/node": "^2.15.2",
8080
"@remix-run/react": "^2.15.2",
81+
"@remix-run/server-runtime": "^2.15.2",
8182
"@types/express": "^4.17.14",
8283
"vite": "^5.4.11"
8384
},

packages/remix/src/server/errors.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
HandleDocumentRequestFunction,
55
LoaderFunctionArgs,
66
} from '@remix-run/node';
7+
import type { ActionFunction, LoaderFunction } from '@remix-run/server-runtime';
78
import type { RequestEventData, Span } from '@sentry/core';
89
import {
910
addExceptionMechanism,
@@ -18,7 +19,8 @@ import { DEBUG_BUILD } from '../utils/debug-build';
1819
import type { RemixOptions } from '../utils/remixOptions';
1920
import { storeFormDataKeys } from '../utils/utils';
2021
import { extractData, isResponse, isRouteErrorResponse } from '../utils/vendor/response';
21-
import type { DataFunction, RemixRequest } from '../utils/vendor/types';
22+
23+
type DataFunction = LoaderFunction | ActionFunction;
2224

2325
/**
2426
* Captures an exception happened in the Remix server.
@@ -87,7 +89,7 @@ export function errorHandleDocumentRequestFunction(
8789
this: unknown,
8890
origDocumentRequestFunction: HandleDocumentRequestFunction,
8991
requestContext: {
90-
request: RemixRequest;
92+
request: Request;
9193
responseStatusCode: number;
9294
responseHeaders: Headers;
9395
context: EntryContext;

packages/remix/src/server/instrumentServer.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
/* eslint-disable max-lines */
2+
import type {
3+
ActionFunction,
4+
ActionFunctionArgs,
5+
AppLoadContext,
6+
CreateRequestHandlerFunction,
7+
EntryContext,
8+
HandleDocumentRequestFunction,
9+
LoaderFunction,
10+
LoaderFunctionArgs,
11+
RequestHandler,
12+
ServerBuild,
13+
} from '@remix-run/server-runtime';
214
import type { RequestEventData, Span, TransactionSource, WrappedFunction } from '@sentry/core';
315
import {
416
continueTrace,
@@ -21,24 +33,14 @@ import {
2133
withIsolationScope,
2234
} from '@sentry/core';
2335
import { DEBUG_BUILD } from '../utils/debug-build';
36+
import type { DeferredData, Route, ServerRouteManifest } from '../utils/types';
2437
import { createRoutes, getTransactionName } from '../utils/utils';
2538
import { extractData, isDeferredData, isResponse, isRouteErrorResponse, json } from '../utils/vendor/response';
26-
import type {
27-
AppData,
28-
AppLoadContext,
29-
CreateRequestHandlerFunction,
30-
DataFunction,
31-
DataFunctionArgs,
32-
EntryContext,
33-
HandleDocumentRequestFunction,
34-
RemixRequest,
35-
RequestHandler,
36-
ServerBuild,
37-
ServerRoute,
38-
ServerRouteManifest,
39-
} from '../utils/vendor/types';
4039
import { captureRemixServerException, errorHandleDataFunction, errorHandleDocumentRequestFunction } from './errors';
4140

41+
type DataFunction = LoaderFunction | ActionFunction;
42+
type DataFunctionArgs = LoaderFunctionArgs | ActionFunctionArgs;
43+
4244
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
4345
function isRedirectResponse(response: Response): boolean {
4446
return redirectStatusCodes.has(response.status);
@@ -163,7 +165,7 @@ function makeWrappedDataFunction(
163165
name: 'action' | 'loader',
164166
instrumentTracing?: boolean,
165167
): DataFunction {
166-
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
168+
return async function (this: unknown, args: DataFunctionArgs): Promise<Response> {
167169
if (instrumentTracing) {
168170
return startSpan(
169171
{
@@ -198,7 +200,7 @@ const makeWrappedLoader =
198200

199201
function makeWrappedRootLoader() {
200202
return function (origLoader: DataFunction): DataFunction {
201-
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
203+
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | DeferredData> {
202204
const res = await origLoader.call(this, args);
203205
const traceAndBaggage = getTraceAndBaggage();
204206

@@ -254,14 +256,14 @@ function wrapRequestHandler(
254256
let name: string;
255257
let source: TransactionSource;
256258

257-
return async function (this: unknown, request: RemixRequest, loadContext?: AppLoadContext): Promise<Response> {
259+
return async function (this: unknown, request: Request, loadContext?: AppLoadContext): Promise<Response> {
258260
const upperCaseMethod = request.method.toUpperCase();
259261
// We don't want to wrap OPTIONS and HEAD requests
260262
if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') {
261263
return origRequestHandler.call(this, request, loadContext);
262264
}
263265

264-
let resolvedRoutes: ServerRoute[] | undefined;
266+
let resolvedRoutes: Route[] | undefined;
265267

266268
if (options?.instrumentTracing) {
267269
if (typeof build === 'function') {
@@ -428,7 +430,7 @@ export const makeWrappedCreateRequestHandler = (options?: { instrumentTracing?:
428430
function (origCreateRequestHandler: CreateRequestHandlerFunction): CreateRequestHandlerFunction {
429431
return function (
430432
this: unknown,
431-
build: ServerBuild | (() => Promise<ServerBuild>),
433+
build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>),
432434
...args: unknown[]
433435
): RequestHandler {
434436
const newBuild = instrumentBuild(build, options);

packages/remix/src/utils/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,29 @@ export type SentryMetaArgs<MetaFN extends (...args: any) => any> = Parameters<Me
44
sentryBaggage: string;
55
};
66
};
7+
8+
export type DeferredData = {
9+
data: Record<string, unknown>;
10+
deferredKeys: string[];
11+
subscribe(fn: (aborted: boolean, settledKey?: string) => void): () => boolean;
12+
cancel(): void;
13+
resolveData(signal: AbortSignal): Promise<boolean>;
14+
};
15+
16+
export interface RouteManifest<Route> {
17+
[routeId: string]: Route;
18+
}
19+
20+
export type ServerRouteManifest = RouteManifest<Omit<ServerRoute, 'children'>>;
21+
export interface ServerRoute extends Route {
22+
children: ServerRoute[];
23+
module: any;
24+
}
25+
26+
export interface Route {
27+
index?: boolean;
28+
caseSensitive?: boolean;
29+
id: string;
30+
parentId?: string;
31+
path?: string;
32+
}

packages/remix/src/utils/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node';
22
import type { Span, TransactionSource } from '@sentry/core';
33
import { logger } from '@sentry/core';
44
import { DEBUG_BUILD } from './debug-build';
5+
import type { Route, ServerRouteManifest } from './types';
56
import { getRequestMatch, matchServerRoutes } from './vendor/response';
6-
import type { ServerRoute, ServerRouteManifest } from './vendor/types';
77

88
/**
99
*
@@ -29,7 +29,7 @@ export async function storeFormDataKeys(args: LoaderFunctionArgs | ActionFunctio
2929
/**
3030
* Get transaction name from routes and url
3131
*/
32-
export function getTransactionName(routes: ServerRoute[], url: URL): [string, TransactionSource] {
32+
export function getTransactionName(routes: Route[], url: URL): [string, TransactionSource] {
3333
const matches = matchServerRoutes(routes, url.pathname);
3434
const match = matches && getRequestMatch(url, matches);
3535
return match === null ? [url.pathname, 'url'] : [match.route.id || 'no-route-id', 'route'];
@@ -41,7 +41,7 @@ export function getTransactionName(routes: ServerRoute[], url: URL): [string, Tr
4141
* @param manifest
4242
* @param parentId
4343
*/
44-
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): ServerRoute[] {
44+
export function createRoutes(manifest: ServerRouteManifest, parentId?: string): Route[] {
4545
return Object.entries(manifest)
4646
.filter(([, route]) => route.parentId === parentId)
4747
.map(([id, route]) => ({

packages/remix/src/utils/vendor/response.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88

9-
import type { AgnosticRouteMatch, AgnosticRouteObject } from '@remix-run/router';
9+
import type { AgnosticRouteMatch, AgnosticRouteObject, ErrorResponse } from '@remix-run/router';
1010
import { matchRoutes } from '@remix-run/router';
11-
import type { DeferredData, ErrorResponse, ServerRoute } from './types';
11+
import type { DeferredData, Route } from '../types';
1212

1313
/**
1414
* Based on Remix Implementation
@@ -76,7 +76,7 @@ export const json: JsonFunction = (data, init = {}) => {
7676
* Changed so that `matchRoutes` function is passed in.
7777
*/
7878
export function matchServerRoutes(
79-
routes: ServerRoute[],
79+
routes: Route[],
8080
pathname: string,
8181
): AgnosticRouteMatch<string, AgnosticRouteObject>[] | null {
8282
const matches = matchRoutes(routes, pathname);
@@ -151,7 +151,9 @@ export function isDeferredData(value: any): value is DeferredData {
151151
*/
152152
// eslint-disable-next-line @typescript-eslint/no-explicit-any
153153
export function isRouteErrorResponse(value: any): value is ErrorResponse {
154-
const error: ErrorResponse = value;
154+
const error: ErrorResponse & {
155+
internal: boolean;
156+
} = value;
155157

156158
return (
157159
error != null &&

0 commit comments

Comments
 (0)