@remix-run/server-runtime
Advanced tools
Comparing version 0.0.0-nightly-3874120-20231103 to 0.0.0-nightly-38ca648a3-20240920
@@ -1,2 +0,2 @@ | ||
import type { DataFunctionArgs } from "./routeModules"; | ||
import type { ActionFunctionArgs, LoaderFunctionArgs } from "./routeModules"; | ||
import type { AssetsManifest, EntryContext, FutureConfig } from "./entry"; | ||
@@ -15,5 +15,7 @@ import type { ServerRouteManifest } from "./routes"; | ||
assets: AssetsManifest; | ||
basename?: string; | ||
publicPath: string; | ||
assetsBuildDirectory: string; | ||
future: FutureConfig; | ||
isSpaMode: boolean; | ||
} | ||
@@ -24,6 +26,6 @@ export interface HandleDocumentRequestFunction { | ||
export interface HandleDataRequestFunction { | ||
(response: Response, args: DataFunctionArgs): Promise<Response> | Response; | ||
(response: Response, args: LoaderFunctionArgs | ActionFunctionArgs): Promise<Response> | Response; | ||
} | ||
export interface HandleErrorFunction { | ||
(error: unknown, args: DataFunctionArgs): void; | ||
(error: unknown, args: LoaderFunctionArgs | ActionFunctionArgs): void; | ||
} | ||
@@ -38,2 +40,3 @@ /** | ||
handleError?: HandleErrorFunction; | ||
streamTimeout?: number; | ||
} |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
@@ -1,2 +0,2 @@ | ||
import type { ActionFunction, DataFunctionArgs, LoaderFunction } from "./routeModules"; | ||
import type { ActionFunction, ActionFunctionArgs, LoaderFunction, LoaderFunctionArgs } from "./routeModules"; | ||
/** | ||
@@ -15,15 +15,17 @@ * An object of unknown type for route loaders and actions provided by the | ||
export type AppData = unknown; | ||
export declare function callRouteActionRR({ loadContext, action, params, request, routeId, }: { | ||
export declare function callRouteAction({ loadContext, action, params, request, routeId, singleFetch, }: { | ||
request: Request; | ||
action: ActionFunction; | ||
params: DataFunctionArgs["params"]; | ||
params: ActionFunctionArgs["params"]; | ||
loadContext: AppLoadContext; | ||
routeId: string; | ||
}): Promise<Response>; | ||
export declare function callRouteLoaderRR({ loadContext, loader, params, request, routeId, }: { | ||
singleFetch: boolean; | ||
}): Promise<{} | Response | null>; | ||
export declare function callRouteLoader({ loadContext, loader, params, request, routeId, singleFetch, }: { | ||
request: Request; | ||
loader: LoaderFunction; | ||
params: DataFunctionArgs["params"]; | ||
params: LoaderFunctionArgs["params"]; | ||
loadContext: AppLoadContext; | ||
routeId: string; | ||
}): Promise<import("@remix-run/router").UNSAFE_DeferredData | Response>; | ||
singleFetch: boolean; | ||
}): Promise<{} | Response | null>; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -28,3 +28,3 @@ * Copyright (c) Remix Software Inc. | ||
async function callRouteActionRR({ | ||
async function callRouteAction({ | ||
loadContext, | ||
@@ -34,6 +34,7 @@ action, | ||
request, | ||
routeId | ||
routeId, | ||
singleFetch | ||
}) { | ||
let result = await action({ | ||
request: stripDataParam(stripIndexParam(request)), | ||
request: singleFetch ? stripRoutesParam(stripIndexParam(request)) : stripDataParam(stripIndexParam(request)), | ||
context: loadContext, | ||
@@ -45,5 +46,10 @@ params | ||
} | ||
// Allow naked object returns when single fetch is enabled | ||
if (singleFetch) { | ||
return result; | ||
} | ||
return responses.isResponse(result) ? result : responses.json(result); | ||
} | ||
async function callRouteLoaderRR({ | ||
async function callRouteLoader({ | ||
loadContext, | ||
@@ -53,6 +59,7 @@ loader, | ||
request, | ||
routeId | ||
routeId, | ||
singleFetch | ||
}) { | ||
let result = await loader({ | ||
request: stripDataParam(stripIndexParam(request)), | ||
request: singleFetch ? stripRoutesParam(stripIndexParam(request)) : stripDataParam(stripIndexParam(request)), | ||
context: loadContext, | ||
@@ -70,2 +77,7 @@ params | ||
} | ||
// Allow naked object returns when single fetch is enabled | ||
if (singleFetch) { | ||
return result; | ||
} | ||
return responses.isResponse(result) ? result : responses.json(result); | ||
@@ -117,4 +129,18 @@ } | ||
} | ||
function stripRoutesParam(request) { | ||
let url = new URL(request.url); | ||
url.searchParams.delete("_routes"); | ||
let init = { | ||
method: request.method, | ||
body: request.body, | ||
headers: request.headers, | ||
signal: request.signal | ||
}; | ||
if (init.body) { | ||
init.duplex = "half"; | ||
} | ||
return new Request(url.href, init); | ||
} | ||
exports.callRouteActionRR = callRouteActionRR; | ||
exports.callRouteLoaderRR = callRouteLoaderRR; | ||
exports.callRouteAction = callRouteAction; | ||
exports.callRouteLoader = callRouteLoader; |
import type { ServerBuild } from "./build"; | ||
export declare function broadcastDevReady(build: ServerBuild, origin?: string): Promise<void>; | ||
export declare function logDevReady(build: ServerBuild): void; | ||
type DevServerHooks = { | ||
getCriticalCss?: (build: ServerBuild, pathname: string) => Promise<string | undefined>; | ||
processRequestError?: (error: unknown) => void; | ||
}; | ||
export declare function setDevServerHooks(devServerHooks: DevServerHooks): void; | ||
export declare function getDevServerHooks(): DevServerHooks | undefined; | ||
export {}; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -40,4 +40,15 @@ * Copyright (c) Remix Software Inc. | ||
} | ||
const globalDevServerHooksKey = "__remix_devServerHooks"; | ||
function setDevServerHooks(devServerHooks) { | ||
// @ts-expect-error | ||
globalThis[globalDevServerHooksKey] = devServerHooks; | ||
} | ||
function getDevServerHooks() { | ||
// @ts-expect-error | ||
return globalThis[globalDevServerHooksKey]; | ||
} | ||
exports.broadcastDevReady = broadcastDevReady; | ||
exports.getDevServerHooks = getDevServerHooks; | ||
exports.logDevReady = logDevReady; | ||
exports.setDevServerHooks = setDevServerHooks; |
@@ -10,4 +10,16 @@ import type { StaticHandlerContext } from "@remix-run/router"; | ||
serverHandoffString?: string; | ||
serverHandoffStream?: ReadableStream<Uint8Array>; | ||
renderMeta?: { | ||
didRenderScripts?: boolean; | ||
streamCache?: Record<number, Promise<void> & { | ||
result?: { | ||
done: boolean; | ||
value: string; | ||
}; | ||
error?: unknown; | ||
}>; | ||
}; | ||
staticHandlerContext: StaticHandlerContext; | ||
future: FutureConfig; | ||
isSpaMode: boolean; | ||
serializeError(error: Error): SerializedError; | ||
@@ -17,2 +29,6 @@ } | ||
v3_fetcherPersist: boolean; | ||
v3_relativeSplatPath: boolean; | ||
v3_throwAbortReason: boolean; | ||
unstable_lazyRouteDiscovery: boolean; | ||
unstable_singleFetch: boolean; | ||
} | ||
@@ -19,0 +35,0 @@ export interface AssetsManifest { |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -78,2 +78,3 @@ * Copyright (c) Remix Software Inc. | ||
// https://github.com/microsoft/TypeScript/issues/15300 | ||
function serializeError(error, serverMode) { | ||
@@ -80,0 +81,0 @@ let sanitized = sanitizeError(error, serverMode); |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -24,3 +24,3 @@ * Copyright (c) Remix Software Inc. | ||
async function callRouteActionRR({ | ||
async function callRouteAction({ | ||
loadContext, | ||
@@ -30,6 +30,7 @@ action, | ||
request, | ||
routeId | ||
routeId, | ||
singleFetch | ||
}) { | ||
let result = await action({ | ||
request: stripDataParam(stripIndexParam(request)), | ||
request: singleFetch ? stripRoutesParam(stripIndexParam(request)) : stripDataParam(stripIndexParam(request)), | ||
context: loadContext, | ||
@@ -41,5 +42,10 @@ params | ||
} | ||
// Allow naked object returns when single fetch is enabled | ||
if (singleFetch) { | ||
return result; | ||
} | ||
return isResponse(result) ? result : json(result); | ||
} | ||
async function callRouteLoaderRR({ | ||
async function callRouteLoader({ | ||
loadContext, | ||
@@ -49,6 +55,7 @@ loader, | ||
request, | ||
routeId | ||
routeId, | ||
singleFetch | ||
}) { | ||
let result = await loader({ | ||
request: stripDataParam(stripIndexParam(request)), | ||
request: singleFetch ? stripRoutesParam(stripIndexParam(request)) : stripDataParam(stripIndexParam(request)), | ||
context: loadContext, | ||
@@ -66,2 +73,7 @@ params | ||
} | ||
// Allow naked object returns when single fetch is enabled | ||
if (singleFetch) { | ||
return result; | ||
} | ||
return isResponse(result) ? result : json(result); | ||
@@ -113,3 +125,17 @@ } | ||
} | ||
function stripRoutesParam(request) { | ||
let url = new URL(request.url); | ||
url.searchParams.delete("_routes"); | ||
let init = { | ||
method: request.method, | ||
body: request.body, | ||
headers: request.headers, | ||
signal: request.signal | ||
}; | ||
if (init.body) { | ||
init.duplex = "half"; | ||
} | ||
return new Request(url.href, init); | ||
} | ||
export { callRouteActionRR, callRouteLoaderRR }; | ||
export { callRouteAction, callRouteLoader }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -36,3 +36,12 @@ * Copyright (c) Remix Software Inc. | ||
} | ||
const globalDevServerHooksKey = "__remix_devServerHooks"; | ||
function setDevServerHooks(devServerHooks) { | ||
// @ts-expect-error | ||
globalThis[globalDevServerHooksKey] = devServerHooks; | ||
} | ||
function getDevServerHooks() { | ||
// @ts-expect-error | ||
return globalThis[globalDevServerHooksKey]; | ||
} | ||
export { broadcastDevReady, logDevReady }; | ||
export { broadcastDevReady, getDevServerHooks, logDevReady, setDevServerHooks }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -74,2 +74,3 @@ * Copyright (c) Remix Software Inc. | ||
// https://github.com/microsoft/TypeScript/issues/15300 | ||
function serializeError(error, serverMode) { | ||
@@ -76,0 +77,0 @@ let sanitized = sanitizeError(error, serverMode); |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -13,3 +13,3 @@ * Copyright (c) Remix Software Inc. | ||
function getDocumentHeadersRR(build, context) { | ||
function getDocumentHeaders(build, context) { | ||
let boundaryIdx = context.errors ? context.matches.findIndex(m => context.errors[m.route.id]) : -1; | ||
@@ -91,2 +91,2 @@ let matches = boundaryIdx >= 0 ? context.matches.slice(0, boundaryIdx + 1) : context.matches; | ||
export { getDocumentHeadersRR }; | ||
export { getDocumentHeaders }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -13,3 +13,4 @@ * Copyright (c) Remix Software Inc. | ||
export { composeUploadHandlers as unstable_composeUploadHandlers, parseMultipartFormData as unstable_parseMultipartFormData } from './formData.js'; | ||
export { defer, json, redirect, redirectDocument } from './responses.js'; | ||
export { defer, json, redirect, redirectDocument, replace } from './responses.js'; | ||
export { SingleFetchRedirectSymbol as UNSAFE_SingleFetchRedirectSymbol, data as unstable_data } from './single-fetch.js'; | ||
export { createRequestHandler } from './server.js'; | ||
@@ -21,2 +22,2 @@ export { createSession, createSessionStorageFactory, isSession } from './sessions.js'; | ||
export { MaxPartSizeExceededError } from './upload/errors.js'; | ||
export { broadcastDevReady, logDevReady } from './dev.js'; | ||
export { broadcastDevReady, logDevReady, setDevServerHooks as unstable_setDevServerHooks } from './dev.js'; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -11,3 +11,3 @@ * Copyright (c) Remix Software Inc. | ||
*/ | ||
import { json as json$1, defer as defer$1, redirect as redirect$1, redirectDocument as redirectDocument$1 } from '@remix-run/router'; | ||
import { json as json$1, defer as defer$1, redirect as redirect$1, replace as replace$1, redirectDocument as redirectDocument$1 } from '@remix-run/router'; | ||
import { serializeError } from './errors.js'; | ||
@@ -17,2 +17,3 @@ | ||
// interfaces must conform to the types they extend | ||
/** | ||
@@ -47,2 +48,12 @@ * This is a shortcut for creating `application/json` responses. Converts `data` | ||
/** | ||
* A redirect response. Sets the status code and the `Location` header. | ||
* Defaults to "302 Found". | ||
* | ||
* @see https://remix.run/utils/redirect | ||
*/ | ||
const replace = (url, init = 302) => { | ||
return replace$1(url, init); | ||
}; | ||
/** | ||
* A redirect response that will force a document reload to the new location. | ||
@@ -125,2 +136,2 @@ * Sets the status code and the `Location` header. | ||
export { createDeferredReadableStream, defer, isDeferredData, isRedirectResponse, isRedirectStatusCode, isResponse, json, redirect, redirectDocument }; | ||
export { createDeferredReadableStream, defer, isDeferredData, isRedirectResponse, isRedirectStatusCode, isResponse, json, redirect, redirectDocument, replace }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -13,4 +13,4 @@ * Copyright (c) Remix Software Inc. | ||
function matchServerRoutes(routes, pathname) { | ||
let matches = matchRoutes(routes, pathname); | ||
function matchServerRoutes(routes, pathname, basename) { | ||
let matches = matchRoutes(routes, pathname, basename); | ||
if (!matches) return null; | ||
@@ -17,0 +17,0 @@ return matches.map(match => ({ |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -11,3 +11,3 @@ * Copyright (c) Remix Software Inc. | ||
*/ | ||
import { callRouteLoaderRR, callRouteActionRR } from './data.js'; | ||
import { callRouteLoader, callRouteAction } from './data.js'; | ||
@@ -51,3 +51,3 @@ // NOTE: make sure to change the Route in remix-react if you change this | ||
// though we know it'll always be provided in remix | ||
args => callRouteLoaderRR({ | ||
(args, dataStrategyCtx) => callRouteLoader({ | ||
request: args.request, | ||
@@ -57,5 +57,6 @@ params: args.params, | ||
loader: route.module.loader, | ||
routeId: route.id | ||
routeId: route.id, | ||
singleFetch: future.unstable_singleFetch === true | ||
}) : undefined, | ||
action: route.module.action ? args => callRouteActionRR({ | ||
action: route.module.action ? (args, dataStrategyCtx) => callRouteAction({ | ||
request: args.request, | ||
@@ -65,3 +66,4 @@ params: args.params, | ||
action: route.module.action, | ||
routeId: route.id | ||
routeId: route.id, | ||
singleFetch: future.unstable_singleFetch === true | ||
}) : undefined, | ||
@@ -68,0 +70,0 @@ handle: route.module.handle |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -11,6 +11,6 @@ * Copyright (c) Remix Software Inc. | ||
*/ | ||
import { UNSAFE_DEFERRED_SYMBOL, isRouteErrorResponse, json, getStaticContextFromError, createStaticHandler } from '@remix-run/router'; | ||
import { UNSAFE_DEFERRED_SYMBOL, isRouteErrorResponse, json as json$1, UNSAFE_ErrorResponseImpl, getStaticContextFromError, stripBasename, createStaticHandler } from '@remix-run/router'; | ||
import { createEntryRouteModules } from './entry.js'; | ||
import { serializeError, sanitizeErrors, serializeErrors } from './errors.js'; | ||
import { getDocumentHeadersRR } from './headers.js'; | ||
import { getDocumentHeaders } from './headers.js'; | ||
import invariant from './invariant.js'; | ||
@@ -20,10 +20,20 @@ import { ServerMode, isServerMode } from './mode.js'; | ||
import { createRoutes, createStaticHandlerDataRoutes } from './routes.js'; | ||
import { isRedirectResponse, createDeferredReadableStream, isResponse } from './responses.js'; | ||
import { isRedirectResponse, json, createDeferredReadableStream, isResponse } from './responses.js'; | ||
import { createServerHandoffString } from './serverHandoff.js'; | ||
import { getDevServerHooks } from './dev.js'; | ||
import { getSingleFetchRedirect, encodeViaTurboStream, SINGLE_FETCH_REDIRECT_STATUS, singleFetchAction, singleFetchLoaders, SingleFetchRedirectSymbol } from './single-fetch.js'; | ||
import { resourceRouteJsonWarning } from './deprecations.js'; | ||
function derive(build, mode) { | ||
var _build$future, _build$future2; | ||
let routes = createRoutes(build.routes); | ||
let dataRoutes = createStaticHandlerDataRoutes(build.routes, build.future); | ||
let serverMode = isServerMode(mode) ? mode : ServerMode.Production; | ||
let staticHandler = createStaticHandler(dataRoutes); | ||
let staticHandler = createStaticHandler(dataRoutes, { | ||
basename: build.basename, | ||
future: { | ||
v7_relativeSplatPath: ((_build$future = build.future) === null || _build$future === void 0 ? void 0 : _build$future.v3_relativeSplatPath) === true, | ||
v7_throwAbortReason: ((_build$future2 = build.future) === null || _build$future2 === void 0 ? void 0 : _build$future2.v3_throwAbortReason) === true | ||
} | ||
}); | ||
let errorHandler = build.entry.module.handleError || ((error, { | ||
@@ -52,6 +62,5 @@ request | ||
let errorHandler; | ||
return async function requestHandler(request, loadContext = {}, { | ||
__criticalCss: criticalCss | ||
} = {}) { | ||
return async function requestHandler(request, loadContext = {}) { | ||
_build = typeof build === "function" ? await build() : build; | ||
mode ??= _build.mode; | ||
if (typeof build === "function") { | ||
@@ -71,24 +80,82 @@ let derived = derive(_build, mode); | ||
let url = new URL(request.url); | ||
let matches = matchServerRoutes(routes, url.pathname); | ||
let handleError = error => errorHandler(error, { | ||
context: loadContext, | ||
params: matches && matches.length > 0 ? matches[0].params : {}, | ||
request | ||
}); | ||
let params = {}; | ||
let handleError = error => { | ||
if (mode === ServerMode.Development) { | ||
var _getDevServerHooks, _getDevServerHooks$pr; | ||
(_getDevServerHooks = getDevServerHooks()) === null || _getDevServerHooks === void 0 ? void 0 : (_getDevServerHooks$pr = _getDevServerHooks.processRequestError) === null || _getDevServerHooks$pr === void 0 ? void 0 : _getDevServerHooks$pr.call(_getDevServerHooks, error); | ||
} | ||
errorHandler(error, { | ||
context: loadContext, | ||
params, | ||
request | ||
}); | ||
}; | ||
// Manifest request for fog of war | ||
let manifestUrl = `${_build.basename ?? "/"}/__manifest`.replace(/\/+/g, "/"); | ||
if (url.pathname === manifestUrl) { | ||
try { | ||
let res = await handleManifestRequest(_build, routes, url); | ||
return res; | ||
} catch (e) { | ||
handleError(e); | ||
return new Response("Unknown Server Error", { | ||
status: 500 | ||
}); | ||
} | ||
} | ||
let matches = matchServerRoutes(routes, url.pathname, _build.basename); | ||
if (matches && matches.length > 0) { | ||
Object.assign(params, matches[0].params); | ||
} | ||
let response; | ||
if (url.searchParams.has("_data")) { | ||
if (_build.future.unstable_singleFetch) { | ||
handleError(new Error("Warning: Single fetch-enabled apps should not be making ?_data requests, " + "this is likely to break in the future")); | ||
} | ||
let routeId = url.searchParams.get("_data"); | ||
response = await handleDataRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError); | ||
response = await handleDataRequest(serverMode, _build, staticHandler, routeId, request, loadContext, handleError); | ||
if (_build.entry.module.handleDataRequest) { | ||
var _matches$find; | ||
response = await _build.entry.module.handleDataRequest(response, { | ||
context: loadContext, | ||
params: (matches === null || matches === void 0 ? void 0 : (_matches$find = matches.find(m => m.route.id == routeId)) === null || _matches$find === void 0 ? void 0 : _matches$find.params) || {}, | ||
params, | ||
request | ||
}); | ||
if (isRedirectResponse(response)) { | ||
response = createRemixRedirectResponse(response, _build.basename); | ||
} | ||
} | ||
} else if (_build.future.unstable_singleFetch && url.pathname.endsWith(".data")) { | ||
let handlerUrl = new URL(request.url); | ||
handlerUrl.pathname = handlerUrl.pathname.replace(/\.data$/, "").replace(/^\/_root$/, "/"); | ||
let singleFetchMatches = matchServerRoutes(routes, handlerUrl.pathname, _build.basename); | ||
response = await handleSingleFetchRequest(serverMode, _build, staticHandler, request, handlerUrl, loadContext, handleError); | ||
if (_build.entry.module.handleDataRequest) { | ||
response = await _build.entry.module.handleDataRequest(response, { | ||
context: loadContext, | ||
params: singleFetchMatches ? singleFetchMatches[0].params : {}, | ||
request | ||
}); | ||
if (isRedirectResponse(response)) { | ||
let result = getSingleFetchRedirect(response.status, response.headers, _build.basename); | ||
if (request.method === "GET") { | ||
result = { | ||
[SingleFetchRedirectSymbol]: result | ||
}; | ||
} | ||
let headers = new Headers(response.headers); | ||
headers.set("Content-Type", "text/x-script"); | ||
return new Response(encodeViaTurboStream(result, request.signal, _build.entry.module.streamTimeout, serverMode), { | ||
status: SINGLE_FETCH_REDIRECT_STATUS, | ||
headers | ||
}); | ||
} | ||
} | ||
} else if (matches && matches[matches.length - 1].route.module.default == null && matches[matches.length - 1].route.module.ErrorBoundary == null) { | ||
response = await handleResourceRequestRR(serverMode, staticHandler, matches.slice(-1)[0].route.id, request, loadContext, handleError); | ||
response = await handleResourceRequest(serverMode, _build, staticHandler, matches.slice(-1)[0].route.id, request, loadContext, handleError); | ||
} else { | ||
response = await handleDocumentRequestRR(serverMode, _build, staticHandler, request, loadContext, handleError, criticalCss); | ||
var _getDevServerHooks2, _getDevServerHooks2$g; | ||
let criticalCss = mode === ServerMode.Development ? await ((_getDevServerHooks2 = getDevServerHooks()) === null || _getDevServerHooks2 === void 0 ? void 0 : (_getDevServerHooks2$g = _getDevServerHooks2.getCriticalCss) === null || _getDevServerHooks2$g === void 0 ? void 0 : _getDevServerHooks2$g.call(_getDevServerHooks2, _build, url.pathname)) : undefined; | ||
response = await handleDocumentRequest(serverMode, _build, staticHandler, request, loadContext, handleError, criticalCss); | ||
} | ||
@@ -105,3 +172,25 @@ if (request.method === "HEAD") { | ||
}; | ||
async function handleDataRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError) { | ||
async function handleManifestRequest(build, routes, url) { | ||
let patches = {}; | ||
if (url.searchParams.has("p")) { | ||
for (let path of url.searchParams.getAll("p")) { | ||
let matches = matchServerRoutes(routes, path, build.basename); | ||
if (matches) { | ||
for (let match of matches) { | ||
let routeId = match.route.id; | ||
patches[routeId] = build.assets.routes[routeId]; | ||
} | ||
} | ||
} | ||
return json(patches, { | ||
headers: { | ||
"Cache-Control": "public, max-age=31536000, immutable" | ||
} | ||
}); // Override the TypedResponse stuff from json() | ||
} | ||
return new Response("Invalid Request", { | ||
status: 400 | ||
}); | ||
} | ||
async function handleDataRequest(serverMode, build, staticHandler, routeId, request, loadContext, handleError) { | ||
try { | ||
@@ -113,16 +202,3 @@ let response = await staticHandler.queryRoute(request, { | ||
if (isRedirectResponse(response)) { | ||
// We don't have any way to prevent a fetch request from following | ||
// redirects. So we use the `X-Remix-Redirect` header to indicate the | ||
// next URL, and then "follow" the redirect manually on the client. | ||
let headers = new Headers(response.headers); | ||
headers.set("X-Remix-Redirect", headers.get("Location")); | ||
headers.set("X-Remix-Status", response.status); | ||
headers.delete("Location"); | ||
if (response.headers.get("Set-Cookie") !== null) { | ||
headers.set("X-Remix-Revalidate", "yes"); | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
headers | ||
}); | ||
return createRemixRedirectResponse(response, build.basename); | ||
} | ||
@@ -144,18 +220,16 @@ if (UNSAFE_DEFERRED_SYMBOL in response) { | ||
// network errors that are missing this header | ||
response.headers.set("X-Remix-Response", "yes"); | ||
response = safelySetHeader(response, "X-Remix-Response", "yes"); | ||
return response; | ||
} catch (error) { | ||
if (isResponse(error)) { | ||
error.headers.set("X-Remix-Catch", "yes"); | ||
return error; | ||
let response = safelySetHeader(error, "X-Remix-Catch", "yes"); | ||
return response; | ||
} | ||
if (isRouteErrorResponse(error)) { | ||
if (error) { | ||
handleError(error); | ||
} | ||
handleError(error); | ||
return errorResponseToJson(error, serverMode); | ||
} | ||
let errorInstance = error instanceof Error ? error : new Error("Unexpected Server Error"); | ||
let errorInstance = error instanceof Error || error instanceof DOMException ? error : new Error("Unexpected Server Error"); | ||
handleError(errorInstance); | ||
return json(serializeError(errorInstance, serverMode), { | ||
return json$1(serializeError(errorInstance, serverMode), { | ||
status: 500, | ||
@@ -168,3 +242,36 @@ headers: { | ||
} | ||
async function handleDocumentRequestRR(serverMode, build, staticHandler, request, loadContext, handleError, criticalCss) { | ||
async function handleSingleFetchRequest(serverMode, build, staticHandler, request, handlerUrl, loadContext, handleError) { | ||
let { | ||
result, | ||
headers, | ||
status | ||
} = request.method !== "GET" ? await singleFetchAction(build, serverMode, staticHandler, request, handlerUrl, loadContext, handleError) : await singleFetchLoaders(build, serverMode, staticHandler, request, handlerUrl, loadContext, handleError); | ||
// Mark all successful responses with a header so we can identify in-flight | ||
// network errors that are missing this header | ||
let resultHeaders = new Headers(headers); | ||
resultHeaders.set("X-Remix-Response", "yes"); | ||
// 304 responses should not have a body | ||
if (status === 304) { | ||
return new Response(null, { | ||
status: 304, | ||
headers: resultHeaders | ||
}); | ||
} | ||
// We use a less-descriptive `text/x-script` here instead of something like | ||
// `text/x-turbo` to enable compression when deployed via Cloudflare. See: | ||
// - https://github.com/remix-run/remix/issues/9884 | ||
// - https://developers.cloudflare.com/speed/optimization/content/brotli/content-compression/ | ||
resultHeaders.set("Content-Type", "text/x-script"); | ||
// Note: Deferred data is already just Promises, so we don't have to mess | ||
// `activeDeferreds` or anything :) | ||
return new Response(encodeViaTurboStream(result, request.signal, build.entry.module.streamTimeout, serverMode), { | ||
status: status || 200, | ||
headers: resultHeaders | ||
}); | ||
} | ||
async function handleDocumentRequest(serverMode, build, staticHandler, request, loadContext, handleError, criticalCss) { | ||
let context; | ||
@@ -184,7 +291,16 @@ try { | ||
} | ||
let headers = getDocumentHeaders(build, context); | ||
// 304 responses should not have a body or a content-type | ||
if (context.statusCode === 304) { | ||
return new Response(null, { | ||
status: 304, | ||
headers | ||
}); | ||
} | ||
// Sanitize errors outside of development environments | ||
if (context.errors) { | ||
Object.values(context.errors).forEach(err => { | ||
// @ts-expect-error This is "private" from users but intended for internal use | ||
// @ts-expect-error `err.error` is "private" from users but intended for internal use | ||
if (!isRouteErrorResponse(err) || err.error) { | ||
@@ -196,3 +312,11 @@ handleError(err); | ||
} | ||
let headers = getDocumentHeadersRR(build, context); | ||
// Server UI state to send to the client. | ||
// - When single fetch is enabled, this is streamed down via `serverHandoffStream` | ||
// - Otherwise it's stringified into `serverHandoffString` | ||
let state = { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: serializeErrors(context.errors, serverMode) | ||
}; | ||
let entryContext = { | ||
@@ -204,12 +328,16 @@ manifest: build.assets, | ||
serverHandoffString: createServerHandoffString({ | ||
url: context.location.pathname, | ||
basename: build.basename, | ||
criticalCss, | ||
state: { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: serializeErrors(context.errors, serverMode) | ||
}, | ||
future: build.future | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
...(!build.future.unstable_singleFetch ? { | ||
state | ||
} : null) | ||
}), | ||
...(build.future.unstable_singleFetch ? { | ||
serverHandoffStream: encodeViaTurboStream(state, request.signal, build.entry.module.streamTimeout, serverMode), | ||
renderMeta: {} | ||
} : null), | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
serializeError: err => serializeError(err, serverMode) | ||
@@ -222,5 +350,17 @@ }; | ||
handleError(error); | ||
let errorForSecondRender = error; | ||
// If they threw a response, unwrap it into an ErrorResponse like we would | ||
// have for a loader/action | ||
if (isResponse(error)) { | ||
try { | ||
let data = await unwrapResponse(error); | ||
errorForSecondRender = new UNSAFE_ErrorResponseImpl(error.status, error.statusText, data); | ||
} catch (e) { | ||
// If we can't unwrap the response - just leave it as-is | ||
} | ||
} | ||
// Get a new StaticHandlerContext that contains the error at the right boundary | ||
context = getStaticContextFromError(staticHandler.dataRoutes, context, error); | ||
context = getStaticContextFromError(staticHandler.dataRoutes, context, errorForSecondRender); | ||
@@ -232,3 +372,11 @@ // Sanitize errors outside of development environments | ||
// Update entryContext for the second render pass | ||
// Get a new entryContext for the second render pass | ||
// Server UI state to send to the client. | ||
// - When single fetch is enabled, this is streamed down via `serverHandoffStream` | ||
// - Otherwise it's stringified into `serverHandoffString` | ||
let state = { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: serializeErrors(context.errors, serverMode) | ||
}; | ||
entryContext = { | ||
@@ -238,10 +386,13 @@ ...entryContext, | ||
serverHandoffString: createServerHandoffString({ | ||
url: context.location.pathname, | ||
state: { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: serializeErrors(context.errors, serverMode) | ||
}, | ||
future: build.future | ||
}) | ||
basename: build.basename, | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
...(!build.future.unstable_singleFetch ? { | ||
state | ||
} : null) | ||
}), | ||
...(build.future.unstable_singleFetch ? { | ||
serverHandoffStream: encodeViaTurboStream(state, request.signal, build.entry.module.streamTimeout, serverMode), | ||
renderMeta: {} | ||
} : null) | ||
}; | ||
@@ -256,3 +407,3 @@ try { | ||
} | ||
async function handleResourceRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError) { | ||
async function handleResourceRequest(serverMode, build, staticHandler, routeId, request, loadContext, handleError) { | ||
try { | ||
@@ -266,3 +417,12 @@ // Note we keep the routeId here to align with the Remix handling of | ||
}); | ||
// callRouteLoader/callRouteAction always return responses | ||
if (typeof response === "object" && response !== null) { | ||
invariant(!(UNSAFE_DEFERRED_SYMBOL in response), `You cannot return a \`defer()\` response from a Resource Route. Did you ` + `forget to export a default UI component from the "${routeId}" route?`); | ||
} | ||
if (build.future.unstable_singleFetch && !isResponse(response)) { | ||
console.warn(resourceRouteJsonWarning(request.method === "GET" ? "loader" : "action", routeId)); | ||
response = json(response); | ||
} | ||
// callRouteLoader/callRouteAction always return responses (w/o single fetch). | ||
// With single fetch, users should always be Responses from resource routes | ||
invariant(isResponse(response), "Expected a Response to be returned from queryRoute"); | ||
@@ -274,4 +434,4 @@ return response; | ||
// match identically to what Remix returns | ||
error.headers.set("X-Remix-Catch", "yes"); | ||
return error; | ||
let response = safelySetHeader(error, "X-Remix-Catch", "yes"); | ||
return response; | ||
} | ||
@@ -289,3 +449,3 @@ if (isRouteErrorResponse(error)) { | ||
function errorResponseToJson(errorResponse, serverMode) { | ||
return json(serializeError( | ||
return json$1(serializeError( | ||
// @ts-expect-error This is "private" from users but intended for internal use | ||
@@ -314,3 +474,42 @@ errorResponse.error || new Error("Unexpected Server Error"), serverMode), { | ||
} | ||
function unwrapResponse(response) { | ||
let contentType = response.headers.get("Content-Type"); | ||
// Check between word boundaries instead of startsWith() due to the last | ||
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type | ||
return contentType && /\bapplication\/json\b/.test(contentType) ? response.body == null ? null : response.json() : response.text(); | ||
} | ||
function createRemixRedirectResponse(response, basename) { | ||
// We don't have any way to prevent a fetch request from following | ||
// redirects. So we use the `X-Remix-Redirect` header to indicate the | ||
// next URL, and then "follow" the redirect manually on the client. | ||
let headers = new Headers(response.headers); | ||
let redirectUrl = headers.get("Location"); | ||
headers.set("X-Remix-Redirect", basename ? stripBasename(redirectUrl, basename) || redirectUrl : redirectUrl); | ||
headers.set("X-Remix-Status", String(response.status)); | ||
headers.delete("Location"); | ||
if (response.headers.get("Set-Cookie") !== null) { | ||
headers.set("X-Remix-Revalidate", "yes"); | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
headers | ||
}); | ||
} | ||
// Anytime we are setting a header on a `Response` created in the loader/action, | ||
// we have to so it in this manner since in an `undici` world, if the `Response` | ||
// came directly from a `fetch` call, the headers are immutable will throw if | ||
// we try to set a new header. This is a sort of shallow clone of the `Response` | ||
// so we can safely set our own header. | ||
function safelySetHeader(response, name, value) { | ||
let headers = new Headers(response.headers); | ||
headers.set(name, value); | ||
return new Response(response.body, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers, | ||
duplex: response.body ? "half" : undefined | ||
}); | ||
} | ||
export { createRequestHandler }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
import type { StaticHandlerContext } from "@remix-run/router"; | ||
import type { ServerBuild } from "./build"; | ||
export declare function getDocumentHeadersRR(build: ServerBuild, context: StaticHandlerContext): Headers; | ||
export declare function getDocumentHeaders(build: ServerBuild, context: StaticHandlerContext): Headers; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -17,3 +17,3 @@ * Copyright (c) Remix Software Inc. | ||
function getDocumentHeadersRR(build, context) { | ||
function getDocumentHeaders(build, context) { | ||
let boundaryIdx = context.errors ? context.matches.findIndex(m => context.errors[m.route.id]) : -1; | ||
@@ -95,2 +95,2 @@ let matches = boundaryIdx >= 0 ? context.matches.slice(0, boundaryIdx + 1) : context.matches; | ||
exports.getDocumentHeadersRR = getDocumentHeadersRR; | ||
exports.getDocumentHeaders = getDocumentHeaders; |
export { createCookieFactory, isCookie } from "./cookies"; | ||
export { composeUploadHandlers as unstable_composeUploadHandlers, parseMultipartFormData as unstable_parseMultipartFormData, } from "./formData"; | ||
export { defer, json, redirect, redirectDocument } from "./responses"; | ||
export { defer, json, redirect, redirectDocument, replace } from "./responses"; | ||
export { SingleFetchRedirectSymbol as UNSAFE_SingleFetchRedirectSymbol, data as unstable_data, } from "./single-fetch"; | ||
export type { SingleFetchResult as UNSAFE_SingleFetchResult, SingleFetchResults as UNSAFE_SingleFetchResults, } from "./single-fetch"; | ||
export { createRequestHandler } from "./server"; | ||
@@ -10,4 +12,5 @@ export { createSession, createSessionStorageFactory, isSession, } from "./sessions"; | ||
export { MaxPartSizeExceededError } from "./upload/errors"; | ||
export { broadcastDevReady, logDevReady } from "./dev"; | ||
export { broadcastDevReady, logDevReady, setDevServerHooks as unstable_setDevServerHooks, } from "./dev"; | ||
export type { CreateCookieFunction, CreateCookieSessionStorageFunction, CreateMemorySessionStorageFunction, CreateRequestHandlerFunction, CreateSessionFunction, CreateSessionStorageFunction, IsCookieFunction, IsSessionFunction, JsonFunction, RedirectFunction, } from "./interface"; | ||
export type { Future } from "./future"; | ||
export type { ActionFunction, ActionFunctionArgs, AppLoadContext, Cookie, CookieOptions, CookieParseOptions, CookieSerializeOptions, CookieSignatureOptions, DataFunctionArgs, EntryContext, ErrorResponse, FlashSessionData, HandleDataRequestFunction, HandleDocumentRequestFunction, HeadersArgs, HeadersFunction, HtmlLinkDescriptor, LinkDescriptor, LinksFunction, LoaderFunction, LoaderFunctionArgs, MemoryUploadHandlerFilterArgs, MemoryUploadHandlerOptions, HandleErrorFunction, PageLinkDescriptor, RequestHandler, SerializeFrom, ServerBuild, ServerEntryModule, ServerRuntimeMetaArgs, ServerRuntimeMetaDescriptor, ServerRuntimeMetaFunction, Session, SessionData, SessionIdStorageStrategy, SessionStorage, SignFunction, TypedDeferredData, TypedResponse, UnsignFunction, UploadHandler, UploadHandlerPart, } from "./reexport"; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -18,2 +18,3 @@ * Copyright (c) Remix Software Inc. | ||
var responses = require('./responses.js'); | ||
var singleFetch = require('./single-fetch.js'); | ||
var server = require('./server.js'); | ||
@@ -37,2 +38,5 @@ var sessions = require('./sessions.js'); | ||
exports.redirectDocument = responses.redirectDocument; | ||
exports.replace = responses.replace; | ||
exports.UNSAFE_SingleFetchRedirectSymbol = singleFetch.SingleFetchRedirectSymbol; | ||
exports.unstable_data = singleFetch.data; | ||
exports.createRequestHandler = server.createRequestHandler; | ||
@@ -48,1 +52,2 @@ exports.createSession = sessions.createSession; | ||
exports.logDevReady = dev.logDevReady; | ||
exports.unstable_setDevServerHooks = dev.setDevServerHooks; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
export type Jsonify<T> = IsAny<T> extends true ? any : T extends { | ||
toJSON(): infer U; | ||
} ? (U extends JsonValue ? U : unknown) : T extends JsonPrimitive ? T : T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends Map<unknown, unknown> ? EmptyObject : T extends Set<unknown> ? EmptyObject : T extends TypedArray ? Record<string, number> : T extends NotJson ? never : T extends [] ? [] : T extends readonly [infer F, ...infer R] ? [NeverToNull<Jsonify<F>>, ...Jsonify<R>] : T extends readonly unknown[] ? Array<NeverToNull<Jsonify<T[number]>>> : T extends Record<keyof unknown, unknown> ? JsonifyObject<T> : unknown extends T ? unknown : never; | ||
} ? (U extends JsonValue ? U : unknown) : T extends JsonPrimitive ? T : T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends Promise<unknown> ? EmptyObject : T extends Map<unknown, unknown> ? EmptyObject : T extends Set<unknown> ? EmptyObject : T extends TypedArray ? Record<string, number> : T extends NotJson ? never : T extends [] ? [] : T extends readonly [infer F, ...infer R] ? [NeverToNull<Jsonify<F>>, ...Jsonify<R>] : T extends readonly unknown[] ? Array<NeverToNull<Jsonify<T[number]>>> : T extends Record<keyof unknown, unknown> ? JsonifyObject<T> : unknown extends T ? unknown : never; | ||
type ValueIsNotJson<T> = T extends NotJson ? true : false; | ||
@@ -29,3 +29,3 @@ type IsNotJson<T> = { | ||
declare const emptyObjectSymbol: unique symbol; | ||
type EmptyObject = { | ||
export type EmptyObject = { | ||
[emptyObjectSymbol]?: never; | ||
@@ -32,0 +32,0 @@ }; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
export type { ErrorResponse } from "@remix-run/router"; | ||
export type { HandleDataRequestFunction, HandleDocumentRequestFunction, HandleErrorFunction, ServerBuild, ServerEntryModule, } from "./build"; | ||
export type { Future } from "./future"; | ||
export type { UploadHandlerPart, UploadHandler } from "./formData"; | ||
@@ -4,0 +5,0 @@ export type { MemoryUploadHandlerOptions, MemoryUploadHandlerFilterArgs, } from "./upload/memoryUploadHandler"; |
@@ -35,2 +35,9 @@ import { type UNSAFE_DeferredData as DeferredData } from "@remix-run/router"; | ||
/** | ||
* A redirect response. Sets the status code and the `Location` header. | ||
* Defaults to "302 Found". | ||
* | ||
* @see https://remix.run/utils/redirect | ||
*/ | ||
export declare const replace: RedirectFunction; | ||
/** | ||
* A redirect response that will force a document reload to the new location. | ||
@@ -37,0 +44,0 @@ * Sets the status code and the `Location` header. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -20,2 +20,3 @@ * Copyright (c) Remix Software Inc. | ||
// interfaces must conform to the types they extend | ||
/** | ||
@@ -50,2 +51,12 @@ * This is a shortcut for creating `application/json` responses. Converts `data` | ||
/** | ||
* A redirect response. Sets the status code and the `Location` header. | ||
* Defaults to "302 Found". | ||
* | ||
* @see https://remix.run/utils/redirect | ||
*/ | ||
const replace = (url, init = 302) => { | ||
return router.replace(url, init); | ||
}; | ||
/** | ||
* A redirect response that will force a document reload to the new location. | ||
@@ -137,1 +148,2 @@ * Sets the status code and the `Location` header. | ||
exports.redirectDocument = redirectDocument; | ||
exports.replace = replace; |
@@ -8,2 +8,2 @@ import type { Params } from "@remix-run/router"; | ||
} | ||
export declare function matchServerRoutes(routes: ServerRoute[], pathname: string): RouteMatch<ServerRoute>[] | null; | ||
export declare function matchServerRoutes(routes: ServerRoute[], pathname: string, basename?: string): RouteMatch<ServerRoute>[] | null; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -17,4 +17,4 @@ * Copyright (c) Remix Software Inc. | ||
function matchServerRoutes(routes, pathname) { | ||
let matches = router.matchRoutes(routes, pathname); | ||
function matchServerRoutes(routes, pathname, basename) { | ||
let matches = router.matchRoutes(routes, pathname, basename); | ||
if (!matches) return null; | ||
@@ -21,0 +21,0 @@ return matches.map(match => ({ |
@@ -6,4 +6,7 @@ import type { ActionFunction as RRActionFunction, ActionFunctionArgs as RRActionFunctionArgs, AgnosticRouteMatch, LoaderFunction as RRLoaderFunction, LoaderFunctionArgs as RRLoaderFunctionArgs, Location, Params } from "@remix-run/router"; | ||
export interface RouteModules<RouteModule> { | ||
[routeId: string]: RouteModule; | ||
[routeId: string]: RouteModule | undefined; | ||
} | ||
/** | ||
* @deprecated Use `LoaderFunctionArgs`/`ActionFunctionArgs` instead | ||
*/ | ||
export type DataFunctionArgs = RRActionFunctionArgs<AppLoadContext> & RRLoaderFunctionArgs<AppLoadContext> & { | ||
@@ -13,11 +16,47 @@ context: AppLoadContext; | ||
/** | ||
* A function that handles data mutations for a route. | ||
* A function that handles data mutations for a route on the server | ||
*/ | ||
export type ActionFunction = (args: DataFunctionArgs) => ReturnType<RRActionFunction>; | ||
export type ActionFunctionArgs = DataFunctionArgs; | ||
export type ActionFunction = (args: ActionFunctionArgs) => ReturnType<RRActionFunction>; | ||
/** | ||
* A function that loads data for a route. | ||
* Arguments passed to a route `action` function | ||
*/ | ||
export type LoaderFunction = (args: DataFunctionArgs) => ReturnType<RRLoaderFunction>; | ||
export type LoaderFunctionArgs = DataFunctionArgs; | ||
export type ActionFunctionArgs = RRActionFunctionArgs<AppLoadContext> & { | ||
context: AppLoadContext; | ||
}; | ||
/** | ||
* A function that handles data mutations for a route on the client | ||
* @private Public API is exported from @remix-run/react | ||
*/ | ||
type ClientActionFunction = (args: ClientActionFunctionArgs) => ReturnType<RRActionFunction>; | ||
/** | ||
* Arguments passed to a route `clientAction` function | ||
* @private Public API is exported from @remix-run/react | ||
*/ | ||
export type ClientActionFunctionArgs = RRActionFunctionArgs<undefined> & { | ||
serverAction: <T = AppData>() => Promise<SerializeFrom<T>>; | ||
}; | ||
/** | ||
* A function that loads data for a route on the server | ||
*/ | ||
export type LoaderFunction = (args: LoaderFunctionArgs) => ReturnType<RRLoaderFunction>; | ||
/** | ||
* Arguments passed to a route `loader` function | ||
*/ | ||
export type LoaderFunctionArgs = RRLoaderFunctionArgs<AppLoadContext> & { | ||
context: AppLoadContext; | ||
}; | ||
/** | ||
* A function that loads data for a route on the client | ||
* @private Public API is exported from @remix-run/react | ||
*/ | ||
type ClientLoaderFunction = ((args: ClientLoaderFunctionArgs) => ReturnType<RRLoaderFunction>) & { | ||
hydrate?: boolean; | ||
}; | ||
/** | ||
* Arguments passed to a route `clientLoader` function | ||
* @private Public API is exported from @remix-run/react | ||
*/ | ||
export type ClientLoaderFunctionArgs = RRLoaderFunctionArgs<undefined> & { | ||
serverLoader: <T = AppData>() => Promise<SerializeFrom<T>>; | ||
}; | ||
export type HeadersArgs = { | ||
@@ -121,2 +160,3 @@ loaderHeaders: Headers; | ||
matches: ServerRuntimeMetaMatches<MatchLoaders>; | ||
error?: unknown; | ||
} | ||
@@ -157,3 +197,7 @@ export type ServerRuntimeMetaDescriptor = { | ||
export interface EntryRouteModule { | ||
clientAction?: ClientActionFunction; | ||
clientLoader?: ClientLoaderFunction; | ||
ErrorBoundary?: any; | ||
HydrateFallback?: any; | ||
Layout?: any; | ||
default: any; | ||
@@ -160,0 +204,0 @@ handle?: RouteHandle; |
@@ -18,2 +18,4 @@ import type { AgnosticDataRouteObject } from "@remix-run/router"; | ||
hasLoader: boolean; | ||
hasClientAction: boolean; | ||
hasClientLoader: boolean; | ||
hasErrorBoundary: boolean; | ||
@@ -20,0 +22,0 @@ imports?: string[]; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -54,3 +54,3 @@ * Copyright (c) Remix Software Inc. | ||
// though we know it'll always be provided in remix | ||
args => data.callRouteLoaderRR({ | ||
(args, dataStrategyCtx) => data.callRouteLoader({ | ||
request: args.request, | ||
@@ -60,5 +60,6 @@ params: args.params, | ||
loader: route.module.loader, | ||
routeId: route.id | ||
routeId: route.id, | ||
singleFetch: future.unstable_singleFetch === true | ||
}) : undefined, | ||
action: route.module.action ? args => data.callRouteActionRR({ | ||
action: route.module.action ? (args, dataStrategyCtx) => data.callRouteAction({ | ||
request: args.request, | ||
@@ -68,3 +69,4 @@ params: args.params, | ||
action: route.module.action, | ||
routeId: route.id | ||
routeId: route.id, | ||
singleFetch: future.unstable_singleFetch === true | ||
}) : undefined, | ||
@@ -71,0 +73,0 @@ handle: route.module.handle |
import type { Jsonify } from "./jsonify"; | ||
import type { TypedDeferredData, TypedResponse } from "./responses"; | ||
import type { ClientActionFunctionArgs, ClientLoaderFunctionArgs } from "./routeModules"; | ||
import { type SerializeFrom as SingleFetch_SerializeFrom } from "./single-fetch"; | ||
import type { Future } from "./future"; | ||
type SingleFetchEnabled = Future extends { | ||
unstable_singleFetch: infer T extends boolean; | ||
} ? T : false; | ||
/** | ||
* Infer JSON serialized data type returned by a loader or action. | ||
* Infer JSON serialized data type returned by a loader or action, while | ||
* avoiding deserialization if the input type if it's a clientLoader or | ||
* clientAction that returns a non-Response | ||
* | ||
@@ -9,3 +17,9 @@ * For example: | ||
*/ | ||
export type SerializeFrom<T> = T extends (...args: any[]) => infer Output ? Serialize<Awaited<Output>> : Jsonify<Awaited<T>>; | ||
export type SerializeFrom<T> = SingleFetchEnabled extends true ? SingleFetch_SerializeFrom<T> : T extends (...args: any[]) => infer Output ? Parameters<T> extends [ClientLoaderFunctionArgs | ClientActionFunctionArgs] ? SerializeClient<Awaited<Output>> : Serialize<Awaited<Output>> : Jsonify<Awaited<T>>; | ||
type SerializeClient<Output> = Output extends TypedDeferredData<infer U> ? { | ||
[K in keyof U as K extends symbol ? never : Promise<any> extends U[K] ? K : never]: DeferValueClient<U[K]>; | ||
} & { | ||
[K in keyof U as Promise<any> extends U[K] ? never : K]: U[K]; | ||
} : Output extends TypedResponse<infer U> ? Jsonify<U> : Awaited<Output>; | ||
type DeferValueClient<T> = T extends undefined ? undefined : T extends Promise<unknown> ? Promise<Awaited<T>> : T; | ||
type Serialize<Output> = Output extends TypedDeferredData<infer U> ? { | ||
@@ -12,0 +26,0 @@ [K in keyof U as K extends symbol ? never : Promise<any> extends U[K] ? K : never]: DeferValue<U[K]>; |
import type { AppLoadContext } from "./data"; | ||
import type { ServerBuild } from "./build"; | ||
export type RequestHandler = (request: Request, loadContext?: AppLoadContext, args?: { | ||
/** | ||
* @private This is an internal API intended for use by the Remix Vite plugin in dev mode | ||
*/ | ||
__criticalCss?: string; | ||
}) => Promise<Response>; | ||
export type CreateRequestHandlerFunction = (build: ServerBuild | (() => Promise<ServerBuild>), mode?: string) => RequestHandler; | ||
export type RequestHandler = (request: Request, loadContext?: AppLoadContext) => Promise<Response>; | ||
export type CreateRequestHandlerFunction = (build: ServerBuild | (() => ServerBuild | Promise<ServerBuild>), mode?: string) => RequestHandler; | ||
export declare const createRequestHandler: CreateRequestHandlerFunction; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -25,8 +25,18 @@ * Copyright (c) Remix Software Inc. | ||
var serverHandoff = require('./serverHandoff.js'); | ||
var dev = require('./dev.js'); | ||
var singleFetch = require('./single-fetch.js'); | ||
var deprecations = require('./deprecations.js'); | ||
function derive(build, mode$1) { | ||
var _build$future, _build$future2; | ||
let routes$1 = routes.createRoutes(build.routes); | ||
let dataRoutes = routes.createStaticHandlerDataRoutes(build.routes, build.future); | ||
let serverMode = mode.isServerMode(mode$1) ? mode$1 : mode.ServerMode.Production; | ||
let staticHandler = router.createStaticHandler(dataRoutes); | ||
let staticHandler = router.createStaticHandler(dataRoutes, { | ||
basename: build.basename, | ||
future: { | ||
v7_relativeSplatPath: ((_build$future = build.future) === null || _build$future === void 0 ? void 0 : _build$future.v3_relativeSplatPath) === true, | ||
v7_throwAbortReason: ((_build$future2 = build.future) === null || _build$future2 === void 0 ? void 0 : _build$future2.v3_throwAbortReason) === true | ||
} | ||
}); | ||
let errorHandler = build.entry.module.handleError || ((error, { | ||
@@ -49,3 +59,3 @@ request | ||
} | ||
const createRequestHandler = (build, mode) => { | ||
const createRequestHandler = (build, mode$1) => { | ||
let _build; | ||
@@ -56,8 +66,7 @@ let routes; | ||
let errorHandler; | ||
return async function requestHandler(request, loadContext = {}, { | ||
__criticalCss: criticalCss | ||
} = {}) { | ||
return async function requestHandler(request, loadContext = {}) { | ||
_build = typeof build === "function" ? await build() : build; | ||
mode$1 ??= _build.mode; | ||
if (typeof build === "function") { | ||
let derived = derive(_build, mode); | ||
let derived = derive(_build, mode$1); | ||
routes = derived.routes; | ||
@@ -68,3 +77,3 @@ serverMode = derived.serverMode; | ||
} else if (!routes || !serverMode || !staticHandler || !errorHandler) { | ||
let derived = derive(_build, mode); | ||
let derived = derive(_build, mode$1); | ||
routes = derived.routes; | ||
@@ -76,24 +85,82 @@ serverMode = derived.serverMode; | ||
let url = new URL(request.url); | ||
let matches = routeMatching.matchServerRoutes(routes, url.pathname); | ||
let handleError = error => errorHandler(error, { | ||
context: loadContext, | ||
params: matches && matches.length > 0 ? matches[0].params : {}, | ||
request | ||
}); | ||
let params = {}; | ||
let handleError = error => { | ||
if (mode$1 === mode.ServerMode.Development) { | ||
var _getDevServerHooks, _getDevServerHooks$pr; | ||
(_getDevServerHooks = dev.getDevServerHooks()) === null || _getDevServerHooks === void 0 ? void 0 : (_getDevServerHooks$pr = _getDevServerHooks.processRequestError) === null || _getDevServerHooks$pr === void 0 ? void 0 : _getDevServerHooks$pr.call(_getDevServerHooks, error); | ||
} | ||
errorHandler(error, { | ||
context: loadContext, | ||
params, | ||
request | ||
}); | ||
}; | ||
// Manifest request for fog of war | ||
let manifestUrl = `${_build.basename ?? "/"}/__manifest`.replace(/\/+/g, "/"); | ||
if (url.pathname === manifestUrl) { | ||
try { | ||
let res = await handleManifestRequest(_build, routes, url); | ||
return res; | ||
} catch (e) { | ||
handleError(e); | ||
return new Response("Unknown Server Error", { | ||
status: 500 | ||
}); | ||
} | ||
} | ||
let matches = routeMatching.matchServerRoutes(routes, url.pathname, _build.basename); | ||
if (matches && matches.length > 0) { | ||
Object.assign(params, matches[0].params); | ||
} | ||
let response; | ||
if (url.searchParams.has("_data")) { | ||
if (_build.future.unstable_singleFetch) { | ||
handleError(new Error("Warning: Single fetch-enabled apps should not be making ?_data requests, " + "this is likely to break in the future")); | ||
} | ||
let routeId = url.searchParams.get("_data"); | ||
response = await handleDataRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError); | ||
response = await handleDataRequest(serverMode, _build, staticHandler, routeId, request, loadContext, handleError); | ||
if (_build.entry.module.handleDataRequest) { | ||
var _matches$find; | ||
response = await _build.entry.module.handleDataRequest(response, { | ||
context: loadContext, | ||
params: (matches === null || matches === void 0 ? void 0 : (_matches$find = matches.find(m => m.route.id == routeId)) === null || _matches$find === void 0 ? void 0 : _matches$find.params) || {}, | ||
params, | ||
request | ||
}); | ||
if (responses.isRedirectResponse(response)) { | ||
response = createRemixRedirectResponse(response, _build.basename); | ||
} | ||
} | ||
} else if (_build.future.unstable_singleFetch && url.pathname.endsWith(".data")) { | ||
let handlerUrl = new URL(request.url); | ||
handlerUrl.pathname = handlerUrl.pathname.replace(/\.data$/, "").replace(/^\/_root$/, "/"); | ||
let singleFetchMatches = routeMatching.matchServerRoutes(routes, handlerUrl.pathname, _build.basename); | ||
response = await handleSingleFetchRequest(serverMode, _build, staticHandler, request, handlerUrl, loadContext, handleError); | ||
if (_build.entry.module.handleDataRequest) { | ||
response = await _build.entry.module.handleDataRequest(response, { | ||
context: loadContext, | ||
params: singleFetchMatches ? singleFetchMatches[0].params : {}, | ||
request | ||
}); | ||
if (responses.isRedirectResponse(response)) { | ||
let result = singleFetch.getSingleFetchRedirect(response.status, response.headers, _build.basename); | ||
if (request.method === "GET") { | ||
result = { | ||
[singleFetch.SingleFetchRedirectSymbol]: result | ||
}; | ||
} | ||
let headers = new Headers(response.headers); | ||
headers.set("Content-Type", "text/x-script"); | ||
return new Response(singleFetch.encodeViaTurboStream(result, request.signal, _build.entry.module.streamTimeout, serverMode), { | ||
status: singleFetch.SINGLE_FETCH_REDIRECT_STATUS, | ||
headers | ||
}); | ||
} | ||
} | ||
} else if (matches && matches[matches.length - 1].route.module.default == null && matches[matches.length - 1].route.module.ErrorBoundary == null) { | ||
response = await handleResourceRequestRR(serverMode, staticHandler, matches.slice(-1)[0].route.id, request, loadContext, handleError); | ||
response = await handleResourceRequest(serverMode, _build, staticHandler, matches.slice(-1)[0].route.id, request, loadContext, handleError); | ||
} else { | ||
response = await handleDocumentRequestRR(serverMode, _build, staticHandler, request, loadContext, handleError, criticalCss); | ||
var _getDevServerHooks2, _getDevServerHooks2$g; | ||
let criticalCss = mode$1 === mode.ServerMode.Development ? await ((_getDevServerHooks2 = dev.getDevServerHooks()) === null || _getDevServerHooks2 === void 0 ? void 0 : (_getDevServerHooks2$g = _getDevServerHooks2.getCriticalCss) === null || _getDevServerHooks2$g === void 0 ? void 0 : _getDevServerHooks2$g.call(_getDevServerHooks2, _build, url.pathname)) : undefined; | ||
response = await handleDocumentRequest(serverMode, _build, staticHandler, request, loadContext, handleError, criticalCss); | ||
} | ||
@@ -110,3 +177,25 @@ if (request.method === "HEAD") { | ||
}; | ||
async function handleDataRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError) { | ||
async function handleManifestRequest(build, routes, url) { | ||
let patches = {}; | ||
if (url.searchParams.has("p")) { | ||
for (let path of url.searchParams.getAll("p")) { | ||
let matches = routeMatching.matchServerRoutes(routes, path, build.basename); | ||
if (matches) { | ||
for (let match of matches) { | ||
let routeId = match.route.id; | ||
patches[routeId] = build.assets.routes[routeId]; | ||
} | ||
} | ||
} | ||
return responses.json(patches, { | ||
headers: { | ||
"Cache-Control": "public, max-age=31536000, immutable" | ||
} | ||
}); // Override the TypedResponse stuff from json() | ||
} | ||
return new Response("Invalid Request", { | ||
status: 400 | ||
}); | ||
} | ||
async function handleDataRequest(serverMode, build, staticHandler, routeId, request, loadContext, handleError) { | ||
try { | ||
@@ -118,16 +207,3 @@ let response = await staticHandler.queryRoute(request, { | ||
if (responses.isRedirectResponse(response)) { | ||
// We don't have any way to prevent a fetch request from following | ||
// redirects. So we use the `X-Remix-Redirect` header to indicate the | ||
// next URL, and then "follow" the redirect manually on the client. | ||
let headers = new Headers(response.headers); | ||
headers.set("X-Remix-Redirect", headers.get("Location")); | ||
headers.set("X-Remix-Status", response.status); | ||
headers.delete("Location"); | ||
if (response.headers.get("Set-Cookie") !== null) { | ||
headers.set("X-Remix-Revalidate", "yes"); | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
headers | ||
}); | ||
return createRemixRedirectResponse(response, build.basename); | ||
} | ||
@@ -149,16 +225,14 @@ if (router.UNSAFE_DEFERRED_SYMBOL in response) { | ||
// network errors that are missing this header | ||
response.headers.set("X-Remix-Response", "yes"); | ||
response = safelySetHeader(response, "X-Remix-Response", "yes"); | ||
return response; | ||
} catch (error) { | ||
if (responses.isResponse(error)) { | ||
error.headers.set("X-Remix-Catch", "yes"); | ||
return error; | ||
let response = safelySetHeader(error, "X-Remix-Catch", "yes"); | ||
return response; | ||
} | ||
if (router.isRouteErrorResponse(error)) { | ||
if (error) { | ||
handleError(error); | ||
} | ||
handleError(error); | ||
return errorResponseToJson(error, serverMode); | ||
} | ||
let errorInstance = error instanceof Error ? error : new Error("Unexpected Server Error"); | ||
let errorInstance = error instanceof Error || error instanceof DOMException ? error : new Error("Unexpected Server Error"); | ||
handleError(errorInstance); | ||
@@ -173,3 +247,36 @@ return router.json(errors.serializeError(errorInstance, serverMode), { | ||
} | ||
async function handleDocumentRequestRR(serverMode, build, staticHandler, request, loadContext, handleError, criticalCss) { | ||
async function handleSingleFetchRequest(serverMode, build, staticHandler, request, handlerUrl, loadContext, handleError) { | ||
let { | ||
result, | ||
headers, | ||
status | ||
} = request.method !== "GET" ? await singleFetch.singleFetchAction(build, serverMode, staticHandler, request, handlerUrl, loadContext, handleError) : await singleFetch.singleFetchLoaders(build, serverMode, staticHandler, request, handlerUrl, loadContext, handleError); | ||
// Mark all successful responses with a header so we can identify in-flight | ||
// network errors that are missing this header | ||
let resultHeaders = new Headers(headers); | ||
resultHeaders.set("X-Remix-Response", "yes"); | ||
// 304 responses should not have a body | ||
if (status === 304) { | ||
return new Response(null, { | ||
status: 304, | ||
headers: resultHeaders | ||
}); | ||
} | ||
// We use a less-descriptive `text/x-script` here instead of something like | ||
// `text/x-turbo` to enable compression when deployed via Cloudflare. See: | ||
// - https://github.com/remix-run/remix/issues/9884 | ||
// - https://developers.cloudflare.com/speed/optimization/content/brotli/content-compression/ | ||
resultHeaders.set("Content-Type", "text/x-script"); | ||
// Note: Deferred data is already just Promises, so we don't have to mess | ||
// `activeDeferreds` or anything :) | ||
return new Response(singleFetch.encodeViaTurboStream(result, request.signal, build.entry.module.streamTimeout, serverMode), { | ||
status: status || 200, | ||
headers: resultHeaders | ||
}); | ||
} | ||
async function handleDocumentRequest(serverMode, build, staticHandler, request, loadContext, handleError, criticalCss) { | ||
let context; | ||
@@ -189,7 +296,16 @@ try { | ||
} | ||
let headers$1 = headers.getDocumentHeaders(build, context); | ||
// 304 responses should not have a body or a content-type | ||
if (context.statusCode === 304) { | ||
return new Response(null, { | ||
status: 304, | ||
headers: headers$1 | ||
}); | ||
} | ||
// Sanitize errors outside of development environments | ||
if (context.errors) { | ||
Object.values(context.errors).forEach(err => { | ||
// @ts-expect-error This is "private" from users but intended for internal use | ||
// @ts-expect-error `err.error` is "private" from users but intended for internal use | ||
if (!router.isRouteErrorResponse(err) || err.error) { | ||
@@ -201,3 +317,11 @@ handleError(err); | ||
} | ||
let headers$1 = headers.getDocumentHeadersRR(build, context); | ||
// Server UI state to send to the client. | ||
// - When single fetch is enabled, this is streamed down via `serverHandoffStream` | ||
// - Otherwise it's stringified into `serverHandoffString` | ||
let state = { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: errors.serializeErrors(context.errors, serverMode) | ||
}; | ||
let entryContext = { | ||
@@ -209,12 +333,16 @@ manifest: build.assets, | ||
serverHandoffString: serverHandoff.createServerHandoffString({ | ||
url: context.location.pathname, | ||
basename: build.basename, | ||
criticalCss, | ||
state: { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: errors.serializeErrors(context.errors, serverMode) | ||
}, | ||
future: build.future | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
...(!build.future.unstable_singleFetch ? { | ||
state | ||
} : null) | ||
}), | ||
...(build.future.unstable_singleFetch ? { | ||
serverHandoffStream: singleFetch.encodeViaTurboStream(state, request.signal, build.entry.module.streamTimeout, serverMode), | ||
renderMeta: {} | ||
} : null), | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
serializeError: err => errors.serializeError(err, serverMode) | ||
@@ -227,5 +355,17 @@ }; | ||
handleError(error); | ||
let errorForSecondRender = error; | ||
// If they threw a response, unwrap it into an ErrorResponse like we would | ||
// have for a loader/action | ||
if (responses.isResponse(error)) { | ||
try { | ||
let data = await unwrapResponse(error); | ||
errorForSecondRender = new router.UNSAFE_ErrorResponseImpl(error.status, error.statusText, data); | ||
} catch (e) { | ||
// If we can't unwrap the response - just leave it as-is | ||
} | ||
} | ||
// Get a new StaticHandlerContext that contains the error at the right boundary | ||
context = router.getStaticContextFromError(staticHandler.dataRoutes, context, error); | ||
context = router.getStaticContextFromError(staticHandler.dataRoutes, context, errorForSecondRender); | ||
@@ -237,3 +377,11 @@ // Sanitize errors outside of development environments | ||
// Update entryContext for the second render pass | ||
// Get a new entryContext for the second render pass | ||
// Server UI state to send to the client. | ||
// - When single fetch is enabled, this is streamed down via `serverHandoffStream` | ||
// - Otherwise it's stringified into `serverHandoffString` | ||
let state = { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: errors.serializeErrors(context.errors, serverMode) | ||
}; | ||
entryContext = { | ||
@@ -243,10 +391,13 @@ ...entryContext, | ||
serverHandoffString: serverHandoff.createServerHandoffString({ | ||
url: context.location.pathname, | ||
state: { | ||
loaderData: context.loaderData, | ||
actionData: context.actionData, | ||
errors: errors.serializeErrors(context.errors, serverMode) | ||
}, | ||
future: build.future | ||
}) | ||
basename: build.basename, | ||
future: build.future, | ||
isSpaMode: build.isSpaMode, | ||
...(!build.future.unstable_singleFetch ? { | ||
state | ||
} : null) | ||
}), | ||
...(build.future.unstable_singleFetch ? { | ||
serverHandoffStream: singleFetch.encodeViaTurboStream(state, request.signal, build.entry.module.streamTimeout, serverMode), | ||
renderMeta: {} | ||
} : null) | ||
}; | ||
@@ -261,3 +412,3 @@ try { | ||
} | ||
async function handleResourceRequestRR(serverMode, staticHandler, routeId, request, loadContext, handleError) { | ||
async function handleResourceRequest(serverMode, build, staticHandler, routeId, request, loadContext, handleError) { | ||
try { | ||
@@ -271,3 +422,12 @@ // Note we keep the routeId here to align with the Remix handling of | ||
}); | ||
// callRouteLoader/callRouteAction always return responses | ||
if (typeof response === "object" && response !== null) { | ||
invariant["default"](!(router.UNSAFE_DEFERRED_SYMBOL in response), `You cannot return a \`defer()\` response from a Resource Route. Did you ` + `forget to export a default UI component from the "${routeId}" route?`); | ||
} | ||
if (build.future.unstable_singleFetch && !responses.isResponse(response)) { | ||
console.warn(deprecations.resourceRouteJsonWarning(request.method === "GET" ? "loader" : "action", routeId)); | ||
response = responses.json(response); | ||
} | ||
// callRouteLoader/callRouteAction always return responses (w/o single fetch). | ||
// With single fetch, users should always be Responses from resource routes | ||
invariant["default"](responses.isResponse(response), "Expected a Response to be returned from queryRoute"); | ||
@@ -279,4 +439,4 @@ return response; | ||
// match identically to what Remix returns | ||
error.headers.set("X-Remix-Catch", "yes"); | ||
return error; | ||
let response = safelySetHeader(error, "X-Remix-Catch", "yes"); | ||
return response; | ||
} | ||
@@ -318,3 +478,42 @@ if (router.isRouteErrorResponse(error)) { | ||
} | ||
function unwrapResponse(response) { | ||
let contentType = response.headers.get("Content-Type"); | ||
// Check between word boundaries instead of startsWith() due to the last | ||
// paragraph of https://httpwg.org/specs/rfc9110.html#field.content-type | ||
return contentType && /\bapplication\/json\b/.test(contentType) ? response.body == null ? null : response.json() : response.text(); | ||
} | ||
function createRemixRedirectResponse(response, basename) { | ||
// We don't have any way to prevent a fetch request from following | ||
// redirects. So we use the `X-Remix-Redirect` header to indicate the | ||
// next URL, and then "follow" the redirect manually on the client. | ||
let headers = new Headers(response.headers); | ||
let redirectUrl = headers.get("Location"); | ||
headers.set("X-Remix-Redirect", basename ? router.stripBasename(redirectUrl, basename) || redirectUrl : redirectUrl); | ||
headers.set("X-Remix-Status", String(response.status)); | ||
headers.delete("Location"); | ||
if (response.headers.get("Set-Cookie") !== null) { | ||
headers.set("X-Remix-Revalidate", "yes"); | ||
} | ||
return new Response(null, { | ||
status: 204, | ||
headers | ||
}); | ||
} | ||
// Anytime we are setting a header on a `Response` created in the loader/action, | ||
// we have to so it in this manner since in an `undici` world, if the `Response` | ||
// came directly from a `fetch` call, the headers are immutable will throw if | ||
// we try to set a new header. This is a sort of shallow clone of the `Response` | ||
// so we can safely set our own header. | ||
function safelySetHeader(response, name, value) { | ||
let headers = new Headers(response.headers); | ||
headers.set(name, value); | ||
return new Response(response.body, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers, | ||
duplex: response.body ? "half" : undefined | ||
}); | ||
} | ||
exports.createRequestHandler = createRequestHandler; |
@@ -5,7 +5,8 @@ import type { HydrationState } from "@remix-run/router"; | ||
export declare function createServerHandoffString<T>(serverHandoff: { | ||
state: ValidateShape<T, HydrationState>; | ||
state?: ValidateShape<T, HydrationState>; | ||
criticalCss?: string; | ||
url: string; | ||
basename: string | undefined; | ||
future: FutureConfig; | ||
isSpaMode: boolean; | ||
}): string; | ||
export {}; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
@@ -1,2 +0,2 @@ | ||
import type { UploadHandler } from "@remix-run/server-runtime"; | ||
import type { UploadHandler } from "../formData"; | ||
export type MemoryUploadHandlerFilterArgs = { | ||
@@ -3,0 +3,0 @@ filename?: string; |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
/** | ||
* @remix-run/server-runtime v0.0.0-nightly-3874120-20231103 | ||
* @remix-run/server-runtime v0.0.0-nightly-38ca648a3-20240920 | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) Remix Software Inc. |
MIT License | ||
Copyright (c) Remix Software Inc. 2020-2021 | ||
Copyright (c) Shopify Inc. 2022-2023 | ||
Copyright (c) Shopify Inc. 2022-2024 | ||
@@ -6,0 +6,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "@remix-run/server-runtime", | ||
"version": "0.0.0-nightly-3874120-20231103", | ||
"version": "0.0.0-nightly-38ca648a3-20240920", | ||
"description": "Server runtime for Remix", | ||
@@ -19,8 +19,9 @@ "bugs": { | ||
"dependencies": { | ||
"@remix-run/router": "1.11.0", | ||
"@types/cookie": "^0.4.1", | ||
"@remix-run/router": "1.19.2", | ||
"@types/cookie": "^0.6.0", | ||
"@web3-storage/multipart-parser": "^1.0.0", | ||
"cookie": "^0.4.1", | ||
"cookie": "^0.6.0", | ||
"set-cookie-parser": "^2.4.8", | ||
"source-map": "^0.7.3" | ||
"source-map": "^0.7.3", | ||
"turbo-stream": "2.4.0" | ||
}, | ||
@@ -47,3 +48,6 @@ "devDependencies": { | ||
"README.md" | ||
] | ||
} | ||
], | ||
"scripts": { | ||
"tsc": "tsc" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85
5213
203480
8
+ Addedturbo-stream@2.4.0
+ Added@remix-run/router@1.19.2(transitive)
+ Added@types/cookie@0.6.0(transitive)
+ Addedcookie@0.6.0(transitive)
+ Addedturbo-stream@2.4.0(transitive)
- Removed@remix-run/router@1.11.0(transitive)
- Removed@types/cookie@0.4.1(transitive)
- Removedcookie@0.4.2(transitive)
Updated@remix-run/router@1.19.2
Updated@types/cookie@^0.6.0
Updatedcookie@^0.6.0