-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
183 lines (169 loc) · 4.65 KB
/
mod.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
serve as _serve,
serveTls as _serveTls,
Status,
STATUS_TEXT,
} from "https://deno.land/std/http/mod.ts";
import * as _path from "https://deno.land/std/path/mod.ts";
import { walk } from "https://deno.land/std/fs/mod.ts";
import handlebars from "https://cdn.skypack.dev/handlebars";
import { lookup } from "https://deno.land/x/media_types/mod.ts";
import { prettyBytes } from "https://deno.land/x/pretty_bytes/mod.ts";
import { markdownToHTML } from "https://deno.land/x/md2html/mod.ts";
import { MatchHandler } from "https://crux.land/router@0.0.5";
type PathParams = Record<string, string> | undefined;
// type Handler = (
// request: Request,
// params: PathParams,
// ) => Response | Promise<Response>;
/** Serve static files or a directory.
*
* @example
* ```
* // a single file
* "/": serveStatic("./public/index.html"),
*
* // a directory of files
* "/public/:filename?": serveStatic("./public"),
* ```
*/
export function serveStatic(
path: string,
): MatchHandler {
return async (
request: Request,
params: Record<string, string> | undefined,
): Promise<Response> => {
try {
const fullPath = (params && params.filename)
? _path.join(path, params.filename)
: path;
const fileInfo = await Deno.stat(fullPath);
if (fileInfo.isDirectory) {
return serveDir(request, path);
} else {
const body = await Deno.readFile(fullPath);
const response = new Response(body);
const contentType = lookup(path);
if (contentType) {
response.headers.set("content-type", contentType);
}
return response;
}
} catch (error) {
if (error?.name == "NotFound") {
return json({ error: STATUS_TEXT.get(Status.NotFound) }, {
status: Status.NotFound,
});
}
return json({ error: STATUS_TEXT.get(Status.InternalServerError) }, {
status: Status.InternalServerError,
});
}
};
}
interface DirectoryEntry {
path: URL;
name: string;
size: string;
dateModified: string;
}
async function serveDir(request: Request, path: string) {
const directoryData: DirectoryEntry[] = [];
for await (const entry of walk(path, { includeDirs: false })) {
const fileInfo = await Deno.stat(entry.path);
const { size, mtime } = fileInfo;
directoryData.push({
path: new URL(entry.path, request.url),
name: entry.name,
size: prettyBytes(size),
dateModified: `${mtime}`,
});
}
const template = handlebars.compile(
await Deno.readTextFile("./dirlisting.html"),
);
const rendered = template({
directory: path,
files: directoryData,
});
const response = new Response(rendered);
response.headers.set("content-type", "text/html; charset=utf-8");
return response;
}
/** Serve a remote resource.
*
* @example
* ```
* "/todos/:id": serveRemote("https://jsonplaceholder.typicode.com/todos/:id"),
* ```
*/
export function serveRemote(
remoteURL: string,
): MatchHandler {
return (
request: Request,
_params: Record<string, string> | undefined,
): Promise<Response> => {
const { pathname } = new URL(request.url);
const url = new URL(pathname, remoteURL);
return fetch(url);
};
}
/** Serve a markdown file as github flavored html.
*
* @example
* ```
* serve(8000, {
* "/markdown": serveRemote("./README.md"),
* });
* ```
*/
export function serveMarkdown(
resource: string | URL,
): MatchHandler {
return async (
_request: Request,
_params: Record<string, string> | undefined,
): Promise<Response> => {
const response = new Response(await markdownToHTML(resource));
response.headers.set("content-type", "text/html; charset=utf-8");
return response;
};
}
/** Converts an object literal to a JSON string and returns
* a Response with `application / json` as the `content - type`.
*
* @example
* serve(8000, {
* "/": () => json({message: "hello world"}),
* })
* ```
*/
export function serveJson(
jsonObj: Parameters<typeof JSON.stringify>[0],
init?: ResponseInit,
): MatchHandler {
return (
_request: Request,
_params: Record<string, string> | undefined,
): Response => {
return json(jsonObj, init);
};
}
function json(
jsobj: Parameters<typeof JSON.stringify>[0],
init?: ResponseInit,
): Response {
const headers = init?.headers instanceof Headers
? init.headers
: new Headers(init?.headers);
if (!headers.has("Content-Type")) {
headers.set("Content-Type", "application/json; charset=utf-8");
}
return new Response(JSON.stringify(jsobj) + "\n", {
statusText: init?.statusText ?? STATUS_TEXT.get(init?.status ?? Status.OK),
status: init?.status ?? Status.OK,
headers,
});
}