ovr 2.1.0 → 3.0.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.
@@ -13,7 +13,7 @@ export declare class Action<Pattern extends string = string> {
13
13
  /** POST middleware */
14
14
  middleware: Middleware<any>[];
15
15
  /** `<form>` component with preset `method` and `action` attributes. */
16
- Form: (props: FormProps<ExtractParams<Pattern>>) => AsyncGenerator<string, void, unknown>;
16
+ Form: (props: FormProps<ExtractParams<Pattern>>) => JSX.Element;
17
17
  /**
18
18
  * @param middleware POST middleware
19
19
  *
@@ -1,6 +1,6 @@
1
- import { hash } from "../hash/index.js";
2
1
  import { jsx } from "../jsx/index.js";
3
2
  import { insertParams } from "../trie/insert-params.js";
3
+ import { hash } from "../util/hash.js";
4
4
  export class Action {
5
5
  /** Route pattern */
6
6
  pattern;
@@ -1,7 +1,8 @@
1
- import { hash } from "../hash/index.js";
1
+ import { Chunk } from "../jsx/chunk/index.js";
2
2
  import { toGenerator } from "../jsx/index.js";
3
- import { Memo } from "../memo/index.js";
3
+ import { hash } from "../util/hash.js";
4
4
  import { App, } from "./index.js";
5
+ import { Memo } from "./memo/index.js";
5
6
  class TagNotFound extends Error {
6
7
  constructor(tag) {
7
8
  super(`No closing ${tag} tag found`);
@@ -155,14 +156,18 @@ export class Context {
155
156
  }
156
157
  elements[3] = bodyParts[0];
157
158
  elements.push(Page, bodyClose + bodyParts[1]);
158
- const gen = toGenerator(elements);
159
+ const gen = toGenerator(elements.map((el) => {
160
+ if (typeof el === "string")
161
+ return new Chunk(el, true);
162
+ return el;
163
+ }));
159
164
  this.html(new ReadableStream({
160
165
  async pull(c) {
161
166
  const { value, done } = await gen.next();
162
167
  if (done)
163
168
  c.close();
164
169
  else
165
- c.enqueue(value);
170
+ c.enqueue(value?.value);
166
171
  },
167
172
  cancel() {
168
173
  gen.return();
@@ -1,4 +1,4 @@
1
- import type { Middleware } from "../app/index.js";
1
+ import type { Middleware } from "../index.js";
2
2
  /**
3
3
  * Basic CSRF protection that checks the request's `method` and `Origin` header.
4
4
  *
@@ -13,7 +13,7 @@ export declare class Page<Pattern extends string = string> {
13
13
  /** GET middleware */
14
14
  middleware: Middleware<any>[];
15
15
  /** `<a>` component with preset `href` attribute. */
16
- Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => AsyncGenerator<string, void, unknown>;
16
+ Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
17
17
  /**
18
18
  * @param pattern Route pattern
19
19
  * @param middleware GET middleware
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export { jsx, toGenerator, toString, type JSX } from "./jsx/index.js";
2
+ export { Chunk } from "./jsx/chunk/index.js";
2
3
  export { Trie, Route } from "./trie/index.js";
3
4
  export { App, type Middleware, type Params } from "./app/index.js";
4
5
  export { Context } from "./app/context.js";
5
6
  export { Page } from "./app/page.js";
6
7
  export { Action } from "./app/action.js";
7
- export { escape } from "./escape/index.js";
8
- export { csrf } from "./mw/csrf.js";
8
+ export { csrf } from "./app/mw/csrf.js";
9
9
  export type * from "./types/index.js";
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  export { jsx, toGenerator, toString } from "./jsx/index.js";
2
+ export { Chunk } from "./jsx/chunk/index.js";
2
3
  export { Trie, Route } from "./trie/index.js";
3
4
  export { App } from "./app/index.js";
4
5
  export { Context } from "./app/context.js";
5
6
  export { Page } from "./app/page.js";
6
7
  export { Action } from "./app/action.js";
7
- export { escape } from "./escape/index.js";
8
- export { csrf } from "./mw/csrf.js";
8
+ export { csrf } from "./app/mw/csrf.js";
@@ -0,0 +1,26 @@
1
+ export declare class Chunk {
2
+ #private;
3
+ /**
4
+ * Escapes strings of HTML.
5
+ *
6
+ * @param str String to escape
7
+ * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
8
+ * @returns Escaped string of HTML
9
+ */
10
+ static escape(str: string, attr?: boolean): string;
11
+ /** Safe value to render. */
12
+ value: string;
13
+ /**
14
+ * @param html converted to a string
15
+ * @param safe Set to `true` if the HTML is safe and should not be escaped
16
+ */
17
+ constructor(html: unknown, safe?: boolean);
18
+ /**
19
+ * @returns `value`
20
+ */
21
+ toString(): string;
22
+ /**
23
+ * @param chunks Chunks to append to the end of the chunk
24
+ */
25
+ concat(...chunks: Chunk[]): void;
26
+ }
@@ -0,0 +1,60 @@
1
+ export class Chunk {
2
+ static #attr = /[&"<]/g;
3
+ static #content = /[&<]/g;
4
+ /**
5
+ * Escapes strings of HTML.
6
+ *
7
+ * @param str String to escape
8
+ * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
9
+ * @returns Escaped string of HTML
10
+ */
11
+ static escape(str, attr) {
12
+ // adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
13
+ const s = String(str ?? "");
14
+ const regex = attr ? Chunk.#attr : Chunk.#content;
15
+ // search starts at beginning of the string
16
+ regex.lastIndex = 0;
17
+ // tracks the position of the last successful match in the input string
18
+ let start = 0;
19
+ let result = "";
20
+ // since `g` flag is used, regex maintains state with each loop,
21
+ // checking from the lastIndex onward each time
22
+ while (regex.test(s)) {
23
+ // index of the match
24
+ const i = regex.lastIndex - 1;
25
+ const match = s[i];
26
+ result +=
27
+ // everything that didn't match during this test
28
+ s.slice(start, i) +
29
+ // replacement
30
+ (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
31
+ start = regex.lastIndex;
32
+ }
33
+ return result + s.slice(start);
34
+ }
35
+ /** Safe value to render. */
36
+ value;
37
+ /**
38
+ * @param html converted to a string
39
+ * @param safe Set to `true` if the HTML is safe and should not be escaped
40
+ */
41
+ constructor(html, safe) {
42
+ const str = String(html ?? "");
43
+ if (safe)
44
+ this.value = str;
45
+ else
46
+ this.value = Chunk.escape(str);
47
+ }
48
+ /**
49
+ * @returns `value`
50
+ */
51
+ toString() {
52
+ return this.value;
53
+ }
54
+ /**
55
+ * @param chunks Chunks to append to the end of the chunk
56
+ */
57
+ concat(...chunks) {
58
+ this.value += chunks.join("");
59
+ }
60
+ }
@@ -1,4 +1,5 @@
1
1
  import type { MaybeFunction, MaybePromise } from "../types/index.js";
2
+ import { Chunk } from "./chunk/index.js";
2
3
  import type { Elements } from "./elements.js";
3
4
  export declare namespace JSX {
4
5
  interface IntrinsicElements extends Elements {
@@ -15,7 +16,7 @@ export type FC<Props> = (props: Props) => JSX.Element;
15
16
  * @param props object containing all the properties and attributes passed to the element or component
16
17
  * @returns an async generator that yields parts of HTML
17
18
  */
18
- export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerator<string, void, unknown>;
19
+ export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerator<Chunk, void, unknown>;
19
20
  /**
20
21
  * JSX requires a `Fragment` export to resolve <></>
21
22
  *
@@ -24,12 +25,12 @@ export declare function jsx(tag: FC<Props> | string, props: Props): AsyncGenerat
24
25
  */
25
26
  export declare function Fragment(props?: {
26
27
  children?: JSX.Element;
27
- }): AsyncGenerator<string, void, unknown>;
28
+ }): AsyncGenerator<Chunk, void, unknown>;
28
29
  /**
29
30
  * @param element
30
- * @yields Rendered strings of HTML as the `Element` resolves.
31
+ * @yields Chunks of HTML as the `Element` resolves.
31
32
  */
32
- export declare function toGenerator(element: JSX.Element): AsyncGenerator<string, void, unknown>;
33
+ export declare function toGenerator(element: JSX.Element): AsyncGenerator<Chunk, void, unknown>;
33
34
  /**
34
35
  * Converts a `JSX.Element` into a fully concatenated string of HTML.
35
36
  *
package/dist/jsx/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { Chunk } from "./chunk/index.js";
1
2
  import { merge } from "./merge-async-iterables.js";
2
3
  // https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
3
4
  const voidElements = new Set([
@@ -44,16 +45,16 @@ export async function* jsx(tag, props) {
44
45
  else if (typeof value === "string" ||
45
46
  typeof value === "number" ||
46
47
  typeof value === "bigint") {
47
- attrParts.push(` ${key}=${JSON.stringify(value)}`);
48
+ attrParts.push(` ${key}="${Chunk.escape(String(value), true)}"`);
48
49
  }
49
50
  // otherwise, don't include the attribute
50
51
  }
51
- yield `<${tag}${attrParts.join("")}>`;
52
+ yield new Chunk(`<${tag}${attrParts.join("")}>`, true);
52
53
  if (voidElements.has(tag))
53
54
  return;
54
55
  if (children)
55
56
  yield* toGenerator(children);
56
- yield `</${tag}>`;
57
+ yield new Chunk(`</${tag}>`, true);
57
58
  }
58
59
  }
59
60
  /**
@@ -67,7 +68,7 @@ export async function* Fragment(props = {}) {
67
68
  }
68
69
  /**
69
70
  * @param element
70
- * @yields Rendered strings of HTML as the `Element` resolves.
71
+ * @yields Chunks of HTML as the `Element` resolves.
71
72
  */
72
73
  export async function* toGenerator(element) {
73
74
  if (typeof element === "function")
@@ -76,12 +77,17 @@ export async function* toGenerator(element) {
76
77
  element = await element;
77
78
  if (element == null || typeof element === "boolean" || element === "")
78
79
  return;
80
+ if (element instanceof Chunk) {
81
+ yield element;
82
+ return;
83
+ }
79
84
  if (typeof element === "object") {
80
85
  if (Symbol.asyncIterator in element) {
81
86
  for await (const children of element)
82
87
  yield* toGenerator(children);
88
+ return;
83
89
  }
84
- else if (Symbol.iterator in element) {
90
+ if (Symbol.iterator in element) {
85
91
  const iterator = element[Symbol.iterator]();
86
92
  if (typeof iterator.next === "function" &&
87
93
  typeof iterator.throw === "function" &&
@@ -94,55 +100,53 @@ export async function* toGenerator(element) {
94
100
  break;
95
101
  yield* toGenerator(result.value);
96
102
  }
103
+ return;
97
104
  }
98
- else {
99
- // other iterable - array, set, etc.
100
- // process children in parallel
101
- const generators = [];
102
- while (true) {
103
- const result = iterator.next();
104
- if (result.done)
105
- break;
106
- generators.push(toGenerator(result.value));
107
- }
108
- let queue = new Array(generators.length).fill("");
109
- let current = 0;
110
- const complete = new Set();
111
- for await (const { index, result } of merge(generators)) {
112
- if (result.done) {
113
- complete.add(index);
114
- if (index === current) {
115
- while (++current < generators.length) {
116
- if (queue[current]) {
117
- // yield whatever is in the next queue even if it hasn't completed yet
118
- yield queue[current];
119
- queue[current] = "";
120
- }
121
- // if it hasn't completed, stop iterating to the next
122
- if (!complete.has(current))
123
- break;
105
+ // other iterable - array, set, etc.
106
+ // process children in parallel
107
+ const generators = [];
108
+ while (true) {
109
+ const result = iterator.next();
110
+ if (result.done)
111
+ break;
112
+ generators.push(toGenerator(result.value));
113
+ }
114
+ const queue = new Array(generators.length).fill(null);
115
+ let current = 0;
116
+ const complete = new Set();
117
+ for await (const { index, result: { done, value: chunk }, } of merge(generators)) {
118
+ if (done) {
119
+ complete.add(index);
120
+ if (index === current) {
121
+ while (++current < generators.length) {
122
+ if (queue[current]) {
123
+ // yield whatever is in the next queue even if it hasn't completed yet
124
+ yield queue[current];
125
+ queue[current] = null;
124
126
  }
127
+ // if it hasn't completed, stop iterating to the next
128
+ if (!complete.has(current))
129
+ break;
125
130
  }
126
131
  }
127
- else if (index === current) {
128
- yield result.value; // stream the current value directly
129
- }
130
- else {
131
- queue[index] += result.value; // queue the value for later
132
- }
133
132
  }
134
- queue = queue.join("");
135
- if (queue)
136
- yield queue; // clear the queue
133
+ else if (index === current) {
134
+ yield chunk; // stream the current value directly
135
+ }
136
+ else {
137
+ // queue the value for later
138
+ if (queue[index])
139
+ queue[index].concat(chunk);
140
+ else
141
+ queue[index] = chunk;
142
+ }
137
143
  }
144
+ // clear the queue
145
+ yield* queue.filter(Boolean);
146
+ return;
138
147
  }
139
- else {
140
- yield JSON.stringify(element); // avoids things like [object Object]
141
- }
142
- }
143
- else {
144
- yield String(element); // primitive
145
148
  }
149
+ yield new Chunk(element); // primitive or other object
146
150
  }
147
151
  /**
148
152
  * Converts a `JSX.Element` into a fully concatenated string of HTML.
@@ -156,8 +160,8 @@ export async function* toGenerator(element) {
156
160
  * @returns A promise that resolves to the concatenated HTML.
157
161
  */
158
162
  export const toString = async (element) => {
159
- const parts = [];
160
- for await (const value of toGenerator(element))
161
- parts.push(value);
162
- return parts.join("");
163
+ const chunks = [];
164
+ for await (const chunk of toGenerator(element))
165
+ chunks.push(chunk);
166
+ return chunks.join("");
163
167
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "Progressive HTML Rendering",
4
- "version": "2.1.0",
4
+ "version": "3.0.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -1,8 +0,0 @@
1
- /**
2
- * Escapes strings of HTML.
3
- *
4
- * @param value Converted to a string, then escaped.
5
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
6
- * @returns Escaped string of HTML
7
- */
8
- export declare const escape: (value: unknown, attr?: boolean) => string;
@@ -1,33 +0,0 @@
1
- const attrRegex = /[&"<]/g;
2
- const contentRegex = /[&<]/g;
3
- /**
4
- * Escapes strings of HTML.
5
- *
6
- * @param value Converted to a string, then escaped.
7
- * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
8
- * @returns Escaped string of HTML
9
- */
10
- export const escape = (value, attr) => {
11
- // copied and adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
12
- const s = String(value ?? "");
13
- const regex = attr ? attrRegex : contentRegex;
14
- // search starts at beginning of the string
15
- regex.lastIndex = 0;
16
- // tracks the position of the last successful match in the input string
17
- let start = 0;
18
- let result = "";
19
- // since `g` flag is used, regex maintains state with each loop,
20
- // checking from the lastIndex onward each time
21
- while (regex.test(s)) {
22
- // index of the match
23
- const i = regex.lastIndex - 1;
24
- const match = s[i];
25
- result +=
26
- // everything that didn't match during this test
27
- s.slice(start, i) +
28
- // replacement
29
- (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
30
- start = regex.lastIndex;
31
- }
32
- return result + s.slice(start);
33
- };
File without changes
File without changes
File without changes
File without changes
File without changes