ovr 4.6.0 → 5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/app/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAe,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EACX,SAAS,EACT,kBAAkB,EAClB,aAAa,EACb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,CACnD,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EACnB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACrB,GAAG,CAAC;AAET,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;AAE1D,KAAK,MAAM,GACR,KAAK,GACL,MAAM,GACN,MAAM,GACN,KAAK,GACL,QAAQ,GACR,SAAS,GACT,SAAS,GACT,OAAO,GACP,OAAO,GACP,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAEjB,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,GAE3E,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;AAEpC,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAC/C,CAAC,CAAC,OAAO,EAAE,gBAAgB,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AAM,EACzB,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,IAAI;IACP;;;;OAIG;IACH,GAAG,CAAC,QAAQ,SAAS,MAAM,EAAE,EAC5B,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,EACvB,GAAG,UAAU,EAAE,UAAU,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,EAAE,GACvD,IAAI;IAQP;;;;OAIG;IACH,IAAI,CAAC,OAAO,SAAS,MAAM,EAC1B,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,IAAI;IACP;;;;OAIG;IACH,IAAI,CAAC,QAAQ,SAAS,MAAM,EAAE,EAC7B,QAAQ,EAAE,CAAC,GAAG,QAAQ,CAAC,EACvB,GAAG,UAAU,EAAE,UAAU,CAAC,kBAAkB,CAAC,QAAQ,CAAC,CAAC,EAAE,GACvD,IAAI;IAQP;;;;;;;;OAQG;IACH,KAAK,GACJ,UAAU,WAAW,GAAG,GAAG,EAC3B,UAAU,WAAW,KACnB,OAAO,CAAC,QAAQ,CAAC,CA6BlB;CAkCF"}
package/dist/app/index.js CHANGED
@@ -2,57 +2,25 @@ import { Route, Trie } from "../trie/index.js";
2
2
  import { Context } from "./context.js";
3
3
  import { Get } from "./helper/get.js";
4
4
  import { Post } from "./helper/post.js";
5
- import { AsyncLocalStorage } from "node:async_hooks";
6
5
  export class App {
6
+ constructor(options) {
7
+ const resolved = {
8
+ csrf: true,
9
+ trailingSlash: "never",
10
+ };
11
+ Object.assign(resolved, options);
12
+ if (resolved.csrf === true)
13
+ this.#use.push(App.#csrf);
14
+ if (resolved.trailingSlash !== "ignore") {
15
+ this.#use.push(App.#trailingSlash(resolved.trailingSlash));
16
+ }
17
+ }
7
18
  /** Built tries per HTTP method. */
8
19
  #trieMap = new Map();
9
20
  /** Added routes per HTTP method. */
10
21
  #routesMap = new Map();
11
22
  /** Global middleware. */
12
23
  #use = [];
13
- /**
14
- * - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
15
- * - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
16
- * - `"ignore"` - no redirects (not recommended, bad for SEO)
17
- *
18
- * [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
19
- *
20
- * @default "never"
21
- */
22
- trailingSlash = "never";
23
- /**
24
- * Base HTML to inject the `head` and `page` elements into.
25
- *
26
- * @default
27
- *
28
- * ```html
29
- * <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
30
- * ```
31
- */
32
- base = '<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>';
33
- /**
34
- * Assign a handler to run when an `Error` is thrown.
35
- *
36
- * If not set, `Error` will be thrown. This might be desired
37
- * if your server already includes error handling. Set in `config.start`
38
- * to handle errors globally.
39
- *
40
- * @default null
41
- */
42
- error = null;
43
- /**
44
- * Middleware to run when no `body` or `status` has been set on the `context`.
45
- * Set to a new function to override the default.
46
- *
47
- * @default
48
- *
49
- * ```ts
50
- * (c) => c.html("Not found", 404)
51
- * ```
52
- */
53
- notFound = (c) => c.html("Not found", 404);
54
- /** Stores context per request. */
55
- static storage = new AsyncLocalStorage();
56
24
  /**
57
25
  * @param helpers `Helper` to add to `App`
58
26
  * @returns `App` instance
@@ -111,58 +79,64 @@ export class App {
111
79
  return this.on("POST", patternOrPatterns, ...middleware);
112
80
  }
113
81
  /**
114
- * @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
115
- * @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
82
+ * Request a resource from the application.
83
+ *
84
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch)
85
+ *
86
+ * @param resource [Resource](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#resource) to fetch
87
+ * @param options [Options](https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch#options) to apply to the request
88
+ * @returns Promise that resolves to the [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) to the request
116
89
  */
117
- fetch = (req) => {
118
- const c = new Context(req, new URL(req.url), this.trailingSlash, this.base, this.error, this.notFound);
119
- return App.storage.run(c, async () => {
120
- try {
121
- // check to see if the method is already built
122
- let trie = this.#trieMap.get(req.method);
123
- if (!trie) {
124
- // check if there are any routes with the method
125
- const routes = this.#routesMap.get(req.method);
126
- if (routes) {
127
- // build trie
128
- trie = new Trie();
129
- for (const route of routes)
130
- trie.add(route);
131
- this.#trieMap.set(req.method, trie);
132
- }
133
- }
134
- if (trie) {
135
- const match = trie.find(c.url.pathname);
136
- if (match) {
137
- Object.assign(c, match);
138
- const middleware = [...this.#use, ...match.route.store];
139
- // compose
140
- let i = -1;
141
- const dispatch = async (current) => {
142
- if (current <= i)
143
- throw new Error("next() called multiple times");
144
- i = current;
145
- if (middleware[current]) {
146
- const result = await middleware[current](c, () => dispatch(current + 1));
147
- if (result instanceof Response)
148
- c.res(result.body, result);
149
- else if (result instanceof ReadableStream)
150
- c.body = result;
151
- else if (result != null)
152
- c.page(result);
153
- }
154
- };
155
- await dispatch(0);
156
- }
157
- }
90
+ fetch = async (resource, options) => {
91
+ const c = new Context(new Request(resource, options));
92
+ // see if the method is already built
93
+ let trie = this.#trieMap.get(c.req.method);
94
+ if (!trie) {
95
+ // check if there are any routes with the method
96
+ const routes = this.#routesMap.get(c.req.method);
97
+ if (routes) {
98
+ // build trie
99
+ trie = new Trie();
100
+ for (const route of routes)
101
+ trie.add(route);
102
+ this.#trieMap.set(c.req.method, trie);
158
103
  }
159
- catch (error) {
160
- if (c.error)
161
- c.error(c, error);
162
- else
163
- throw error;
104
+ }
105
+ if (trie) {
106
+ const match = trie.find(c.url.pathname);
107
+ if (match) {
108
+ Object.assign(c, match);
109
+ return c.build(this.#use.concat(match.route.store));
164
110
  }
165
- return c.build();
166
- });
111
+ }
112
+ // no match, just run global middleware
113
+ return c.build(this.#use);
167
114
  };
115
+ /** Basic CSRF middleware. */
116
+ static async #csrf(c, next) {
117
+ if (c.req.method === "GET" ||
118
+ c.req.method === "HEAD" ||
119
+ c.req.headers.get("sec-fetch-site") === "same-origin" ||
120
+ c.req.headers.get("origin") === c.url.origin) {
121
+ return next();
122
+ }
123
+ c.text("Forbidden", 403);
124
+ }
125
+ /** @returns Trailing slash middleware */
126
+ static #trailingSlash(mode) {
127
+ return async (c, next) => {
128
+ await next();
129
+ if (c.status && c.status !== 404)
130
+ return;
131
+ const last = c.url.pathname.at(-1);
132
+ if (mode === "always" && last !== "/") {
133
+ c.url.pathname += "/";
134
+ c.redirect(c.url, 308);
135
+ }
136
+ else if (mode === "never" && c.url.pathname !== "/" && last === "/") {
137
+ c.url.pathname = c.url.pathname.slice(0, -1);
138
+ c.redirect(c.url, 308);
139
+ }
140
+ };
141
+ }
168
142
  }
@@ -7,6 +7,6 @@ export declare class Memo {
7
7
  * @param fn Function to memoize.
8
8
  * @returns The memoized function.
9
9
  */
10
- use: <A extends any[], R>(fn: (...args: A) => R) => (...args: A) => R;
10
+ use<A extends any[], R>(fn: (...args: A) => R): (...args: A) => R;
11
11
  }
12
12
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/memo/index.ts"],"names":[],"mappings":"AAAA,qBAAa,IAAI;;IAOhB;;;;;;OAMG;IACH,GAAG,GAAI,CAAC,SAAS,GAAG,EAAE,EAAE,CAAC,EAAE,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,eAAP,CAAC,KAAK,CAAC,CAwB9C;CACF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/app/memo/index.ts"],"names":[],"mappings":"AAAA,qBAAa,IAAI;;IAOhB;;;;;;OAMG;IACH,GAAG,CAAC,CAAC,SAAS,GAAG,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC,aAAP,CAAC,KAAK,CAAC;CAyB7C"}
@@ -10,7 +10,7 @@ export class Memo {
10
10
  * @param fn Function to memoize.
11
11
  * @returns The memoized function.
12
12
  */
13
- use = (fn) => {
13
+ use(fn) {
14
14
  if (this.#memoized.has(fn))
15
15
  return this.#memoized.get(fn);
16
16
  // closure will GC automatically
@@ -30,5 +30,5 @@ export class Memo {
30
30
  };
31
31
  this.#memoized.set(fn, memo);
32
32
  return memo;
33
- };
33
+ }
34
34
  }
package/dist/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  export { jsx, toGenerator, toStream, toString, type JSX } from "./jsx/index.js";
2
2
  export { Chunk } from "./jsx/chunk/index.js";
3
3
  export { Trie, Route } from "./trie/index.js";
4
- export { App, type Middleware } from "./app/index.js";
4
+ export { App, type Middleware, type Next } from "./app/index.js";
5
5
  export { Context } from "./app/context.js";
6
6
  export { Get } from "./app/helper/get.js";
7
7
  export { Post } from "./app/helper/post.js";
8
- export { csrf } from "./app/mw/csrf.js";
9
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAChF,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,KAAK,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -5,4 +5,3 @@ export { App } from "./app/index.js";
5
5
  export { Context } from "./app/context.js";
6
6
  export { Get } from "./app/helper/get.js";
7
7
  export { Post } from "./app/helper/post.js";
8
- export { csrf } from "./app/mw/csrf.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/jsx/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,KAAK,EAAE,iBAAiB,IAAI,EAAE,EAAE,MAAM,yBAAyB,CAAC;AAMvE,wBAAwB;AACxB,yBAAiB,GAAG,CAAC;IACpB,6BAA6B;IAC7B,UAAiB,iBAAkB,SAAQ,EAAE;KAAG;IAEhD,kBAAkB;IAClB,KAAY,OAAO,GAAG,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IAC7B,UAAiB,iBAAkB,SAAQ,EAAE;KAAG;IAEhD,kBAAkB;IAClB,KAAY,OAAO,GAAG,aAAa,CAClC,YAAY,CACT,MAAM,GACN,MAAM,GACN,MAAM,GACN,OAAO,GACP,MAAM,GACN,IAAI,GACJ,SAAS,GACT,MAAM,GACN,QAAQ,CAAC,OAAO,CAAC,GACjB,aAAa,CAAC,OAAO,CAAC,CACxB,CACD,CAAC;CACF;AAED,8BAA8B;AAC9B,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;AAuBhD;;;;;;;;GAQG;AACH,wBAAuB,GAAG,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,EACjD,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,MAAM,EACzC,KAAK,EAAE,CAAC,wCAsCR;AAED;;;;;GAKG;AACH,wBAAuB,QAAQ,CAAC,KAAK,GAAE;IAAE,QAAQ,CAAC,EAAE,GAAG,CAAC,OAAO,CAAA;CAAO,wCAErE;AAED;;;GAGG;AACH,wBAAuB,WAAW,CACjC,OAAO,EAAE,GAAG,CAAC,OAAO,GAClB,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,OAAO,CAAC,CAyEtC;AAED;;;;;;;;;;GAUG;AACH,eAAO,MAAM,QAAQ,GAAU,SAAS,GAAG,CAAC,OAAO,oBACI,CAAC;AAKxD;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,SAAS,GAAG,CAAC,OAAO,gDAwC5C,CAAC"}
package/dist/jsx/index.js CHANGED
@@ -1,8 +1,5 @@
1
1
  import { Chunk } from "./chunk/index.js";
2
2
  import { merge } from "./merge-async-generators.js";
3
- import { performance } from "node:perf_hooks";
4
- import { setImmediate } from "node:timers/promises";
5
- import { types } from "node:util";
6
3
  /**
7
4
  * These are the HTML tags that do not require a closing tag.
8
5
  *
@@ -58,6 +55,8 @@ export async function* jsx(tag, props) {
58
55
  }
59
56
  // otherwise, don't include the attribute
60
57
  }
58
+ if (tag === "html")
59
+ yield Chunk.safe("<!doctype html>");
61
60
  yield Chunk.safe(`<${tag}${attributes}>`);
62
61
  if (voidElements.has(tag))
63
62
  return;
@@ -103,19 +102,10 @@ export async function* toGenerator(element) {
103
102
  }
104
103
  if (Symbol.iterator in element) {
105
104
  // sync iterable
106
- if (types.isGeneratorObject(element)) {
105
+ if ("next" in element) {
107
106
  // sync generator - lazily resolve, avoids loading all in memory
108
- const ms = 8;
109
- let deadline = performance.now() + ms;
110
- for (const children of element) {
107
+ for (const children of element)
111
108
  yield* toGenerator(children);
112
- if (performance.now() > deadline) {
113
- // yields back to the event loop if deadline is reached
114
- // to send chunks or check if the request has been cancelled
115
- await setImmediate();
116
- deadline = performance.now() + ms;
117
- }
118
- }
119
109
  return;
120
110
  }
121
111
  // other iterable - array, set, etc.
@@ -184,6 +174,10 @@ const encoder = new TextEncoder();
184
174
  export const toStream = (element) => {
185
175
  const gen = toGenerator(element);
186
176
  return new ReadableStream({
177
+ // enables zero-copy transfer from underlying source when queue is empty
178
+ // https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_byte_streams#overview
179
+ type: "bytes",
180
+ // `pull` ensures backpressure and cancelled requests stop the generator
187
181
  async pull(c) {
188
182
  const result = await gen.next();
189
183
  if (result.done) {
@@ -192,7 +186,7 @@ export const toStream = (element) => {
192
186
  return;
193
187
  }
194
188
  c.enqueue(
195
- // need to encode for Node JS (ex: during prerendering) or it will error
189
+ // need to encode for Node (ex: during prerendering) or it will error
196
190
  // doesn't seem to be needed for browsers
197
191
  // faster than piping through a `TextEncoderStream`
198
192
  encoder.encode(result.value.value));
@@ -200,5 +194,14 @@ export const toStream = (element) => {
200
194
  cancel() {
201
195
  gen.return();
202
196
  },
197
+ }, {
198
+ // `highWaterMark` defaults to 1
199
+ // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream/ReadableStream#highwatermark
200
+ // setting this ensures at least a small buffer is maintained if the
201
+ // underlying server does not have its own high water mark set
202
+ // https://blog.cloudflare.com/unpacking-cloudflare-workers-cpu-performance-benchmarks/#inefficient-streams-adapters
203
+ // in Node, the default is 16kb, so this stacks another 2kb in front
204
+ // https://nodejs.org/api/http.html#outgoingmessagewritablehighwatermark
205
+ highWaterMark: 2048,
203
206
  });
204
207
  };
@@ -4,6 +4,10 @@ export declare class Route<Store> {
4
4
  pattern: string;
5
5
  /** Store returned when route is found */
6
6
  store: Store;
7
+ /**
8
+ * @param pattern route pattern
9
+ * @param store value to store in the patterns final node
10
+ */
7
11
  constructor(pattern: string, store: Store);
8
12
  }
9
13
  declare class ParamNode<Store> {
@@ -16,6 +20,7 @@ declare class ParamNode<Store> {
16
20
  constructor(name: string);
17
21
  }
18
22
  export declare class Trie<Store> {
23
+ #private;
19
24
  /** Unique segment of the pattern trie */
20
25
  segment: string;
21
26
  /** Static child node map, key is the first character in the segment */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/trie/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAE5C,qBAAa,KAAK,CAAC,KAAK;IACvB,oBAAoB;IACpB,OAAO,EAAE,MAAM,CAAC;IAEhB,yCAAyC;IACzC,KAAK,EAAE,KAAK,CAAC;gBAED,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;CAUzC;AAED,cAAM,SAAS,CAAC,KAAK;IACpB,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAC;IAEb,oBAAoB;IACpB,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;IAElC,wBAAwB;IACxB,WAAW,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAQ;gBAE3B,IAAI,EAAE,MAAM;CAGxB;AAED,qBAAa,IAAI,CAAC,KAAK;IACtB,yCAAyC;IACzC,O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,IAAI;CAgFtE"}
@@ -3,6 +3,10 @@ export class Route {
3
3
  pattern;
4
4
  /** Store returned when route is found */
5
5
  store;
6
+ /**
7
+ * @param pattern route pattern
8
+ * @param store value to store in the patterns final node
9
+ */
6
10
  constructor(pattern, store) {
7
11
  if (pattern[0] !== "/") {
8
12
  throw new Error(`Invalid route: ${pattern} - route pattern must begin with "/"`);
@@ -33,6 +37,8 @@ export class Trie {
33
37
  route = null;
34
38
  /** Matched wildcard route */
35
39
  wildcardRoute = null;
40
+ static #paramMatch = /:.+?(?=\/|$)/g;
41
+ static #paramSplit = /:.+?(?=\/|$)/;
36
42
  /**
37
43
  * @param segment pattern segment
38
44
  * @param staticChildren static children nodes to add to staticMap
@@ -107,8 +113,8 @@ export class Trie {
107
113
  const endsWithWildcard = pattern.endsWith("*");
108
114
  if (endsWithWildcard)
109
115
  pattern = pattern.slice(0, -1);
110
- const paramSegments = pattern.match(/:.+?(?=\/|$)/g) ?? []; // match the params
111
- const staticSegments = pattern.split(/:.+?(?=\/|$)/); // split on the params
116
+ const paramSegments = pattern.match(Trie.#paramMatch) ?? [];
117
+ const staticSegments = pattern.split(Trie.#paramSplit);
112
118
  // if the last segment is a param without a trailing slash
113
119
  // then there will be an empty string, remove
114
120
  if (staticSegments.at(-1) === "")
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ovr",
3
- "description": "Progressive HTML Rendering",
4
- "version": "4.6.0",
3
+ "description": "The Streaming Framework",
4
+ "version": "5.0.0",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",