@blogmcp/sdk 0.8.1 → 0.9.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/community/channels.d.ts +12 -0
- package/dist/community/channels.js +7 -0
- package/dist/community/client.d.ts +211 -0
- package/dist/community/client.js +188 -0
- package/dist/community/index.d.ts +34 -0
- package/dist/community/index.js +92 -0
- package/dist/community/links.d.ts +65 -0
- package/dist/community/links.js +81 -0
- package/dist/community/messages.cs.d.ts +331 -0
- package/dist/community/messages.cs.js +342 -0
- package/dist/community/messages.d.ts +40 -0
- package/dist/community/messages.en.d.ts +315 -0
- package/dist/community/messages.en.js +313 -0
- package/dist/community/messages.js +66 -0
- package/dist/community/outcome.d.ts +96 -0
- package/dist/community/outcome.js +211 -0
- package/dist/community/poll.d.ts +35 -0
- package/dist/community/poll.js +70 -0
- package/dist/community/routes.d.ts +110 -0
- package/dist/community/routes.js +66 -0
- package/dist/community/server/bff.d.ts +123 -0
- package/dist/community/server/bff.js +214 -0
- package/dist/community/server/env.d.ts +20 -0
- package/dist/community/server/env.js +16 -0
- package/dist/community/server/host-routes.d.ts +15 -0
- package/dist/community/server/host-routes.js +35 -0
- package/dist/community/server/http.d.ts +64 -0
- package/dist/community/server/http.js +90 -0
- package/dist/community/server/index.d.ts +45 -0
- package/dist/community/server/index.js +19 -0
- package/dist/community/server/route-decision.d.ts +70 -0
- package/dist/community/server/route-decision.js +89 -0
- package/dist/community/server/unknown-path.d.ts +21 -0
- package/dist/community/server/unknown-path.js +11 -0
- package/dist/community/shapes.d.ts +457 -0
- package/dist/community/shapes.js +51 -0
- package/dist/community/store.d.ts +183 -0
- package/dist/community/store.js +423 -0
- package/dist/community/transport.d.ts +42 -0
- package/dist/community/transport.js +59 -0
- package/dist/core/client.d.ts +17 -0
- package/dist/core/client.js +58 -55
- package/dist/core/comments-legacy.d.ts +19 -0
- package/dist/core/comments-legacy.js +18 -0
- package/dist/core/index.d.ts +3 -0
- package/dist/core/index.js +107 -101
- package/dist/core/queries.d.ts +46 -0
- package/dist/core/queries.js +128 -84
- package/dist/core/seo.js +40 -39
- package/dist/core/translations/cs.json.js +6 -6
- package/dist/core/translations/en.json.js +1 -1
- package/dist/core/types.d.ts +23 -1
- package/dist/core/view-model.d.ts +7 -0
- package/dist/core/view-model.js +29 -25
- package/dist/react/KbMdxContent.js +4 -3
- package/dist/vue/components/KbMdxContent.vue2.js +3 -2
- package/dist/vue/composables/useKbComments.d.ts +8 -0
- package/package.json +13 -4
- package/src/community/channels.ts +12 -0
- package/src/community/client.ts +437 -0
- package/src/community/index.ts +190 -0
- package/src/community/links.ts +166 -0
- package/src/community/messages.cs.ts +371 -0
- package/src/community/messages.en.ts +341 -0
- package/src/community/messages.ts +112 -0
- package/src/community/outcome.ts +317 -0
- package/src/community/poll.ts +154 -0
- package/src/community/routes.ts +139 -0
- package/src/community/server/bff.ts +530 -0
- package/src/community/server/env.ts +42 -0
- package/src/community/server/host-routes.ts +48 -0
- package/src/community/server/http.ts +175 -0
- package/src/community/server/index.ts +55 -0
- package/src/community/server/route-decision.ts +226 -0
- package/src/community/server/unknown-path.ts +32 -0
- package/src/community/shapes.ts +513 -0
- package/src/community/store.ts +786 -0
- package/src/community/transport.ts +111 -0
- package/src/core/client.ts +24 -1
- package/src/core/comments-legacy.ts +39 -0
- package/src/core/index.ts +8 -0
- package/src/core/queries.ts +93 -0
- package/src/core/seo.ts +5 -1
- package/src/core/translations/cs.json +3 -1
- package/src/core/translations/en.json +3 -1
- package/src/core/types.ts +19 -1
- package/src/core/view-model.ts +12 -1
- package/src/vue/composables/useKbComments.ts +8 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createCommunityBff` — the server half a headless site puts in front of kb-api's community routes
|
|
3
|
+
* (docs/internal/m1-contracts.md §10.3, §3.4; community.md §3.7, §7.1, invariant 3). The page's community client
|
|
4
|
+
* POSTs to the site's own `basePath` (`/api/community`); the BFF forwards kb-api's host routes (host-routes.ts)
|
|
5
|
+
* with the host's member token, or the site's notice key for a visitor without a session, and answers everything
|
|
6
|
+
* else itself.
|
|
7
|
+
*
|
|
8
|
+
* The notice key (`community:notice`) and every member token live in this closure only: each goes into the
|
|
9
|
+
* `Authorization` header of its own upstream call and nowhere else — never into an answer, a log line or an
|
|
10
|
+
* error. Server-only: the factory throws when a global `window` exists. Runtime-neutral otherwise (Web `fetch`,
|
|
11
|
+
* `URL`, `AbortSignal.timeout`, no `node:*`): Nitro on Bun or Node, Next, React Router, a worker.
|
|
12
|
+
*
|
|
13
|
+
* Every request, in this order; a step that refuses answers alone and nothing after it runs:
|
|
14
|
+
* 1. the method: POST only. Anything else gets kb-api's uniform unknown-path answer, as kb-api itself and the
|
|
15
|
+
* blogs' proxies answer it.
|
|
16
|
+
* 2. the path: `<basePath>/v1/public/…` names kb-api's `/community/v1/public/…`, `<basePath>/trust/v1/notices/…`
|
|
17
|
+
* its `/trust/v1/notices/…`; `communityRouteDecision` judges that path in every spelling. Anything but
|
|
18
|
+
* `allow` (outside the base or the namespaces, a closed surface, an unlisted route, a strange spelling) gets
|
|
19
|
+
* the unknown-path answer byte for byte, and kb-api is not asked.
|
|
20
|
+
* 3. the Origin: exactly one of the site URLs' origins, `Sec-Fetch-Site: same-origin` when sent (the
|
|
21
|
+
* consultations' `isAllowedOrigin`): else 403 ORIGIN. The host's session is not looked at before this.
|
|
22
|
+
* 4. the body (`http.json()`): JSON only, at most 64 KB, parsed to validate and forwarded as the raw bytes:
|
|
23
|
+
* else 413 TOO_LARGE or 400 INVALID_JSON.
|
|
24
|
+
* 5. the upstream call: `POST <kbApiUrl><route>` with `Authorization: Bearer <member token>` when the host has
|
|
25
|
+
* a session (`memberToken`); else `Bearer <notice key>` on every allowed route — public and notice — when the
|
|
26
|
+
* site configured one; else no `Authorization` (a notice route then answers 503: the form needs the key or a
|
|
27
|
+
* session). Besides it the visitor's IP in `X-Blogmcp-Client-Ip` (`clientIpFrom`) and `Content-Type` —
|
|
28
|
+
* nothing else, never a cookie; `redirect: 'error'` (a redirect would carry the credential elsewhere),
|
|
29
|
+
* `cache: 'no-store'`, a timeout.
|
|
30
|
+
*
|
|
31
|
+
* Why the notice key travels on anonymous public calls too: kb-api takes the forwarded IP only behind a credential
|
|
32
|
+
* it trusts for that (a delegated member token, the site's verified `community:notice` key; m1-contracts.md §7.2,
|
|
33
|
+
* §7.14 P-RV5). Without the key an anonymous call (a public read without a session) is counted on the site
|
|
34
|
+
* server's own IP, and every anonymous visitor of the site shares one bucket of kb-api's public limiter. The key
|
|
35
|
+
* authorises nothing there: outside the notice routes kb-api treats a request that carries it exactly as an
|
|
36
|
+
* anonymous one (a scoped key leaves the caller anonymous — a write or a `me/*` call still answers UNAUTHORIZED,
|
|
37
|
+
* which the page reads as "sign in"); it only lets kb-api count the visitor by their own IP. A 401 is therefore
|
|
38
|
+
* the key's refusal on a notice route only.
|
|
39
|
+
*
|
|
40
|
+
* kb-api's status and body pass through, with the BFF's own headers (`Cache-Control: private, no-store`,
|
|
41
|
+
* `Referrer-Policy: same-origin`, `X-Robots-Tag: noindex`, `X-Frame-Options: DENY`, nosniff, JSON) and kb-api's
|
|
42
|
+
* `Retry-After`. The BFF's own refusals are kb-api's envelope `{ ok: false, code, message }`. UNAVAILABLE (503)
|
|
43
|
+
* is everything the visitor cannot fix: no answer or one that is not kb-api's JSON object, an answer that quotes
|
|
44
|
+
* the call's own credential, the site's notice key missing or refused, a member token the host returned that
|
|
45
|
+
* cannot be one, anything unexpected — logged by operation, code and status only; `handle` never throws.
|
|
46
|
+
*/
|
|
47
|
+
import { type CommunityHttp, type CommunityResponse } from './http.js';
|
|
48
|
+
/**
|
|
49
|
+
* What the BFF tells its logger: an outage or the unexpected. Never a body, a name, a token or an address. A
|
|
50
|
+
* subtype of the client's log event, so the page's `CommunityLogger` serves both.
|
|
51
|
+
*/
|
|
52
|
+
export interface CommunityBffLogEvent {
|
|
53
|
+
/** The operation (`messages/post`, `notices/submit`), or `bff` before one is known. */
|
|
54
|
+
op: string;
|
|
55
|
+
/** The client's code for every outage the BFF logs. */
|
|
56
|
+
outcome: 'unavailable';
|
|
57
|
+
/** kb-api's code when its answer named one (a code shape, else `other`), else null. */
|
|
58
|
+
kbCode: string | null;
|
|
59
|
+
/** kb-api's HTTP status, or null when there was no answer. */
|
|
60
|
+
status: number | null;
|
|
61
|
+
}
|
|
62
|
+
export interface CommunityBffConfig {
|
|
63
|
+
/**
|
|
64
|
+
* The server-side kb-api base URL the routes are under: kb-api's own origin inside the cluster, or the instance's
|
|
65
|
+
* public address WITH the `/api` prefix it serves kb-api under (`https://tenant.example/api` — a bare
|
|
66
|
+
* `https://tenant.example` reaches the blog, not kb-api).
|
|
67
|
+
*/
|
|
68
|
+
kbApiUrl: string;
|
|
69
|
+
/** The site's own URLs: their origins are the Origin allowlist. */
|
|
70
|
+
siteUrls: string[];
|
|
71
|
+
/** Origins allowed to POST besides the site URLs' own. */
|
|
72
|
+
allowedOrigins?: string[];
|
|
73
|
+
/**
|
|
74
|
+
* The host's session → the member's token for kb-api (the bare JWT, no `Bearer `), or null without a session:
|
|
75
|
+
* the storefront's delegated identity, a native blog's own session. Called once per forwarded request, only
|
|
76
|
+
* after the path, the Origin and the body passed. Throwing, or a value that cannot be a token: UNAVAILABLE.
|
|
77
|
+
*/
|
|
78
|
+
memberToken: (http: CommunityHttp) => Promise<string | null>;
|
|
79
|
+
/**
|
|
80
|
+
* The site's `community:notice` key, sent for a visitor without a session on every allowed route. On the notice
|
|
81
|
+
* routes it is the anonymous notifier's credential; on the public routes it authorises nothing (kb-api treats
|
|
82
|
+
* the call as anonymous) and only lets kb-api's limiter count each visitor by the IP the BFF forwards, instead
|
|
83
|
+
* of every anonymous visitor of the site sharing the site server's one bucket. Missing: the notice routes are
|
|
84
|
+
* UNAVAILABLE without a session, and anonymous public calls share that bucket.
|
|
85
|
+
*/
|
|
86
|
+
noticeKey?: string | null;
|
|
87
|
+
/**
|
|
88
|
+
* Where the page's client sends its requests — its `base` (`createCommunityClient`), the elements' `api-base` —
|
|
89
|
+
* in the grammar the client takes (`isRelativeCommunityBase`): an absolute path of plain segments, `/` the root.
|
|
90
|
+
* Default `/api/community`.
|
|
91
|
+
*/
|
|
92
|
+
basePath?: string;
|
|
93
|
+
/**
|
|
94
|
+
* The edge header holding the visitor's IP. Default `cf-connecting-ip`; `''` = none (not behind Cloudflare). kb-api
|
|
95
|
+
* uses the forwarded IP behind a delegated member token or the site's notice key only (see the module doc) — so
|
|
96
|
+
* for anonymous visitors, only when `noticeKey` is configured.
|
|
97
|
+
*/
|
|
98
|
+
clientIpHeader?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Per kb-api request, in milliseconds (default 10 000): 1 … 2 147 483 647, the range a timer keeps; a fraction is
|
|
101
|
+
* rounded down. Anything else is the default, warned once.
|
|
102
|
+
*/
|
|
103
|
+
timeoutMs?: number;
|
|
104
|
+
/** The fetch for upstream calls, resolved per call (a test spy or a framework-patched global fetch is honoured). */
|
|
105
|
+
fetch?: () => typeof fetch;
|
|
106
|
+
/** The clock (an HTTP-date `Retry-After`). */
|
|
107
|
+
now?: () => number;
|
|
108
|
+
/** Gets outages and the unexpected only (the client's `CommunityLogger` fits); default one `console.warn` line. */
|
|
109
|
+
logger?: (e: CommunityBffLogEvent) => void;
|
|
110
|
+
}
|
|
111
|
+
export interface CommunityBff {
|
|
112
|
+
/** One request of the page's client: its answer, never a throw. */
|
|
113
|
+
handle(http: CommunityHttp): Promise<CommunityResponse>;
|
|
114
|
+
}
|
|
115
|
+
/** The BFF's own refusals (kb-api's envelope); the client maps them like kb-api's codes. */
|
|
116
|
+
export type CommunityBffCode = 'ORIGIN' | 'TOO_LARGE' | 'INVALID_JSON' | 'UNAVAILABLE';
|
|
117
|
+
/** Each refusal's status and its one fixed message (English, constant, nothing of the request). */
|
|
118
|
+
export declare const COMMUNITY_BFF_REFUSALS: Readonly<Record<CommunityBffCode, {
|
|
119
|
+
readonly status: number;
|
|
120
|
+
readonly message: string;
|
|
121
|
+
}>>;
|
|
122
|
+
export declare const DEFAULT_COMMUNITY_BASE_PATH = "/api/community";
|
|
123
|
+
export declare function createCommunityBff(c: CommunityBffConfig): CommunityBff;
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { assertServerOnly as $ } from "../../core/server-only.js";
|
|
2
|
+
import { DEFAULT_CLIENT_IP_HEADER as H, isHeaderName as F, clientIpFrom as z, KB_CLIENT_IP_HEADER as J } from "../../consultations/server/client-ip.js";
|
|
3
|
+
import { originOf as Y, siteOrigins as x, isAllowedOrigin as G } from "../../consultations/server/origin.js";
|
|
4
|
+
import { isBytes as q, COMMUNITY_BODY_MAX_BYTES as X, parseCommunityJson as K, isJsonContentType as V } from "./http.js";
|
|
5
|
+
import { communityRouteDecision as W } from "./route-decision.js";
|
|
6
|
+
import { communityUnknownPath as E } from "./unknown-path.js";
|
|
7
|
+
const Z = Object.freeze({
|
|
8
|
+
ORIGIN: Object.freeze({ status: 403, message: "This request did not come from the site" }),
|
|
9
|
+
TOO_LARGE: Object.freeze({ status: 413, message: "The request body is too large" }),
|
|
10
|
+
INVALID_JSON: Object.freeze({ status: 400, message: "The request body must be a JSON object" }),
|
|
11
|
+
// kb-api's own message for its UNAVAILABLE.
|
|
12
|
+
UNAVAILABLE: Object.freeze({ status: 503, message: "Temporarily unavailable" })
|
|
13
|
+
}), Q = Object.freeze({
|
|
14
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
15
|
+
"Cache-Control": "private, no-store",
|
|
16
|
+
"Referrer-Policy": "same-origin",
|
|
17
|
+
"X-Robots-Tag": "noindex",
|
|
18
|
+
"X-Frame-Options": "DENY",
|
|
19
|
+
"X-Content-Type-Options": "nosniff"
|
|
20
|
+
}), ee = "/api/community", I = 1e4, te = 2147483647, C = /^[\x21-\x7E]+$/, L = 8192, ne = /^(?:\/[A-Za-z0-9_~-][A-Za-z0-9._~-]*)*\/?$/, oe = /^[A-Z][A-Z0-9_]{0,63}$/, re = "/trust/v1/notices/";
|
|
21
|
+
function se(t) {
|
|
22
|
+
if (t === void 0) return ee;
|
|
23
|
+
if (typeof t != "string" || !t.startsWith("/") || !ne.test(t))
|
|
24
|
+
throw new TypeError(
|
|
25
|
+
"[blogmcp-sdk] createCommunityBff basePath must be an absolute path of plain segments, like /api/community"
|
|
26
|
+
);
|
|
27
|
+
return t.replace(/\/$/, "");
|
|
28
|
+
}
|
|
29
|
+
function ie(t) {
|
|
30
|
+
if (typeof t != "string" || t.trim() === "") return null;
|
|
31
|
+
try {
|
|
32
|
+
const e = new URL(t.trim());
|
|
33
|
+
return e.protocol !== "http:" && e.protocol !== "https:" || e.search || e.hash ? null : e.origin + e.pathname.replace(/\/+$/, "");
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function ae(t, e) {
|
|
39
|
+
if (typeof t != "string") return null;
|
|
40
|
+
const r = t.trim();
|
|
41
|
+
return r === "" ? null : !C.test(r) || r.length > L ? (e("notice_key_invalid", "[community] the notice key contains characters not allowed in an HTTP header — ignored."), null) : r;
|
|
42
|
+
}
|
|
43
|
+
function ce(t, e) {
|
|
44
|
+
if (t === void 0) return I;
|
|
45
|
+
const r = typeof t == "number" ? Math.floor(t) : Number.NaN;
|
|
46
|
+
return r >= 1 && r <= te ? r : (e("timeout_ms", "[community] timeoutMs is not a number of milliseconds from 1 to 2147483647 — the default 10 s is used."), I);
|
|
47
|
+
}
|
|
48
|
+
function ue(t) {
|
|
49
|
+
return (e) => {
|
|
50
|
+
const r = typeof e.kbCode == "string" ? oe.test(e.kbCode) ? e.kbCode : "other" : null, u = typeof e.status == "number" && Number.isInteger(e.status) ? e.status : null;
|
|
51
|
+
try {
|
|
52
|
+
t({ op: String(e.op).replace(/[^A-Za-z0-9_./-]/g, "").slice(0, 64), outcome: e.outcome, kbCode: r, status: u });
|
|
53
|
+
} catch {
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const le = (t) => {
|
|
58
|
+
console.warn(`[community] op=${t.op} outcome=${t.outcome} kb_code=${t.kbCode ?? "none"} status=${t.status ?? "none"}`);
|
|
59
|
+
};
|
|
60
|
+
function me(t, e) {
|
|
61
|
+
if (typeof e != "string") return null;
|
|
62
|
+
const r = e.search(/[?#]/), u = r === -1 ? e : e.slice(0, r);
|
|
63
|
+
if (!u.startsWith(`${t}/`)) return null;
|
|
64
|
+
const s = u.slice(t.length);
|
|
65
|
+
return s.startsWith("/trust/") ? s : `/community${s}`;
|
|
66
|
+
}
|
|
67
|
+
function fe(t) {
|
|
68
|
+
return t.replace(/^\/community\/v1\/public\//, "").replace(/^\/trust\/v1\//, "");
|
|
69
|
+
}
|
|
70
|
+
function ye(t) {
|
|
71
|
+
try {
|
|
72
|
+
const e = JSON.parse(t);
|
|
73
|
+
return typeof e == "object" && e !== null && !Array.isArray(e) ? e : null;
|
|
74
|
+
} catch {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function N(t) {
|
|
79
|
+
return typeof t.code == "string" ? t.code : null;
|
|
80
|
+
}
|
|
81
|
+
function de(t, e) {
|
|
82
|
+
const r = t.get("retry-after")?.trim();
|
|
83
|
+
if (!r) return null;
|
|
84
|
+
if (/^\d{1,9}$/.test(r)) return Number(r);
|
|
85
|
+
const u = Date.parse(r);
|
|
86
|
+
return Number.isNaN(u) ? null : Math.max(0, Math.ceil((u - e) / 1e3));
|
|
87
|
+
}
|
|
88
|
+
function R(t, e, r = {}) {
|
|
89
|
+
return { status: t, headers: { ...Q, ...r }, body: e };
|
|
90
|
+
}
|
|
91
|
+
function g(t) {
|
|
92
|
+
const { status: e, message: r } = Z[t];
|
|
93
|
+
return R(e, JSON.stringify({ ok: !1, code: t, message: r }));
|
|
94
|
+
}
|
|
95
|
+
function _e(t) {
|
|
96
|
+
$(
|
|
97
|
+
"createCommunityBff",
|
|
98
|
+
"it holds the site's community notice key and its members' tokens, which must never reach the browser."
|
|
99
|
+
);
|
|
100
|
+
const e = t ?? {};
|
|
101
|
+
if (typeof e.memberToken != "function")
|
|
102
|
+
throw new TypeError("[blogmcp-sdk] createCommunityBff needs memberToken: (http) => Promise<string | null>");
|
|
103
|
+
const r = se(e.basePath), u = /* @__PURE__ */ new Set(), s = (n, o) => {
|
|
104
|
+
u.has(n) || (u.add(n), console.warn(o));
|
|
105
|
+
}, p = ie(e.kbApiUrl);
|
|
106
|
+
p === null && s("kb_api_url", "[community] kbApiUrl is not an absolute http(s) URL — every community request answers 503.");
|
|
107
|
+
const O = Array.isArray(e.allowedOrigins) ? e.allowedOrigins : [];
|
|
108
|
+
O.some((n) => Y(n) === null) && s("allowed_origins", "[community] an allowedOrigins entry is not an http(s) URL — ignored.");
|
|
109
|
+
const k = x(Array.isArray(e.siteUrls) ? e.siteUrls : [], O);
|
|
110
|
+
k.length === 0 && s("site_urls", "[community] no usable siteUrls — every community request is refused (ORIGIN).");
|
|
111
|
+
let h = typeof e.clientIpHeader == "string" ? e.clientIpHeader.trim() : H;
|
|
112
|
+
h !== "" && !F(h) && (s("client_ip_header", "[community] clientIpHeader is not an HTTP header name — no edge header is read."), h = "");
|
|
113
|
+
const _ = ae(e.noticeKey, s), U = ce(e.timeoutMs, s), S = typeof e.now == "function" ? e.now : Date.now, P = () => typeof e.fetch == "function" ? e.fetch() : globalThis.fetch, T = e.logger, A = ue(typeof T == "function" ? (n) => T.call(e, n) : le), M = e.memberToken, l = (n, o = null, i = null) => (A({ op: n, outcome: "unavailable", kbCode: o, status: i }), g("UNAVAILABLE"));
|
|
114
|
+
async function v(n) {
|
|
115
|
+
if (typeof n.json != "function")
|
|
116
|
+
return s(
|
|
117
|
+
"json_missing",
|
|
118
|
+
"[community] the request adapter has no json() — use the community server's fromWebRequest or add it."
|
|
119
|
+
), null;
|
|
120
|
+
let o;
|
|
121
|
+
try {
|
|
122
|
+
o = await n.json();
|
|
123
|
+
} catch {
|
|
124
|
+
return { ok: !1, code: "INVALID_JSON" };
|
|
125
|
+
}
|
|
126
|
+
if (!o || typeof o != "object" || o.ok !== !0)
|
|
127
|
+
return { ok: !1, code: !!o && typeof o == "object" && o.ok === !1 && o.code === "TOO_LARGE" ? "TOO_LARGE" : "INVALID_JSON" };
|
|
128
|
+
if (!q(o.bytes)) return { ok: !1, code: "INVALID_JSON" };
|
|
129
|
+
if (o.bytes.byteLength > X) return { ok: !1, code: "TOO_LARGE" };
|
|
130
|
+
const i = new Uint8Array(o.bytes), c = K(n.header("content-type"), i);
|
|
131
|
+
return c.ok ? { ok: !0, bytes: i } : c;
|
|
132
|
+
}
|
|
133
|
+
async function B(n, o) {
|
|
134
|
+
const i = await M(n);
|
|
135
|
+
return i != null && i !== "" ? typeof i == "string" && C.test(i) && i.length <= L ? { kind: "member", token: i } : (s(
|
|
136
|
+
"member_token_invalid",
|
|
137
|
+
'[community] memberToken returned something that is not a token (the bare JWT, visible ASCII, no "Bearer ") — answered 503.'
|
|
138
|
+
), { kind: "unavailable" }) : _ ? { kind: "notice", token: _ } : o ? (s(
|
|
139
|
+
"notice_key",
|
|
140
|
+
"[community] no notice key (COMMUNITY_NOTICE_KEY) — the notice form works only for signed-in members."
|
|
141
|
+
), { kind: "unavailable" }) : { kind: "anonymous" };
|
|
142
|
+
}
|
|
143
|
+
async function D(n, o, i, c, m, f) {
|
|
144
|
+
const d = { "Content-Type": "application/json" };
|
|
145
|
+
c.kind !== "anonymous" && (d.Authorization = `Bearer ${c.token}`), f && (d[J] = f);
|
|
146
|
+
let a;
|
|
147
|
+
try {
|
|
148
|
+
a = await P()(`${p}${o}`, {
|
|
149
|
+
method: "POST",
|
|
150
|
+
headers: d,
|
|
151
|
+
body: i,
|
|
152
|
+
redirect: "error",
|
|
153
|
+
cache: "no-store",
|
|
154
|
+
signal: AbortSignal.timeout(U)
|
|
155
|
+
});
|
|
156
|
+
} catch {
|
|
157
|
+
return l(n);
|
|
158
|
+
}
|
|
159
|
+
let y;
|
|
160
|
+
try {
|
|
161
|
+
y = await a.text();
|
|
162
|
+
} catch {
|
|
163
|
+
return l(n, null, a.status);
|
|
164
|
+
}
|
|
165
|
+
const b = V(a.headers.get("content-type")) ? ye(y) : null;
|
|
166
|
+
if (b === null) return l(n, null, a.status);
|
|
167
|
+
if (c.kind !== "anonymous" && y.includes(c.token))
|
|
168
|
+
return s("credential_echo", "[community] kb-api's answer quoted the request's credential — not passed on (503)."), l(n, null, a.status);
|
|
169
|
+
if (a.status === 401 && c.kind === "notice" && m)
|
|
170
|
+
return s(
|
|
171
|
+
"notice_key_refused",
|
|
172
|
+
"[community] kb-api refused the notice key — check COMMUNITY_NOTICE_KEY (scope community:notice)."
|
|
173
|
+
), l(n, N(b), a.status);
|
|
174
|
+
a.status >= 500 && A({ op: n, outcome: "unavailable", kbCode: N(b), status: a.status });
|
|
175
|
+
const w = de(a.headers, S());
|
|
176
|
+
return R(a.status, y, w === null ? {} : { "Retry-After": String(w) });
|
|
177
|
+
}
|
|
178
|
+
async function j(n, o) {
|
|
179
|
+
if ((typeof n.method == "string" ? n.method.toUpperCase() : "") !== "POST") return E();
|
|
180
|
+
const c = me(r, n.path), m = c === null ? null : W("POST", c);
|
|
181
|
+
if (m?.kind !== "allow") return E();
|
|
182
|
+
if (o.op = fe(m.upstreamPath), !G(n, k))
|
|
183
|
+
return s(
|
|
184
|
+
"origin",
|
|
185
|
+
"[community] a request was refused for its Origin (not one of siteUrls / allowedOrigins, or not same-origin) — if the site's own pages are refused, check siteUrls."
|
|
186
|
+
), g("ORIGIN");
|
|
187
|
+
const f = await v(n);
|
|
188
|
+
if (f === null) return l(o.op);
|
|
189
|
+
if (!f.ok) return g(f.code);
|
|
190
|
+
if (p === null) return l(o.op);
|
|
191
|
+
const d = m.upstreamPath.startsWith(re), a = await B(n, d);
|
|
192
|
+
if (a.kind === "unavailable") return l(o.op);
|
|
193
|
+
const y = z(n, { header: h });
|
|
194
|
+
return y === null && s(
|
|
195
|
+
"client_ip",
|
|
196
|
+
"[community] a request without a client IP (no valid edge header or X-Forwarded-For) — check clientIpHeader and the reverse-proxy configuration. kb-api counts a visitor by a forwarded IP only behind a delegated member token or the site's notice key; without one the call is counted on the site server's own IP."
|
|
197
|
+
), D(o.op, m.upstreamPath, f.bytes, a, d, y);
|
|
198
|
+
}
|
|
199
|
+
return {
|
|
200
|
+
async handle(n) {
|
|
201
|
+
const o = { op: "bff" };
|
|
202
|
+
try {
|
|
203
|
+
return await j(n, o);
|
|
204
|
+
} catch {
|
|
205
|
+
return l(o.op);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export {
|
|
211
|
+
Z as COMMUNITY_BFF_REFUSALS,
|
|
212
|
+
ee as DEFAULT_COMMUNITY_BASE_PATH,
|
|
213
|
+
_e as createCommunityBff
|
|
214
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The community BFF's key from the environment (docs/internal/m1-contracts.md §10.3, Appendix A) — one name for
|
|
3
|
+
* every headless host:
|
|
4
|
+
*
|
|
5
|
+
* {prefix}COMMUNITY_NOTICE_KEY the site's `community:notice` key: the notice form for a visitor without a session
|
|
6
|
+
*
|
|
7
|
+
* `prefix` is `''` for Next, React Router and a plain server, `'NUXT_'` for Nuxt (whose runtime config reads
|
|
8
|
+
* `NUXT_*`). The managed blogs read their own `KB_COMMUNITY_NOTICE_KEY`, which the platform provisions.
|
|
9
|
+
*
|
|
10
|
+
* A key is never PUBLIC: a variable in a namespace a framework bundles into page JavaScript —
|
|
11
|
+
* `NEXT_PUBLIC_COMMUNITY_*`, `NUXT_PUBLIC_COMMUNITY_*`, `PUBLIC_COMMUNITY_*` (SvelteKit, Astro), `VITE_COMMUNITY_*` —
|
|
12
|
+
* throws by its mere presence, whatever the prefix and before anything reads it. The message names the variable,
|
|
13
|
+
* never its value. (The Turnstile site key is public page config; name it outside these namespaces.)
|
|
14
|
+
*/
|
|
15
|
+
import type { CommunityBffConfig } from './bff.js';
|
|
16
|
+
/** What the environment configures of `createCommunityBff`. */
|
|
17
|
+
export type CommunityServerEnv = Required<Pick<CommunityBffConfig, 'noticeKey'>>;
|
|
18
|
+
export declare function communityServerEnv(env: Record<string, string | undefined>, o?: {
|
|
19
|
+
prefix?: '' | 'NUXT_';
|
|
20
|
+
}): CommunityServerEnv;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const a = ["NEXT_PUBLIC_COMMUNITY_", "NUXT_PUBLIC_COMMUNITY_", "PUBLIC_COMMUNITY_", "VITE_COMMUNITY_"];
|
|
2
|
+
function I(i, s = {}) {
|
|
3
|
+
const t = i ?? {};
|
|
4
|
+
for (const e of Object.keys(t)) {
|
|
5
|
+
const r = t[e];
|
|
6
|
+
if (typeof r == "string" && r !== "" && a.some((_) => e.startsWith(_)))
|
|
7
|
+
throw new Error(
|
|
8
|
+
`[blogmcp-sdk] ${e} is set — community keys are never PUBLIC (a public variable ends up in page JavaScript). Rename it without the public prefix and rotate the key.`
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
const c = s.prefix === "NUXT_" ? "NUXT_" : "", n = t[`${c}COMMUNITY_NOTICE_KEY`], o = typeof n == "string" ? n.trim() : "";
|
|
12
|
+
return { noticeKey: o === "" ? null : o };
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
I as communityServerEnv
|
|
16
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host-session routes (docs/internal/m1-contracts.md §7.1, §10.3): every kb-api operation a managed blog's
|
|
3
|
+
* proxy or a headless site's BFF may forward for a host session — the public operations of Comments 2.0 and the
|
|
4
|
+
* notice form. One operation, one literal path, POST only; ids travel in the JSON body, never in the path.
|
|
5
|
+
*
|
|
6
|
+
* A copy of kb-api's `apps/knowledge-base/api/src/routes/community-public-routes.json`, which kb-api generates from
|
|
7
|
+
* its sealed routers. test/community/server/host-routes-drift.spec.ts reads that file as text and fails when the
|
|
8
|
+
* two differ, so a route kb-api adds or removes is a change here too: a route missing from this list answers the
|
|
9
|
+
* unknown path through every host, a stale one would forward to a path kb-api no longer serves. The team, adapter
|
|
10
|
+
* and platform surfaces are never in it (the team is not a host session; the others call kb-api themselves), nor
|
|
11
|
+
* is either closed surface — route-decision.ts refuses those whatever this list says.
|
|
12
|
+
*/
|
|
13
|
+
import type { CommunityHostRoute } from './route-decision.js';
|
|
14
|
+
/** kb-api's allowlist, sorted by path as kb-api writes it. Frozen, every entry too. */
|
|
15
|
+
export declare const COMMUNITY_HOST_ROUTES: readonly CommunityHostRoute[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const e = Object.freeze([
|
|
2
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/blocks/list" }),
|
|
3
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/blocks/set" }),
|
|
4
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/discussions/head" }),
|
|
5
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/discussions/list" }),
|
|
6
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/legal/current" }),
|
|
7
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/appeals/file" }),
|
|
8
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/appeals/list" }),
|
|
9
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/appeals/withdraw" }),
|
|
10
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/consents/withdraw" }),
|
|
11
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/decisions/get" }),
|
|
12
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/decisions/list" }),
|
|
13
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/erase" }),
|
|
14
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/erase/cancel" }),
|
|
15
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/export" }),
|
|
16
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/get" }),
|
|
17
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/notifications/list" }),
|
|
18
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/notifications/mark-read" }),
|
|
19
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/onboard" }),
|
|
20
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/profile/update" }),
|
|
21
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/me/reports/list" }),
|
|
22
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/messages/delete" }),
|
|
23
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/messages/edit" }),
|
|
24
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/messages/post" }),
|
|
25
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/messages/report" }),
|
|
26
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/profile/get" }),
|
|
27
|
+
Object.freeze({ method: "POST", path: "/community/v1/public/reactions/set" }),
|
|
28
|
+
Object.freeze({ method: "POST", path: "/trust/v1/notices/appeal" }),
|
|
29
|
+
Object.freeze({ method: "POST", path: "/trust/v1/notices/appeal/withdraw" }),
|
|
30
|
+
Object.freeze({ method: "POST", path: "/trust/v1/notices/status" }),
|
|
31
|
+
Object.freeze({ method: "POST", path: "/trust/v1/notices/submit" })
|
|
32
|
+
]);
|
|
33
|
+
export {
|
|
34
|
+
e as COMMUNITY_HOST_ROUTES
|
|
35
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The request/response seam between the community BFF and a framework (docs/internal/m1-contracts.md §10.3): the
|
|
3
|
+
* consultations' `ConsultationHttp` plus the request's path and its JSON body. `fromWebRequest` is the ready-made
|
|
4
|
+
* adapter for anything that speaks Web `Request`/`Response` (Next route handlers, React Router resource routes,
|
|
5
|
+
* Nitro's `toWebRequest`, workers); another framework writes its own in a few lines (the kit's `h3Http`) and
|
|
6
|
+
* reads the body through `parseCommunityJson`, so every adapter answers the same.
|
|
7
|
+
*
|
|
8
|
+
* Composed on the consultations' seam (src/consultations/server/http.ts), not copied: `method`, `header`,
|
|
9
|
+
* `cookie` (the host's own session, which `memberToken` reads), `setCookie` and `form` are its. The BFF reads no
|
|
10
|
+
* cookie and forwards none.
|
|
11
|
+
*
|
|
12
|
+
* The JSON body: `application/json` only (a charset, when named, is UTF-8), at most 64 KB — kb-api's own limit for
|
|
13
|
+
* these routes, so whatever passes here passes there — read no further than that, valid UTF-8 that parses to a
|
|
14
|
+
* JSON object (every operation's body is one). The bytes come back exactly as received: the BFF forwards them as
|
|
15
|
+
* they are, never re-serialised.
|
|
16
|
+
*/
|
|
17
|
+
import { type ConsultationHttp, type ConsultationResponse } from '../../consultations/server/http.js';
|
|
18
|
+
/** The largest JSON body in bytes: kb-api's `express.json({ limit: '64kb' })` on every community router. */
|
|
19
|
+
export declare const COMMUNITY_BODY_MAX_BYTES = 65536;
|
|
20
|
+
/** Why a request has no JSON body the BFF forwards. */
|
|
21
|
+
export type CommunityJsonRefusal = 'TOO_LARGE' | 'INVALID_JSON';
|
|
22
|
+
export type CommunityJsonBody =
|
|
23
|
+
/** `bytes` exactly as received (what the BFF forwards); `value` is what they parse to. */
|
|
24
|
+
{
|
|
25
|
+
ok: true;
|
|
26
|
+
bytes: Uint8Array;
|
|
27
|
+
value: Record<string, unknown>;
|
|
28
|
+
} | {
|
|
29
|
+
ok: false;
|
|
30
|
+
code: CommunityJsonRefusal;
|
|
31
|
+
};
|
|
32
|
+
export interface CommunityHttp extends ConsultationHttp {
|
|
33
|
+
/**
|
|
34
|
+
* The request's URL path as received — still percent-encoded, e.g. `/api/community/v1/public/me/get`. A query
|
|
35
|
+
* or fragment is ignored; the BFF judges every spelling of what is left.
|
|
36
|
+
*/
|
|
37
|
+
path: string;
|
|
38
|
+
/**
|
|
39
|
+
* The JSON body (see the module doc), read once: every call answers the same. An adapter MUST enforce what
|
|
40
|
+
* `fromWebRequest` enforces — `parseCommunityJson` over at most COMMUNITY_BODY_MAX_BYTES, reading stopped past
|
|
41
|
+
* them. `form()` reads the same body: an adapter serves one of them per request.
|
|
42
|
+
*/
|
|
43
|
+
json(): Promise<CommunityJsonBody>;
|
|
44
|
+
}
|
|
45
|
+
/** What the BFF answers: the status, every header, and the body as text (JSON; a HEAD answer sends none). */
|
|
46
|
+
export type CommunityResponse = ConsultationResponse<string>;
|
|
47
|
+
/** `application/json`, with no charset or a UTF-8 one (RFC 8259: JSON between systems is UTF-8). */
|
|
48
|
+
export declare function isJsonContentType(contentType: unknown): boolean;
|
|
49
|
+
/** A byte array, also one from another realm (a Node `Buffer` is one too). */
|
|
50
|
+
export declare function isBytes(value: unknown): value is Uint8Array;
|
|
51
|
+
/**
|
|
52
|
+
* The verdict on a request body: its `Content-Type` and its bytes → the JSON object they hold, or why there is
|
|
53
|
+
* none. Never throws. The rules of every adapter's `json()` (and the BFF's own check of what an adapter hands it).
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseCommunityJson(contentType: unknown, bytes: unknown): CommunityJsonBody;
|
|
56
|
+
/**
|
|
57
|
+
* A `CommunityHttp` over a Web `Request`, and `respond` to turn the BFF's answer into a `Response`: its status, its
|
|
58
|
+
* headers, every `Set-Cookie` the host queued (`memberToken` may refresh its own session) and its body as it is —
|
|
59
|
+
* none for a HEAD request or a status that takes none.
|
|
60
|
+
*/
|
|
61
|
+
export declare function fromWebRequest(req: Request): {
|
|
62
|
+
http: CommunityHttp;
|
|
63
|
+
respond(r: CommunityResponse): Response;
|
|
64
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { fromWebRequest as y } from "../../consultations/server/http.js";
|
|
2
|
+
const f = 65536, l = Object.freeze({ ok: !1, code: "TOO_LARGE" }), u = Object.freeze({ ok: !1, code: "INVALID_JSON" }), h = /* @__PURE__ */ new Set([101, 103, 204, 205, 304]);
|
|
3
|
+
function d(e) {
|
|
4
|
+
if (typeof e != "string") return !1;
|
|
5
|
+
const [n, ...t] = e.split(";");
|
|
6
|
+
if (n.trim().toLowerCase() !== "application/json") return !1;
|
|
7
|
+
for (const s of t) {
|
|
8
|
+
const r = s.indexOf("=");
|
|
9
|
+
if ((r === -1 ? s : s.slice(0, r)).trim().toLowerCase() !== "charset") continue;
|
|
10
|
+
const a = r === -1 ? "" : s.slice(r + 1).trim().replace(/^"(.*)"$/, "$1").toLowerCase();
|
|
11
|
+
if (a !== "utf-8" && a !== "utf8") return !1;
|
|
12
|
+
}
|
|
13
|
+
return !0;
|
|
14
|
+
}
|
|
15
|
+
function b(e) {
|
|
16
|
+
return Object.prototype.toString.call(e) === "[object Uint8Array]";
|
|
17
|
+
}
|
|
18
|
+
function m(e, n) {
|
|
19
|
+
if (!d(e) || !b(n)) return u;
|
|
20
|
+
if (n.byteLength > f) return l;
|
|
21
|
+
let t;
|
|
22
|
+
try {
|
|
23
|
+
t = JSON.parse(new TextDecoder("utf-8", { fatal: !0, ignoreBOM: !0 }).decode(n));
|
|
24
|
+
} catch {
|
|
25
|
+
return u;
|
|
26
|
+
}
|
|
27
|
+
return typeof t != "object" || t === null || Array.isArray(t) ? u : Object.freeze({ ok: !0, bytes: n, value: t });
|
|
28
|
+
}
|
|
29
|
+
async function O(e, n) {
|
|
30
|
+
const t = Number(e.headers.get("content-length"));
|
|
31
|
+
if (Number.isFinite(t) && t > n) return "too_large";
|
|
32
|
+
if (!e.body) return new Uint8Array(0);
|
|
33
|
+
const s = [];
|
|
34
|
+
let r = 0;
|
|
35
|
+
try {
|
|
36
|
+
const c = e.body.getReader();
|
|
37
|
+
for (; ; ) {
|
|
38
|
+
const { done: p, value: i } = await c.read();
|
|
39
|
+
if (p) break;
|
|
40
|
+
if (r += i.byteLength, r > n)
|
|
41
|
+
return await c.cancel().catch(() => {
|
|
42
|
+
}), "too_large";
|
|
43
|
+
s.push(i);
|
|
44
|
+
}
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const o = new Uint8Array(r);
|
|
49
|
+
let a = 0;
|
|
50
|
+
for (const c of s)
|
|
51
|
+
o.set(c, a), a += c.byteLength;
|
|
52
|
+
return o;
|
|
53
|
+
}
|
|
54
|
+
async function g(e) {
|
|
55
|
+
const n = e.headers.get("content-type");
|
|
56
|
+
if (!d(n)) return u;
|
|
57
|
+
const t = await O(e, f);
|
|
58
|
+
return t === "too_large" ? l : t === null ? u : m(n, t);
|
|
59
|
+
}
|
|
60
|
+
function L(e) {
|
|
61
|
+
try {
|
|
62
|
+
return new URL(e).pathname;
|
|
63
|
+
} catch {
|
|
64
|
+
return "";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function A(e) {
|
|
68
|
+
const n = y(e);
|
|
69
|
+
let t = null;
|
|
70
|
+
const s = {
|
|
71
|
+
...n.http,
|
|
72
|
+
path: L(e.url),
|
|
73
|
+
// Memoised: the body stream can be read only once.
|
|
74
|
+
json: () => t ??= g(e)
|
|
75
|
+
}, r = e.method.toUpperCase() === "HEAD";
|
|
76
|
+
return {
|
|
77
|
+
http: s,
|
|
78
|
+
respond(o) {
|
|
79
|
+
const a = n.respond({ status: o.status, headers: o.headers, body: null }), c = r || h.has(o.status) || typeof o.body != "string";
|
|
80
|
+
return new Response(c ? null : o.body, { status: o.status, headers: a.headers });
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
f as COMMUNITY_BODY_MAX_BYTES,
|
|
86
|
+
A as fromWebRequest,
|
|
87
|
+
b as isBytes,
|
|
88
|
+
d as isJsonContentType,
|
|
89
|
+
m as parseCommunityJson
|
|
90
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@blogmcp/sdk/community/server` — the server half of the community (docs/internal/m1-contracts.md §3.4, §10.3):
|
|
3
|
+
* the host-session route decision a managed blog's `/api` proxy runs (which kb-api paths a host session may reach:
|
|
4
|
+
* kb-api's allowlist of public and notice operations; the uniform unknown-path answer for everything else in its
|
|
5
|
+
* `/community` and `/trust` namespaces), and the BFF a headless site mounts in front of those routes.
|
|
6
|
+
*
|
|
7
|
+
* SERVER-ONLY by contract, like `@blogmcp/sdk/consultations/server`: `createCommunityBff` holds the site's notice
|
|
8
|
+
* key and its members' tokens and throws when a global `window` exists. What keeps the entry out of page
|
|
9
|
+
* JavaScript is structural too — the hosts import it from server code only (their import-boundary specs) and
|
|
10
|
+
* `scripts/verify-pack.mjs` fails a browser bundle that contains it. The browser half is `@blogmcp/sdk/community`.
|
|
11
|
+
* Runtime-neutral: no `node:*`, so it runs in Nitro on Bun or Node, in Next, in React Router and in a worker.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // A React Router resource route, app/routes/api/community.$.tsx (POST only):
|
|
15
|
+
* const bff = createCommunityBff({
|
|
16
|
+
* // kb-api WITH the `/api` prefix the instance serves it under: KB_API_URL = https://<instance>/api
|
|
17
|
+
* // (or `${process.env.BLOGMCP_API_URL}/api`); the BFF appends `/community/v1/public/…` and `/trust/v1/notices/…`.
|
|
18
|
+
* kbApiUrl: process.env.KB_API_URL!, siteUrls: ['https://shop.example'],
|
|
19
|
+
* memberToken: async (http) => (await sessionOf(http))?.memberJwt ?? null,
|
|
20
|
+
* ...communityServerEnv(process.env),
|
|
21
|
+
* })
|
|
22
|
+
* export async function action({ request }: { request: Request }) {
|
|
23
|
+
* const { http, respond } = fromWebRequest(request)
|
|
24
|
+
* return respond(await bff.handle(http))
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* // A proxy that forwards on its own (the managed blogs): before its other rules, before any cookie or Bearer work.
|
|
28
|
+
* const decision = communityRouteDecision(request.method, path)
|
|
29
|
+
* if (decision.kind === 'deny') {
|
|
30
|
+
* const answer = communityUnknownPath()
|
|
31
|
+
* return new Response(request.method === 'HEAD' ? null : answer.body, { status: answer.status, headers: answer.headers })
|
|
32
|
+
* }
|
|
33
|
+
* if (decision.kind === 'allow') return forward(decision.upstreamPath) // the bearer, no cookie, the Origin checked
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export { COMMUNITY_NAMESPACES, COMMUNITY_HOST_ROUTES, isCommunityNamespacePath, communityRouteDecision, } from './route-decision.js';
|
|
37
|
+
export type { CommunityHostRoute, CommunityRouteDecision } from './route-decision.js';
|
|
38
|
+
export { COMMUNITY_UNKNOWN_PATH_BODY, communityUnknownPath } from './unknown-path.js';
|
|
39
|
+
export type { CommunityUnknownPathAnswer } from './unknown-path.js';
|
|
40
|
+
export { createCommunityBff } from './bff.js';
|
|
41
|
+
export type { CommunityBff, CommunityBffCode, CommunityBffConfig, CommunityBffLogEvent } from './bff.js';
|
|
42
|
+
export { communityServerEnv } from './env.js';
|
|
43
|
+
export type { CommunityServerEnv } from './env.js';
|
|
44
|
+
export { COMMUNITY_BODY_MAX_BYTES, fromWebRequest, parseCommunityJson } from './http.js';
|
|
45
|
+
export type { CommunityHttp, CommunityJsonBody, CommunityJsonRefusal, CommunityResponse } from './http.js';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { COMMUNITY_NAMESPACES as e, communityRouteDecision as r, isCommunityNamespacePath as t } from "./route-decision.js";
|
|
2
|
+
import { COMMUNITY_UNKNOWN_PATH_BODY as M, communityUnknownPath as f } from "./unknown-path.js";
|
|
3
|
+
import { createCommunityBff as N } from "./bff.js";
|
|
4
|
+
import { communityServerEnv as _ } from "./env.js";
|
|
5
|
+
import { COMMUNITY_BODY_MAX_BYTES as u, fromWebRequest as C, parseCommunityJson as T } from "./http.js";
|
|
6
|
+
import { COMMUNITY_HOST_ROUTES as Y } from "./host-routes.js";
|
|
7
|
+
export {
|
|
8
|
+
u as COMMUNITY_BODY_MAX_BYTES,
|
|
9
|
+
Y as COMMUNITY_HOST_ROUTES,
|
|
10
|
+
e as COMMUNITY_NAMESPACES,
|
|
11
|
+
M as COMMUNITY_UNKNOWN_PATH_BODY,
|
|
12
|
+
r as communityRouteDecision,
|
|
13
|
+
_ as communityServerEnv,
|
|
14
|
+
f as communityUnknownPath,
|
|
15
|
+
N as createCommunityBff,
|
|
16
|
+
C as fromWebRequest,
|
|
17
|
+
t as isCommunityNamespacePath,
|
|
18
|
+
T as parseCommunityJson
|
|
19
|
+
};
|