deno.land / x / lume@v2.1.4 / core / server.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/// <reference lib="deno.unstable" />// Deno.Server.shutdown() is unstable
import { posix } from "../deps/path.ts";import Events from "./events.ts";import { serveFile as HttpServeFile } from "../deps/http.ts";
import type { Event, EventListener, EventOptions } from "./events.ts";
/** The options to configure the local server */export interface Options extends Deno.ServeOptions { /** The root path */ root: string;}
export const defaults: Options = { root: `${Deno.cwd()}/_site`, port: 8000,};
export type RequestHandler = (req: Request) => Promise<Response>;export type Middleware = ( req: Request, next: RequestHandler, info: Deno.ServeHandlerInfo,) => Promise<Response>;
/** Custom events for server */export interface ServerEvent extends Event { /** The event type */ type: ServerEventType;
/** The request object */ request?: Request;
/** The error object (only for "error" events) */ error?: Error;}
/** The available event types */export type ServerEventType = | "start" | "error";
export default class Server { events: Events<ServerEvent> = new Events<ServerEvent>(); options: Options; middlewares: Middleware[] = []; #server?: Deno.HttpServer;
constructor(options: Partial<Options> = {}) { this.options = { ...defaults, ...options }; }
/** Register one or more middlewares */ use(...middleware: Middleware[]) { this.middlewares.push(...middleware); return this; }
/** Add a listener to an event */ addEventListener( type: ServerEventType, listener: EventListener<ServerEvent>, options?: EventOptions, ) { this.events.addEventListener(type, listener, options); return this; }
/** Dispatch an event */ dispatchEvent(event: ServerEvent) { return this.events.dispatchEvent(event); }
/** Start the server */ start(signal?: Deno.ServeOptions["signal"]) { this.#server = Deno.serve({ ...this.options, signal, onListen: () => this.dispatchEvent({ type: "start" }), }, this.handle.bind(this)); }
/** Stops the server */ stop() { try { this.#server?.shutdown(); } catch (error) { this.dispatchEvent({ type: "error", error, }); } }
/** Handle a http request event */ async handle( request: Request, info: Deno.ServeHandlerInfo, ): Promise<Response> { const middlewares = [...this.middlewares];
const next: RequestHandler = async ( request: Request, ): Promise<Response> => { const middleware = middlewares.shift();
if (middleware) { return await middleware(request, next, info); }
return await serveFile(this.options.root, request); };
return await next(request); }}
/** Server a static file */export async function serveFile( root: string, request: Request,): Promise<Response> { const url = new URL(request.url); const pathname = decodeURIComponent(url.pathname); const path = posix.join(root, pathname);
try { const file = path.endsWith("/") ? path + "index.html" : path;
// Redirect /example to /example/ const info = await Deno.stat(file);
if (info.isDirectory) { return new Response(null, { status: 301, headers: { location: posix.join(pathname, "/"), }, }); }
// Serve the static file return await HttpServeFile(request, file); } catch { try { // Exists a HTML file with this name? if (!posix.extname(path)) { return await HttpServeFile(request, path + ".html"); } } catch { // Continue }
return new Response( "Not found", { status: 404 }, ); }}
lume

Version Info

Tagged at
7 months ago