-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathresponses.ts
182 lines (148 loc) · 4.56 KB
/
responses.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
import type { ByteRangeContext } from './byte-range-context'
import type { SupportedBodyTypes } from '../types.js'
import type { Logger } from '@libp2p/interface'
function setField (response: Response, name: string, value: string | boolean): void {
Object.defineProperty(response, name, {
enumerable: true,
configurable: false,
set: () => {},
get: () => value
})
}
function setType (response: Response, value: 'basic' | 'cors' | 'error' | 'opaque' | 'opaqueredirect'): void {
setField(response, 'type', value)
}
function setUrl (response: Response, value: string): void {
setField(response, 'url', value)
}
function setRedirected (response: Response): void {
setField(response, 'redirected', true)
}
export interface ResponseOptions extends ResponseInit {
redirected?: boolean
}
export function okResponse (url: string, body?: SupportedBodyTypes, init?: ResponseOptions): Response {
const response = new Response(body, {
...(init ?? {}),
status: 200,
statusText: 'OK'
})
if (init?.redirected === true) {
setRedirected(response)
}
setType(response, 'basic')
setUrl(response, url)
response.headers.set('Accept-Ranges', 'bytes')
return response
}
export function badGatewayResponse (url: string, body?: SupportedBodyTypes, init?: ResponseInit): Response {
const response = new Response(body, {
...(init ?? {}),
status: 502,
statusText: 'Bad Gateway'
})
setType(response, 'basic')
setUrl(response, url)
return response
}
export function notSupportedResponse (url: string, body?: SupportedBodyTypes, init?: ResponseInit): Response {
const response = new Response(body, {
...(init ?? {}),
status: 501,
statusText: 'Not Implemented'
})
response.headers.set('X-Content-Type-Options', 'nosniff') // see https://specs.ipfs.tech/http-gateways/path-gateway/#x-content-type-options-response-header
setType(response, 'basic')
setUrl(response, url)
return response
}
export function notAcceptableResponse (url: string, body?: SupportedBodyTypes, init?: ResponseInit): Response {
const response = new Response(body, {
...(init ?? {}),
status: 406,
statusText: 'Not Acceptable'
})
setType(response, 'basic')
setUrl(response, url)
return response
}
/**
* if body is an Error, it will be converted to a string containing the error message.
*/
export function badRequestResponse (url: string, body?: SupportedBodyTypes | Error, init?: ResponseInit): Response {
if (body instanceof Error) {
body = body.message
}
const response = new Response(body, {
...(init ?? {}),
status: 400,
statusText: 'Bad Request'
})
setType(response, 'basic')
setUrl(response, url)
return response
}
export function movedPermanentlyResponse (url: string, location: string, init?: ResponseInit): Response {
const response = new Response(null, {
...(init ?? {}),
status: 301,
statusText: 'Moved Permanently',
headers: {
...(init?.headers ?? {}),
location
}
})
setType(response, 'basic')
setUrl(response, url)
return response
}
interface RangeOptions {
byteRangeContext: ByteRangeContext
log?: Logger
}
export function okRangeResponse (url: string, body: SupportedBodyTypes, { byteRangeContext, log }: RangeOptions, init?: ResponseOptions): Response {
if (!byteRangeContext.isRangeRequest) {
return okResponse(url, body, init)
}
if (!byteRangeContext.isValidRangeRequest) {
return badRangeResponse(url, body, init)
}
let response: Response
try {
response = new Response(body, {
...(init ?? {}),
status: 206,
statusText: 'Partial Content',
headers: {
...(init?.headers ?? {}),
'content-range': byteRangeContext.contentRangeHeaderValue
}
})
} catch (e: any) {
log?.error('failed to create range response', e)
return badRangeResponse(url, body, init)
}
if (init?.redirected === true) {
setRedirected(response)
}
setType(response, 'basic')
setUrl(response, url)
response.headers.set('Accept-Ranges', 'bytes')
return response
}
/**
* We likely need to catch errors handled by upstream helia libraries if range-request throws an error. Some examples:
* * The range is out of bounds
* * The range is invalid
* * The range is not supported for the given type
*/
export function badRangeResponse (url: string, body?: SupportedBodyTypes, init?: ResponseInit): Response {
const response = new Response(body, {
...(init ?? {}),
status: 416,
statusText: 'Requested Range Not Satisfiable'
})
setType(response, 'basic')
setUrl(response, url)
return response
}