1 |
|
2 | import type { Awaitable } from "./utils/common-types.js";
|
3 | import { combine, Context } from "./context.js";
|
4 | import { accepts, Accepted } from './content-negotiation.js'
|
5 | import { payloadTooLarge } from '@worker-tools/response-creators'
|
6 |
|
7 | export const JSON = 'application/json';
|
8 | export const FORM = 'application/x-www-form-urlencoded'
|
9 | export const FORM_DATA = 'multipart/form-data';
|
10 | export const TEXT_HTML = 'text/html'
|
11 | export const TEXT_PLAIN = 'text/plain'
|
12 |
|
13 | export const BINARY = 'application/octet-stream';
|
14 |
|
15 | export const X_BINARY = 'application/x-binary';
|
16 |
|
17 | export interface BodyParserOptions<J> {
|
18 | defaultJSON?: J,
|
19 | maxSize?: number
|
20 | }
|
21 |
|
22 | export type BodyParsable =
|
23 | | typeof FORM
|
24 | | typeof FORM_DATA
|
25 | | typeof JSON
|
26 | | typeof BINARY
|
27 | | typeof X_BINARY
|
28 | | `application/${string}+json`
|
29 | | `text/${string}`
|
30 |
|
31 | const defaultBody: BodyParsable[] = [
|
32 | FORM,
|
33 | FORM_DATA,
|
34 | JSON,
|
35 | BINARY,
|
36 | X_BINARY,
|
37 | TEXT_HTML,
|
38 | TEXT_PLAIN,
|
39 | ]
|
40 |
|
41 | export interface BodyJSONContext<J = any> {
|
42 | accepted: typeof JSON,
|
43 | body: J
|
44 | json: J
|
45 | }
|
46 |
|
47 | export interface BodyVendorJSONContext<J = any> {
|
48 | accepted: `application/${string}+json`,
|
49 | body: J
|
50 | json: J
|
51 | }
|
52 |
|
53 | export interface BodyFormContext {
|
54 | accepted: typeof FORM,
|
55 | body: URLSearchParams,
|
56 | form: URLSearchParams,
|
57 |
|
58 | }
|
59 |
|
60 | export interface BodyFormDataContext {
|
61 | accepted: typeof FORM_DATA,
|
62 | body: FormData
|
63 | formData: FormData
|
64 |
|
65 |
|
66 | }
|
67 |
|
68 | export interface BodyBinaryContext {
|
69 | accepted: typeof BINARY | typeof X_BINARY
|
70 | body: ArrayBuffer,
|
71 | arrayBuffer: ArrayBuffer,
|
72 | blob: Blob,
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | export interface BodyTextContext {
|
83 | accepted: `text/${string}`,
|
84 | body: string,
|
85 | text: string,
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | export type BodyContext<J> =
|
96 | | BodyJSONContext<J>
|
97 | | BodyBinaryContext
|
98 | | BodyFormContext
|
99 | | BodyFormDataContext
|
100 | | BodyTextContext
|
101 | | BodyVendorJSONContext<J>
|
102 |
|
103 |
|
104 | const _isString = (x: [string, FormDataEntryValue]): x is [string, string] => !(x[1] instanceof File)
|
105 | const _isFile = (x: [string, FormDataEntryValue]): x is [string, File] => x[1] instanceof File
|
106 |
|
107 | export type BodyParserDeps = Context & Accepted<BodyParsable>
|
108 |
|
109 | const isBodyTextContext = <J = any>(nx: BodyContext<J>): nx is BodyTextContext =>
|
110 | nx.accepted?.startsWith('text/')
|
111 |
|
112 | const isBodyVendorJSONContext = <J = any>(nx: BodyContext<J>): nx is BodyVendorJSONContext<J> =>
|
113 | nx.accepted?.startsWith('application/') && nx.accepted.endsWith('+json')
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | const MB = 1024**2
|
119 |
|
120 | async function checkSize(req: Request, maxSize: number) {
|
121 | let size = 0;
|
122 | await req.clone().body!.pipeTo(
|
123 | new WritableStream({
|
124 | write(chunk, ctrl) {
|
125 | size += chunk.byteLength
|
126 | if (size > maxSize) {
|
127 | ctrl.error(new Error('Payload too large'))
|
128 | }
|
129 | }
|
130 | }))
|
131 | return size <= maxSize
|
132 | }
|
133 |
|
134 | export const bodyParser = <J = any>(
|
135 | opts: BodyParserOptions<J> = {},
|
136 | ) => async <X extends BodyParserDeps>(
|
137 | ax: Awaitable<X>
|
138 | ): Promise<X & BodyContext<J>> => {
|
139 | const x = await ax;
|
140 | const nx = x as X & BodyContext<J>;
|
141 |
|
142 | const ok = await checkSize(x.request, opts.maxSize ?? 1 * MB)
|
143 | if (!ok) throw payloadTooLarge()
|
144 |
|
145 | switch (nx.accepted) {
|
146 | case JSON: {
|
147 | nx.body = nx.json = await x.request.json()
|
148 | return nx;
|
149 | }
|
150 | case FORM: {
|
151 | nx.body = nx.form = new URLSearchParams(await x.request.text())
|
152 |
|
153 |
|
154 | return nx;
|
155 | }
|
156 | case FORM_DATA: {
|
157 | nx.body = nx.formData = await x.request.formData();
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | return nx;
|
163 | }
|
164 | case BINARY:
|
165 | case X_BINARY: {
|
166 | nx.body = nx.arrayBuffer = await x.request.arrayBuffer();
|
167 | nx.blob = new Blob([nx.arrayBuffer])
|
168 | return nx;
|
169 | }
|
170 | default: {
|
171 | if (isBodyTextContext(nx)) {
|
172 | nx.body = nx.text = await x.request.text();
|
173 | } else if (isBodyVendorJSONContext(nx)) {
|
174 | nx.body = nx.json = await x.request.json()
|
175 | return nx;
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | } else {
|
181 |
|
182 | (<any>nx).body = (<any>nx).buffer = await x.request.arrayBuffer();
|
183 | (<any>nx).blob = new Blob([(<any>nx).buffer])
|
184 | return nx;
|
185 | }
|
186 | return nx;
|
187 | }
|
188 | }
|
189 | }
|
190 |
|
191 | export const defaultBodyParser = <J = any>(options?: BodyParserOptions<J>) =>
|
192 | combine(accepts(defaultBody), bodyParser(options));
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|