@vesk/plugin-multipart 0.2.70 → 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.
@@ -1,263 +0,0 @@
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
- });