@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/src/parser.ts DELETED
@@ -1,479 +0,0 @@
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
-
24
- /** A single parsed multipart part. */
25
- export interface MultipartPart {
26
- /** The `name` attribute of the part's Content-Disposition. */
27
- name: string;
28
- /** Raw filename from the part, when it was a file upload. */
29
- filename?: string;
30
- /** Lowercased MIME type from the part's Content-Type, when present. */
31
- contentType?: string;
32
- /** Every header on the part. */
33
- headers: Record<string, string>;
34
- /** The part body. Text parts get a decoded string; files a `File`. */
35
- value: string | File;
36
- /** Byte length of the part body. */
37
- size: number;
38
- /** True when the part carried a filename (i.e. is an upload). */
39
- isFile: boolean;
40
- }
41
-
42
- export interface MultipartLimits {
43
- /** Max bytes for a single file part. Default 10 MiB. */
44
- maxFileSize?: number;
45
- /** Max bytes across the whole body. Default 100 MiB. */
46
- maxTotalSize?: number;
47
- /** Max number of file parts. Default 20. */
48
- maxFiles?: number;
49
- /** Max number of non-file fields. Default 100. */
50
- maxFields?: number;
51
- }
52
-
53
- export interface MultipartOptions extends MultipartLimits {
54
- /**
55
- * When set, any file whose content type is not in this list is rejected
56
- * with a 415. Matching is on the full type or a `type/*` wildcard.
57
- */
58
- allowedMimeTypes?: string[];
59
- /**
60
- * Extension allowlist (with or without the leading dot), checked against
61
- * the filename. Applied even when `allowedMimeTypes` is unset.
62
- */
63
- allowedExtensions?: string[];
64
- }
65
-
66
- /** Thrown for any rejection. `status` maps onto an HTTP response. */
67
- export class MultipartError extends Error {
68
- status: number;
69
- code: string;
70
- constructor(message: string, code: string, status: number) {
71
- super(message);
72
- this.name = 'MultipartError';
73
- this.code = code;
74
- this.status = status;
75
- }
76
- }
77
-
78
- const DASH = 0x2d; // '-'
79
- const CR = 0x0d;
80
- const LF = 0x0a;
81
-
82
- const SPACE = 0x20;
83
- const TAB = 0x09;
84
- const QUOTE = '"';
85
- const SEMI = 0x3b;
86
- const ZERO = 0x30;
87
-
88
- const encoder = new TextEncoder();
89
- const decoder = new TextDecoder();
90
-
91
- function lower(s: string): string {
92
- let out = '';
93
- for (let i = 0; i < s.length; i++) out += s[i].toLowerCase();
94
- return out;
95
- }
96
-
97
- function indexOfFrom(haystack: Uint8Array, needles: number[][], from: number): number {
98
- const maxLen = needles.reduce((m, n) => (n.length > m ? n.length : m), 0);
99
- outer: for (let i = from; i <= haystack.length - maxLen; i++) {
100
- for (const needle of needles) {
101
- if (i + needle.length > haystack.length) continue;
102
- for (let k = 0; k < needle.length; k++) {
103
- if (haystack[i + k] !== needle[k]) continue outer;
104
- }
105
- return i;
106
- }
107
- }
108
- return -1;
109
- }
110
-
111
- function parseContentDisposition(value: string): { name: string; filename?: string } {
112
- // name="x"; filename="y" — attribute walk, no regex.
113
- let name = '';
114
- let filename: string | undefined;
115
- let i = 0;
116
- while (i < value.length) {
117
- while (i < value.length && (value[i] === ' ' || value[i] === ';' || value[i] === '\t')) i++;
118
- let key = '';
119
- while (i < value.length && value[i] !== '=' && value[i] !== ';') key += value[i++];
120
- key = lower(key.trim());
121
- let val = '';
122
- if (value[i] === '=') {
123
- i++;
124
- if (value[i] === QUOTE) {
125
- i++;
126
- while (i < value.length && value[i] !== QUOTE) val += value[i++];
127
- i++; // closing quote
128
- } else {
129
- while (i < value.length && value[i] !== ';') val += value[i++];
130
- val = val.trim();
131
- }
132
- }
133
- if (key === 'name') name = val;
134
- else if (key === 'filename') filename = val;
135
- if (value[i] === ';') i++;
136
- }
137
- return { name, filename };
138
- }
139
-
140
- function parseHeaders(block: string): Record<string, string> {
141
- const headers: Record<string, string> = {};
142
- for (const line of block.split('\r\n')) {
143
- if (!line) continue;
144
- const idx = line.indexOf(':');
145
- if (idx === -1) continue;
146
- headers[lower(line.slice(0, idx).trim())] = line.slice(idx + 1).trim();
147
- }
148
- return headers;
149
- }
150
-
151
- function encodeName(name: string): string {
152
- // Handles the exotic case of a non-ASCII field name without throwing.
153
- try {
154
- encodeURIComponent(name);
155
- return name;
156
- } catch {
157
- return name;
158
- }
159
- }
160
-
161
- function extractFilenamePath(filename: string): string {
162
- let i = filename.lastIndexOf('/');
163
- if (i !== -1) filename = filename.slice(i + 1);
164
- i = filename.lastIndexOf('\\');
165
- if (i !== -1) filename = filename.slice(i + 1);
166
- return filename;
167
- }
168
-
169
- function mimeAllowed(contentType: string, list: string[]): boolean {
170
- const ct = lower(contentType);
171
- for (const raw of list) {
172
- const entry = lower(raw.trim());
173
- if (entry.endsWith('/*')) {
174
- if (ct.startsWith(entry.slice(0, entry.length - 1))) return true;
175
- } else if (entry === ct) {
176
- return true;
177
- }
178
- }
179
- return false;
180
- }
181
-
182
- export interface ParsedMultipart {
183
- parts: MultipartPart[];
184
- files: MultipartPart[];
185
- fields: MultipartPart[];
186
- /** Sum of every part body, excluding part headers and boundaries. */
187
- totalSize: number;
188
- }
189
-
190
- const DEFAULTS = {
191
- maxFileSize: 10 * 1024 * 1024,
192
- maxTotalSize: 100 * 1024 * 1024,
193
- maxFiles: 20,
194
- maxFields: 100,
195
- };
196
-
197
- /**
198
- * Parse a multipart body from a `ReadableStream`, enforcing limits as bytes
199
- * arrive. Rejects with `MultipartError` (413/415/400) on the first violation.
200
- */
201
- export async function parseMultipart(
202
- stream: ReadableStream<Uint8Array>,
203
- boundary: string,
204
- options: MultipartOptions = {},
205
- ): Promise<ParsedMultipart> {
206
- const maxFileSize = options.maxFileSize ?? DEFAULTS.maxFileSize;
207
- const maxTotalSize = options.maxTotalSize ?? DEFAULTS.maxTotalSize;
208
- const maxFiles = options.maxFiles ?? DEFAULTS.maxFiles;
209
- const maxFields = options.maxFields ?? DEFAULTS.maxFields;
210
-
211
- const delimiter = encoder.encode(`--${boundary}`);
212
- const parts: MultipartPart[] = [];
213
- let totalSize = 0;
214
- let fileCount = 0;
215
- let fieldCount = 0;
216
-
217
- // Everything read so far, minus whatever has already been consumed.
218
- let buffer: Uint8Array = new Uint8Array(0);
219
- let consumed = 0;
220
-
221
- /** Drop already-consumed prefix so the buffer does not grow without bound. */
222
- function compact(): void {
223
- if (consumed === 0) return;
224
- buffer = buffer.slice(consumed);
225
- consumed = 0;
226
- }
227
-
228
- function append(chunk: Uint8Array): void {
229
- if (buffer.length - consumed === 0) {
230
- buffer = chunk;
231
- consumed = 0;
232
- return;
233
- }
234
- const merged = new Uint8Array(buffer.length - consumed + chunk.length);
235
- merged.set(buffer.subarray(consumed), 0);
236
- merged.set(chunk, buffer.length - consumed);
237
- buffer = merged;
238
- consumed = 0;
239
- }
240
-
241
- const reader = stream.getReader();
242
- try {
243
- for (;;) {
244
- // ── headers ──────────────────────────────────────────────
245
- let headerEnd = -1;
246
- for (;;) {
247
- headerEnd = indexOfFrom(buffer, [[CR, LF, CR, LF]], consumed);
248
- if (headerEnd !== -1) break;
249
- const next = await reader.read();
250
- if (next.done) throw new MultipartError('Malformed multipart: no part headers found', 'MALFORMED_MULTIPART', 400);
251
- append(next.value);
252
- totalSize += next.value.length;
253
- if (totalSize > maxTotalSize) {
254
- throw new MultipartError(`Upload exceeds maxTotalSize (${maxTotalSize} bytes)`, 'UPLOAD_TOO_LARGE', 413);
255
- }
256
- }
257
-
258
- const headerText = decoder.decode(buffer.subarray(consumed, headerEnd));
259
- const headers = parseHeaders(headerText);
260
- const disposition = headers['content-disposition'] || '';
261
- const { name, filename } = parseContentDisposition(disposition);
262
- const contentType = headers['content-type'];
263
-
264
- let bodyStart = headerEnd + 4;
265
-
266
- // ── body, streamed until the next boundary ───────────────
267
- const bodyChunks: Uint8Array[] = [];
268
- let bodySize = 0;
269
- let boundaryAt = -1;
270
-
271
- for (;;) {
272
- boundaryAt = findDelimiter(buffer, delimiter, bodyStart);
273
- if (boundaryAt !== -1) break;
274
- if (buffer.length - bodyStart > 0) {
275
- const keep = delimiter.length + 4;
276
- if (buffer.length - bodyStart > keep) {
277
- bodyChunks.push(buffer.slice(bodyStart, buffer.length - keep));
278
- bodySize += buffer.length - bodyStart - keep;
279
- bodyStart = buffer.length - keep;
280
- }
281
- }
282
- if (bodySize > maxFileSize) {
283
- throw new MultipartError(
284
- filename
285
- ? `File "${filename}" exceeds maxFileSize (${maxFileSize} bytes)`
286
- : `Field "${name}" exceeds maxFileSize (${maxFileSize} bytes)`,
287
- 'FILE_TOO_LARGE',
288
- 413,
289
- );
290
- }
291
- const next = await reader.read();
292
- if (next.done) throw new MultipartError('Malformed multipart: truncated body', 'MALFORMED_MULTIPART', 400);
293
- // append() drops the already-consumed prefix and rebases the
294
- // buffer, so every absolute offset shifts by exactly that
295
- // amount. Without this the scan drifted and multi-chunk bodies
296
- // came back empty or truncated.
297
- const dropped = consumed;
298
- append(next.value);
299
- bodyStart -= dropped;
300
- totalSize += next.value.length;
301
- if (totalSize > maxTotalSize) {
302
- throw new MultipartError(`Upload exceeds maxTotalSize (${maxTotalSize} bytes)`, 'UPLOAD_TOO_LARGE', 413);
303
- }
304
- }
305
-
306
- if (boundaryAt > bodyStart) {
307
- bodyChunks.push(buffer.slice(bodyStart, boundaryAt));
308
- bodySize += boundaryAt - bodyStart;
309
- }
310
- // The in-loop check only runs while the delimiter is still missing,
311
- // so a body that arrives in one chunk skipped it entirely.
312
- if (bodySize > maxFileSize) {
313
- throw new MultipartError(
314
- filename !== undefined
315
- ? `File "${filename}" exceeds maxFileSize (${maxFileSize} bytes)`
316
- : `Field "${name}" exceeds maxFileSize (${maxFileSize} bytes)`,
317
- 'FILE_TOO_LARGE',
318
- 413,
319
- );
320
- }
321
-
322
- const isFile = filename !== undefined;
323
- const safeName = isFile ? extractFilenamePath(filename as string) : undefined;
324
- if (isFile) {
325
- fileCount++;
326
- if (fileCount > maxFiles) {
327
- throw new MultipartError(`Too many files (max ${maxFiles})`, 'TOO_MANY_FILES', 413);
328
- }
329
- if (options.allowedMimeTypes && contentType && !mimeAllowed(contentType, options.allowedMimeTypes)) {
330
- throw new MultipartError(`File type not allowed: ${contentType}`, 'FILE_TYPE_NOT_ALLOWED', 415);
331
- }
332
- if (options.allowedExtensions?.length && safeName) {
333
- const dot = safeName.lastIndexOf('.');
334
- const ext = dot === -1 ? '' : lower(safeName.slice(dot));
335
- const ok = options.allowedExtensions.some((e) => lower(e) === ext || lower(e) === `.${ext.slice(1)}`);
336
- if (!ok) {
337
- throw new MultipartError(`File extension not allowed: ${ext || '(none)'}`, 'FILE_TYPE_NOT_ALLOWED', 415);
338
- }
339
- }
340
- } else {
341
- fieldCount++;
342
- if (fieldCount > maxFields) {
343
- throw new MultipartError(`Too many form fields (max ${maxFields})`, 'TOO_MANY_FIELDS', 400);
344
- }
345
- }
346
-
347
- const merged = concat(bodyChunks, bodySize);
348
- const value: string | File = isFile
349
- ? new File([merged as BlobPart], safeName ?? 'upload', { type: contentType || 'application/octet-stream' })
350
- : decoder.decode(merged);
351
-
352
- parts.push({
353
- name: encodeName(name),
354
- filename: isFile ? safeName : undefined,
355
- contentType,
356
- headers,
357
- value,
358
- size: bodySize,
359
- isFile,
360
- });
361
-
362
- // Advance past the delimiter and the CRLF that follows it.
363
- let after = boundaryAt + 2 + delimiter.length;
364
- if (buffer[after] === DASH && buffer[after + 1] === DASH) {
365
- // closing boundary "--" — done
366
- compact();
367
- return finish(parts, totalSize);
368
- }
369
- if (buffer[after] === CR && buffer[after + 1] === LF) after += 2;
370
- consumed = after;
371
-
372
- if (parts.length > maxFiles + maxFields + 8) {
373
- throw new MultipartError('Too many multipart parts', 'TOO_MANY_FIELDS', 400);
374
- }
375
- }
376
- } finally {
377
- try { reader.releaseLock(); } catch { /* already released */ }
378
- }
379
- }
380
-
381
- /** Locate `\r\n--boundary` at or after `from`. */
382
- function findDelimiter(buffer: Uint8Array, delimiter: Uint8Array, from: number): number {
383
- const needle = new Uint8Array(2 + delimiter.length);
384
- needle[0] = CR;
385
- needle[1] = LF;
386
- needle.set(delimiter, 2);
387
- // A candidate only counts when the bytes AFTER the delimiter are valid:
388
- // CRLF (another part follows) or "--" (final boundary). Without this a
389
- // file whose contents happen to contain "\r\n--<boundary>" was cut short.
390
- for (let i = from; i <= buffer.length - needle.length; i++) {
391
- let match = true;
392
- for (let k = 0; k < needle.length; k++) {
393
- if (buffer[i + k] !== needle[k]) { match = false; break; }
394
- }
395
- if (!match) continue;
396
- const after = i + needle.length;
397
- const nextIsCrlf = buffer[after] === CR && buffer[after + 1] === LF;
398
- const nextIsFinal = buffer[after] === DASH && buffer[after + 1] === DASH;
399
- if (nextIsCrlf || nextIsFinal) return i;
400
- }
401
- return -1;
402
- }
403
-
404
- function concat(chunks: Uint8Array[], size: number): Uint8Array {
405
- if (chunks.length === 1) return chunks[0];
406
- const out = new Uint8Array(size);
407
- let off = 0;
408
- for (const c of chunks) {
409
- out.set(c, off);
410
- off += c.length;
411
- }
412
- return out;
413
- }
414
-
415
- function finish(parts: MultipartPart[], totalSize: number): ParsedMultipart {
416
- return {
417
- parts,
418
- files: parts.filter(p => p.isFile),
419
- fields: parts.filter(p => !p.isFile),
420
- totalSize,
421
- };
422
- }
423
-
424
- /**
425
- * Extract the boundary token from a `Content-Type` header. Returns null when
426
- * the header is not multipart or carries no boundary.
427
- */
428
- export function boundaryFromContentType(contentType: string | null | undefined): string | null {
429
- if (!contentType) return null;
430
- // Only the media type and the parameter NAME are case-insensitive; the
431
- // boundary VALUE is opaque and must survive verbatim (lowercasing it
432
- // changed the delimiter the parser then searched for).
433
- if (!lower(contentType.trim()).startsWith('multipart/')) return null;
434
- for (const chunk of contentType.split(';')) {
435
- const trimmed = chunk.trim();
436
- const eq = trimmed.indexOf('=');
437
- if (eq === -1) continue;
438
- if (lower(trimmed.slice(0, eq).trim()) !== 'boundary') continue;
439
- let v = trimmed.slice(eq + 1).trim();
440
- if (v.startsWith('"') && v.endsWith('"')) v = v.slice(1, -1);
441
- return v.length > 0 ? v : null;
442
- }
443
- return null;
444
- }
445
-
446
- /** Convenience: parse straight from a `Request` using its content-type. */
447
- export async function parseMultipartRequest(
448
- request: Request,
449
- options: MultipartOptions = {},
450
- ): Promise<ParsedMultipart> {
451
- if (!request.body) throw new MultipartError('Request has no body', 'NO_BODY', 400);
452
- const boundary = boundaryFromContentType(request.headers.get('content-type'));
453
- if (!boundary) {
454
- throw new MultipartError('Not a multipart request (missing boundary)', 'NOT_MULTIPART', 415);
455
- }
456
- return parseMultipart(request.body as ReadableStream<Uint8Array>, boundary, options);
457
- }
458
-
459
- /**
460
- * Shape a parsed upload for `withValidation()`-style code and for
461
- * `Object.fromEntries`-style flattening WITHOUT destroying `File` values.
462
- * Repeated field names collect into an array.
463
- */
464
- export function toRecord(parsed: ParsedMultipart): Record<string, string | File | Array<string | File>> {
465
- const out: Record<string, string | File | Array<string | File>> = {};
466
- for (const part of parsed.parts) {
467
- const existing = out[part.name];
468
- if (existing === undefined) {
469
- out[part.name] = part.value;
470
- } else if (Array.isArray(existing)) {
471
- existing.push(part.value);
472
- } else {
473
- out[part.name] = [existing as string | File, part.value];
474
- }
475
- }
476
- return out;
477
- }
478
-
479
- export { ZERO as _ZERO };
@@ -1,12 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "noEmit": false,
5
- "outDir": "dist",
6
- "rootDir": "src",
7
- "sourceMap": false,
8
- "paths": {}
9
- },
10
- "include": ["src/**/*"],
11
- "exclude": ["node_modules", "dist", "src/**/*.test.ts", "src/**/*.test.js"]
12
- }
package/tsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "dist",
5
- "rootDir": "src"
6
- },
7
- "include": ["src/**/*"]
8
- }