@casterly/core
Advanced tools
Comparing version 0.9.0-alpha.10 to 0.9.0-alpha.11
@@ -1,1 +0,1 @@ | ||
export * from './lib/server/defaultServer' | ||
export * from './lib/index' |
'use strict' | ||
if (process.env.NODE_ENV === 'production') { | ||
module.exports = require('./lib/server/defaultServer') | ||
module.exports = require('./lib/index') | ||
} else { | ||
module.exports = require('./lib/server/devServer') | ||
module.exports = require('./lib/index.development') | ||
} |
import type { RoutesManifest } from 'casterly'; | ||
import type { Request } from '../fetch'; | ||
import { Headers } from '../fetch'; | ||
import { Headers, Request } from '../fetch'; | ||
import type { ResponseObject } from './response'; | ||
@@ -18,3 +17,3 @@ export interface ServerOptions { | ||
private getServerContextForRoute; | ||
private getAppRequestHandler; | ||
private getServerEntrypoint; | ||
private renderDocument; | ||
@@ -21,0 +20,0 @@ } |
@@ -39,3 +39,2 @@ "use strict"; | ||
const path = __importStar(require("path")); | ||
const url_1 = require("url"); | ||
const utils_1 = require("@casterly/utils"); | ||
@@ -53,7 +52,10 @@ const fresh_1 = __importDefault(require("fresh")); | ||
this.getRoutesManifestFile = () => this._routesManifest; | ||
this.getServerContextForRoute = async (url) => { | ||
this.getServerContextForRoute = async (request) => { | ||
const url = request.url; | ||
const routesManifest = this.getRoutesManifestFile(); | ||
const { getAppContext } = await this.getServerEntrypoint(); | ||
const appRoutesModule = await Promise.resolve().then(() => __importStar(require(path.join(utils_1.paths.appServerBuildFolder, STATIC_ENTRYPOINTS_ROUTES)))); | ||
const appRoutes = appRoutesModule.default || appRoutesModule; | ||
const appNotFoundRoute = appRoutesModule.notFound; | ||
const appContext = getAppContext === null || getAppContext === void 0 ? void 0 : getAppContext(request); | ||
const { routes, matchedRoutes, matchedRoutesAssets, routeHeaders, status } = await routes_1.getMatchedRoutes({ | ||
@@ -64,2 +66,3 @@ location: url, | ||
notFoundRoutePromiseComponent: appNotFoundRoute, | ||
appContext, | ||
}); | ||
@@ -81,9 +84,17 @@ const serverContext = { | ||
status, | ||
appContext, | ||
}; | ||
return serverContext; | ||
}; | ||
this.getAppRequestHandler = async () => { | ||
const handleRequest = await Promise.resolve().then(() => __importStar(require(path.join(utils_1.paths.appServerBuildFolder, STATIC_RUNTIME_MAIN)))).then(utils_2.interopDefault) | ||
.then(utils_2.interopDefault); | ||
return handleRequest; | ||
this.getServerEntrypoint = async () => { | ||
let serverEntrypointModule = await Promise.resolve().then(() => __importStar(require(path.join(utils_1.paths.appServerBuildFolder, STATIC_RUNTIME_MAIN)))); | ||
if (typeof serverEntrypointModule.default === 'object' && | ||
serverEntrypointModule.default.default) { | ||
// TypeScript will wrap the above import call with an __importStar | ||
// function, which will make the default exports the reference to the | ||
// actual module, so we must interop twice to get the actual | ||
// implementation | ||
serverEntrypointModule = serverEntrypointModule.default; | ||
} | ||
return serverEntrypointModule; | ||
}; | ||
@@ -93,9 +104,9 @@ this.renderDocument = async (request, responseHeaders, adapterOptions) => { | ||
const headers = new fetch_1.Headers(responseHeaders); | ||
const serverContext = await this.getServerContextForRoute(request.url); | ||
const _b = await this.getServerContextForRoute(request), { appContext } = _b, serverContext = __rest(_b, ["appContext"]); | ||
(_a = serverContext.routeHeaders) === null || _a === void 0 ? void 0 : _a.forEach((value, key) => { | ||
headers.set(key, value); | ||
}); | ||
const handleRequest = await this.getAppRequestHandler(); | ||
const { default: handleRequest } = await this.getServerEntrypoint(); | ||
let status = serverContext.status; | ||
let response = handleRequest(request, status, headers, serverContext, adapterOptions); | ||
let response = handleRequest(request, status, headers, serverContext, Object.assign(Object.assign({}, adapterOptions), { appContext })); | ||
if ('then' in response) { | ||
@@ -196,4 +207,4 @@ response = await response; | ||
else { | ||
const url = url_1.parse(query.path); | ||
const _a = await this.getServerContextForRoute(url.pathname), { routes, routeHeaders, status: serverContextStatus } = _a, clientContext = __rest(_a, ["routes", "routeHeaders", "status"]); | ||
const urlRequest = new fetch_1.Request(query.path); | ||
const _a = await this.getServerContextForRoute(urlRequest), { routes, routeHeaders, status: serverContextStatus, appContext } = _a, clientContext = __rest(_a, ["routes", "routeHeaders", "status", "appContext"]); | ||
status = serverContextStatus; | ||
@@ -200,0 +211,0 @@ body = JSON.stringify(clientContext); |
@@ -6,3 +6,3 @@ import type { RoutesManifest } from 'casterly'; | ||
export declare type RouteModule = { | ||
default: React.ComponentType; | ||
default: React.ComponentType<any>; | ||
headers?: (headersParams: { | ||
@@ -12,2 +12,6 @@ params: RouteMatch['params']; | ||
}) => Record<string, string>; | ||
loaderMetadata?: (options: { | ||
params: RouteMatch['params']; | ||
context: unknown; | ||
}) => unknown; | ||
}; | ||
@@ -26,2 +30,4 @@ export declare type RoutePromiseComponent = { | ||
headers: RouteModule['headers']; | ||
loaderMetadata: RouteModule['loaderMetadata']; | ||
metadata?: unknown; | ||
key: number; | ||
@@ -33,3 +39,3 @@ }; | ||
export declare const mergeRouteAssetsAndRoutes: (routesManifestRoutes: RoutesManifest['routes'], routePromises: RoutePromiseComponent[]) => Promise<RouteObjectWithAssets[]>; | ||
export declare const getMatchedRoutes: ({ location, routesPromiseComponent, routesManifest, notFoundRoutePromiseComponent, }: { | ||
export declare const getMatchedRoutes: ({ location, routesPromiseComponent, routesManifest, notFoundRoutePromiseComponent, appContext, }: { | ||
location: string; | ||
@@ -39,2 +45,3 @@ routesManifest: RoutesManifest; | ||
notFoundRoutePromiseComponent?: RoutePromiseComponent | undefined; | ||
appContext: unknown; | ||
}) => Promise<{ | ||
@@ -41,0 +48,0 @@ routes: RouteObjectWithAssets[]; |
@@ -21,11 +21,18 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getMatchedRoutes = exports.mergeRouteAssetsAndRoutes = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
const utils_1 = require("@casterly/utils"); | ||
const React = __importStar(require("react")); | ||
const react_router_1 = require("react-router"); | ||
const fetch_1 = require("../fetch"); | ||
const utils_2 = require("../server/utils"); | ||
const { STATIC_RUNTIME_LOADER } = utils_1.constants; | ||
const mergeRoute = async ({ index, route, manifestRoute, children = [], }) => { | ||
var _a; | ||
const routeComponentModule = await route.component(); | ||
return Object.assign(Object.assign({}, route), { path: manifestRoute.path, caseSensitive: route.caseSensitive === true, element: React.createElement(routeComponentModule.default, route.props), headers: routeComponentModule.headers, assets: (_a = manifestRoute.assets) !== null && _a !== void 0 ? _a : [], componentName: manifestRoute.componentName, children, key: index }); | ||
return Object.assign(Object.assign({}, route), { path: manifestRoute.path, caseSensitive: route.caseSensitive === true, element: React.createElement(routeComponentModule.default, route.props), headers: routeComponentModule.headers, loaderMetadata: routeComponentModule.loaderMetadata, assets: (_a = manifestRoute.assets) !== null && _a !== void 0 ? _a : [], componentName: manifestRoute.componentName, children, key: index }); | ||
}; | ||
@@ -46,4 +53,4 @@ const mergeRouteAssetsAndRoutes = (routesManifestRoutes, routePromises) => { | ||
exports.mergeRouteAssetsAndRoutes = mergeRouteAssetsAndRoutes; | ||
const getMatchedRoutes = async ({ location, routesPromiseComponent, routesManifest, notFoundRoutePromiseComponent, }) => { | ||
var _a; | ||
const getMatchedRoutes = async ({ location, routesPromiseComponent, routesManifest, notFoundRoutePromiseComponent, appContext, }) => { | ||
var _a, _b; | ||
const routes = await exports.mergeRouteAssetsAndRoutes(routesManifest.routes, routesPromiseComponent); | ||
@@ -86,2 +93,29 @@ const notFoundRoute = notFoundRoutePromiseComponent && routesManifest.notFound | ||
})) !== null && _a !== void 0 ? _a : []).filter((file) => !routesManifest.main.includes(file)))); | ||
const loaderModule = await Promise.resolve().then(() => __importStar(require(path_1.default.join(utils_1.paths.appServerBuildFolder, STATIC_RUNTIME_LOADER)))).then(utils_2.interopDefault); | ||
const loadFn = loaderModule.createPreloadForContext(appContext); | ||
let routesToPreload = routes; | ||
for (const matchedRoute of matchedRoutes) { | ||
const route = routesToPreload.find((route) => route === matchedRoute.route); | ||
if (!route) { | ||
break; | ||
} | ||
const metadata = (_b = route.loaderMetadata) === null || _b === void 0 ? void 0 : _b.call(route, { | ||
params: matchedRoute.params, | ||
context: appContext, | ||
}); | ||
if (!metadata && route.children) { | ||
routesToPreload = route.children; | ||
continue; | ||
} | ||
else if (!metadata && !route.children) { | ||
break; | ||
} | ||
const preloadedData = loadFn(metadata); | ||
route.element = React.cloneElement(route.element, Object.assign(Object.assign({}, route.element.props), { preloadedData })); | ||
route.metadata = metadata; | ||
if (!route.children) { | ||
break; | ||
} | ||
routesToPreload = route.children; | ||
} | ||
return { | ||
@@ -88,0 +122,0 @@ routes, |
{ | ||
"name": "@casterly/core", | ||
"version": "0.9.0-alpha.10", | ||
"version": "0.9.0-alpha.11", | ||
"description": "Core package for Casterly", | ||
@@ -36,3 +36,3 @@ "author": "Lucas Cordeiro <ecdb.lucas@gmail.com>", | ||
"dependencies": { | ||
"@casterly/utils": "^0.9.0-alpha.4", | ||
"@casterly/utils": "^0.9.0-alpha.11", | ||
"etag": "^1.8.1", | ||
@@ -46,3 +46,3 @@ "fresh": "^0.5.2", | ||
"devDependencies": { | ||
"@casterly/components": "^0.9.0-alpha.9", | ||
"@casterly/components": "^0.9.0-alpha.11", | ||
"@types/etag": "^1.8.1", | ||
@@ -55,3 +55,3 @@ "@types/fresh": "^0.5.0", | ||
"@types/react-dom": "^17.0.9", | ||
"casterly": "^0.9.0-alpha.10", | ||
"casterly": "^0.9.0-alpha.11", | ||
"cross-env": "^7.0.2" | ||
@@ -66,3 +66,3 @@ }, | ||
}, | ||
"gitHead": "6d9fa41b73e1fa60b14c3aadb3235ce7f47aa5bc" | ||
"gitHead": "381cb03f7180821192c33c7391505535cbba77b3" | ||
} |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
38644
34
835
8