ovr 6.0.0 → 6.1.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.
@@ -469,69 +469,66 @@ type LabelAttributes = Attributes<{ for: string }>;
469
469
 
470
470
  type LiAttributes = Attributes<{ value: string | number }>;
471
471
 
472
- type LinkAttributes = Attributes<
473
- {
474
- as:
475
- | "audio"
476
- | "document"
477
- | "embed"
478
- | "fetch"
479
- | "font"
480
- | "image"
481
- | "object"
482
- | "script"
483
- | "style"
484
- | "track"
485
- | "video"
486
- | "worker";
487
- blocking: SharedAttributes["blocking"];
488
- crossorigin: SharedAttributes["crossorigin"];
489
- disabled: boolean;
490
- fetchpriority: SharedAttributes["fetchpriority"];
491
- href: string;
492
- hreflang: string;
493
- imagesizes: string;
494
- imagesrcset: string;
495
- integrity: string;
496
- media: string;
497
- // not all the same options as others
498
- referrerpolicy:
499
- | "no-referrer"
500
- | "no-referrer-when-downgrade"
501
- | "origin"
502
- | "origin-when-cross-origin"
503
- | "unsafe-url";
504
- // not all the same options as others
505
- rel:
506
- | "alternate"
507
- | "author"
508
- | "canonical"
509
- | "dns-prefetch"
510
- | "expect"
511
- | "help"
512
- | "icon"
513
- | "license"
514
- | "manifest"
515
- | "me"
516
- | "modulepreload"
517
- | "next"
518
- | "pingback"
519
- | "preconnect"
520
- | "prefetch"
521
- | "preload"
522
- | "prerender"
523
- | "prev"
524
- | "privacy-policy"
525
- | "search"
526
- | "stylesheet"
527
- | "terms-of-service"
528
- // allows multiple
529
- | (string & {});
530
- sizes: string;
531
- type: string;
532
- },
533
- undefined
534
- >;
472
+ type LinkAttributes = Attributes<{
473
+ as:
474
+ | "audio"
475
+ | "document"
476
+ | "embed"
477
+ | "fetch"
478
+ | "font"
479
+ | "image"
480
+ | "object"
481
+ | "script"
482
+ | "style"
483
+ | "track"
484
+ | "video"
485
+ | "worker";
486
+ blocking: SharedAttributes["blocking"];
487
+ crossorigin: SharedAttributes["crossorigin"];
488
+ disabled: boolean;
489
+ fetchpriority: SharedAttributes["fetchpriority"];
490
+ href: string;
491
+ hreflang: string;
492
+ imagesizes: string;
493
+ imagesrcset: string;
494
+ integrity: string;
495
+ media: string;
496
+ // not all the same options as others
497
+ referrerpolicy:
498
+ | "no-referrer"
499
+ | "no-referrer-when-downgrade"
500
+ | "origin"
501
+ | "origin-when-cross-origin"
502
+ | "unsafe-url";
503
+ // not all the same options as others
504
+ rel:
505
+ | "alternate"
506
+ | "author"
507
+ | "canonical"
508
+ | "dns-prefetch"
509
+ | "expect"
510
+ | "help"
511
+ | "icon"
512
+ | "license"
513
+ | "manifest"
514
+ | "me"
515
+ | "modulepreload"
516
+ | "next"
517
+ | "pingback"
518
+ | "preconnect"
519
+ | "prefetch"
520
+ | "preload"
521
+ | "prerender"
522
+ | "prev"
523
+ | "privacy-policy"
524
+ | "search"
525
+ | "stylesheet"
526
+ | "terms-of-service"
527
+ // allows multiple
528
+ | (string & {});
529
+ sizes: string;
530
+ type: string;
531
+ }>; // not strictly a void element since it can have children in XML
535
532
 
536
533
  type MapAttributes = Attributes<{ name: string }>;
537
534
 
@@ -713,7 +710,234 @@ type VideoAttributes = Attributes<{
713
710
  width: string | number;
714
711
  }>;
715
712
 
716
- /** Standard HTML elements */
713
+ // XML
714
+ type XMLAttributes<
715
+ P extends JSX.Props = JSX.Props,
716
+ Children = JSX.Element,
717
+ > = Partial<{ children: Children; xmlns: string }> & JSX.Props & Partial<P>;
718
+
719
+ type XMLDeclarationAttributes = {
720
+ version?: "1.0" | (string & {});
721
+ encoding?: "utf-8" | (string & {});
722
+ standalone?: "yes" | "no";
723
+ };
724
+
725
+ type RSSAttributes = XMLAttributes<{
726
+ version: "2.0" | (string & {});
727
+ "xmlns:atom": string;
728
+ }>;
729
+
730
+ type AtomLinkAttributes = {
731
+ href: string;
732
+ rel?: "alternate" | "related" | "self" | "enclosure" | "via" | (string & {});
733
+ type?:
734
+ | "application/atom+xml"
735
+ | "application/rss+xml"
736
+ | "application/xml"
737
+ | "text/xml"
738
+ | "text/html"
739
+ | (string & {});
740
+ hreflang?: string;
741
+ title?: string;
742
+ length?: string | number;
743
+ };
744
+
745
+ // SVG
746
+ // SVG
747
+ type SvgAttributes<
748
+ P extends JSX.Props = JSX.Props,
749
+ Children = JSX.Element,
750
+ > = Partial<{
751
+ children: Children;
752
+ // Core attributes
753
+ id: string;
754
+ class: string;
755
+ style: string;
756
+ // Presentation attributes
757
+ fill: string;
758
+ "fill-opacity": string | number;
759
+ "fill-rule": "nonzero" | "evenodd";
760
+ stroke: string;
761
+ "stroke-width": string | number;
762
+ "stroke-opacity": string | number;
763
+ "stroke-linecap": "butt" | "round" | "square";
764
+ "stroke-linejoin": "miter" | "round" | "bevel";
765
+ "stroke-dasharray": string;
766
+ "stroke-dashoffset": string | number;
767
+ opacity: string | number;
768
+ transform: string;
769
+ "transform-origin": string;
770
+ // Clipping / masking
771
+ "clip-path": string;
772
+ "clip-rule": "nonzero" | "evenodd";
773
+ mask: string;
774
+ }> &
775
+ JSX.Props &
776
+ Partial<P>;
777
+
778
+ type SvgRootAttributes = SvgAttributes<{
779
+ xmlns: string;
780
+ "xmlns:xlink": string;
781
+ width: string | number;
782
+ height: string | number;
783
+ viewBox: string;
784
+ preserveAspectRatio:
785
+ | "none"
786
+ | "xMinYMin"
787
+ | "xMidYMin"
788
+ | "xMaxYMin"
789
+ | "xMinYMid"
790
+ | "xMidYMid"
791
+ | "xMaxYMid"
792
+ | "xMinYMax"
793
+ | "xMidYMax"
794
+ | "xMaxYMax"
795
+ | (string & {});
796
+ }>;
797
+
798
+ type SvgPathAttributes = SvgAttributes<{ d: string }>;
799
+
800
+ type SvgCircleAttributes = SvgAttributes<{
801
+ cx: string | number;
802
+ cy: string | number;
803
+ r: string | number;
804
+ }>;
805
+
806
+ type SvgEllipseAttributes = SvgAttributes<{
807
+ cx: string | number;
808
+ cy: string | number;
809
+ rx: string | number;
810
+ ry: string | number;
811
+ }>;
812
+
813
+ type SvgRectAttributes = SvgAttributes<{
814
+ x: string | number;
815
+ y: string | number;
816
+ width: string | number;
817
+ height: string | number;
818
+ rx: string | number;
819
+ ry: string | number;
820
+ }>;
821
+
822
+ type SvgLineAttributes = SvgAttributes<{
823
+ x1: string | number;
824
+ y1: string | number;
825
+ x2: string | number;
826
+ y2: string | number;
827
+ }>;
828
+
829
+ type SvgPolylineAttributes = SvgAttributes<{ points: string }>;
830
+
831
+ type SvgPolygonAttributes = SvgAttributes<{ points: string }>;
832
+
833
+ type SvgTextAttributes = SvgAttributes<{
834
+ x: string | number;
835
+ y: string | number;
836
+ dx: string | number;
837
+ dy: string | number;
838
+ "text-anchor": "start" | "middle" | "end";
839
+ "dominant-baseline":
840
+ | "auto"
841
+ | "middle"
842
+ | "hanging"
843
+ | "mathematical"
844
+ | "text-top"
845
+ | "text-bottom"
846
+ | (string & {});
847
+ "font-family": string;
848
+ "font-size": string | number;
849
+ "font-weight": string | number;
850
+ "letter-spacing": string | number;
851
+ }>;
852
+
853
+ type SvgTspanAttributes = SvgAttributes<{
854
+ x: string | number;
855
+ y: string | number;
856
+ dx: string | number;
857
+ dy: string | number;
858
+ }>;
859
+
860
+ type SvgUseAttributes = SvgAttributes<{
861
+ href: string;
862
+ "xlink:href": string;
863
+ x: string | number;
864
+ y: string | number;
865
+ width: string | number;
866
+ height: string | number;
867
+ }>;
868
+
869
+ type SvgSymbolAttributes = SvgAttributes<{
870
+ viewBox: string;
871
+ preserveAspectRatio: SvgRootAttributes["preserveAspectRatio"];
872
+ }>;
873
+
874
+ type SvgImageAttributes = SvgAttributes<{
875
+ href: string;
876
+ "xlink:href": string;
877
+ x: string | number;
878
+ y: string | number;
879
+ width: string | number;
880
+ height: string | number;
881
+ preserveAspectRatio: SvgRootAttributes["preserveAspectRatio"];
882
+ }>;
883
+
884
+ type SvgLinearGradientAttributes = SvgAttributes<
885
+ {
886
+ x1: string | number;
887
+ y1: string | number;
888
+ x2: string | number;
889
+ y2: string | number;
890
+ gradientUnits: "userSpaceOnUse" | "objectBoundingBox";
891
+ gradientTransform: string;
892
+ spreadMethod: "pad" | "reflect" | "repeat";
893
+ },
894
+ undefined
895
+ >;
896
+
897
+ type SvgRadialGradientAttributes = SvgAttributes<
898
+ {
899
+ cx: string | number;
900
+ cy: string | number;
901
+ r: string | number;
902
+ fx: string | number;
903
+ fy: string | number;
904
+ gradientUnits: "userSpaceOnUse" | "objectBoundingBox";
905
+ gradientTransform: string;
906
+ spreadMethod: "pad" | "reflect" | "repeat";
907
+ },
908
+ undefined
909
+ >;
910
+
911
+ type SvgStopAttributes = SvgAttributes<
912
+ {
913
+ offset: string | number;
914
+ "stop-color": string;
915
+ "stop-opacity": string | number;
916
+ },
917
+ undefined
918
+ >;
919
+
920
+ type SvgClipPathAttributes = SvgAttributes<{
921
+ clipPathUnits: "userSpaceOnUse" | "objectBoundingBox";
922
+ }>;
923
+
924
+ type SvgMaskAttributes = SvgAttributes<{
925
+ x: string | number;
926
+ y: string | number;
927
+ width: string | number;
928
+ height: string | number;
929
+ maskUnits: "userSpaceOnUse" | "objectBoundingBox";
930
+ maskContentUnits: "userSpaceOnUse" | "objectBoundingBox";
931
+ }>;
932
+
933
+ type SvgForeignObjectAttributes = SvgAttributes<{
934
+ x: string | number;
935
+ y: string | number;
936
+ width: string | number;
937
+ height: string | number;
938
+ }>;
939
+
940
+ /** Standard HTML/XML elements */
717
941
  export type IntrinsicElements =
718
942
  // allows custom elements
719
943
  Record<string, Attributes> & {
@@ -828,4 +1052,36 @@ export type IntrinsicElements =
828
1052
  var: Attributes;
829
1053
  video: VideoAttributes;
830
1054
  wbr: Attributes<JSX.Props, undefined>;
1055
+
1056
+ // XML that have xmlns or more
1057
+ // custom are still allowed so they are covered
1058
+ xml: XMLDeclarationAttributes;
1059
+ rss: RSSAttributes;
1060
+ channel: XMLAttributes;
1061
+ item: XMLAttributes;
1062
+ urlset: XMLAttributes;
1063
+ "atom:link": AtomLinkAttributes;
1064
+
1065
+ // SVG
1066
+ svg: SvgRootAttributes;
1067
+ g: SvgAttributes;
1068
+ defs: SvgAttributes;
1069
+ path: SvgPathAttributes;
1070
+ circle: SvgCircleAttributes;
1071
+ ellipse: SvgEllipseAttributes;
1072
+ rect: SvgRectAttributes;
1073
+ line: SvgLineAttributes;
1074
+ polyline: SvgPolylineAttributes;
1075
+ polygon: SvgPolygonAttributes;
1076
+ text: SvgTextAttributes;
1077
+ tspan: SvgTspanAttributes;
1078
+ use: SvgUseAttributes;
1079
+ symbol: SvgSymbolAttributes;
1080
+ image: SvgImageAttributes;
1081
+ linearGradient: SvgLinearGradientAttributes;
1082
+ radialGradient: SvgRadialGradientAttributes;
1083
+ stop: SvgStopAttributes;
1084
+ clipPath: SvgClipPathAttributes;
1085
+ mask: SvgMaskAttributes;
1086
+ foreignObject: SvgForeignObjectAttributes;
831
1087
  };
package/src/jsx/index.ts CHANGED
@@ -1,14 +1,16 @@
1
1
  import { Render } from "../render/index.js";
2
- import type { Util } from "../util/index.js";
3
2
  import type { IntrinsicElements as IE } from "./elements.js";
4
3
 
5
4
  export namespace JSX {
5
+ type MaybePromise<T> = T | Promise<T>;
6
+ type MaybeFunction<T> = T | (() => T);
7
+
6
8
  /** Standard HTML elements */
7
9
  export interface IntrinsicElements extends IE {}
8
10
 
9
11
  /** JSX Element */
10
- export type Element = Util.MaybeFunction<
11
- Util.MaybePromise<
12
+ export type Element = MaybeFunction<
13
+ MaybePromise<
12
14
  | string
13
15
  | number
14
16
  | bigint
@@ -47,6 +49,7 @@ export class JSX {
47
49
  "source",
48
50
  "track",
49
51
  "wbr",
52
+ "xml",
50
53
  ]);
51
54
 
52
55
  /**
@@ -70,11 +73,9 @@ export class JSX {
70
73
  return;
71
74
  }
72
75
 
73
- if (tag === "html") yield Render.html("<!doctype html>");
74
-
75
76
  // intrinsic element
76
77
  // faster to concatenate attributes than to yield them as separate chunks
77
- let attributes = "";
78
+ let opening = tag;
78
79
 
79
80
  for (const key in props) {
80
81
  // more memory efficient to skip children instead of destructuring and using ...rest
@@ -84,18 +85,27 @@ export class JSX {
84
85
 
85
86
  if (value === true) {
86
87
  // just put the key without the value
87
- attributes += ` ${key}`;
88
+ opening += ` ${key}`;
88
89
  } else if (typeof value === "string") {
89
- attributes += ` ${key}="${Render.escape(value, true)}"`;
90
+ opening += ` ${key}="${Render.escape(value, true)}"`;
90
91
  } else if (typeof value === "number" || typeof value === "bigint") {
91
- attributes += ` ${key}="${value}"`;
92
+ opening += ` ${key}="${value}"`;
92
93
  }
93
94
  // otherwise, don't include the attribute
94
95
  }
95
96
 
96
- yield Render.html(`<${tag}${attributes}>`);
97
+ if (tag === "html") {
98
+ yield Render.html(`<!doctype html><${opening}>`);
99
+ } else if (tag === "xml") {
100
+ yield Render.html(`<?${opening}?>`);
101
+ } else {
102
+ yield Render.html(`<${opening}>`);
103
+ }
97
104
 
98
- if (JSX.#void.has(tag)) return;
105
+ // if children and a void element, render it like normal
106
+ // to account for cases like XML <link>content</link>
107
+ // assumes the user knows what they are doing
108
+ if (props.children == null && JSX.#void.has(tag)) return;
99
109
 
100
110
  yield* new Render(props.children);
101
111
 
@@ -1,4 +1,4 @@
1
- import { Codec, Header } from "../util/index.js";
1
+ import { Codec, Header, Mime } from "../util/index.js";
2
2
 
3
3
  /** Sequence of bytes to find within the stream */
4
4
  class Needle extends Uint8Array {
@@ -50,6 +50,7 @@ class Part extends Request {
50
50
  */
51
51
  readonly filename: string | null;
52
52
 
53
+ // this is named `type` instead of `mime` to align with `Blob.type`
53
54
  /**
54
55
  * Media type of the part
55
56
  *
@@ -91,10 +92,10 @@ class Part extends Request {
91
92
  duplex: "half",
92
93
  });
93
94
 
94
- [this.type] = Header.shift(this.headers.get(Header.contentType));
95
+ [this.type] = Header.shift(this.headers.get(Header.type));
95
96
 
96
97
  ({ name: this.name = null, filename: this.filename = null } = Header.params(
97
- this.headers.get("content-disposition"),
98
+ this.headers.get(Header.disposition),
98
99
  ));
99
100
  }
100
101
  }
@@ -161,7 +162,6 @@ export namespace Multipart {
161
162
  export class Multipart extends Request {
162
163
  static readonly #kb = 1024;
163
164
  static readonly #mb = 1024 ** 2;
164
- static #multipartType = "multipart/form-data";
165
165
 
166
166
  /** New line needle to share across requests and parts */
167
167
  static readonly #newLine = new Needle("\r\n\r\n");
@@ -215,11 +215,9 @@ export class Multipart extends Request {
215
215
  constructor(req: Request, options?: Multipart.Options) {
216
216
  super(req);
217
217
 
218
- const [type, params] = Header.shift(this.headers.get(Header.contentType));
218
+ const [mime, params] = Header.shift(this.headers.get(Header.type));
219
219
 
220
- if (type !== Multipart.#multipartType) {
221
- throw new TypeError("Unsupported Media Type");
222
- }
220
+ if (!Mime.multipart(mime)) throw new TypeError("Unsupported Media Type");
223
221
 
224
222
  const { boundary } = Header.params(params);
225
223
 
@@ -468,7 +466,7 @@ export class Multipart extends Request {
468
466
  if (part.name) {
469
467
  let value: string | File;
470
468
 
471
- if (part.filename || part.type === "application/octet-stream") {
469
+ if (part.filename || part.type === Mime.stream) {
472
470
  const blob = await part.blob();
473
471
  value = new File([blob], part.filename ?? "blob", {
474
472
  type: blob.type,
@@ -1,7 +1,7 @@
1
1
  import { type JSX, jsx } from "../jsx/index.js";
2
2
  import type { Middleware } from "../middleware/index.js";
3
3
  import type { Trie } from "../trie/index.js";
4
- import { Hash } from "../util/index.js";
4
+ import { Hash, Method, Mime } from "../util/index.js";
5
5
 
6
6
  /** Helper type to extract the route params (`:slug`) into a record */
7
7
  export type ExtractParams<Pattern extends string = string> =
@@ -247,7 +247,8 @@ export class Route<Pattern extends string = string> {
247
247
  * @returns Route with added components
248
248
  */
249
249
  static #withComponents<Pattern extends string>(route: Route<Pattern>) {
250
- const enctype = route.method === "POST" ? "multipart/form-data" : undefined;
250
+ const enctype =
251
+ route.method === Method.post ? Mime.multipartFormData : undefined;
251
252
 
252
253
  return Object.assign(route, {
253
254
  Button: (({ params, search, hash, ...rest }) =>
@@ -285,7 +286,7 @@ export class Route<Pattern extends string = string> {
285
286
  WithForm<Pattern> &
286
287
  WithAnchor<Pattern> {
287
288
  const route = Route.#withComponents(
288
- new Route("GET", pattern, ...middleware),
289
+ new Route(Method.get, pattern, ...middleware),
289
290
  );
290
291
 
291
292
  return Object.assign(route, {
@@ -327,6 +328,8 @@ export class Route<Pattern extends string = string> {
327
328
  pattern = `/_p/${Hash.djb2(middleware.join())}` as Pattern;
328
329
  }
329
330
 
330
- return Route.#withComponents(new Route("POST", pattern, ...middleware));
331
+ return Route.#withComponents(
332
+ new Route(Method.post, pattern, ...middleware),
333
+ );
331
334
  }
332
335
  }
package/src/util/index.ts CHANGED
@@ -1,14 +1,72 @@
1
1
  /** General utility types */
2
2
  export namespace Util {
3
- export type MaybePromise<T> = T | Promise<T>;
4
- export type MaybeFunction<T> = T | (() => T);
5
3
  export type DeepArray<T> = T | DeepArray<T>[];
6
4
  }
7
5
 
6
+ /** Media type utils */
7
+ export class Mime {
8
+ static readonly html = Mime.#text("html");
9
+ static readonly text = Mime.#text("plain");
10
+ static readonly json = Mime.#application("json");
11
+ static readonly stream = Mime.#application("octet-stream");
12
+ static readonly #multipartBase = "multipart/";
13
+ static readonly multipartFormData = Mime.#multipart("form-data");
14
+
15
+ static #text<T extends string>(type: T) {
16
+ return `text/${type}` as const;
17
+ }
18
+
19
+ static #application<T extends string>(type: T) {
20
+ return `application/${type}` as const;
21
+ }
22
+
23
+ static #multipart<T extends string>(type: T) {
24
+ return `${Mime.#multipartBase}${type}` as const;
25
+ }
26
+
27
+ static readonly #markup = new Set<string>([
28
+ Mime.html,
29
+ Mime.#text("xml"),
30
+ Mime.#application("xml"),
31
+ ]);
32
+
33
+ /**
34
+ * @param mime Media type
35
+ * @returns `true` if the mime is markup
36
+ */
37
+ static markup(mime: string) {
38
+ return (
39
+ Mime.#markup.has(mime) ||
40
+ // covers other xml types like svg
41
+ mime.includes("+xml")
42
+ );
43
+ }
44
+
45
+ /**
46
+ * @param mime Media type
47
+ * @returns `true` if the mime is multipart
48
+ */
49
+ static multipart(mime: string | null) {
50
+ return mime?.startsWith(Mime.#multipartBase);
51
+ }
52
+ }
53
+
8
54
  /** Header parsing utils */
9
55
  export class Header {
10
- // shared content type header to minify better
11
- static readonly contentType = "content-type";
56
+ static readonly type = "content-type";
57
+ static readonly disposition = "content-disposition";
58
+ static readonly etag = "etag";
59
+ static readonly ifNoneMatch = "if-none-match";
60
+ static readonly cookie = "cookie";
61
+ static readonly setCookie = "set-cookie";
62
+
63
+ /**
64
+ * @param mime
65
+ * @returns mime; charset=utf-8
66
+ */
67
+ static utf8<M extends string>(mime: M) {
68
+ return `${mime}; charset=utf-8` as const;
69
+ }
12
70
 
13
71
  /**
14
72
  * @param header header value to parse
@@ -110,3 +168,10 @@ export class Codec {
110
168
  /** DO NOT USE FOR STREAMS */
111
169
  static decode = (input?: Uint8Array) => Codec.#decoder.decode(input);
112
170
  }
171
+
172
+ /** HTTP methods */
173
+ export class Method {
174
+ static readonly get = "GET";
175
+ static readonly post = "POST";
176
+ static readonly head = "HEAD";
177
+ }