ovr 2.1.0 → 3.0.1
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/dist/app/action.d.ts +2 -1
- package/dist/app/action.d.ts.map +1 -0
- package/dist/app/action.js +1 -1
- package/dist/app/context.d.ts +1 -0
- package/dist/app/context.d.ts.map +1 -0
- package/dist/app/context.js +9 -4
- package/dist/app/index.d.ts +1 -0
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +1 -1
- package/dist/{memo → app/memo}/index.d.ts +1 -0
- package/dist/app/memo/index.d.ts.map +1 -0
- package/dist/{mw → app/mw}/csrf.d.ts +2 -1
- package/dist/app/mw/csrf.d.ts.map +1 -0
- package/dist/app/page.d.ts +2 -1
- package/dist/app/page.d.ts.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -2
- package/dist/jsx/chunk/index.d.ts +29 -0
- package/dist/jsx/chunk/index.d.ts.map +1 -0
- package/dist/jsx/chunk/index.js +61 -0
- package/dist/jsx/index.d.ts +17 -12
- package/dist/jsx/index.d.ts.map +1 -0
- package/dist/jsx/index.js +95 -79
- package/dist/jsx/{elements.d.ts → intrinsic-elements.d.ts} +2 -1
- package/dist/jsx/intrinsic-elements.d.ts.map +1 -0
- package/dist/jsx/{merge-async-iterables.d.ts → merge-async-generators.d.ts} +1 -0
- package/dist/jsx/merge-async-generators.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +1 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +1 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/trie/index.d.ts +1 -0
- package/dist/trie/index.d.ts.map +1 -0
- package/dist/trie/insert-params.d.ts +1 -0
- package/dist/trie/insert-params.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/util/hash.d.ts +11 -0
- package/dist/util/hash.d.ts.map +1 -0
- package/dist/util/hash.js +16 -0
- package/package.json +4 -2
- package/src/app/action.ts +81 -0
- package/src/app/context.ts +329 -0
- package/src/app/index.ts +302 -0
- package/src/app/memo/index.ts +40 -0
- package/src/app/mw/csrf.ts +28 -0
- package/src/app/page.ts +56 -0
- package/src/index.ts +9 -0
- package/src/jsx/chunk/index.ts +70 -0
- package/src/jsx/index.ts +235 -0
- package/src/jsx/intrinsic-elements.ts +812 -0
- package/src/jsx/merge-async-generators.ts +42 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +1 -0
- package/src/trie/index.ts +335 -0
- package/src/trie/insert-params.ts +31 -0
- package/src/types/index.ts +23 -0
- package/src/util/hash.ts +17 -0
- package/dist/escape/index.d.ts +0 -8
- package/dist/escape/index.js +0 -33
- package/dist/hash/index.d.ts +0 -1
- package/dist/hash/index.js +0 -9
- /package/dist/{memo → app/memo}/index.js +0 -0
- /package/dist/{mw → app/mw}/csrf.js +0 -0
- /package/dist/jsx/{elements.js → intrinsic-elements.js} +0 -0
- /package/dist/jsx/{merge-async-iterables.js → merge-async-generators.js} +0 -0
package/src/app/index.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { Route, Trie } from "../trie/index.js";
|
|
2
|
+
import type {
|
|
3
|
+
DeepArray,
|
|
4
|
+
ExtractMultiParams,
|
|
5
|
+
ExtractParams,
|
|
6
|
+
} from "../types/index.js";
|
|
7
|
+
import { Action } from "./action.js";
|
|
8
|
+
import { Context } from "./context.js";
|
|
9
|
+
import { Page } from "./page.js";
|
|
10
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
|
+
|
|
12
|
+
export type Params = Record<string, string>;
|
|
13
|
+
|
|
14
|
+
export type Middleware<P extends Params = Params> = (
|
|
15
|
+
context: Context<P>,
|
|
16
|
+
next: () => Promise<void>,
|
|
17
|
+
) => any;
|
|
18
|
+
|
|
19
|
+
export type TrailingSlash = "always" | "never" | "ignore";
|
|
20
|
+
|
|
21
|
+
type Method =
|
|
22
|
+
| "GET"
|
|
23
|
+
| "HEAD"
|
|
24
|
+
| "POST"
|
|
25
|
+
| "PUT"
|
|
26
|
+
| "DELETE"
|
|
27
|
+
| "CONNECT"
|
|
28
|
+
| "OPTIONS"
|
|
29
|
+
| "TRACE"
|
|
30
|
+
| "PATCH"
|
|
31
|
+
| (string & {});
|
|
32
|
+
|
|
33
|
+
type UnmatchedContext<P extends Params = Params> = Omit<Context<P>, "route"> &
|
|
34
|
+
// route might be defined
|
|
35
|
+
Partial<Pick<Context<P>, "route">>;
|
|
36
|
+
|
|
37
|
+
export type ErrorHandler<P extends Params = Params> =
|
|
38
|
+
| ((context: UnmatchedContext<P>, error: unknown) => any)
|
|
39
|
+
| null;
|
|
40
|
+
|
|
41
|
+
export type NotFoundHandler<P extends Params = Params> = (
|
|
42
|
+
context: UnmatchedContext<P>,
|
|
43
|
+
) => any;
|
|
44
|
+
|
|
45
|
+
export class App {
|
|
46
|
+
// allows for users to put other properties on the app
|
|
47
|
+
[key: string | symbol | number]: any;
|
|
48
|
+
|
|
49
|
+
/** Built tries per HTTP method. */
|
|
50
|
+
#trieMap = new Map<Method, Trie<Middleware[]>>();
|
|
51
|
+
|
|
52
|
+
/** Added routes per HTTP method. */
|
|
53
|
+
#routesMap = new Map<Method, Route<Middleware[]>[]>();
|
|
54
|
+
|
|
55
|
+
/** Global middleware. */
|
|
56
|
+
#use: Middleware[] = [];
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* - `"never"` - Not found requests with a trailing slash will be redirected to the same path without a trailing slash
|
|
60
|
+
* - `"always"` - Not found requests without a trailing slash will be redirected to the same path with a trailing slash
|
|
61
|
+
* - `"ignore"` - no redirects (not recommended, bad for SEO)
|
|
62
|
+
*
|
|
63
|
+
* [Trailing Slash for Frameworks by Bjorn Lu](https://bjornlu.com/blog/trailing-slash-for-frameworks)
|
|
64
|
+
*
|
|
65
|
+
* @default "never"
|
|
66
|
+
*/
|
|
67
|
+
trailingSlash: TrailingSlash = "never";
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Base HTML to inject the `head` and `page` elements into.
|
|
71
|
+
*
|
|
72
|
+
* @default
|
|
73
|
+
*
|
|
74
|
+
* ```html
|
|
75
|
+
* <!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
base =
|
|
79
|
+
'<!doctype html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body></body></html>';
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Assign a handler to run when an `Error` is thrown.
|
|
83
|
+
*
|
|
84
|
+
* If not set, `Error` will be thrown. This might be desired
|
|
85
|
+
* if your server already includes error handling. Set in `config.start`
|
|
86
|
+
* to handle errors globally.
|
|
87
|
+
*
|
|
88
|
+
* @default null
|
|
89
|
+
*/
|
|
90
|
+
error: ErrorHandler = null;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Middleware to run when no `body` or `status` has been set on the `context`.
|
|
94
|
+
* Set to a new function to override the default.
|
|
95
|
+
*
|
|
96
|
+
* @default
|
|
97
|
+
*
|
|
98
|
+
* ```ts
|
|
99
|
+
* (c) => c.html("Not found", 404)
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
notFound: NotFoundHandler = (c) => c.html("Not found", 404);
|
|
103
|
+
|
|
104
|
+
/** Stores context per request. */
|
|
105
|
+
static storage = new AsyncLocalStorage<Context>();
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* @param routes `Page`s or `Action`s to add to the `App`
|
|
109
|
+
* @returns `App` instance
|
|
110
|
+
*/
|
|
111
|
+
add(...routes: DeepArray<Page | Action | Record<string, Page | Action>>[]) {
|
|
112
|
+
for (const route of routes) {
|
|
113
|
+
if (route instanceof Array) {
|
|
114
|
+
this.add(...route);
|
|
115
|
+
} else if (route instanceof Page) {
|
|
116
|
+
this.get(route.pattern, ...route.middleware);
|
|
117
|
+
} else if (route instanceof Action) {
|
|
118
|
+
this.post(route.pattern, ...route.middleware);
|
|
119
|
+
} else {
|
|
120
|
+
this.add(...Object.values(route));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Add global middleware.
|
|
129
|
+
*
|
|
130
|
+
* @param middleware
|
|
131
|
+
* @returns `App` instance
|
|
132
|
+
*/
|
|
133
|
+
use(...middleware: Middleware[]) {
|
|
134
|
+
this.#use.push(...middleware);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @param method HTTP method
|
|
140
|
+
* @param pattern Route pattern
|
|
141
|
+
* @param middleware
|
|
142
|
+
* @returns `App` instance
|
|
143
|
+
*/
|
|
144
|
+
on<Pattern extends string>(
|
|
145
|
+
method: Method | Method[],
|
|
146
|
+
pattern: Pattern,
|
|
147
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
148
|
+
): this;
|
|
149
|
+
/**
|
|
150
|
+
* @param method HTTP method
|
|
151
|
+
* @param patterns Array of route patterns
|
|
152
|
+
* @param middleware
|
|
153
|
+
* @returns `App` instance
|
|
154
|
+
*/
|
|
155
|
+
on<Patterns extends string[]>(
|
|
156
|
+
method: Method | Method[],
|
|
157
|
+
patterns: [...Patterns],
|
|
158
|
+
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
159
|
+
): this;
|
|
160
|
+
on<PatternOrPatterns extends string | string[]>(
|
|
161
|
+
method: Method | Method[],
|
|
162
|
+
pattern: PatternOrPatterns,
|
|
163
|
+
...middleware: Middleware[]
|
|
164
|
+
) {
|
|
165
|
+
if (!Array.isArray(method)) method = [method];
|
|
166
|
+
|
|
167
|
+
let patterns: string[];
|
|
168
|
+
if (!Array.isArray(pattern)) patterns = [pattern];
|
|
169
|
+
else patterns = pattern;
|
|
170
|
+
|
|
171
|
+
for (const p of patterns) {
|
|
172
|
+
const route = new Route(p, middleware);
|
|
173
|
+
|
|
174
|
+
for (const m of method) {
|
|
175
|
+
const routes = this.#routesMap.get(m);
|
|
176
|
+
|
|
177
|
+
if (routes) routes.push(route);
|
|
178
|
+
else this.#routesMap.set(m, [route]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param pattern Route pattern
|
|
187
|
+
* @param middleware
|
|
188
|
+
* @returns `App` instance
|
|
189
|
+
*/
|
|
190
|
+
get<Pattern extends string>(
|
|
191
|
+
pattern: Pattern,
|
|
192
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
193
|
+
): this;
|
|
194
|
+
/**
|
|
195
|
+
* @param patterns Array of route patterns
|
|
196
|
+
* @param middleware
|
|
197
|
+
* @returns `App` instance
|
|
198
|
+
*/
|
|
199
|
+
get<Patterns extends string[]>(
|
|
200
|
+
patterns: [...Patterns],
|
|
201
|
+
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
202
|
+
): this;
|
|
203
|
+
get<PatternOrPatterns extends string | string[]>(
|
|
204
|
+
patternOrPatterns: PatternOrPatterns,
|
|
205
|
+
...middleware: Middleware[]
|
|
206
|
+
) {
|
|
207
|
+
return this.on("GET", patternOrPatterns as string, ...middleware);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @param pattern Route pattern
|
|
212
|
+
* @param middleware
|
|
213
|
+
* @returns the router instance
|
|
214
|
+
*/
|
|
215
|
+
post<Pattern extends string>(
|
|
216
|
+
pattern: Pattern,
|
|
217
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
218
|
+
): this;
|
|
219
|
+
/**
|
|
220
|
+
* @param patterns Array of route patterns
|
|
221
|
+
* @param middleware
|
|
222
|
+
* @returns the router instance
|
|
223
|
+
*/
|
|
224
|
+
post<Patterns extends string[]>(
|
|
225
|
+
patterns: [...Patterns],
|
|
226
|
+
...middleware: Middleware<ExtractMultiParams<Patterns>>[]
|
|
227
|
+
): this;
|
|
228
|
+
post<PatternOrPatterns extends string | string[]>(
|
|
229
|
+
patternOrPatterns: PatternOrPatterns,
|
|
230
|
+
...middleware: Middleware[]
|
|
231
|
+
): this {
|
|
232
|
+
return this.on("POST", patternOrPatterns as string, ...middleware);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @param req [`Request` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
237
|
+
* @returns [`Response` Reference](https://developer.mozilla.org/en-US/docs/Web/API/Response)
|
|
238
|
+
*/
|
|
239
|
+
fetch = (req: Request): Promise<Response> => {
|
|
240
|
+
const c = new Context(
|
|
241
|
+
req,
|
|
242
|
+
new URL(req.url),
|
|
243
|
+
this.trailingSlash,
|
|
244
|
+
this.base,
|
|
245
|
+
this.error,
|
|
246
|
+
this.notFound,
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
return App.storage.run(c, async () => {
|
|
250
|
+
try {
|
|
251
|
+
// check to see if the method is already built
|
|
252
|
+
let trie = this.#trieMap.get(req.method);
|
|
253
|
+
|
|
254
|
+
if (!trie) {
|
|
255
|
+
// check if there are any routes with the method
|
|
256
|
+
const routes = this.#routesMap.get(req.method);
|
|
257
|
+
|
|
258
|
+
if (routes) {
|
|
259
|
+
// build trie
|
|
260
|
+
trie = new Trie<Middleware[]>();
|
|
261
|
+
for (const route of routes) trie.add(route);
|
|
262
|
+
this.#trieMap.set(req.method, trie);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (trie) {
|
|
267
|
+
const match = trie.find(c.url.pathname);
|
|
268
|
+
|
|
269
|
+
if (match) {
|
|
270
|
+
Object.assign(c, match);
|
|
271
|
+
|
|
272
|
+
const middleware = [...this.#use, ...match.route.store];
|
|
273
|
+
|
|
274
|
+
// compose
|
|
275
|
+
let i = -1;
|
|
276
|
+
const dispatch = async (current: number): Promise<void> => {
|
|
277
|
+
if (current <= i) throw new Error("next() called multiple times");
|
|
278
|
+
i = current;
|
|
279
|
+
|
|
280
|
+
if (middleware[current]) {
|
|
281
|
+
const result: unknown = await middleware[current](c, () =>
|
|
282
|
+
dispatch(current + 1),
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
if (result instanceof Response) c.res(result.body, result);
|
|
286
|
+
else if (result instanceof ReadableStream) c.body = result;
|
|
287
|
+
else if (result) c.page(result);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
await dispatch(0);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (c.error) c.error(c, error);
|
|
296
|
+
else throw error;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return c.build();
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class Memo {
|
|
2
|
+
/** Already memoized functions. */
|
|
3
|
+
#memoized = new WeakMap<Function, Function>();
|
|
4
|
+
|
|
5
|
+
/** Symbol to store the result of each memoized function. */
|
|
6
|
+
static #RESULT = Symbol();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calls to the returned function with the same arguments will
|
|
10
|
+
* return a cached result.
|
|
11
|
+
*
|
|
12
|
+
* @param fn Function to memoize.
|
|
13
|
+
* @returns The memoized function.
|
|
14
|
+
*/
|
|
15
|
+
use = <A extends any[], R>(fn: (...args: A) => R) => {
|
|
16
|
+
if (this.#memoized.has(fn)) return this.#memoized.get(fn) as typeof fn;
|
|
17
|
+
|
|
18
|
+
// closure will GC automatically
|
|
19
|
+
const trie = new Map();
|
|
20
|
+
|
|
21
|
+
const memo = (...args: A): R => {
|
|
22
|
+
let node = trie;
|
|
23
|
+
|
|
24
|
+
for (const arg of args) {
|
|
25
|
+
// get/create a node for each arg
|
|
26
|
+
if (!node.has(arg)) node.set(arg, new Map());
|
|
27
|
+
node = node.get(arg);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// set the final result if not there
|
|
31
|
+
if (!node.has(Memo.#RESULT)) node.set(Memo.#RESULT, fn(...args));
|
|
32
|
+
|
|
33
|
+
return node.get(Memo.#RESULT);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
this.#memoized.set(fn, memo);
|
|
37
|
+
|
|
38
|
+
return memo;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Middleware } from "../index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
5
|
+
*
|
|
6
|
+
* @param options
|
|
7
|
+
* @returns CSRF middleware
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { csrf } from "ovr";
|
|
13
|
+
*
|
|
14
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export const csrf = (options: { origin: string | string[] }): Middleware => {
|
|
18
|
+
if (typeof options.origin === "string") options.origin = [options.origin];
|
|
19
|
+
|
|
20
|
+
return (c, next) => {
|
|
21
|
+
if (["GET", "HEAD"].includes(c.req.method)) return next();
|
|
22
|
+
|
|
23
|
+
const origin = c.req.headers.get("Origin");
|
|
24
|
+
if (origin && options.origin.includes(origin)) return next();
|
|
25
|
+
|
|
26
|
+
return c.text("Forbidden", 403);
|
|
27
|
+
};
|
|
28
|
+
};
|
package/src/app/page.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type JSX, jsx } from "../jsx/index.js";
|
|
2
|
+
import { insertParams } from "../trie/insert-params.js";
|
|
3
|
+
import type { ExtractParams } from "../types/index.js";
|
|
4
|
+
import type { Middleware, Params } from "./index.js";
|
|
5
|
+
|
|
6
|
+
type AnchorProps<P extends Params> = JSX.IntrinsicElements["a"] &
|
|
7
|
+
(keyof P extends never ? { params?: never } : { params: P });
|
|
8
|
+
|
|
9
|
+
export class Page<Pattern extends string = string> {
|
|
10
|
+
/** Route pattern */
|
|
11
|
+
pattern: Pattern;
|
|
12
|
+
|
|
13
|
+
/** GET middleware */
|
|
14
|
+
middleware: Middleware<any>[];
|
|
15
|
+
|
|
16
|
+
/** `<a>` component with preset `href` attribute. */
|
|
17
|
+
Anchor: (props: AnchorProps<ExtractParams<Pattern>>) => JSX.Element;
|
|
18
|
+
|
|
19
|
+
#parts: string[];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param pattern Route pattern
|
|
23
|
+
* @param middleware GET middleware
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
*
|
|
27
|
+
* ```tsx
|
|
28
|
+
* // page.tsx
|
|
29
|
+
* import { Page } from "ovr";
|
|
30
|
+
*
|
|
31
|
+
* export const page = new Page("/", () => {
|
|
32
|
+
* return <p>Hello world</p>
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* const Nav = () => {
|
|
36
|
+
* return <page.Anchor>Home</page.Anchor>
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* // app.tsx
|
|
40
|
+
* app.add(page); registers the page
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
constructor(
|
|
44
|
+
pattern: Pattern,
|
|
45
|
+
...middleware: Middleware<ExtractParams<Pattern>>[]
|
|
46
|
+
) {
|
|
47
|
+
this.pattern = pattern;
|
|
48
|
+
this.middleware = middleware;
|
|
49
|
+
this.#parts = pattern.split("/");
|
|
50
|
+
this.Anchor = (props) => {
|
|
51
|
+
const { params = {}, ...rest } = props;
|
|
52
|
+
|
|
53
|
+
return jsx("a", { href: insertParams(this.#parts, params), ...rest });
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { jsx, toGenerator, toString, type JSX } from "./jsx/index.js";
|
|
2
|
+
export { Chunk } from "./jsx/chunk/index.js";
|
|
3
|
+
export { Trie, Route } from "./trie/index.js";
|
|
4
|
+
export { App, type Middleware, type Params } from "./app/index.js";
|
|
5
|
+
export { Context } from "./app/context.js";
|
|
6
|
+
export { Page } from "./app/page.js";
|
|
7
|
+
export { Action } from "./app/action.js";
|
|
8
|
+
export { csrf } from "./app/mw/csrf.js";
|
|
9
|
+
export type * from "./types/index.js";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export class Chunk {
|
|
2
|
+
static #attr = /[&"<]/g;
|
|
3
|
+
static #content = /[&<]/g;
|
|
4
|
+
|
|
5
|
+
/** Safe value to render */
|
|
6
|
+
value: string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `Chunk` to send in an HTML stream.
|
|
10
|
+
*
|
|
11
|
+
* @param s string of HTML to escape
|
|
12
|
+
* @param safe Set to `true` if the HTML is safe and should not be escaped
|
|
13
|
+
*/
|
|
14
|
+
constructor(s: unknown, safe?: boolean) {
|
|
15
|
+
const value = String(s ?? "");
|
|
16
|
+
|
|
17
|
+
if (safe) this.value = value;
|
|
18
|
+
else this.value = Chunk.escape(value);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @returns `value`
|
|
23
|
+
*/
|
|
24
|
+
toString() {
|
|
25
|
+
return this.value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param chunks Chunks to append to the end of the chunk
|
|
30
|
+
*/
|
|
31
|
+
concat(...chunks: Chunk[]) {
|
|
32
|
+
this.value += chunks.join("");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Escapes strings of HTML.
|
|
37
|
+
*
|
|
38
|
+
* @param s String to escape
|
|
39
|
+
* @param attr Set to `true` if the value is an attribute, otherwise it's a string of HTML content
|
|
40
|
+
* @returns Escaped string of HTML
|
|
41
|
+
*/
|
|
42
|
+
static escape(s: string, attr?: boolean) {
|
|
43
|
+
// adapted from https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/escaping.js
|
|
44
|
+
const regex = attr ? Chunk.#attr : Chunk.#content;
|
|
45
|
+
|
|
46
|
+
// search starts at beginning of the string
|
|
47
|
+
regex.lastIndex = 0;
|
|
48
|
+
// tracks the position of the last successful match in the input string
|
|
49
|
+
let start = 0;
|
|
50
|
+
let result = "";
|
|
51
|
+
|
|
52
|
+
// since `g` flag is used, regex maintains state with each loop,
|
|
53
|
+
// checking from the lastIndex onward each time
|
|
54
|
+
while (regex.test(s)) {
|
|
55
|
+
// index of the match
|
|
56
|
+
const i = regex.lastIndex - 1;
|
|
57
|
+
const match = s[i];
|
|
58
|
+
|
|
59
|
+
result +=
|
|
60
|
+
// everything that didn't match during this test
|
|
61
|
+
s.slice(start, i) +
|
|
62
|
+
// replacement
|
|
63
|
+
(match === "&" ? "&" : match === '"' ? """ : "<");
|
|
64
|
+
|
|
65
|
+
start = regex.lastIndex;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return result + s.slice(start);
|
|
69
|
+
}
|
|
70
|
+
}
|