|
| 1 | +import { relative as relativePath } from "https://deno.land/std@0.190.0/path/mod.ts"; |
| 2 | +import { Globals } from "../global.ts"; |
| 3 | +import { stripPrefix } from "../utils.ts"; |
| 4 | +import { log, log_error } from "../log.ts"; |
| 5 | +import { HTTPRequest, HTTPResponse } from "../runtime.ts"; |
| 6 | +import type { CompileOptions, EntryController } from "../types.ts"; |
| 7 | +import { BaseServer, BaseServerOptions } from "./BaseServer.ts"; |
| 8 | +import { join } from "https://deno.land/std@0.190.0/path/posix.ts"; |
| 9 | + |
| 10 | +type ControllerResolved = { |
| 11 | + middlewares: Array<string>; |
| 12 | + fallbacks: Array<string>; |
| 13 | + controller: string; |
| 14 | +}; |
| 15 | +type ManifestResolver = (req: HTTPRequest) => ControllerResolved | null; |
| 16 | + |
| 17 | +export class DevServer extends BaseServer { |
| 18 | + lastId = 0; |
| 19 | + waitingRequests = new Map<number, Deno.RequestEvent>(); |
| 20 | + |
| 21 | + manifestResolver: ManifestResolver | null = null; |
| 22 | + cache = new Map<string, EntryController>(); |
| 23 | + |
| 24 | + constructor( |
| 25 | + options: BaseServerOptions, |
| 26 | + compileOptions: CompileOptions, |
| 27 | + ) { |
| 28 | + super(options); |
| 29 | + if (compileOptions.viewsPath) { |
| 30 | + HTTPResponse.viewsPath = join(Globals.cwd, "/.densky/views"); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + importWithoutCache(url: string): Promise<unknown> { |
| 35 | + log("./" + stripPrefix(url, Globals.cwd), "CACHE", "import"); |
| 36 | + return import( |
| 37 | + "file://" + url + "?k=" + |
| 38 | + (Math.random() * 16000 | 0).toString(32) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + async importController(url: string): Promise<EntryController | null> { |
| 43 | + if (this.cache.has(url)) { |
| 44 | + return this.cache.get(url)!; |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + const controller = await this.importWithoutCache(url) as EntryController; |
| 49 | + this.cache.set(url, controller); |
| 50 | + |
| 51 | + return controller; |
| 52 | + } catch (e) { |
| 53 | + log_error(e); |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async handleRequest(req: HTTPRequest) { |
| 59 | + if (!this.manifestResolver) { |
| 60 | + type ManifestResolverModule = { default: ManifestResolver }; |
| 61 | + |
| 62 | + this.manifestResolver = await this.importWithoutCache( |
| 63 | + join(Globals.cwd, ".densky/manifest.ts"), |
| 64 | + ).then((m) => (m as ManifestResolverModule).default); |
| 65 | + } |
| 66 | + |
| 67 | + if (req.pathname === "/$/dev") { |
| 68 | + this.manifestResolver = null; |
| 69 | + for (const entry of await req.raw.json()) { |
| 70 | + this.cache.delete(entry[1]); |
| 71 | + log( |
| 72 | + relativePath(Globals.cwd, entry[1]), |
| 73 | + "WATCHER", |
| 74 | + entry[0].toUpperCase(), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return (new Response("Updated!", { status: 200 })); |
| 79 | + } |
| 80 | + |
| 81 | + const resolvedController = this.manifestResolver!(req); |
| 82 | + if (resolvedController == null) { |
| 83 | + return new Response("Not Found", { status: 404 }); |
| 84 | + } |
| 85 | + |
| 86 | + for (const middleware of resolvedController.middlewares) { |
| 87 | + const out = await this.handleController(middleware, req); |
| 88 | + if (out) return out; |
| 89 | + } |
| 90 | + |
| 91 | + for (const fallback of resolvedController.fallbacks) { |
| 92 | + const out = await this.handleController(fallback, req); |
| 93 | + if (out) return out; |
| 94 | + } |
| 95 | + |
| 96 | + const out = await this.handleController(resolvedController.controller, req); |
| 97 | + if (out) return out; |
| 98 | + |
| 99 | + return new Response("Not found", { status: 404 }); |
| 100 | + } |
| 101 | + |
| 102 | + async handleController( |
| 103 | + path: string, |
| 104 | + req: HTTPRequest, |
| 105 | + ): Promise<Response | null> { |
| 106 | + const controller = await this.importController(path); |
| 107 | + if (!controller) { |
| 108 | + return new Response("Controller doesn't exist: " + path, { |
| 109 | + status: 500, |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + const entry = controller[req.method as keyof EntryController] ?? |
| 114 | + controller.default; |
| 115 | + if (!entry) return new Response("Method not implemented", { status: 402 }); |
| 116 | + |
| 117 | + await req.prepare(); |
| 118 | + const out = await entry(req); |
| 119 | + if (out == null) return null; |
| 120 | + |
| 121 | + return HTTPResponse.toResponse(req, out); |
| 122 | + } |
| 123 | +} |
0 commit comments