Revert: feat(server): add safety checks during dev (#1567)

This commit is contained in:
Marvin Hagemeister
2023-08-01 18:10:30 +02:00
committed by GitHub
parent ba413c6412
commit df5931e105
20 changed files with 18 additions and 400 deletions
+15 -44
View File
@@ -37,7 +37,6 @@ import {
RouterOptions,
RouterState,
ServeHandlerInfo,
StaticFile,
UnknownPage,
UnknownPageModule,
} from "./types.ts";
@@ -55,18 +54,6 @@ import {
JSXConfig,
} from "../build/mod.ts";
import { InternalRoute } from "./router.ts";
import {
assertModuleExportsDefault,
assertNoStaticRouteConflicts,
assertPluginsCallRender,
assertPluginsCallRenderAsync,
assertPluginsInjectModules,
assertRoutesHaveHandlerOrComponent,
assertSingleModule,
assertSingleRoutePattern,
assertStaticDirSafety,
CheckFunction,
} from "$fresh/src/server/dev_checks.ts";
const DEFAULT_CONN_INFO: ServeHandlerInfo = {
localAddr: { transport: "tcp", hostname: "localhost", port: 8080 },
@@ -83,6 +70,20 @@ function isDevMode() {
// Env var is only set in prod (on Deploy).
return Deno.env.get("DENO_DEPLOYMENT_ID") === undefined;
}
interface StaticFile {
/** The URL to the static file on disk. */
localUrl: URL;
/** The path to the file as it would be in the incoming request. */
path: string;
/** The size of the file. */
size: number;
/** The content-type of the file. */
contentType: string;
/** Hash of the file contents. */
etag: string;
}
export class ServerContext {
#dev: boolean;
#routes: Route[];
@@ -144,7 +145,6 @@ export class ServerContext {
): Promise<ServerContext> {
// Get the manifest' base URL.
const baseUrl = new URL("./", manifest.baseUrl).href;
const defaultStaticDir = "./static";
const { config, path: configPath } = await readDenoConfig(
fromFileUrl(baseUrl),
@@ -182,9 +182,7 @@ export class ServerContext {
let app: AppModule = DEFAULT_APP;
const layouts: LayoutRoute[] = [];
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
let unknownModule: UnknownPageModule | null = null;
let error: ErrorPage = DEFAULT_ERROR;
let errorModule: ErrorPageModule | null = null;
const allRoutes = [
...Object.entries(manifest.routes),
...(opts.plugins ? getMiddlewareRoutesFromPlugins(opts.plugins) : []),
@@ -273,7 +271,6 @@ export class ServerContext {
path === "/_404.tsx" || path === "/_404.ts" ||
path === "/_404.jsx" || path === "/_404.js"
) {
unknownModule = module as UnknownPageModule;
const { default: component, config } = module as UnknownPageModule;
let { handler } = module as UnknownPageModule;
if (component && handler === undefined) {
@@ -292,7 +289,6 @@ export class ServerContext {
path === "/_500.tsx" || path === "/_500.ts" ||
path === "/_500.jsx" || path === "/_500.js"
) {
errorModule = module as ErrorPageModule;
const { default: component, config } = module as ErrorPageModule;
let { handler } = module as ErrorPageModule;
if (component && handler === undefined) {
@@ -338,7 +334,7 @@ export class ServerContext {
const staticFiles: StaticFile[] = [];
try {
const staticFolder = new URL(
opts.staticDir ?? defaultStaticDir,
opts.staticDir ?? "./static",
manifest.baseUrl,
);
const entries = walk(fromFileUrl(staticFolder), {
@@ -380,31 +376,6 @@ export class ServerContext {
const dev = isDevMode();
if (dev) {
const checks: CheckFunction[] = [
() => assertModuleExportsDefault(app, "_app"),
() => assertSingleModule(routes, "_app"),
() => assertModuleExportsDefault(unknownModule, "_404"),
() => assertSingleModule(routes, "_404"),
() => assertSingleRoutePattern(routes),
() => assertModuleExportsDefault(errorModule, "_500"),
() => assertSingleModule(routes, "_500"),
() => assertRoutesHaveHandlerOrComponent(routes),
() => assertStaticDirSafety(opts.staticDir ?? "", defaultStaticDir),
() => assertNoStaticRouteConflicts(routes, staticFiles),
() => assertPluginsCallRender(opts.plugins ?? []),
() => assertPluginsCallRenderAsync(opts.plugins ?? []),
() => assertPluginsInjectModules(opts.plugins ?? []),
];
const results = checks.flatMap((check) => check());
results.forEach((result) => {
console.log(`[${result.category}] ${result.message}`);
if (result.fileLink) {
console.log(`See: ${result.fileLink}`);
}
});
// Ensure that debugging hooks are set up for SSR rendering
await import("preact/debug");
}