ovr 4.2.2 → 4.2.4

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.
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/app/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,GAAG,EAAe,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEtD,OAAO,EAEN,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,eAAe,EACpB,KAAK,aAAa,EAClB,MAAM,YAAY,CAAC;AAGpB,KAAK,MAAM,GAAG,CAAC,KAAK,EAAE;IAAE,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAA;CAAE,KAAK,GAAG,CAAC,OAAO,CAAC;AAShE,qBAAa,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IAC7C,gFAAgF;IAChF,GAAG,EAAE,OAAO,CAAC;IAEb,4EAA4E;IAC5E,GAAG,EAAE,GAAG,CAAC;IAET;;;;;;;OAOG;IACH,MAAM,EAAG,CAAC,CAAC;IAEX,oCAAoC;IACpC,KAAK,EAAG,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE/B,+FAA+F;IAC/F,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAQ;IAE7B,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,gFAAgF;IAChF,OAAO,UAAiB;IAQxB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IAEb,8BAA8B;IAC9B,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IAEvB,iCAAiC;IACjC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;IAI7B,IAAI,mEAAkB;gBAKrB,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,GAAG,EACR,aAAa,EA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AE,MAAM,CAAC;QAEhB,gFAAgF;QAChF,OAAO,CAAC,EAAE,WAAW,CAAC;KACtB;IAYF;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM;IAO3C;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,MAAM;IAO/B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAOpC;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,EAAE,MAAM,GAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAS;IAK1E;;OAEG;IACH,IAAI,CAAC,GAAG,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE;IAa9B;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE;IAI1B;;;;;OAKG;IACH,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAmEvC;;;;;;;;OAQG;IACH,IAAI,CAAC,CAAC,EAAE,MAAM;IAed;;OAEG;IACH,KAAK;IAwCL;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,MAAM,CAAC,GAAG;CAUV"}
@@ -40,8 +40,12 @@ export class Context {
40
40
  error;
41
41
  /** Passed from `app.notFound` */
42
42
  notFound;
43
+ /** `Memo` unique to the current `Request` context */
43
44
  #memo = new Memo();
44
45
  memo = this.#memo.use;
46
+ /** Final `Response` set in `build` */
47
+ #response;
48
+ /** Used across requests */
45
49
  static #encoder = new TextEncoder();
46
50
  constructor(req, url, trailingSlash, base, error, notFound) {
47
51
  this.req = req;
@@ -122,6 +126,12 @@ export class Context {
122
126
  * @param Element `JSX.Element` to add to the head
123
127
  */
124
128
  head(...Element) {
129
+ if (this.#bodyUsed) {
130
+ throw new Error("Cannot call `head` after `Response.body` has been read. " +
131
+ "This can occur if `head` is called within a component since " +
132
+ "components are evaluated during the stream, often times after " +
133
+ "the <head> element has already been sent.");
134
+ }
125
135
  this.#headElements.push(...Element);
126
136
  }
127
137
  /**
@@ -171,25 +181,22 @@ export class Context {
171
181
  // HTML partial - just use the layouts + page
172
182
  gen = toGenerator(Page);
173
183
  }
174
- const close = (c) => {
175
- c?.close();
176
- gen.return();
177
- };
178
184
  let result;
179
185
  this.html(new ReadableStream({
180
186
  pull: async (c) => {
181
- if (this.req.signal.aborted)
182
- return close(c);
183
187
  result = await gen.next();
184
- if (result.done)
185
- return close(c);
188
+ if (result.done) {
189
+ c.close();
190
+ gen.return();
191
+ return;
192
+ }
186
193
  // need to encode for Node JS (ex: during prerendering)
187
194
  c.enqueue(Context.#encoder.encode(result.value.value));
188
195
  },
189
196
  cancel() {
190
- close();
197
+ gen.return();
191
198
  },
192
- }, { highWaterMark: 16_000, size: (chunk) => chunk.length }), status);
199
+ }), status);
193
200
  }
194
201
  /**
195
202
  * Generates an etag from a hash of the string provided.
@@ -235,7 +242,15 @@ export class Context {
235
242
  }
236
243
  if (!this.body && !this.status)
237
244
  this.notFound(this);
238
- return new Response(this.body, this);
245
+ return (this.#response = new Response(this.body, this));
246
+ }
247
+ /**
248
+ * Check if the `Response` has been set and the body has been read.
249
+ *
250
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/bodyUsed)
251
+ */
252
+ get #bodyUsed() {
253
+ return Boolean(this.#response?.bodyUsed);
239
254
  }
240
255
  /**
241
256
  * Call within the scope of a handler to get the current context.
@@ -5,10 +5,10 @@ export declare class Chunk {
5
5
  /**
6
6
  * `Chunk` to send in an HTML stream.
7
7
  *
8
- * @param s string of HTML to escape
8
+ * @param html string of HTML to escape
9
9
  * @param safe Set to `true` if the HTML is safe and should not be escaped
10
10
  */
11
- constructor(s: unknown, safe?: boolean);
11
+ constructor(html: unknown, safe?: boolean);
12
12
  /**
13
13
  * @returns `value`
14
14
  */
@@ -20,10 +20,10 @@ export declare class Chunk {
20
20
  /**
21
21
  * Escapes strings of HTML.
22
22
  *
23
- * @param s String to escape
23
+ * @param html String to escape
24
24
  * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
25
25
  * @returns Escaped string of HTML
26
26
  */
27
- static escape(s: string, attr?: boolean): string;
27
+ static escape(html: string, attr?: boolean): string;
28
28
  }
29
29
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKtC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA4BvC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/jsx/chunk/index.ts"],"names":[],"mappings":"AAAA,qBAAa,KAAK;;IAIjB,2BAA2B;IAC3B,KAAK,EAAE,MAAM,CAAC;IAEd;;;;;OAKG;gBACS,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,OAAO;IAKzC;;OAEG;IACH,QAAQ;IAIR;;OAEG;IACH,MAAM,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE;IAIzB;;;;;;OAMG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO;CA2B1C"}
@@ -6,11 +6,11 @@ export class Chunk {
6
6
  /**
7
7
  * `Chunk` to send in an HTML stream.
8
8
  *
9
- * @param s string of HTML to escape
9
+ * @param html string of HTML to escape
10
10
  * @param safe Set to `true` if the HTML is safe and should not be escaped
11
11
  */
12
- constructor(s, safe) {
13
- const value = String(s ?? "");
12
+ constructor(html, safe) {
13
+ const value = String(html ?? "");
14
14
  this.value = safe ? value : Chunk.escape(value);
15
15
  }
16
16
  /**
@@ -28,11 +28,11 @@ export class Chunk {
28
28
  /**
29
29
  * Escapes strings of HTML.
30
30
  *
31
- * @param s String to escape
31
+ * @param html String to escape
32
32
  * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
33
33
  * @returns Escaped string of HTML
34
34
  */
35
- static escape(s, attr) {
35
+ static escape(html, attr) {
36
36
  // adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
37
37
  const regex = attr ? Chunk.#attr : Chunk.#content;
38
38
  // search starts at beginning of the string
@@ -40,21 +40,19 @@ export class Chunk {
40
40
  // tracks the position of the last successful match in the input string
41
41
  let start = 0;
42
42
  let result = "";
43
- // index of the match
44
- let i;
45
- let match;
46
43
  // since `g` flag is used, regex maintains state with each loop,
47
44
  // checking from the lastIndex onward each time
48
- while (regex.test(s)) {
49
- i = regex.lastIndex - 1;
50
- match = s[i];
45
+ while (regex.test(html)) {
46
+ // index of the match
47
+ const i = regex.lastIndex - 1;
48
+ const match = html[i];
51
49
  result +=
52
50
  // everything that didn't match during this test
53
- s.slice(start, i) +
51
+ html.slice(start, i) +
54
52
  // replacement
55
53
  (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
56
54
  start = regex.lastIndex;
57
55
  }
58
- return result + s.slice(start);
56
+ return result + html.slice(start);
59
57
  }
60
58
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
3
  "description": "Progressive HTML Rendering",
4
- "version": "4.2.2",
4
+ "version": "4.2.4",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -64,10 +64,15 @@ export class Context<P extends Params = Params> {
64
64
  /** Passed from `app.notFound` */
65
65
  notFound: NotFoundHandler<P>;
66
66
 
67
+ /** `Memo` unique to the current `Request` context */
67
68
  #memo = new Memo();
68
69
 
69
70
  memo = this.#memo.use;
70
71
 
72
+ /** Final `Response` set in `build` */
73
+ #response: Response | undefined;
74
+
75
+ /** Used across requests */
71
76
  static #encoder = new TextEncoder();
72
77
 
73
78
  constructor(
@@ -176,6 +181,15 @@ export class Context<P extends Params = Params> {
176
181
  * @param Element `JSX.Element` to add to the head
177
182
  */
178
183
  head(...Element: JSX.Element[]) {
184
+ if (this.#bodyUsed) {
185
+ throw new Error(
186
+ "Cannot call `head` after `Response.body` has been read. " +
187
+ "This can occur if `head` is called within a component since " +
188
+ "components are evaluated during the stream, often times after " +
189
+ "the <head> element has already been sent.",
190
+ );
191
+ }
192
+
179
193
  this.#headElements.push(...Element);
180
194
  }
181
195
 
@@ -234,33 +248,27 @@ export class Context<P extends Params = Params> {
234
248
  gen = toGenerator(Page);
235
249
  }
236
250
 
237
- const close = (c?: ReadableStreamDefaultController) => {
238
- c?.close();
239
- gen.return();
240
- };
241
-
242
251
  let result: IteratorResult<Chunk>;
243
252
 
244
253
  this.html(
245
- new ReadableStream<Uint8Array>(
246
- {
247
- pull: async (c) => {
248
- if (this.req.signal.aborted) return close(c);
249
-
250
- result = await gen.next();
251
-
252
- if (result.done) return close(c);
253
-
254
- // need to encode for Node JS (ex: during prerendering)
255
- c.enqueue(Context.#encoder.encode(result.value.value));
256
- },
254
+ new ReadableStream<Uint8Array>({
255
+ pull: async (c) => {
256
+ result = await gen.next();
257
+
258
+ if (result.done) {
259
+ c.close();
260
+ gen.return();
261
+ return;
262
+ }
263
+
264
+ // need to encode for Node JS (ex: during prerendering)
265
+ c.enqueue(Context.#encoder.encode(result.value.value));
266
+ },
257
267
 
258
- cancel() {
259
- close();
260
- },
268
+ cancel() {
269
+ gen.return();
261
270
  },
262
- { highWaterMark: 16_000, size: (chunk) => chunk.length },
263
- ),
271
+ }),
264
272
  status,
265
273
  );
266
274
  }
@@ -320,7 +328,16 @@ export class Context<P extends Params = Params> {
320
328
 
321
329
  if (!this.body && !this.status) this.notFound(this);
322
330
 
323
- return new Response(this.body, this);
331
+ return (this.#response = new Response(this.body, this));
332
+ }
333
+
334
+ /**
335
+ * Check if the `Response` has been set and the body has been read.
336
+ *
337
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response/bodyUsed)
338
+ */
339
+ get #bodyUsed() {
340
+ return Boolean(this.#response?.bodyUsed);
324
341
  }
325
342
 
326
343
  /**
@@ -8,11 +8,11 @@ export class Chunk {
8
8
  /**
9
9
  * `Chunk` to send in an HTML stream.
10
10
  *
11
- * @param s string of HTML to escape
11
+ * @param html string of HTML to escape
12
12
  * @param safe Set to `true` if the HTML is safe and should not be escaped
13
13
  */
14
- constructor(s: unknown, safe?: boolean) {
15
- const value = String(s ?? "");
14
+ constructor(html: unknown, safe?: boolean) {
15
+ const value = String(html ?? "");
16
16
  this.value = safe ? value : Chunk.escape(value);
17
17
  }
18
18
 
@@ -33,11 +33,11 @@ export class Chunk {
33
33
  /**
34
34
  * Escapes strings of HTML.
35
35
  *
36
- * @param s String to escape
36
+ * @param html String to escape
37
37
  * @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
38
38
  * @returns Escaped string of HTML
39
39
  */
40
- static escape(s: string, attr?: boolean) {
40
+ static escape(html: string, attr?: boolean) {
41
41
  // adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
42
42
  const regex = attr ? Chunk.#attr : Chunk.#content;
43
43
 
@@ -46,23 +46,22 @@ export class Chunk {
46
46
  // tracks the position of the last successful match in the input string
47
47
  let start = 0;
48
48
  let result = "";
49
- // index of the match
50
- let i: number;
51
- let match: string | undefined;
52
49
 
53
50
  // since `g` flag is used, regex maintains state with each loop,
54
51
  // checking from the lastIndex onward each time
55
- while (regex.test(s)) {
56
- i = regex.lastIndex - 1;
57
- match = s[i];
52
+ while (regex.test(html)) {
53
+ // index of the match
54
+ const i = regex.lastIndex - 1;
55
+ const match = html[i];
56
+
58
57
  result +=
59
58
  // everything that didn't match during this test
60
- s.slice(start, i) +
59
+ html.slice(start, i) +
61
60
  // replacement
62
61
  (match === "&" ? "&amp;" : match === '"' ? "&quot;" : "&lt;");
63
62
  start = regex.lastIndex;
64
63
  }
65
64
 
66
- return result + s.slice(start);
65
+ return result + html.slice(start);
67
66
  }
68
67
  }