ovr 0.0.2 → 0.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.
- package/README.md +305 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/jsx/index.d.ts +4 -2
- package/dist/jsx/index.js +4 -2
- package/dist/router/context.d.ts +2 -3
- package/dist/router/index.d.ts +1 -2
- package/dist/router/index.js +2 -2
- package/dist/trie/index.d.ts +6 -6
- package/dist/trie/index.js +7 -7
- package/package.json +2 -3
- package/dist/types.d.ts +0 -1
- package/dist/types.js +0 -1
- /package/dist/{util → jsx}/merge-async-iterables.d.ts +0 -0
- /package/dist/{util → jsx}/merge-async-iterables.js +0 -0
package/README.md
CHANGED
|
@@ -1 +1,306 @@
|
|
|
1
1
|
# ovr
|
|
2
|
+
|
|
3
|
+
**ovr** is a lightweight toolkit for building fast, streaming web applications using asynchronous JSX and a modern Fetch API-based router.
|
|
4
|
+
|
|
5
|
+
It's designed for server-side rendering (SSR) where performance and Time-To-First-Byte (TTFB) matter. ovr evaluates components concurrently but streams the resulting HTML **in order**, allowing browsers to render content progressively as it arrives.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **Asynchronous Streaming JSX**: Write components that perform async operations (like data fetching) directly. ovr handles concurrent evaluation and ordered streaming output.
|
|
10
|
+
- **Performance Focused**: Deliver HTML faster to the client by streaming content as it becomes ready, improving performance and TTFB.
|
|
11
|
+
- **Fetch API Router**: A modern, flexible router built on the standard Fetch API `Request` and `Response` objects.
|
|
12
|
+
- **Minimal & Platform Agnostic**: Uses standard JavaScript/Web APIs, allowing it to run in Node.js, Deno, Bun, Cloudflare Workers, browsers, and other environments.
|
|
13
|
+
- **[Trie](https://en.wikipedia.org/wiki/Radix_tree)-Based Routing**: Efficient and fast route matching, supporting static paths, parameters, and wildcards with clear prioritization. Performance does not degrade as you add more routes.
|
|
14
|
+
|
|
15
|
+
## Table of Contents
|
|
16
|
+
|
|
17
|
+
- [JSX](#jsx)
|
|
18
|
+
- [Router](#router)
|
|
19
|
+
- [Trie](#trie)
|
|
20
|
+
|
|
21
|
+
## JSX
|
|
22
|
+
|
|
23
|
+
ovr provides an asynchronous JSX runtime designed for server-side rendering. Instead of building the entire HTML string in memory, it produces an `AsyncGenerator` that yields HTML chunks.
|
|
24
|
+
|
|
25
|
+
When you render multiple asynchronous components (e.g., components fetching data), ovr initiates their evaluation concurrently. As each component resolves, its corresponding HTML is generated.
|
|
26
|
+
|
|
27
|
+
Crucially, ovr ensures that these HTML chunks are yielded in the original source order, even if components finish evaluating out of order. This allows the browser to start parsing and rendering the initial parts of your page while waiting for slower data fetches further down, significantly improving perceived load times.
|
|
28
|
+
|
|
29
|
+
For example, ovr will immediately send the `<head>` of your document for the browser to start requesting the linked assets. Then the rest of the page streams in as it becomes available.
|
|
30
|
+
|
|
31
|
+
> [!WARNING]
|
|
32
|
+
>
|
|
33
|
+
> ovr does not escape HTML automatically, use the `escape` function provided.
|
|
34
|
+
|
|
35
|
+
### Configuration
|
|
36
|
+
|
|
37
|
+
Add the following to your `tsconfig.json` to enable the JSX transform,
|
|
38
|
+
|
|
39
|
+
```json
|
|
40
|
+
{
|
|
41
|
+
"jsx": "react-jsx",
|
|
42
|
+
"jsxImportSource": "ovr"
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
or use JSDoc comments within a module.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
/** @jsx jsx */
|
|
50
|
+
/** @jsxImportSource ovr */
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Usage
|
|
54
|
+
|
|
55
|
+
JSX evaluates to an `AsyncGenerator`, with this, the `Router` creates an in-order stream of components.
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
// Basic component with props
|
|
59
|
+
const Component = (props: { foo: string }) => <div>{props.foo}</div>;
|
|
60
|
+
|
|
61
|
+
// Components can be asynchronous, for example you can fetch directly in a component
|
|
62
|
+
const Data = async () => {
|
|
63
|
+
const res = await fetch("...");
|
|
64
|
+
const data = await res.json();
|
|
65
|
+
|
|
66
|
+
return <div>{JSON.stringify(data)}</div>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Components can also be generators, `yield` values instead of `return`
|
|
70
|
+
async function* Generator() {
|
|
71
|
+
yield <p>start</p>; // streamed immediately
|
|
72
|
+
await promise;
|
|
73
|
+
yield <p>after</p>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const Page = () => {
|
|
77
|
+
return (
|
|
78
|
+
<div>
|
|
79
|
+
<Component foo="bar" />
|
|
80
|
+
|
|
81
|
+
{/* These three components await in parallel when this component is called. */}
|
|
82
|
+
{/* Then they will stream in order as soon as they are ready. */}
|
|
83
|
+
<Generator />
|
|
84
|
+
<Data />
|
|
85
|
+
<Data />
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
You can `return` or `yield` most data types from a component, they will be rendered as you might expect.
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
function* DataTypes() {
|
|
95
|
+
yield null; // ""
|
|
96
|
+
yield undefined; // ""
|
|
97
|
+
yield false; // ""
|
|
98
|
+
yield "string"; // "string"
|
|
99
|
+
yield 0; // "0";
|
|
100
|
+
yield BigInt(9007199254740991); // "9007199254740991"
|
|
101
|
+
yield true; // "true"
|
|
102
|
+
yield { foo: "bar" }; // '{ "foo": "bar" }'
|
|
103
|
+
yield <p>jsx</p>; // "<p>jsx</p>"
|
|
104
|
+
yield ["any-", "iterable", 1, null]; // "any-iterable1"
|
|
105
|
+
yield () => "function"; // "function"
|
|
106
|
+
yield async () => "async"; // "async"
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Router
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import { Router } from "ovr";
|
|
114
|
+
|
|
115
|
+
const router = new Router();
|
|
116
|
+
|
|
117
|
+
router.get("/", (c) => c.text("Hello world"));
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Configuration
|
|
121
|
+
|
|
122
|
+
Optional configuration when creating the router.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
const router = new Router({
|
|
126
|
+
// redirect trailing slash preference
|
|
127
|
+
trailingSlash: "always",
|
|
128
|
+
|
|
129
|
+
// runs at the start of each request
|
|
130
|
+
start(c) {
|
|
131
|
+
// customize the not found response
|
|
132
|
+
c.notFound = (c) => c.res("custom", { status: 404 });
|
|
133
|
+
|
|
134
|
+
// add a global error handler
|
|
135
|
+
c.error = (c, error) => c.res(error.message, { status: 500 });
|
|
136
|
+
|
|
137
|
+
// base HTML to inject head and body elements into, this is the default
|
|
138
|
+
c.base =
|
|
139
|
+
'<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>';
|
|
140
|
+
// (these can be set in middleware as well)
|
|
141
|
+
|
|
142
|
+
// return state to use in middleware
|
|
143
|
+
return { foo: "bar" };
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Context
|
|
149
|
+
|
|
150
|
+
`Context` contains context for the current request.
|
|
151
|
+
|
|
152
|
+
```tsx
|
|
153
|
+
router.get("/api/:id", (c) => {
|
|
154
|
+
// Request Info
|
|
155
|
+
c.req; // The original Request object
|
|
156
|
+
c.url; // The parsed URL object
|
|
157
|
+
c.params; // Type-safe route parameters (e.g., { id: "123" })
|
|
158
|
+
c.route; // The matched Route object (contains pattern, store)
|
|
159
|
+
c.state; // State returned from `config.start` (e.g., { dbClient, user })
|
|
160
|
+
|
|
161
|
+
// Response Building Methods
|
|
162
|
+
c.res(body, init); // Generic response (like `new Response()`)
|
|
163
|
+
c.html(body, status); // Set HTML response
|
|
164
|
+
c.text(body, status); // Set plain text response
|
|
165
|
+
c.json(data, status); // Set JSON response
|
|
166
|
+
c.redirect(location, status); // Set redirect response
|
|
167
|
+
|
|
168
|
+
// JSX Page Building Methods (Leverages Streaming JSX)
|
|
169
|
+
c.head(<meta name="description" content="..." />); // Add elements to <head>
|
|
170
|
+
c.layout(MainLayout); // Wrap page content with layout components
|
|
171
|
+
c.page(<UserProfilePage userId={c.params.id} />); // Render JSX page, streaming enabled!
|
|
172
|
+
|
|
173
|
+
// Other Utilities
|
|
174
|
+
c.etag("content-to-hash"); // Generate and check ETag for caching
|
|
175
|
+
c.build(); // (Internal) Builds the final Response object
|
|
176
|
+
});
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Examples
|
|
180
|
+
|
|
181
|
+
#### Overview
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
// Basic
|
|
185
|
+
router.get("/", (c) => c.text("Hello world"));
|
|
186
|
+
|
|
187
|
+
// Params
|
|
188
|
+
router.post("/api/:id", (c) => {
|
|
189
|
+
// matches "/api/123"
|
|
190
|
+
c.params; // { id: "123" }
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// Wildcard - add an asterisk `*` to match all remaining segments in the route
|
|
194
|
+
router.get("/files/*", (c) => {
|
|
195
|
+
// c.params["*"] contains the matched wildcard path (e.g., "images/logo.png")
|
|
196
|
+
return c.text(`Serving file: ${c.params["*"]}`);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// Other or custom methods
|
|
200
|
+
router.on("METHOD", "/pattern", () => {
|
|
201
|
+
// ...
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
#### Middleware
|
|
206
|
+
|
|
207
|
+
Add middleware to a route, the first middleware added to the route will be called, and the `next` middleware can be called within the first by using `await next()`. Middleware is based on [koa-compose](https://github.com/koajs/compose).
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
router.get(
|
|
211
|
+
"/multi",
|
|
212
|
+
async (c, next) => {
|
|
213
|
+
// middleware
|
|
214
|
+
console.log("pre"); // 1
|
|
215
|
+
await next(); // calls the next middleware below
|
|
216
|
+
console.log("post"); // 3
|
|
217
|
+
},
|
|
218
|
+
() => console.log("final"); // 2
|
|
219
|
+
);
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`Context` is passed between between each middleware that is stored in the matched `Route`. After all the handlers have been run, the `Context` will `build` and return the final response.
|
|
223
|
+
|
|
224
|
+
#### Multiple patterns
|
|
225
|
+
|
|
226
|
+
Apply handlers to multiple patterns at once with type safe parameters.
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
router.get(["/multi/:param", "/pattern/:another"], (c) => {
|
|
230
|
+
c.param; // { param: string } | { another: string }
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### fetch
|
|
235
|
+
|
|
236
|
+
Use the `fetch` method to create a response,
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
const res = await router.fetch(new Request("https://example.com/"));
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
or use in a framework.
|
|
243
|
+
|
|
244
|
+
```ts
|
|
245
|
+
// next, sveltekit, astro...
|
|
246
|
+
export const GET = router.fetch;
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
// bun, deno, cloudflare...
|
|
251
|
+
export default router;
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### mount
|
|
255
|
+
|
|
256
|
+
Mount routers onto another with a base pattern.
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
const app = new Router();
|
|
260
|
+
|
|
261
|
+
const hello = new Router();
|
|
262
|
+
|
|
263
|
+
hello.get("/world", (c) => c.text("hello world"));
|
|
264
|
+
|
|
265
|
+
app.mount("/hello", hello); // creates route at "/hello/world"
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Trie
|
|
269
|
+
|
|
270
|
+
[Router](#router) is built using the `Trie` and `Route` classes. You can build your own trie based router by importing them.
|
|
271
|
+
|
|
272
|
+
The trie is forked and adapted from [memoirist](https://github.com/SaltyAom/memoirist) and [@medley/router](https://github.com/medleyjs/router).
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
import { Trie, Route } from "ovr";
|
|
276
|
+
|
|
277
|
+
// specify the type of the store in the generic
|
|
278
|
+
const trie = new Trie<string>();
|
|
279
|
+
const route = new Route("/hello/:name", "store");
|
|
280
|
+
|
|
281
|
+
trie.add(route);
|
|
282
|
+
|
|
283
|
+
const match = trie.find("/hello/world"); // { route, params: { name: "world" } }
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Prioritization
|
|
287
|
+
|
|
288
|
+
The trie prioritizes matches in this order: **Static > Parametric > Wildcard**.
|
|
289
|
+
|
|
290
|
+
Given three routes are added in any order,
|
|
291
|
+
|
|
292
|
+
```ts
|
|
293
|
+
trie.add(new Route("/hello/world", "store"));
|
|
294
|
+
trie.add(new Route("/hello/:name", "store"));
|
|
295
|
+
trie.add(new Route("/hello/*", "store"));
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
The following pathnames would match the corresponding patterns.
|
|
299
|
+
|
|
300
|
+
| pathname | Route.pattern |
|
|
301
|
+
| --------------------- | ---------------- |
|
|
302
|
+
| `"/hello/world"` | `"/hello/world"` |
|
|
303
|
+
| `"/hello/john"` | `"/hello/:name"` |
|
|
304
|
+
| `"/hello/john/smith"` | `"/hello/*"` |
|
|
305
|
+
|
|
306
|
+
More specific matches are prioritized. First, the static match is found, then the parametric, and finally the wildcard.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Context } from "./router/context.js";
|
|
2
2
|
export { Router, type Middleware, type Params } from "./router/index.js";
|
|
3
|
-
export {
|
|
3
|
+
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { escape } from "./escape/index.js";
|
|
5
5
|
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, type FC, type JSX, } from "./jsx/index.js";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Context } from "./router/context.js";
|
|
2
2
|
export { Router } from "./router/index.js";
|
|
3
|
-
export {
|
|
3
|
+
export { Trie, Route } from "./trie/index.js";
|
|
4
4
|
export { escape } from "./escape/index.js";
|
|
5
5
|
export { jsx, jsx as jsxs, jsx as jsxDEV, Fragment, toGenerator, toString, } from "./jsx/index.js";
|
package/dist/jsx/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { MaybePromise } from "../types.js";
|
|
2
1
|
import type { Elements } from "./elements.js";
|
|
2
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
3
3
|
type MaybeFunction<T> = T | (() => T);
|
|
4
4
|
export declare namespace JSX {
|
|
5
5
|
type IntrinsicElements = Elements;
|
|
@@ -37,7 +37,9 @@ export declare function toGenerator(element: JSX.Element): AsyncGenerator<string
|
|
|
37
37
|
/**
|
|
38
38
|
* Converts a `JSX.Element` into its fully concatenated string representation.
|
|
39
39
|
*
|
|
40
|
-
* WARNING
|
|
40
|
+
* > [!WARNING]
|
|
41
|
+
* >
|
|
42
|
+
* > This negates streaming benefits and buffers the result into a string.
|
|
41
43
|
*
|
|
42
44
|
* @param element
|
|
43
45
|
* @returns A promise that resolves to the concatenated string.
|
package/dist/jsx/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mergeAsyncIterables } from "
|
|
1
|
+
import { mergeAsyncIterables } from "./merge-async-iterables.js";
|
|
2
2
|
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
|
|
3
3
|
const voidElements = new Set([
|
|
4
4
|
"area",
|
|
@@ -125,7 +125,9 @@ export async function* toGenerator(element) {
|
|
|
125
125
|
/**
|
|
126
126
|
* Converts a `JSX.Element` into its fully concatenated string representation.
|
|
127
127
|
*
|
|
128
|
-
* WARNING
|
|
128
|
+
* > [!WARNING]
|
|
129
|
+
* >
|
|
130
|
+
* > This negates streaming benefits and buffers the result into a string.
|
|
129
131
|
*
|
|
130
132
|
* @param element
|
|
131
133
|
* @returns A promise that resolves to the concatenated string.
|
package/dist/router/context.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { type JSX } from "../jsx/index.js";
|
|
2
2
|
import type { Route } from "../trie/index.js";
|
|
3
|
-
import type { MaybePromise } from "../types.js";
|
|
4
3
|
import type { Middleware, Params, TrailingSlash, UnmatchedContext } from "./index.js";
|
|
5
4
|
type Layout = (props: {
|
|
6
5
|
children: JSX.Element;
|
|
@@ -53,7 +52,7 @@ export declare class Context<State, P extends Params> {
|
|
|
53
52
|
*
|
|
54
53
|
* @default null
|
|
55
54
|
*/
|
|
56
|
-
error: ((context: UnmatchedContext<State, Params>, error: unknown) =>
|
|
55
|
+
error: ((context: UnmatchedContext<State, Params>, error: unknown) => any) | null;
|
|
57
56
|
constructor(req: Request, url: URL, trailingSlash: TrailingSlash);
|
|
58
57
|
/**
|
|
59
58
|
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
@@ -65,7 +64,7 @@ export declare class Context<State, P extends Params> {
|
|
|
65
64
|
* () => this.html("Not found", 404)
|
|
66
65
|
* ```
|
|
67
66
|
*/
|
|
68
|
-
notFound(_context: UnmatchedContext<State, P>):
|
|
67
|
+
notFound(_context: UnmatchedContext<State, P>): any;
|
|
69
68
|
/**
|
|
70
69
|
* Mirrors `new Response()` constructor, set values with one function.
|
|
71
70
|
*
|
package/dist/router/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import type { MaybePromise } from "../types.js";
|
|
2
1
|
import { Context } from "./context.js";
|
|
3
2
|
export type Params = Record<string, string>;
|
|
4
3
|
export type UnmatchedContext<State, P extends Params> = Omit<Context<State, P>, "route"> & Partial<Pick<Context<State, P>, "route">>;
|
|
5
4
|
export type Start<State> = (context: Omit<UnmatchedContext<any, Params>, "state" | "route" | "params">) => State;
|
|
6
|
-
export type Middleware<State = null, P extends Params = Params> = (context: Context<State, P>, next: () => Promise<void>) =>
|
|
5
|
+
export type Middleware<State = null, P extends Params = Params> = (context: Context<State, P>, next: () => Promise<void>) => any;
|
|
7
6
|
export type TrailingSlash = "always" | "never" | "ignore";
|
|
8
7
|
type Method = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH" | (string & {});
|
|
9
8
|
type ExtractParams<Pattern extends string = string> = Pattern extends `${infer _Start}:${infer Param}/${infer Rest}` ? {
|
package/dist/router/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Trie, Route } from "../trie/index.js";
|
|
2
2
|
import { Context } from "./context.js";
|
|
3
3
|
export class Router {
|
|
4
4
|
/** Built tries per HTTP method */
|
|
@@ -68,7 +68,7 @@ export class Router {
|
|
|
68
68
|
const routes = this.#routesMap.get(req.method);
|
|
69
69
|
if (routes) {
|
|
70
70
|
// build trie
|
|
71
|
-
trie = new
|
|
71
|
+
trie = new Trie();
|
|
72
72
|
for (const route of routes)
|
|
73
73
|
trie.add(route);
|
|
74
74
|
this.#trieMap.set(req.method, trie);
|
package/dist/trie/index.d.ts
CHANGED
|
@@ -11,14 +11,14 @@ declare class ParamNode<Store> {
|
|
|
11
11
|
/** Matched route */
|
|
12
12
|
route: Route<Store> | null;
|
|
13
13
|
/** Static child node */
|
|
14
|
-
staticChild:
|
|
14
|
+
staticChild: Trie<Store> | null;
|
|
15
15
|
constructor(name: string);
|
|
16
16
|
}
|
|
17
|
-
export declare class
|
|
17
|
+
export declare class Trie<Store> {
|
|
18
18
|
/** Unique segment of the pattern trie */
|
|
19
19
|
segment: string;
|
|
20
20
|
/** Static child node map, key is the first character in the segment */
|
|
21
|
-
staticMap: Map<number,
|
|
21
|
+
staticMap: Map<number, Trie<Store>> | null;
|
|
22
22
|
/** Parametric child node */
|
|
23
23
|
paramChild: ParamNode<Store> | null;
|
|
24
24
|
/** Matched route */
|
|
@@ -29,12 +29,12 @@ export declare class Node<Store> {
|
|
|
29
29
|
* @param segment pattern segment
|
|
30
30
|
* @param staticChildren static children nodes to add to staticMap
|
|
31
31
|
*/
|
|
32
|
-
constructor(segment?: string, staticChildren?:
|
|
32
|
+
constructor(segment?: string, staticChildren?: Trie<Store>[]);
|
|
33
33
|
/**
|
|
34
34
|
* @param segment new segment
|
|
35
35
|
* @returns a clone of the Node with a new segment
|
|
36
36
|
*/
|
|
37
|
-
clone(segment: string):
|
|
37
|
+
clone(segment: string): Trie<unknown>;
|
|
38
38
|
/**
|
|
39
39
|
* If the current segment is "api/posts/"
|
|
40
40
|
* and "api/movies/" is added,
|
|
@@ -44,7 +44,7 @@ export declare class Node<Store> {
|
|
|
44
44
|
* @param segment new segment to use
|
|
45
45
|
* @returns the new child produced from the new segment
|
|
46
46
|
*/
|
|
47
|
-
fork(charIndex: number, segment: string):
|
|
47
|
+
fork(charIndex: number, segment: string): Trie<Store>;
|
|
48
48
|
/**
|
|
49
49
|
* node.segment = "static/static",
|
|
50
50
|
* staticSegment = "static/",
|
package/dist/trie/index.js
CHANGED
|
@@ -22,7 +22,7 @@ class ParamNode {
|
|
|
22
22
|
this.name = name;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
export class
|
|
25
|
+
export class Trie {
|
|
26
26
|
/** Unique segment of the pattern trie */
|
|
27
27
|
segment;
|
|
28
28
|
/** Static child node map, key is the first character in the segment */
|
|
@@ -51,7 +51,7 @@ export class Node {
|
|
|
51
51
|
* @returns a clone of the Node with a new segment
|
|
52
52
|
*/
|
|
53
53
|
clone(segment) {
|
|
54
|
-
const clone = new
|
|
54
|
+
const clone = new Trie(segment);
|
|
55
55
|
clone.staticMap = this.staticMap;
|
|
56
56
|
clone.paramChild = this.paramChild;
|
|
57
57
|
clone.route = this.route;
|
|
@@ -69,10 +69,10 @@ export class Node {
|
|
|
69
69
|
*/
|
|
70
70
|
fork(charIndex, segment) {
|
|
71
71
|
const existingChild = this.clone(this.segment.slice(charIndex)); // "posts/"
|
|
72
|
-
const newChild = new
|
|
72
|
+
const newChild = new Trie(segment.slice(charIndex)); // "movies/"
|
|
73
73
|
Object.assign(this,
|
|
74
74
|
// "api/" with the above as children
|
|
75
|
-
new
|
|
75
|
+
new Trie(this.segment.slice(0, charIndex), [existingChild, newChild]));
|
|
76
76
|
return newChild;
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
@@ -84,7 +84,7 @@ export class Node {
|
|
|
84
84
|
*/
|
|
85
85
|
split(segment) {
|
|
86
86
|
const secondHalf = this.clone(this.segment.slice(segment.length));
|
|
87
|
-
Object.assign(this, new
|
|
87
|
+
Object.assign(this, new Trie(segment, [secondHalf]));
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* @param name name of the param
|
|
@@ -125,7 +125,7 @@ export class Node {
|
|
|
125
125
|
paramSegments[paramIndex++].slice(1));
|
|
126
126
|
if (!paramChild.staticChild) {
|
|
127
127
|
// new - create node with the next static segment
|
|
128
|
-
current = paramChild.staticChild = new
|
|
128
|
+
current = paramChild.staticChild = new Trie(staticSegment);
|
|
129
129
|
continue; // next segment - no need to check since it's new
|
|
130
130
|
}
|
|
131
131
|
// there's already a static child - need to check if it's a match
|
|
@@ -160,7 +160,7 @@ export class Node {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
// otherwise, add new staticChild
|
|
163
|
-
const staticChild = new
|
|
163
|
+
const staticChild = new Trie(staticSegment.slice(charIndex));
|
|
164
164
|
current.staticMap.set(staticSegment.charCodeAt(charIndex), staticChild);
|
|
165
165
|
current = staticChild;
|
|
166
166
|
break; // next segment
|
package/package.json
CHANGED
package/dist/types.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type MaybePromise<T> = T | Promise<T>;
|
package/dist/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
File without changes
|
|
File without changes
|