major: import Fresh 2.0 code (#2449)

🎉
This commit is contained in:
Marvin Hagemeister
2024-05-24 09:40:08 +02:00
committed by GitHub
parent 990357629e
commit 287929e4d2
958 changed files with 15794 additions and 28287 deletions
+112
View File
@@ -0,0 +1,112 @@
import { FreshReqContext } from "./context.ts";
import type { MiddlewareFn } from "./middlewares/mod.ts";
import type { FsAdapter } from "./fs.ts";
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
import type { ResolvedFreshConfig } from "./config.ts";
import type { WalkEntry } from "@std/fs/walk";
import { DEFAULT_CONN_INFO } from "./app.ts";
const STUB = {} as unknown as Deno.ServeHandlerInfo;
export class FakeServer {
constructor(
public handler: (
req: Request,
info: Deno.ServeHandlerInfo,
) => Response | Promise<Response>,
) {}
async get(path: string, init?: RequestInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, init);
return await this.handler(req, STUB);
}
async post(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "post", body });
return await this.handler(req, STUB);
}
async patch(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "patch", body });
return await this.handler(req, STUB);
}
async put(path: string, body?: BodyInit): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "put", body });
return await this.handler(req, STUB);
}
async delete(path: string): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "delete" });
return await this.handler(req, STUB);
}
async head(path: string): Promise<Response> {
const url = this.toUrl(path);
const req = new Request(url, { method: "head" });
return await this.handler(req, STUB);
}
private toUrl(path: string) {
return new URL(path, "http://localhost/");
}
}
const DEFAULT_CONFIG: ResolvedFreshConfig = {
build: {
outDir: "",
},
mode: "production",
basePath: "",
root: "",
staticDir: "",
};
export function serveMiddleware<T>(
middleware: MiddlewareFn<T>,
options: { config?: ResolvedFreshConfig; buildCache?: BuildCache } = {},
): FakeServer {
return new FakeServer(async (req) => {
const next = () => new Response("not found", { status: 404 });
const config = options.config ?? DEFAULT_CONFIG;
const buildCache = options.buildCache ??
new ProdBuildCache(config, new Map(), new Map(), true);
const ctx = new FreshReqContext<T>(
req,
config,
() => Promise.resolve(next()),
new Map(),
buildCache,
DEFAULT_CONN_INFO,
);
return await middleware(ctx);
});
}
export function createFakeFs(files: Record<string, unknown>): FsAdapter {
return {
async *walk(_root) {
// FIXME: ignore
for (const file of Object.keys(files)) {
const entry: WalkEntry = {
isDirectory: false,
isFile: true,
isSymlink: false,
name: file, // FIXME?
path: file,
};
yield entry;
}
},
// deno-lint-ignore require-await
async isDirectory(dir) {
return Object.keys(files).some((file) => file.startsWith(dir + "/"));
},
async mkdirp(_dir: string) {
},
readFile: Deno.readFile,
};
}
export const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));