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.
- package/README.md +3 -1
- package/dist/app/index.d.ts +51 -7
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +163 -28
- package/dist/auth/auth-data.d.ts +26 -0
- package/dist/auth/auth-data.d.ts.map +1 -0
- package/dist/auth/auth-data.js +42 -0
- package/dist/auth/cbor.d.ts +50 -0
- package/dist/auth/cbor.d.ts.map +1 -0
- package/dist/auth/cbor.js +200 -0
- package/dist/auth/client-source.d.ts +3 -0
- package/dist/auth/client-source.d.ts.map +1 -0
- package/dist/auth/client-source.js +3 -0
- package/dist/auth/client.d.ts +3 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +186 -0
- package/dist/auth/configure.d.ts +28 -0
- package/dist/auth/configure.d.ts.map +1 -0
- package/dist/auth/configure.js +228 -0
- package/dist/auth/der.d.ts +6 -0
- package/dist/auth/der.d.ts.map +1 -0
- package/dist/auth/der.js +36 -0
- package/dist/auth/index.d.ts +242 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +152 -0
- package/dist/auth/issue.d.ts +36 -0
- package/dist/auth/issue.d.ts.map +1 -0
- package/dist/auth/issue.js +47 -0
- package/dist/auth/metadata.d.ts +22 -0
- package/dist/auth/metadata.d.ts.map +1 -0
- package/dist/auth/metadata.js +134 -0
- package/dist/auth/oauth.d.ts +29 -0
- package/dist/auth/oauth.d.ts.map +1 -0
- package/dist/auth/oauth.js +724 -0
- package/dist/auth/passkey.d.ts +101 -0
- package/dist/auth/passkey.d.ts.map +1 -0
- package/dist/auth/passkey.js +393 -0
- package/dist/auth/state.d.ts +49 -0
- package/dist/auth/state.d.ts.map +1 -0
- package/dist/auth/state.js +184 -0
- package/dist/auth/storage.d.ts +193 -0
- package/dist/auth/storage.d.ts.map +1 -0
- package/dist/auth/storage.js +410 -0
- package/dist/auth/token.d.ts +12 -0
- package/dist/auth/token.d.ts.map +1 -0
- package/dist/auth/token.js +39 -0
- package/dist/context/index.d.ts +86 -16
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +166 -42
- package/dist/cookie/index.d.ts +3 -0
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +1 -0
- package/dist/database/index.d.ts +262 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +807 -0
- package/dist/form/index.d.ts +699 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +901 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/jsx/elements.d.ts +28 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts +20 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +6 -65
- package/dist/limiter/index.d.ts +65 -0
- package/dist/limiter/index.d.ts.map +1 -0
- package/dist/limiter/index.js +157 -0
- package/dist/markdown/index.d.ts +61 -0
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +1160 -0
- package/dist/markup/index.d.ts +188 -0
- package/dist/markup/index.d.ts.map +1 -0
- package/dist/markup/index.js +718 -0
- package/dist/mcp/index.d.ts +73 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +988 -0
- package/dist/middleware/index.d.ts +3 -4
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +53 -7
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +332 -124
- package/dist/route/index.d.ts +226 -80
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +239 -99
- package/dist/schema/index.d.ts +307 -734
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +783 -1050
- package/dist/table/index.d.ts +132 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +188 -0
- package/dist/trie/index.js +4 -4
- package/dist/util/index.d.ts +37 -5
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +156 -41
- package/package.json +11 -4
- package/src/app/index.ts +230 -42
- package/src/auth/auth-data.ts +56 -0
- package/src/auth/cbor.ts +231 -0
- package/src/auth/client-source.ts +3 -0
- package/src/auth/client.ts +205 -0
- package/src/auth/configure.ts +279 -0
- package/src/auth/der.ts +35 -0
- package/src/auth/index.ts +387 -0
- package/src/auth/issue.ts +52 -0
- package/src/auth/metadata.ts +149 -0
- package/src/auth/oauth.ts +882 -0
- package/src/auth/passkey.ts +556 -0
- package/src/auth/state.ts +213 -0
- package/src/auth/storage.ts +572 -0
- package/src/auth/token.ts +67 -0
- package/src/context/index.ts +240 -65
- package/src/cookie/index.ts +3 -0
- package/src/database/index.ts +1193 -0
- package/src/form/index.ts +1720 -0
- package/src/index.ts +10 -3
- package/src/jsx/elements.ts +45 -25
- package/src/jsx/index.ts +24 -69
- package/src/limiter/index.ts +221 -0
- package/src/markdown/index.ts +1322 -0
- package/src/markup/index.ts +853 -0
- package/src/mcp/conformance.2025-11-25.yml +39 -0
- package/src/mcp/conformance.2026-07-28.yml +56 -0
- package/src/mcp/index.ts +1262 -0
- package/src/middleware/index.ts +5 -6
- package/src/multipart/index.ts +410 -158
- package/src/route/index.ts +559 -278
- package/src/schema/index.ts +1542 -2256
- package/src/table/index.ts +387 -0
- package/src/trie/index.ts +4 -4
- package/src/util/index.ts +163 -49
- package/dist/render/index.d.ts +0 -79
- package/dist/render/index.d.ts.map +0 -1
- package/dist/render/index.js +0 -257
- package/src/render/index.ts +0 -314
package/dist/multipart/index.js
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
1
|
import { Codec, Header, Mime, Size } from "../util/index.js";
|
|
2
|
+
/** A request body over one of the parser's limits. */
|
|
3
|
+
export class Limit extends RangeError {
|
|
4
|
+
}
|
|
5
|
+
/** A request that is not valid multipart form data, the client's error. */
|
|
6
|
+
export class Malformed extends TypeError {
|
|
7
|
+
}
|
|
8
|
+
/** A request body of another type than multipart form data. */
|
|
9
|
+
export class Unsupported extends Malformed {
|
|
10
|
+
}
|
|
2
11
|
/** Sequence of bytes to find within the stream */
|
|
3
12
|
class Needle extends Uint8Array {
|
|
4
13
|
/** Index of the last character in the needle */
|
|
5
14
|
end = this.length - 1;
|
|
6
15
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
16
|
+
* How far the search can move on from the byte it reads where the needle
|
|
17
|
+
* would end: from that byte's last place in the needle to the needle's end,
|
|
18
|
+
* or the whole length for a byte the needle does not have. Most bytes of a
|
|
19
|
+
* file are not in its boundary, so most of a file is never read.
|
|
10
20
|
*/
|
|
11
21
|
skip = new Uint8Array(256).fill(this.length);
|
|
12
22
|
/** Stores where each byte is located in the needle */
|
|
13
23
|
loc = new Array(256);
|
|
14
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Index of a byte that occurs once in the needle, such as the `\r` of a
|
|
26
|
+
* boundary, searched for first. A match can only start at its occurrences,
|
|
27
|
+
* and comparing from each is linear in the haystack whatever the client
|
|
28
|
+
* chose as its boundary.
|
|
29
|
+
*/
|
|
30
|
+
anchor;
|
|
31
|
+
/** @param needle String to find within the stream, at most 255 bytes */
|
|
15
32
|
constructor(needle) {
|
|
16
33
|
super(Codec.encode(needle).buffer);
|
|
34
|
+
// boundaries are at most 70 characters, checked before a needle is made
|
|
17
35
|
for (let i = 0; i < this.length; i++) {
|
|
18
36
|
const byte = this[i];
|
|
19
|
-
//
|
|
37
|
+
// the last byte is where the search reads, so it moves on from before it
|
|
20
38
|
if (i !== this.end)
|
|
21
39
|
this.skip[byte] = this.end - i;
|
|
22
40
|
(this.loc[byte] ??= []).push(i);
|
|
23
41
|
}
|
|
42
|
+
// a short needle without a unique byte, such as "\r\n\r\n", starts at 0
|
|
43
|
+
const unique = this.findIndex((byte) => this.loc[byte].length === 1);
|
|
44
|
+
this.anchor = unique === -1 ? 0 : unique;
|
|
24
45
|
}
|
|
25
46
|
}
|
|
26
47
|
/** Multipart part */
|
|
@@ -48,6 +69,8 @@ class Part extends Request {
|
|
|
48
69
|
* @example "image/png"
|
|
49
70
|
*/
|
|
50
71
|
type;
|
|
72
|
+
/** Signal of the request the part is from */
|
|
73
|
+
#signal;
|
|
51
74
|
/**
|
|
52
75
|
* Create a new multipart part
|
|
53
76
|
*
|
|
@@ -57,8 +80,13 @@ class Part extends Request {
|
|
|
57
80
|
*/
|
|
58
81
|
constructor(req, body, rawHeaders) {
|
|
59
82
|
const headers = [];
|
|
83
|
+
// each byte as one character, so `Headers` accepts the UTF-8 bytes
|
|
84
|
+
// browsers send in names and filenames, decoded below
|
|
85
|
+
let text = "";
|
|
86
|
+
for (let i = 0; i < rawHeaders.length; i++)
|
|
87
|
+
text += String.fromCharCode(rawHeaders[i]);
|
|
60
88
|
// create headers
|
|
61
|
-
for (const line of
|
|
89
|
+
for (const line of text.split("\r\n")) {
|
|
62
90
|
const colon = line.indexOf(":");
|
|
63
91
|
if (colon !== -1) {
|
|
64
92
|
headers.push([
|
|
@@ -68,14 +96,55 @@ class Part extends Request {
|
|
|
68
96
|
]);
|
|
69
97
|
}
|
|
70
98
|
}
|
|
71
|
-
|
|
72
|
-
|
|
99
|
+
let parsed;
|
|
100
|
+
try {
|
|
101
|
+
parsed = new Headers(headers);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// an invalid name or value, the client's error
|
|
105
|
+
throw new Malformed("Invalid Part Header");
|
|
106
|
+
}
|
|
107
|
+
// from its parts, since a server's own request object, such as srvx's,
|
|
108
|
+
// is not one the platform's constructor can copy
|
|
109
|
+
super(req.url, {
|
|
110
|
+
method: req.method,
|
|
111
|
+
headers: parsed,
|
|
73
112
|
body,
|
|
113
|
+
// A request made from another follows its signal with a listener,
|
|
114
|
+
// and each listener added is compared with all those before it, so
|
|
115
|
+
// the cost of a form grew with the square of its number of parts.
|
|
116
|
+
// The part has the request's own signal instead, see `signal`.
|
|
117
|
+
signal: null,
|
|
74
118
|
// @ts-expect-error - streaming https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#duplex
|
|
75
119
|
duplex: "half",
|
|
76
120
|
});
|
|
121
|
+
this.#signal = req.signal;
|
|
77
122
|
[this.type] = Header.shift(this.headers.get(Header.name.type));
|
|
78
|
-
|
|
123
|
+
const { name, filename } = Header.params(this.headers.get(Header.name.disp), true);
|
|
124
|
+
this.name = Part.#utf8(name);
|
|
125
|
+
this.filename = Part.#utf8(filename);
|
|
126
|
+
}
|
|
127
|
+
/** Signal of the request the part is from, the same one for each part */
|
|
128
|
+
get signal() {
|
|
129
|
+
return this.#signal;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @param value Header parameter, one character per byte
|
|
133
|
+
* @returns The UTF-8 text it encodes, or `null` without one
|
|
134
|
+
*/
|
|
135
|
+
static #utf8(value) {
|
|
136
|
+
if (value === undefined)
|
|
137
|
+
return null;
|
|
138
|
+
// most names are ASCII, which is the same text in UTF-8
|
|
139
|
+
if (!/[^\0-\x7f]/.test(value))
|
|
140
|
+
return value;
|
|
141
|
+
try {
|
|
142
|
+
return Codec.decode(Uint8Array.from(value, (c) => c.charCodeAt(0)));
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
// not UTF-8, the client's error
|
|
146
|
+
throw new Malformed("Invalid Part Header");
|
|
147
|
+
}
|
|
79
148
|
}
|
|
80
149
|
/**
|
|
81
150
|
*
|
|
@@ -89,21 +158,45 @@ class Part extends Request {
|
|
|
89
158
|
return this.text();
|
|
90
159
|
}
|
|
91
160
|
}
|
|
92
|
-
/**
|
|
93
|
-
export
|
|
94
|
-
|
|
95
|
-
|
|
161
|
+
/** Default most memory a request body takes while it is read, 4MB */
|
|
162
|
+
export const memory = 4 * Size.mb;
|
|
163
|
+
/** Message for a body that ends before its closing boundary */
|
|
164
|
+
const unexpectedEnd = "Unexpected End of Body";
|
|
165
|
+
/**
|
|
166
|
+
* New line needle to share across requests and parts. Outside the class, since
|
|
167
|
+
* a static made as the class loads is a side effect, which keeps `Multipart`
|
|
168
|
+
* in bundles that only use its errors.
|
|
169
|
+
*/
|
|
170
|
+
const newLine = new Needle("\r\n\r\n");
|
|
171
|
+
/**
|
|
172
|
+
* The parts of a multipart request, read one at a time as the body arrives.
|
|
173
|
+
* It reads the request's body, which cannot be read another way after.
|
|
174
|
+
*/
|
|
175
|
+
export class Multipart {
|
|
176
|
+
/** A body over a limit, answered 413 when a route does not catch it */
|
|
177
|
+
static Limit = Limit;
|
|
178
|
+
/** Malformed form data, answered 400 when a route does not catch it */
|
|
179
|
+
static Malformed = Malformed;
|
|
180
|
+
/**
|
|
181
|
+
* A content type other than multipart form data, answered 415 when a route
|
|
182
|
+
* does not catch it. A kind of `Malformed`.
|
|
183
|
+
*/
|
|
184
|
+
static Unsupported = Unsupported;
|
|
96
185
|
/** Parser options */
|
|
97
186
|
#options = {
|
|
98
|
-
memory
|
|
187
|
+
memory,
|
|
99
188
|
payload: 16 * Size.mb,
|
|
100
189
|
parts: Infinity,
|
|
101
190
|
};
|
|
191
|
+
/** Request the parts are read from */
|
|
192
|
+
#req;
|
|
102
193
|
/** Request body reader */
|
|
103
194
|
#reader;
|
|
104
195
|
/** Current values being buffered in memory */
|
|
105
|
-
#memory;
|
|
106
|
-
/**
|
|
196
|
+
#memory = new Uint8Array(0);
|
|
197
|
+
/** Owned storage for headers and boundaries spanning input chunks. */
|
|
198
|
+
#scratch;
|
|
199
|
+
/** Dash and boundary, the first delimiter without a new line before it */
|
|
107
200
|
#opening;
|
|
108
201
|
/** Part boundary needle */
|
|
109
202
|
#boundary;
|
|
@@ -111,6 +204,8 @@ export class Multipart extends Request {
|
|
|
111
204
|
#valid = 0;
|
|
112
205
|
/** Start index of the found needle */
|
|
113
206
|
#start = 0;
|
|
207
|
+
/** Start of unread data within the buffer. */
|
|
208
|
+
#offset = 0;
|
|
114
209
|
/** End index of the found needle */
|
|
115
210
|
#end = 0;
|
|
116
211
|
/** Total bytes read from the stream */
|
|
@@ -131,71 +226,80 @@ export class Multipart extends Request {
|
|
|
131
226
|
* ```
|
|
132
227
|
*/
|
|
133
228
|
constructor(req, options) {
|
|
134
|
-
|
|
135
|
-
const [mime, params] = Header.shift(this.headers.get(Header.name.type));
|
|
229
|
+
const [mime, params] = Header.shift(req.headers.get(Header.name.type));
|
|
136
230
|
if (!Mime.multipart(mime))
|
|
137
|
-
throw new
|
|
138
|
-
const { boundary } = Header.params(params);
|
|
231
|
+
throw new Unsupported("Unsupported Media Type");
|
|
232
|
+
const { boundary } = Header.params(params, true);
|
|
139
233
|
if (!boundary)
|
|
140
|
-
throw new
|
|
141
|
-
|
|
142
|
-
|
|
234
|
+
throw new Malformed("Boundary Not Found");
|
|
235
|
+
// RFC 2046: 1 to 70 characters from a limited set, not ending in a space
|
|
236
|
+
if (!/^[\w'()+,./:=? -]{0,69}[\w'()+,./:=?-]$/.test(boundary))
|
|
237
|
+
throw new Malformed("Invalid Boundary");
|
|
238
|
+
if (!req.body)
|
|
239
|
+
throw new Malformed("No Request Body");
|
|
143
240
|
Object.assign(this.#options, options);
|
|
144
|
-
this.#
|
|
145
|
-
this.#
|
|
241
|
+
this.#req = req;
|
|
242
|
+
this.#reader = req.body.getReader();
|
|
243
|
+
this.#opening = new Needle(`--${boundary}`);
|
|
146
244
|
this.#boundary = new Needle(`\r\n--${boundary}`);
|
|
147
|
-
this.#
|
|
245
|
+
this.#scratch = new Uint8Array(new ArrayBuffer(
|
|
148
246
|
// slightly larger than common chunk size/high water mark 64kb for leftover boundary
|
|
149
247
|
// prevents having to resize memory for most requests
|
|
150
248
|
65 * Size.kb,
|
|
151
249
|
// cap max chunk size + leftover
|
|
152
250
|
{ maxByteLength: this.#options.memory }));
|
|
153
251
|
}
|
|
154
|
-
/**
|
|
155
|
-
|
|
156
|
-
* onto the buffer.
|
|
157
|
-
*
|
|
158
|
-
* @param append
|
|
159
|
-
* @returns `true` if done
|
|
160
|
-
*/
|
|
161
|
-
async #read(append = true) {
|
|
162
|
-
const next = await this.#reader.read();
|
|
252
|
+
/** Account for a source read and append or temporarily borrow its bytes. */
|
|
253
|
+
#read(next, append = true, borrow = false) {
|
|
163
254
|
if (next.done)
|
|
164
255
|
return true;
|
|
165
256
|
const nextLength = next.value.length;
|
|
166
257
|
if ((this.#payloadSize += nextLength) > this.#options.payload) {
|
|
167
|
-
throw new
|
|
258
|
+
throw new Limit("Payload Too Large");
|
|
168
259
|
}
|
|
169
260
|
if (append) {
|
|
170
|
-
const
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
this.#memory.buffer.resize(Math.min(this.#options.memory, // in case double is larger than the max
|
|
175
|
-
Math.max(required, size * 2)));
|
|
261
|
+
const length = this.#valid - this.#offset;
|
|
262
|
+
const required = length + nextLength;
|
|
263
|
+
if (required > this.#scratch.buffer.maxByteLength) {
|
|
264
|
+
throw new Limit("Multipart buffer limit exceeded");
|
|
176
265
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
266
|
+
if (length || !borrow) {
|
|
267
|
+
this.#retain();
|
|
268
|
+
if (required > this.#scratch.length) {
|
|
269
|
+
this.#scratch.buffer.resize(Math.min(this.#scratch.buffer.maxByteLength, Math.max(required, this.#scratch.length * 2)));
|
|
270
|
+
}
|
|
271
|
+
this.#scratch.set(next.value, length);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
// The caller must consume or retain these bytes before yielding.
|
|
275
|
+
this.#memory = next.value;
|
|
276
|
+
}
|
|
277
|
+
this.#valid = required;
|
|
278
|
+
this.#start -= this.#offset;
|
|
279
|
+
this.#end -= this.#offset;
|
|
280
|
+
this.#offset = 0;
|
|
180
281
|
}
|
|
181
282
|
}
|
|
283
|
+
/** Preserve unread bytes before the source can reuse its input storage. */
|
|
284
|
+
#retain() {
|
|
285
|
+
const length = this.#valid - this.#offset;
|
|
286
|
+
if (length > this.#scratch.length)
|
|
287
|
+
this.#scratch.buffer.resize(length);
|
|
288
|
+
this.#scratch.set(this.#memory.subarray(this.#offset, this.#valid));
|
|
289
|
+
this.#memory = this.#scratch;
|
|
290
|
+
this.#valid = length;
|
|
291
|
+
this.#start -= this.#offset;
|
|
292
|
+
this.#end -= this.#offset;
|
|
293
|
+
this.#offset = 0;
|
|
294
|
+
}
|
|
182
295
|
/**
|
|
183
296
|
* Cuts off the buffer < end index.
|
|
184
297
|
*
|
|
185
298
|
* @returns Shifted off buffer < start index
|
|
186
299
|
*/
|
|
187
300
|
#shift() {
|
|
188
|
-
const before = this.#memory.
|
|
189
|
-
|
|
190
|
-
const remainder = this.#valid - this.#end;
|
|
191
|
-
if (remainder) {
|
|
192
|
-
// copy remainder to the start of the buffer
|
|
193
|
-
// this doesn't move the chunks, just copies to the start
|
|
194
|
-
// old will be overwritten
|
|
195
|
-
this.#memory.copyWithin(0, this.#end, this.#valid);
|
|
196
|
-
}
|
|
197
|
-
this.#valid = remainder;
|
|
198
|
-
this.#start = this.#end = 0;
|
|
301
|
+
const before = this.#memory.subarray(this.#offset, this.#start);
|
|
302
|
+
this.#offset = this.#start = this.#end;
|
|
199
303
|
return before;
|
|
200
304
|
}
|
|
201
305
|
/**
|
|
@@ -207,24 +311,33 @@ export class Multipart extends Request {
|
|
|
207
311
|
* @returns If found, shifts the buffer and returns the result.
|
|
208
312
|
*/
|
|
209
313
|
#find(needle) {
|
|
314
|
+
const haystack = this.#memory;
|
|
210
315
|
const haystackLength = this.#valid;
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
316
|
+
const { anchor, end, length, skip } = needle;
|
|
317
|
+
const byte = needle[anchor];
|
|
318
|
+
const last = needle[end];
|
|
319
|
+
// Reading only where the needle would end skips most of the haystack.
|
|
320
|
+
// `Uint8Array.indexOf` reads every byte of it, and in V8 does so several
|
|
321
|
+
// times slower than a loop, which made large files slow to parse.
|
|
322
|
+
for (let i = this.#start + end, current; i < haystackLength; i += skip[current]) {
|
|
323
|
+
current = haystack[i];
|
|
324
|
+
// the anchor is checked before comparing, so a haystack of near
|
|
325
|
+
// matches costs one read for each of its bytes
|
|
326
|
+
if (current === last && haystack[i - end + anchor] === byte) {
|
|
327
|
+
const start = i - end;
|
|
328
|
+
let match = 0;
|
|
329
|
+
while (match < length && needle[match] === haystack[start + match])
|
|
330
|
+
match++;
|
|
331
|
+
if (match === length) {
|
|
332
|
+
this.#start = start;
|
|
333
|
+
this.#end = start + length;
|
|
219
334
|
return this.#shift();
|
|
220
335
|
}
|
|
221
336
|
}
|
|
222
|
-
i += needle.skip[this.#memory[i]];
|
|
223
337
|
}
|
|
224
338
|
// where to safely start the next search in the concatenated chunk result
|
|
225
339
|
// go back len - 1 in case it was partially at the end
|
|
226
|
-
this.#start = this.#end =
|
|
227
|
-
needle.length < haystackLength ? haystackLength - (needle.length - 1) : 0;
|
|
340
|
+
this.#start = this.#end = Math.max(this.#offset, haystackLength - (length - 1));
|
|
228
341
|
}
|
|
229
342
|
/**
|
|
230
343
|
* Tries to find the needle in the buffer, if not found, the next
|
|
@@ -235,92 +348,187 @@ export class Multipart extends Request {
|
|
|
235
348
|
*/
|
|
236
349
|
async #findConcat(needle) {
|
|
237
350
|
let found;
|
|
238
|
-
while (!(found = this.#find(needle)) &&
|
|
351
|
+
while (!(found = this.#find(needle)) &&
|
|
352
|
+
!this.#read(await this.#reader.read()))
|
|
239
353
|
;
|
|
240
354
|
return found;
|
|
241
355
|
}
|
|
242
356
|
/**
|
|
243
357
|
* @param needle Needle to find
|
|
244
|
-
* @returns Stream that streams the content until the next find
|
|
358
|
+
* @returns Stream that streams the content until the next find, and its
|
|
359
|
+
* state: whether it reached the needle, and its pull in progress
|
|
245
360
|
*/
|
|
246
361
|
#findStream(needle) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
362
|
+
let pending;
|
|
363
|
+
const state = { ended: false, detached: false };
|
|
364
|
+
const pull = async (c) => {
|
|
365
|
+
if (state.detached) {
|
|
366
|
+
c.close();
|
|
367
|
+
c.byobRequest?.respond(0);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (!pending?.length) {
|
|
250
371
|
let found;
|
|
251
|
-
for (let done; !(found = this.#find(needle)) && !done; done = await this.#read()) {
|
|
252
|
-
//
|
|
253
|
-
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
372
|
+
for (let done; !(found = this.#find(needle)) && !done; done = this.#read(await this.#reader.read(), true, true)) {
|
|
373
|
+
// Retain only a suffix that could begin a boundary.
|
|
374
|
+
this.#start = this.#end = this.#valid;
|
|
375
|
+
const last = this.#valid - 1;
|
|
376
|
+
const indices = this.#valid > this.#offset
|
|
377
|
+
? needle.loc[this.#memory[last]]
|
|
378
|
+
: undefined;
|
|
379
|
+
if (indices) {
|
|
380
|
+
search: for (let i = indices.length - 1; i >= 0; i--) {
|
|
381
|
+
for (let match = indices[i], cursor = last; match >= 0 &&
|
|
382
|
+
cursor >= this.#offset &&
|
|
383
|
+
needle[match] === this.#memory[cursor]; match--, cursor--) {
|
|
384
|
+
if (match === 0) {
|
|
263
385
|
this.#start = this.#end = cursor;
|
|
264
|
-
|
|
265
|
-
break indices;
|
|
386
|
+
break search;
|
|
266
387
|
}
|
|
267
388
|
}
|
|
268
389
|
}
|
|
269
390
|
}
|
|
270
|
-
|
|
271
|
-
if (
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
391
|
+
pending = this.#shift();
|
|
392
|
+
if (pending.length)
|
|
393
|
+
break;
|
|
394
|
+
if (this.#memory !== this.#scratch)
|
|
395
|
+
this.#retain();
|
|
396
|
+
}
|
|
397
|
+
if (!pending?.length) {
|
|
398
|
+
// the body ended inside the part
|
|
399
|
+
if (!found)
|
|
400
|
+
throw new Malformed(unexpectedEnd);
|
|
401
|
+
pending = found;
|
|
402
|
+
state.ended = true;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (pending?.length) {
|
|
406
|
+
const request = c.byobRequest;
|
|
407
|
+
const view = request?.view;
|
|
408
|
+
if (view) {
|
|
409
|
+
const length = Math.min(view.byteLength, pending.length);
|
|
410
|
+
new Uint8Array(view.buffer, view.byteOffset, length).set(pending.subarray(0, length));
|
|
411
|
+
pending = pending.subarray(length);
|
|
412
|
+
request.respond(length);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
// Byte-stream enqueue transfers this independent allocation.
|
|
416
|
+
c.enqueue(new Uint8Array(pending));
|
|
417
|
+
pending = undefined;
|
|
275
418
|
}
|
|
276
|
-
|
|
277
|
-
|
|
419
|
+
}
|
|
420
|
+
if (this.#memory !== this.#scratch) {
|
|
421
|
+
// Source buffers may be reused on the next pull. Borrow only
|
|
422
|
+
// within this synchronous continuation, including small BYOB reads.
|
|
423
|
+
if (pending?.length)
|
|
424
|
+
pending = new Uint8Array(pending);
|
|
425
|
+
this.#retain();
|
|
426
|
+
}
|
|
427
|
+
if (state.ended && !pending?.length) {
|
|
278
428
|
c.close();
|
|
279
|
-
|
|
429
|
+
// An empty part or final short BYOB read still needs a response.
|
|
430
|
+
c.byobRequest?.respond(0);
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
const stream = new ReadableStream({
|
|
434
|
+
type: "bytes",
|
|
435
|
+
pull: (c) => (state.pulling = pull(c)),
|
|
280
436
|
});
|
|
437
|
+
return { stream, state };
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Discard the rest of a part its reader stopped reading, up to its
|
|
441
|
+
* boundary, so its bytes are never parsed as the next part.
|
|
442
|
+
*
|
|
443
|
+
* @param needle Boundary
|
|
444
|
+
* @returns `false` when the body ends first
|
|
445
|
+
*/
|
|
446
|
+
async #skip(needle) {
|
|
447
|
+
while (!this.#find(needle)) {
|
|
448
|
+
// only a suffix that could begin the boundary is kept
|
|
449
|
+
this.#offset = this.#start;
|
|
450
|
+
if (this.#read(await this.#reader.read()))
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Reads what follows a delimiter: `--` ends the body, a new line starts
|
|
457
|
+
* the next part's headers, which it is left in front of. Spaces and tabs
|
|
458
|
+
* before the new line are transport padding, which RFC 2046 says to ignore.
|
|
459
|
+
*
|
|
460
|
+
* @returns `true` when the delimiter closes the body
|
|
461
|
+
* @throws {Malformed} Otherwise, since RFC 2046 does not let the boundary
|
|
462
|
+
* start a line of content, as in `--boundary-suffix`
|
|
463
|
+
*/
|
|
464
|
+
async #closed() {
|
|
465
|
+
/**
|
|
466
|
+
* @param i Index after the offset
|
|
467
|
+
* @returns The byte there, reading the next chunk if needed
|
|
468
|
+
*/
|
|
469
|
+
const at = async (i) => {
|
|
470
|
+
while (this.#valid - this.#offset <= i &&
|
|
471
|
+
!this.#read(await this.#reader.read()))
|
|
472
|
+
;
|
|
473
|
+
return this.#valid - this.#offset > i
|
|
474
|
+
? this.#memory[this.#offset + i]
|
|
475
|
+
: undefined;
|
|
476
|
+
};
|
|
477
|
+
if ((await at(0)) === 45 && (await at(1)) === 45) {
|
|
478
|
+
// done, drain any epilogue without parsing it
|
|
479
|
+
while (!this.#read(await this.#reader.read(), false))
|
|
480
|
+
;
|
|
481
|
+
return true;
|
|
482
|
+
}
|
|
483
|
+
// consumed as it is read, so padding cannot fill the buffer
|
|
484
|
+
for (let byte = await at(0); byte === 32 || byte === 9; byte = await at(0))
|
|
485
|
+
this.#start = this.#end = ++this.#offset;
|
|
486
|
+
if ((await at(0)) === 13 && (await at(1)) === 10)
|
|
487
|
+
return false;
|
|
488
|
+
throw new Malformed("Invalid Boundary");
|
|
281
489
|
}
|
|
282
490
|
/** @yields Multipart form data `Part`(s) */
|
|
283
491
|
async *[Symbol.asyncIterator]() {
|
|
492
|
+
let complete = false;
|
|
284
493
|
try {
|
|
285
|
-
await this.#findConcat(this.#opening);
|
|
286
|
-
|
|
287
|
-
|
|
494
|
+
let preamble = await this.#findConcat(this.#opening);
|
|
495
|
+
// the first delimiter starts the body or a line, one mentioned within
|
|
496
|
+
// a line of the preamble is not one
|
|
497
|
+
if (preamble?.length &&
|
|
498
|
+
!(preamble.at(-2) === 13 && preamble.at(-1) === 10))
|
|
499
|
+
preamble = await this.#findConcat(this.#boundary);
|
|
500
|
+
if (!preamble)
|
|
501
|
+
throw new Malformed("Missing Opening Boundary");
|
|
502
|
+
// a form without fields sends only the closing delimiter
|
|
503
|
+
for (let i = 0; !(await this.#closed()); i++) {
|
|
504
|
+
const headers = await this.#findConcat(newLine);
|
|
505
|
+
// a body ends with the closing delimiter, after the last part
|
|
506
|
+
if (!headers)
|
|
507
|
+
throw new Malformed(unexpectedEnd);
|
|
288
508
|
if (i === this.#options.parts)
|
|
289
|
-
throw new
|
|
290
|
-
const
|
|
291
|
-
yield
|
|
292
|
-
//
|
|
293
|
-
//
|
|
294
|
-
if (
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
finally {
|
|
301
|
-
partReader.releaseLock();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
if (
|
|
305
|
-
// ensures invalid characters aren't used to determine if it's done
|
|
306
|
-
this.#valid > 1 &&
|
|
307
|
-
// check for --
|
|
308
|
-
// if not done it would be \r\nNEXT-HEADER
|
|
309
|
-
this.#memory[0] === 45 &&
|
|
310
|
-
this.#memory[1] === 45) {
|
|
311
|
-
// done, drain any epilogue
|
|
312
|
-
while (!(await this.#read(false)))
|
|
313
|
-
;
|
|
314
|
-
break;
|
|
509
|
+
throw new Limit("Too Many Parts");
|
|
510
|
+
const { stream, state } = this.#findStream(this.#boundary);
|
|
511
|
+
yield new Part(this.#req, stream, headers);
|
|
512
|
+
// the next part starts after this one's boundary, however much of it
|
|
513
|
+
// was read: skip the rest here, never parse it as headers
|
|
514
|
+
if (!state.ended) {
|
|
515
|
+
state.detached = true;
|
|
516
|
+
await state.pulling?.catch(() => { });
|
|
517
|
+
if (!state.ended && !(await this.#skip(this.#boundary)))
|
|
518
|
+
throw new Malformed(unexpectedEnd);
|
|
315
519
|
}
|
|
316
520
|
}
|
|
521
|
+
complete = true;
|
|
317
522
|
}
|
|
318
523
|
finally {
|
|
524
|
+
// stopped early by a limit, an error, or the caller, so stop the upload
|
|
525
|
+
if (!complete)
|
|
526
|
+
await this.#reader.cancel().catch(() => { });
|
|
319
527
|
this.#reader.releaseLock();
|
|
320
528
|
}
|
|
321
529
|
}
|
|
322
530
|
/**
|
|
323
|
-
*
|
|
531
|
+
* Read every part into `FormData`, within the parser's limits.
|
|
324
532
|
*
|
|
325
533
|
* Only use `data` when buffering all content is needed, otherwise iterate through
|
|
326
534
|
* the multipart using `for await...of` instead.
|