@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,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A legal text of the private surfaces as blocks (docs/internal/m4-contracts.md §5.11 "Formatting"): the body kb-api
|
|
3
|
+
* serves — `PrivateLegalText.body`, the account host's text pages — is written in a small subset of Markdown, and a
|
|
4
|
+
* page renders the blocks this function returns as elements with text in them, never as markup. Nothing here produces
|
|
5
|
+
* HTML, and the blocks carry no HTML: a `<b>` in a body is the four characters `<b>`.
|
|
6
|
+
*
|
|
7
|
+
* The subset (the legal lane's drafts use exactly this):
|
|
8
|
+
*
|
|
9
|
+
* - `#`…`######` headings, with an optional `{#anchor}` at the end (the anchor becomes the heading's `id`);
|
|
10
|
+
* - paragraphs: lines up to a blank line, joined with a space; a line ending in two spaces or `\` breaks the line
|
|
11
|
+
* (`\n` in a run's text);
|
|
12
|
+
* - lists: `-`, `*` or `+` bullets and `1.` / `1)` numbers, items continued by lines indented to the item's text
|
|
13
|
+
* (or by lazy lines right after it), lists nested by that indentation (at most LEGAL_TEXT_MAX_DEPTH deep); a
|
|
14
|
+
* number list starts after a blank line or a heading, never inside a paragraph (write `1\.` for a paragraph
|
|
15
|
+
* that starts with a number and a period);
|
|
16
|
+
* - inline: `**strong**`, `*emphasis*`, `***both***`, `` `code` ``, `[label](https://…)`, a bare `https://…`
|
|
17
|
+
* address, and `\` before a punctuation character to write it literally;
|
|
18
|
+
* - HTML comments (`<!-- … -->`, the drafts' markers) are dropped.
|
|
19
|
+
*
|
|
20
|
+
* A link's target is an `https:` address or a `mailto:` one and nothing else; any other target (an unfilled
|
|
21
|
+
* `{{placeholder}}`, `javascript:`, a relative path) leaves the label as text. Everything outside the subset —
|
|
22
|
+
* tables, quotes, images, raw HTML, `_underscores_` — is text as written.
|
|
23
|
+
*
|
|
24
|
+
* Fill a session's placeholders (`{{approval_email}}`, …) in the runs AFTER parsing, never in the body before it:
|
|
25
|
+
* a masked address (`l***@…`) would otherwise be read as emphasis.
|
|
26
|
+
*
|
|
27
|
+
* Import-free and the same code as kb-api's `services/legal-texts/legal-text-blocks.ts`, which renders the account
|
|
28
|
+
* host's text pages from it (test/community/legal-text-drift.spec.ts compares the two files).
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
// ── shared code: @blogmcp/sdk's community/legal-text.ts and kb-api's services/legal-texts/legal-text-blocks.ts are identical below this line ──
|
|
32
|
+
|
|
33
|
+
/** A piece of text with its marks; consecutive runs of one link share its `href`. */
|
|
34
|
+
export interface LegalTextRun {
|
|
35
|
+
/** The text; a `\n` is a hard line break. */
|
|
36
|
+
readonly text: string
|
|
37
|
+
readonly strong?: true
|
|
38
|
+
readonly em?: true
|
|
39
|
+
readonly code?: true
|
|
40
|
+
/** An `https:` or `mailto:` address — never anything else. */
|
|
41
|
+
readonly href?: string
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface LegalTextHeading {
|
|
45
|
+
readonly type: 'heading'
|
|
46
|
+
readonly level: 1 | 2 | 3 | 4 | 5 | 6
|
|
47
|
+
/** From a trailing `{#anchor}`: letters, digits, `-` and `_`, at most 64. */
|
|
48
|
+
readonly id: string | null
|
|
49
|
+
readonly runs: readonly LegalTextRun[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface LegalTextParagraph {
|
|
53
|
+
readonly type: 'paragraph'
|
|
54
|
+
readonly runs: readonly LegalTextRun[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface LegalTextList {
|
|
58
|
+
readonly type: 'list'
|
|
59
|
+
readonly ordered: boolean
|
|
60
|
+
/** The first item's number (1 for a bullet list). */
|
|
61
|
+
readonly start: number
|
|
62
|
+
readonly items: readonly LegalTextListItem[]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface LegalTextListItem {
|
|
66
|
+
readonly blocks: readonly LegalTextBlock[]
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type LegalTextBlock = LegalTextHeading | LegalTextParagraph | LegalTextList
|
|
70
|
+
|
|
71
|
+
/** How deep lists nest; a deeper item's line is paragraph text. */
|
|
72
|
+
export const LEGAL_TEXT_MAX_DEPTH = 8
|
|
73
|
+
|
|
74
|
+
/** A link target the blocks may carry. */
|
|
75
|
+
const HREF_RE = /^(?:https:\/\/[^\s"'<>\\`\u0000-\u001f\u007f]+|mailto:[^\s"'<>\\`@\u0000-\u001f\u007f]+@[^\s"'<>\\`\u0000-\u001f\u007f]+)$/
|
|
76
|
+
const HREF_MAX = 2048
|
|
77
|
+
const ANCHOR_RE = /[ \t]*\{#([A-Za-z0-9][A-Za-z0-9_-]{0,63})\}$/
|
|
78
|
+
const HEADING_RE = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?[ \t]*$/
|
|
79
|
+
const ITEM_RE = /^( {0,3})([-*+]|(\d{1,9})([.)]))([ \t]+)(\S.*)$/
|
|
80
|
+
const ESCAPABLE = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
|
|
81
|
+
/** Characters that end a sentence more often than an address. */
|
|
82
|
+
const URL_TRAILING = '.,;:!?…\'"»”’]*'
|
|
83
|
+
const URL_RE = /https:\/\/[^\s<>"'`„“”‘’]+/y
|
|
84
|
+
/** How deep marks nest inside one another (beyond it, the rest is text). */
|
|
85
|
+
const INLINE_MAX_DEPTH = 6
|
|
86
|
+
|
|
87
|
+
/** The blocks of a legal text's body (see the module comment). Never throws. */
|
|
88
|
+
export function legalTextBlocks(body: string): LegalTextBlock[] {
|
|
89
|
+
if (typeof body !== 'string' || body === '') return []
|
|
90
|
+
const lines = body
|
|
91
|
+
.replace(/\r\n?/g, '\n')
|
|
92
|
+
.replace(/<!--[\s\S]*?-->/g, '')
|
|
93
|
+
.split('\n')
|
|
94
|
+
.map((line) => line.replace(/^[ \t]+/, (ws) => ws.replace(/\t/g, ' ')))
|
|
95
|
+
return blocksOf(lines, 0)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const isBlank = (line: string): boolean => line.trim() === ''
|
|
99
|
+
const indentOf = (line: string): number => line.length - line.trimStart().length
|
|
100
|
+
|
|
101
|
+
interface ItemStart {
|
|
102
|
+
readonly ordered: boolean
|
|
103
|
+
readonly delimiter: string
|
|
104
|
+
readonly start: number
|
|
105
|
+
/** Where the item's text starts: its continuation lines are indented at least this far. */
|
|
106
|
+
readonly contentIndent: number
|
|
107
|
+
readonly content: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function itemOf(line: string): ItemStart | null {
|
|
111
|
+
const m = ITEM_RE.exec(line)
|
|
112
|
+
if (!m) return null
|
|
113
|
+
const spaces = m[5]!.length > 4 ? 1 : m[5]!.length
|
|
114
|
+
const ordered = m[3] !== undefined
|
|
115
|
+
return {
|
|
116
|
+
ordered,
|
|
117
|
+
delimiter: ordered ? m[4]! : m[2]!,
|
|
118
|
+
start: ordered ? Number(m[3]) : 1,
|
|
119
|
+
contentIndent: m[1]!.length + m[2]!.length + spaces,
|
|
120
|
+
content: m[5]!.length > 4 ? m[5]!.slice(1) + m[6]! : m[6]!,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function headingOf(line: string): LegalTextHeading | null {
|
|
125
|
+
const m = HEADING_RE.exec(line)
|
|
126
|
+
if (!m) return null
|
|
127
|
+
let text = (m[2] ?? '').replace(/(?:^|[ \t]+)#+$/, '')
|
|
128
|
+
let id: string | null = null
|
|
129
|
+
const anchor = ANCHOR_RE.exec(text)
|
|
130
|
+
if (anchor) {
|
|
131
|
+
id = anchor[1]!
|
|
132
|
+
text = text.slice(0, anchor.index)
|
|
133
|
+
}
|
|
134
|
+
text = text.trim()
|
|
135
|
+
if (text === '') return null
|
|
136
|
+
return { type: 'heading', level: m[1]!.length as LegalTextHeading['level'], id, runs: inlineRuns(text) }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A list item that may start in the middle of a paragraph: a bullet only. Unlike CommonMark, not even a number list
|
|
141
|
+
* that starts at 1 — a hard-wrapped Czech sentence puts „1. ledna“ at the start of a line.
|
|
142
|
+
*/
|
|
143
|
+
function interrupts(line: string, depth: number): boolean {
|
|
144
|
+
if (depth >= LEGAL_TEXT_MAX_DEPTH) return false
|
|
145
|
+
const item = itemOf(line)
|
|
146
|
+
return item !== null && !item.ordered
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function blocksOf(lines: readonly string[], depth: number): LegalTextBlock[] {
|
|
150
|
+
const out: LegalTextBlock[] = []
|
|
151
|
+
let i = 0
|
|
152
|
+
while (i < lines.length) {
|
|
153
|
+
const line = lines[i]!
|
|
154
|
+
if (isBlank(line)) {
|
|
155
|
+
i++
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
const heading = headingOf(line)
|
|
159
|
+
if (heading) {
|
|
160
|
+
out.push(heading)
|
|
161
|
+
i++
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
const item = depth < LEGAL_TEXT_MAX_DEPTH ? itemOf(line) : null
|
|
165
|
+
if (item) {
|
|
166
|
+
const [list, next] = listFrom(lines, i, item, depth)
|
|
167
|
+
out.push(list)
|
|
168
|
+
i = next
|
|
169
|
+
continue
|
|
170
|
+
}
|
|
171
|
+
const paragraph = [line]
|
|
172
|
+
i++
|
|
173
|
+
while (i < lines.length && !isBlank(lines[i]!) && !headingOf(lines[i]!) && !interrupts(lines[i]!, depth)) {
|
|
174
|
+
paragraph.push(lines[i]!)
|
|
175
|
+
i++
|
|
176
|
+
}
|
|
177
|
+
out.push({ type: 'paragraph', runs: inlineRuns(joinLines(paragraph)) })
|
|
178
|
+
}
|
|
179
|
+
return out
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A list from line `at` (an item's line), and the index of the first line after it. */
|
|
183
|
+
function listFrom(lines: readonly string[], at: number, first: ItemStart, depth: number): [LegalTextList, number] {
|
|
184
|
+
const items: LegalTextListItem[] = []
|
|
185
|
+
let item = first
|
|
186
|
+
let i = at
|
|
187
|
+
for (;;) {
|
|
188
|
+
const own: string[] = [item.content]
|
|
189
|
+
i++
|
|
190
|
+
let afterBlank = false
|
|
191
|
+
while (i < lines.length) {
|
|
192
|
+
const line = lines[i]!
|
|
193
|
+
if (isBlank(line)) {
|
|
194
|
+
own.push('')
|
|
195
|
+
afterBlank = true
|
|
196
|
+
i++
|
|
197
|
+
continue
|
|
198
|
+
}
|
|
199
|
+
if (indentOf(line) >= item.contentIndent) {
|
|
200
|
+
own.push(line.slice(item.contentIndent))
|
|
201
|
+
afterBlank = false
|
|
202
|
+
i++
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
205
|
+
// A lazy line: right after the item's text, and nothing that starts a block of its own.
|
|
206
|
+
if (!afterBlank && !itemOf(line) && !headingOf(line)) {
|
|
207
|
+
own.push(line.trimStart())
|
|
208
|
+
i++
|
|
209
|
+
continue
|
|
210
|
+
}
|
|
211
|
+
break
|
|
212
|
+
}
|
|
213
|
+
items.push({ blocks: blocksOf(own, depth + 1) })
|
|
214
|
+
if (i >= lines.length) break
|
|
215
|
+
const next = itemOf(lines[i]!)
|
|
216
|
+
if (!next || next.ordered !== first.ordered || next.delimiter !== first.delimiter) break
|
|
217
|
+
item = next
|
|
218
|
+
}
|
|
219
|
+
// The blank lines the last item collected belong to no one; the caller skips them.
|
|
220
|
+
while (i > at && isBlank(lines[i - 1]!)) i--
|
|
221
|
+
return [{ type: 'list', ordered: first.ordered, start: first.start, items }, i]
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A paragraph's lines as one string: a soft break is a space, a hard one (two spaces or `\` at the end) a `\n`. */
|
|
225
|
+
function joinLines(lines: readonly string[]): string {
|
|
226
|
+
let out = ''
|
|
227
|
+
lines.forEach((raw, k) => {
|
|
228
|
+
const line = raw.trimStart()
|
|
229
|
+
if (k === lines.length - 1) {
|
|
230
|
+
out += line.trimEnd()
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
if (/ {2,}$/.test(line)) out += line.trimEnd() + '\n'
|
|
234
|
+
else if (/(?:^|[^\\])(?:\\\\)*\\$/.test(line)) out += line.slice(0, -1).trimEnd() + '\n'
|
|
235
|
+
else out += line.trimEnd() + ' '
|
|
236
|
+
})
|
|
237
|
+
return out
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface Marks {
|
|
241
|
+
readonly strong?: true
|
|
242
|
+
readonly em?: true
|
|
243
|
+
readonly href?: string
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** The runs of one block's text, adjacent runs with the same marks merged. */
|
|
247
|
+
function inlineRuns(text: string): LegalTextRun[] {
|
|
248
|
+
const out: LegalTextRun[] = []
|
|
249
|
+
inline(text, {}, out, false, 0)
|
|
250
|
+
return out
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function push(out: LegalTextRun[], text: string, marks: Marks, code = false): void {
|
|
254
|
+
if (text === '') return
|
|
255
|
+
const run: LegalTextRun = {
|
|
256
|
+
text,
|
|
257
|
+
...(marks.strong ? { strong: true as const } : {}),
|
|
258
|
+
...(marks.em ? { em: true as const } : {}),
|
|
259
|
+
...(code ? { code: true as const } : {}),
|
|
260
|
+
...(marks.href ? { href: marks.href } : {}),
|
|
261
|
+
}
|
|
262
|
+
const last = out[out.length - 1]
|
|
263
|
+
if (last && !code && !last.code && last.strong === run.strong && last.em === run.em && last.href === run.href) {
|
|
264
|
+
out[out.length - 1] = { ...last, text: last.text + text }
|
|
265
|
+
} else {
|
|
266
|
+
out.push(run)
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const safeHref = (raw: string): string | null => (raw.length <= HREF_MAX && HREF_RE.test(raw) ? raw : null)
|
|
271
|
+
|
|
272
|
+
/** An address without the punctuation after it; a `)` stays when it closes a `(` inside the address. */
|
|
273
|
+
function trimUrl(raw: string): string {
|
|
274
|
+
let url = raw
|
|
275
|
+
while (url.length > 0) {
|
|
276
|
+
const last = url[url.length - 1]!
|
|
277
|
+
if (last === ')') {
|
|
278
|
+
if (url.split(')').length - 1 <= url.split('(').length - 1) break
|
|
279
|
+
} else if (!URL_TRAILING.includes(last)) {
|
|
280
|
+
break
|
|
281
|
+
}
|
|
282
|
+
url = url.slice(0, -1)
|
|
283
|
+
}
|
|
284
|
+
return url
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* A link's `(target)` at `at`: no whitespace, parentheses balanced, at most HREF_MAX characters (so an unclosed one
|
|
289
|
+
* costs a bounded scan); null when there is none.
|
|
290
|
+
*/
|
|
291
|
+
function targetAt(s: string, at: number): { raw: string; end: number } | null {
|
|
292
|
+
if (s[at] !== '(') return null
|
|
293
|
+
let depth = 0
|
|
294
|
+
for (let k = at + 1; k < s.length && k - at <= HREF_MAX + 1; k++) {
|
|
295
|
+
const c = s[k]!
|
|
296
|
+
if (/\s/.test(c)) return null
|
|
297
|
+
if (c === '(') depth++
|
|
298
|
+
else if (c === ')') {
|
|
299
|
+
if (depth > 0) depth--
|
|
300
|
+
else return k === at + 1 ? null : { raw: s.slice(at + 1, k), end: k + 1 }
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return null
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** The positions of the delimiter runs a span can close at, per run length (1–3), and of the unescaped brackets. */
|
|
307
|
+
interface Scan {
|
|
308
|
+
readonly closers: readonly [number[], number[], number[]]
|
|
309
|
+
readonly opens: number[]
|
|
310
|
+
readonly closes: number[]
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function scan(s: string): Scan {
|
|
314
|
+
const closers: [number[], number[], number[]] = [[], [], []]
|
|
315
|
+
const opens: number[] = []
|
|
316
|
+
const closes: number[] = []
|
|
317
|
+
let i = 0
|
|
318
|
+
while (i < s.length) {
|
|
319
|
+
const c = s[i]!
|
|
320
|
+
if (c === '\\' && i + 1 < s.length && ESCAPABLE.includes(s[i + 1]!)) {
|
|
321
|
+
i += 2
|
|
322
|
+
continue
|
|
323
|
+
}
|
|
324
|
+
if (c === '*') {
|
|
325
|
+
let n = 1
|
|
326
|
+
while (s[i + n] === '*') n++
|
|
327
|
+
if (n <= 3 && i > 0 && !/\s/.test(s[i - 1]!)) closers[n - 1]!.push(i)
|
|
328
|
+
i += n
|
|
329
|
+
continue
|
|
330
|
+
}
|
|
331
|
+
if (c === '[') opens.push(i)
|
|
332
|
+
else if (c === ']') closes.push(i)
|
|
333
|
+
i++
|
|
334
|
+
}
|
|
335
|
+
return { closers, opens, closes }
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/** The first position in a sorted list that is at least `min`, or -1. */
|
|
339
|
+
function firstAtLeast(list: readonly number[], min: number): number {
|
|
340
|
+
let lo = 0
|
|
341
|
+
let hi = list.length
|
|
342
|
+
while (lo < hi) {
|
|
343
|
+
const mid = (lo + hi) >> 1
|
|
344
|
+
if (list[mid]! < min) lo = mid + 1
|
|
345
|
+
else hi = mid
|
|
346
|
+
}
|
|
347
|
+
return lo < list.length ? list[lo]! : -1
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function inline(s: string, marks: Marks, out: LegalTextRun[], inLink: boolean, depth: number): void {
|
|
351
|
+
if (depth >= INLINE_MAX_DEPTH) {
|
|
352
|
+
push(out, s, marks)
|
|
353
|
+
return
|
|
354
|
+
}
|
|
355
|
+
const found = scan(s)
|
|
356
|
+
let text = ''
|
|
357
|
+
let i = 0
|
|
358
|
+
const flush = () => {
|
|
359
|
+
push(out, text, marks)
|
|
360
|
+
text = ''
|
|
361
|
+
}
|
|
362
|
+
while (i < s.length) {
|
|
363
|
+
const c = s[i]!
|
|
364
|
+
if (c === '\\' && i + 1 < s.length && ESCAPABLE.includes(s[i + 1]!)) {
|
|
365
|
+
text += s[i + 1]
|
|
366
|
+
i += 2
|
|
367
|
+
continue
|
|
368
|
+
}
|
|
369
|
+
if (c === '`') {
|
|
370
|
+
let n = 1
|
|
371
|
+
while (s[i + n] === '`') n++
|
|
372
|
+
const fence = '`'.repeat(n)
|
|
373
|
+
let end = s.indexOf(fence, i + n)
|
|
374
|
+
while (end !== -1 && s[end + n] === '`') {
|
|
375
|
+
let m = n
|
|
376
|
+
while (s[end + m] === '`') m++
|
|
377
|
+
end = s.indexOf(fence, end + m)
|
|
378
|
+
}
|
|
379
|
+
if (end === -1) {
|
|
380
|
+
text += fence
|
|
381
|
+
i += n
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
let code = s.slice(i + n, end).replace(/\n/g, ' ')
|
|
385
|
+
if (code.length > 1 && code.startsWith(' ') && code.endsWith(' ') && code.trim() !== '') code = code.slice(1, -1)
|
|
386
|
+
flush()
|
|
387
|
+
push(out, code, marks, true)
|
|
388
|
+
i = end + n
|
|
389
|
+
continue
|
|
390
|
+
}
|
|
391
|
+
if (c === '*') {
|
|
392
|
+
let n = 1
|
|
393
|
+
while (s[i + n] === '*') n++
|
|
394
|
+
const opens = n <= 3 && i + n < s.length && !/\s/.test(s[i + n]!)
|
|
395
|
+
const close = opens ? firstAtLeast(found.closers[n - 1]!, i + n + 1) : -1
|
|
396
|
+
if (close === -1) {
|
|
397
|
+
text += '*'.repeat(n)
|
|
398
|
+
i += n
|
|
399
|
+
continue
|
|
400
|
+
}
|
|
401
|
+
flush()
|
|
402
|
+
const inner: Marks = { ...marks, ...(n !== 1 ? { strong: true as const } : {}), ...(n !== 2 ? { em: true as const } : {}) }
|
|
403
|
+
inline(s.slice(i + n, close), inner, out, inLink, depth + 1)
|
|
404
|
+
i = close + n
|
|
405
|
+
continue
|
|
406
|
+
}
|
|
407
|
+
if (c === '[' && !inLink) {
|
|
408
|
+
const close = firstAtLeast(found.closes, i + 1)
|
|
409
|
+
const nested = firstAtLeast(found.opens, i + 1)
|
|
410
|
+
const target = close !== -1 && (nested === -1 || nested > close) ? targetAt(s, close + 1) : null
|
|
411
|
+
if (!target) {
|
|
412
|
+
text += '['
|
|
413
|
+
i++
|
|
414
|
+
continue
|
|
415
|
+
}
|
|
416
|
+
flush()
|
|
417
|
+
const href = safeHref(target.raw)
|
|
418
|
+
inline(s.slice(i + 1, close), href ? { ...marks, href } : marks, out, true, depth + 1)
|
|
419
|
+
i = target.end
|
|
420
|
+
continue
|
|
421
|
+
}
|
|
422
|
+
if (c === 'h' && !inLink && (i === 0 || /[\s(\[<„"“'‘]/.test(s[i - 1]!))) {
|
|
423
|
+
URL_RE.lastIndex = i
|
|
424
|
+
const m = URL_RE.exec(s)
|
|
425
|
+
const url = m ? trimUrl(m[0]) : ''
|
|
426
|
+
const href = url.length > 'https://'.length ? safeHref(url) : null
|
|
427
|
+
if (href) {
|
|
428
|
+
flush()
|
|
429
|
+
push(out, url, { ...marks, href })
|
|
430
|
+
i += url.length
|
|
431
|
+
continue
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
text += c
|
|
435
|
+
i++
|
|
436
|
+
}
|
|
437
|
+
flush()
|
|
438
|
+
}
|
package/src/community/links.ts
CHANGED
|
@@ -62,6 +62,14 @@ export const COMMUNITY_ROUTES = Object.freeze({
|
|
|
62
62
|
topic: '/community/t/{id}',
|
|
63
63
|
/** The forum's search. */
|
|
64
64
|
search: '/community/search',
|
|
65
|
+
// M4 PU1 (m4-contracts.md §6.1, A.2): the private view and the two grant routes the host's server serves. The
|
|
66
|
+
// storefront names its own (`/komunita/zpravy`, `/komunita/grant`, `/komunita/grant/handoff`).
|
|
67
|
+
/** The private view (the dock's „Soukromé zprávy"), client-only. */
|
|
68
|
+
messages: '/community/messages',
|
|
69
|
+
/** The grant route (POST, `createPrivateGrantHandler`): the page's private SDK posts its key's thumbprint. */
|
|
70
|
+
grant: '/community/grant',
|
|
71
|
+
/** The provider_page hand-off (GET, `createPrivateGrantHandoff`): the account host's `/zpravy` navigates here. */
|
|
72
|
+
grantHandoff: '/community/grant/handoff',
|
|
65
73
|
} as const)
|
|
66
74
|
|
|
67
75
|
/** A decision's id (kb-api's UUIDs). */
|
package/src/community/outcome.ts
CHANGED
|
@@ -179,6 +179,23 @@ export const COMMUNITY_API_CODES = [
|
|
|
179
179
|
'FOLLOW_LIMIT',
|
|
180
180
|
'STALE_REVISION',
|
|
181
181
|
'CASE_UNDECIDED',
|
|
182
|
+
'DPOP_INVALID',
|
|
183
|
+
'DPOP_NONCE',
|
|
184
|
+
'GRANT_INVALID',
|
|
185
|
+
'PRIVATE_IDENTITY',
|
|
186
|
+
'DEVICE_PENDING',
|
|
187
|
+
'DEVICE_REMOVED',
|
|
188
|
+
'APPROVAL_INVALID',
|
|
189
|
+
'APPROVAL_EXHAUSTED',
|
|
190
|
+
'ADDRESS_REQUIRED',
|
|
191
|
+
'ADDRESS_LOCKED',
|
|
192
|
+
'ACTIVATION_REQUIRED',
|
|
193
|
+
'TEXT_CHANGED',
|
|
194
|
+
'DM_NOT_ALLOWED',
|
|
195
|
+
'EDIT_WINDOW',
|
|
196
|
+
'DELETE_WINDOW',
|
|
197
|
+
'PRIVATE_RESTRICTED',
|
|
198
|
+
'STEP_UP_REQUIRED',
|
|
182
199
|
'NOT_A_MEMBER_SESSION',
|
|
183
200
|
'IDENTITY_NOT_ADMITTED',
|
|
184
201
|
'ACCOUNT_NOT_ELIGIBLE',
|
|
@@ -287,6 +304,26 @@ export const COMMUNITY_OUTCOME_OF_KB: Readonly<Record<CommunityApiCode, Communit
|
|
|
287
304
|
// CASE_UNDECIDED: the team's adverse-events/close while the case's message is held by no decision (decide it first).
|
|
288
305
|
STALE_REVISION: 'submit',
|
|
289
306
|
CASE_UNDECIDED: 'submit',
|
|
307
|
+
// M4 PX0 (m4-contracts.md Appendix A.6): the private, account, grant and staff surfaces' codes. No public, notice or
|
|
308
|
+
// forum operation produces them, and the private client that meets them (`@blogmcp/sdk/community/private`, PU1)
|
|
309
|
+
// handles most itself (a fresh nonce, a new key on DEVICE_REMOVED) — so here, as every code no element meets, `submit`.
|
|
310
|
+
DPOP_INVALID: 'submit',
|
|
311
|
+
DPOP_NONCE: 'submit',
|
|
312
|
+
GRANT_INVALID: 'submit',
|
|
313
|
+
PRIVATE_IDENTITY: 'submit',
|
|
314
|
+
DEVICE_PENDING: 'submit',
|
|
315
|
+
DEVICE_REMOVED: 'submit',
|
|
316
|
+
APPROVAL_INVALID: 'submit',
|
|
317
|
+
APPROVAL_EXHAUSTED: 'submit',
|
|
318
|
+
ADDRESS_REQUIRED: 'submit',
|
|
319
|
+
ADDRESS_LOCKED: 'submit',
|
|
320
|
+
ACTIVATION_REQUIRED: 'submit',
|
|
321
|
+
TEXT_CHANGED: 'submit',
|
|
322
|
+
DM_NOT_ALLOWED: 'submit',
|
|
323
|
+
EDIT_WINDOW: 'submit',
|
|
324
|
+
DELETE_WINDOW: 'submit',
|
|
325
|
+
PRIVATE_RESTRICTED: 'submit',
|
|
326
|
+
STEP_UP_REQUIRED: 'submit',
|
|
290
327
|
// A session that is not a reader's (the team, a key) or an identity the tenant does not admit: sign in as a reader.
|
|
291
328
|
NOT_A_MEMBER_SESSION: 'signIn',
|
|
292
329
|
IDENTITY_NOT_ADMITTED: 'signIn',
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The private entry runs in a browser only (docs/internal/m4-contracts.md §6.1, §7.7). Its key lives in IndexedDB and
|
|
3
|
+
* signs with WebCrypto, and the whole point of the device key is that it never leaves the member's browser: a copy of
|
|
4
|
+
* this code running on a server (an SSR pass of the host, a worker of the tenant's backend) would hold a key the
|
|
5
|
+
* member never approved and could not tell apart from their own device. So the entry refuses to load where `window`,
|
|
6
|
+
* `indexedDB` or `crypto.subtle` is missing — a structural test imports it on a server and expects the throw.
|
|
7
|
+
*
|
|
8
|
+
* Every module that holds or uses browser state calls `assertPrivateBrowser()` at load, not only the entry's index:
|
|
9
|
+
* the package is `sideEffects: false`, so a bundler may import a leaf module directly and skip the index.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** The message of the throw, so a host's error page can tell the case from a bug. */
|
|
13
|
+
export const PRIVATE_SERVER_IMPORT_MESSAGE =
|
|
14
|
+
'[blogmcp-sdk] @blogmcp/sdk/community/private runs only in a browser (it needs window, indexedDB and crypto.subtle): ' +
|
|
15
|
+
'the device key must never exist on a server. Import it from client-only code.'
|
|
16
|
+
|
|
17
|
+
/** True where the private entry may run: a global `window`, `indexedDB` and WebCrypto's `subtle`. */
|
|
18
|
+
export function isPrivateBrowser(): boolean {
|
|
19
|
+
const g = globalThis as { window?: unknown; indexedDB?: unknown; crypto?: { subtle?: unknown } }
|
|
20
|
+
return typeof g.window !== 'undefined' && g.window !== null && typeof g.indexedDB !== 'undefined' && g.indexedDB !== null && !!g.crypto?.subtle
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Throws everywhere but in a browser (see the module doc). */
|
|
24
|
+
export function assertPrivateBrowser(): void {
|
|
25
|
+
if (!isPrivateBrowser()) throw new Error(PRIVATE_SERVER_IMPORT_MESSAGE)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** WebCrypto's SubtleCrypto, read on every use (a test replaces the global between cases). */
|
|
29
|
+
export function subtle(): SubtleCrypto {
|
|
30
|
+
return globalThis.crypto.subtle
|
|
31
|
+
}
|