@vesk/plugin-multipart 0.2.69 → 0.2.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +201 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/parser.d.ts +114 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +502 -0
- package/package.json +7 -3
- package/src/index.ts +0 -125
- package/src/parser.test.ts +0 -263
- package/src/parser.ts +0 -479
- package/tsconfig.build.json +0 -12
- package/tsconfig.json +0 -8
package/dist/parser.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming `multipart/form-data` parser for Vesk.
|
|
3
|
+
*
|
|
4
|
+
* Why this exists instead of `await request.formData()`:
|
|
5
|
+
*
|
|
6
|
+
* 1. **It buffered the whole body in memory.** The core servers cap request
|
|
7
|
+
* bodies at 1 MiB (`DEFAULT_MAX_BODY_BYTES`) and hand the buffer to
|
|
8
|
+
* undici, which then re-parses it. A 40 MB upload could not be accepted
|
|
9
|
+
* at all, and the process held every byte before rejecting anything.
|
|
10
|
+
* 2. **`Object.fromEntries(fd.entries())` destroys files.** A `File` value
|
|
11
|
+
* collapses to an opaque string, so `useBody()` / `withValidation()` could
|
|
12
|
+
* never see a real upload, and nothing could enforce type or size.
|
|
13
|
+
* 3. **No limits could be applied per field.** Validation had to happen after
|
|
14
|
+
* the entire body was resident.
|
|
15
|
+
*
|
|
16
|
+
* This parser consumes the request stream incrementally and enforces the
|
|
17
|
+
* limits as bytes arrive, so an over-limit upload is aborted after the first
|
|
18
|
+
* few KB rather than after buffering all of it.
|
|
19
|
+
*
|
|
20
|
+
* No regex is used for parsing (see the repo's no-regex rule): this is a byte
|
|
21
|
+
* state machine over an incremental buffer.
|
|
22
|
+
*/
|
|
23
|
+
/** Thrown for any rejection. `status` maps onto an HTTP response. */
|
|
24
|
+
export class MultipartError extends Error {
|
|
25
|
+
status;
|
|
26
|
+
code;
|
|
27
|
+
constructor(message, code, status) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = 'MultipartError';
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.status = status;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const DASH = 0x2d; // '-'
|
|
35
|
+
const CR = 0x0d;
|
|
36
|
+
const LF = 0x0a;
|
|
37
|
+
const SPACE = 0x20;
|
|
38
|
+
const TAB = 0x09;
|
|
39
|
+
const QUOTE = '"';
|
|
40
|
+
const SEMI = 0x3b;
|
|
41
|
+
const ZERO = 0x30;
|
|
42
|
+
const encoder = new TextEncoder();
|
|
43
|
+
const decoder = new TextDecoder();
|
|
44
|
+
function lower(s) {
|
|
45
|
+
let out = '';
|
|
46
|
+
for (let i = 0; i < s.length; i++)
|
|
47
|
+
out += s[i].toLowerCase();
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
function indexOfFrom(haystack, needles, from) {
|
|
51
|
+
const maxLen = needles.reduce((m, n) => (n.length > m ? n.length : m), 0);
|
|
52
|
+
outer: for (let i = from; i <= haystack.length - maxLen; i++) {
|
|
53
|
+
for (const needle of needles) {
|
|
54
|
+
if (i + needle.length > haystack.length)
|
|
55
|
+
continue;
|
|
56
|
+
for (let k = 0; k < needle.length; k++) {
|
|
57
|
+
if (haystack[i + k] !== needle[k])
|
|
58
|
+
continue outer;
|
|
59
|
+
}
|
|
60
|
+
return i;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return -1;
|
|
64
|
+
}
|
|
65
|
+
function parseContentDisposition(value) {
|
|
66
|
+
// name="x"; filename="y" — attribute walk, no regex.
|
|
67
|
+
let name = '';
|
|
68
|
+
let filename;
|
|
69
|
+
let i = 0;
|
|
70
|
+
while (i < value.length) {
|
|
71
|
+
while (i < value.length && (value[i] === ' ' || value[i] === ';' || value[i] === '\t'))
|
|
72
|
+
i++;
|
|
73
|
+
let key = '';
|
|
74
|
+
while (i < value.length && value[i] !== '=' && value[i] !== ';')
|
|
75
|
+
key += value[i++];
|
|
76
|
+
key = lower(key.trim());
|
|
77
|
+
let val = '';
|
|
78
|
+
if (value[i] === '=') {
|
|
79
|
+
i++;
|
|
80
|
+
if (value[i] === QUOTE) {
|
|
81
|
+
i++;
|
|
82
|
+
while (i < value.length && value[i] !== QUOTE)
|
|
83
|
+
val += value[i++];
|
|
84
|
+
i++; // closing quote
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
while (i < value.length && value[i] !== ';')
|
|
88
|
+
val += value[i++];
|
|
89
|
+
val = val.trim();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (key === 'name')
|
|
93
|
+
name = val;
|
|
94
|
+
else if (key === 'filename')
|
|
95
|
+
filename = val;
|
|
96
|
+
if (value[i] === ';')
|
|
97
|
+
i++;
|
|
98
|
+
}
|
|
99
|
+
return { name, filename };
|
|
100
|
+
}
|
|
101
|
+
function parseHeaders(block) {
|
|
102
|
+
const headers = {};
|
|
103
|
+
for (const line of block.split('\r\n')) {
|
|
104
|
+
if (!line)
|
|
105
|
+
continue;
|
|
106
|
+
const idx = line.indexOf(':');
|
|
107
|
+
if (idx === -1)
|
|
108
|
+
continue;
|
|
109
|
+
headers[lower(line.slice(0, idx).trim())] = line.slice(idx + 1).trim();
|
|
110
|
+
}
|
|
111
|
+
return headers;
|
|
112
|
+
}
|
|
113
|
+
function encodeName(name) {
|
|
114
|
+
// Handles the exotic case of a non-ASCII field name without throwing.
|
|
115
|
+
try {
|
|
116
|
+
encodeURIComponent(name);
|
|
117
|
+
return name;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
return name;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function extractFilenamePath(filename) {
|
|
124
|
+
let i = filename.lastIndexOf('/');
|
|
125
|
+
if (i !== -1)
|
|
126
|
+
filename = filename.slice(i + 1);
|
|
127
|
+
i = filename.lastIndexOf('\\');
|
|
128
|
+
if (i !== -1)
|
|
129
|
+
filename = filename.slice(i + 1);
|
|
130
|
+
return filename;
|
|
131
|
+
}
|
|
132
|
+
function mimeAllowed(contentType, list) {
|
|
133
|
+
const ct = lower(contentType);
|
|
134
|
+
for (const raw of list) {
|
|
135
|
+
const entry = lower(raw.trim());
|
|
136
|
+
if (entry.endsWith('/*')) {
|
|
137
|
+
if (ct.startsWith(entry.slice(0, entry.length - 1)))
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
else if (entry === ct) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const DEFAULTS = {
|
|
147
|
+
maxFileSize: 10 * 1024 * 1024,
|
|
148
|
+
maxTotalSize: 100 * 1024 * 1024,
|
|
149
|
+
maxFiles: 20,
|
|
150
|
+
maxFields: 100,
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Parse a multipart body from a `ReadableStream`, enforcing limits as bytes
|
|
154
|
+
* arrive. Rejects with `MultipartError` (413/415/400) on the first violation.
|
|
155
|
+
*/
|
|
156
|
+
export async function parseMultipart(stream, boundary, options = {}) {
|
|
157
|
+
const maxFileSize = options.maxFileSize ?? DEFAULTS.maxFileSize;
|
|
158
|
+
const maxTotalSize = options.maxTotalSize ?? DEFAULTS.maxTotalSize;
|
|
159
|
+
const maxFiles = options.maxFiles ?? DEFAULTS.maxFiles;
|
|
160
|
+
const maxFields = options.maxFields ?? DEFAULTS.maxFields;
|
|
161
|
+
const delimiter = encoder.encode(`--${boundary}`);
|
|
162
|
+
const parts = [];
|
|
163
|
+
let totalSize = 0;
|
|
164
|
+
let fileCount = 0;
|
|
165
|
+
let fieldCount = 0;
|
|
166
|
+
// Everything read so far, minus whatever has already been consumed.
|
|
167
|
+
let buffer = new Uint8Array(0);
|
|
168
|
+
let consumed = 0;
|
|
169
|
+
/** Drop already-consumed prefix so the buffer does not grow without bound. */
|
|
170
|
+
function compact() {
|
|
171
|
+
if (consumed === 0)
|
|
172
|
+
return;
|
|
173
|
+
buffer = buffer.slice(consumed);
|
|
174
|
+
consumed = 0;
|
|
175
|
+
}
|
|
176
|
+
function append(chunk) {
|
|
177
|
+
if (buffer.length - consumed === 0) {
|
|
178
|
+
buffer = chunk;
|
|
179
|
+
consumed = 0;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const merged = new Uint8Array(buffer.length - consumed + chunk.length);
|
|
183
|
+
merged.set(buffer.subarray(consumed), 0);
|
|
184
|
+
merged.set(chunk, buffer.length - consumed);
|
|
185
|
+
buffer = merged;
|
|
186
|
+
consumed = 0;
|
|
187
|
+
}
|
|
188
|
+
const reader = stream.getReader();
|
|
189
|
+
try {
|
|
190
|
+
for (;;) {
|
|
191
|
+
// ── headers ──────────────────────────────────────────────
|
|
192
|
+
let headerEnd = -1;
|
|
193
|
+
for (;;) {
|
|
194
|
+
headerEnd = indexOfFrom(buffer, [[CR, LF, CR, LF]], consumed);
|
|
195
|
+
if (headerEnd !== -1)
|
|
196
|
+
break;
|
|
197
|
+
const next = await reader.read();
|
|
198
|
+
if (next.done)
|
|
199
|
+
throw new MultipartError('Malformed multipart: no part headers found', 'MALFORMED_MULTIPART', 400);
|
|
200
|
+
append(next.value);
|
|
201
|
+
totalSize += next.value.length;
|
|
202
|
+
if (totalSize > maxTotalSize) {
|
|
203
|
+
throw new MultipartError(`Upload exceeds maxTotalSize (${maxTotalSize} bytes)`, 'UPLOAD_TOO_LARGE', 413);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const headerText = decoder.decode(buffer.subarray(consumed, headerEnd));
|
|
207
|
+
const headers = parseHeaders(headerText);
|
|
208
|
+
const disposition = headers['content-disposition'] || '';
|
|
209
|
+
const { name, filename } = parseContentDisposition(disposition);
|
|
210
|
+
const contentType = headers['content-type'];
|
|
211
|
+
let bodyStart = headerEnd + 4;
|
|
212
|
+
// ── body, streamed until the next boundary ───────────────
|
|
213
|
+
const bodyChunks = [];
|
|
214
|
+
let bodySize = 0;
|
|
215
|
+
let boundaryAt = -1;
|
|
216
|
+
for (;;) {
|
|
217
|
+
boundaryAt = findDelimiter(buffer, delimiter, bodyStart);
|
|
218
|
+
if (boundaryAt !== -1)
|
|
219
|
+
break;
|
|
220
|
+
if (buffer.length - bodyStart > 0) {
|
|
221
|
+
const keep = delimiter.length + 4;
|
|
222
|
+
if (buffer.length - bodyStart > keep) {
|
|
223
|
+
bodyChunks.push(buffer.slice(bodyStart, buffer.length - keep));
|
|
224
|
+
bodySize += buffer.length - bodyStart - keep;
|
|
225
|
+
bodyStart = buffer.length - keep;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (bodySize > maxFileSize) {
|
|
229
|
+
throw new MultipartError(filename
|
|
230
|
+
? `File "${filename}" exceeds maxFileSize (${maxFileSize} bytes)`
|
|
231
|
+
: `Field "${name}" exceeds maxFileSize (${maxFileSize} bytes)`, 'FILE_TOO_LARGE', 413);
|
|
232
|
+
}
|
|
233
|
+
const next = await reader.read();
|
|
234
|
+
if (next.done)
|
|
235
|
+
throw new MultipartError('Malformed multipart: truncated body', 'MALFORMED_MULTIPART', 400);
|
|
236
|
+
// append() drops the already-consumed prefix and rebases the
|
|
237
|
+
// buffer, so every absolute offset shifts by exactly that
|
|
238
|
+
// amount. Without this the scan drifted and multi-chunk bodies
|
|
239
|
+
// came back empty or truncated.
|
|
240
|
+
const dropped = consumed;
|
|
241
|
+
append(next.value);
|
|
242
|
+
bodyStart -= dropped;
|
|
243
|
+
totalSize += next.value.length;
|
|
244
|
+
if (totalSize > maxTotalSize) {
|
|
245
|
+
throw new MultipartError(`Upload exceeds maxTotalSize (${maxTotalSize} bytes)`, 'UPLOAD_TOO_LARGE', 413);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (boundaryAt > bodyStart) {
|
|
249
|
+
bodyChunks.push(buffer.slice(bodyStart, boundaryAt));
|
|
250
|
+
bodySize += boundaryAt - bodyStart;
|
|
251
|
+
}
|
|
252
|
+
// The in-loop check only runs while the delimiter is still missing,
|
|
253
|
+
// so a body that arrives in one chunk skipped it entirely.
|
|
254
|
+
if (bodySize > maxFileSize) {
|
|
255
|
+
throw new MultipartError(filename !== undefined
|
|
256
|
+
? `File "${filename}" exceeds maxFileSize (${maxFileSize} bytes)`
|
|
257
|
+
: `Field "${name}" exceeds maxFileSize (${maxFileSize} bytes)`, 'FILE_TOO_LARGE', 413);
|
|
258
|
+
}
|
|
259
|
+
const isFile = filename !== undefined;
|
|
260
|
+
const safeName = isFile ? extractFilenamePath(filename) : undefined;
|
|
261
|
+
if (isFile) {
|
|
262
|
+
fileCount++;
|
|
263
|
+
if (fileCount > maxFiles) {
|
|
264
|
+
throw new MultipartError(`Too many files (max ${maxFiles})`, 'TOO_MANY_FILES', 413);
|
|
265
|
+
}
|
|
266
|
+
if (options.allowedMimeTypes && contentType && !mimeAllowed(contentType, options.allowedMimeTypes)) {
|
|
267
|
+
throw new MultipartError(`File type not allowed: ${contentType}`, 'FILE_TYPE_NOT_ALLOWED', 415);
|
|
268
|
+
}
|
|
269
|
+
if (options.allowedExtensions?.length && safeName) {
|
|
270
|
+
const dot = safeName.lastIndexOf('.');
|
|
271
|
+
const ext = dot === -1 ? '' : lower(safeName.slice(dot));
|
|
272
|
+
const ok = options.allowedExtensions.some((e) => lower(e) === ext || lower(e) === `.${ext.slice(1)}`);
|
|
273
|
+
if (!ok) {
|
|
274
|
+
throw new MultipartError(`File extension not allowed: ${ext || '(none)'}`, 'FILE_TYPE_NOT_ALLOWED', 415);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
fieldCount++;
|
|
280
|
+
if (fieldCount > maxFields) {
|
|
281
|
+
throw new MultipartError(`Too many form fields (max ${maxFields})`, 'TOO_MANY_FIELDS', 400);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const merged = concat(bodyChunks, bodySize);
|
|
285
|
+
const value = isFile
|
|
286
|
+
? new File([merged], safeName ?? 'upload', { type: contentType || 'application/octet-stream' })
|
|
287
|
+
: decoder.decode(merged);
|
|
288
|
+
parts.push({
|
|
289
|
+
name: encodeName(name),
|
|
290
|
+
filename: isFile ? safeName : undefined,
|
|
291
|
+
contentType,
|
|
292
|
+
headers,
|
|
293
|
+
value,
|
|
294
|
+
size: bodySize,
|
|
295
|
+
isFile,
|
|
296
|
+
});
|
|
297
|
+
// Advance past the delimiter and the CRLF that follows it.
|
|
298
|
+
let after = boundaryAt + 2 + delimiter.length;
|
|
299
|
+
if (buffer[after] === DASH && buffer[after + 1] === DASH) {
|
|
300
|
+
// closing boundary "--" — done
|
|
301
|
+
compact();
|
|
302
|
+
return finish(parts, totalSize);
|
|
303
|
+
}
|
|
304
|
+
if (buffer[after] === CR && buffer[after + 1] === LF)
|
|
305
|
+
after += 2;
|
|
306
|
+
consumed = after;
|
|
307
|
+
if (parts.length > maxFiles + maxFields + 8) {
|
|
308
|
+
throw new MultipartError('Too many multipart parts', 'TOO_MANY_FIELDS', 400);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
finally {
|
|
313
|
+
try {
|
|
314
|
+
reader.releaseLock();
|
|
315
|
+
}
|
|
316
|
+
catch { /* already released */ }
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/** Locate `\r\n--boundary` at or after `from`. */
|
|
320
|
+
function findDelimiter(buffer, delimiter, from) {
|
|
321
|
+
const needle = new Uint8Array(2 + delimiter.length);
|
|
322
|
+
needle[0] = CR;
|
|
323
|
+
needle[1] = LF;
|
|
324
|
+
needle.set(delimiter, 2);
|
|
325
|
+
// A candidate only counts when the bytes AFTER the delimiter are valid:
|
|
326
|
+
// CRLF (another part follows) or "--" (final boundary). Without this a
|
|
327
|
+
// file whose contents happen to contain "\r\n--<boundary>" was cut short.
|
|
328
|
+
for (let i = from; i <= buffer.length - needle.length; i++) {
|
|
329
|
+
let match = true;
|
|
330
|
+
for (let k = 0; k < needle.length; k++) {
|
|
331
|
+
if (buffer[i + k] !== needle[k]) {
|
|
332
|
+
match = false;
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (!match)
|
|
337
|
+
continue;
|
|
338
|
+
const after = i + needle.length;
|
|
339
|
+
const nextIsCrlf = buffer[after] === CR && buffer[after + 1] === LF;
|
|
340
|
+
const nextIsFinal = buffer[after] === DASH && buffer[after + 1] === DASH;
|
|
341
|
+
if (nextIsCrlf || nextIsFinal)
|
|
342
|
+
return i;
|
|
343
|
+
}
|
|
344
|
+
return -1;
|
|
345
|
+
}
|
|
346
|
+
function concat(chunks, size) {
|
|
347
|
+
if (chunks.length === 1)
|
|
348
|
+
return chunks[0];
|
|
349
|
+
const out = new Uint8Array(size);
|
|
350
|
+
let off = 0;
|
|
351
|
+
for (const c of chunks) {
|
|
352
|
+
out.set(c, off);
|
|
353
|
+
off += c.length;
|
|
354
|
+
}
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
function finish(parts, totalSize) {
|
|
358
|
+
return {
|
|
359
|
+
parts,
|
|
360
|
+
files: parts.filter(p => p.isFile),
|
|
361
|
+
fields: parts.filter(p => !p.isFile),
|
|
362
|
+
totalSize,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Extract the boundary token from a `Content-Type` header. Returns null when
|
|
367
|
+
* the header is not multipart or carries no boundary.
|
|
368
|
+
*/
|
|
369
|
+
export function boundaryFromContentType(contentType) {
|
|
370
|
+
if (!contentType)
|
|
371
|
+
return null;
|
|
372
|
+
// Only the media type and the parameter NAME are case-insensitive; the
|
|
373
|
+
// boundary VALUE is opaque and must survive verbatim (lowercasing it
|
|
374
|
+
// changed the delimiter the parser then searched for).
|
|
375
|
+
if (!lower(contentType.trim()).startsWith('multipart/'))
|
|
376
|
+
return null;
|
|
377
|
+
for (const chunk of contentType.split(';')) {
|
|
378
|
+
const trimmed = chunk.trim();
|
|
379
|
+
const eq = trimmed.indexOf('=');
|
|
380
|
+
if (eq === -1)
|
|
381
|
+
continue;
|
|
382
|
+
if (lower(trimmed.slice(0, eq).trim()) !== 'boundary')
|
|
383
|
+
continue;
|
|
384
|
+
let v = trimmed.slice(eq + 1).trim();
|
|
385
|
+
if (v.startsWith('"') && v.endsWith('"'))
|
|
386
|
+
v = v.slice(1, -1);
|
|
387
|
+
return v.length > 0 ? v : null;
|
|
388
|
+
}
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
/** Convenience: parse straight from a `Request` using its content-type. */
|
|
392
|
+
/**
|
|
393
|
+
* Where an auto-parsed upload is parked on the request.
|
|
394
|
+
*
|
|
395
|
+
* A Symbol, not a property name. A string key is either a public name the
|
|
396
|
+
* framework has to keep reserved forever — `request.__vesk_multipart` printed an
|
|
397
|
+
* internal convention on the very object the handler holds, with no type for it
|
|
398
|
+
* and no way to reach it without a cast — or a collision waiting to happen with
|
|
399
|
+
* user code. A symbol is invisible to `Object.keys`, cannot collide, and puts no
|
|
400
|
+
* name in the public surface. Reach it with `multipartResult()`.
|
|
401
|
+
*/
|
|
402
|
+
const AUTO_PARSED = Symbol('vesk.multipart');
|
|
403
|
+
/**
|
|
404
|
+
* The upload a `multipart({ autoParse: true })` plugin already parsed for this
|
|
405
|
+
* request, or null if none did.
|
|
406
|
+
*
|
|
407
|
+
* You rarely need this: `parseUploadOrError` / `parseMultipartRequest` reuse
|
|
408
|
+
* the auto-parsed body on their own. It exists for a handler that wants the
|
|
409
|
+
* result without being able to fail — an error page, say.
|
|
410
|
+
*/
|
|
411
|
+
export function multipartResult(request) {
|
|
412
|
+
return request[AUTO_PARSED] ?? null;
|
|
413
|
+
}
|
|
414
|
+
/** @internal — used by the plugin's `onRequest` hook. */
|
|
415
|
+
export function attachMultipartResult(request, parsed) {
|
|
416
|
+
request[AUTO_PARSED] = parsed;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Apply `options` to an already-parsed upload.
|
|
420
|
+
*
|
|
421
|
+
* Only correct after the fact — the whole point of `parseMultipart` is that it
|
|
422
|
+
* stops a hostile body mid-stream — but the only alternative when a plugin has
|
|
423
|
+
* already read the body is to serve the result with the caller's limits silently
|
|
424
|
+
* ignored, which is worse.
|
|
425
|
+
*/
|
|
426
|
+
export function recheckLimits(parsed, options = {}) {
|
|
427
|
+
const maxFileSize = options.maxFileSize ?? DEFAULTS.maxFileSize;
|
|
428
|
+
const maxTotalSize = options.maxTotalSize ?? DEFAULTS.maxTotalSize;
|
|
429
|
+
const maxFiles = options.maxFiles ?? DEFAULTS.maxFiles;
|
|
430
|
+
const maxFields = options.maxFields ?? DEFAULTS.maxFields;
|
|
431
|
+
if (parsed.totalSize > maxTotalSize) {
|
|
432
|
+
throw new MultipartError(`Upload exceeds maxTotalSize (${maxTotalSize} bytes)`, 'UPLOAD_TOO_LARGE', 413);
|
|
433
|
+
}
|
|
434
|
+
let fileCount = 0;
|
|
435
|
+
let fieldCount = 0;
|
|
436
|
+
for (const part of parsed.parts) {
|
|
437
|
+
if (part.size > maxFileSize) {
|
|
438
|
+
throw new MultipartError(part.isFile
|
|
439
|
+
? `File "${part.filename}" exceeds maxFileSize (${maxFileSize} bytes)`
|
|
440
|
+
: `Field "${part.name}" exceeds maxFileSize (${maxFileSize} bytes)`, 'FILE_TOO_LARGE', 413);
|
|
441
|
+
}
|
|
442
|
+
if (!part.isFile) {
|
|
443
|
+
fieldCount++;
|
|
444
|
+
if (fieldCount > maxFields)
|
|
445
|
+
throw new MultipartError(`Too many form fields (max ${maxFields})`, 'TOO_MANY_FIELDS', 400);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
fileCount++;
|
|
449
|
+
if (fileCount > maxFiles)
|
|
450
|
+
throw new MultipartError(`Too many files (max ${maxFiles})`, 'TOO_MANY_FILES', 413);
|
|
451
|
+
if (options.allowedMimeTypes && part.contentType && !mimeAllowed(part.contentType, options.allowedMimeTypes)) {
|
|
452
|
+
throw new MultipartError(`File type not allowed: ${part.contentType}`, 'FILE_TYPE_NOT_ALLOWED', 415);
|
|
453
|
+
}
|
|
454
|
+
if (options.allowedExtensions?.length && part.filename) {
|
|
455
|
+
const dot = part.filename.lastIndexOf('.');
|
|
456
|
+
const ext = dot === -1 ? '' : lower(part.filename.slice(dot));
|
|
457
|
+
const ok = options.allowedExtensions.some((e) => lower(e) === ext || lower(e) === `.${ext.slice(1)}`);
|
|
458
|
+
if (!ok)
|
|
459
|
+
throw new MultipartError(`File extension not allowed: ${ext || '(none)'}`, 'FILE_TYPE_NOT_ALLOWED', 415);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return parsed;
|
|
463
|
+
}
|
|
464
|
+
export async function parseMultipartRequest(request, options = {}) {
|
|
465
|
+
// A request body is single-pass. If a plugin already parsed it, reading it a
|
|
466
|
+
// second time can only fail — so reuse the stored parse and re-apply this
|
|
467
|
+
// call's limits to it.
|
|
468
|
+
const existing = multipartResult(request);
|
|
469
|
+
if (existing)
|
|
470
|
+
return recheckLimits(existing, options);
|
|
471
|
+
if (!request.body)
|
|
472
|
+
throw new MultipartError('Request has no body', 'NO_BODY', 400);
|
|
473
|
+
const boundary = boundaryFromContentType(request.headers.get('content-type'));
|
|
474
|
+
if (!boundary) {
|
|
475
|
+
throw new MultipartError('Not a multipart request (missing boundary)', 'NOT_MULTIPART', 415);
|
|
476
|
+
}
|
|
477
|
+
const parsed = await parseMultipart(request.body, boundary, options);
|
|
478
|
+
attachMultipartResult(request, parsed);
|
|
479
|
+
return parsed;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Shape a parsed upload for `withValidation()`-style code and for
|
|
483
|
+
* `Object.fromEntries`-style flattening WITHOUT destroying `File` values.
|
|
484
|
+
* Repeated field names collect into an array.
|
|
485
|
+
*/
|
|
486
|
+
export function toRecord(parsed) {
|
|
487
|
+
const out = {};
|
|
488
|
+
for (const part of parsed.parts) {
|
|
489
|
+
const existing = out[part.name];
|
|
490
|
+
if (existing === undefined) {
|
|
491
|
+
out[part.name] = part.value;
|
|
492
|
+
}
|
|
493
|
+
else if (Array.isArray(existing)) {
|
|
494
|
+
existing.push(part.value);
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
out[part.name] = [existing, part.value];
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return out;
|
|
501
|
+
}
|
|
502
|
+
export { ZERO as _ZERO };
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vesk/plugin-multipart",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.71",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "Streaming multipart/form-data uploads for Vesk — real File values, per-field
|
|
5
|
+
"description": "Streaming multipart/form-data uploads for Vesk — real File values, per-file/per-field limits enforced as bytes arrive",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
8
12
|
"exports": {
|
|
9
13
|
".": {
|
|
10
14
|
"types": "./dist/index.d.ts",
|
|
@@ -22,7 +26,7 @@
|
|
|
22
26
|
"typecheck": "tsc --noEmit"
|
|
23
27
|
},
|
|
24
28
|
"dependencies": {
|
|
25
|
-
"@vesk/types": "^0.2.
|
|
29
|
+
"@vesk/types": "^0.2.71"
|
|
26
30
|
},
|
|
27
31
|
"devDependencies": {
|
|
28
32
|
"typescript": "^5.8.0"
|
package/src/index.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vesk/plugin-multipart — streaming `multipart/form-data` uploads for Vesk.
|
|
3
|
-
*
|
|
4
|
-
* Core Vesk can parse multipart through `request.formData()`, but the body is
|
|
5
|
-
* buffered whole under the 1 MiB `DEFAULT_MAX_BODY_BYTES` cap and
|
|
6
|
-
* `Object.fromEntries(fd.entries())` collapses `File` values into opaque
|
|
7
|
-
* strings — so an upload could not be inspected, validated or persisted. That
|
|
8
|
-
* work belongs here rather than in core: most apps never accept a file, and the
|
|
9
|
-
* parser is a few hundred lines that should not ship to everyone.
|
|
10
|
-
*
|
|
11
|
-
* Usage — in a route or API handler:
|
|
12
|
-
*
|
|
13
|
-
* ```ts
|
|
14
|
-
* import { parseUpload } from '@vesk/plugin-multipart';
|
|
15
|
-
*
|
|
16
|
-
* export async function POST(req: VeskRequest) {
|
|
17
|
-
* const upload = await parseUpload(req, {
|
|
18
|
-
* maxFileSize: 8 * 1024 * 1024,
|
|
19
|
-
* allowedMimeTypes: ['image/png', 'image/jpeg'],
|
|
20
|
-
* });
|
|
21
|
-
* const avatar = upload.files[0]?.value as File;
|
|
22
|
-
* return ServerResponse.json({ ok: true, name: avatar?.name });
|
|
23
|
-
* }
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* Rejections throw `MultipartError`, whose `status` is 413 (too large / too
|
|
27
|
-
* many), 415 (disallowed type) or 400 (malformed). Nothing is buffered beyond
|
|
28
|
-
* `maxFileSize` per part, so an oversized upload is cut off after the first few
|
|
29
|
-
* KB instead of after the whole body has been read.
|
|
30
|
-
*
|
|
31
|
-
* @module plugin-multipart
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
import type { VeskPlugin } from '@vesk/types';
|
|
35
|
-
import {
|
|
36
|
-
parseMultipart,
|
|
37
|
-
parseMultipartRequest,
|
|
38
|
-
boundaryFromContentType,
|
|
39
|
-
toRecord,
|
|
40
|
-
MultipartError,
|
|
41
|
-
type MultipartPart,
|
|
42
|
-
type MultipartOptions,
|
|
43
|
-
type ParsedMultipart,
|
|
44
|
-
} from './parser.js';
|
|
45
|
-
|
|
46
|
-
export {
|
|
47
|
-
parseMultipart,
|
|
48
|
-
parseMultipartRequest,
|
|
49
|
-
boundaryFromContentType,
|
|
50
|
-
toRecord,
|
|
51
|
-
MultipartError,
|
|
52
|
-
};
|
|
53
|
-
export type { MultipartPart, MultipartOptions, ParsedMultipart };
|
|
54
|
-
|
|
55
|
-
const MiB = 1024 * 1024;
|
|
56
|
-
|
|
57
|
-
export interface MultipartPluginOptions extends MultipartOptions {
|
|
58
|
-
/** Skip parsing requests below this content-length (default 1). */
|
|
59
|
-
minBytes?: number;
|
|
60
|
-
/**
|
|
61
|
-
* Parse every multipart request before it reaches the handler. The parsed
|
|
62
|
-
* result is attached to the request as `__vesk_multipart`, so a handler can
|
|
63
|
-
* reach it without re-reading the stream.
|
|
64
|
-
*
|
|
65
|
-
* Off by default: a handler that never calls `parseUpload` pays nothing.
|
|
66
|
-
*/
|
|
67
|
-
autoParse?: boolean;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* `parseUpload` that returns the JSON error `Response` itself on rejection —
|
|
72
|
-
* the ergonomic form for a route handler:
|
|
73
|
-
*
|
|
74
|
-
* ```ts
|
|
75
|
-
* const upload = await parseUploadOrError(req, { maxFileSize: 8 * MiB });
|
|
76
|
-
* if (upload instanceof Response) return upload; // already a 413/415/400
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
export async function parseUploadOrError(
|
|
80
|
-
request: Request,
|
|
81
|
-
options: MultipartOptions = {},
|
|
82
|
-
): Promise<ParsedMultipart | Response> {
|
|
83
|
-
try {
|
|
84
|
-
return await parseMultipartRequest(request, options);
|
|
85
|
-
} catch (e) {
|
|
86
|
-
if (e instanceof MultipartError) {
|
|
87
|
-
return new Response(
|
|
88
|
-
JSON.stringify({ error: e.message, code: e.code }),
|
|
89
|
-
{ status: e.status, headers: { 'Content-Type': 'application/json' } },
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
throw e;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function multipart(options: MultipartPluginOptions = {}): VeskPlugin {
|
|
97
|
-
const { autoParse = false, minBytes = 1, ...parseOptions } = options;
|
|
98
|
-
return {
|
|
99
|
-
name: 'multipart',
|
|
100
|
-
async onRequest(ctx) {
|
|
101
|
-
if (!autoParse) return;
|
|
102
|
-
const request = ctx.request;
|
|
103
|
-
if (!request) return;
|
|
104
|
-
const length = Number(request.headers.get('content-length') || '0');
|
|
105
|
-
if (Number.isFinite(length) && length < minBytes) return;
|
|
106
|
-
if (!boundaryFromContentType(request.headers.get('content-type'))) return;
|
|
107
|
-
try {
|
|
108
|
-
const parsed = await parseMultipartRequest(request, parseOptions);
|
|
109
|
-
(request as unknown as Record<string, unknown>).__vesk_multipart = parsed;
|
|
110
|
-
} catch (e) {
|
|
111
|
-
if (e instanceof MultipartError) {
|
|
112
|
-
(ctx as unknown as { response?: Response }).response = new Response(
|
|
113
|
-
JSON.stringify({ error: e.message, code: e.code }),
|
|
114
|
-
{ status: e.status, headers: { 'Content-Type': 'application/json' } },
|
|
115
|
-
);
|
|
116
|
-
} else {
|
|
117
|
-
throw e;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
},
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export default multipart;
|
|
125
|
-
export { MiB };
|