@blogmcp/sdk 0.6.1 → 0.7.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/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/auth.js +21 -21
- 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/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/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/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 +5 -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/{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,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The KB's public consultation config (`consultationPublicConfig(locale)`),
|
|
3
|
+
* memoised per locale.
|
|
4
|
+
*
|
|
5
|
+
* It is the same for everyone, and every reader page needs it: the reader-site
|
|
6
|
+
* gate (`readerSiteStatus`), the contact form's text, the new-question page in
|
|
7
|
+
* notice mode. One memo per server instance (or per browser tab) keeps that
|
|
8
|
+
* at one KB call per locale per five minutes.
|
|
9
|
+
*
|
|
10
|
+
* - A success is remembered for 5 minutes, a failure for 30 seconds: a KB
|
|
11
|
+
* outage must not switch the pages off for five minutes.
|
|
12
|
+
* - Concurrent requests for one locale share one load.
|
|
13
|
+
* - `fresh` always loads — an action checking that the reader saw the CURRENT
|
|
14
|
+
* text — and a success replaces the memo, so the next page view shows the
|
|
15
|
+
* new text at once. A failed `fresh` load never replaces a good memo.
|
|
16
|
+
* - Only an allowlisted, frozen copy is kept: a field the KB (or a proxy in
|
|
17
|
+
* between) adds is never cached and never handed out.
|
|
18
|
+
* - The locale is checked first; nothing is loaded for a malformed one, and at
|
|
19
|
+
* most `maxLocales` locales are kept (least recently used goes first).
|
|
20
|
+
*
|
|
21
|
+
* `get` never throws. `peek` answers from the memo alone, synchronously —
|
|
22
|
+
* for a status that must not wait for the KB. Load it with an identity-free
|
|
23
|
+
* client (the reader-proxy key, no reader IP, no reader JWT): the answer is
|
|
24
|
+
* shared by everyone, so whoever triggered the load must not matter.
|
|
25
|
+
*/
|
|
26
|
+
import type { ConsultationPublicConfig } from '../core/types.js';
|
|
27
|
+
import { type ConsultationLogger, type ConsultationOutcome } from './outcome.js';
|
|
28
|
+
export interface PublicConfigCache {
|
|
29
|
+
/** The config in `locale`; `fresh` skips the memo (and refreshes it on success). Never throws. */
|
|
30
|
+
get(locale: string, o?: {
|
|
31
|
+
fresh?: boolean;
|
|
32
|
+
}): Promise<ConsultationOutcome<ConsultationPublicConfig>>;
|
|
33
|
+
/**
|
|
34
|
+
* What is remembered for `locale` right now — a config or a failure, while
|
|
35
|
+
* its TTL lasts — without loading and without waiting: `undefined` when
|
|
36
|
+
* nothing is remembered (never loaded, expired, a load still in flight) or
|
|
37
|
+
* the locale is malformed. A peek does not count as a use for the
|
|
38
|
+
* least-recently-used order.
|
|
39
|
+
*/
|
|
40
|
+
peek(locale: string): ConsultationOutcome<ConsultationPublicConfig> | undefined;
|
|
41
|
+
/** Forget everything remembered (loads still in flight are not remembered either). */
|
|
42
|
+
clear(): void;
|
|
43
|
+
}
|
|
44
|
+
export interface PublicConfigCacheOptions {
|
|
45
|
+
/** How long a loaded config is used. Default 5 minutes. */
|
|
46
|
+
ttlMs?: number;
|
|
47
|
+
/** How long a failure is remembered. Default 30 seconds. */
|
|
48
|
+
failureTtlMs?: number;
|
|
49
|
+
/** How many locales are kept. Default 8. */
|
|
50
|
+
maxLocales?: number;
|
|
51
|
+
/** The clock (tests). */
|
|
52
|
+
now?: () => number;
|
|
53
|
+
/** Told about failures that are outages or unexpected (`unavailable`, `submit`). */
|
|
54
|
+
logger?: ConsultationLogger;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The public config reduced to what the SDK uses, as a frozen copy: `enabled`,
|
|
58
|
+
* `readerSiteUrl`, `emailNotices`, and `channels[].{channel, mode, text{purpose,
|
|
59
|
+
* version, locale, body}}` — nothing else. Null when the answer is malformed
|
|
60
|
+
* (not an object, `enabled` or `emailNotices` not booleans, `readerSiteUrl`
|
|
61
|
+
* neither a string nor null, `channels` not a list). A channel of an unknown
|
|
62
|
+
* shape or mode is dropped, and a malformed text becomes no text; the rest of
|
|
63
|
+
* the config stays usable.
|
|
64
|
+
*/
|
|
65
|
+
export declare function sanitizePublicConfig(raw: unknown): ConsultationPublicConfig | null;
|
|
66
|
+
/**
|
|
67
|
+
* A memo of the public config per locale. `load` asks the KB, e.g.
|
|
68
|
+
* `(locale) => client.consultations.publicConfig(locale)`; whatever it throws
|
|
69
|
+
* becomes a code (`consultationFailureOf`), whatever it returns is sanitised.
|
|
70
|
+
*/
|
|
71
|
+
export declare function createPublicConfigCache(load: (locale: string) => Promise<unknown>, o?: PublicConfigCacheOptions): PublicConfigCache;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { consultationFailureOf as M, isLoggedConsultationOutcome as T, isConsultationTransportError as j } from "./outcome.js";
|
|
2
|
+
import { LOCALE_RE as x } from "./routes.js";
|
|
3
|
+
import { isKbGraphQLError as z } from "../core/errors.js";
|
|
4
|
+
const C = 300 * 1e3, _ = 30 * 1e3, U = 8, b = Object.freeze({ ok: !1, code: "unavailable" }), F = Object.freeze({ ok: !1, code: "invalid" });
|
|
5
|
+
function A(t) {
|
|
6
|
+
return typeof t == "object" && t !== null && !Array.isArray(t);
|
|
7
|
+
}
|
|
8
|
+
function v(t) {
|
|
9
|
+
if (!A(t)) return null;
|
|
10
|
+
const { purpose: n, version: i, locale: c, body: l } = t;
|
|
11
|
+
return n !== "notice" && n !== "consent" || typeof i != "string" || typeof c != "string" || typeof l != "string" ? null : Object.freeze({ purpose: n, version: i, locale: c, body: l });
|
|
12
|
+
}
|
|
13
|
+
function q(t) {
|
|
14
|
+
if (!A(t)) return null;
|
|
15
|
+
const { channel: n, mode: i, text: c } = t;
|
|
16
|
+
return typeof n != "string" || n === "" || i !== "notice" && i !== "consent" ? null : Object.freeze({ channel: n, mode: i, text: v(c) });
|
|
17
|
+
}
|
|
18
|
+
function w(t) {
|
|
19
|
+
if (!A(t)) return null;
|
|
20
|
+
const { enabled: n, readerSiteUrl: i, emailNotices: c, channels: l } = t;
|
|
21
|
+
if (typeof n != "boolean" || typeof c != "boolean" || i !== null && typeof i != "string" || !Array.isArray(l)) return null;
|
|
22
|
+
const a = l.map(q).filter((m) => m !== null);
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
enabled: n,
|
|
25
|
+
readerSiteUrl: i,
|
|
26
|
+
emailNotices: c,
|
|
27
|
+
channels: Object.freeze(a)
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function h(t, n) {
|
|
31
|
+
return typeof t == "number" && Number.isFinite(t) && t >= 0 ? t : n;
|
|
32
|
+
}
|
|
33
|
+
function N(t, n = {}) {
|
|
34
|
+
const i = h(n.ttlMs, C), c = h(n.failureTtlMs, _), l = Math.max(1, Math.floor(h(n.maxLocales, U))), a = n.now ?? Date.now, m = n.logger, f = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map();
|
|
35
|
+
let k = 0, y = 0;
|
|
36
|
+
function L(e, r) {
|
|
37
|
+
if (!(e.ok || !m || !T(e.code)))
|
|
38
|
+
try {
|
|
39
|
+
m({
|
|
40
|
+
op: "publicConfig",
|
|
41
|
+
outcome: e.code,
|
|
42
|
+
kbCode: z(r) ? r.code : null,
|
|
43
|
+
status: j(r) ? r.status : null
|
|
44
|
+
});
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function E(e) {
|
|
49
|
+
let r;
|
|
50
|
+
try {
|
|
51
|
+
r = await t(e);
|
|
52
|
+
} catch (p) {
|
|
53
|
+
const s = M(p), u = Object.freeze({ ok: !1, ...s });
|
|
54
|
+
return L(u, p), u;
|
|
55
|
+
}
|
|
56
|
+
const o = w(r);
|
|
57
|
+
return o ? Object.freeze({ ok: !0, value: o }) : (L(b), b);
|
|
58
|
+
}
|
|
59
|
+
function d(e, r) {
|
|
60
|
+
for (f.delete(e), f.set(e, r); f.size > l; ) {
|
|
61
|
+
const o = f.keys().next().value;
|
|
62
|
+
f.delete(o);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function O(e, r) {
|
|
66
|
+
const o = ++k, p = y;
|
|
67
|
+
return E(e).then((s) => {
|
|
68
|
+
if (p !== y) return s;
|
|
69
|
+
const u = f.get(e);
|
|
70
|
+
return s.ok ? (!u || !u.outcome.ok || u.seq < o) && d(e, { outcome: s, expiresAt: a() + i, seq: o }) : r || (u?.outcome.ok ? u.expiresAt : -1 / 0) <= a() && d(e, { outcome: s, expiresAt: a() + c, seq: o }), s;
|
|
71
|
+
}).catch(() => b);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
get(e, r = {}) {
|
|
75
|
+
if (typeof e != "string" || !x.test(e)) return Promise.resolve(F);
|
|
76
|
+
if (r.fresh) return O(e, !0);
|
|
77
|
+
const o = f.get(e);
|
|
78
|
+
if (o && o.expiresAt > a())
|
|
79
|
+
return d(e, o), Promise.resolve(o.outcome);
|
|
80
|
+
const p = g.get(e);
|
|
81
|
+
if (p) return p;
|
|
82
|
+
const s = O(e, !1).finally(() => {
|
|
83
|
+
g.get(e) === s && g.delete(e);
|
|
84
|
+
});
|
|
85
|
+
return g.set(e, s), s;
|
|
86
|
+
},
|
|
87
|
+
peek(e) {
|
|
88
|
+
if (!(typeof e != "string" || !x.test(e)))
|
|
89
|
+
try {
|
|
90
|
+
const r = f.get(e);
|
|
91
|
+
return r && r.expiresAt > a() ? r.outcome : void 0;
|
|
92
|
+
} catch {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
clear() {
|
|
97
|
+
y++, f.clear(), g.clear();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export {
|
|
102
|
+
N as createPublicConfigCache,
|
|
103
|
+
w as sanitizePublicConfig
|
|
104
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "My consultations" for a signed-in reader: the list, a thread, a reply, an
|
|
3
|
+
* erasure, the reader's consent and its withdrawal, and a new question on the
|
|
4
|
+
* `web` channel (docs/internal/reader-consultations.md §6.1, §8.1).
|
|
5
|
+
*
|
|
6
|
+
* It runs wherever the reader's own identity is: in the browser with the
|
|
7
|
+
* reader's KB token through the blog's `/api` proxy, or on a server with a
|
|
8
|
+
* delegated token. It uses `client.consultations` ONLY — never
|
|
9
|
+
* `consultationSession` (a magic-link session is server-only and has its own
|
|
10
|
+
* handlers in `@blogmcp/sdk/consultations/server`).
|
|
11
|
+
*
|
|
12
|
+
* Every method resolves to a `ConsultationOutcome` and never throws: a KB
|
|
13
|
+
* outage, a KB code, a malformed answer and a bad argument all end as a code.
|
|
14
|
+
* What can be decided without the KB is decided first, so a malformed id, an
|
|
15
|
+
* empty message or a bad key costs neither a request nor a unit of quota.
|
|
16
|
+
*
|
|
17
|
+
* Nothing here logs a body, an address, a token or an id: the logger gets the
|
|
18
|
+
* operation, the outcome, the KB code and the HTTP status.
|
|
19
|
+
*/
|
|
20
|
+
import type { KbClient } from '../core/client.js';
|
|
21
|
+
import type { ConsultationMessageInput, ReaderConsultation, ReaderConsultationConsent, ReaderConsultationSummary } from '../core/types.js';
|
|
22
|
+
import { type WebAskStatus } from './channel-text.js';
|
|
23
|
+
import { type ConsultationLogger, type ConsultationOutcome } from './outcome.js';
|
|
24
|
+
import type { PublicConfigCache } from './public-config.js';
|
|
25
|
+
/** A new question on the `web` channel: what the reader wrote and which legal text the page showed. */
|
|
26
|
+
export interface NewQuestion {
|
|
27
|
+
/** The question (checked against the KB limit; the page applies its own, shorter one first). */
|
|
28
|
+
body: string;
|
|
29
|
+
/** The page's language — the KB picks the notice e-mails' language by it. */
|
|
30
|
+
languageCode: string;
|
|
31
|
+
/**
|
|
32
|
+
* The text the page showed: in `consent` mode the reader ticked it, in
|
|
33
|
+
* `notice` mode they saw it. It goes as `consent` OR `notice`, never both.
|
|
34
|
+
* `null`: nothing was shown — the reader's consent already covers new
|
|
35
|
+
* questions, and the KB links the question to it by itself (a consent sent
|
|
36
|
+
* anyway would be ignored; one that stopped covering answers CONSENT_REQUIRED).
|
|
37
|
+
*/
|
|
38
|
+
legal: {
|
|
39
|
+
mode: 'consent' | 'notice';
|
|
40
|
+
version: string;
|
|
41
|
+
locale: string;
|
|
42
|
+
} | null;
|
|
43
|
+
/** The idempotency key of this attempt (a retry reuses it: see `submitNewConsultation`). */
|
|
44
|
+
clientRequestId: string;
|
|
45
|
+
}
|
|
46
|
+
export interface ReaderConsultations {
|
|
47
|
+
/** The reader's consultations, newest activity first (`limit` 1..100, default 100). */
|
|
48
|
+
list(o?: {
|
|
49
|
+
limit?: number;
|
|
50
|
+
}): Promise<ConsultationOutcome<ReaderConsultationSummary[]>>;
|
|
51
|
+
/** One of the reader's consultations. A foreign, missing or malformed id is `notFound` (a malformed one without asking). */
|
|
52
|
+
get(id: string): Promise<ConsultationOutcome<ReaderConsultation>>;
|
|
53
|
+
/** Write in one of the reader's consultations (reopens an answered one). `duplicate`: a replay of the same key. */
|
|
54
|
+
post(id: string, m: ConsultationMessageInput): Promise<ConsultationOutcome<{
|
|
55
|
+
duplicate: boolean;
|
|
56
|
+
}>>;
|
|
57
|
+
/** Close a consultation now; it is deleted after the tenant's grace period. The reader's consent stays. */
|
|
58
|
+
requestErasure(id: string): Promise<ConsultationOutcome<null>>;
|
|
59
|
+
/** The reader's own consent and whether their next question needs one (read afresh every time). */
|
|
60
|
+
consent(locale: string): Promise<ConsultationOutcome<ReaderConsultationConsent>>;
|
|
61
|
+
/** What the new-question page offers this reader (`webAskFrom`); `fresh` reads the notice text past the memo. */
|
|
62
|
+
webAsk(locale: string, o?: {
|
|
63
|
+
fresh?: boolean;
|
|
64
|
+
}): Promise<ConsultationOutcome<WebAskStatus>>;
|
|
65
|
+
/** Open a consultation on the `web` channel. The reply's id is checked: only a UUID is ever returned. */
|
|
66
|
+
create(q: NewQuestion): Promise<ConsultationOutcome<{
|
|
67
|
+
id: string;
|
|
68
|
+
duplicate: boolean;
|
|
69
|
+
}>>;
|
|
70
|
+
/**
|
|
71
|
+
* Withdraw the reader's consent: every question that relied on it closes now.
|
|
72
|
+
* "There was nothing to withdraw" (`NOTHING_TO_DO`, e.g. done in another
|
|
73
|
+
* window) is the reader's wish fulfilled: `ok` with `alreadyWithdrawn`.
|
|
74
|
+
*/
|
|
75
|
+
withdrawConsent(): Promise<ConsultationOutcome<{
|
|
76
|
+
threadsClosed: number | null;
|
|
77
|
+
alreadyWithdrawn: boolean;
|
|
78
|
+
}>>;
|
|
79
|
+
}
|
|
80
|
+
export declare function createReaderConsultations(client: Pick<KbClient, 'consultations'>, o: {
|
|
81
|
+
publicConfig: PublicConfigCache;
|
|
82
|
+
logger?: ConsultationLogger;
|
|
83
|
+
}): ReaderConsultations;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { isKbGraphQLError as h } from "../core/errors.js";
|
|
2
|
+
import { webAskFrom as k, channelTextFrom as L, CONSULTATION_CHANNELS as N } from "./channel-text.js";
|
|
3
|
+
import { consultationFailureOf as A, isConsultationTransportError as _, isLoggedConsultationOutcome as R, consultationOutcomeOf as T } from "./outcome.js";
|
|
4
|
+
import { normalizeBody as I, READER_BODY_MAX as C, CLIENT_REQUEST_ID_RE as P, LOCALE_RE as j, CONSULTATION_ID_RE as D, LEGAL_VERSION_RE as F } from "./routes.js";
|
|
5
|
+
const E = 100, y = Object.freeze({ ok: !1, code: "invalid" }), p = Object.freeze({ ok: !1, code: "notFound" }), f = Object.freeze({ ok: !1, code: "unavailable" }), b = (n) => typeof n == "string" && D.test(n), O = (n) => typeof n == "string" && P.test(n), v = (n) => typeof n == "string" && j.test(n);
|
|
6
|
+
function M(n) {
|
|
7
|
+
return typeof n != "number" || !Number.isFinite(n) ? E : Math.min(E, Math.max(1, Math.floor(n)));
|
|
8
|
+
}
|
|
9
|
+
function S(n) {
|
|
10
|
+
return typeof n == "number" && Number.isInteger(n) && n >= 0 ? n : null;
|
|
11
|
+
}
|
|
12
|
+
function U(n) {
|
|
13
|
+
if (n == null) return {};
|
|
14
|
+
if (typeof n != "object") return null;
|
|
15
|
+
const { mode: i, version: c, locale: l } = n;
|
|
16
|
+
if (i !== "consent" && i !== "notice" || typeof c != "string" || !F.test(c) || !v(l)) return null;
|
|
17
|
+
const u = { version: c, locale: l };
|
|
18
|
+
return i === "consent" ? { consent: u } : { notice: u };
|
|
19
|
+
}
|
|
20
|
+
function G(n, i) {
|
|
21
|
+
const { publicConfig: c, logger: l } = i;
|
|
22
|
+
function u(e, t, o = null, s = null) {
|
|
23
|
+
if (!(!l || !R(t)))
|
|
24
|
+
try {
|
|
25
|
+
l({ op: e, outcome: t, kbCode: o, status: s });
|
|
26
|
+
} catch {
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function a(e, t) {
|
|
30
|
+
try {
|
|
31
|
+
return await t();
|
|
32
|
+
} catch (o) {
|
|
33
|
+
const s = A(o);
|
|
34
|
+
return u(e, s.code, h(o) ? o.code : null, _(o) ? o.status : null), { ok: !1, ...s };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function d(e, t, o) {
|
|
38
|
+
if (!t || typeof t != "object")
|
|
39
|
+
return u(e, "unavailable"), f;
|
|
40
|
+
if (t.success === !0) return { ok: !0, value: o(t) };
|
|
41
|
+
const s = typeof t.code == "string" ? t.code : null, r = T(s);
|
|
42
|
+
return u(e, r, s), { ok: !1, code: r };
|
|
43
|
+
}
|
|
44
|
+
async function w(e, t) {
|
|
45
|
+
const o = await c.get(e, { fresh: t });
|
|
46
|
+
return o.ok ? { ok: !0, value: L(o.value, N.web, e) } : o;
|
|
47
|
+
}
|
|
48
|
+
const g = {
|
|
49
|
+
list: (e = {}) => a("list", async () => {
|
|
50
|
+
const t = await n.consultations.mine({ limit: M(e?.limit) });
|
|
51
|
+
return { ok: !0, value: Array.isArray(t) ? t : [] };
|
|
52
|
+
}),
|
|
53
|
+
get: (e) => b(e) ? a("get", async () => {
|
|
54
|
+
const t = await n.consultations.get(e);
|
|
55
|
+
return !t || typeof t != "object" ? (u("get", "unavailable"), f) : { ok: !0, value: t };
|
|
56
|
+
}) : Promise.resolve(p),
|
|
57
|
+
post: (e, t) => {
|
|
58
|
+
if (!b(e)) return Promise.resolve(p);
|
|
59
|
+
const o = I(t?.body, C);
|
|
60
|
+
if (!o.ok) return Promise.resolve({ ok: !1, code: o.code });
|
|
61
|
+
if (!O(t.clientRequestId)) return Promise.resolve(y);
|
|
62
|
+
const s = { body: o.body, clientRequestId: t.clientRequestId };
|
|
63
|
+
return a(
|
|
64
|
+
"post",
|
|
65
|
+
async () => d("post", await n.consultations.post(e, s), (r) => ({ duplicate: r.duplicate === !0 }))
|
|
66
|
+
);
|
|
67
|
+
},
|
|
68
|
+
requestErasure: (e) => b(e) ? a(
|
|
69
|
+
"requestErasure",
|
|
70
|
+
async () => d("requestErasure", await n.consultations.requestErasure(e), () => null)
|
|
71
|
+
) : Promise.resolve(p),
|
|
72
|
+
consent: (e) => v(e) ? a("consent", async () => {
|
|
73
|
+
const t = await n.consultations.consent(e);
|
|
74
|
+
return !t || typeof t != "object" ? (u("consent", "unavailable"), f) : { ok: !0, value: t };
|
|
75
|
+
}) : Promise.resolve(y),
|
|
76
|
+
/**
|
|
77
|
+
* The reader's consent status is one call with their own identity, never
|
|
78
|
+
* memoised (it belongs to one person and changes with their own actions).
|
|
79
|
+
* The public config is read only in notice mode, for the notice text. Not
|
|
80
|
+
* `fresh`: from the memo — but when the memo holds no notice text, once
|
|
81
|
+
* more past it: a channel switched from consent to notice a minute ago
|
|
82
|
+
* would otherwise keep this page closed for five.
|
|
83
|
+
*/
|
|
84
|
+
webAsk: async (e, t = {}) => {
|
|
85
|
+
try {
|
|
86
|
+
const o = await g.consent(e);
|
|
87
|
+
if (!o.ok) return o;
|
|
88
|
+
if (o.value.mode !== "notice") return { ok: !0, value: k(o.value, e) };
|
|
89
|
+
const s = t?.fresh === !0;
|
|
90
|
+
let r = await w(e, s);
|
|
91
|
+
return r.ok && !s && !(r.value.enabled && r.value.mode === "notice") && (r = await w(e, !0)), r.ok ? { ok: !0, value: k(o.value, e, r.value) } : r;
|
|
92
|
+
} catch {
|
|
93
|
+
return u("webAsk", "unavailable"), f;
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
create: (e) => {
|
|
97
|
+
const t = I(e?.body, C);
|
|
98
|
+
if (!t.ok) return Promise.resolve({ ok: !1, code: t.code });
|
|
99
|
+
if (!O(e.clientRequestId) || !v(e.languageCode)) return Promise.resolve(y);
|
|
100
|
+
const o = U(e.legal);
|
|
101
|
+
if (!o) return Promise.resolve(y);
|
|
102
|
+
const s = {
|
|
103
|
+
body: t.body,
|
|
104
|
+
languageCode: e.languageCode,
|
|
105
|
+
...o,
|
|
106
|
+
clientRequestId: e.clientRequestId
|
|
107
|
+
};
|
|
108
|
+
return a("create", async () => {
|
|
109
|
+
const r = d("create", await n.consultations.create(s), (m) => ({
|
|
110
|
+
id: typeof m.consultation?.id == "string" ? m.consultation.id : "",
|
|
111
|
+
duplicate: m.duplicate === !0
|
|
112
|
+
}));
|
|
113
|
+
return r.ok && !b(r.value.id) ? (u("create", "unavailable"), f) : r;
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
withdrawConsent: () => a("withdrawConsent", async () => {
|
|
117
|
+
const e = await n.consultations.withdrawConsent();
|
|
118
|
+
return e && typeof e == "object" && e.success !== !0 && e.code === "NOTHING_TO_DO" ? { ok: !0, value: { threadsClosed: 0, alreadyWithdrawn: !0 } } : d("withdrawConsent", e, (t) => ({
|
|
119
|
+
threadsClosed: S(t.threadsClosed),
|
|
120
|
+
alreadyWithdrawn: !1
|
|
121
|
+
}));
|
|
122
|
+
})
|
|
123
|
+
};
|
|
124
|
+
return g;
|
|
125
|
+
}
|
|
126
|
+
export {
|
|
127
|
+
G as createReaderConsultations
|
|
128
|
+
};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader consultation pages — where they live, what their forms post, and the
|
|
3
|
+
* shapes the KB accepts (docs/internal/reader-consultations.md §7.4, §8.1).
|
|
4
|
+
*
|
|
5
|
+
* The route constants are router-relative: a blog mounted under `/blog` in
|
|
6
|
+
* production prepends its own base. Two of them are the KB's own
|
|
7
|
+
* (`MANAGED_BLOG_READER_LINK_PATH` / `_THREAD_PATH` in the KB's
|
|
8
|
+
* managed-blog.ts): the KB builds the links in its notice e-mails from them,
|
|
9
|
+
* so a drift test pins them — a blog page under another path would receive
|
|
10
|
+
* every e-mailed link as a 404.
|
|
11
|
+
*
|
|
12
|
+
* The patterns mirror the KB's validators (drift-tested as well). They are
|
|
13
|
+
* checked BEFORE any KB call, so a malformed value costs neither a request nor
|
|
14
|
+
* a unit of the reader's quota.
|
|
15
|
+
*
|
|
16
|
+
* Browser-safe: nothing here takes a key, and nothing imports `node:*`.
|
|
17
|
+
*/
|
|
18
|
+
/** The reader pages (router-relative). */
|
|
19
|
+
export declare const CONSULTATION_ROUTES: {
|
|
20
|
+
/** The magic-link landing page; the token travels in the fragment (`#t=`), never in the path or query. */
|
|
21
|
+
readonly open: "/consultation/open";
|
|
22
|
+
/** The thread an anonymous reader's session cookie opens (no id in the URL, ever). */
|
|
23
|
+
readonly session: "/consultation";
|
|
24
|
+
/** Ask for a fresh link by e-mail address. */
|
|
25
|
+
readonly newLink: "/consultation/new-link";
|
|
26
|
+
/** A signed-in reader's list ("My consultations"). */
|
|
27
|
+
readonly account: "/my-consultations";
|
|
28
|
+
/** A signed-in reader asks a new question (KB channel `web`). */
|
|
29
|
+
readonly accountNew: "/my-consultations/new";
|
|
30
|
+
/** One of the signed-in reader's threads; build it with `accountThreadPath(id)`. */
|
|
31
|
+
readonly accountThread: "/my-consultations/:id";
|
|
32
|
+
/** The anonymous contact form (KB channel `contact_form`). */
|
|
33
|
+
readonly contact: "/contact";
|
|
34
|
+
};
|
|
35
|
+
/** The JSON endpoints the reader pages post to from JavaScript (router-relative). */
|
|
36
|
+
export declare const CONSULTATION_API_PATHS: {
|
|
37
|
+
/** Exchange the link token for the session cookie (`intent=exchange&token=…`). */
|
|
38
|
+
readonly access: "/api/consultations/access";
|
|
39
|
+
/** Move the session's thread to the signed-in reader's account. */
|
|
40
|
+
readonly attach: "/api/consultations/attach";
|
|
41
|
+
/** Drop the session cookie. */
|
|
42
|
+
readonly forget: "/api/consultations/forget";
|
|
43
|
+
};
|
|
44
|
+
/** Where the KB points an e-mailed link (== KB `MANAGED_BLOG_READER_LINK_PATH`). */
|
|
45
|
+
export declare const CONSULTATION_KB_READER_LINK_PATH = "/consultation/open#t={token}";
|
|
46
|
+
/** Where the KB points a signed-in reader's thread (== KB `MANAGED_BLOG_READER_THREAD_PATH`). */
|
|
47
|
+
export declare const CONSULTATION_KB_READER_THREAD_PATH = "/my-consultations/{id}";
|
|
48
|
+
/** A link token and a session value: 32 random bytes, base64url (== KB `SECRET_RE`). */
|
|
49
|
+
export declare const CONSULTATION_SECRET_RE: RegExp;
|
|
50
|
+
/** An idempotency key of one reader action; a UUID fits (== KB `CLIENT_REQUEST_RE`). */
|
|
51
|
+
export declare const CLIENT_REQUEST_ID_RE: RegExp;
|
|
52
|
+
/** A legal text version (== KB `VERSION_RE`; stricter than the drhoubicka storefront's `{1,64}`). */
|
|
53
|
+
export declare const LEGAL_VERSION_RE: RegExp;
|
|
54
|
+
/** A consultation id (the KB's thread UUID). Anything else is "not found" without asking the KB. */
|
|
55
|
+
export declare const CONSULTATION_ID_RE: RegExp;
|
|
56
|
+
/** A locale: `cs`, `en`, `pt-BR` (== the KB's locale / language pattern). */
|
|
57
|
+
export declare const LOCALE_RE: RegExp;
|
|
58
|
+
/** The longest reader message the KB takes (== KB `READER_BODY_MAX`): a reply in a thread. */
|
|
59
|
+
export declare const READER_BODY_MAX = 5000;
|
|
60
|
+
/** The longest NEW question on the account page. The KB would take 5000; the form keeps questions short. */
|
|
61
|
+
export declare const NEW_QUESTION_BODY_MAX = 2000;
|
|
62
|
+
/** The longest contact-form message. */
|
|
63
|
+
export declare const CONTACT_MESSAGE_MAX = 2000;
|
|
64
|
+
/** The longest name the contact form takes (the KB would take 255; the server refuses more than this). */
|
|
65
|
+
export declare const CONTACT_NAME_MAX = 100;
|
|
66
|
+
/** The longest reader e-mail address accepted (the RFC 5321 path limit). */
|
|
67
|
+
export declare const READER_EMAIL_MAX = 254;
|
|
68
|
+
/**
|
|
69
|
+
* The Turnstile action the contact form's widget declares (`data-action`) and the
|
|
70
|
+
* server expects when it verifies the token — one constant, so the two never drift.
|
|
71
|
+
*/
|
|
72
|
+
export declare const CONSULTATION_TURNSTILE_ACTION = "consultation_contact";
|
|
73
|
+
/**
|
|
74
|
+
* Whether `value` is an address the server takes for a new link or the contact
|
|
75
|
+
* form — the same check it runs, so a page can show the field error before posting.
|
|
76
|
+
*/
|
|
77
|
+
export declare function isReaderEmail(value: string): boolean;
|
|
78
|
+
/**
|
|
79
|
+
* The fields the new-question form (`/my-consultations/new`) posts.
|
|
80
|
+
*
|
|
81
|
+
* `legalMode` says what the page SHOWED: `consent` (the text with an unticked
|
|
82
|
+
* box), `notice` (the text, no box) or `covered` (no text: the reader's
|
|
83
|
+
* consent already covers new questions). `legalVersion` and `legalLocale`
|
|
84
|
+
* carry the shown text's version and locale; with `covered` they are ignored.
|
|
85
|
+
* `consent` is the checkbox, posted only when ticked, with the value `on`.
|
|
86
|
+
*/
|
|
87
|
+
export declare const NEW_QUESTION_FIELDS: {
|
|
88
|
+
readonly body: "body";
|
|
89
|
+
readonly clientRequestId: "clientRequestId";
|
|
90
|
+
readonly legalMode: "legalMode";
|
|
91
|
+
readonly legalVersion: "legalVersion";
|
|
92
|
+
readonly legalLocale: "legalLocale";
|
|
93
|
+
readonly consent: "consent";
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* The fields of the session page's forms (`/consultation`) and of the access
|
|
97
|
+
* endpoint. `intent` is one of `CONSULTATION_INTENTS`: `reply` (with `body`
|
|
98
|
+
* and `clientRequestId`), `erase`, `logout` on the page; `exchange` (with
|
|
99
|
+
* `token`) on `CONSULTATION_API_PATHS.access`.
|
|
100
|
+
*/
|
|
101
|
+
export declare const SESSION_FIELDS: {
|
|
102
|
+
readonly intent: "intent";
|
|
103
|
+
readonly body: "body";
|
|
104
|
+
readonly clientRequestId: "clientRequestId";
|
|
105
|
+
readonly token: "token";
|
|
106
|
+
};
|
|
107
|
+
/** The values of the `intent` field. */
|
|
108
|
+
export declare const CONSULTATION_INTENTS: {
|
|
109
|
+
readonly exchange: "exchange";
|
|
110
|
+
readonly reply: "reply";
|
|
111
|
+
readonly erase: "erase";
|
|
112
|
+
readonly logout: "logout";
|
|
113
|
+
};
|
|
114
|
+
/** The fields of the new-link form (`/consultation/new-link`). `website` is the honeypot: left empty by people. */
|
|
115
|
+
export declare const NEW_LINK_FIELDS: {
|
|
116
|
+
readonly email: "email";
|
|
117
|
+
readonly honeypot: "website";
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* The fields of the contact form (`/contact`). `messageId` is the idempotency
|
|
121
|
+
* key the page was rendered with; `legalMode` / `legalVersion` /
|
|
122
|
+
* `legalLocale` name the text the form showed, and `consent` is the checkbox
|
|
123
|
+
* in consent mode (`on` when ticked). `website` is the honeypot and
|
|
124
|
+
* `cf-turnstile-response` the Turnstile widget's own field.
|
|
125
|
+
*/
|
|
126
|
+
export declare const CONTACT_FIELDS: {
|
|
127
|
+
readonly name: "name";
|
|
128
|
+
readonly email: "email";
|
|
129
|
+
readonly message: "message";
|
|
130
|
+
readonly messageId: "messageId";
|
|
131
|
+
readonly legalMode: "legalMode";
|
|
132
|
+
readonly legalVersion: "legalVersion";
|
|
133
|
+
readonly legalLocale: "legalLocale";
|
|
134
|
+
readonly consent: "consent";
|
|
135
|
+
readonly honeypot: "website";
|
|
136
|
+
readonly turnstile: "cf-turnstile-response";
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* The path of one of the signed-in reader's threads. A value that is not a
|
|
140
|
+
* consultation id links to the list instead: no path is ever built from
|
|
141
|
+
* arbitrary text (a redirect after a create, a link in the list).
|
|
142
|
+
*/
|
|
143
|
+
export declare function accountThreadPath(id: string): string;
|
|
144
|
+
/**
|
|
145
|
+
* A reader message as the KB should get it: line breaks as `\n`, trimmed,
|
|
146
|
+
* 1..`max` characters, no NUL.
|
|
147
|
+
*
|
|
148
|
+
* A native form POST (before hydration, without JavaScript) sends every line
|
|
149
|
+
* break as CRLF — two characters — while the textarea's `maxLength` and a page
|
|
150
|
+
* counter count one. Without the normalisation a text that fit into the field
|
|
151
|
+
* would come back "too long", and without JavaScript there is no counter to
|
|
152
|
+
* show what to cut.
|
|
153
|
+
*
|
|
154
|
+
* A failure carries the typed text back for the form (line breaks normalised,
|
|
155
|
+
* NOT trimmed): the reader gets their text back to fix, never an empty field
|
|
156
|
+
* — and never a silently shortened one, which would pass on the next attempt
|
|
157
|
+
* without its end. Only a text over twice the limit (a hand-made POST, not a
|
|
158
|
+
* textarea) is cut, to bound the response.
|
|
159
|
+
*/
|
|
160
|
+
export declare function normalizeBody(raw: unknown, max: number): {
|
|
161
|
+
ok: true;
|
|
162
|
+
body: string;
|
|
163
|
+
} | {
|
|
164
|
+
ok: false;
|
|
165
|
+
code: 'invalid' | 'tooLong';
|
|
166
|
+
body: string;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* A fresh idempotency key for one reader action (a UUID). Mint it when the
|
|
170
|
+
* form is rendered and send the SAME key again on a retry: the KB answers a
|
|
171
|
+
* replay as the stored write (`duplicate`) instead of writing twice.
|
|
172
|
+
*
|
|
173
|
+
* `crypto.randomUUID` exists only in secure contexts; a blog opened over plain
|
|
174
|
+
* http on a LAN address (a phone during development) gets a v4 UUID from
|
|
175
|
+
* `crypto.getRandomValues`, which exists everywhere.
|
|
176
|
+
*/
|
|
177
|
+
export declare function newClientRequestId(): string;
|
|
178
|
+
/**
|
|
179
|
+
* The link token in a URL fragment (`#t=<token>`), or null.
|
|
180
|
+
*
|
|
181
|
+
* Nothing else is read from the fragment, and only the KB's exact token shape
|
|
182
|
+
* is accepted. A truncated link from a mail client or an appended tracking
|
|
183
|
+
* parameter is "the link has no access code", never a token that goes to the
|
|
184
|
+
* KB.
|
|
185
|
+
*/
|
|
186
|
+
export declare function tokenFromHash(hash: string): string | null;
|
|
187
|
+
/**
|
|
188
|
+
* The link token of the magic-link page, read from the address bar and then
|
|
189
|
+
* removed from it — from the URL and from the router's copy in
|
|
190
|
+
* `history.state`.
|
|
191
|
+
*
|
|
192
|
+
* The token is a bearer capability: left in the address bar it would end up
|
|
193
|
+
* in the history, in a bookmark, in a copied link. So the first call strips
|
|
194
|
+
* the fragment with `history.replaceState`, keeping the query string and the
|
|
195
|
+
* router's state (the routers keep their bookkeeping there; `null` would break
|
|
196
|
+
* their back navigation) — minus every copy of the fragment in it.
|
|
197
|
+
*
|
|
198
|
+
* The token is remembered per window: a second call — React StrictMode runs
|
|
199
|
+
* mount effects twice, Vue remounts a page — finds an address bar that is
|
|
200
|
+
* already clean and would otherwise answer "the link has no access code". A
|
|
201
|
+
* later call touches the address bar only for a new link (`#t=…` pasted into
|
|
202
|
+
* the same tab, whose valid token then replaces the remembered one), never for
|
|
203
|
+
* an anchor the page set itself.
|
|
204
|
+
*
|
|
205
|
+
* ⚠️ A router may also keep the old URL in its own memory: vue-router writes
|
|
206
|
+
* its last known location into the next entry's `state.back` on the next
|
|
207
|
+
* `push`. In a vue-router app, follow this with `router.replace({ hash: '' })`
|
|
208
|
+
* (or navigate away with `replace`), so the router forgets the fragment too.
|
|
209
|
+
*
|
|
210
|
+
* Send the token to the KB only on an explicit gesture (a button), never on
|
|
211
|
+
* load: mail scanners and link previews open links but do not press buttons,
|
|
212
|
+
* and the KB allows only a few exchanges per token.
|
|
213
|
+
*/
|
|
214
|
+
export declare function readLinkToken(win?: Window): string | null;
|