deno.land / x / servest@v1.3.4 / body_parser.ts

body_parser.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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.import { MultipartFormData, MultipartReader,} from "./vendor/https/deno.land/std/mime/multipart.ts";import { decode } from "./_util.ts";import { Buffer } from "./vendor/https/deno.land/std/io/buffer.ts";
import Reader = Deno.Reader;export interface BodyParser { text(): Promise<string>; json(): Promise<any>; arrayBuffer(): Promise<Uint8Array>; formData(): Promise<MultipartFormData>;}
export function createBodyParser(holder: { readonly reader: Deno.Reader; readonly contentType: string; readonly maxMemory?: number;}): BodyParser { let bodyBuf: Buffer | undefined; let formBody: MultipartFormData | undefined; let textBody: string | undefined; let jsonBody: any | undefined; async function formDataInternal( contentType: string, body: Reader, maxMemory?: number, ): Promise<MultipartFormData> { const headers = new Headers({ "content-type": contentType, }); if (contentType.match(/^multipart\/form-data/)) { return parserMultipartRequest({ headers, body }, maxMemory); } else if (contentType.match(/^application\/x-www-form-urlencoded/)) { return parseUrlEncodedForm({ headers, body, }); } else { throw new Error( "request is not multipart/form-data nor application/x-www-form-urlencoded", ); } } async function formData(): Promise<MultipartFormData> { const { contentType, reader, maxMemory } = holder; if (formBody) { return formBody; } else if (bodyBuf) { return (formBody = await formDataInternal( contentType, bodyBuf, maxMemory, )); } return (formBody = await formDataInternal( contentType, reader, maxMemory, )); }
async function json<T>(): Promise<T> { if (jsonBody) { return jsonBody as T; } else if (bodyBuf) { return (jsonBody = JSON.parse(decode(bodyBuf.bytes()))); } bodyBuf = new Buffer(); await Deno.copy(holder.reader, bodyBuf); return JSON.parse(decode(bodyBuf.bytes())); }
async function text(): Promise<string> { if (textBody) { return textBody; } else if (bodyBuf) { return (textBody = decode(bodyBuf.bytes())); } bodyBuf = new Buffer(); await Deno.copy(holder.reader, bodyBuf); return (textBody = decode(bodyBuf.bytes())); }
async function arrayBuffer(): Promise<Uint8Array> { if (bodyBuf) { return bodyBuf.bytes(); } bodyBuf = new Buffer(); await Deno.copy(holder.reader, bodyBuf); return bodyBuf.bytes(); } return { json, text, formData, arrayBuffer };}
/** * Parse multipart/form-data request * @param req ServerRequest * @param maxMemory maximum memory size for file part. * Small file will be stored in memory, Big file in tempfile */export async function parserMultipartRequest( req: { headers: Headers; body?: Reader }, maxMemory: number = 1 << 10, // 10MB by default): Promise<MultipartFormData> { // Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryO5quBRiT4G7Vm3R7 const contentType = req.headers.get("content-type"); if (!req.body) { throw new Error("request has no body"); } if (!contentType || !contentType.match("multipart/form-data")) { throw new Error("is not multipart request"); } let m = contentType.match(/boundary=([^ ]+?)$/); if (!m) { throw new Error("doesn't have boundary"); } const boundary = m[1]; const reader = new MultipartReader(req.body, boundary); return reader.readForm(maxMemory);}/** * Parse application/x-www-form-urlencoded request * @param req part of ServerRequest */export async function parseUrlEncodedForm(req: { headers: Headers; body?: Reader;}): Promise<MultipartFormData> { const contentType = req.headers.get("content-type"); if (!req.body) { throw new Error("request has no body"); } if ( !contentType || !contentType.match(/^application\/x-www-form-urlencoded/) ) { throw new Error("is not form urlencoded request"); } const buf = new Buffer(); await Deno.copy(req.body, buf); const params = new URLSearchParams(decodeURIComponent(decode(buf.bytes()))); function* entries() { for (const i of params.entries()) { yield i; } } return { value(f: string) { return params.get(f) ?? undefined; }, entries, [Symbol.iterator]() { return entries(); }, file(f: string) { return undefined; }, async removeAll() { }, };}
servest

Version Info

Tagged at
2 years ago