@tdacorp/hyle-sdk 0.21.0 → 0.23.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 +141 -18
- package/dist/check-routes-bin.js +174 -42
- package/dist/{chunk-56Y24XBV.js → chunk-JHZJOJDC.js} +4 -3
- package/dist/{chunk-K5VDLROQ.js → chunk-SFFBZFEN.js} +34 -1
- package/dist/{chunk-HJX57NLI.js → chunk-UFMKLCXS.js} +246 -225
- package/dist/{chunk-X2TLYM7Y.js → chunk-VUML2UPD.js} +1 -1
- package/dist/chunk-VXARM37R.js +68 -0
- package/dist/{chunk-75JFLML4.js → chunk-YHXE3TGS.js} +94 -12
- package/dist/{client-41fU6OY2.d.cts → client--ighAWaj.d.ts} +13 -1
- package/dist/{client-LwzHwqpM.d.ts → client-DK-S2580.d.cts} +13 -1
- package/dist/code-copy.cjs +4 -2
- package/dist/code-copy.d.cts +5 -2
- package/dist/code-copy.d.ts +5 -2
- package/dist/code-copy.js +5 -3
- package/dist/comments.cjs +14 -6
- package/dist/comments.js +14 -6
- package/dist/docs-enhance.cjs +7 -4
- package/dist/docs-enhance.d.cts +5 -3
- package/dist/docs-enhance.d.ts +5 -3
- package/dist/docs-enhance.js +8 -5
- package/dist/{embed-BUFRQboD.d.ts → embed-XT0x_Jaa.d.cts} +4 -2
- package/dist/{embed-CYt-q13P.d.cts → embed-g7NT1CyB.d.ts} +4 -2
- package/dist/html.cjs +14 -6
- package/dist/html.d.cts +1 -1
- package/dist/html.d.ts +1 -1
- package/dist/html.js +2 -2
- package/dist/index.cjs +85 -12
- package/dist/index.d.cts +13 -11
- package/dist/index.d.ts +13 -11
- package/dist/index.js +2 -2
- package/dist/mermaid-diagram.cjs +46 -6
- package/dist/mermaid-diagram.js +15 -6
- package/dist/mermaid-image.cjs +4 -1
- package/dist/mermaid-image.js +1 -1
- package/dist/next.cjs +521 -242
- package/dist/next.d.cts +74 -17
- package/dist/next.d.ts +74 -17
- package/dist/next.js +207 -33
- package/dist/print-coordinator.js +1 -1
- package/dist/react.cjs +233 -197
- package/dist/react.d.cts +17 -6
- package/dist/react.d.ts +17 -6
- package/dist/react.js +5 -3
- package/dist/{resolve-BjAK0k_Y.d.ts → resolve-BqC9l5RH.d.ts} +1 -1
- package/dist/{resolve-eI1muCKL.d.cts → resolve-CiZCPysX.d.cts} +1 -1
- package/dist/{sanitize-ijiWjEm9.d.cts → sanitize-BpY_AdZh.d.cts} +1 -1
- package/dist/{sanitize-ijiWjEm9.d.ts → sanitize-BpY_AdZh.d.ts} +1 -1
- package/dist/styles.css +4 -4
- package/dist/toc-spy.cjs +2 -1
- package/dist/toc-spy.js +2 -1
- package/dist/{types-3F0wqD-o.d.cts → types-b9QbmEDd.d.cts} +44 -1
- package/dist/{types-3F0wqD-o.d.ts → types-b9QbmEDd.d.ts} +44 -1
- package/dist/use-cache.cjs +73 -3
- package/dist/use-cache.d.cts +3 -3
- package/dist/use-cache.d.ts +3 -3
- package/dist/use-cache.js +3 -3
- package/package.json +3 -3
- package/dist/chunk-67IOIAVW.js +0 -35
package/dist/next.js
CHANGED
|
@@ -19,15 +19,15 @@ import {
|
|
|
19
19
|
readingMinutes,
|
|
20
20
|
validOgImage,
|
|
21
21
|
wordCountOf
|
|
22
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-UFMKLCXS.js";
|
|
23
23
|
import {
|
|
24
24
|
stringsFor
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-JHZJOJDC.js";
|
|
26
26
|
import {
|
|
27
27
|
DEFAULT_REVALIDATE,
|
|
28
28
|
fetchResolver,
|
|
29
29
|
workspaceTags
|
|
30
|
-
} from "./chunk-
|
|
30
|
+
} from "./chunk-VUML2UPD.js";
|
|
31
31
|
import {
|
|
32
32
|
Cp1ClientError,
|
|
33
33
|
checkBaseUrl,
|
|
@@ -35,15 +35,24 @@ import {
|
|
|
35
35
|
createHyleClient,
|
|
36
36
|
createRevalidationCallbackRoute,
|
|
37
37
|
engagementSigningKey,
|
|
38
|
+
isNotFoundBody,
|
|
39
|
+
isNotFoundResponse,
|
|
38
40
|
listPublicationPaths,
|
|
39
41
|
readCapped,
|
|
40
42
|
signReaderAssertion
|
|
41
|
-
} from "./chunk-
|
|
43
|
+
} from "./chunk-YHXE3TGS.js";
|
|
42
44
|
import {
|
|
43
45
|
ASSET_CONTENT_TYPES,
|
|
44
46
|
ASSET_FILE,
|
|
45
|
-
ASSET_ROUTE_PREFIX
|
|
46
|
-
|
|
47
|
+
ASSET_ROUTE_PREFIX,
|
|
48
|
+
PREVIEW_PARAM,
|
|
49
|
+
PREVIEW_TOKEN,
|
|
50
|
+
PROTECTED_ID,
|
|
51
|
+
PROTECTED_PARAM,
|
|
52
|
+
PROTECTED_PASSWORD,
|
|
53
|
+
THEME_FILE,
|
|
54
|
+
protectedCookieName
|
|
55
|
+
} from "./chunk-VXARM37R.js";
|
|
47
56
|
|
|
48
57
|
// src/next/page.tsx
|
|
49
58
|
import { notFound, permanentRedirect, redirect } from "next/navigation";
|
|
@@ -70,7 +79,7 @@ function sitePathFrom(basePath, segments) {
|
|
|
70
79
|
return void 0;
|
|
71
80
|
}
|
|
72
81
|
if (segment !== segment.normalize("NFC")) return void 0;
|
|
73
|
-
if (segment === "" || segment === "." || segment
|
|
82
|
+
if (segment === "" || segment === "." || segment.includes("..") || /[/\\\u0000-\u001f\u007f]|%[0-9a-f]{2}/i.test(segment)) return void 0;
|
|
74
83
|
decoded.push(segment);
|
|
75
84
|
}
|
|
76
85
|
const path = decoded.length === 0 ? basePath || "/" : `${basePath}/${decoded.join("/")}`;
|
|
@@ -105,9 +114,10 @@ function routeContext(options, params) {
|
|
|
105
114
|
const toHref = (sitePath, linkLocale = locale) => (linkLocale ? prefix(linkLocale) : "") + sitePath;
|
|
106
115
|
return { basePath, path, locale, toHref };
|
|
107
116
|
}
|
|
108
|
-
function
|
|
117
|
+
function isNotFoundError(error, cursor) {
|
|
109
118
|
unstable_rethrow(error);
|
|
110
|
-
|
|
119
|
+
if (!(error instanceof Cp1ClientError)) return false;
|
|
120
|
+
return error.status === 404 && error.code === "NOT_FOUND" || cursor !== void 0 && error.status === 400 && error.code === "VALIDATION_ERROR";
|
|
111
121
|
}
|
|
112
122
|
|
|
113
123
|
// src/next/embed.tsx
|
|
@@ -219,6 +229,10 @@ function sameOriginProblem(req, write, allowedOrigins) {
|
|
|
219
229
|
const site = req.headers.get("sec-fetch-site");
|
|
220
230
|
if (site !== null && site !== "same-origin") return true;
|
|
221
231
|
if (!write) return false;
|
|
232
|
+
if (req.headers.get(ENGAGE_CSRF_HEADER) !== "1") return true;
|
|
233
|
+
return originMismatch(req, allowedOrigins);
|
|
234
|
+
}
|
|
235
|
+
function originMismatch(req, allowedOrigins) {
|
|
222
236
|
const origin = req.headers.get("origin");
|
|
223
237
|
if (origin === null) return true;
|
|
224
238
|
let parsed;
|
|
@@ -227,7 +241,6 @@ function sameOriginProblem(req, write, allowedOrigins) {
|
|
|
227
241
|
} catch {
|
|
228
242
|
return true;
|
|
229
243
|
}
|
|
230
|
-
if (req.headers.get(ENGAGE_CSRF_HEADER) !== "1") return true;
|
|
231
244
|
if (allowedOrigins) return !allowedOrigins.some((o) => safeOrigin(o) === parsed.origin);
|
|
232
245
|
const host = req.headers.get("x-forwarded-host")?.split(",")[0]?.trim() || req.headers.get("host") || new URL(req.url).host;
|
|
233
246
|
const proto = req.headers.get("x-forwarded-proto")?.split(",")[0]?.trim().toLowerCase();
|
|
@@ -408,7 +421,9 @@ function createEngagementRoute(options) {
|
|
|
408
421
|
redirect: "error",
|
|
409
422
|
signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS)
|
|
410
423
|
});
|
|
411
|
-
|
|
424
|
+
const text = await readCapped(res, MAX_RESPONSE_BYTES);
|
|
425
|
+
const status = res.status === 404 && !isNotFoundBody(res.status, text) ? 502 : res.status;
|
|
426
|
+
return { status, ok: res.ok, text, retryAfter: res.headers.get("retry-after"), throttle: res.headers.get("hyle-throttle") };
|
|
412
427
|
};
|
|
413
428
|
try {
|
|
414
429
|
const out = route.action(req.method) === "comments.list" ? await shared(`${viewer?.reader.sub ?? ""}
|
|
@@ -426,11 +441,11 @@ ${built.query.toString()}`, () => listing(viewer, call)) : await call();
|
|
|
426
441
|
|
|
427
442
|
// src/next/embed.tsx
|
|
428
443
|
import { jsx } from "react/jsx-runtime";
|
|
429
|
-
async function resolveEmbed(options, run) {
|
|
444
|
+
async function resolveEmbed(options, run, cursor) {
|
|
430
445
|
try {
|
|
431
446
|
return { result: await run() };
|
|
432
447
|
} catch (error) {
|
|
433
|
-
if (
|
|
448
|
+
if (isNotFoundError(error, cursor)) return {};
|
|
434
449
|
console.error(`hyle: ${error instanceof Error ? error.name : "UnknownError"}`);
|
|
435
450
|
return { failure: options.onError ? options.onError(error) : null };
|
|
436
451
|
}
|
|
@@ -440,10 +455,11 @@ async function HyleList({ options, limit = 5, locale, layout, title, headingLeve
|
|
|
440
455
|
const route = routeContext(locale ? { ...options, locale } : options, {});
|
|
441
456
|
const path = sitePathFrom(route.basePath, void 0);
|
|
442
457
|
if (path === void 0) return null;
|
|
443
|
-
const
|
|
458
|
+
const after = validCursor(cursor);
|
|
459
|
+
const { result, failure } = await resolveEmbed(options, () => resolve(path, route.locale, limit, after), after);
|
|
444
460
|
if (failure !== void 0) return failure;
|
|
445
461
|
const next = olderHref && result?.kind === "list" ? validCursor(result.next_cursor) : void 0;
|
|
446
|
-
return /* @__PURE__ */ jsx(HyleListView, { items: result?.kind === "list" ? result.items : [], layout, title, headingLevel, moreHref, olderHref: next ? olderHref?.(next) : void 0, components: options.components, slots: options.slots, locale: route.locale, toHref: route.toHref });
|
|
462
|
+
return /* @__PURE__ */ jsx(HyleListView, { items: result?.kind === "list" ? result.items : [], theme: result?.kind === "list" ? result.theme : void 0, layout, title, headingLevel, moreHref, olderHref: next ? olderHref?.(next) : void 0, components: options.components, slots: options.slots, locale: route.locale, toHref: route.toHref });
|
|
447
463
|
}
|
|
448
464
|
async function HyleArticle({ options, path, locale, headingLevel }) {
|
|
449
465
|
if (!isSitePath(path)) return null;
|
|
@@ -472,7 +488,7 @@ async function HyleComments({ options, path, revalidate = 60, interactive }) {
|
|
|
472
488
|
};
|
|
473
489
|
const res = await fetch(`${options.baseUrl.replace(/\/+$/, "")}/cp1/engage/comments?${new URLSearchParams({ path })}`, init);
|
|
474
490
|
if (!res.ok) {
|
|
475
|
-
if (res
|
|
491
|
+
if (!await isNotFoundResponse(res)) console.error(`hyle: comments answered HTTP ${res.status}`);
|
|
476
492
|
return null;
|
|
477
493
|
}
|
|
478
494
|
listing = parseListing(JSON.parse(await readCapped(res, MAX_LISTING_BYTES)).data);
|
|
@@ -503,8 +519,9 @@ function hylePage(options) {
|
|
|
503
519
|
return async function HylePage({ params, searchParams }) {
|
|
504
520
|
const { path, locale, toHref } = routeContext(options, await params ?? {});
|
|
505
521
|
if (path === void 0) notFound();
|
|
522
|
+
let cursor;
|
|
506
523
|
try {
|
|
507
|
-
|
|
524
|
+
cursor = options.paginate === "searchParams" ? validCursor((await searchParams)?.cursor) : void 0;
|
|
508
525
|
const result = await resolve(path, locale, options.listLimit, cursor, options.layout === "docs" ? "tree" : void 0);
|
|
509
526
|
switch (result.kind) {
|
|
510
527
|
case "article": {
|
|
@@ -532,7 +549,7 @@ function hylePage(options) {
|
|
|
532
549
|
notFound();
|
|
533
550
|
}
|
|
534
551
|
} catch (error) {
|
|
535
|
-
if (
|
|
552
|
+
if (isNotFoundError(error, cursor)) notFound();
|
|
536
553
|
if (!options.onError) throw error;
|
|
537
554
|
console.error(`hyle: ${error instanceof Error ? error.name : "UnknownError"}`);
|
|
538
555
|
return options.onError(error);
|
|
@@ -633,8 +650,9 @@ function hyleMetadata(options) {
|
|
|
633
650
|
};
|
|
634
651
|
return Object.keys(types).length ? { ...alternates, types } : alternates;
|
|
635
652
|
};
|
|
653
|
+
let cursor;
|
|
636
654
|
try {
|
|
637
|
-
|
|
655
|
+
cursor = options.paginate === "searchParams" ? validCursor((await searchParams)?.cursor) : void 0;
|
|
638
656
|
const result = await resolve(path, locale, options.listLimit, cursor, options.layout === "docs" ? "tree" : void 0);
|
|
639
657
|
const parentMeta = parent ? await parent : void 0;
|
|
640
658
|
const parentImages = parentMeta?.openGraph?.images;
|
|
@@ -710,7 +728,7 @@ function hyleMetadata(options) {
|
|
|
710
728
|
} : {}
|
|
711
729
|
};
|
|
712
730
|
} catch (error) {
|
|
713
|
-
if (
|
|
731
|
+
if (isNotFoundError(error, cursor)) return {};
|
|
714
732
|
throw error;
|
|
715
733
|
}
|
|
716
734
|
};
|
|
@@ -780,7 +798,7 @@ function createRevalidateRoute(options) {
|
|
|
780
798
|
}
|
|
781
799
|
|
|
782
800
|
// src/next/root.ts
|
|
783
|
-
import { notFound as notFound2 } from "next/navigation";
|
|
801
|
+
import { notFound as notFound2, unstable_rethrow as unstable_rethrow4 } from "next/navigation";
|
|
784
802
|
|
|
785
803
|
// src/next/mount-match.ts
|
|
786
804
|
function activeMounts(mounts) {
|
|
@@ -827,10 +845,7 @@ function mountsLoader(config) {
|
|
|
827
845
|
lastKnownGood.set(key, { loaded, at: Date.now() });
|
|
828
846
|
return loaded;
|
|
829
847
|
} catch (error) {
|
|
830
|
-
|
|
831
|
-
lastKnownGood.delete(key);
|
|
832
|
-
return NONE;
|
|
833
|
-
}
|
|
848
|
+
unstable_rethrow4(error);
|
|
834
849
|
if (error instanceof Cp1ClientError && (error.status === 401 || error.status === 403)) {
|
|
835
850
|
lastKnownGood.delete(key);
|
|
836
851
|
throw error;
|
|
@@ -962,7 +977,7 @@ function createMarkdownRoute(options) {
|
|
|
962
977
|
}
|
|
963
978
|
return reply(null, 404);
|
|
964
979
|
} catch (error) {
|
|
965
|
-
if (
|
|
980
|
+
if (isNotFoundError(error)) return reply(null, 404);
|
|
966
981
|
console.error(`hyle: ${error instanceof Error ? error.name : "UnknownError"}`);
|
|
967
982
|
return reply(null, 502);
|
|
968
983
|
}
|
|
@@ -971,7 +986,7 @@ function createMarkdownRoute(options) {
|
|
|
971
986
|
}
|
|
972
987
|
|
|
973
988
|
// src/next/llms.ts
|
|
974
|
-
import { unstable_rethrow as
|
|
989
|
+
import { unstable_rethrow as unstable_rethrow5 } from "next/navigation";
|
|
975
990
|
var oneLine = (text) => text.replace(/\s+/g, " ").trim();
|
|
976
991
|
var linkTarget = (url) => url.replace(/[()]/g, (c) => c === "(" ? "%28" : "%29");
|
|
977
992
|
function createLlmsTxtRoute(options, llms) {
|
|
@@ -996,7 +1011,7 @@ function createLlmsTxtRoute(options, llms) {
|
|
|
996
1011
|
return new Response(`${lines.join("\n")}
|
|
997
1012
|
`, { headers: { "Content-Type": "text/plain; charset=utf-8" } });
|
|
998
1013
|
} catch (error) {
|
|
999
|
-
|
|
1014
|
+
unstable_rethrow5(error);
|
|
1000
1015
|
console.error(`hyle: ${error instanceof Error ? error.name : "UnknownError"}`);
|
|
1001
1016
|
return new Response(null, { status: 503 });
|
|
1002
1017
|
}
|
|
@@ -1013,8 +1028,40 @@ var DEFAULT_TIMEOUT_MS2 = 3e4;
|
|
|
1013
1028
|
var CACHE_CONTROL = "public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400";
|
|
1014
1029
|
var PARTIAL_CACHE_CONTROL = "no-store";
|
|
1015
1030
|
var SAFE_HEADERS = { "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox" };
|
|
1031
|
+
var PRIVATE_HEADERS = { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer", "X-Robots-Tag": "noindex, nofollow" };
|
|
1016
1032
|
var empty = (status, headers = {}) => new Response(null, { status, headers: { "Cache-Control": "no-store", ...headers } });
|
|
1017
1033
|
var isSwitchedOff3 = (value) => ["0", "false"].includes(value?.trim().toLowerCase() ?? "");
|
|
1034
|
+
var THEME_CACHE_CONTROL = "public, max-age=31536000, immutable";
|
|
1035
|
+
var OUTDATED_THEME_CACHE_CONTROL = "public, max-age=60";
|
|
1036
|
+
var THEME_SCOPE_HEADER = "cp-theme-scope";
|
|
1037
|
+
var MAX_THEME_BYTES = 256 * 1024;
|
|
1038
|
+
var sha256Hex = async (text) => Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text))), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1039
|
+
async function serveTheme(client, scope, hash, timeoutMs, head) {
|
|
1040
|
+
try {
|
|
1041
|
+
const upstream = await client.theme({ timeoutMs });
|
|
1042
|
+
const type = upstream.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
|
|
1043
|
+
if (upstream.status === 404) return empty(await isNotFoundResponse(upstream) ? 404 : 502);
|
|
1044
|
+
if (upstream.status !== 200 || type !== "text/css") {
|
|
1045
|
+
void upstream.body?.cancel().catch(() => {
|
|
1046
|
+
});
|
|
1047
|
+
return empty(502);
|
|
1048
|
+
}
|
|
1049
|
+
const css = await readCapped(upstream, MAX_THEME_BYTES);
|
|
1050
|
+
const current = await sha256Hex(css);
|
|
1051
|
+
if (current !== hash && upstream.headers.get(THEME_SCOPE_HEADER) !== scope) return empty(404);
|
|
1052
|
+
const cache = current === hash ? THEME_CACHE_CONTROL : OUTDATED_THEME_CACHE_CONTROL;
|
|
1053
|
+
const headers = { "Content-Type": "text/css; charset=utf-8", "Cache-Control": cache, ETag: `"${current}"`, ...SAFE_HEADERS };
|
|
1054
|
+
return new Response(head ? null : css, { status: 200, headers });
|
|
1055
|
+
} catch {
|
|
1056
|
+
return empty(502);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
function protectedAccessFor(req, pageId) {
|
|
1060
|
+
if (!PROTECTED_ID.test(pageId)) return void 0;
|
|
1061
|
+
const name = protectedCookieName(pageId);
|
|
1062
|
+
const password = req.headers.get("cookie")?.split(";").map((c) => c.trim()).find((c) => c.startsWith(`${name}=`))?.slice(name.length + 1);
|
|
1063
|
+
return password !== void 0 && PROTECTED_PASSWORD.test(password) ? { pageId, password } : void 0;
|
|
1064
|
+
}
|
|
1018
1065
|
function capped(body, max) {
|
|
1019
1066
|
let seen = 0;
|
|
1020
1067
|
return body.pipeThrough(
|
|
@@ -1031,18 +1078,33 @@ function createAssetRoute(options) {
|
|
|
1031
1078
|
let client;
|
|
1032
1079
|
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
1033
1080
|
async function serve(req, ctx, head) {
|
|
1081
|
+
const query = new URL(req.url).searchParams;
|
|
1082
|
+
const previewToken = query.get(PREVIEW_PARAM);
|
|
1083
|
+
const pageId = query.get(PROTECTED_PARAM);
|
|
1084
|
+
const res = await relay(req, ctx, head, previewToken, pageId);
|
|
1085
|
+
if (previewToken !== null || pageId !== null) for (const [name, value] of Object.entries(PRIVATE_HEADERS)) res.headers.set(name, value);
|
|
1086
|
+
return res;
|
|
1087
|
+
}
|
|
1088
|
+
async function relay(req, ctx, head, previewToken, pageId) {
|
|
1034
1089
|
if (isSwitchedOff3((options.env ?? process.env).HYLE_CONTENT_ENABLED)) return empty(404);
|
|
1035
1090
|
const file = (await ctx.params).file;
|
|
1036
|
-
|
|
1091
|
+
const [, themeScope, themeHash] = typeof file === "string" ? THEME_FILE.exec(file) ?? [] : [];
|
|
1092
|
+
if (typeof file !== "string" || !themeHash && !ASSET_FILE.test(file)) return empty(404);
|
|
1093
|
+
if (previewToken !== null && !PREVIEW_TOKEN.test(previewToken)) return empty(404);
|
|
1094
|
+
const protectedAccess = pageId === null ? void 0 : protectedAccessFor(req, pageId);
|
|
1095
|
+
if (pageId !== null && protectedAccess === void 0) return empty(404);
|
|
1037
1096
|
try {
|
|
1038
1097
|
client ??= createHyleClient({ baseUrl: options.baseUrl?.trim() ?? "", token: options.token?.trim() ?? "", ...options.fetch ? { fetch: options.fetch } : {} });
|
|
1039
1098
|
} catch {
|
|
1040
1099
|
return empty(404);
|
|
1041
1100
|
}
|
|
1101
|
+
if (themeScope && themeHash) return serveTheme(client, themeScope, themeHash, options.timeoutMs ?? DEFAULT_TIMEOUT_MS2, head);
|
|
1042
1102
|
let upstream;
|
|
1043
1103
|
try {
|
|
1044
1104
|
upstream = await client.asset(file, {
|
|
1045
1105
|
timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS2,
|
|
1106
|
+
...previewToken === null ? {} : { previewToken },
|
|
1107
|
+
...protectedAccess === void 0 ? {} : { protectedAccess },
|
|
1046
1108
|
headers: { range: req.headers.get("range"), "if-range": req.headers.get("if-range"), "if-none-match": req.headers.get("if-none-match") }
|
|
1047
1109
|
});
|
|
1048
1110
|
} catch {
|
|
@@ -1055,10 +1117,16 @@ function createAssetRoute(options) {
|
|
|
1055
1117
|
drop();
|
|
1056
1118
|
return new Response(null, { status: 304, headers: { "Cache-Control": CACHE_CONTROL, Vary: "Range", ...SAFE_HEADERS, ...etag ? { ETag: etag } : {} } });
|
|
1057
1119
|
}
|
|
1058
|
-
if (upstream.status === 404
|
|
1120
|
+
if (upstream.status === 404) return empty(await isNotFoundResponse(upstream) ? 404 : 502);
|
|
1121
|
+
if (upstream.status === 416) {
|
|
1059
1122
|
drop();
|
|
1060
|
-
const range = upstream.
|
|
1061
|
-
return empty(
|
|
1123
|
+
const range = upstream.headers.get("content-range");
|
|
1124
|
+
return empty(416, range ? { "Content-Range": range } : {});
|
|
1125
|
+
}
|
|
1126
|
+
if (upstream.status === 429 && (previewToken !== null || pageId !== null)) {
|
|
1127
|
+
drop();
|
|
1128
|
+
const wait = upstream.headers.get("retry-after");
|
|
1129
|
+
return empty(429, wait ? { "Retry-After": wait } : {});
|
|
1062
1130
|
}
|
|
1063
1131
|
const type = upstream.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
|
|
1064
1132
|
const length = Number(upstream.headers.get("content-length"));
|
|
@@ -1107,7 +1175,7 @@ function createOgImageRoute(options) {
|
|
|
1107
1175
|
if (result.kind !== "article") return new Response(null, { status: 404 });
|
|
1108
1176
|
title = result.publication.title;
|
|
1109
1177
|
} catch (error) {
|
|
1110
|
-
return new Response(null, { status:
|
|
1178
|
+
return new Response(null, { status: isNotFoundError(error) ? 404 : 502 });
|
|
1111
1179
|
}
|
|
1112
1180
|
const chars = [...title];
|
|
1113
1181
|
const shown = chars.length > MAX_TITLE ? `${chars.slice(0, MAX_TITLE - 1).join("")}\u2026` : title;
|
|
@@ -1122,6 +1190,107 @@ function createOgImageRoute(options) {
|
|
|
1122
1190
|
}
|
|
1123
1191
|
};
|
|
1124
1192
|
}
|
|
1193
|
+
|
|
1194
|
+
// src/next/preview.tsx
|
|
1195
|
+
import { notFound as notFound3 } from "next/navigation";
|
|
1196
|
+
import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
1197
|
+
function drawArticle(article, options) {
|
|
1198
|
+
const { locale } = article.publication;
|
|
1199
|
+
const toHref = (sitePath, linkLocale = locale) => (options.localePrefix?.(linkLocale) ?? "") + sitePath;
|
|
1200
|
+
const props = { article, components: options.components, slots: options.slots, locale, toHref };
|
|
1201
|
+
return options.layout === "docs" ? /* @__PURE__ */ jsx4(DocsPage, { ...props }) : /* @__PURE__ */ jsx4(ArticlePage, { ...props });
|
|
1202
|
+
}
|
|
1203
|
+
var hylePreviewMetadata = { robots: { index: false, follow: false }, referrer: "no-referrer" };
|
|
1204
|
+
var BANNER = {
|
|
1205
|
+
position: "sticky",
|
|
1206
|
+
top: 0,
|
|
1207
|
+
zIndex: 2147483647,
|
|
1208
|
+
padding: "8px 16px",
|
|
1209
|
+
background: "#fde047",
|
|
1210
|
+
color: "#1a1a1a",
|
|
1211
|
+
font: "600 14px/1.4 system-ui, sans-serif",
|
|
1212
|
+
textAlign: "center"
|
|
1213
|
+
};
|
|
1214
|
+
function hylePreviewPage(options) {
|
|
1215
|
+
return async function HylePreviewPage({ searchParams }) {
|
|
1216
|
+
const { token } = await searchParams ?? {};
|
|
1217
|
+
const result = await createHyleClient(options).preview(typeof token === "string" ? token : void 0);
|
|
1218
|
+
if (result.kind !== "preview") notFound3();
|
|
1219
|
+
return /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
1220
|
+
/* @__PURE__ */ jsx4("meta", { name: "robots", content: "noindex, nofollow" }),
|
|
1221
|
+
/* @__PURE__ */ jsx4("meta", { name: "referrer", content: "no-referrer" }),
|
|
1222
|
+
/* @__PURE__ */ jsx4("div", { role: "status", style: BANNER, children: "Unpublished draft preview" }),
|
|
1223
|
+
drawArticle(result.article, options)
|
|
1224
|
+
] });
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
// src/next/protected.tsx
|
|
1229
|
+
import { cookies } from "next/headers";
|
|
1230
|
+
import { notFound as notFound4 } from "next/navigation";
|
|
1231
|
+
import { Fragment as Fragment2, jsx as jsx5, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
1232
|
+
var hyleProtectedMetadata = { robots: { index: false, follow: false }, referrer: "same-origin" };
|
|
1233
|
+
var COOKIE_MAX_AGE_SECONDS = 12 * 60 * 60;
|
|
1234
|
+
var UNLOCK_MAX_BYTES = 1024;
|
|
1235
|
+
var PRIVATE_HEADERS2 = { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer", "X-Robots-Tag": "noindex, nofollow" };
|
|
1236
|
+
var PASSWORD_FIELD = "hyle-protected-password";
|
|
1237
|
+
var ERROR_ID = "hyle-protected-error";
|
|
1238
|
+
function PasswordForm({ pageId, failed }) {
|
|
1239
|
+
return /* @__PURE__ */ jsxs3("main", { children: [
|
|
1240
|
+
/* @__PURE__ */ jsx5("h1", { children: "Protected page" }),
|
|
1241
|
+
/* @__PURE__ */ jsxs3("form", { method: "post", action: `/p/${pageId}/unlock`, autoComplete: "off", children: [
|
|
1242
|
+
/* @__PURE__ */ jsx5("label", { htmlFor: PASSWORD_FIELD, children: "Password" }),
|
|
1243
|
+
/* @__PURE__ */ jsx5(
|
|
1244
|
+
"input",
|
|
1245
|
+
{
|
|
1246
|
+
id: PASSWORD_FIELD,
|
|
1247
|
+
name: "password",
|
|
1248
|
+
type: "password",
|
|
1249
|
+
autoComplete: "off",
|
|
1250
|
+
spellCheck: false,
|
|
1251
|
+
required: true,
|
|
1252
|
+
pattern: "[A-Za-z0-9]{16}",
|
|
1253
|
+
title: "16 letters or digits",
|
|
1254
|
+
"aria-invalid": failed || void 0,
|
|
1255
|
+
"aria-describedby": failed ? ERROR_ID : void 0
|
|
1256
|
+
}
|
|
1257
|
+
),
|
|
1258
|
+
/* @__PURE__ */ jsx5("button", { type: "submit", children: "Open" }),
|
|
1259
|
+
failed ? /* @__PURE__ */ jsx5("p", { id: ERROR_ID, role: "alert", children: "Wrong or expired password." }) : null
|
|
1260
|
+
] })
|
|
1261
|
+
] });
|
|
1262
|
+
}
|
|
1263
|
+
function hyleProtectedPage(options) {
|
|
1264
|
+
return async function HyleProtectedPage({ params }) {
|
|
1265
|
+
const { id } = await params;
|
|
1266
|
+
if (typeof id !== "string" || !PROTECTED_ID.test(id)) notFound4();
|
|
1267
|
+
const password = (await cookies()).get(protectedCookieName(id))?.value;
|
|
1268
|
+
const result = password === void 0 ? void 0 : await createHyleClient(options).protected(id, password);
|
|
1269
|
+
if (result?.kind === "disabled") notFound4();
|
|
1270
|
+
return /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
|
1271
|
+
/* @__PURE__ */ jsx5("meta", { name: "robots", content: "noindex, nofollow" }),
|
|
1272
|
+
/* @__PURE__ */ jsx5("meta", { name: "referrer", content: "same-origin" }),
|
|
1273
|
+
result?.kind === "protected" ? drawArticle(result.article, options) : /* @__PURE__ */ jsx5(PasswordForm, { pageId: id, failed: result !== void 0 })
|
|
1274
|
+
] });
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
function createProtectedUnlockRoute() {
|
|
1278
|
+
return {
|
|
1279
|
+
async POST(req, ctx) {
|
|
1280
|
+
const { id } = await ctx.params;
|
|
1281
|
+
if (typeof id !== "string" || !PROTECTED_ID.test(id)) return new Response(null, { status: 404, headers: PRIVATE_HEADERS2 });
|
|
1282
|
+
const site = req.headers.get("sec-fetch-site");
|
|
1283
|
+
if (site === null ? originMismatch(req) : site !== "same-origin") return new Response(null, { status: 403, headers: PRIVATE_HEADERS2 });
|
|
1284
|
+
if (!/^application\/x-www-form-urlencoded(\s*;|$)/i.test(req.headers.get("content-type") ?? "")) return new Response(null, { status: 415, headers: PRIVATE_HEADERS2 });
|
|
1285
|
+
if (Number(req.headers.get("content-length")) > UNLOCK_MAX_BYTES) return new Response(null, { status: 413, headers: PRIVATE_HEADERS2 });
|
|
1286
|
+
const form = await readCapped(new Response(req.body), UNLOCK_MAX_BYTES).catch(() => void 0);
|
|
1287
|
+
if (form === void 0) return new Response(null, { status: 413, headers: PRIVATE_HEADERS2 });
|
|
1288
|
+
const password = new URLSearchParams(form).get("password") ?? "";
|
|
1289
|
+
const cookie = PROTECTED_PASSWORD.test(password) ? { "Set-Cookie": `${protectedCookieName(id)}=${password}; Max-Age=${COOKIE_MAX_AGE_SECONDS}; Path=/; HttpOnly; Secure; SameSite=Lax` } : {};
|
|
1290
|
+
return new Response(null, { status: 303, headers: { ...PRIVATE_HEADERS2, Location: `/p/${id}`, ...cookie } });
|
|
1291
|
+
}
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1125
1294
|
export {
|
|
1126
1295
|
ENGAGE_CSRF_HEADER,
|
|
1127
1296
|
HyleArticle,
|
|
@@ -1134,12 +1303,17 @@ export {
|
|
|
1134
1303
|
createLlmsTxtRoute,
|
|
1135
1304
|
createMarkdownRoute,
|
|
1136
1305
|
createOgImageRoute,
|
|
1306
|
+
createProtectedUnlockRoute,
|
|
1137
1307
|
createRevalidateRoute,
|
|
1138
1308
|
fetchResolver,
|
|
1139
1309
|
hyleFeed,
|
|
1140
1310
|
hyleFeedLink,
|
|
1141
1311
|
hyleMetadata,
|
|
1142
1312
|
hylePage,
|
|
1313
|
+
hylePreviewMetadata,
|
|
1314
|
+
hylePreviewPage,
|
|
1315
|
+
hyleProtectedMetadata,
|
|
1316
|
+
hyleProtectedPage,
|
|
1143
1317
|
hyleRootMetadata,
|
|
1144
1318
|
hyleRootPage,
|
|
1145
1319
|
hyleRootSitemap,
|