deno.land / x / servest@v1.3.4 / serve_static.ts

serve_static.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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.import * as path from "./vendor/https/deno.land/std/path/mod.ts";import { resolveIndexPath } from "./_matcher.ts";import { ServeHandler } from "./server.ts";import { contentTypeByExt } from "./media_types.ts";import { toIMF } from "./vendor/https/deno.land/std/datetime/mod.ts";
export interface ServeStaticOptions { /** * Custom Content-Type mapper. * .ext -> application/some-type * By default, .ts/.tsx will be resolved by application/typescript. */ contentTypeMap?: Map<string, string>; /** * Custom Content-Disposition mapper. * .ext -> "inline" | "attachment" * By default, Content-Disposition header won't be set for any files. */ contentDispositionMap?: Map<string, "inline" | "attachment">; /** Custom filter function for files */ filter?: (file: string) => boolean | Promise<boolean>; /** * Delactives for Cache-Control header * No value will be sent by default. * */ cacheControl?: CacheControlOptions; /** Value for Expires header */ expires?: Date;}
export interface CacheControlOptions { // public: default none public?: boolean; // private: default none private?: boolean; // max-age=<sec>: default: 0 maxAge?: number; // s-maxage: default none sMaxAge?: number; // no-cache: default none noCache?: boolean; // no-store: default none noStore?: boolean; // no-transform: default none noTransform?: boolean; // must-revalidate: default none mustRevalidate?: boolean; // proxy-revalidate: default none proxyRevalidate?: boolean;}
/** * Serve static files in specified directory. * */
export function serveStatic( dir: string, opts: ServeStaticOptions = {},): ServeHandler { const contentTypeMap = new Map<string, string>([ ...(opts.contentTypeMap || new Map<string, string>()).entries(), ]); const contentDispositionMap = opts.contentDispositionMap || new Map([]); const filter = opts.filter || (() => true); return async function serveStatic(req) { if (req.method === "GET" || req.method === "HEAD") { const filepath = await resolveIndexPath( dir, decodeURIComponent(req.path), ); if (!filepath || !(await filter(filepath))) { return; } const stat = await Deno.stat(filepath); const ext = path.extname(filepath); const base = path.basename(filepath); let contentType = contentTypeMap.get(ext) || contentTypeByExt(ext) || "application/octet-stream"; const headers = new Headers({ "content-length": stat.size + "", "content-type": contentType, }); const contentDisposition = contentDispositionMap.get(ext); if (contentDisposition === "attachment") { headers.set("content-disposition", `attachment; filename="${base}"`); } else if (contentDisposition === "inline") { headers.set("content-disposition", "inline"); } if (opts.cacheControl) { const val = buildCacheControlHeader(opts.cacheControl); if (val) { headers.set("cache-control", val); } } if (opts.expires) { headers.set("expires", toIMF(opts.expires)); } if (req.method === "HEAD") { return req.respond({ status: 200, headers, }); } else { const file = await Deno.open(filepath, { read: true }); try { await req.respond({ status: 200, headers, body: file }); } finally { file.close(); } } } };}
export function buildCacheControlHeader(opts: CacheControlOptions): string { let ret: string[] = []; if (opts.public) { ret.push("public"); } if (opts.private) { ret.push("private"); } if (opts.noCache) { ret.push("no-cache"); } if (opts.noStore) { ret.push("no-store"); } if (opts.maxAge != null) { ret.push("max-age=" + opts.maxAge); } if (opts.sMaxAge != null) { ret.push("s-maxage=" + opts.sMaxAge); } if (opts.mustRevalidate) { ret.push("must-revalidate"); } if (opts.proxyRevalidate) { ret.push("proxy-revalidate"); } if (opts.noTransform) { ret.push("no-transform"); } return ret.join(", ");}
servest

Version Info

Tagged at
2 years ago