@vesk/plugin-multipart 0.0.0-stage → 0.2.67
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/package.json +34 -4
- package/src/index.ts +125 -0
- package/src/parser.test.ts +263 -0
- package/src/parser.ts +479 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +8 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vesk/plugin-multipart",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.2.67",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Streaming multipart/form-data uploads for Vesk — real File values, per-field size/type limits, no full-body buffering",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./parser": {
|
|
14
|
+
"types": "./dist/parser.d.ts",
|
|
15
|
+
"default": "./dist/parser.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc -p tsconfig.build.json",
|
|
20
|
+
"prepublishOnly": "npm run build",
|
|
21
|
+
"test": "tsx src/parser.test.ts",
|
|
22
|
+
"typecheck": "tsc --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@vesk/types": "^0.2.67"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.8.0"
|
|
29
|
+
},
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "opencode",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/emeraldlinks/veskTs.git"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
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 };
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import {
|
|
4
|
+
parseMultipart,
|
|
5
|
+
parseMultipartRequest,
|
|
6
|
+
boundaryFromContentType,
|
|
7
|
+
toRecord,
|
|
8
|
+
MultipartError,
|
|
9
|
+
} from './parser.js';
|
|
10
|
+
|
|
11
|
+
const CRLF = '\r\n';
|
|
12
|
+
const B = '----veskTestBoundary9x';
|
|
13
|
+
|
|
14
|
+
function streamOf(chunks: Uint8Array[]): ReadableStream<Uint8Array> {
|
|
15
|
+
return new ReadableStream<Uint8Array>({
|
|
16
|
+
start(controller) {
|
|
17
|
+
for (const c of chunks) controller.enqueue(c);
|
|
18
|
+
controller.close();
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function bodyOf(text: string): ReadableStream<Uint8Array> {
|
|
24
|
+
return streamOf([new TextEncoder().encode(text)]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Split a text body into `n` chunks to exercise the incremental path. */
|
|
28
|
+
function chunked(text: string, n: number): ReadableStream<Uint8Array> {
|
|
29
|
+
const bytes = new TextEncoder().encode(text);
|
|
30
|
+
const size = Math.ceil(bytes.length / n) || 1;
|
|
31
|
+
const chunks: Uint8Array[] = [];
|
|
32
|
+
for (let i = 0; i < bytes.length; i += size) chunks.push(bytes.slice(i, i + size));
|
|
33
|
+
return streamOf(chunks);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('boundaryFromContentType', () => {
|
|
37
|
+
it('reads a plain boundary', () => {
|
|
38
|
+
assert.equal(boundaryFromContentType(`multipart/form-data; boundary=${B}`), B);
|
|
39
|
+
});
|
|
40
|
+
it('reads a quoted boundary', () => {
|
|
41
|
+
assert.equal(boundaryFromContentType(`multipart/form-data; boundary="${B}"`), B);
|
|
42
|
+
});
|
|
43
|
+
it('is case-insensitive on the parameter name', () => {
|
|
44
|
+
assert.equal(boundaryFromContentType(`MULTIPART/FORM-DATA; BOUNDARY=${B}`), B);
|
|
45
|
+
});
|
|
46
|
+
it('returns null for non-multipart', () => {
|
|
47
|
+
assert.equal(boundaryFromContentType('application/json'), null);
|
|
48
|
+
});
|
|
49
|
+
it('returns null when boundary is absent', () => {
|
|
50
|
+
assert.equal(boundaryFromContentType('multipart/form-data'), null);
|
|
51
|
+
});
|
|
52
|
+
it('returns null for a null header', () => {
|
|
53
|
+
assert.equal(boundaryFromContentType(null), null);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('parseMultipart', () => {
|
|
58
|
+
it('parses a single text field', async () => {
|
|
59
|
+
const body = `--${B}${CRLF}Content-Disposition: form-data; name="title"${CRLF}${CRLF}Hello${CRLF}--${B}--${CRLF}`;
|
|
60
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
61
|
+
assert.equal(r.parts.length, 1);
|
|
62
|
+
assert.equal(r.parts[0].name, 'title');
|
|
63
|
+
assert.equal(r.parts[0].value, 'Hello');
|
|
64
|
+
assert.equal(r.parts[0].isFile, false);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('parses several fields in order', async () => {
|
|
68
|
+
const body =
|
|
69
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="a"${CRLF}${CRLF}1${CRLF}` +
|
|
70
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="b"${CRLF}${CRLF}2${CRLF}` +
|
|
71
|
+
`--${B}--${CRLF}`;
|
|
72
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
73
|
+
assert.deepEqual(r.fields.map(p => [p.name, p.value]), [['a', '1'], ['b', '2']]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('preserves File values (the core Object.fromEntries bug)', async () => {
|
|
77
|
+
const body =
|
|
78
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="doc"; filename="report.pdf"${CRLF}` +
|
|
79
|
+
`Content-Type: application/pdf${CRLF}${CRLF}PDFDATA${CRLF}--${B}--${CRLF}`;
|
|
80
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
81
|
+
assert.equal(r.files.length, 1);
|
|
82
|
+
const f = r.files[0].value as File;
|
|
83
|
+
assert.ok(f instanceof File, 'value must be a real File, not an opaque string');
|
|
84
|
+
assert.equal(f.name, 'report.pdf');
|
|
85
|
+
assert.equal(f.type, 'application/pdf');
|
|
86
|
+
assert.equal(await f.text(), 'PDFDATA');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('keeps binary bytes intact through the stream', async () => {
|
|
90
|
+
const bytes = new Uint8Array(256);
|
|
91
|
+
for (let i = 0; i < 256; i++) bytes[i] = i;
|
|
92
|
+
const head = new TextEncoder().encode(
|
|
93
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="b.bin"${CRLF}` +
|
|
94
|
+
`Content-Type: application/octet-stream${CRLF}${CRLF}`,
|
|
95
|
+
);
|
|
96
|
+
const tail = new TextEncoder().encode(`${CRLF}--${B}--${CRLF}`);
|
|
97
|
+
const r = await parseMultipart(streamOf([head, bytes, tail]), B);
|
|
98
|
+
const got = new Uint8Array((r.files[0].value as File).size);
|
|
99
|
+
await (r.files[0].value as Blob).arrayBuffer().then((ab) => got.set(new Uint8Array(ab)));
|
|
100
|
+
assert.deepEqual([...got], [...bytes]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('parses correctly when the body arrives in many chunks', async () => {
|
|
104
|
+
const body =
|
|
105
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="title"${CRLF}${CRLF}Hello world${CRLF}` +
|
|
106
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="x.txt"${CRLF}` +
|
|
107
|
+
`Content-Type: text/plain${CRLF}${CRLF}chunky${CRLF}--${B}--${CRLF}`;
|
|
108
|
+
for (const n of [1, 3, 7, 16, 64, 512]) {
|
|
109
|
+
const r = await parseMultipart(chunked(body, n), B);
|
|
110
|
+
assert.equal(r.fields[0].value, 'Hello world', `chunked by ${n}`);
|
|
111
|
+
assert.equal((r.files[0].value as File).name, 'x.txt', `chunked by ${n}`);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('handles a boundary-like sequence inside the file body', async () => {
|
|
116
|
+
const inner = `${CRLF}--${B}x`;
|
|
117
|
+
const body =
|
|
118
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="t.txt"${CRLF}` +
|
|
119
|
+
`Content-Type: text/plain${CRLF}${CRLF}before${inner}after${CRLF}--${B}--${CRLF}`;
|
|
120
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
121
|
+
assert.equal(await (r.files[0].value as File).text(), `before${inner}after`);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('strips a directory path from the filename', async () => {
|
|
125
|
+
const body =
|
|
126
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="../../etc/passwd"${CRLF}` +
|
|
127
|
+
`Content-Type: text/plain${CRLF}${CRLF}x${CRLF}--${B}--${CRLF}`;
|
|
128
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
129
|
+
assert.equal((r.files[0].value as File).name, 'passwd');
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('handles an empty field value', async () => {
|
|
133
|
+
const body = `--${B}${CRLF}Content-Disposition: form-data; name="empty"${CRLF}${CRLF}${CRLF}--${B}--${CRLF}`;
|
|
134
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
135
|
+
assert.equal(r.fields[0].value, '');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('rejects a file over maxFileSize with 413', async () => {
|
|
139
|
+
const body =
|
|
140
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="big.bin"${CRLF}` +
|
|
141
|
+
`Content-Type: application/octet-stream${CRLF}${CRLF}` +
|
|
142
|
+
'x'.repeat(5000) + `${CRLF}--${B}--${CRLF}`;
|
|
143
|
+
await assert.rejects(
|
|
144
|
+
() => parseMultipart(bodyOf(body), B, { maxFileSize: 1000 }),
|
|
145
|
+
(e: MultipartError) => e.status === 413 && e.code === 'FILE_TOO_LARGE',
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('rejects a body over maxTotalSize with 413', async () => {
|
|
150
|
+
const body =
|
|
151
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="big.bin"${CRLF}` +
|
|
152
|
+
`Content-Type: application/octet-stream${CRLF}${CRLF}` +
|
|
153
|
+
'x'.repeat(5000) + `${CRLF}--${B}--${CRLF}`;
|
|
154
|
+
await assert.rejects(
|
|
155
|
+
() => parseMultipart(bodyOf(body), B, { maxTotalSize: 2000 }),
|
|
156
|
+
(e: MultipartError) => e.status === 413 && e.code === 'UPLOAD_TOO_LARGE',
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('rejects too many files with 413', async () => {
|
|
161
|
+
const mk = (i: number) =>
|
|
162
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f${i}"; filename="f${i}.txt"${CRLF}` +
|
|
163
|
+
`Content-Type: text/plain${CRLF}${CRLF}v${CRLF}`;
|
|
164
|
+
const body = mk(1) + mk(2) + mk(3) + `--${B}--${CRLF}`;
|
|
165
|
+
await assert.rejects(
|
|
166
|
+
() => parseMultipart(bodyOf(body), B, { maxFiles: 2 }),
|
|
167
|
+
(e: MultipartError) => e.status === 413 && e.code === 'TOO_MANY_FILES',
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('rejects too many fields with 400', async () => {
|
|
172
|
+
const mk = (i: number) =>
|
|
173
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f${i}"${CRLF}${CRLF}v${CRLF}`;
|
|
174
|
+
const body = mk(1) + mk(2) + mk(3) + `--${B}--${CRLF}`;
|
|
175
|
+
await assert.rejects(
|
|
176
|
+
() => parseMultipart(bodyOf(body), B, { maxFields: 2 }),
|
|
177
|
+
(e: MultipartError) => e.status === 400 && e.code === 'TOO_MANY_FIELDS',
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('rejects a disallowed mime type with 415', async () => {
|
|
182
|
+
const body =
|
|
183
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="a.exe"${CRLF}` +
|
|
184
|
+
`Content-Type: application/x-msdownload${CRLF}${CRLF}MZ${CRLF}--${B}--${CRLF}`;
|
|
185
|
+
await assert.rejects(
|
|
186
|
+
() => parseMultipart(bodyOf(body), B, { allowedMimeTypes: ['image/*'] }),
|
|
187
|
+
(e: MultipartError) => e.status === 415 && e.code === 'FILE_TYPE_NOT_ALLOWED',
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('accepts a wildcard mime type', async () => {
|
|
192
|
+
const body =
|
|
193
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="a.png"${CRLF}` +
|
|
194
|
+
`Content-Type: image/png${CRLF}${CRLF}PNG${CRLF}--${B}--${CRLF}`;
|
|
195
|
+
const r = await parseMultipart(bodyOf(body), B, { allowedMimeTypes: ['image/*'] });
|
|
196
|
+
assert.equal(r.files.length, 1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('rejects a disallowed extension with 415', async () => {
|
|
200
|
+
const body =
|
|
201
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="a.php"${CRLF}` +
|
|
202
|
+
`Content-Type: text/plain${CRLF}${CRLF}x${CRLF}--${B}--${CRLF}`;
|
|
203
|
+
await assert.rejects(
|
|
204
|
+
() => parseMultipart(bodyOf(body), B, { allowedExtensions: ['.png', '.jpg'] }),
|
|
205
|
+
(e: MultipartError) => e.status === 415,
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('rejects a truncated body with 400', async () => {
|
|
210
|
+
const body = `--${B}${CRLF}Content-Disposition: form-data; name="f"${CRLF}${CRLF}never ends`;
|
|
211
|
+
await assert.rejects(
|
|
212
|
+
() => parseMultipart(bodyOf(body), B),
|
|
213
|
+
(e: MultipartError) => e.status === 400,
|
|
214
|
+
);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
describe('parseMultipartRequest', () => {
|
|
219
|
+
it('parses a real Request', async () => {
|
|
220
|
+
const body =
|
|
221
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="a"${CRLF}${CRLF}1${CRLF}` +
|
|
222
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="n.txt"${CRLF}` +
|
|
223
|
+
`Content-Type: text/plain${CRLF}${CRLF}n${CRLF}--${B}--${CRLF}`;
|
|
224
|
+
const req = new Request('http://x/', {
|
|
225
|
+
method: 'POST',
|
|
226
|
+
headers: { 'content-type': `multipart/form-data; boundary=${B}` },
|
|
227
|
+
body,
|
|
228
|
+
});
|
|
229
|
+
const r = await parseMultipartRequest(req);
|
|
230
|
+
assert.equal(r.fields[0].value, '1');
|
|
231
|
+
assert.equal((r.files[0].value as File).name, 'n.txt');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('rejects a non-multipart request with 415', async () => {
|
|
235
|
+
const req = new Request('http://x/', { method: 'POST', body: '{}' });
|
|
236
|
+
await assert.rejects(
|
|
237
|
+
() => parseMultipartRequest(req),
|
|
238
|
+
(e: MultipartError) => e.status === 415 && e.code === 'NOT_MULTIPART',
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
describe('toRecord', () => {
|
|
244
|
+
it('keeps File values (unlike Object.fromEntries)', async () => {
|
|
245
|
+
const body =
|
|
246
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="t"${CRLF}${CRLF}v${CRLF}` +
|
|
247
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="f"; filename="a.txt"${CRLF}` +
|
|
248
|
+
`Content-Type: text/plain${CRLF}${CRLF}A${CRLF}--${B}--${CRLF}`;
|
|
249
|
+
const r = await parseMultipart(bodyOf(body), B);
|
|
250
|
+
const rec = toRecord(r);
|
|
251
|
+
assert.equal(rec.t, 'v');
|
|
252
|
+
assert.ok(rec.f instanceof File);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('collects repeated names into an array', async () => {
|
|
256
|
+
const body =
|
|
257
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="tag"${CRLF}${CRLF}a${CRLF}` +
|
|
258
|
+
`--${B}${CRLF}Content-Disposition: form-data; name="tag"${CRLF}${CRLF}b${CRLF}` +
|
|
259
|
+
`--${B}--${CRLF}`;
|
|
260
|
+
const rec = toRecord(await parseMultipart(bodyOf(body), B));
|
|
261
|
+
assert.deepEqual(rec.tag, ['a', 'b']);
|
|
262
|
+
});
|
|
263
|
+
});
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,479 @@
|
|
|
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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
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
ADDED
package/README.md
DELETED