deno.land / x / servest@v1.3.4 / responder.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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.import { ServerResponse } from "./server.ts";import { CookieSetter, cookieSetter } from "./cookie.ts";import { basename, extname } from "./vendor/https/deno.land/std/path/mod.ts";import { contentTypeByExt } from "./media_types.ts";/** Basic responder for http response */export interface Responder extends CookieSetter { /** * Respond to request * Error will be thrown if request has already been responded. * headers is merged with responseHeaders * */ respond(response: ServerResponse): Promise<void>;
/** * Send file as a response. Content-Type will be guessed but may not be found. * Default Content-Type is application/octet-stream * */ sendFile( path: string, opts?: { contentDisposition?: "inline" | "attachment"; headers?: Headers; }, ): Promise<void>;
/** Redirect request with 302 (Found ) */ redirect( url: string, resp?: Partial<ServerResponse>, ): Promise<void>;
/** Headers to be used for response */ readonly responseHeaders: Headers;
/** Mark as responded manually */ markAsResponded(status: number): void;
isResponded(): boolean;
respondedStatus(): number | undefined;}
/** create ServerResponder object */export function createResponder( onResponse: (resp: ServerResponse) => Promise<void>,): Responder { const responseHeaders = new Headers(); const cookie = cookieSetter(responseHeaders); let responseStatus: number | undefined; function isResponded() { return responseStatus !== undefined; } async function redirect( url: string, resp: Partial<ServerResponse> = {}, ) { let { status, headers, body, ...rest } = resp; status = status ?? 302; if (status < 300 || 400 <= status) { throw new Error("redirection status code must be 300 ~ 399"); } headers = headers ?? new Headers(); headers.set("location", url); await respond({ status, headers, body, ...rest }); } async function respond(response: ServerResponse): Promise<void> { if (isResponded()) { throw new Error("Request already responded"); } const { status, headers, body } = response; responseStatus = status; if (headers) { for (const [k, v] of headers.entries()) { responseHeaders.append(k, v); } } await onResponse({ status, headers: responseHeaders, body, }); } async function sendFile( path: string, opts?: { contentDisposition?: "inline" | "attachment"; headers?: Headers; }, ): Promise<void> { const body = await Deno.open(path); const headers = opts?.headers ?? new Headers(); try { const contentType = contentTypeByExt(extname(path)) ?? "application/octet-stream"; headers.set("content-type", contentType); const contentDisposition = opts?.contentDisposition; if (contentDisposition === "inline") { headers.set("content-disposition", contentDisposition); } else if (contentDisposition === "attachment") { const filename = basename(path); headers.set( "content-disposition", `attachment; filename="${filename}"`, ); } await onResponse({ status: 200, headers, body, }); } finally { body.close(); } } function markAsResponded(status: number) { responseStatus = status; } function respondedStatus() { return responseStatus; } return { respond, redirect, sendFile, isResponded, responseHeaders, respondedStatus, markAsResponded, ...cookie, };}
servest

Version Info

Tagged at
2 years ago