ovr 2.0.3 → 2.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/dist/app/context.js +9 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/jsx/index.js +47 -39
- package/dist/jsx/merge-async-iterables.d.ts +7 -6
- package/dist/jsx/merge-async-iterables.js +21 -14
- package/dist/mw/csrf.d.ts +18 -0
- package/dist/mw/csrf.js +26 -0
- package/dist/trie/insert-params.js +18 -16
- package/package.json +1 -1
- package/dist/jsx/is-generator.d.ts +0 -1
- package/dist/jsx/is-generator.js +0 -7
- package/dist/jsx/yield-controller.d.ts +0 -4
- package/dist/jsx/yield-controller.js +0 -8
package/dist/app/context.js
CHANGED
|
@@ -155,11 +155,17 @@ export class Context {
|
|
|
155
155
|
}
|
|
156
156
|
elements[3] = bodyParts[0];
|
|
157
157
|
elements.push(Page, bodyClose + bodyParts[1]);
|
|
158
|
+
const gen = toGenerator(elements);
|
|
158
159
|
this.html(new ReadableStream({
|
|
159
|
-
async
|
|
160
|
-
|
|
160
|
+
async pull(c) {
|
|
161
|
+
const { value, done } = await gen.next();
|
|
162
|
+
if (done)
|
|
163
|
+
c.close();
|
|
164
|
+
else
|
|
161
165
|
c.enqueue(value);
|
|
162
|
-
|
|
166
|
+
},
|
|
167
|
+
cancel() {
|
|
168
|
+
gen.return();
|
|
163
169
|
},
|
|
164
170
|
}).pipeThrough(new TextEncoderStream()), status);
|
|
165
171
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/jsx/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { isGenerator } from "./is-generator.js";
|
|
2
1
|
import { merge } from "./merge-async-iterables.js";
|
|
3
|
-
import { YieldController } from "./yield-controller.js";
|
|
4
2
|
// https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags
|
|
5
3
|
const voidElements = new Set([
|
|
6
4
|
"area",
|
|
@@ -17,7 +15,6 @@ const voidElements = new Set([
|
|
|
17
15
|
"track",
|
|
18
16
|
"wbr",
|
|
19
17
|
]);
|
|
20
|
-
const yieldController = new YieldController();
|
|
21
18
|
/**
|
|
22
19
|
* The main function of the JSX transform cycle, each time JSX is encountered
|
|
23
20
|
* it is passed into this function to be resolved.
|
|
@@ -85,48 +82,59 @@ export async function* toGenerator(element) {
|
|
|
85
82
|
yield* toGenerator(children);
|
|
86
83
|
}
|
|
87
84
|
else if (Symbol.iterator in element) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
85
|
+
const iterator = element[Symbol.iterator]();
|
|
86
|
+
if (typeof iterator.next === "function" &&
|
|
87
|
+
typeof iterator.throw === "function" &&
|
|
88
|
+
typeof iterator.return === "function") {
|
|
89
|
+
// sync generator
|
|
90
|
+
// process lazily - avoids loading all in memory
|
|
91
|
+
while (true) {
|
|
92
|
+
const result = iterator.next();
|
|
93
|
+
if (result.done)
|
|
94
|
+
break;
|
|
95
|
+
yield* toGenerator(result.value);
|
|
96
|
+
}
|
|
96
97
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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;
|
|
113
124
|
}
|
|
114
|
-
// if it hasn't completed, stop iterating to the next
|
|
115
|
-
if (!complete.has(current))
|
|
116
|
-
break;
|
|
117
125
|
}
|
|
118
126
|
}
|
|
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
|
+
}
|
|
119
133
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
else {
|
|
124
|
-
queue[index] += result.value; // queue the value for later
|
|
125
|
-
}
|
|
134
|
+
queue = queue.join("");
|
|
135
|
+
if (queue)
|
|
136
|
+
yield queue; // clear the queue
|
|
126
137
|
}
|
|
127
|
-
queue = queue.join("");
|
|
128
|
-
if (queue)
|
|
129
|
-
yield queue; // clear the queue
|
|
130
138
|
}
|
|
131
139
|
else {
|
|
132
140
|
yield JSON.stringify(element); // avoids things like [object Object]
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Merges `
|
|
3
|
-
* The return of each `
|
|
2
|
+
* Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
3
|
+
* The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
|
|
4
4
|
*
|
|
5
5
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
6
6
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
7
|
+
* @param generators Resolved in parallel.
|
|
8
|
+
* @param returnValue Passed into each `iterator.return()`
|
|
9
|
+
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
9
10
|
*/
|
|
10
|
-
export declare function merge<T
|
|
11
|
+
export declare function merge<T>(generators: AsyncGenerator<T, void>[]): AsyncGenerator<{
|
|
11
12
|
index: number;
|
|
12
|
-
result: IteratorResult<T,
|
|
13
|
+
result: IteratorResult<T, void>;
|
|
13
14
|
}, void, unknown>;
|
|
@@ -4,27 +4,34 @@ const next = async (iterator, index) => ({
|
|
|
4
4
|
result: await iterator.next(),
|
|
5
5
|
});
|
|
6
6
|
/**
|
|
7
|
-
* Merges `
|
|
8
|
-
* The return of each `
|
|
7
|
+
* Merges `AsyncGenerator[]` into a single `AsyncGenerator`, resolving all in parallel.
|
|
8
|
+
* The return of each `AsyncGenerator` is yielded from the generator with `done: true`.
|
|
9
9
|
*
|
|
10
10
|
* Adapted from [stack overflow answers](https://stackoverflow.com/questions/50585456).
|
|
11
11
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @
|
|
12
|
+
* @param generators Resolved in parallel.
|
|
13
|
+
* @param returnValue Passed into each `iterator.return()`
|
|
14
|
+
* @yields `IteratorResult` and `index` of the resolved generator.
|
|
14
15
|
*/
|
|
15
|
-
export async function* merge(
|
|
16
|
-
const iterators =
|
|
16
|
+
export async function* merge(generators) {
|
|
17
|
+
const iterators = generators.map((gen) => gen[Symbol.asyncIterator]());
|
|
17
18
|
const promises = iterators.map(next);
|
|
18
19
|
let remaining = promises.length;
|
|
19
20
|
let current;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
try {
|
|
22
|
+
while (remaining) {
|
|
23
|
+
yield (current = await Promise.race(promises));
|
|
24
|
+
if (current.result.done) {
|
|
25
|
+
promises[current.index] = never;
|
|
26
|
+
remaining--;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
promises[current.index] = next(iterators[current.index], current.index);
|
|
30
|
+
}
|
|
28
31
|
}
|
|
29
32
|
}
|
|
33
|
+
finally {
|
|
34
|
+
for (const iter of iterators)
|
|
35
|
+
iter.return().catch(); // could have already returned
|
|
36
|
+
}
|
|
30
37
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Middleware } from "../app/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
4
|
+
*
|
|
5
|
+
* @param options
|
|
6
|
+
* @returns CSRF middleware
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { csrf } from "ovr";
|
|
12
|
+
*
|
|
13
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const csrf: (options: {
|
|
17
|
+
origin: string | string[];
|
|
18
|
+
}) => Middleware;
|
package/dist/mw/csrf.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic CSRF protection that checks the request's `method` and `Origin` header.
|
|
3
|
+
*
|
|
4
|
+
* @param options
|
|
5
|
+
* @returns CSRF middleware
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { csrf } from "ovr";
|
|
11
|
+
*
|
|
12
|
+
* app.use(csrf({ origin: "https://example.com" }));
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export const csrf = (options) => {
|
|
16
|
+
if (typeof options.origin === "string")
|
|
17
|
+
options.origin = [options.origin];
|
|
18
|
+
return (c, next) => {
|
|
19
|
+
if (["GET", "HEAD"].includes(c.req.method))
|
|
20
|
+
return next();
|
|
21
|
+
const origin = c.req.headers.get("Origin");
|
|
22
|
+
if (origin && options.origin.includes(origin))
|
|
23
|
+
return next();
|
|
24
|
+
return c.text("Forbidden", 403);
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -3,20 +3,22 @@
|
|
|
3
3
|
* @param params Parameters to insert
|
|
4
4
|
* @returns Resolved pathname with params
|
|
5
5
|
*/
|
|
6
|
-
export const insertParams = (parts, params) =>
|
|
7
|
-
.map((part) => {
|
|
8
|
-
if (part.startsWith(":")) {
|
|
9
|
-
const param = part.slice(1);
|
|
10
|
-
if (!(param in params))
|
|
11
|
-
throw new Error(`Parameter "${param}" did not match pattern..`);
|
|
12
|
-
return params[param];
|
|
13
|
-
}
|
|
6
|
+
export const insertParams = (parts, params) => {
|
|
14
7
|
const wild = "*";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
8
|
+
return parts
|
|
9
|
+
.map((part) => {
|
|
10
|
+
if (part.startsWith(":")) {
|
|
11
|
+
const param = part.slice(1);
|
|
12
|
+
if (!(param in params))
|
|
13
|
+
throw new Error(`Parameter "${param}" did not match pattern.`);
|
|
14
|
+
return params[param];
|
|
15
|
+
}
|
|
16
|
+
if (part === wild) {
|
|
17
|
+
if (!(wild in params))
|
|
18
|
+
throw new Error("No wildcard parameter found.");
|
|
19
|
+
return params[wild];
|
|
20
|
+
}
|
|
21
|
+
return part;
|
|
22
|
+
})
|
|
23
|
+
.join("/");
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isGenerator: <T, R = unknown, N = unknown>(iterable: Iterable<T, R, N>) => iterable is Generator<T, R, N>;
|
package/dist/jsx/is-generator.js
DELETED