-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
publicFileResponse.mjs
88 lines (72 loc) · 2.46 KB
/
publicFileResponse.mjs
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
// @ts-check
import { Status, STATUS_TEXT } from "std/http/http_status.ts";
import { contentType } from "std/media_types/mod.ts";
import { extname } from "std/path/mod.ts";
import { readableStreamFromReader } from "std/streams/conversion.ts";
/**
* Creates a response for a public file request.
* @param {Request} request Request.
* @param {URL} publicDir Public directory file URL.
* @param {(
* request: Request,
* responseInit: import("./serve.mjs").ResponseInit
* ) => import("./serve.mjs").ResponseInit
* | Promise<import("./serve.mjs").ResponseInit
* >} [customizeResponseInit] Customizes the response init.
* @returns {Promise<Response>} Response that streams the public file.
*/
export default async function publicFileResponse(
request,
publicDir,
customizeResponseInit,
) {
if (!(request instanceof Request)) {
throw new TypeError("Argument 1 `request` must be a `Request` instance.");
}
if (!(publicDir instanceof URL)) {
throw new TypeError("Argument 2 `publicDir` must be a `URL` instance.");
}
if (!publicDir.href.endsWith("/")) {
throw new TypeError(
"Argument 2 `publicDir` must be a URL ending with `/`.",
);
}
if (
customizeResponseInit !== undefined &&
typeof customizeResponseInit !== "function"
) {
throw new TypeError(
"Argument 3 `customizeResponseInit` must be a function.",
);
}
const requestUrl = new URL(request.url);
const fileUrl = new URL("." + requestUrl.pathname, publicDir);
const file = await Deno.open(fileUrl);
try {
const { isFile } = await file.stat();
if (!isFile) throw new Deno.errors.NotFound();
const body = readableStreamFromReader(file);
/** @type {import("./serve.mjs").ResponseInit} */
let responseInit = {
status: Status.OK,
statusText: STATUS_TEXT[Status.OK],
headers: new Headers(),
};
const fileExtension = extname(fileUrl.pathname);
if (fileExtension) {
const contentTypeHeader = contentType(fileExtension);
if (contentTypeHeader) {
responseInit.headers.set("content-type", contentTypeHeader);
}
}
if (typeof customizeResponseInit === "function") {
responseInit = await customizeResponseInit(request, responseInit);
}
return new Response(body, responseInit);
} catch (error) {
// Avoid closing an already closed file, see:
// https://github.com/denoland/deno/issues/14210
if (file.rid in Deno.resources()) Deno.close(file.rid);
throw error;
}
}