@blogmcp/sdk 0.13.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/community/index.d.ts +4 -1
- package/dist/community/index.js +39 -36
- package/dist/community/legal-text.d.ts +65 -0
- package/dist/community/legal-text.js +247 -0
- package/dist/community/links.d.ts +6 -0
- package/dist/community/links.js +9 -1
- package/dist/community/outcome.d.ts +1 -1
- package/dist/community/outcome.js +60 -23
- package/dist/community/private/browser.d.ts +18 -0
- package/dist/community/private/browser.js +17 -0
- package/dist/community/private/client.d.ts +259 -0
- package/dist/community/private/client.js +98 -0
- package/dist/community/private/dm.d.ts +547 -0
- package/dist/community/private/dm.js +852 -0
- package/dist/community/private/dpop.d.ts +59 -0
- package/dist/community/private/dpop.js +46 -0
- package/dist/community/private/encoding.d.ts +17 -0
- package/dist/community/private/encoding.js +50 -0
- package/dist/community/private/host-state.d.ts +20 -0
- package/dist/community/private/host-state.js +15 -0
- package/dist/community/private/index.d.ts +51 -0
- package/dist/community/private/index.js +71 -0
- package/dist/community/private/keystore.d.ts +73 -0
- package/dist/community/private/keystore.js +91 -0
- package/dist/community/private/marker.d.ts +17 -0
- package/dist/community/private/marker.js +11 -0
- package/dist/community/private/private-routes.d.ts +6 -0
- package/dist/community/private/private-routes.js +72 -0
- package/dist/community/private/promise.d.ts +65 -0
- package/dist/community/private/promise.js +39 -0
- package/dist/community/private/realtime.d.ts +127 -0
- package/dist/community/private/realtime.js +199 -0
- package/dist/community/private/session.d.ts +175 -0
- package/dist/community/private/session.js +441 -0
- package/dist/community/private/transport.d.ts +47 -0
- package/dist/community/private/transport.js +65 -0
- package/dist/community/server/grant-handoff.d.ts +53 -0
- package/dist/community/server/grant-handoff.js +96 -0
- package/dist/community/server/grant.d.ts +100 -0
- package/dist/community/server/grant.js +165 -0
- package/dist/community/server/index.d.ts +4 -0
- package/dist/community/server/index.js +20 -16
- package/dist/community/shapes.d.ts +27 -1
- package/dist/community/shapes.js +27 -17
- package/package.json +7 -3
- package/src/community/index.ts +6 -1
- package/src/community/legal-text.ts +438 -0
- package/src/community/links.ts +8 -0
- package/src/community/outcome.ts +37 -0
- package/src/community/private/browser.ts +31 -0
- package/src/community/private/client.ts +304 -0
- package/src/community/private/dm.ts +1687 -0
- package/src/community/private/dpop.ts +110 -0
- package/src/community/private/encoding.ts +71 -0
- package/src/community/private/host-state.ts +37 -0
- package/src/community/private/index.ts +169 -0
- package/src/community/private/keystore.ts +179 -0
- package/src/community/private/marker.ts +28 -0
- package/src/community/private/private-routes.ts +80 -0
- package/src/community/private/promise.ts +125 -0
- package/src/community/private/realtime.ts +431 -0
- package/src/community/private/session.ts +848 -0
- package/src/community/private/transport.ts +125 -0
- package/src/community/server/grant-handoff.ts +179 -0
- package/src/community/server/grant.ts +292 -0
- package/src/community/server/index.ts +6 -0
- package/src/community/shapes.ts +46 -1
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { assertServerOnly as w } from "../../core/server-only.js";
|
|
2
|
+
import { clientIpFrom as k } from "../../consultations/server/client-ip.js";
|
|
3
|
+
import { grantUpstreamBase as R, clientIpHeaderOf as T, timeoutOf as A, memberTokenOf as E, issueUpstreamGrant as O, PRIVATE_JKT_RE as P } from "./grant.js";
|
|
4
|
+
import { fromWebRequest as U } from "./http.js";
|
|
5
|
+
import { communityUnknownPath as v } from "./unknown-path.js";
|
|
6
|
+
const H = "/zpravy", I = /^[A-Za-z0-9_-]{32,128}$/, L = Object.freeze({
|
|
7
|
+
"Cache-Control": "no-store",
|
|
8
|
+
"Referrer-Policy": "no-referrer",
|
|
9
|
+
"X-Robots-Tag": "noindex",
|
|
10
|
+
"X-Content-Type-Options": "nosniff"
|
|
11
|
+
});
|
|
12
|
+
function S(r) {
|
|
13
|
+
if (typeof r != "string") return null;
|
|
14
|
+
try {
|
|
15
|
+
const t = new URL(r.trim()), o = t.hostname === "localhost" || t.hostname === "127.0.0.1";
|
|
16
|
+
return !(t.protocol === "https:" || t.protocol === "http:" && o) || t.username || t.password || t.search || t.hash || t.pathname !== "/" && t.pathname !== "" ? null : t.origin;
|
|
17
|
+
} catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function _(r) {
|
|
22
|
+
if (typeof r != "string") return null;
|
|
23
|
+
const t = r.startsWith("?") ? r.slice(1) : r;
|
|
24
|
+
if (t === "" || t.includes("#")) return null;
|
|
25
|
+
const o = new URLSearchParams(t), s = [...o.keys()];
|
|
26
|
+
if (s.length !== 2 || new Set(s).size !== 2 || !s.includes("jkt") || !s.includes("state")) return null;
|
|
27
|
+
const f = o.get("jkt"), l = o.get("state");
|
|
28
|
+
return P.test(f) && I.test(l) ? { jkt: f, state: l } : null;
|
|
29
|
+
}
|
|
30
|
+
function D(r) {
|
|
31
|
+
w("createPrivateGrantHandoff", "it holds the site's members' tokens, which must never reach the browser.");
|
|
32
|
+
const t = r ?? {};
|
|
33
|
+
if (typeof t.memberToken != "function")
|
|
34
|
+
throw new TypeError("[blogmcp-sdk] createPrivateGrantHandoff needs memberToken: (http) => Promise<string | null>");
|
|
35
|
+
const o = R(t.kbApiUrl);
|
|
36
|
+
if (o === null) throw new TypeError("[blogmcp-sdk] createPrivateGrantHandoff needs kbApiUrl: an absolute http(s) URL");
|
|
37
|
+
const s = S(t.accountOrigin);
|
|
38
|
+
if (s === null) throw new TypeError("[blogmcp-sdk] createPrivateGrantHandoff needs accountOrigin: https://<slug>--account.<base>");
|
|
39
|
+
const f = o, l = `${s}${H}`, d = T(t.clientIpHeader), b = () => typeof t.fetch == "function" ? t.fetch() : globalThis.fetch, y = A(t.timeoutMs), c = () => {
|
|
40
|
+
const e = v();
|
|
41
|
+
return { status: e.status, headers: e.headers, body: e.body };
|
|
42
|
+
}, i = (e) => ({
|
|
43
|
+
status: 303,
|
|
44
|
+
headers: { ...L, Location: `${l}#${new URLSearchParams(e).toString()}` },
|
|
45
|
+
body: ""
|
|
46
|
+
});
|
|
47
|
+
async function g(e, p) {
|
|
48
|
+
try {
|
|
49
|
+
if (String(e.method).toUpperCase() !== "GET") return c();
|
|
50
|
+
const n = _(p);
|
|
51
|
+
if (n === null) return c();
|
|
52
|
+
const a = e.header("sec-fetch-mode");
|
|
53
|
+
if (typeof a == "string" && a.trim().toLowerCase() !== "navigate") return c();
|
|
54
|
+
const h = await E(t.memberToken, e);
|
|
55
|
+
if (h === "invalid") return i({ state: n.state, error: "UNAVAILABLE" });
|
|
56
|
+
if (h === null) return i({ state: n.state, error: "UNAUTHORIZED" });
|
|
57
|
+
const m = await O({
|
|
58
|
+
base: f,
|
|
59
|
+
token: h,
|
|
60
|
+
body: { jkt: n.jkt, purpose: "handoff" },
|
|
61
|
+
clientIp: d === "" ? null : k(e, { header: d }),
|
|
62
|
+
fetch: b,
|
|
63
|
+
timeoutMs: y
|
|
64
|
+
});
|
|
65
|
+
if (!m.ok) return m.unknownPath ? c() : i({ state: n.state, error: m.code });
|
|
66
|
+
const u = m.grant;
|
|
67
|
+
return u.memberRef === void 0 ? i({ state: n.state, error: "UNAVAILABLE" }) : i({
|
|
68
|
+
grant: u.grant,
|
|
69
|
+
state: n.state,
|
|
70
|
+
dpopNonce: u.dpopNonce,
|
|
71
|
+
serverTime: String(u.serverTime),
|
|
72
|
+
memberRef: u.memberRef
|
|
73
|
+
});
|
|
74
|
+
} catch {
|
|
75
|
+
return c();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
handle: g,
|
|
80
|
+
async handleRequest(e) {
|
|
81
|
+
const { http: p, respond: n } = U(e);
|
|
82
|
+
let a = "";
|
|
83
|
+
try {
|
|
84
|
+
a = new URL(e.url).search;
|
|
85
|
+
} catch {
|
|
86
|
+
a = "";
|
|
87
|
+
}
|
|
88
|
+
return n(await g(p, a));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
H as PRIVATE_HANDOFF_PAGE,
|
|
94
|
+
I as PRIVATE_HANDOFF_STATE_RE,
|
|
95
|
+
D as createPrivateGrantHandoff
|
|
96
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createPrivateGrantHandler` — the host's grant route (docs/internal/m4-contracts.md §6.1, §5.9): the storefront's
|
|
3
|
+
* `/komunita/grant`, a managed blog's `/blog/community/grant`. The page's private SDK posts the thumbprint of its
|
|
4
|
+
* device key here; the handler turns the host's own member session into a one-minute, single-use private grant for
|
|
5
|
+
* that key and hands kb-api's answer back. The grant is useless without the key (kb-api compares the proof's key with
|
|
6
|
+
* the grant's at `private/session`), so a page that sees it learns nothing it could use elsewhere.
|
|
7
|
+
*
|
|
8
|
+
* Every request, in this order; a step that refuses answers alone:
|
|
9
|
+
* 1. POST only: anything else gets kb-api's uniform unknown-path answer (the route serves nothing else);
|
|
10
|
+
* 2. `Origin` exactly one of the site URLs' origins and, when sent, `Sec-Fetch-Site: same-origin` — the BFF's check
|
|
11
|
+
* (`isAllowedOrigin`): another site's page, or a sister host of a shared domain, cannot drive it (403 ORIGIN);
|
|
12
|
+
* 3. a strict body `{ jkt }` of at most 1 KB: the RFC 7638 thumbprint and nothing else (400 VALIDATION, 413
|
|
13
|
+
* TOO_LARGE, 400 INVALID_JSON) — never a member id, never a purpose (the hand-off has its own route);
|
|
14
|
+
* 4. the host's session: `memberToken(http)` → the member's bearer, or 401 UNAUTHORIZED without one;
|
|
15
|
+
* 5. one upstream `POST <kbApiUrl>/community/v1/grant/issue { jkt }` with `Authorization: Bearer <member token>`,
|
|
16
|
+
* `Content-Type` and — when `clientIpHeader` names an edge header — the visitor's IP in `X-Blogmcp-Client-Ip`
|
|
17
|
+
* (the grant's own ingress router keeps it, §5.8, so every member is not counted in the site server's one
|
|
18
|
+
* per-IP bucket); nothing else: no cookie, no Origin, `redirect: 'error'`, `cache: 'no-store'`, a timeout;
|
|
19
|
+
* 6. the answer: kb-api's success passed through as exactly `{ ok: true, grant, expiresIn, privateBase, dpopNonce,
|
|
20
|
+
* serverTime }` — the first nonce and the clock the first proof needs (§5.7) — and a refusal as `{ ok: false,
|
|
21
|
+
* code, message }` with kb-api's status (its unknown-path answer byte for byte); `Cache-Control: no-store`.
|
|
22
|
+
* Anything else from upstream is 503 UNAVAILABLE. Nothing is stored and nothing logged but codes.
|
|
23
|
+
*
|
|
24
|
+
* A flag-false instance: kb-api answers a signed-in member's grant request with its unknown-path answer, passed
|
|
25
|
+
* through unchanged, so to that member the route looks absent. Steps 1–4 answer before kb-api is asked, whatever the
|
|
26
|
+
* flag (a visitor without a host session gets 401 UNAUTHORIZED): nothing private is reached, but those answers show
|
|
27
|
+
* that the route is mounted, so whether to mount it on a dark instance is the host's decision (the PH slices).
|
|
28
|
+
*
|
|
29
|
+
* Server-only (it holds members' tokens): the factory throws where a global `window` exists. Runtime-neutral (Web
|
|
30
|
+
* fetch, no `node:*`).
|
|
31
|
+
*/
|
|
32
|
+
import { type CommunityHttp, type CommunityResponse } from './http.js';
|
|
33
|
+
/** The largest grant request body: `{ "jkt": "<43 characters>" }` with room to spare. */
|
|
34
|
+
export declare const PRIVATE_GRANT_BODY_MAX_BYTES = 1024;
|
|
35
|
+
/** kb-api's grant operation under its base (the base carries `/api` when it is the public address). */
|
|
36
|
+
export declare const PRIVATE_GRANT_UPSTREAM_PATH = "/community/v1/grant/issue";
|
|
37
|
+
/** An RFC 7638 SHA-256 thumbprint: 43 base64url characters (kb-api's JKT_RE). */
|
|
38
|
+
export declare const PRIVATE_JKT_RE: RegExp;
|
|
39
|
+
export type PrivateGrantRefusal = 'ORIGIN' | 'VALIDATION' | 'TOO_LARGE' | 'INVALID_JSON' | 'UNAUTHORIZED' | 'UNAVAILABLE';
|
|
40
|
+
export interface PrivateGrantHandlerConfig {
|
|
41
|
+
/** kb-api's base URL: its in-cluster origin, or the instance's public address WITH `/api` (as the BFF's). */
|
|
42
|
+
kbApiUrl: string;
|
|
43
|
+
/** The site's own URLs: their origins are the only `Origin`s admitted. */
|
|
44
|
+
siteUrls: string[];
|
|
45
|
+
/** The host's session → the member's bearer for kb-api (the bare token), or null without a session. */
|
|
46
|
+
memberToken: (http: CommunityHttp) => Promise<string | null>;
|
|
47
|
+
/** The edge header with the visitor's IP (e.g. `cf-connecting-ip`), forwarded as `X-Blogmcp-Client-Ip`; none by default. */
|
|
48
|
+
clientIpHeader?: string;
|
|
49
|
+
fetch?: () => typeof fetch;
|
|
50
|
+
timeoutMs?: number;
|
|
51
|
+
}
|
|
52
|
+
export interface PrivateGrantHandler {
|
|
53
|
+
/** One request of the page's private SDK: its answer, never a throw. */
|
|
54
|
+
handle(http: CommunityHttp): Promise<CommunityResponse>;
|
|
55
|
+
}
|
|
56
|
+
/** kb-api's base without a trailing slash, or null when it is not an absolute http(s) URL without a query. */
|
|
57
|
+
export declare function grantUpstreamBase(value: unknown): string | null;
|
|
58
|
+
/** kb-api's grant answer, checked: exactly the fields the page needs, or null. */
|
|
59
|
+
export interface IssuedGrant {
|
|
60
|
+
grant: string;
|
|
61
|
+
expiresIn: number;
|
|
62
|
+
privateBase: string;
|
|
63
|
+
dpopNonce: string;
|
|
64
|
+
serverTime: number;
|
|
65
|
+
memberRef?: string;
|
|
66
|
+
}
|
|
67
|
+
export type UpstreamGrant = {
|
|
68
|
+
ok: true;
|
|
69
|
+
grant: IssuedGrant;
|
|
70
|
+
} | {
|
|
71
|
+
ok: false;
|
|
72
|
+
status: number;
|
|
73
|
+
code: string;
|
|
74
|
+
message: string;
|
|
75
|
+
unknownPath: boolean;
|
|
76
|
+
} | {
|
|
77
|
+
ok: false;
|
|
78
|
+
status: 503;
|
|
79
|
+
code: 'UNAVAILABLE';
|
|
80
|
+
message: string;
|
|
81
|
+
unknownPath: false;
|
|
82
|
+
};
|
|
83
|
+
/** One upstream `grant/issue` call with the member's bearer; never throws. */
|
|
84
|
+
export declare function issueUpstreamGrant(options: {
|
|
85
|
+
base: string;
|
|
86
|
+
token: string;
|
|
87
|
+
body: {
|
|
88
|
+
jkt: string;
|
|
89
|
+
purpose?: 'handoff';
|
|
90
|
+
};
|
|
91
|
+
clientIp: string | null;
|
|
92
|
+
fetch: () => typeof fetch;
|
|
93
|
+
timeoutMs: number;
|
|
94
|
+
}): Promise<UpstreamGrant>;
|
|
95
|
+
/** A member token from the host, checked: the bare token, visible ASCII. */
|
|
96
|
+
export declare function memberTokenOf(memberToken: (http: CommunityHttp) => Promise<string | null>, http: CommunityHttp): Promise<string | null | 'invalid'>;
|
|
97
|
+
/** The edge header to read the visitor's IP from, or '' for none (a name that is no header name counts as none). */
|
|
98
|
+
export declare function clientIpHeaderOf(value: unknown): string;
|
|
99
|
+
export declare function timeoutOf(value: unknown): number;
|
|
100
|
+
export declare function createPrivateGrantHandler(c: PrivateGrantHandlerConfig): PrivateGrantHandler;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { assertServerOnly as O } from "../../core/server-only.js";
|
|
2
|
+
import { clientIpFrom as E, isHeaderName as k, KB_CLIENT_IP_HEADER as I } from "../../consultations/server/client-ip.js";
|
|
3
|
+
import { siteOrigins as _, originOf as R, isAllowedOrigin as U } from "../../consultations/server/origin.js";
|
|
4
|
+
import { isJsonContentType as h } from "./http.js";
|
|
5
|
+
import { communityUnknownPath as y, COMMUNITY_UNKNOWN_PATH_BODY as w } from "./unknown-path.js";
|
|
6
|
+
const g = 1024, L = "/community/v1/grant/issue", A = /^[A-Za-z0-9_-]{43}$/, P = /^kbpg_[A-Za-z0-9_-]{16,256}$/, v = /^[A-Za-z0-9_-]{8,128}$/, S = /^[A-Z][A-Z0-9_]{0,63}$/, H = /^[\x21-\x7E]{1,8192}$/, j = 1e4, b = Object.freeze({
|
|
7
|
+
ORIGIN: { status: 403, message: "This request did not come from the site" },
|
|
8
|
+
VALIDATION: { status: 400, message: "The request body is not valid" },
|
|
9
|
+
TOO_LARGE: { status: 413, message: "The request body is too large" },
|
|
10
|
+
INVALID_JSON: { status: 400, message: "The request body must be a JSON object" },
|
|
11
|
+
UNAUTHORIZED: { status: 401, message: "Sign in first" },
|
|
12
|
+
UNAVAILABLE: { status: 503, message: "Temporarily unavailable" }
|
|
13
|
+
}), B = Object.freeze({
|
|
14
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
15
|
+
"Cache-Control": "no-store",
|
|
16
|
+
"Referrer-Policy": "no-referrer",
|
|
17
|
+
"X-Robots-Tag": "noindex",
|
|
18
|
+
"X-Content-Type-Options": "nosniff"
|
|
19
|
+
});
|
|
20
|
+
function N(t) {
|
|
21
|
+
if (typeof t != "string" || t.trim() === "") return null;
|
|
22
|
+
try {
|
|
23
|
+
const e = new URL(t.trim());
|
|
24
|
+
return e.protocol !== "http:" && e.protocol !== "https:" || e.search || e.hash || e.username || e.password ? null : e.origin + e.pathname.replace(/\/+$/, "");
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function D(t) {
|
|
30
|
+
const e = { ok: !1, status: 503, code: "UNAVAILABLE", message: b.UNAVAILABLE.message, unknownPath: !1 }, r = { "Content-Type": "application/json", Authorization: `Bearer ${t.token}` };
|
|
31
|
+
t.clientIp && (r[I] = t.clientIp);
|
|
32
|
+
let s, f;
|
|
33
|
+
try {
|
|
34
|
+
s = await t.fetch()(`${t.base}${L}`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: r,
|
|
37
|
+
body: JSON.stringify(t.body),
|
|
38
|
+
redirect: "error",
|
|
39
|
+
cache: "no-store",
|
|
40
|
+
signal: AbortSignal.timeout(t.timeoutMs)
|
|
41
|
+
}), f = await s.text();
|
|
42
|
+
} catch {
|
|
43
|
+
return e;
|
|
44
|
+
}
|
|
45
|
+
if (s.status === 404 && f === w)
|
|
46
|
+
return { ok: !1, status: 404, code: "UNKNOWN_PATH", message: "Not found", unknownPath: !0 };
|
|
47
|
+
if (!h(s.headers.get("content-type")) || f.includes(t.token)) return e;
|
|
48
|
+
let i;
|
|
49
|
+
try {
|
|
50
|
+
const o = JSON.parse(f);
|
|
51
|
+
if (!o || typeof o != "object" || Array.isArray(o)) return e;
|
|
52
|
+
i = o;
|
|
53
|
+
} catch {
|
|
54
|
+
return e;
|
|
55
|
+
}
|
|
56
|
+
if (s.status === 200 && i.ok === !0) {
|
|
57
|
+
const { grant: o, expiresIn: a, privateBase: c, dpopNonce: l, serverTime: u, memberRef: n } = i, T = typeof c == "string" ? N(c) : null;
|
|
58
|
+
if (typeof o != "string" || !P.test(o) || typeof a != "number" || !Number.isInteger(a) || a <= 0 || T === null || !/^https?:\/\//.test(c) || typeof l != "string" || !v.test(l) || typeof u != "number" || !Number.isFinite(u))
|
|
59
|
+
return e;
|
|
60
|
+
const p = { grant: o, expiresIn: a, privateBase: c, dpopNonce: l, serverTime: u };
|
|
61
|
+
return typeof n == "string" && /^[A-Za-z0-9_-]{16,64}$/.test(n) && (p.memberRef = n), { ok: !0, grant: p };
|
|
62
|
+
}
|
|
63
|
+
if (s.status >= 400 && s.status < 500 && typeof i.code == "string" && S.test(i.code)) {
|
|
64
|
+
const o = typeof i.message == "string" ? i.message.slice(0, 200) : "";
|
|
65
|
+
return { ok: !1, status: s.status, code: i.code, message: o, unknownPath: !1 };
|
|
66
|
+
}
|
|
67
|
+
return e;
|
|
68
|
+
}
|
|
69
|
+
async function G(t, e) {
|
|
70
|
+
let r;
|
|
71
|
+
try {
|
|
72
|
+
r = await t(e);
|
|
73
|
+
} catch {
|
|
74
|
+
return "invalid";
|
|
75
|
+
}
|
|
76
|
+
return r == null || r === "" ? null : typeof r == "string" && H.test(r) ? r : "invalid";
|
|
77
|
+
}
|
|
78
|
+
function V(t) {
|
|
79
|
+
const e = typeof t == "string" ? t.trim() : "";
|
|
80
|
+
return e !== "" && k(e) ? e : "";
|
|
81
|
+
}
|
|
82
|
+
function C(t) {
|
|
83
|
+
const e = typeof t == "number" ? Math.floor(t) : Number.NaN;
|
|
84
|
+
return e >= 1 && e <= 6e4 ? e : j;
|
|
85
|
+
}
|
|
86
|
+
function d(t, e) {
|
|
87
|
+
return { status: t, headers: { ...B }, body: JSON.stringify(e) };
|
|
88
|
+
}
|
|
89
|
+
function m(t) {
|
|
90
|
+
const { status: e, message: r } = b[t];
|
|
91
|
+
return d(e, { ok: !1, code: t, message: r });
|
|
92
|
+
}
|
|
93
|
+
async function M(t) {
|
|
94
|
+
if (!h(t.header("content-type"))) return "INVALID_JSON";
|
|
95
|
+
const e = Number(t.header("content-length"));
|
|
96
|
+
if (Number.isFinite(e) && e > g) return "TOO_LARGE";
|
|
97
|
+
let r;
|
|
98
|
+
try {
|
|
99
|
+
r = await t.json();
|
|
100
|
+
} catch {
|
|
101
|
+
return "INVALID_JSON";
|
|
102
|
+
}
|
|
103
|
+
if (!r.ok) return r.code;
|
|
104
|
+
if (r.bytes.byteLength > g) return "TOO_LARGE";
|
|
105
|
+
const s = Object.keys(r.value);
|
|
106
|
+
return s.length !== 1 || s[0] !== "jkt" || typeof r.value.jkt != "string" || !A.test(r.value.jkt) ? "VALIDATION" : r.value.jkt;
|
|
107
|
+
}
|
|
108
|
+
function F(t) {
|
|
109
|
+
O("createPrivateGrantHandler", "it holds the site's members' tokens, which must never reach the browser.");
|
|
110
|
+
const e = t ?? {};
|
|
111
|
+
if (typeof e.memberToken != "function")
|
|
112
|
+
throw new TypeError("[blogmcp-sdk] createPrivateGrantHandler needs memberToken: (http) => Promise<string | null>");
|
|
113
|
+
const r = N(e.kbApiUrl);
|
|
114
|
+
if (r === null) throw new TypeError("[blogmcp-sdk] createPrivateGrantHandler needs kbApiUrl: an absolute http(s) URL");
|
|
115
|
+
const s = _(Array.isArray(e.siteUrls) ? e.siteUrls : []);
|
|
116
|
+
if (s.length === 0 || e.siteUrls.some((a) => R(a) === null))
|
|
117
|
+
throw new TypeError("[blogmcp-sdk] createPrivateGrantHandler needs siteUrls: the site’s own http(s) URLs");
|
|
118
|
+
const f = V(e.clientIpHeader), i = () => typeof e.fetch == "function" ? e.fetch() : globalThis.fetch, o = C(e.timeoutMs);
|
|
119
|
+
return {
|
|
120
|
+
async handle(a) {
|
|
121
|
+
try {
|
|
122
|
+
if (String(a.method).toUpperCase() !== "POST") {
|
|
123
|
+
const n = y();
|
|
124
|
+
return { status: n.status, headers: n.headers, body: n.body };
|
|
125
|
+
}
|
|
126
|
+
if (!U(a, s)) return m("ORIGIN");
|
|
127
|
+
const c = await M(a);
|
|
128
|
+
if (!A.test(c)) return m(c);
|
|
129
|
+
const l = await G(e.memberToken, a);
|
|
130
|
+
if (l === "invalid") return m("UNAVAILABLE");
|
|
131
|
+
if (l === null) return m("UNAUTHORIZED");
|
|
132
|
+
const u = await D({
|
|
133
|
+
base: r,
|
|
134
|
+
token: l,
|
|
135
|
+
body: { jkt: c },
|
|
136
|
+
clientIp: f === "" ? null : E(a, { header: f }),
|
|
137
|
+
fetch: i,
|
|
138
|
+
timeoutMs: o
|
|
139
|
+
});
|
|
140
|
+
if (u.ok) {
|
|
141
|
+
const n = u.grant;
|
|
142
|
+
return d(200, { ok: !0, grant: n.grant, expiresIn: n.expiresIn, privateBase: n.privateBase, dpopNonce: n.dpopNonce, serverTime: n.serverTime });
|
|
143
|
+
}
|
|
144
|
+
if (u.unknownPath) {
|
|
145
|
+
const n = y();
|
|
146
|
+
return { status: n.status, headers: n.headers, body: n.body };
|
|
147
|
+
}
|
|
148
|
+
return d(u.status, { ok: !1, code: u.code, message: u.message });
|
|
149
|
+
} catch {
|
|
150
|
+
return m("UNAVAILABLE");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export {
|
|
156
|
+
g as PRIVATE_GRANT_BODY_MAX_BYTES,
|
|
157
|
+
L as PRIVATE_GRANT_UPSTREAM_PATH,
|
|
158
|
+
A as PRIVATE_JKT_RE,
|
|
159
|
+
V as clientIpHeaderOf,
|
|
160
|
+
F as createPrivateGrantHandler,
|
|
161
|
+
N as grantUpstreamBase,
|
|
162
|
+
D as issueUpstreamGrant,
|
|
163
|
+
G as memberTokenOf,
|
|
164
|
+
C as timeoutOf
|
|
165
|
+
};
|
|
@@ -47,3 +47,7 @@ export { communityServerEnv } from './env.js';
|
|
|
47
47
|
export type { CommunityServerEnv } from './env.js';
|
|
48
48
|
export { COMMUNITY_BODY_MAX_BYTES, fromWebRequest, parseCommunityJson } from './http.js';
|
|
49
49
|
export type { CommunityHttp, CommunityJsonBody, CommunityJsonRefusal, CommunityResponse } from './http.js';
|
|
50
|
+
export { createPrivateGrantHandler } from './grant.js';
|
|
51
|
+
export type { PrivateGrantHandler, PrivateGrantHandlerConfig, PrivateGrantRefusal } from './grant.js';
|
|
52
|
+
export { createPrivateGrantHandoff } from './grant-handoff.js';
|
|
53
|
+
export type { PrivateGrantHandoff, PrivateGrantHandoffConfig } from './grant-handoff.js';
|
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import { COMMUNITY_NAMESPACES as
|
|
2
|
-
import { COMMUNITY_UNKNOWN_PATH_BODY as
|
|
3
|
-
import { createCommunityBff as
|
|
4
|
-
import { communityServerEnv as
|
|
5
|
-
import { COMMUNITY_BODY_MAX_BYTES as
|
|
6
|
-
import {
|
|
1
|
+
import { COMMUNITY_NAMESPACES as r, communityRouteDecision as t, isCommunityNamespacePath as m, isNoticeRoute as n } from "./route-decision.js";
|
|
2
|
+
import { COMMUNITY_UNKNOWN_PATH_BODY as f, communityUnknownPath as i } from "./unknown-path.js";
|
|
3
|
+
import { createCommunityBff as M } from "./bff.js";
|
|
4
|
+
import { communityServerEnv as c } from "./env.js";
|
|
5
|
+
import { COMMUNITY_BODY_MAX_BYTES as O, fromWebRequest as _, parseCommunityJson as x } from "./http.js";
|
|
6
|
+
import { createPrivateGrantHandler as T } from "./grant.js";
|
|
7
|
+
import { createPrivateGrantHandoff as U } from "./grant-handoff.js";
|
|
8
|
+
import { COMMUNITY_HOST_ROUTES as y } from "./host-routes.js";
|
|
7
9
|
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
O as COMMUNITY_BODY_MAX_BYTES,
|
|
11
|
+
y as COMMUNITY_HOST_ROUTES,
|
|
12
|
+
r as COMMUNITY_NAMESPACES,
|
|
13
|
+
f as COMMUNITY_UNKNOWN_PATH_BODY,
|
|
12
14
|
t as communityRouteDecision,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
T as
|
|
17
|
-
|
|
15
|
+
c as communityServerEnv,
|
|
16
|
+
i as communityUnknownPath,
|
|
17
|
+
M as createCommunityBff,
|
|
18
|
+
T as createPrivateGrantHandler,
|
|
19
|
+
U as createPrivateGrantHandoff,
|
|
20
|
+
_ as fromWebRequest,
|
|
21
|
+
m as isCommunityNamespacePath,
|
|
18
22
|
n as isNoticeRoute,
|
|
19
|
-
|
|
23
|
+
x as parseCommunityJson
|
|
20
24
|
};
|
|
@@ -68,8 +68,21 @@ export type AdverseEventOutcome = Open<KnownAdverseEventOutcome>;
|
|
|
68
68
|
export declare const isKnownAdverseEventOutcome: (outcome: unknown) => outcome is KnownAdverseEventOutcome;
|
|
69
69
|
export declare const AGE_BANDS: readonly ["16_17", "18_plus"];
|
|
70
70
|
export type AgeBand = (typeof AGE_BANDS)[number];
|
|
71
|
-
|
|
71
|
+
/**
|
|
72
|
+
* kb-api's LEGAL_TEXT_KINDS (services/legal-texts/legal-texts.service.ts), in its order; kb-api's
|
|
73
|
+
* src/__tests__/legal-kinds-drift.spec.ts reads both copies. M4 PX0 (m4-contracts.md Appendix A.8) adds the private
|
|
74
|
+
* surfaces' member-facing texts, which only the platform publishes (PLATFORM_ONLY_LEGAL_TEXT_KINDS): a host's
|
|
75
|
+
* `legal/current` never has one of them, because the tenant never publishes them and the account host renders them.
|
|
76
|
+
*/
|
|
77
|
+
export declare const LEGAL_TEXT_KINDS: readonly ["rules", "terms", "privacy", "art9_consent", "private_terms", "private_privacy", "private_notice_63e", "private_contact", "private_activation"];
|
|
72
78
|
export type LegalTextKind = (typeof LEGAL_TEXT_KINDS)[number];
|
|
79
|
+
/**
|
|
80
|
+
* The kinds only blogmcp publishes (kb-api's PLATFORM_ONLY_LEGAL_TEXT_KINDS): no host page renders one. It stays
|
|
81
|
+
* off the community barrel, because no host needs it yet; it is here so that the drift spec holds the SDK's copy
|
|
82
|
+
* of the split, and a later slice exports it the day a host must tell the two groups apart.
|
|
83
|
+
*/
|
|
84
|
+
export declare const PLATFORM_ONLY_LEGAL_TEXT_KINDS: readonly ["private_terms", "private_privacy", "private_notice_63e", "private_contact", "private_activation"];
|
|
85
|
+
export type PlatformOnlyLegalTextKind = (typeof PLATFORM_ONLY_LEGAL_TEXT_KINDS)[number];
|
|
73
86
|
export type AcceptanceState = 'accepted' | 'reconsent_required' | 'never';
|
|
74
87
|
export interface MemberAuthor {
|
|
75
88
|
readonly kind: 'member';
|
|
@@ -295,6 +308,19 @@ export interface ModuleInfo {
|
|
|
295
308
|
* (`state.away`). Absent from a kb-api older than M3 — read it through communityAwayOf.
|
|
296
309
|
*/
|
|
297
310
|
readonly away?: CommunityAway | null;
|
|
311
|
+
/**
|
|
312
|
+
* M4 (m4-contracts.md §5.11 "What the hosts read", C.3): the private surfaces' state and who renders the private view.
|
|
313
|
+
* Absent from a kb-api before M4 and on an instance that never had a private admission — read it through the private
|
|
314
|
+
* entry's `privateBlockOf`, which reads an absent or unknown block as `dark` (nothing private is mounted).
|
|
315
|
+
*/
|
|
316
|
+
readonly private?: PrivateModuleBlock;
|
|
317
|
+
}
|
|
318
|
+
/** The states of the private surfaces (§5.13): `braked` stops writes, not reads; `dark` serves nothing private. */
|
|
319
|
+
export type PrivateSurfaceState = 'dark' | 'live' | 'read_only' | 'braked';
|
|
320
|
+
/** MyState's `module.private` (M4 §5.11): the type only — the public entry holds no private code. */
|
|
321
|
+
export interface PrivateModuleBlock {
|
|
322
|
+
readonly state: PrivateSurfaceState;
|
|
323
|
+
readonly rendering: 'tenant' | 'provider_page';
|
|
298
324
|
}
|
|
299
325
|
/**
|
|
300
326
|
* The brake of one surface (M3 §2.3, §4.9): set by the team (≤ 24 h, `tenant`) or by the automatic brake (`auto`); it
|
package/dist/community/shapes.js
CHANGED
|
@@ -32,21 +32,31 @@ const t = (e, o) => typeof o == "string" && e.includes(o), f = Object.freeze(["v
|
|
|
32
32
|
"adverse_event",
|
|
33
33
|
"third_party_health_data",
|
|
34
34
|
"suspected_minor"
|
|
35
|
-
]), g = (e) => t(a, e),
|
|
35
|
+
]), g = (e) => t(a, e), v = "child_at_risk", h = Object.freeze(
|
|
36
36
|
a.filter((e) => e !== "rules" && e !== "adverse_event" && e !== "third_party_health_data" && e !== "suspected_minor")
|
|
37
|
-
),
|
|
37
|
+
), p = Object.freeze(["recorded", "reported_to_authority", "not_adverse"]), K = (e) => t(p, e), D = Object.freeze(["16_17", "18_plus"]), M = Object.freeze([
|
|
38
|
+
"rules",
|
|
39
|
+
"terms",
|
|
40
|
+
"privacy",
|
|
41
|
+
"art9_consent",
|
|
42
|
+
"private_terms",
|
|
43
|
+
"private_privacy",
|
|
44
|
+
"private_notice_63e",
|
|
45
|
+
"private_contact",
|
|
46
|
+
"private_activation"
|
|
47
|
+
]);
|
|
38
48
|
function w(e) {
|
|
39
49
|
return typeof e == "object" && e !== null && l(e.kind);
|
|
40
50
|
}
|
|
41
|
-
const
|
|
51
|
+
const T = Object.freeze(["dark", "live", "read_only", "braked"]), U = (e) => t(T, e), L = Object.freeze(["joint", "split"]), k = Object.freeze(["read_only", "public_write"]), F = (e) => e === "split" ? "split" : "joint", G = (e) => e === "public_write" ? "public_write" : "read_only", P = Object.freeze(["conversation", "member"]), B = Object.freeze(["android", "ios", "desktop", "other"]), x = Object.freeze(["message", "edit", "delete", "reaction", "resync"]);
|
|
42
52
|
function H(e) {
|
|
43
53
|
const o = typeof e == "object" && e !== null ? e.rules : void 0;
|
|
44
54
|
if (!Array.isArray(o)) return [];
|
|
45
55
|
const r = [];
|
|
46
56
|
for (const n of o) {
|
|
47
57
|
if (typeof n != "object" || n === null) continue;
|
|
48
|
-
const { id:
|
|
49
|
-
typeof
|
|
58
|
+
const { id: c, title: s, summary: i, anchor: _ } = n;
|
|
59
|
+
typeof c != "string" || typeof s != "string" || typeof _ != "string" || r.push(Object.freeze({ id: c, title: s, summary: typeof i == "string" ? i : null, anchor: _ }));
|
|
50
60
|
}
|
|
51
61
|
return r;
|
|
52
62
|
}
|
|
@@ -58,8 +68,8 @@ function Y(e) {
|
|
|
58
68
|
for (const n of o) {
|
|
59
69
|
if (r.length >= b) break;
|
|
60
70
|
if (typeof n != "object" || n === null) continue;
|
|
61
|
-
const { entity:
|
|
62
|
-
typeof
|
|
71
|
+
const { entity: c, label: s } = n;
|
|
72
|
+
typeof c != "string" || typeof s != "string" || c.trim() === "" || s.trim() === "" || r.push(Object.freeze({ entity: c, label: s.trim() }));
|
|
63
73
|
}
|
|
64
74
|
return r;
|
|
65
75
|
}
|
|
@@ -69,14 +79,14 @@ function V(e) {
|
|
|
69
79
|
const r = [];
|
|
70
80
|
for (const n of o) {
|
|
71
81
|
if (typeof n != "object" || n === null) continue;
|
|
72
|
-
const { id:
|
|
73
|
-
typeof
|
|
82
|
+
const { id: c, title: s, anchor: i } = n;
|
|
83
|
+
typeof c != "string" || typeof s != "string" || typeof i != "string" || r.push(Object.freeze({ id: c, title: s, anchor: i }));
|
|
74
84
|
}
|
|
75
85
|
return r;
|
|
76
86
|
}
|
|
77
|
-
const A = Object.freeze(["topics", "article_discussions"]), W = (e) => t(A, e), j = Object.freeze(["post", "hold_flagged", "pre"]), X = (e) => t(j, e), q = Object.freeze(["members", "team"]), m = Object.freeze(["open", "locked", "archived"]), Q = (e) => t(m, e), Z = Object.freeze(["topic", "article"]), $ = Object.freeze(["activity", "new"]), J = Object.freeze(["forum", "article"]), ee = Object.freeze(["topic", "message"]), te = Object.freeze(["conversation", "category"]), R = Object.freeze(["forum", "article_discussions"]), oe = (e) => t(R, e), ne = Object.freeze(["case", "premod"]), re = Object.freeze(["open", "decided"]),
|
|
87
|
+
const A = Object.freeze(["topics", "article_discussions"]), W = (e) => t(A, e), j = Object.freeze(["post", "hold_flagged", "pre"]), X = (e) => t(j, e), q = Object.freeze(["members", "team"]), m = Object.freeze(["open", "locked", "archived"]), Q = (e) => t(m, e), Z = Object.freeze(["topic", "article"]), $ = Object.freeze(["activity", "new"]), J = Object.freeze(["forum", "article"]), ee = Object.freeze(["topic", "message"]), te = Object.freeze(["conversation", "category"]), R = Object.freeze(["forum", "article_discussions"]), oe = (e) => t(R, e), ne = Object.freeze(["case", "premod"]), re = Object.freeze(["open", "decided"]), ce = Object.freeze(["member_report", "filter", "staff"]), se = Object.freeze(["no_action", "visibility_restricted", "removal"]), ie = Object.freeze(["notice", "own_initiative"]), _e = Object.freeze(["open", "locked"]), ae = Object.freeze(["approve", "reject"]);
|
|
78
88
|
export {
|
|
79
|
-
|
|
89
|
+
p as ADVERSE_EVENT_OUTCOMES,
|
|
80
90
|
D as AGE_BANDS,
|
|
81
91
|
L as ART9_CONSENT_LAYOUTS,
|
|
82
92
|
b as ART9_CONTROLLERS_MAX,
|
|
@@ -84,8 +94,8 @@ export {
|
|
|
84
94
|
O as AUTHOR_KINDS,
|
|
85
95
|
R as BRAKE_SURFACES,
|
|
86
96
|
a as CASE_CATEGORIES,
|
|
87
|
-
|
|
88
|
-
|
|
97
|
+
v as CATEGORY_WITHOUT_NOTIFIER,
|
|
98
|
+
h as ESCALATION_CATEGORIES,
|
|
89
99
|
te as FOLLOW_TARGET_KINDS,
|
|
90
100
|
A as FORUM_CATEGORY_KINDS,
|
|
91
101
|
j as FORUM_CATEGORY_MODES,
|
|
@@ -96,10 +106,10 @@ export {
|
|
|
96
106
|
ie as MODERATION_BASES,
|
|
97
107
|
re as MODERATION_CASE_STATUSES,
|
|
98
108
|
ne as MODERATION_ITEM_KINDS,
|
|
99
|
-
|
|
100
|
-
|
|
109
|
+
se as MODERATION_RESTRICTIONS,
|
|
110
|
+
ce as MODERATION_SOURCES,
|
|
101
111
|
_e as MODERATOR_TOPIC_STATES,
|
|
102
|
-
|
|
112
|
+
T as MODULE_STATES,
|
|
103
113
|
P as MUTE_TARGET_KINDS,
|
|
104
114
|
u as NEEDS,
|
|
105
115
|
E as NOTIFICATION_CODES,
|
|
@@ -118,7 +128,7 @@ export {
|
|
|
118
128
|
V as communityLegalSectionsOf,
|
|
119
129
|
H as communityRulesOf,
|
|
120
130
|
g as isCaseCategory,
|
|
121
|
-
|
|
131
|
+
K as isKnownAdverseEventOutcome,
|
|
122
132
|
w as isKnownAuthor,
|
|
123
133
|
l as isKnownAuthorKind,
|
|
124
134
|
oe as isKnownBrakeSurface,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blogmcp/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.1",
|
|
4
4
|
"description": "Headless SDK for Knowledge Base blog — core client, Vue composables, Nuxt adapter",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -59,6 +59,10 @@
|
|
|
59
59
|
"./community/realtime": {
|
|
60
60
|
"types": "./dist/community/realtime/index.d.ts",
|
|
61
61
|
"import": "./dist/community/realtime/index.js"
|
|
62
|
+
},
|
|
63
|
+
"./community/private": {
|
|
64
|
+
"types": "./dist/community/private/index.d.ts",
|
|
65
|
+
"import": "./dist/community/private/index.js"
|
|
62
66
|
}
|
|
63
67
|
},
|
|
64
68
|
"main": "./dist/core/index.js",
|
|
@@ -98,8 +102,8 @@
|
|
|
98
102
|
}
|
|
99
103
|
},
|
|
100
104
|
"dependencies": {
|
|
101
|
-
"@blogmcp/mdx-schema": "^0.
|
|
102
|
-
"@blogmcp/sanitize": "^0.
|
|
105
|
+
"@blogmcp/mdx-schema": "^0.14.1",
|
|
106
|
+
"@blogmcp/sanitize": "^0.14.1",
|
|
103
107
|
"marked": "^15.0.7"
|
|
104
108
|
},
|
|
105
109
|
"optionalDependencies": {
|
package/src/community/index.ts
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
* communityMessage every text, cs and en, under the tone test;
|
|
10
10
|
* links statements, the panel, the notifier's receipt in the fragment; M2: profiles, the
|
|
11
11
|
* module's pages and the digest's unsubscribe token in the fragment;
|
|
12
|
-
* communityMentionSpans `@handle` in a body as text spans a page links (never HTML)
|
|
12
|
+
* communityMentionSpans `@handle` in a body as text spans a page links (never HTML);
|
|
13
|
+
* legalTextBlocks M4: a private surface's legal text (its Markdown subset) as blocks a page renders as text.
|
|
13
14
|
*
|
|
14
15
|
* M3, the forum (m3-contracts.md §5.2): the client's `forum.*`, `me.follows.*` and `moderation.*`; createTopicStore
|
|
15
16
|
* (a topic over the discussion store, with its header) and pollTopicHead; the forum's helpers (follow targets, the
|
|
@@ -378,6 +379,10 @@ export {
|
|
|
378
379
|
export { COMMUNITY_MENTION_RE, communityMentionSpans, communityMentionHandles } from './mentions'
|
|
379
380
|
export type { CommunityTextSpan } from './mentions'
|
|
380
381
|
|
|
382
|
+
// M4 (m4-contracts.md §5.11 "Formatting"): a private surface's legal text as blocks — rendered as text, never as HTML.
|
|
383
|
+
export { legalTextBlocks, LEGAL_TEXT_MAX_DEPTH } from './legal-text'
|
|
384
|
+
export type { LegalTextBlock, LegalTextHeading, LegalTextParagraph, LegalTextList, LegalTextListItem, LegalTextRun } from './legal-text'
|
|
385
|
+
|
|
381
386
|
export {
|
|
382
387
|
COMMUNITY_MESSAGES,
|
|
383
388
|
communityMessage,
|