ovr 6.2.5 → 7.0.0-next.0

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.
Files changed (136) hide show
  1. package/README.md +3 -1
  2. package/dist/app/index.d.ts +51 -7
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +163 -28
  5. package/dist/auth/auth-data.d.ts +26 -0
  6. package/dist/auth/auth-data.d.ts.map +1 -0
  7. package/dist/auth/auth-data.js +42 -0
  8. package/dist/auth/cbor.d.ts +50 -0
  9. package/dist/auth/cbor.d.ts.map +1 -0
  10. package/dist/auth/cbor.js +200 -0
  11. package/dist/auth/client-source.d.ts +3 -0
  12. package/dist/auth/client-source.d.ts.map +1 -0
  13. package/dist/auth/client-source.js +3 -0
  14. package/dist/auth/client.d.ts +3 -0
  15. package/dist/auth/client.d.ts.map +1 -0
  16. package/dist/auth/client.js +186 -0
  17. package/dist/auth/configure.d.ts +28 -0
  18. package/dist/auth/configure.d.ts.map +1 -0
  19. package/dist/auth/configure.js +228 -0
  20. package/dist/auth/der.d.ts +6 -0
  21. package/dist/auth/der.d.ts.map +1 -0
  22. package/dist/auth/der.js +36 -0
  23. package/dist/auth/index.d.ts +242 -0
  24. package/dist/auth/index.d.ts.map +1 -0
  25. package/dist/auth/index.js +152 -0
  26. package/dist/auth/issue.d.ts +36 -0
  27. package/dist/auth/issue.d.ts.map +1 -0
  28. package/dist/auth/issue.js +47 -0
  29. package/dist/auth/metadata.d.ts +22 -0
  30. package/dist/auth/metadata.d.ts.map +1 -0
  31. package/dist/auth/metadata.js +134 -0
  32. package/dist/auth/oauth.d.ts +29 -0
  33. package/dist/auth/oauth.d.ts.map +1 -0
  34. package/dist/auth/oauth.js +724 -0
  35. package/dist/auth/passkey.d.ts +101 -0
  36. package/dist/auth/passkey.d.ts.map +1 -0
  37. package/dist/auth/passkey.js +393 -0
  38. package/dist/auth/state.d.ts +49 -0
  39. package/dist/auth/state.d.ts.map +1 -0
  40. package/dist/auth/state.js +184 -0
  41. package/dist/auth/storage.d.ts +193 -0
  42. package/dist/auth/storage.d.ts.map +1 -0
  43. package/dist/auth/storage.js +410 -0
  44. package/dist/auth/token.d.ts +12 -0
  45. package/dist/auth/token.d.ts.map +1 -0
  46. package/dist/auth/token.js +39 -0
  47. package/dist/context/index.d.ts +86 -16
  48. package/dist/context/index.d.ts.map +1 -1
  49. package/dist/context/index.js +166 -42
  50. package/dist/cookie/index.d.ts +3 -0
  51. package/dist/cookie/index.d.ts.map +1 -1
  52. package/dist/cookie/index.js +1 -0
  53. package/dist/database/index.d.ts +262 -0
  54. package/dist/database/index.d.ts.map +1 -0
  55. package/dist/database/index.js +807 -0
  56. package/dist/form/index.d.ts +699 -0
  57. package/dist/form/index.d.ts.map +1 -0
  58. package/dist/form/index.js +901 -0
  59. package/dist/index.d.ts +10 -3
  60. package/dist/index.d.ts.map +1 -1
  61. package/dist/index.js +9 -3
  62. package/dist/jsx/elements.d.ts +28 -2
  63. package/dist/jsx/elements.d.ts.map +1 -1
  64. package/dist/jsx/index.d.ts +20 -4
  65. package/dist/jsx/index.d.ts.map +1 -1
  66. package/dist/jsx/index.js +6 -65
  67. package/dist/limiter/index.d.ts +65 -0
  68. package/dist/limiter/index.d.ts.map +1 -0
  69. package/dist/limiter/index.js +157 -0
  70. package/dist/markdown/index.d.ts +61 -0
  71. package/dist/markdown/index.d.ts.map +1 -0
  72. package/dist/markdown/index.js +1160 -0
  73. package/dist/markup/index.d.ts +188 -0
  74. package/dist/markup/index.d.ts.map +1 -0
  75. package/dist/markup/index.js +718 -0
  76. package/dist/mcp/index.d.ts +73 -0
  77. package/dist/mcp/index.d.ts.map +1 -0
  78. package/dist/mcp/index.js +988 -0
  79. package/dist/middleware/index.d.ts +3 -4
  80. package/dist/middleware/index.d.ts.map +1 -1
  81. package/dist/multipart/index.d.ts +53 -7
  82. package/dist/multipart/index.d.ts.map +1 -1
  83. package/dist/multipart/index.js +289 -123
  84. package/dist/route/index.d.ts +226 -80
  85. package/dist/route/index.d.ts.map +1 -1
  86. package/dist/route/index.js +239 -99
  87. package/dist/schema/index.d.ts +307 -734
  88. package/dist/schema/index.d.ts.map +1 -1
  89. package/dist/schema/index.js +783 -1050
  90. package/dist/table/index.d.ts +132 -0
  91. package/dist/table/index.d.ts.map +1 -0
  92. package/dist/table/index.js +188 -0
  93. package/dist/trie/index.js +4 -4
  94. package/dist/util/index.d.ts +37 -5
  95. package/dist/util/index.d.ts.map +1 -1
  96. package/dist/util/index.js +156 -41
  97. package/package.json +11 -4
  98. package/src/app/index.ts +230 -42
  99. package/src/auth/auth-data.ts +56 -0
  100. package/src/auth/cbor.ts +231 -0
  101. package/src/auth/client-source.ts +3 -0
  102. package/src/auth/client.ts +205 -0
  103. package/src/auth/configure.ts +279 -0
  104. package/src/auth/der.ts +35 -0
  105. package/src/auth/index.ts +387 -0
  106. package/src/auth/issue.ts +52 -0
  107. package/src/auth/metadata.ts +149 -0
  108. package/src/auth/oauth.ts +882 -0
  109. package/src/auth/passkey.ts +556 -0
  110. package/src/auth/state.ts +213 -0
  111. package/src/auth/storage.ts +572 -0
  112. package/src/auth/token.ts +67 -0
  113. package/src/context/index.ts +240 -65
  114. package/src/cookie/index.ts +3 -0
  115. package/src/database/index.ts +1193 -0
  116. package/src/form/index.ts +1720 -0
  117. package/src/index.ts +10 -3
  118. package/src/jsx/elements.ts +45 -25
  119. package/src/jsx/index.ts +24 -69
  120. package/src/limiter/index.ts +221 -0
  121. package/src/markdown/index.ts +1322 -0
  122. package/src/markup/index.ts +853 -0
  123. package/src/mcp/conformance.2025-11-25.yml +39 -0
  124. package/src/mcp/conformance.2026-07-28.yml +56 -0
  125. package/src/mcp/index.ts +1262 -0
  126. package/src/middleware/index.ts +5 -6
  127. package/src/multipart/index.ts +360 -148
  128. package/src/route/index.ts +559 -278
  129. package/src/schema/index.ts +1542 -2256
  130. package/src/table/index.ts +387 -0
  131. package/src/trie/index.ts +4 -4
  132. package/src/util/index.ts +163 -49
  133. package/dist/render/index.d.ts +0 -79
  134. package/dist/render/index.d.ts.map +0 -1
  135. package/dist/render/index.js +0 -265
  136. package/src/render/index.ts +0 -318
@@ -1,32 +1,56 @@
1
1
  import { Codec, Header, Mime, Size } from "../util/index.js";
2
2
 
3
+ /** A request body over one of the parser's limits. */
4
+ export class Limit extends RangeError {}
5
+
6
+ /** A request that is not valid multipart form data, the client's error. */
7
+ export class Malformed extends TypeError {}
8
+
9
+ /** A request body of another type than multipart form data. */
10
+ export class Unsupported extends Malformed {}
11
+
3
12
  /** Sequence of bytes to find within the stream */
4
13
  class Needle extends Uint8Array {
5
14
  /** Index of the last character in the needle */
6
15
  readonly end = this.length - 1;
7
16
 
8
17
  /**
9
- * Stores the how far from the last character each char in the needle is so
10
- * the iterator know how far to safely skip forward when the character is
11
- * found the rest of the array is filled with the length (default skip)
18
+ * How far the search can move on from the byte it reads where the needle
19
+ * would end: from that byte's last place in the needle to the needle's end,
20
+ * or the whole length for a byte the needle does not have. Most bytes of a
21
+ * file are not in its boundary, so most of a file is never read.
12
22
  */
13
23
  readonly skip = new Uint8Array(256).fill(this.length);
14
24
 
15
25
  /** Stores where each byte is located in the needle */
16
26
  readonly loc: (number[] | undefined)[] = new Array(256);
17
27
 
18
- /** @param needle String to find within the stream */
28
+ /**
29
+ * Index of a byte that occurs once in the needle, such as the `\r` of a
30
+ * boundary, searched for first. A match can only start at its occurrences,
31
+ * and comparing from each is linear in the haystack whatever the client
32
+ * chose as its boundary.
33
+ */
34
+ readonly anchor: number;
35
+
36
+ /** @param needle String to find within the stream, at most 255 bytes */
19
37
  constructor(needle: string) {
20
38
  super(Codec.encode(needle).buffer);
21
39
 
40
+ // boundaries are at most 70 characters, checked before a needle is made
41
+
22
42
  for (let i = 0; i < this.length; i++) {
23
43
  const byte = this[i]!;
24
44
 
25
- // skip the last char of the needle since that would be a find
45
+ // the last byte is where the search reads, so it moves on from before it
26
46
  if (i !== this.end) this.skip[byte] = this.end - i;
27
47
 
28
48
  (this.loc[byte] ??= []).push(i);
29
49
  }
50
+
51
+ // a short needle without a unique byte, such as "\r\n\r\n", starts at 0
52
+ const unique = this.findIndex((byte) => this.loc[byte]!.length === 1);
53
+ this.anchor = unique === -1 ? 0 : unique;
30
54
  }
31
55
  }
32
56
 
@@ -62,6 +86,9 @@ class Part extends Request {
62
86
  /** Part body */
63
87
  declare readonly body: ReadableStream<Uint8Array<ArrayBuffer>>;
64
88
 
89
+ /** Signal of the request the part is from */
90
+ readonly #signal: AbortSignal;
91
+
65
92
  /**
66
93
  * Create a new multipart part
67
94
  *
@@ -72,8 +99,14 @@ class Part extends Request {
72
99
  constructor(req: Request, body: ReadableStream, rawHeaders: Uint8Array) {
73
100
  const headers: [string, string][] = [];
74
101
 
102
+ // each byte as one character, so `Headers` accepts the UTF-8 bytes
103
+ // browsers send in names and filenames, decoded below
104
+ let text = "";
105
+ for (let i = 0; i < rawHeaders.length; i++)
106
+ text += String.fromCharCode(rawHeaders[i]!);
107
+
75
108
  // create headers
76
- for (const line of Codec.decode(rawHeaders).split("\r\n")) {
109
+ for (const line of text.split("\r\n")) {
77
110
  const colon = line.indexOf(":");
78
111
 
79
112
  if (colon !== -1) {
@@ -85,18 +118,60 @@ class Part extends Request {
85
118
  }
86
119
  }
87
120
 
88
- super(req, {
89
- headers,
121
+ let parsed: Headers;
122
+ try {
123
+ parsed = new Headers(headers);
124
+ } catch {
125
+ // an invalid name or value, the client's error
126
+ throw new Malformed("Invalid Part Header");
127
+ }
128
+
129
+ // from its parts, since a server's own request object, such as srvx's,
130
+ // is not one the platform's constructor can copy
131
+ super(req.url, {
132
+ method: req.method,
133
+ headers: parsed,
90
134
  body,
135
+ // A request made from another follows its signal with a listener,
136
+ // and each listener added is compared with all those before it, so
137
+ // the cost of a form grew with the square of its number of parts.
138
+ // The part has the request's own signal instead, see `signal`.
139
+ signal: null,
91
140
  // @ts-expect-error - streaming https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#duplex
92
141
  duplex: "half",
93
142
  });
94
143
 
144
+ this.#signal = req.signal;
145
+
95
146
  [this.type] = Header.shift(this.headers.get(Header.name.type));
96
147
 
97
- ({ name: this.name = null, filename: this.filename = null } = Header.params(
148
+ const { name, filename } = Header.params(
98
149
  this.headers.get(Header.name.disp),
99
- ));
150
+ true,
151
+ );
152
+ this.name = Part.#utf8(name);
153
+ this.filename = Part.#utf8(filename);
154
+ }
155
+
156
+ /** Signal of the request the part is from, the same one for each part */
157
+ override get signal() {
158
+ return this.#signal;
159
+ }
160
+
161
+ /**
162
+ * @param value Header parameter, one character per byte
163
+ * @returns The UTF-8 text it encodes, or `null` without one
164
+ */
165
+ static #utf8(value: string | undefined) {
166
+ if (value === undefined) return null;
167
+ // most names are ASCII, which is the same text in UTF-8
168
+ if (!/[^\0-\x7f]/.test(value)) return value;
169
+ try {
170
+ return Codec.decode(Uint8Array.from(value, (c) => c.charCodeAt(0)));
171
+ } catch {
172
+ // not UTF-8, the client's error
173
+ throw new Malformed("Invalid Part Header");
174
+ }
100
175
  }
101
176
 
102
177
  /**
@@ -118,13 +193,18 @@ export namespace Multipart {
118
193
  /** Multipart options */
119
194
  export type Options = {
120
195
  /**
121
- * Maximum memory allocation for request body processing (default 4MB)
196
+ * Most memory the parser holds at once as it reads the body (default 4MB)
122
197
  *
123
198
  * Even for large files, the parser does not hold the entire file in memory,
124
199
  * it processes each chunk of the request body as it arrives.
125
200
  * Set to a larger number if for example clients are sending all the
126
201
  * data in a massive chunk instead of broken into smaller packets.
127
202
  *
203
+ * It bounds the parser, not what is read from it: `data()`, and
204
+ * `c.data()` in a route, keep each value, a file's included, so they are
205
+ * bounded by `payload`. An app's `multipart.memory` is also the largest request
206
+ * its MCP endpoint reads.
207
+ *
128
208
  * @default 4 * 1024 * 1024
129
209
  * @example
130
210
  *
@@ -168,22 +248,70 @@ export namespace Multipart {
168
248
  parts?: number | undefined;
169
249
  };
170
250
 
171
- /** Type for a `Part` of the multipart body */
172
- export type Part = InstanceType<typeof Part>;
251
+ // interfaces, so that declaration files name them `Multipart.Part` and
252
+ // so on, which an alias of the class would not be
253
+
254
+ /** A `Part` of the multipart body */
255
+ export interface Part extends PartClass {}
256
+
257
+ /** A body over a limit */
258
+ export interface Limit extends LimitClass {}
259
+
260
+ /** Malformed form data */
261
+ export interface Malformed extends MalformedClass {}
262
+
263
+ /** A content type other than multipart form data */
264
+ export interface Unsupported extends UnsupportedClass {}
173
265
  }
174
266
 
175
- /** Multipart request */
176
- export class Multipart extends Request {
177
- /** New line needle to share across requests and parts */
178
- static readonly #newLine = new Needle("\r\n\r\n");
267
+ /** The classes `Multipart`'s types are named for */
268
+ type PartClass = Part;
269
+ type LimitClass = Limit;
270
+ type MalformedClass = Malformed;
271
+ type UnsupportedClass = Unsupported;
272
+
273
+ /** Default most memory a request body takes while it is read, 4MB */
274
+ export const memory = 4 * Size.mb;
275
+
276
+ /** Message for a body that ends before its closing boundary */
277
+ const unexpectedEnd = "Unexpected End of Body";
278
+
279
+ /**
280
+ * New line needle to share across requests and parts. Outside the class, since
281
+ * a static made as the class loads is a side effect, which keeps `Multipart`
282
+ * in bundles that only use its errors.
283
+ */
284
+ const newLine = new Needle("\r\n\r\n");
285
+
286
+ /**
287
+ * The parts of a multipart request, read one at a time as the body arrives.
288
+ * It reads the request's body, which cannot be read another way after.
289
+ */
290
+ export class Multipart {
291
+ /** A body over a limit, answered 413 when a route does not catch it */
292
+ static readonly Limit: new (message?: string) => Multipart.Limit = Limit;
293
+
294
+ /** Malformed form data, answered 400 when a route does not catch it */
295
+ static readonly Malformed: new (message?: string) => Multipart.Malformed =
296
+ Malformed;
297
+
298
+ /**
299
+ * A content type other than multipart form data, answered 415 when a route
300
+ * does not catch it. A kind of `Malformed`.
301
+ */
302
+ static readonly Unsupported: new (message?: string) => Multipart.Unsupported =
303
+ Unsupported;
179
304
 
180
305
  /** Parser options */
181
306
  readonly #options: Required<Multipart.Options> = {
182
- memory: 4 * Size.mb,
307
+ memory,
183
308
  payload: 16 * Size.mb,
184
309
  parts: Infinity,
185
310
  };
186
311
 
312
+ /** Request the parts are read from */
313
+ readonly #req: Request;
314
+
187
315
  /** Request body reader */
188
316
  readonly #reader: ReadableStreamDefaultReader;
189
317
 
@@ -193,7 +321,7 @@ export class Multipart extends Request {
193
321
  /** Owned storage for headers and boundaries spanning input chunks. */
194
322
  readonly #scratch: Uint8Array<ArrayBuffer>;
195
323
 
196
- /** Opening boundary needle */
324
+ /** Dash and boundary, the first delimiter without a new line before it */
197
325
  readonly #opening: Needle;
198
326
 
199
327
  /** Part boundary needle */
@@ -230,20 +358,22 @@ export class Multipart extends Request {
230
358
  * ```
231
359
  */
232
360
  constructor(req: Request, options?: Multipart.Options) {
233
- super(req);
361
+ const [mime, params] = Header.shift(req.headers.get(Header.name.type));
234
362
 
235
- const [mime, params] = Header.shift(this.headers.get(Header.name.type));
363
+ if (!Mime.multipart(mime)) throw new Unsupported("Unsupported Media Type");
236
364
 
237
- if (!Mime.multipart(mime)) throw new TypeError("Unsupported Media Type");
365
+ const { boundary } = Header.params(params, true);
238
366
 
239
- const { boundary } = Header.params(params);
240
-
241
- if (!boundary) throw new TypeError("Boundary Not Found");
242
- if (!this.body) throw new TypeError("No Request Body");
367
+ if (!boundary) throw new Malformed("Boundary Not Found");
368
+ // RFC 2046: 1 to 70 characters from a limited set, not ending in a space
369
+ if (!/^[\w'()+,./:=? -]{0,69}[\w'()+,./:=?-]$/.test(boundary))
370
+ throw new Malformed("Invalid Boundary");
371
+ if (!req.body) throw new Malformed("No Request Body");
243
372
 
244
373
  Object.assign(this.#options, options);
245
- this.#reader = this.body.getReader();
246
- this.#opening = new Needle(`--${boundary}\r\n`);
374
+ this.#req = req;
375
+ this.#reader = req.body.getReader();
376
+ this.#opening = new Needle(`--${boundary}`);
247
377
  this.#boundary = new Needle(`\r\n--${boundary}`);
248
378
 
249
379
  this.#scratch = new Uint8Array(
@@ -266,13 +396,13 @@ export class Multipart extends Request {
266
396
  if (next.done) return true;
267
397
  const nextLength = next.value.length;
268
398
  if ((this.#payloadSize += nextLength) > this.#options.payload) {
269
- throw new RangeError("Payload Too Large");
399
+ throw new Limit("Payload Too Large");
270
400
  }
271
401
  if (append) {
272
402
  const length = this.#valid - this.#offset;
273
403
  const required = length + nextLength;
274
404
  if (required > this.#scratch.buffer.maxByteLength) {
275
- throw new RangeError("Multipart buffer limit exceeded");
405
+ throw new Limit("Multipart buffer limit exceeded");
276
406
  }
277
407
  if (length || !borrow) {
278
408
  this.#retain();
@@ -329,33 +459,44 @@ export class Multipart extends Request {
329
459
  * @returns If found, shifts the buffer and returns the result.
330
460
  */
331
461
  #find(needle: Needle) {
462
+ const haystack = this.#memory;
332
463
  const haystackLength = this.#valid;
333
-
334
- // start the search at the last char of the needle
335
- // since it could be at the very start
336
- let i = this.#start + needle.end;
337
-
338
- while (i < haystackLength) {
339
- for (
340
- let needleIndex = needle.end, cursor = i;
341
- needleIndex >= 0 && needle[needleIndex] === this.#memory[cursor];
342
- needleIndex--, cursor--
343
- ) {
344
- if (needleIndex === 0) {
345
- this.#start = cursor;
346
- this.#end = cursor + needle.length;
464
+ const { anchor, end, length, skip } = needle;
465
+ const byte = needle[anchor]!;
466
+ const last = needle[end]!;
467
+
468
+ // Reading only where the needle would end skips most of the haystack.
469
+ // `Uint8Array.indexOf` reads every byte of it, and in V8 does so several
470
+ // times slower than a loop, which made large files slow to parse.
471
+ for (
472
+ let i = this.#start + end, current: number;
473
+ i < haystackLength;
474
+ i += skip[current]!
475
+ ) {
476
+ current = haystack[i]!;
477
+
478
+ // the anchor is checked before comparing, so a haystack of near
479
+ // matches costs one read for each of its bytes
480
+ if (current === last && haystack[i - end + anchor] === byte) {
481
+ const start = i - end;
482
+
483
+ let match = 0;
484
+ while (match < length && needle[match] === haystack[start + match])
485
+ match++;
486
+
487
+ if (match === length) {
488
+ this.#start = start;
489
+ this.#end = start + length;
347
490
  return this.#shift();
348
491
  }
349
492
  }
350
-
351
- i += needle.skip[this.#memory[i]!]!;
352
493
  }
353
494
 
354
495
  // where to safely start the next search in the concatenated chunk result
355
496
  // go back len - 1 in case it was partially at the end
356
497
  this.#start = this.#end = Math.max(
357
498
  this.#offset,
358
- haystackLength - (needle.length - 1),
499
+ haystackLength - (length - 1),
359
500
  );
360
501
  }
361
502
 
@@ -377,134 +518,205 @@ export class Multipart extends Request {
377
518
 
378
519
  /**
379
520
  * @param needle Needle to find
380
- * @returns Stream that streams the content until the next find
521
+ * @returns Stream that streams the content until the next find, and its
522
+ * state: whether it reached the needle, and its pull in progress
381
523
  */
382
524
  #findStream(needle: Needle) {
383
525
  let pending: Uint8Array<ArrayBuffer> | undefined;
384
- let ended = false;
385
- return new ReadableStream({
386
- type: "bytes",
387
- pull: async (c) => {
388
- if (!pending?.length) {
389
- let found: Uint8Array<ArrayBuffer> | undefined;
390
- for (
391
- let done: boolean | undefined;
392
- !(found = this.#find(needle)) && !done;
393
- done = this.#read(await this.#reader.read(), true, true)
394
- ) {
395
- // Retain only a suffix that could begin a boundary.
396
- this.#start = this.#end = this.#valid;
397
- const last = this.#valid - 1;
398
- const indices =
399
- this.#valid > this.#offset
400
- ? needle.loc[this.#memory[last]!]
401
- : undefined;
402
- if (indices) {
403
- search: for (let i = indices.length - 1; i >= 0; i--) {
404
- for (
405
- let match = indices[i]!, cursor = last;
406
- match >= 0 &&
407
- cursor >= this.#offset &&
408
- needle[match] === this.#memory[cursor];
409
- match--, cursor--
410
- ) {
411
- if (match === 0) {
412
- this.#start = this.#end = cursor;
413
- break search;
414
- }
526
+ const state: {
527
+ /** Reached the needle */
528
+ ended: boolean;
529
+ /** The parser moved on, so a reader left on it gets no more */
530
+ detached: boolean;
531
+ /** Pull in progress, finished before the parser reads on */
532
+ pulling?: Promise<void>;
533
+ } = { ended: false, detached: false };
534
+ const pull = async (c: ReadableByteStreamController) => {
535
+ if (state.detached) {
536
+ c.close();
537
+ c.byobRequest?.respond(0);
538
+ return;
539
+ }
540
+
541
+ if (!pending?.length) {
542
+ let found: Uint8Array<ArrayBuffer> | undefined;
543
+ for (
544
+ let done: boolean | undefined;
545
+ !(found = this.#find(needle)) && !done;
546
+ done = this.#read(await this.#reader.read(), true, true)
547
+ ) {
548
+ // Retain only a suffix that could begin a boundary.
549
+ this.#start = this.#end = this.#valid;
550
+ const last = this.#valid - 1;
551
+ const indices =
552
+ this.#valid > this.#offset
553
+ ? needle.loc[this.#memory[last]!]
554
+ : undefined;
555
+ if (indices) {
556
+ search: for (let i = indices.length - 1; i >= 0; i--) {
557
+ for (
558
+ let match = indices[i]!, cursor = last;
559
+ match >= 0 &&
560
+ cursor >= this.#offset &&
561
+ needle[match] === this.#memory[cursor];
562
+ match--, cursor--
563
+ ) {
564
+ if (match === 0) {
565
+ this.#start = this.#end = cursor;
566
+ break search;
415
567
  }
416
568
  }
417
569
  }
418
- pending = this.#shift();
419
- if (pending.length) break;
420
- if (this.#memory !== this.#scratch) this.#retain();
421
- }
422
- if (!pending?.length) {
423
- pending = found;
424
- ended = true;
425
- }
426
- }
427
-
428
- if (pending?.length) {
429
- const request = c.byobRequest;
430
- const view = request?.view;
431
- if (view) {
432
- const length = Math.min(view.byteLength, pending.length);
433
- new Uint8Array(view.buffer, view.byteOffset, length).set(
434
- pending.subarray(0, length),
435
- );
436
- pending = pending.subarray(length);
437
- request.respond(length);
438
- } else {
439
- // Byte-stream enqueue transfers this independent allocation.
440
- c.enqueue(new Uint8Array(pending));
441
- pending = undefined;
442
570
  }
571
+ pending = this.#shift();
572
+ if (pending.length) break;
573
+ if (this.#memory !== this.#scratch) this.#retain();
443
574
  }
444
- if (this.#memory !== this.#scratch) {
445
- // Source buffers may be reused on the next pull. Borrow only
446
- // within this synchronous continuation, including small BYOB reads.
447
- if (pending?.length) pending = new Uint8Array(pending);
448
- this.#retain();
575
+ if (!pending?.length) {
576
+ // the body ended inside the part
577
+ if (!found) throw new Malformed(unexpectedEnd);
578
+ pending = found;
579
+ state.ended = true;
449
580
  }
450
- if (ended && !pending?.length) {
451
- c.close();
452
- // An empty part or final short BYOB read still needs a response.
453
- c.byobRequest?.respond(0);
581
+ }
582
+
583
+ if (pending?.length) {
584
+ const request = c.byobRequest;
585
+ const view = request?.view;
586
+ if (view) {
587
+ const length = Math.min(view.byteLength, pending.length);
588
+ new Uint8Array(view.buffer, view.byteOffset, length).set(
589
+ pending.subarray(0, length),
590
+ );
591
+ pending = pending.subarray(length);
592
+ request.respond(length);
593
+ } else {
594
+ // Byte-stream enqueue transfers this independent allocation.
595
+ c.enqueue(new Uint8Array(pending));
596
+ pending = undefined;
454
597
  }
455
- },
598
+ }
599
+ if (this.#memory !== this.#scratch) {
600
+ // Source buffers may be reused on the next pull. Borrow only
601
+ // within this synchronous continuation, including small BYOB reads.
602
+ if (pending?.length) pending = new Uint8Array(pending);
603
+ this.#retain();
604
+ }
605
+ if (state.ended && !pending?.length) {
606
+ c.close();
607
+ // An empty part or final short BYOB read still needs a response.
608
+ c.byobRequest?.respond(0);
609
+ }
610
+ };
611
+
612
+ const stream = new ReadableStream({
613
+ type: "bytes",
614
+ pull: (c) => (state.pulling = pull(c)),
456
615
  });
616
+
617
+ return { stream, state };
457
618
  }
458
619
 
459
- /** @yields Multipart form data `Part`(s) */
460
- async *[Symbol.asyncIterator]() {
461
- try {
462
- await this.#findConcat(this.#opening);
620
+ /**
621
+ * Discard the rest of a part its reader stopped reading, up to its
622
+ * boundary, so its bytes are never parsed as the next part.
623
+ *
624
+ * @param needle Boundary
625
+ * @returns `false` when the body ends first
626
+ */
627
+ async #skip(needle: Needle) {
628
+ while (!this.#find(needle)) {
629
+ // only a suffix that could begin the boundary is kept
630
+ this.#offset = this.#start;
631
+ if (this.#read(await this.#reader.read())) return false;
632
+ }
633
+ return true;
634
+ }
463
635
 
464
- let headers: Uint8Array | undefined;
636
+ /**
637
+ * Reads what follows a delimiter: `--` ends the body, a new line starts
638
+ * the next part's headers, which it is left in front of. Spaces and tabs
639
+ * before the new line are transport padding, which RFC 2046 says to ignore.
640
+ *
641
+ * @returns `true` when the delimiter closes the body
642
+ * @throws {Malformed} Otherwise, since RFC 2046 does not let the boundary
643
+ * start a line of content, as in `--boundary-suffix`
644
+ */
645
+ async #closed() {
646
+ /**
647
+ * @param i Index after the offset
648
+ * @returns The byte there, reading the next chunk if needed
649
+ */
650
+ const at = async (i: number) => {
651
+ while (
652
+ this.#valid - this.#offset <= i &&
653
+ !this.#read(await this.#reader.read())
654
+ );
655
+ return this.#valid - this.#offset > i
656
+ ? this.#memory[this.#offset + i]
657
+ : undefined;
658
+ };
659
+
660
+ if ((await at(0)) === 45 && (await at(1)) === 45) {
661
+ // done, drain any epilogue without parsing it
662
+ while (!this.#read(await this.#reader.read(), false));
663
+ return true;
664
+ }
465
665
 
466
- for (
467
- let i = 0;
468
- (headers = await this.#findConcat(Multipart.#newLine));
469
- i++
470
- ) {
471
- if (i === this.#options.parts) throw new RangeError("Too Many Parts");
666
+ // consumed as it is read, so padding cannot fill the buffer
667
+ for (let byte = await at(0); byte === 32 || byte === 9; byte = await at(0))
668
+ this.#start = this.#end = ++this.#offset;
472
669
 
473
- const part = new Part(this, this.#findStream(this.#boundary), headers);
474
- yield part;
670
+ if ((await at(0)) === 13 && (await at(1)) === 10) return false;
475
671
 
476
- // to get next part, the entire body must be read
477
- // cannot cancel, chunks would be in the next header
478
- if (part.body && !part.bodyUsed) {
479
- const partReader = part.body.getReader();
672
+ throw new Malformed("Invalid Boundary");
673
+ }
480
674
 
481
- try {
482
- while (!(await partReader.read()).done);
483
- } finally {
484
- partReader.releaseLock();
485
- }
486
- }
675
+ /** @yields Multipart form data `Part`(s) */
676
+ async *[Symbol.asyncIterator](): AsyncGenerator<Multipart.Part, void> {
677
+ let complete = false;
487
678
 
488
- if (
489
- // ensures invalid characters aren't used to determine if it's done
490
- this.#valid - this.#offset > 1 &&
491
- // check for --
492
- // if not done it would be \r\nNEXT-HEADER
493
- this.#memory[this.#offset] === 45 &&
494
- this.#memory[this.#offset + 1] === 45
495
- ) {
496
- // done, drain any epilogue
497
- while (!this.#read(await this.#reader.read(), false));
498
- break;
679
+ try {
680
+ let preamble = await this.#findConcat(this.#opening);
681
+ // the first delimiter starts the body or a line, one mentioned within
682
+ // a line of the preamble is not one
683
+ if (
684
+ preamble?.length &&
685
+ !(preamble.at(-2) === 13 && preamble.at(-1) === 10)
686
+ )
687
+ preamble = await this.#findConcat(this.#boundary);
688
+ if (!preamble) throw new Malformed("Missing Opening Boundary");
689
+
690
+ // a form without fields sends only the closing delimiter
691
+ for (let i = 0; !(await this.#closed()); i++) {
692
+ const headers = await this.#findConcat(newLine);
693
+ // a body ends with the closing delimiter, after the last part
694
+ if (!headers) throw new Malformed(unexpectedEnd);
695
+ if (i === this.#options.parts) throw new Limit("Too Many Parts");
696
+
697
+ const { stream, state } = this.#findStream(this.#boundary);
698
+ yield new Part(this.#req, stream, headers);
699
+
700
+ // the next part starts after this one's boundary, however much of it
701
+ // was read: skip the rest here, never parse it as headers
702
+ if (!state.ended) {
703
+ state.detached = true;
704
+ await state.pulling?.catch(() => {});
705
+ if (!state.ended && !(await this.#skip(this.#boundary)))
706
+ throw new Malformed(unexpectedEnd);
499
707
  }
500
708
  }
709
+
710
+ complete = true;
501
711
  } finally {
712
+ // stopped early by a limit, an error, or the caller, so stop the upload
713
+ if (!complete) await this.#reader.cancel().catch(() => {});
502
714
  this.#reader.releaseLock();
503
715
  }
504
716
  }
505
717
 
506
718
  /**
507
- * Drop in replacement for `Request.formData` enhanced with size thresholds.
719
+ * Read every part into `FormData`, within the parser's limits.
508
720
  *
509
721
  * Only use `data` when buffering all content is needed, otherwise iterate through
510
722
  * the multipart using `for await...of` instead.