This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse_context.ts
153 lines (133 loc) · 3.02 KB
/
response_context.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
// Copyright 2023 Samuel Kopp. All rights reserved. Apache-2.0 license.
import { Payload } from './handler.ts'
export class ResponseContext {
#i
constructor(__internal: {
b: Exclude<Payload, void> | null
c: number
h: Headers
}) {
this.#i = __internal
}
get body(): Exclude<Payload, void> | null {
return this.#i.b
}
set body(data: Exclude<Payload, void> | null) {
this.#i.b = data
}
/**
* The size of the response body (`-1` if it cannot be calculated).
*/
get bodySize() {
if (
this.#i.b === null
) {
return 0
}
let s
switch (this.#i.b.constructor.name) {
case 'String': {
s = (this.#i.b as string).length
break
}
case 'Object': {
s = JSON.stringify(this.#i.b).length
break
}
case 'ArrayBuffer': {
s = (this.#i.b as ArrayBuffer).byteLength
break
}
case 'Uint8Array': {
s = (this.#i.b as ArrayBuffer).byteLength
break
}
case 'Blob': {
s = (this.#i.b as Blob).size
break
}
default: {
s = -1
break
}
}
return s
}
/**
* The status code of the response.
*/
get code() {
return this.#i.c
}
set code(code: number) {
this.#i.c = code
}
/**
* Attach a cookie to the response.
*
* @since v1.4
*/
setCookie(name: string, value: string, options?: {
expires?: Date
maxAge?: number
domain?: string
path?: string
secure?: boolean
httpOnly?: boolean
sameSite?:
| 'strict'
| 'lax'
| 'none'
}) {
let cookie = `${name}=${value};`
this.#i.h.append(
'Set-Cookie',
(
options?.expires &&
(cookie += ` Expires=${options.expires.toUTCString()};`),
options?.maxAge && (cookie += ` Max-Age=${options.maxAge};`),
options?.domain && (cookie += ` Domain=${options.domain};`),
options?.path && (cookie += ` Path=${options.path};`),
options?.secure && (cookie += ' Secure;'),
options?.httpOnly && (cookie += ' HttpOnly;'),
options?.sameSite &&
(cookie += ` SameSite=${
options.sameSite.charAt(0).toUpperCase() +
options.sameSite.slice(1)
};`),
cookie
),
)
}
/**
* Set an empty `Set-Cookie` header to delete the cookie.
*
* @since v1.4
*/
deleteCookie(name: string, options?: { path?: string; domain?: string }) {
this.setCookie(name, '', {
expires: new Date(0),
...options,
})
}
/**
* Attach a header to the response.
*/
header(name: string, value: string | undefined) {
if (value === undefined) {
this.#i.h.delete(name)
} else {
this.#i.h.set(name, value)
}
}
/**
* Redirect the incoming request.
*
* @param destination e.g. https://google.com
* @param code e.g. 301, default 307
*/
redirect(destination: string, code = 307) {
this.#i.c = code
this.#i.h.set('location', destination)
}
}