ovr 6.2.4 → 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 +332 -124
  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 +410 -158
  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 -257
  136. package/src/render/index.ts +0 -314
@@ -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,29 +248,80 @@ 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
 
190
318
  /** Current values being buffered in memory */
191
- readonly #memory: Uint8Array<ArrayBuffer>;
319
+ #memory = new Uint8Array(0);
192
320
 
193
- /** Opening boundary needle */
321
+ /** Owned storage for headers and boundaries spanning input chunks. */
322
+ readonly #scratch: Uint8Array<ArrayBuffer>;
323
+
324
+ /** Dash and boundary, the first delimiter without a new line before it */
194
325
  readonly #opening: Needle;
195
326
 
196
327
  /** Part boundary needle */
@@ -202,6 +333,9 @@ export class Multipart extends Request {
202
333
  /** Start index of the found needle */
203
334
  #start = 0;
204
335
 
336
+ /** Start of unread data within the buffer. */
337
+ #offset = 0;
338
+
205
339
  /** End index of the found needle */
206
340
  #end = 0;
207
341
 
@@ -224,23 +358,25 @@ export class Multipart extends Request {
224
358
  * ```
225
359
  */
226
360
  constructor(req: Request, options?: Multipart.Options) {
227
- super(req);
228
-
229
- const [mime, params] = Header.shift(this.headers.get(Header.name.type));
361
+ const [mime, params] = Header.shift(req.headers.get(Header.name.type));
230
362
 
231
- if (!Mime.multipart(mime)) throw new TypeError("Unsupported Media Type");
363
+ if (!Mime.multipart(mime)) throw new Unsupported("Unsupported Media Type");
232
364
 
233
- const { boundary } = Header.params(params);
365
+ const { boundary } = Header.params(params, true);
234
366
 
235
- if (!boundary) throw new TypeError("Boundary Not Found");
236
- 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");
237
372
 
238
373
  Object.assign(this.#options, options);
239
- this.#reader = this.body.getReader();
240
- this.#opening = new Needle(`--${boundary}\r\n`);
374
+ this.#req = req;
375
+ this.#reader = req.body.getReader();
376
+ this.#opening = new Needle(`--${boundary}`);
241
377
  this.#boundary = new Needle(`\r\n--${boundary}`);
242
378
 
243
- this.#memory = new Uint8Array(
379
+ this.#scratch = new Uint8Array(
244
380
  new ArrayBuffer(
245
381
  // slightly larger than common chunk size/high water mark 64kb for leftover boundary
246
382
  // prevents having to resize memory for most requests
@@ -251,64 +387,65 @@ export class Multipart extends Request {
251
387
  );
252
388
  }
253
389
 
254
- /**
255
- * Reads the next chunk in the request stream and concatenates it
256
- * onto the buffer.
257
- *
258
- * @param append
259
- * @returns `true` if done
260
- */
261
- async #read(append = true) {
262
- const next = await this.#reader.read();
263
-
390
+ /** Account for a source read and append or temporarily borrow its bytes. */
391
+ #read(
392
+ next: ReadableStreamReadResult<Uint8Array<ArrayBuffer>>,
393
+ append = true,
394
+ borrow = false,
395
+ ) {
264
396
  if (next.done) return true;
265
-
266
397
  const nextLength = next.value.length;
267
-
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
-
272
401
  if (append) {
273
- const required = this.#valid + nextLength;
274
- const size = this.#memory.buffer.byteLength;
275
-
276
- // resize if full
277
- if (required > size) {
278
- this.#memory.buffer.resize(
279
- Math.min(
280
- this.#options.memory, // in case double is larger than the max
281
- Math.max(required, size * 2),
282
- ),
283
- );
402
+ const length = this.#valid - this.#offset;
403
+ const required = length + nextLength;
404
+ if (required > this.#scratch.buffer.maxByteLength) {
405
+ throw new Limit("Multipart buffer limit exceeded");
284
406
  }
285
-
286
- // write at the valid cutoff
287
- this.#memory.set(next.value, this.#valid);
288
- this.#valid += nextLength;
407
+ if (length || !borrow) {
408
+ this.#retain();
409
+ if (required > this.#scratch.length) {
410
+ this.#scratch.buffer.resize(
411
+ Math.min(
412
+ this.#scratch.buffer.maxByteLength,
413
+ Math.max(required, this.#scratch.length * 2),
414
+ ),
415
+ );
416
+ }
417
+ this.#scratch.set(next.value, length);
418
+ } else {
419
+ // The caller must consume or retain these bytes before yielding.
420
+ this.#memory = next.value;
421
+ }
422
+ this.#valid = required;
423
+ this.#start -= this.#offset;
424
+ this.#end -= this.#offset;
425
+ this.#offset = 0;
289
426
  }
290
427
  }
291
428
 
429
+ /** Preserve unread bytes before the source can reuse its input storage. */
430
+ #retain() {
431
+ const length = this.#valid - this.#offset;
432
+ if (length > this.#scratch.length) this.#scratch.buffer.resize(length);
433
+ this.#scratch.set(this.#memory.subarray(this.#offset, this.#valid));
434
+ this.#memory = this.#scratch;
435
+ this.#valid = length;
436
+ this.#start -= this.#offset;
437
+ this.#end -= this.#offset;
438
+ this.#offset = 0;
439
+ }
440
+
292
441
  /**
293
442
  * Cuts off the buffer < end index.
294
443
  *
295
444
  * @returns Shifted off buffer < start index
296
445
  */
297
446
  #shift() {
298
- const before = this.#memory.slice(0, this.#start);
299
-
300
- // how much data is left after the found needle
301
- const remainder = this.#valid - this.#end;
302
-
303
- if (remainder) {
304
- // copy remainder to the start of the buffer
305
- // this doesn't move the chunks, just copies to the start
306
- // old will be overwritten
307
- this.#memory.copyWithin(0, this.#end, this.#valid);
308
- }
309
-
310
- this.#valid = remainder;
311
- this.#start = this.#end = 0;
447
+ const before = this.#memory.subarray(this.#offset, this.#start);
448
+ this.#offset = this.#start = this.#end;
312
449
 
313
450
  return before;
314
451
  }
@@ -322,32 +459,45 @@ export class Multipart extends Request {
322
459
  * @returns If found, shifts the buffer and returns the result.
323
460
  */
324
461
  #find(needle: Needle) {
462
+ const haystack = this.#memory;
325
463
  const haystackLength = this.#valid;
326
-
327
- // start the search at the last char of the needle
328
- // since it could be at the very start
329
- let i = this.#start + needle.end;
330
-
331
- while (i < haystackLength) {
332
- for (
333
- let needleIndex = needle.end, cursor = i;
334
- needleIndex >= 0 && needle[needleIndex] === this.#memory[cursor];
335
- needleIndex--, cursor--
336
- ) {
337
- if (needleIndex === 0) {
338
- this.#start = cursor;
339
- 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;
340
490
  return this.#shift();
341
491
  }
342
492
  }
343
-
344
- i += needle.skip[this.#memory[i]!]!;
345
493
  }
346
494
 
347
495
  // where to safely start the next search in the concatenated chunk result
348
496
  // go back len - 1 in case it was partially at the end
349
- this.#start = this.#end =
350
- needle.length < haystackLength ? haystackLength - (needle.length - 1) : 0;
497
+ this.#start = this.#end = Math.max(
498
+ this.#offset,
499
+ haystackLength - (length - 1),
500
+ );
351
501
  }
352
502
 
353
503
  /**
@@ -359,112 +509,214 @@ export class Multipart extends Request {
359
509
  */
360
510
  async #findConcat(needle: Needle) {
361
511
  let found: Uint8Array | undefined;
362
- while (!(found = this.#find(needle)) && !(await this.#read()));
512
+ while (
513
+ !(found = this.#find(needle)) &&
514
+ !this.#read(await this.#reader.read())
515
+ );
363
516
  return found;
364
517
  }
365
518
 
366
519
  /**
367
520
  * @param needle Needle to find
368
- * @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
369
523
  */
370
524
  #findStream(needle: Needle) {
371
- return new ReadableStream({
372
- type: "bytes",
373
- pull: async (c) => {
374
- let found: Uint8Array<ArrayBuffer> | undefined;
525
+ let pending: Uint8Array<ArrayBuffer> | undefined;
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
+ }
375
540
 
541
+ if (!pending?.length) {
542
+ let found: Uint8Array<ArrayBuffer> | undefined;
376
543
  for (
377
544
  let done: boolean | undefined;
378
545
  !(found = this.#find(needle)) && !done;
379
- done = await this.#read()
546
+ done = this.#read(await this.#reader.read(), true, true)
380
547
  ) {
381
- // not found within current chunk
382
- const lastIndex = this.#valid - 1;
383
- const needleIndices = needle.loc[this.#memory[lastIndex]!];
384
-
385
- if (needleIndices) {
386
- // last char is in the boundary, check for partial boundary
387
- // iterate backwards through the indices
388
- indices: for (let i = needleIndices.length - 1; i >= 0; i--) {
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--) {
389
557
  for (
390
- let needleIndex = needleIndices[i]!, cursor = lastIndex;
391
- needleIndex >= 0 &&
392
- needle[needleIndex] === this.#memory[cursor];
393
- needleIndex--, cursor--
558
+ let match = indices[i]!, cursor = last;
559
+ match >= 0 &&
560
+ cursor >= this.#offset &&
561
+ needle[match] === this.#memory[cursor];
562
+ match--, cursor--
394
563
  ) {
395
- if (needleIndex === 0) {
396
- // these are the same since no needle was found
564
+ if (match === 0) {
397
565
  this.#start = this.#end = cursor;
398
- // rerun check if next has the rest
399
- break indices;
566
+ break search;
400
567
  }
401
568
  }
402
569
  }
403
570
  }
404
-
405
- const before = this.#shift();
406
- if (before.length) {
407
- c.enqueue(before);
408
- return; // wait for next pull
409
- }
571
+ pending = this.#shift();
572
+ if (pending.length) break;
573
+ if (this.#memory !== this.#scratch) this.#retain();
410
574
  }
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;
580
+ }
581
+ }
411
582
 
412
- if (found?.length) c.enqueue(found);
413
-
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;
597
+ }
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) {
414
606
  c.close();
415
- },
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)),
416
615
  });
616
+
617
+ return { stream, state };
417
618
  }
418
619
 
419
- /** @yields Multipart form data `Part`(s) */
420
- async *[Symbol.asyncIterator]() {
421
- try {
422
- 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
+ }
423
635
 
424
- 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
+ }
425
665
 
426
- for (
427
- let i = 0;
428
- (headers = await this.#findConcat(Multipart.#newLine));
429
- i++
430
- ) {
431
- 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;
432
669
 
433
- const part = new Part(this, this.#findStream(this.#boundary), headers);
434
- yield part;
670
+ if ((await at(0)) === 13 && (await at(1)) === 10) return false;
435
671
 
436
- // to get next part, the entire body must be read
437
- // cannot cancel, chunks would be in the next header
438
- if (part.body && !part.bodyUsed) {
439
- const partReader = part.body.getReader();
672
+ throw new Malformed("Invalid Boundary");
673
+ }
440
674
 
441
- try {
442
- while (!(await partReader.read()).done);
443
- } finally {
444
- partReader.releaseLock();
445
- }
446
- }
675
+ /** @yields Multipart form data `Part`(s) */
676
+ async *[Symbol.asyncIterator](): AsyncGenerator<Multipart.Part, void> {
677
+ let complete = false;
447
678
 
448
- if (
449
- // ensures invalid characters aren't used to determine if it's done
450
- this.#valid > 1 &&
451
- // check for --
452
- // if not done it would be \r\nNEXT-HEADER
453
- this.#memory[0] === 45 &&
454
- this.#memory[1] === 45
455
- ) {
456
- // done, drain any epilogue
457
- while (!(await this.#read(false)));
458
- 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);
459
707
  }
460
708
  }
709
+
710
+ complete = true;
461
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(() => {});
462
714
  this.#reader.releaseLock();
463
715
  }
464
716
  }
465
717
 
466
718
  /**
467
- * Drop in replacement for `Request.formData` enhanced with size thresholds.
719
+ * Read every part into `FormData`, within the parser's limits.
468
720
  *
469
721
  * Only use `data` when buffering all content is needed, otherwise iterate through
470
722
  * the multipart using `for await...of` instead.