8000 chore(remix): Add license text to vendored files (#7629) · bertho-zero/sentry-javascript@b068c68 · GitHub
[go: up one dir, main page]

Skip to content

Commit b068c68

Browse files
authored
chore(remix): Add license text to vendored files (getsentry#7629)
1 parent 32675e8 commit b068c68

File tree

6 files changed

+186
-109
lines changed

6 files changed

+186
-109
lines changed

packages/remix/src/utils/instrumentServer.ts

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,16 @@ import type {
2424
ReactRouterDomPkg,
2525
RemixRequest,
2626
RequestHandler,
27-
RouteMatch,
2827
ServerBuild,
2928
ServerRoute,
3029
ServerRouteManifest,
3130
} from './types';
31+
import { extractData, getRequestMatch, isResponse, json, matchServerRoutes } from './vendor/response';
3232
import { normalizeRemixRequest } from './web-fetch';
3333

3434
// Flag to track if the core request handler is instrumented.
3535
export let isRequestHandlerWrapped = false;
3636

37-
// Taken from Remix Implementation
38-
// https://github.com/remix-run/remix/blob/32300ec6e6e8025602cea63e17a2201989589eab/packages/remix-server-runtime/responses.ts#L60-L77
39-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40-
function isResponse(value: any): value is Response {
41-
return (
42-
value != null &&
43-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
44-
typeof value.status === 'number' &&
45-
typeof value.statusText === 'string' &&
46-
typeof value.headers === 'object' &&
47-
typeof value.body !== 'undefined'
48-
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
49-
);
50-
}
51-
5237
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
5338
function isRedirectResponse(response: Response): boolean {
5439
return redirectStatusCodes.has(response.status);
@@ -58,21 +43,6 @@ function isCatchResponse(response: Response): boolean {
5843
return response.headers.get('X-Remix-Catch') != null;
5944
}
6045

61-
// Based on Remix Implementation
62-
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145
63-
async function extractData(response: Response): Promise<unknown> {
64-
const contentType = response.headers.get('Content-Type');
65-
66-
// Cloning the response to avoid consuming the original body stream
67-
const responseClone = response.clone();
68-
69-
if (contentType && /\bapplication\/json\b/.test(contentType)) {
70-
return responseClone.json();
71-
}
72-
73-
return responseClone.text();
74-
}
75-
7646
async function extractResponseError(response: Response): Promise<unknown> {
7747
const responseData = await extractData(response);
7848

@@ -94,9 +64,11 @@ async function captureRemixServerException(err: Error, name: string, request: Re
9464
return;
9565
}
9666

67+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9768
let normalizedRequest: Record<string, unknown> = request as unknown as any;
9869

9970
try {
71+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10072
normalizedRequest = normalizeRemixRequest(request as unknown as any);
10173
} catch (e) {
10274
__DEBUG_BUILD__ && logger.warn('Failed to normalize Remix request');
@@ -245,29 +217,6 @@ function getTraceAndBaggage(): { sentryTrace?: string; sentryBaggage?: string }
245217
return {};
246218
}
247219

248-
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L1-L4
249-
export type JsonFunction = <Data>(data: Data, init?: number | ResponseInit) => Response;
250-
251-
/**
252-
* This is a shortcut for creating `application/json` responses. Converts `data`
253-
* to JSON and sets the `Content-Type` header.
254-
*
255-
* @see https://remix.run/api/remix#json
256-
*
257-
* https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L12-L24
258-
*/
259-
const json: JsonFunction = (data, init = {}) => {
260-
const responseInit = typeof init === 'number' ? { status: init } : init;
261-
const headers = new Headers(responseInit.headers);
262-
if (!headers.has('Content-Type')) {
263-
headers.set('Content-Type', 'application/json; charset=utf-8');
264-
}
265-
return new Response(JSON.stringify(data), {
266-
...responseInit,
267-
headers,
268-
});
269-
};
270-
271220
function makeWrappedRootLoader(origLoader: DataFunction): DataFunction {
272221
return async function (this: unknown, args: DataFunctionArgs): Promise<Response | AppData> {
273222
const res = await origLoader.call(this, args);
@@ -307,31 +256,6 @@ export function createRoutes(manifest: ServerRouteManifest, parentId?: string):
307256
}));
308257
}
309258

310-
// Remix Implementation:
311-
// https://github.com/remix-run/remix/blob/38e127b1d97485900b9c220d93503de0deb1fc81/packages/remix-server-runtime/routeMatching.ts#L12-L24
312-
//
313-
// Changed so that `matchRoutes` function is passed in.
314-
function matchServerRoutes(
315-
routes: ServerRoute[],
316-
pathname: string,
317-
pkg?: ReactRouterDomPkg,
318-
): RouteMatch<ServerRoute>[] | null {
319-
if (!pkg) {
320-
return null;
321-
}
322-
323-
const matches = pkg.matchRoutes(routes, pathname);
324-
if (!matches) {
325-
return null;
326-
}
327-
328-
return matches.map(match => ({
329-
params: match.params,
330-
pathname: match.pathname,
331-
route: match.route,
332-
}));
333-
}
334-
335259
/**
336260
* Starts a new transaction for the given request to be used by different `RequestHandler` wrappers.
337261
*
@@ -445,33 +369,6 @@ function wrapRequestHandler(origRequestHandler: RequestHandler, build: ServerBui
445369
};
446370
}
447371

448-
// https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L573-L586
449-
function isIndexRequestUrl(url: URL): boolean {
450-
for (const param of url.searchParams.getAll('index')) {
451-
// only use bare `?index` params without a value
452-
// ✅ /foo?index
453-
// ✅ /foo?index&index=123
454-
// ✅ /foo?index=123&index
455-
// ❌ /foo?index=123
456-
if (param === '') {
457-
return true;
458-
}
459-
}
460-
461-
return false;
462-
}
463-
464-
// https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L588-L596
465-
function getRequestMatch(url: URL, matches: RouteMatch<ServerRoute>[]): RouteMatch<ServerRoute> {
466-
const match = matches.slice(-1)[0];
467-
468-
if (!isIndexRequestUrl(url) && match.route.id.endsWith('/index')) {
469-
return matches.slice(-2)[0];
470-
}
471-
472-
return match;
473-
}
474-
475372
/**
476373
* Instruments `remix` ServerBuild for performance tracing and error tracking.
477374
*/

packages/remix/src/utils/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
/* eslint-disable max-lines */
23
/* eslint-disable @typescript-eslint/ban-types */
34
// Types vendored from @remix-run/server-runtime@1.6.0:
45
// https://github.com/remix-run/remix/blob/f3691d51027b93caa3fd2cdfe146d7b62a6eb8f2/packages/remix-server-runtime/server.ts
6+
// Copyright 2021 Remix Software Inc.
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11+
//
12+
// 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.
513
import type * as Express from 'express';
614
import type { Agent } from 'https';
715
import type { ComponentType } from 'react';

packages/remix/src/utils/getIpAddress.ts renamed to packages/remix/src/utils/vendor/getIpAddress.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
// Vendored / modified from @sergiodxa/remix-utils
2+
23
// https://github.com/sergiodxa/remix-utils/blob/02af80e12829a53696bfa8f3c2363975cf59f55e/src/server/get-client-ip-address.ts
4+
// MIT License
5+
6+
// Copyright (c) 2021 Sergio Xalambrí
7+
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
325

426
import { isIP } from 'net';
527

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2021 Remix Software Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
//
5+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
//
7+
// 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.
8+
9+
import type { ReactRouterDomPkg, RouteMatch, ServerRoute } from '../types';
10+
11+
/**
12+
* Based on Remix Implementation
13+
*
14+
* https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/data.ts#L131-L145
15+
*/
16+
export async function extractData(response: Response): Promise<unknown> {
17+
const contentType = response.headers.get('Content-Type');
18+
19+
// Cloning the response to avoid consuming the original body stream
20+
const responseClone = response.clone();
21+
22+
if (contentType && /\bapplication\/json\b/.test(contentType)) {
23+
return responseClone.json();
24+
}
25+
26+
return responseClone.text();
27+
}
28+
29+
/**
30+
* Taken from Remix Implementation
31+
*
32+
* https://github.com/remix-run/remix/blob/32300ec6e6e8025602cea63e17a2201989589eab/packages/remix-server-runtime/responses.ts#L60-L77
33+
*/
34+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
35+
export function isResponse(value: any): value is Response {
36+
return (
37+
value != null &&
38+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
39+
typeof value.status === 'number' &&
40+
typeof value.statusText === 'string' &&
41+
typeof value.headers === 'object' &&
42+
typeof value.body !== 'undefined'
43+
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
44+
);
45+
}
46+
47+
// https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L1-L4
48+
export type JsonFunction = <Data>(data: Data, init?: number | ResponseInit) => Response;
49+
50+
/**
51+
* This is a shortcut for creating `application/json` responses. Converts `data`
52+
* to JSON and sets the `Content-Type` header.
53+
*
54+
* @see https://remix.run/api/remix#json
55+
*
56+
* https://github.com/remix-run/remix/blob/7688da5c75190a2e29496c78721456d6e12e3abe/packages/remix-server-runtime/responses.ts#L12-L24
57+
*/
58+
export const json: JsonFunction = (data, init = {}) => {
59+
const responseInit = typeof init === 'number' ? { status: init } : init;
60+
const headers = new Headers(responseInit.headers);
61+
if (!headers.has('Content-Type')) {
62+
headers.set('Content-Type', 'application/json; charset=utf-8');
63+
}
64+
return new Response(JSON.stringify(data), {
65+
...responseInit,
66+
headers,
67+
});
68+
};
69+
70+
/**
71+
* Remix Implementation:
72+
* https://github.com/remix-run/remix/blob/38e127b1d97485900b9c220d93503de0deb1fc81/packages/remix-server-runtime/routeMatching.ts#L12-L24
73+
*
74+
* Changed so that `matchRoutes` function is passed in.
75+
*/
76+
export function matchServerRoutes(
77+
routes: ServerRoute[],
78+
pathname: string,
79+
pkg?: ReactRouterDomPkg,
80+
): RouteMatch<ServerRoute>[] | null {
81+
if (!pkg) {
82+
return null;
83+
}
84+
85+
const matches = pkg.matchRoutes(routes, pathname);
86+
if (!matches) {
87+
return null;
88+
}
89+
90+
return matches.map(match => ({
91+
params: match.params,
92+
pathname: match.pathname,
93+
route: match.route,
94+
}));
95+
}
96+
97+
/**
98+
* https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L573-L586
99+
*/
100+
export function isIndexRequestUrl(url: URL): boolean {
101+
for (const param of url.searchParams.getAll('index')) {
102+
// only use bare `?index` params without a value
103+
// ✅ /foo?index
104+
// ✅ /foo?index&index=123
105+
// ✅ /foo?index=123&index
106+
// ❌ /foo?index=123
107+
if (param === '') {
108+
return true;
109+
}
110+
}
111+
112+
return false;
113+
}
114+
115+
/**
116+
* https://github.com/remix-run/remix/blob/97999d02493e8114c39d48b76944069d58526e8d/packages/remix-server-runtime/server.ts#L588-L596
117+
*/
118+
export function getRequestMatch(url: URL, matches: RouteMatch<ServerRoute>[]): RouteMatch<ServerRoute> {
119+
const match = matches.slice(-1)[0];
120+
121+
if (!isIndexRequestUrl(url) && match.route.id.endsWith('/index')) {
122+
return matches.slice(-2)[0];
123+
}
124+
125+
return match;
126+
}

packages/remix/src/utils/web-fetch.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
// Based on Remix's implementation of Fetch API
2-
// https://github.com/remix-run/web-std-io/tree/main/packages/fetch
2+
// https://github.com/remix-run/web-std-io/blob/d2a003fe92096aaf97ab2a618b74875ccaadc280/packages/fetch/
3+
// The MIT License (MIT)
4+
5+
// Copyright (c) 2016 - 2020 Node Fetch Team
6+
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
324

425
import { logger } from '@sentry/utils';
526

6-
import { getClientIPAddress } from './getIpAddress';
727
import type { RemixRequest } from './types';
28+
import { getClientIPAddress } from './vendor/getIpAddress';
829

930
/*
1031
* Symbol extractor utility to be able to access internal fields of Remix requests.
@@ -17,7 +38,9 @@ const getInternalSymbols = (
1738
} => {
1839
const symbols = Object.getOwnPropertySymbols(request);
1940
return {
41+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2042
bodyInternalsSymbol: symbols.find(symbol => symbol.toString().includes('Body internals')) as any,
43+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2144
requestInternalsSymbol: symbols.find(symbol => symbol.toString().includes('Request internals')) as any,
2245
};
2346
};
@@ -42,6 +65,7 @@ export const getSearch = (parsedURL: URL): string => {
4265
* Vendored / modified from:
4366
* https://github.com/remix-run/web-std-io/blob/f715b354c8c5b8edc550c5442dec5712705e25e7/packages/fetch/src/request.js#L259
4467
*/
68+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4569
export const normalizeRemixRequest = (request: RemixRequest): Record<string, any> => {
4670
const { requestInternalsSymbol, bodyInternalsSymbol } = getInternalSymbols(request);
4771

packages/remix/test/utils/getIpAddress.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getClientIPAddress } from '../../src/utils/getIpAddress';
1+
import { getClientIPAddress } from '../../src/utils/vendor/getIpAddress';
22

33
class Headers {
44
private _headers: Record<string, string> = {};

0 commit comments

Comments
 (0)
0