@blogmcp/sdk 0.6.0 → 0.7.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/consultations/channel-text.d.ts +118 -0
- package/dist/consultations/channel-text.js +40 -0
- package/dist/consultations/index.d.ts +28 -0
- package/dist/consultations/index.js +60 -0
- package/dist/consultations/messages.cs.d.ts +149 -0
- package/dist/consultations/messages.cs.js +143 -0
- package/dist/consultations/messages.d.ts +34 -0
- package/dist/consultations/messages.en.d.ts +143 -0
- package/dist/consultations/messages.en.js +141 -0
- package/dist/consultations/messages.js +48 -0
- package/dist/consultations/outcome.d.ts +121 -0
- package/dist/consultations/outcome.js +161 -0
- package/dist/consultations/public-config.d.ts +71 -0
- package/dist/consultations/public-config.js +104 -0
- package/dist/consultations/reader.d.ts +83 -0
- package/dist/consultations/reader.js +128 -0
- package/dist/consultations/routes.d.ts +214 -0
- package/dist/consultations/routes.js +149 -0
- package/dist/consultations/server/client-ip.d.ts +50 -0
- package/dist/consultations/server/client-ip.js +44 -0
- package/dist/consultations/server/contact.d.ts +23 -0
- package/dist/consultations/server/contact.js +138 -0
- package/dist/consultations/server/context.d.ts +246 -0
- package/dist/consultations/server/context.js +152 -0
- package/dist/consultations/server/cookie.d.ts +60 -0
- package/dist/consultations/server/cookie.js +57 -0
- package/dist/consultations/server/env.d.ts +20 -0
- package/dist/consultations/server/env.js +35 -0
- package/dist/consultations/server/http.d.ts +59 -0
- package/dist/consultations/server/http.js +64 -0
- package/dist/consultations/server/index.d.ts +37 -0
- package/dist/consultations/server/index.js +19 -0
- package/dist/consultations/server/origin.d.ts +39 -0
- package/dist/consultations/server/origin.js +31 -0
- package/dist/consultations/server/server.d.ts +106 -0
- package/dist/consultations/server/server.js +122 -0
- package/dist/consultations/server/session.d.ts +38 -0
- package/dist/consultations/server/session.js +210 -0
- package/dist/consultations/server/transport.d.ts +69 -0
- package/dist/consultations/server/transport.js +117 -0
- package/dist/consultations/server/turnstile.d.ts +44 -0
- package/dist/consultations/server/turnstile.js +31 -0
- package/dist/consultations/site.d.ts +100 -0
- package/dist/consultations/site.js +60 -0
- package/dist/consultations/submit-new.d.ts +103 -0
- package/dist/consultations/submit-new.js +72 -0
- package/dist/core/auth.d.ts +2 -2
- package/dist/core/client.d.ts +25 -4
- package/dist/core/client.js +35 -30
- package/dist/core/consultation-adapter.d.ts +2 -2
- package/dist/core/consultations.d.ts +1 -1
- package/dist/core/errors.d.ts +1 -1
- package/dist/core/errors.js +16 -15
- package/dist/core/index.d.ts +31 -31
- package/dist/core/index.js +35 -34
- package/dist/core/markdown.d.ts +1 -1
- package/dist/core/queries.d.ts +4 -1
- package/dist/core/queries.js +131 -86
- package/dist/core/review/format.d.ts +1 -1
- package/dist/core/review/index.d.ts +2 -2
- package/dist/core/seo.d.ts +1 -1
- package/dist/core/translations/cs.json.js +1 -1
- package/dist/core/translations/en.json.js +1 -1
- package/dist/core/types.d.ts +50 -1
- package/dist/core/view-model.d.ts +12 -2
- package/dist/core/view-model.js +40 -34
- package/dist/{packages/tripmap-core/node_modules → node_modules}/pmtiles/dist/esm/index.js +1 -1
- package/dist/nuxt/composables/useAsyncKb.d.ts +4 -4
- package/dist/nuxt/index.d.ts +1 -1
- package/dist/packages/mdx-render-vue/dist/composables/useGallery.js +12 -11
- package/dist/packages/mdx-render-vue/dist/composables/useTripMap.js +16 -16
- package/dist/packages/mdx-render-vue/dist/edit/TripMapEdit.vue.js +1 -1
- package/dist/packages/mdx-render-vue/dist/registry.js +21 -22
- package/dist/packages/mdx-render-vue/dist/render/alert/Alert.vue2.js +1 -1
- package/dist/packages/mdx-render-vue/dist/render/badge/Badge.vue2.js +1 -1
- package/dist/packages/mdx-render-vue/dist/render/button/Button.vue2.js +1 -1
- package/dist/packages/mdx-render-vue/dist/render/callout/CalloutDefault.vue.js +1 -1
- package/dist/packages/mdx-render-vue/dist/render/callout/CalloutMinimal.vue.js +1 -1
- package/dist/packages/mdx-render-vue/dist/render/tripmap/TripMapDefault.vue.js +2 -2
- package/dist/packages/tripmap-core/dist/engine.js +3 -3
- package/dist/packages/tripmap-core/dist/generated/maplibre-worker.bundle.js +1 -1
- package/dist/react/KbMdxContent.d.ts +1 -1
- package/dist/react/KbMdxContent.js +7 -8
- package/dist/react/context.d.ts +4 -4
- package/dist/react/hooks/useImageRenderer.d.ts +1 -1
- package/dist/react/hooks/useKbClient.d.ts +1 -1
- package/dist/react/hooks/useKbContext.d.ts +1 -1
- package/dist/react/hooks/useKbI18n.d.ts +1 -1
- package/dist/react/hooks/useKbLanguage.d.ts +1 -1
- package/dist/react/hooks/useKbSearch.d.ts +1 -1
- package/dist/react/hooks/useMdxContent.d.ts +1 -1
- package/dist/react/index.d.ts +15 -15
- package/dist/react/provider.d.ts +2 -2
- package/dist/react/server/index.d.ts +2 -2
- package/dist/vue/components/KbAccessGate.vue.d.ts +4 -1
- package/dist/vue/components/KbAccessGate.vue.js +42 -32
- package/dist/vue/components/KbArticleCard.vue.d.ts +3 -1
- package/dist/vue/components/KbArticleCard.vue.js +67 -39
- package/dist/vue/components/KbMdxContent.vue2.js +6 -7
- package/dist/vue/composables/useArticles.d.ts +1 -1
- package/dist/vue/composables/useImageRenderer.d.ts +1 -1
- package/dist/vue/composables/useKbAuth.d.ts +1 -1
- package/dist/vue/composables/useKbClient.d.ts +1 -1
- package/dist/vue/composables/useKbComments.d.ts +1 -1
- package/dist/vue/composables/useKbI18n.d.ts +1 -1
- package/dist/vue/composables/useKbLanguage.d.ts +1 -1
- package/dist/vue/composables/useNavigation.d.ts +1 -1
- package/dist/vue/composables/useSearch.d.ts +1 -1
- package/dist/vue/composables/useTags.d.ts +1 -1
- package/dist/vue/index.d.ts +19 -19
- package/dist/vue/plugin.d.ts +4 -4
- package/package.json +15 -5
- package/src/consultations/channel-text.ts +168 -0
- package/src/consultations/index.ts +87 -0
- package/src/consultations/messages.cs.ts +175 -0
- package/src/consultations/messages.en.ts +171 -0
- package/src/consultations/messages.ts +91 -0
- package/src/consultations/outcome.ts +278 -0
- package/src/consultations/public-config.ts +246 -0
- package/src/consultations/reader.ts +281 -0
- package/src/consultations/routes.ts +341 -0
- package/src/consultations/server/client-ip.ts +106 -0
- package/src/consultations/server/contact.ts +267 -0
- package/src/consultations/server/context.ts +403 -0
- package/src/consultations/server/cookie.ts +129 -0
- package/src/consultations/server/env.ts +66 -0
- package/src/consultations/server/http.ts +148 -0
- package/src/consultations/server/index.ts +55 -0
- package/src/consultations/server/origin.ts +71 -0
- package/src/consultations/server/server.ts +322 -0
- package/src/consultations/server/session.ts +414 -0
- package/src/consultations/server/transport.ts +196 -0
- package/src/consultations/server/turnstile.ts +98 -0
- package/src/consultations/site.ts +183 -0
- package/src/consultations/submit-new.ts +266 -0
- package/src/core/auth.ts +1 -1
- package/src/core/client.ts +32 -1
- package/src/core/errors.ts +5 -2
- package/src/core/index.ts +3 -0
- package/src/core/queries.ts +56 -0
- package/src/core/translations/cs.json +2 -1
- package/src/core/translations/en.json +2 -1
- package/src/core/types.ts +51 -0
- package/src/core/view-model.ts +18 -0
- package/src/vue/components/KbAccessGate.vue +14 -1
- package/src/vue/components/KbArticleCard.vue +25 -0
- package/dist/_virtual/constants.js +0 -4
- package/dist/_virtual/index.js +0 -2
- package/dist/_virtual/index2.js +0 -4
- package/dist/_virtual/index3.js +0 -2
- package/dist/_virtual/re.js +0 -4
- package/dist/node_modules/@braintree/sanitize-url/dist/constants.js +0 -8
- package/dist/node_modules/@braintree/sanitize-url/dist/index.js +0 -60
- package/dist/node_modules/dompurify/dist/purify.es.js +0 -662
- package/dist/node_modules/semver/classes/comparator.js +0 -56
- package/dist/node_modules/semver/classes/range.js +0 -148
- package/dist/node_modules/semver/classes/semver.js +0 -169
- package/dist/node_modules/semver/functions/clean.js +0 -14
- package/dist/node_modules/semver/functions/cmp.js +0 -39
- package/dist/node_modules/semver/functions/coerce.js +0 -33
- package/dist/node_modules/semver/functions/compare-build.js +0 -14
- package/dist/node_modules/semver/functions/compare-loose.js +0 -11
- package/dist/node_modules/semver/functions/compare.js +0 -11
- package/dist/node_modules/semver/functions/diff.js +0 -24
- package/dist/node_modules/semver/functions/eq.js +0 -11
- package/dist/node_modules/semver/functions/gt.js +0 -11
- package/dist/node_modules/semver/functions/gte.js +0 -11
- package/dist/node_modules/semver/functions/inc.js +0 -21
- package/dist/node_modules/semver/functions/lt.js +0 -11
- package/dist/node_modules/semver/functions/lte.js +0 -11
- package/dist/node_modules/semver/functions/major.js +0 -11
- package/dist/node_modules/semver/functions/minor.js +0 -11
- package/dist/node_modules/semver/functions/neq.js +0 -11
- package/dist/node_modules/semver/functions/parse.js +0 -21
- package/dist/node_modules/semver/functions/patch.js +0 -11
- package/dist/node_modules/semver/functions/prerelease.js +0 -14
- package/dist/node_modules/semver/functions/rcompare.js +0 -11
- package/dist/node_modules/semver/functions/rsort.js +0 -11
- package/dist/node_modules/semver/functions/satisfies.js +0 -18
- package/dist/node_modules/semver/functions/sort.js +0 -11
- package/dist/node_modules/semver/functions/valid.js +0 -14
- package/dist/node_modules/semver/index.js +0 -97
- package/dist/node_modules/semver/internal/constants.js +0 -28
- package/dist/node_modules/semver/internal/debug.js +0 -8
- package/dist/node_modules/semver/internal/identifiers.js +0 -18
- package/dist/node_modules/semver/internal/lrucache.js +0 -32
- package/dist/node_modules/semver/internal/parse-options.js +0 -10
- package/dist/node_modules/semver/internal/re.js +0 -32
- package/dist/node_modules/semver/ranges/gtr.js +0 -11
- package/dist/node_modules/semver/ranges/intersects.js +0 -11
- package/dist/node_modules/semver/ranges/ltr.js +0 -11
- package/dist/node_modules/semver/ranges/max-satisfying.js +0 -22
- package/dist/node_modules/semver/ranges/min-satisfying.js +0 -22
- package/dist/node_modules/semver/ranges/min-version.js +0 -42
- package/dist/node_modules/semver/ranges/outside.js +0 -44
- package/dist/node_modules/semver/ranges/simplify.js +0 -24
- package/dist/node_modules/semver/ranges/subset.js +0 -94
- package/dist/node_modules/semver/ranges/to-comparators.js +0 -11
- package/dist/node_modules/semver/ranges/valid.js +0 -17
- package/dist/packages/mdx-schema/dist/composables/useMdxParsing.js +0 -389
- package/dist/packages/mdx-schema/dist/metadata/index.js +0 -526
- package/dist/packages/mdx-schema/dist/prepareProps.js +0 -46
- package/dist/packages/mdx-schema/dist/styles/index.js +0 -147
- package/dist/packages/mdx-schema/dist/tripmap.js +0 -101
- package/dist/packages/mdx-schema/dist/types/index.js +0 -26
- package/dist/packages/sanitize/dist/html.js +0 -89
- /package/dist/{packages/tripmap-core/node_modules → node_modules}/maplibre-gl/dist/maplibre-gl-shared.js +0 -0
- /package/dist/{packages/tripmap-core/node_modules → node_modules}/maplibre-gl/dist/maplibre-gl.js +0 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createConsultationServer` — one object per blog process that serves every
|
|
3
|
+
* reader consultation page and endpoint (docs/internal/reader-consultations.md;
|
|
4
|
+
* the F5 plan §3).
|
|
5
|
+
*
|
|
6
|
+
* The blog's keys live only in this closure: the reader-proxy key (a PUBLIC
|
|
7
|
+
* service key — session calls, the public config, link requests) and the
|
|
8
|
+
* adapter key (`consultations:ingest:contact_form` — the contact form only).
|
|
9
|
+
* Each goes only into the `Authorization` header of its own kind of call; no
|
|
10
|
+
* handler ever puts a key, a session, a link token or a JWT into a response
|
|
11
|
+
* body or a log line.
|
|
12
|
+
*
|
|
13
|
+
* A missing key never throws: the pages are dark (404) instead, and one
|
|
14
|
+
* console warning says why. The one thing that throws is running it in a
|
|
15
|
+
* browser.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { assertServerOnly } from '../../core/server-only'
|
|
19
|
+
import {
|
|
20
|
+
CONSULTATION_TURNSTILE_ACTION,
|
|
21
|
+
CONTACT_MESSAGE_MAX,
|
|
22
|
+
ConsultationTransportError,
|
|
23
|
+
READER_BODY_MAX,
|
|
24
|
+
createPublicConfigCache,
|
|
25
|
+
newClientRequestId,
|
|
26
|
+
parseSiteUrls,
|
|
27
|
+
type ConsultationLogger,
|
|
28
|
+
type ConsultationSiteStatus,
|
|
29
|
+
type PublicConfigCache,
|
|
30
|
+
} from '../index'
|
|
31
|
+
import { DEFAULT_CLIENT_IP_HEADER, isHeaderName } from './client-ip'
|
|
32
|
+
import { handleContact, loadContact } from './contact'
|
|
33
|
+
import {
|
|
34
|
+
GATE_RETRY_AFTER_S,
|
|
35
|
+
defaultConsultationLogger,
|
|
36
|
+
gate,
|
|
37
|
+
hygienicLogger,
|
|
38
|
+
responseHeaders,
|
|
39
|
+
siteStatusOf,
|
|
40
|
+
type AccessResult,
|
|
41
|
+
type AttachResult,
|
|
42
|
+
type ConsultationFeature,
|
|
43
|
+
type ForgetResult,
|
|
44
|
+
type ConsultationPageGate,
|
|
45
|
+
type ContactPageState,
|
|
46
|
+
type ContactResult,
|
|
47
|
+
type RequestLinkResult,
|
|
48
|
+
type ResolvedTurnstile,
|
|
49
|
+
type ServerContext,
|
|
50
|
+
type SessionActionResult,
|
|
51
|
+
type SessionPageState,
|
|
52
|
+
} from './context'
|
|
53
|
+
import { CONSULTATION_COOKIE_NAME_RE, CONSULTATION_SESSION_COOKIE } from './cookie'
|
|
54
|
+
import type { ConsultationHttp, ConsultationResponse } from './http'
|
|
55
|
+
import { originOf, siteOrigins } from './origin'
|
|
56
|
+
import {
|
|
57
|
+
bearerToken,
|
|
58
|
+
handleAccess,
|
|
59
|
+
handleAttach,
|
|
60
|
+
handleForget,
|
|
61
|
+
handleRequestLink,
|
|
62
|
+
handleSessionAction,
|
|
63
|
+
loadSession,
|
|
64
|
+
} from './session'
|
|
65
|
+
import { HEADER_TOKEN_RE, createKbTransport, kbApiBase } from './transport'
|
|
66
|
+
|
|
67
|
+
export interface ConsultationServerConfig {
|
|
68
|
+
/** The server-side kb-api base URL: GraphQL at `${kbApiUrl}/graphql`, the adapter at `${kbApiUrl}/consultations/adapter`. */
|
|
69
|
+
kbApiUrl: string
|
|
70
|
+
/**
|
|
71
|
+
* The blog's own URLs (`parseSiteUrls(KB_MANAGED_BLOG_URLS, SITE_URL)`): the
|
|
72
|
+
* pages are live only when the KB's reader site is one of them, and their
|
|
73
|
+
* origins are the Origin allowlist.
|
|
74
|
+
*/
|
|
75
|
+
siteUrls: string[]
|
|
76
|
+
/** The PUBLIC reader-proxy service key: session calls, the public config, link requests. Missing → dark. */
|
|
77
|
+
readerProxyKey?: string | null
|
|
78
|
+
/** The adapter key (scope `consultations:ingest:contact_form`, no ref prefix): the contact form only. Missing → no form. */
|
|
79
|
+
adapterKey?: string | null
|
|
80
|
+
/** The edge header holding the reader's IP. Default `cf-connecting-ip`; `''` = none (not behind Cloudflare). */
|
|
81
|
+
clientIpHeader?: string
|
|
82
|
+
/** Origins allowed to POST besides the site URLs' own. */
|
|
83
|
+
allowedOrigins?: string[]
|
|
84
|
+
/** The session cookie's name; must match `^__Host-[A-Za-z0-9_]+$`. Default `__Host-kb_consult`. */
|
|
85
|
+
cookieName?: string
|
|
86
|
+
/**
|
|
87
|
+
* Cloudflare Turnstile on the contact form. `action` defaults to
|
|
88
|
+
* `consultation_contact`; `failOpen` (default true) lets a question through
|
|
89
|
+
* when Cloudflare cannot be reached — `false` refuses it (`unavailable`).
|
|
90
|
+
*/
|
|
91
|
+
turnstile?: { siteKey: string; secretKey: string; action?: string; failOpen?: boolean } | null
|
|
92
|
+
/** The signed-in reader's JWT for `handleAttach`. Default: `Authorization: Bearer <JWT>`. */
|
|
93
|
+
readerToken?: (http: ConsultationHttp) => string | null
|
|
94
|
+
/** `contactMessage`: the contact form's longest message (default 2000, at most 5000). */
|
|
95
|
+
limits?: { contactMessage?: number }
|
|
96
|
+
/** Per KB request (default 10 000 ms). */
|
|
97
|
+
timeoutMs?: number
|
|
98
|
+
/** Custom fetch for every outbound call (KB and Turnstile); default the global `fetch`, resolved per call. */
|
|
99
|
+
fetch?: typeof fetch
|
|
100
|
+
/** The clock (tests). */
|
|
101
|
+
now?: () => number
|
|
102
|
+
/** Idempotency keys the pages are rendered with (tests); default a UUID. */
|
|
103
|
+
randomId?: () => string
|
|
104
|
+
/** Gets outages and the unexpected only; default one `console.warn` line. */
|
|
105
|
+
logger?: ConsultationLogger
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface ConsultationServer {
|
|
109
|
+
/** Whether this blog serves the reader pages in `locale`, and which (for logs, smoke tests, menus). */
|
|
110
|
+
siteStatus(locale: string): Promise<ConsultationSiteStatus>
|
|
111
|
+
/**
|
|
112
|
+
* Non-blocking (menus, footers): the status from the memo as it is, or null
|
|
113
|
+
* when nothing is known for `locale` yet; a load starts in the background.
|
|
114
|
+
*/
|
|
115
|
+
siteStatus(locale: string, o: { peek: true }): Promise<ConsultationSiteStatus | null>
|
|
116
|
+
siteStatus(locale: string, o?: { peek?: boolean }): Promise<ConsultationSiteStatus | null>
|
|
117
|
+
/** The Origin allowlist the handlers use — for a blog's `/api` proxy: `isAllowedOrigin(http, allowedOrigins())`. */
|
|
118
|
+
allowedOrigins(): readonly string[]
|
|
119
|
+
/** A loader's answer when it needs only the gate: 200, 404 (dark or the feature off) or 503 (`Retry-After: 30`). */
|
|
120
|
+
pageGate(feature: ConsultationFeature, locale: string): Promise<ConsultationPageGate>
|
|
121
|
+
/** GET `/consultation`: the thread the session cookie opens. */
|
|
122
|
+
loadSession(http: ConsultationHttp): Promise<ConsultationResponse<SessionPageState>>
|
|
123
|
+
/** POST `/api/consultations/access` (`intent=exchange&token=…`): the link token → the session cookie. */
|
|
124
|
+
handleAccess(http: ConsultationHttp): Promise<ConsultationResponse<AccessResult>>
|
|
125
|
+
/** POST `/consultation` (`intent` = `reply` | `erase` | `logout`). Works without JavaScript. */
|
|
126
|
+
handleSessionAction(http: ConsultationHttp): Promise<ConsultationResponse<SessionActionResult>>
|
|
127
|
+
/**
|
|
128
|
+
* POST `/api/consultations/forget`: clear a session cookie the KB no longer
|
|
129
|
+
* accepts (204 no session, 410 cleared, 200 still valid — left alone).
|
|
130
|
+
*/
|
|
131
|
+
handleForget(http: ConsultationHttp): Promise<ConsultationResponse<ForgetResult>>
|
|
132
|
+
/** POST `/api/consultations/attach`: move the session's thread to the signed-in reader's account (their JWT). */
|
|
133
|
+
handleAttach(http: ConsultationHttp): Promise<ConsultationResponse<AttachResult>>
|
|
134
|
+
/** POST `/consultation/new-link`: e-mail fresh links; the same answer whatever the address. */
|
|
135
|
+
handleRequestLink(http: ConsultationHttp): Promise<ConsultationResponse<RequestLinkResult>>
|
|
136
|
+
/** GET `/contact`: the form's legal text, its idempotency key and the Turnstile site key. */
|
|
137
|
+
loadContact(http: ConsultationHttp, locale: string): Promise<ConsultationResponse<ContactPageState>>
|
|
138
|
+
/** POST `/contact`: the question becomes a KB thread (adapter key). */
|
|
139
|
+
handleContact(http: ConsultationHttp, locale: string): Promise<ConsultationResponse<ContactResult>>
|
|
140
|
+
/** The server's memo of the KB's public config (identity-free: the proxy key, no reader IP, no JWT). */
|
|
141
|
+
publicConfig: PublicConfigCache
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const DEFAULT_TIMEOUT_MS = 10_000
|
|
145
|
+
|
|
146
|
+
/** A key from config: trimmed (secret files end in a newline), null when empty or not a header-safe token. */
|
|
147
|
+
function keyOf(
|
|
148
|
+
value: string | null | undefined,
|
|
149
|
+
name: string,
|
|
150
|
+
warnOnce: ServerContext['warnOnce'],
|
|
151
|
+
): string | null {
|
|
152
|
+
if (typeof value !== 'string') return null
|
|
153
|
+
const key = value.trim()
|
|
154
|
+
if (key === '') return null
|
|
155
|
+
if (!HEADER_TOKEN_RE.test(key)) {
|
|
156
|
+
// Never echo the key.
|
|
157
|
+
warnOnce(`${name}_invalid`, `[consultations] the ${name} contains characters not allowed in an HTTP header — ignored.`)
|
|
158
|
+
return null
|
|
159
|
+
}
|
|
160
|
+
return key
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function turnstileOf(
|
|
164
|
+
t: ConsultationServerConfig['turnstile'],
|
|
165
|
+
warnOnce: ServerContext['warnOnce'],
|
|
166
|
+
): ResolvedTurnstile | null {
|
|
167
|
+
if (!t) return null
|
|
168
|
+
const siteKey = typeof t.siteKey === 'string' ? t.siteKey.trim() : ''
|
|
169
|
+
const secretKey = typeof t.secretKey === 'string' ? t.secretKey.trim() : ''
|
|
170
|
+
if (!siteKey || !secretKey) {
|
|
171
|
+
warnOnce('turnstile_half', '[consultations] Turnstile needs both a site key and a secret key — the contact form runs without it.')
|
|
172
|
+
return null
|
|
173
|
+
}
|
|
174
|
+
const action = typeof t.action === 'string' && t.action.trim() !== '' ? t.action.trim() : CONSULTATION_TURNSTILE_ACTION
|
|
175
|
+
return { siteKey, secretKey, action, failOpen: t.failOpen !== false }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function createConsultationServer(c: ConsultationServerConfig): ConsultationServer {
|
|
179
|
+
assertServerOnly(
|
|
180
|
+
'createConsultationServer',
|
|
181
|
+
"it holds the blog's consultation keys and readers' sessions, which must never reach the browser.",
|
|
182
|
+
)
|
|
183
|
+
const config = c ?? ({} as ConsultationServerConfig)
|
|
184
|
+
|
|
185
|
+
const warned = new Set<string>()
|
|
186
|
+
const warnOnce: ServerContext['warnOnce'] = (key, message) => {
|
|
187
|
+
if (warned.has(key)) return
|
|
188
|
+
warned.add(key)
|
|
189
|
+
console.warn(message)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const base = kbApiBase(config.kbApiUrl)
|
|
193
|
+
if (base === null) {
|
|
194
|
+
warnOnce('kb_api_url', '[consultations] kbApiUrl is not an absolute http(s) URL — every consultation page answers 503.')
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let clientIpHeader = typeof config.clientIpHeader === 'string' ? config.clientIpHeader.trim() : DEFAULT_CLIENT_IP_HEADER
|
|
198
|
+
if (clientIpHeader !== '' && !isHeaderName(clientIpHeader)) {
|
|
199
|
+
warnOnce('client_ip_header', '[consultations] clientIpHeader is not an HTTP header name — no edge header is read.')
|
|
200
|
+
clientIpHeader = ''
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const cookieName = config.cookieName ?? CONSULTATION_SESSION_COOKIE
|
|
204
|
+
if (!CONSULTATION_COOKIE_NAME_RE.test(cookieName)) {
|
|
205
|
+
throw new TypeError('[blogmcp-sdk] consultations cookieName must match ^__Host-[A-Za-z0-9_]+$')
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const siteUrls = parseSiteUrls(Array.isArray(config.siteUrls) ? config.siteUrls : [])
|
|
209
|
+
const extras = Array.isArray(config.allowedOrigins) ? config.allowedOrigins : []
|
|
210
|
+
if (extras.some((o) => originOf(o) === null)) {
|
|
211
|
+
warnOnce('allowed_origins', '[consultations] an allowedOrigins entry is not an http(s) URL — ignored.')
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const contactLimit = config.limits?.contactMessage
|
|
215
|
+
const contactMessageMax =
|
|
216
|
+
typeof contactLimit === 'number' && Number.isInteger(contactLimit) && contactLimit >= 1
|
|
217
|
+
? Math.min(contactLimit, READER_BODY_MAX)
|
|
218
|
+
: CONTACT_MESSAGE_MAX
|
|
219
|
+
const timeoutMs =
|
|
220
|
+
typeof config.timeoutMs === 'number' && Number.isFinite(config.timeoutMs) && config.timeoutMs > 0
|
|
221
|
+
? config.timeoutMs
|
|
222
|
+
: DEFAULT_TIMEOUT_MS
|
|
223
|
+
|
|
224
|
+
const now = config.now ?? Date.now
|
|
225
|
+
const fetchFn = (): typeof fetch => config.fetch ?? globalThis.fetch
|
|
226
|
+
const logger = hygienicLogger(config.logger ?? defaultConsultationLogger)
|
|
227
|
+
const transport = base === null ? null : createKbTransport({ base, fetch: fetchFn, timeoutMs, now })
|
|
228
|
+
const proxyKey = keyOf(config.readerProxyKey, 'reader-proxy key', warnOnce)
|
|
229
|
+
const adapterKey = keyOf(config.adapterKey, 'adapter key', warnOnce)
|
|
230
|
+
|
|
231
|
+
// ONE memo per server instance, loaded identity-free: the proxy key, no reader IP, no JWT —
|
|
232
|
+
// whoever's request triggers a load, the answer is everyone's. Without the key nothing is
|
|
233
|
+
// asked at all (the pages are dark whatever the KB would say).
|
|
234
|
+
const publicConfig = createPublicConfigCache(
|
|
235
|
+
async (locale) => {
|
|
236
|
+
if (!transport) throw new ConsultationTransportError('kbApiUrl is not configured')
|
|
237
|
+
if (!proxyKey) throw new ConsultationTransportError('no reader-proxy key is configured')
|
|
238
|
+
return transport.graphql({ token: proxyKey, clientIp: null }).consultations.publicConfig(locale)
|
|
239
|
+
},
|
|
240
|
+
{ now, logger },
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
const ctx: ServerContext = {
|
|
244
|
+
transport,
|
|
245
|
+
siteUrls,
|
|
246
|
+
origins: siteOrigins(siteUrls, extras),
|
|
247
|
+
proxyKey,
|
|
248
|
+
adapterKey,
|
|
249
|
+
clientIpHeader,
|
|
250
|
+
cookieName,
|
|
251
|
+
turnstile: turnstileOf(config.turnstile, warnOnce),
|
|
252
|
+
readerToken: typeof config.readerToken === 'function' ? config.readerToken : bearerToken,
|
|
253
|
+
contactMessageMax,
|
|
254
|
+
timeoutMs,
|
|
255
|
+
fetch: fetchFn,
|
|
256
|
+
now,
|
|
257
|
+
randomId: typeof config.randomId === 'function' ? config.randomId : newClientRequestId,
|
|
258
|
+
publicConfig,
|
|
259
|
+
logger,
|
|
260
|
+
warnOnce,
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** A handler that never throws: an unexpected error is an outage (logged without detail). */
|
|
264
|
+
const guarded =
|
|
265
|
+
<A extends unknown[], B>(op: string, run: (...args: A) => Promise<ConsultationResponse<B>>, fallback: () => B) =>
|
|
266
|
+
async (...args: A): Promise<ConsultationResponse<B>> => {
|
|
267
|
+
try {
|
|
268
|
+
return await run(...args)
|
|
269
|
+
} catch {
|
|
270
|
+
logger({ op, outcome: 'unavailable', kbCode: null, status: null })
|
|
271
|
+
return { status: 503, headers: responseHeaders(503), body: fallback() }
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
siteStatus: ((locale: string, o?: { peek?: boolean }) =>
|
|
277
|
+
o?.peek ? siteStatusOf(ctx, locale, { peek: true }) : siteStatusOf(ctx, locale)) as ConsultationServer['siteStatus'],
|
|
278
|
+
allowedOrigins: () => ctx.origins,
|
|
279
|
+
async pageGate(feature, locale) {
|
|
280
|
+
const g = await gate(ctx, feature, locale)
|
|
281
|
+
if (g.ok) return { status: 200, headers: responseHeaders(200) }
|
|
282
|
+
if (g.code === 'unavailable') return { status: 503, headers: responseHeaders(503, { retryAfterS: g.retryAfterS }) }
|
|
283
|
+
return { status: 404, headers: responseHeaders(404) }
|
|
284
|
+
},
|
|
285
|
+
loadSession: guarded('loadSession', (http: ConsultationHttp) => loadSession(ctx, http), () => ({
|
|
286
|
+
state: 'error' as const,
|
|
287
|
+
error: 'unavailable' as const,
|
|
288
|
+
retryAfterS: GATE_RETRY_AFTER_S,
|
|
289
|
+
})),
|
|
290
|
+
handleAccess: guarded('access', (http: ConsultationHttp) => handleAccess(ctx, http), () => ({
|
|
291
|
+
success: false as const,
|
|
292
|
+
error: 'unavailable' as const,
|
|
293
|
+
})),
|
|
294
|
+
handleSessionAction: guarded('sessionAction', (http: ConsultationHttp) => handleSessionAction(ctx, http), () => ({
|
|
295
|
+
success: false as const,
|
|
296
|
+
error: 'unavailable' as const,
|
|
297
|
+
})),
|
|
298
|
+
handleForget: guarded('forget', (http: ConsultationHttp) => handleForget(ctx, http), () => ({
|
|
299
|
+
success: false as const,
|
|
300
|
+
error: 'unavailable' as const,
|
|
301
|
+
})),
|
|
302
|
+
handleAttach: guarded('attach', (http: ConsultationHttp) => handleAttach(ctx, http), () => ({
|
|
303
|
+
success: false as const,
|
|
304
|
+
error: 'unavailable' as const,
|
|
305
|
+
})),
|
|
306
|
+
handleRequestLink: guarded('requestLink', (http: ConsultationHttp) => handleRequestLink(ctx, http), () => ({
|
|
307
|
+
done: false as const,
|
|
308
|
+
error: 'unavailable' as const,
|
|
309
|
+
})),
|
|
310
|
+
loadContact: guarded(
|
|
311
|
+
'loadContact',
|
|
312
|
+
(http: ConsultationHttp, locale: string) => loadContact(ctx, http, locale),
|
|
313
|
+
() => ({ state: 'error' as const, error: 'unavailable' as const, retryAfterS: GATE_RETRY_AFTER_S }),
|
|
314
|
+
),
|
|
315
|
+
handleContact: guarded(
|
|
316
|
+
'contact',
|
|
317
|
+
(http: ConsultationHttp, locale: string) => handleContact(ctx, http, locale),
|
|
318
|
+
() => ({ done: false as const, error: 'unavailable' as const, messageId: `blog-contact:${newClientRequestId()}` }),
|
|
319
|
+
),
|
|
320
|
+
publicConfig,
|
|
321
|
+
}
|
|
322
|
+
}
|