ovr 5.0.0 → 6.0.1

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 (86) hide show
  1. package/dist/LICENSE.md +26 -0
  2. package/dist/app/index.d.ts +49 -83
  3. package/dist/app/index.d.ts.map +1 -1
  4. package/dist/app/index.js +40 -92
  5. package/dist/context/index.d.ts +138 -0
  6. package/dist/context/index.d.ts.map +1 -0
  7. package/dist/context/index.js +218 -0
  8. package/dist/cookie/index.d.ts +121 -0
  9. package/dist/cookie/index.d.ts.map +1 -0
  10. package/dist/cookie/index.js +56 -0
  11. package/dist/index.d.ts +6 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +4 -6
  14. package/dist/jsx/{intrinsic-elements.d.ts → elements.d.ts} +14 -7
  15. package/dist/jsx/elements.d.ts.map +1 -0
  16. package/dist/jsx/index.d.ts +40 -53
  17. package/dist/jsx/index.d.ts.map +1 -1
  18. package/dist/jsx/index.js +71 -200
  19. package/dist/middleware/index.d.ts +22 -0
  20. package/dist/middleware/index.d.ts.map +1 -0
  21. package/dist/multipart/index.d.ts +122 -0
  22. package/dist/multipart/index.d.ts.map +1 -0
  23. package/dist/multipart/index.js +340 -0
  24. package/dist/render/index.d.ts +78 -0
  25. package/dist/render/index.d.ts.map +1 -0
  26. package/dist/render/index.js +248 -0
  27. package/dist/route/index.d.ts +147 -0
  28. package/dist/route/index.d.ts.map +1 -0
  29. package/dist/route/index.js +126 -0
  30. package/dist/trie/index.d.ts +30 -30
  31. package/dist/trie/index.d.ts.map +1 -1
  32. package/dist/trie/index.js +67 -82
  33. package/dist/util/index.d.ts +36 -0
  34. package/dist/util/index.d.ts.map +1 -0
  35. package/dist/util/index.js +87 -0
  36. package/package.json +3 -3
  37. package/src/app/index.ts +99 -224
  38. package/src/context/index.ts +262 -0
  39. package/src/cookie/index.ts +189 -0
  40. package/src/index.ts +6 -7
  41. package/src/jsx/{intrinsic-elements.ts → elements.ts} +14 -7
  42. package/src/jsx/index.ts +81 -221
  43. package/src/middleware/index.ts +29 -0
  44. package/src/multipart/index.ts +485 -0
  45. package/src/render/index.ts +300 -0
  46. package/src/route/index.ts +332 -0
  47. package/src/trie/index.ts +87 -111
  48. package/src/util/index.ts +109 -0
  49. package/dist/app/context.d.ts +0 -149
  50. package/dist/app/context.d.ts.map +0 -1
  51. package/dist/app/context.js +0 -246
  52. package/dist/app/helper/get.d.ts +0 -30
  53. package/dist/app/helper/get.d.ts.map +0 -1
  54. package/dist/app/helper/get.js +0 -33
  55. package/dist/app/helper/index.d.ts +0 -51
  56. package/dist/app/helper/index.d.ts.map +0 -1
  57. package/dist/app/helper/index.js +0 -82
  58. package/dist/app/helper/post.d.ts +0 -35
  59. package/dist/app/helper/post.d.ts.map +0 -1
  60. package/dist/app/helper/post.js +0 -15
  61. package/dist/app/memo/index.d.ts +0 -12
  62. package/dist/app/memo/index.d.ts.map +0 -1
  63. package/dist/app/memo/index.js +0 -34
  64. package/dist/jsx/chunk/index.d.ts +0 -38
  65. package/dist/jsx/chunk/index.d.ts.map +0 -1
  66. package/dist/jsx/chunk/index.js +0 -69
  67. package/dist/jsx/intrinsic-elements.d.ts.map +0 -1
  68. package/dist/jsx/merge-async-generators.d.ts +0 -14
  69. package/dist/jsx/merge-async-generators.d.ts.map +0 -1
  70. package/dist/jsx/merge-async-generators.js +0 -42
  71. package/dist/types/index.d.ts +0 -17
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/util/hash.d.ts +0 -11
  74. package/dist/util/hash.d.ts.map +0 -1
  75. package/dist/util/hash.js +0 -16
  76. package/src/app/context.ts +0 -303
  77. package/src/app/helper/get.ts +0 -46
  78. package/src/app/helper/index.ts +0 -156
  79. package/src/app/helper/post.ts +0 -54
  80. package/src/app/memo/index.ts +0 -40
  81. package/src/jsx/chunk/index.ts +0 -79
  82. package/src/jsx/merge-async-generators.ts +0 -47
  83. package/src/types/index.ts +0 -42
  84. package/src/util/hash.ts +0 -17
  85. /package/dist/jsx/{intrinsic-elements.js → elements.js} +0 -0
  86. /package/dist/{types → middleware}/index.js +0 -0
@@ -0,0 +1,485 @@
1
+ import { Codec, Header } from "../util/index.js";
2
+
3
+ /** Sequence of bytes to find within the stream */
4
+ class Needle extends Uint8Array {
5
+ /** Index of the last character in the needle */
6
+ readonly end = this.length - 1;
7
+
8
+ /**
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)
12
+ */
13
+ readonly skip = new Uint8Array(256).fill(this.length);
14
+
15
+ /** Stores where each byte is located in the needle */
16
+ readonly loc: (number[] | undefined)[] = new Array(256);
17
+
18
+ /** @param needle String to find within the stream */
19
+ constructor(needle: string) {
20
+ super(Codec.encode(needle).buffer);
21
+
22
+ for (let i = 0; i < this.length; i++) {
23
+ const byte = this[i]!;
24
+
25
+ // skip the last char of the needle since that would be a find
26
+ if (i !== this.end) this.skip[byte] = this.end - i;
27
+
28
+ (this.loc[byte] ??= []).push(i);
29
+ }
30
+ }
31
+ }
32
+
33
+ /** Multipart part */
34
+ class Part extends Request {
35
+ /**
36
+ * Form input `name` attribute
37
+ *
38
+ * @example
39
+ *
40
+ * ```html
41
+ * <input type="file" name="photo">
42
+ * ```
43
+ */
44
+ readonly name: string | null;
45
+
46
+ /**
47
+ * Filename from Content-Disposition header if file
48
+ *
49
+ * @example "my-image.png"
50
+ */
51
+ readonly filename: string | null;
52
+
53
+ /**
54
+ * Media type of the part
55
+ *
56
+ * @example "image/png"
57
+ */
58
+ readonly type: string | null;
59
+
60
+ // part body will always be defined, this removes the `null` type
61
+ /** Part body */
62
+ declare readonly body: ReadableStream<Uint8Array<ArrayBuffer>>;
63
+
64
+ /**
65
+ * Create a new multipart part
66
+ *
67
+ * @param req Original request
68
+ * @param body Part body
69
+ * @param rawHeaders Raw buffer of HTTP headers for the part
70
+ */
71
+ constructor(req: Request, body: ReadableStream, rawHeaders: Uint8Array) {
72
+ const headers: [string, string][] = [];
73
+
74
+ // create headers
75
+ for (const line of Codec.decode(rawHeaders).split("\r\n")) {
76
+ const colon = line.indexOf(":");
77
+
78
+ if (colon !== -1) {
79
+ headers.push([
80
+ line.slice(0, colon).trim(),
81
+ // no need to trim value - Headers.set does this already
82
+ line.slice(colon + 1),
83
+ ]);
84
+ }
85
+ }
86
+
87
+ super(req, {
88
+ headers,
89
+ body,
90
+ // @ts-expect-error - streaming https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#duplex
91
+ duplex: "half",
92
+ });
93
+
94
+ [this.type] = Header.shift(this.headers.get(Header.contentType));
95
+
96
+ ({ name: this.name = null, filename: this.filename = null } = Header.params(
97
+ this.headers.get("content-disposition"),
98
+ ));
99
+ }
100
+ }
101
+
102
+ export namespace Multipart {
103
+ /** Multipart options */
104
+ export type Options = {
105
+ /**
106
+ * Maximum memory allocation for request body processing (default 4MB)
107
+ *
108
+ * Even for large files, the parser does not hold the entire file in memory,
109
+ * it processes each chunk of the request body as it arrives.
110
+ * Set to a larger number if for example clients are sending all the
111
+ * data in a massive chunk instead of broken into smaller packets.
112
+ *
113
+ * @default 4 * 1024 * 1024
114
+ * @example
115
+ *
116
+ * ```ts
117
+ * const memory = 8 * 1024 * 1024; // increase to 8MB
118
+ * new Multipart(request, { memory });
119
+ * ```
120
+ */
121
+ memory?: number;
122
+
123
+ /**
124
+ * Maximum `Request.body` size in bytes (default 16MB)
125
+ *
126
+ * Prevents attackers from creating massive requests.
127
+ *
128
+ * Since the parser doesn't buffer all chunks in memory, it's possible
129
+ * to handle very large requests. Use this option to adjust the
130
+ * maximum total request body size that will be processed.
131
+ *
132
+ * @default 16 * 1024 * 1024
133
+ * @example
134
+ *
135
+ * ```ts
136
+ * const payload = 1024 ** 3; // increase to 1GB
137
+ * new Multipart(request, { payload });
138
+ * ```
139
+ */
140
+ payload?: number;
141
+
142
+ /**
143
+ * Maximum number of parts.
144
+ *
145
+ * @default Infinity
146
+ * @example
147
+ *
148
+ * ```ts
149
+ * const parts = 4; // only allow 4 parts
150
+ * new Multipart(request, { parts });
151
+ * ```
152
+ */
153
+ parts?: number;
154
+ };
155
+
156
+ /** Type for a `Part` of the multipart body */
157
+ export type Part = InstanceType<typeof Part>;
158
+ }
159
+
160
+ /** Multipart request */
161
+ export class Multipart extends Request {
162
+ static readonly #kb = 1024;
163
+ static readonly #mb = 1024 ** 2;
164
+
165
+ /** New line needle to share across requests and parts */
166
+ static readonly #newLine = new Needle("\r\n\r\n");
167
+
168
+ /** Parser options */
169
+ readonly #options: Required<Multipart.Options> = {
170
+ memory: 4 * Multipart.#mb,
171
+ payload: 16 * Multipart.#mb,
172
+ parts: Infinity,
173
+ };
174
+
175
+ /** Request body reader */
176
+ readonly #reader: ReadableStreamDefaultReader;
177
+
178
+ /** Current values being buffered in memory */
179
+ readonly #memory: Uint8Array<ArrayBuffer>;
180
+
181
+ /** Opening boundary needle */
182
+ readonly #opening: Needle;
183
+
184
+ /** Part boundary needle */
185
+ readonly #boundary: Needle;
186
+
187
+ /** Where valid data ends, valid < #valid >= invalid or empty space */
188
+ #valid = 0;
189
+
190
+ /** Start index of the found needle */
191
+ #start = 0;
192
+
193
+ /** End index of the found needle */
194
+ #end = 0;
195
+
196
+ /** Total bytes read from the stream */
197
+ #payloadSize = 0;
198
+
199
+ /**
200
+ * Split a multipart request into parts.
201
+ *
202
+ * @param req Multipart request
203
+ * @param options Options
204
+ * @yields Multipart form data `Part`(s)
205
+ * @throws {TypeError} If request headers are invalid
206
+ * @example
207
+ *
208
+ * ```ts
209
+ * for await (const part of new Multipart(request)) {
210
+ * // ...
211
+ * }
212
+ * ```
213
+ */
214
+ constructor(req: Request, options?: Multipart.Options) {
215
+ super(req);
216
+
217
+ const [type, params] = Header.shift(this.headers.get(Header.contentType));
218
+
219
+ if (!type?.startsWith("multipart/")) {
220
+ throw new TypeError("Unsupported Media Type");
221
+ }
222
+
223
+ const { boundary } = Header.params(params);
224
+
225
+ if (!boundary) throw new TypeError("Boundary Not Found");
226
+ if (!this.body) throw new TypeError("No Request Body");
227
+
228
+ Object.assign(this.#options, options);
229
+ this.#reader = this.body.getReader();
230
+ this.#opening = new Needle(`--${boundary}\r\n`);
231
+ this.#boundary = new Needle(`\r\n--${boundary}`);
232
+
233
+ this.#memory = new Uint8Array(
234
+ new ArrayBuffer(
235
+ // slightly larger than common chunk size/high water mark 64kb for leftover boundary
236
+ // prevents having to resize memory for most requests
237
+ 65 * Multipart.#kb,
238
+ // cap max chunk size + leftover
239
+ { maxByteLength: this.#options.memory },
240
+ ),
241
+ );
242
+ }
243
+
244
+ /**
245
+ * Reads the next chunk in the request stream and concatenates it
246
+ * onto the buffer.
247
+ *
248
+ * @param append
249
+ * @returns `true` if done
250
+ */
251
+ async #read(append = true) {
252
+ const next = await this.#reader.read();
253
+
254
+ if (next.done) return true;
255
+
256
+ const nextLength = next.value.length;
257
+
258
+ if ((this.#payloadSize += nextLength) > this.#options.payload) {
259
+ throw new RangeError("Payload Too Large");
260
+ }
261
+
262
+ if (append) {
263
+ const required = this.#valid + nextLength;
264
+ const size = this.#memory.buffer.byteLength;
265
+
266
+ // resize if full
267
+ if (required > size) {
268
+ this.#memory.buffer.resize(
269
+ Math.min(
270
+ this.#options.memory, // in case double is larger than the max
271
+ Math.max(required, size * 2),
272
+ ),
273
+ );
274
+ }
275
+
276
+ // write at the valid cutoff
277
+ this.#memory.set(next.value, this.#valid);
278
+ this.#valid += nextLength;
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Cuts off the buffer < end index.
284
+ *
285
+ * @returns Shifted off buffer < start index
286
+ */
287
+ #shift() {
288
+ const before = this.#memory.slice(0, this.#start);
289
+
290
+ // how much data is left after the found needle
291
+ const remainder = this.#valid - this.#end;
292
+
293
+ if (remainder) {
294
+ // copy remainder to the start of the buffer
295
+ // this doesn't move the chunks, just copies to the start
296
+ // old will be overwritten
297
+ this.#memory.copyWithin(0, this.#end, this.#valid);
298
+ }
299
+
300
+ this.#valid = remainder;
301
+ this.#start = this.#end = 0;
302
+
303
+ return before;
304
+ }
305
+
306
+ /**
307
+ * Attempts to find the needle within the current buffer (haystack).
308
+ * Sets start and end to the start and end of the found needle, or the
309
+ * safe place to start the next search from if not found.
310
+ *
311
+ * @param needle Needle to find
312
+ * @returns If found, shifts the buffer and returns the result.
313
+ */
314
+ #find(needle: Needle) {
315
+ const haystackLength = this.#valid;
316
+
317
+ // start the search at the last char of the needle
318
+ // since it could be at the very start
319
+ let i = this.#start + needle.end;
320
+
321
+ while (i < haystackLength) {
322
+ for (
323
+ let needleIndex = needle.end, cursor = i;
324
+ needleIndex >= 0 && needle[needleIndex] === this.#memory[cursor];
325
+ needleIndex--, cursor--
326
+ ) {
327
+ if (needleIndex === 0) {
328
+ this.#start = cursor;
329
+ this.#end = cursor + needle.length;
330
+ return this.#shift();
331
+ }
332
+ }
333
+
334
+ i += needle.skip[this.#memory[i]!]!;
335
+ }
336
+
337
+ // where to safely start the next search in the concatenated chunk result
338
+ // go back len - 1 in case it was partially at the end
339
+ this.#start = this.#end =
340
+ needle.length < haystackLength ? haystackLength - (needle.length - 1) : 0;
341
+ }
342
+
343
+ /**
344
+ * Tries to find the needle in the buffer, if not found, the next
345
+ * chunk is read an concatenated onto the buffer.
346
+ *
347
+ * @param needle Needle to find
348
+ * @returns Buffer up until the found needle
349
+ */
350
+ async #findConcat(needle: Needle) {
351
+ let found: Uint8Array | undefined;
352
+ while (!(found = this.#find(needle)) && !(await this.#read()));
353
+ return found;
354
+ }
355
+
356
+ /**
357
+ * @param needle Needle to find
358
+ * @returns Stream that streams the content until the next find
359
+ */
360
+ #findStream(needle: Needle) {
361
+ return new ReadableStream({
362
+ type: "bytes",
363
+ pull: async (c) => {
364
+ let found: Uint8Array<ArrayBuffer> | undefined;
365
+
366
+ for (
367
+ let done: boolean | undefined;
368
+ !(found = this.#find(needle)) && !done;
369
+ done = await this.#read()
370
+ ) {
371
+ // not found within current chunk
372
+ const lastIndex = this.#valid - 1;
373
+ const needleIndices = needle.loc[this.#memory[lastIndex]!];
374
+
375
+ if (needleIndices) {
376
+ // last char is in the boundary, check for partial boundary
377
+ // iterate backwards through the indices
378
+ indices: for (let i = needleIndices.length - 1; i >= 0; i--) {
379
+ for (
380
+ let needleIndex = needleIndices[i]!, cursor = lastIndex;
381
+ needleIndex >= 0 &&
382
+ needle[needleIndex] === this.#memory[cursor];
383
+ needleIndex--, cursor--
384
+ ) {
385
+ if (needleIndex === 0) {
386
+ // these are the same since no needle was found
387
+ this.#start = this.#end = cursor;
388
+ // rerun check if next has the rest
389
+ break indices;
390
+ }
391
+ }
392
+ }
393
+ }
394
+
395
+ const before = this.#shift();
396
+ if (before.length) {
397
+ c.enqueue(before);
398
+ return; // wait for next pull
399
+ }
400
+ }
401
+
402
+ if (found?.length) c.enqueue(found);
403
+
404
+ c.close();
405
+ },
406
+ });
407
+ }
408
+
409
+ /** @yields Multipart form data `Part`(s) */
410
+ async *[Symbol.asyncIterator]() {
411
+ try {
412
+ await this.#findConcat(this.#opening);
413
+
414
+ let headers: Uint8Array | undefined;
415
+
416
+ for (
417
+ let i = 0;
418
+ (headers = await this.#findConcat(Multipart.#newLine));
419
+ i++
420
+ ) {
421
+ if (i === this.#options.parts) throw new RangeError("Too Many Parts");
422
+
423
+ const part = new Part(this, this.#findStream(this.#boundary), headers);
424
+ yield part;
425
+
426
+ // to get next part, the entire body must be read
427
+ // cannot cancel, chunks would be in the next header
428
+ if (part.body && !part.bodyUsed) {
429
+ const partReader = part.body.getReader();
430
+ try {
431
+ while (!(await partReader.read()).done);
432
+ } finally {
433
+ partReader.releaseLock();
434
+ }
435
+ }
436
+
437
+ if (
438
+ // ensures invalid characters aren't used to determine if it's done
439
+ this.#valid > 1 &&
440
+ // check for --
441
+ // if not done it would be \r\nNEXT-HEADER
442
+ this.#memory[0] === 45 &&
443
+ this.#memory[1] === 45
444
+ ) {
445
+ // done, drain any epilogue
446
+ while (!(await this.#read(false)));
447
+ break;
448
+ }
449
+ }
450
+ } finally {
451
+ this.#reader.releaseLock();
452
+ }
453
+ }
454
+
455
+ /**
456
+ * Drop in replacement for `Request.formData` enhanced with size thresholds.
457
+ *
458
+ * Only use `data` when buffering all content is needed, otherwise iterate through
459
+ * the multipart using `for await...of` instead.
460
+ *
461
+ * @returns Buffered `FormData`
462
+ */
463
+ async data() {
464
+ const data = new FormData();
465
+
466
+ for await (const part of this) {
467
+ if (part.name) {
468
+ let value: string | File;
469
+
470
+ if (part.filename || part.type === "application/octet-stream") {
471
+ const blob = await part.blob();
472
+ value = new File([blob], part.filename ?? "blob", {
473
+ type: blob.type,
474
+ });
475
+ } else {
476
+ value = await part.text();
477
+ }
478
+
479
+ data.append(part.name, value);
480
+ }
481
+ }
482
+
483
+ return data;
484
+ }
485
+ }