@ogygia/router 0.11.0 → 0.13.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 +1 -1
- package/dist/client-api.js +6 -3
- package/dist/define.js +15 -10
- package/dist/router.js +3 -2
- package/dist/stream.d.ts +14 -1
- package/dist/stream.js +90 -23
- package/package.json +11 -6
package/README.md
CHANGED
|
@@ -40,4 +40,4 @@ export const handle = sequence(ogygia.handle(), router.handle);
|
|
|
40
40
|
|
|
41
41
|
The typed client is `api<App>()` from `@ogygia/router/api` (browser-safe).
|
|
42
42
|
|
|
43
|
-
Docs: [Server router](https://ogygiajs.dev/docs/
|
|
43
|
+
Docs: [@ogygia/router](https://ogygiajs.dev/docs/router/overview), with the full [Server router](https://ogygiajs.dev/docs/router/guide) guide.
|
package/dist/client-api.js
CHANGED
|
@@ -51,9 +51,12 @@ function api(base, opts = {}) {
|
|
|
51
51
|
...has_body ? { body: JSON.stringify(o.body) } : {}
|
|
52
52
|
});
|
|
53
53
|
if (res.status === 204) return void 0;
|
|
54
|
-
const
|
|
55
|
-
if (!res.ok)
|
|
56
|
-
|
|
54
|
+
const is_json = (res.headers.get("content-type") ?? "").includes("json");
|
|
55
|
+
if (!res.ok) {
|
|
56
|
+
const body = is_json ? await res.json().catch(() => void 0) : await res.text();
|
|
57
|
+
throw new ApiError(res.status, body);
|
|
58
|
+
}
|
|
59
|
+
return is_json ? await res.json() : await res.text();
|
|
57
60
|
};
|
|
58
61
|
return {
|
|
59
62
|
get: (p, ...a) => call("GET", p, a[0]),
|
package/dist/define.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
|
+
import { STALE, flights } from "@ogygia/machine";
|
|
2
|
+
import { request_scope } from "@ogygia/core/internal/server";
|
|
1
3
|
// src/define.ts
|
|
2
4
|
function memo_of(c) {
|
|
3
5
|
const holder = c;
|
|
4
|
-
|
|
6
|
+
if (holder.__ogloads) return holder.__ogloads;
|
|
7
|
+
const memo = flights.keyed()({
|
|
8
|
+
name: "router load",
|
|
9
|
+
key: ["def"],
|
|
10
|
+
ignore: { c: "the request this memo is made for: one per request" },
|
|
11
|
+
run: (i) => i.def.run(i.c)
|
|
12
|
+
});
|
|
13
|
+
request_scope(c.request).adopt(memo);
|
|
14
|
+
return holder.__ogloads = memo;
|
|
5
15
|
}
|
|
6
16
|
function load(a, b) {
|
|
7
17
|
const pattern = typeof a === "string" ? a : void 0;
|
|
8
18
|
const run = typeof a === "string" ? b : a;
|
|
9
|
-
const fn = ((c) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
p = Promise.resolve(run(c));
|
|
14
|
-
memo.set(fn, p);
|
|
15
|
-
}
|
|
16
|
-
return p;
|
|
17
|
-
});
|
|
19
|
+
const fn = ((c) => memo_of(c).get({
|
|
20
|
+
def: fn,
|
|
21
|
+
c
|
|
22
|
+
}).then((v) => v === STALE ? Promise.reject(request_scope(c.request).signal.reason) : v));
|
|
18
23
|
fn.__ogload = true;
|
|
19
24
|
fn.pattern = pattern;
|
|
20
25
|
fn.run = run;
|
package/dist/router.js
CHANGED
|
@@ -2,7 +2,8 @@ import { compile_all, fill, match_path } from "./match.js";
|
|
|
2
2
|
import { make_ctx } from "./ctx.js";
|
|
3
3
|
import { compile_endpoint, is_page, to_load } from "./define.js";
|
|
4
4
|
import { error, finalize, is_action_failure, is_http_error, is_redirect, json_response, method_not_allowed, not_found, options_response, redirect_response } from "./respond.js";
|
|
5
|
-
import { bake_yield, is_stream_slot, late_region_chunks, merge_chunks, page_slot_chunks, stream_document } from "./stream.js";
|
|
5
|
+
import { bake_yield, is_stream_slot, late_region_chunks, merge_chunks, page_slot_chunks, stream_document, until_drained } from "./stream.js";
|
|
6
|
+
import { request_scope } from "@ogygia/core/internal/server";
|
|
6
7
|
import { document } from "@ogygia/core/server";
|
|
7
8
|
import { lateSlot, openLateScope, region, registerRouteConfig, takeLateRegions } from "@ogygia/core/plugin";
|
|
8
9
|
import { componentCssHead } from "@ogygia/core/plugin/css";
|
|
@@ -260,7 +261,7 @@ function routes(typed, opts = {}) {
|
|
|
260
261
|
if (method === "HEAD") return new Response(null, res);
|
|
261
262
|
const late = takeLateRegions();
|
|
262
263
|
const chunk_src = gen_rest && late?.length ? merge_chunks(page_slot_chunks(gen_rest), late_region_chunks(late)) : gen_rest ? page_slot_chunks(gen_rest) : late?.length ? late_region_chunks(late) : null;
|
|
263
|
-
if (chunk_src && m === "GET") return stream_document(await res.text(), res, chunk_src);
|
|
264
|
+
if (chunk_src && m === "GET") return stream_document(await res.text(), res, until_drained(chunk_src, takeLateRegions), "pg", request_scope(ctx.request));
|
|
264
265
|
return res;
|
|
265
266
|
}
|
|
266
267
|
async function handle_thrown(thrown, def, ctx) {
|
package/dist/stream.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Scope } from "@ogygia/machine";
|
|
1
2
|
/** The slot id for the page position (one streamed slot per page in v1). */
|
|
2
3
|
export declare const PAGE_SLOT_ID = "pg";
|
|
3
4
|
/** Bake ANY yielded value to html: a string passes through, an inline region carries its html,
|
|
@@ -21,7 +22,7 @@ export interface LateChunk {
|
|
|
21
22
|
}
|
|
22
23
|
/** Adapt a page GENERATOR's remaining yields into page-slot chunks (bake included). */
|
|
23
24
|
export declare function page_slot_chunks(rest: AsyncGenerator<unknown>): AsyncGenerator<LateChunk>;
|
|
24
|
-
export declare function stream_document(full_html: string, res: Response, rest: AsyncGenerator<LateChunk>, error_slot?: string): Response;
|
|
25
|
+
export declare function stream_document(full_html: string, res: Response, rest: AsyncGenerator<LateChunk>, error_slot?: string, request?: Scope): Response;
|
|
25
26
|
/** The in-slot failure card (message escaped) — a stream must never end broken or empty. */
|
|
26
27
|
export declare function error_card_html(e: unknown): string;
|
|
27
28
|
/** Drain registered LATE REGIONS (`<Region of={promise}>` holes) in COMPLETION order — the slot
|
|
@@ -31,6 +32,18 @@ export declare function late_region_chunks(list: ReadonlyArray<{
|
|
|
31
32
|
id: string;
|
|
32
33
|
promise: Promise<unknown>;
|
|
33
34
|
}>): AsyncGenerator<LateChunk>;
|
|
35
|
+
/**
|
|
36
|
+
* Every late chunk of the document, until nothing new registers. A chunk bakes its region as its
|
|
37
|
+
* promise settles, and a region baked there may hold a `<Region of={promise}>` of its own: it
|
|
38
|
+
* registers then, after the document's list was taken. So after each chunk the request's late
|
|
39
|
+
* regions are taken again (`take`), and whatever registered joins as one more source, in
|
|
40
|
+
* completion order with the rest. It ends only when every source ended and a last take finds
|
|
41
|
+
* none: no slot is left that nothing fills.
|
|
42
|
+
*/
|
|
43
|
+
export declare function until_drained(first: AsyncGenerator<LateChunk>, take: () => ReadonlyArray<{
|
|
44
|
+
id: string;
|
|
45
|
+
promise: Promise<unknown>;
|
|
46
|
+
}> | null): AsyncGenerator<LateChunk>;
|
|
34
47
|
/** Interleave chunk sources by READINESS (a page generator + the late-region pool run together;
|
|
35
48
|
* whichever has a chunk ready streams next). Ends when every source ends. */
|
|
36
49
|
export declare function merge_chunks(...sources: AsyncGenerator<LateChunk>[]): AsyncGenerator<LateChunk>;
|
package/dist/stream.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { STALE, scope } from "@ogygia/machine";
|
|
2
|
+
import { cleanup_failed } from "@ogygia/core/internal/server";
|
|
1
3
|
import { LATE_BOOT_SCRIPT, lateChunk } from "@ogygia/core/plugin";
|
|
2
4
|
import { escapeText } from "@ogygia/core/plugin/utils";
|
|
3
5
|
// src/stream.ts
|
|
@@ -58,26 +60,36 @@ async function* page_slot_chunks(rest) {
|
|
|
58
60
|
};
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
|
-
function stream_document(full_html, res, rest, error_slot = "pg") {
|
|
63
|
+
function stream_document(full_html, res, rest, error_slot = "pg", request) {
|
|
62
64
|
const with_boot = full_html.replace(HEAD_CLOSE_RE, LATE_BOOT_SCRIPT + "</head>");
|
|
63
65
|
const cut = with_boot.lastIndexOf("</body>");
|
|
64
66
|
const head_part = cut === -1 ? with_boot : with_boot.slice(0, cut);
|
|
65
67
|
const tail_part = cut === -1 ? "" : with_boot.slice(cut);
|
|
66
68
|
const enc = new TextEncoder();
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
const owner = scope(request);
|
|
70
|
+
owner.defer(() => void rest.return(void 0).catch(cleanup_failed));
|
|
71
|
+
const body = new ReadableStream({
|
|
72
|
+
async start(controller) {
|
|
73
|
+
controller.enqueue(enc.encode(head_part));
|
|
74
|
+
try {
|
|
75
|
+
for (;;) {
|
|
76
|
+
const n = await owner.task(() => rest.next());
|
|
77
|
+
if (n === STALE) return;
|
|
78
|
+
if (n.done) break;
|
|
79
|
+
controller.enqueue(enc.encode(lateChunk(n.value.slot, n.value.html)));
|
|
80
|
+
}
|
|
81
|
+
} catch (e) {
|
|
82
|
+
if (owner.closed) return;
|
|
83
|
+
controller.enqueue(enc.encode(lateChunk(error_slot, error_card_html(e))));
|
|
74
84
|
}
|
|
75
|
-
|
|
76
|
-
controller.
|
|
85
|
+
controller.enqueue(enc.encode(tail_part));
|
|
86
|
+
controller.close();
|
|
87
|
+
owner.close();
|
|
88
|
+
},
|
|
89
|
+
cancel() {
|
|
90
|
+
owner.close();
|
|
77
91
|
}
|
|
78
|
-
|
|
79
|
-
controller.close();
|
|
80
|
-
} });
|
|
92
|
+
});
|
|
81
93
|
const headers = new Headers(res.headers);
|
|
82
94
|
headers.delete("content-length");
|
|
83
95
|
const cc = headers.get("cache-control");
|
|
@@ -113,6 +125,57 @@ async function* late_region_chunks(list) {
|
|
|
113
125
|
};
|
|
114
126
|
}
|
|
115
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Every late chunk of the document, until nothing new registers. A chunk bakes its region as its
|
|
130
|
+
* promise settles, and a region baked there may hold a `<Region of={promise}>` of its own: it
|
|
131
|
+
* registers then, after the document's list was taken. So after each chunk the request's late
|
|
132
|
+
* regions are taken again (`take`), and whatever registered joins as one more source, in
|
|
133
|
+
* completion order with the rest. It ends only when every source ended and a last take finds
|
|
134
|
+
* none: no slot is left that nothing fills.
|
|
135
|
+
*/
|
|
136
|
+
async function* until_drained(first, take) {
|
|
137
|
+
const sources = [];
|
|
138
|
+
const nexts = /* @__PURE__ */ new Map();
|
|
139
|
+
const add = (source) => {
|
|
140
|
+
const i = sources.push(source) - 1;
|
|
141
|
+
nexts.set(i, source.next().then((r) => ({
|
|
142
|
+
i,
|
|
143
|
+
r
|
|
144
|
+
})));
|
|
145
|
+
};
|
|
146
|
+
const take_more = () => {
|
|
147
|
+
const more = take();
|
|
148
|
+
if (more?.length) add(late_region_chunks(more));
|
|
149
|
+
};
|
|
150
|
+
add(first);
|
|
151
|
+
try {
|
|
152
|
+
for (;;) {
|
|
153
|
+
if (nexts.size === 0) {
|
|
154
|
+
take_more();
|
|
155
|
+
if (nexts.size === 0) return;
|
|
156
|
+
}
|
|
157
|
+
const { i, r } = await Promise.race(nexts.values());
|
|
158
|
+
if (r.done) {
|
|
159
|
+
nexts.delete(i);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
nexts.set(i, sources[i].next().then((res) => ({
|
|
163
|
+
i,
|
|
164
|
+
r: res
|
|
165
|
+
})));
|
|
166
|
+
yield r.value;
|
|
167
|
+
take_more();
|
|
168
|
+
}
|
|
169
|
+
} finally {
|
|
170
|
+
end_all(sources);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** Ends each source (its `finally` runs once its pending step lands); one that already ended is
|
|
174
|
+
* left as it is. Nobody waits on that ending, so a `finally` that throws is said
|
|
175
|
+
* (`request_cleanup_failed`). */
|
|
176
|
+
function end_all(sources) {
|
|
177
|
+
for (const s of sources) s.return(void 0).catch(cleanup_failed);
|
|
178
|
+
}
|
|
116
179
|
/** Interleave chunk sources by READINESS (a page generator + the late-region pool run together;
|
|
117
180
|
* whichever has a chunk ready streams next). Ends when every source ends. */
|
|
118
181
|
async function* merge_chunks(...sources) {
|
|
@@ -120,16 +183,20 @@ async function* merge_chunks(...sources) {
|
|
|
120
183
|
i,
|
|
121
184
|
r
|
|
122
185
|
}))]));
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
i,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
186
|
+
try {
|
|
187
|
+
while (nexts.size) {
|
|
188
|
+
const { i, r } = await Promise.race(nexts.values());
|
|
189
|
+
if (r.done) nexts.delete(i);
|
|
190
|
+
else {
|
|
191
|
+
nexts.set(i, sources[i].next().then((res) => ({
|
|
192
|
+
i,
|
|
193
|
+
r: res
|
|
194
|
+
})));
|
|
195
|
+
yield r.value;
|
|
196
|
+
}
|
|
132
197
|
}
|
|
198
|
+
} finally {
|
|
199
|
+
end_all(sources);
|
|
133
200
|
}
|
|
134
201
|
}
|
|
135
|
-
export { PAGE_SLOT_ID, bake_yield, error_card_html, is_stream_slot, late_region_chunks, merge_chunks, page_slot_chunks, stream_document };
|
|
202
|
+
export { PAGE_SLOT_ID, bake_yield, error_card_html, is_stream_slot, late_region_chunks, merge_chunks, page_slot_chunks, stream_document, until_drained };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ogygia/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "The server router for ogygia: Kit's routing model as values (routes, layouts, loads, actions, endpoints, streamed pages), with a typed fetch client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ogygia",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"README.md"
|
|
23
23
|
],
|
|
24
24
|
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
25
26
|
"exports": {
|
|
26
27
|
".": {
|
|
27
28
|
"types": "./dist/index.d.ts",
|
|
@@ -40,7 +41,13 @@
|
|
|
40
41
|
"publishConfig": {
|
|
41
42
|
"access": "public"
|
|
42
43
|
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@ogygia/machine": "^0.13.0"
|
|
46
|
+
},
|
|
43
47
|
"devDependencies": {
|
|
48
|
+
"@ogygia/config": "0.0.0",
|
|
49
|
+
"@ogygia/core": "0.13.0",
|
|
50
|
+
"@ogygia/tooling": "0.0.0",
|
|
44
51
|
"@playwright/test": "1.62.1",
|
|
45
52
|
"@sveltejs/kit": "^2.70.2",
|
|
46
53
|
"@sveltejs/vite-plugin-svelte": "^7.2.0",
|
|
@@ -51,14 +58,12 @@
|
|
|
51
58
|
"tsdown": "^0.23.4",
|
|
52
59
|
"typescript": "^6.0.3",
|
|
53
60
|
"vite": "^8.3.5",
|
|
54
|
-
"vitest": "^4.1.12"
|
|
55
|
-
"@ogygia/core": "0.11.0",
|
|
56
|
-
"@ogygia/tooling": "0.0.0"
|
|
61
|
+
"vitest": "^4.1.12"
|
|
57
62
|
},
|
|
58
63
|
"peerDependencies": {
|
|
64
|
+
"@ogygia/core": "^0.13.0",
|
|
59
65
|
"@sveltejs/kit": ">=2.70.2 <4",
|
|
60
|
-
"svelte": "^5.56.8"
|
|
61
|
-
"@ogygia/core": "^0.11.0"
|
|
66
|
+
"svelte": "^5.56.8"
|
|
62
67
|
},
|
|
63
68
|
"scripts": {
|
|
64
69
|
"build": "tsdown",
|