@blogmcp/sdk 0.9.4 → 0.10.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.
Files changed (59) hide show
  1. package/dist/community/channels.d.ts +7 -6
  2. package/dist/community/channels.js +4 -4
  3. package/dist/community/client.d.ts +74 -4
  4. package/dist/community/client.js +143 -91
  5. package/dist/community/index.d.ts +11 -6
  6. package/dist/community/index.js +111 -80
  7. package/dist/community/links.d.ts +61 -1
  8. package/dist/community/links.js +94 -40
  9. package/dist/community/manifest/index.d.ts +54 -0
  10. package/dist/community/manifest/index.js +46 -0
  11. package/dist/community/mentions.d.ts +38 -0
  12. package/dist/community/mentions.js +25 -0
  13. package/dist/community/messages.cs.d.ts +73 -3
  14. package/dist/community/messages.cs.js +82 -3
  15. package/dist/community/messages.d.ts +11 -0
  16. package/dist/community/messages.en.d.ts +69 -1
  17. package/dist/community/messages.en.js +71 -1
  18. package/dist/community/messages.js +26 -16
  19. package/dist/community/outcome.d.ts +8 -3
  20. package/dist/community/outcome.js +75 -28
  21. package/dist/community/realtime/index.d.ts +118 -0
  22. package/dist/community/realtime/index.js +215 -0
  23. package/dist/community/routes.d.ts +12 -0
  24. package/dist/community/routes.js +41 -25
  25. package/dist/community/server/bff.js +25 -25
  26. package/dist/community/server/host-routes.js +12 -0
  27. package/dist/community/server/index.d.ts +1 -1
  28. package/dist/community/server/index.js +15 -14
  29. package/dist/community/server/route-decision.d.ts +8 -0
  30. package/dist/community/server/route-decision.js +28 -22
  31. package/dist/community/shapes.d.ts +214 -2
  32. package/dist/community/shapes.js +65 -25
  33. package/dist/community/store.d.ts +18 -1
  34. package/dist/community/store.js +316 -261
  35. package/dist/community/sw/index.d.ts +45 -0
  36. package/dist/community/sw/index.js +59 -0
  37. package/dist/community/ua.d.ts +12 -0
  38. package/dist/community/ua.js +9 -0
  39. package/package.json +20 -3
  40. package/src/community/channels.ts +8 -7
  41. package/src/community/client.ts +116 -5
  42. package/src/community/index.ts +59 -1
  43. package/src/community/links.ts +119 -2
  44. package/src/community/manifest/index.ts +107 -0
  45. package/src/community/mentions.ts +59 -0
  46. package/src/community/messages.cs.ts +88 -3
  47. package/src/community/messages.en.ts +77 -1
  48. package/src/community/messages.ts +19 -0
  49. package/src/community/outcome.ts +53 -1
  50. package/src/community/realtime/index.ts +522 -0
  51. package/src/community/routes.ts +16 -0
  52. package/src/community/server/bff.ts +2 -4
  53. package/src/community/server/host-routes.ts +12 -0
  54. package/src/community/server/index.ts +1 -0
  55. package/src/community/server/route-decision.ts +15 -0
  56. package/src/community/shapes.ts +256 -3
  57. package/src/community/store.ts +114 -0
  58. package/src/community/sw/index.ts +112 -0
  59. package/src/community/ua.ts +17 -0
@@ -0,0 +1,112 @@
1
+ /**
2
+ * `@blogmcp/sdk/community/sw` — the community's service worker, as source text (community M2, slice T4;
3
+ * docs/internal/m2-contracts.md §3.4 "The service worker and the manifest"; community.md §7.5).
4
+ *
5
+ * A host serves the string this returns from one route, as `text/javascript` (COMMUNITY_SW_CONTENT_TYPE), at its
6
+ * base: the managed blogs' `/blog/sw.js` (scope `/blog/`), the storefront's `/sw.js` — no bundler step. The worker
7
+ * does two things and nothing else:
8
+ * - `push`: it shows ONE notification with the generic text („Novinky v komunitě <tenant>" / "News in the
9
+ * <tenant> community"), `tag: 'community'` (a newer one replaces it), `renotify: false`, the badge icon. The
10
+ * payload kb-api sends is exactly `{"t":"community"}`; the worker reads `t` and nothing else, and what it shows
11
+ * never depends on the payload (invariant 6: nothing of the event — no count, no name, no text — leaves kb-api);
12
+ * - `notificationclick`: it focuses a window of the notification centre, or opens one (`centrePath`).
13
+ * No `fetch` handler and no cache (`caches`): the site works exactly as it does without the worker. A structural
14
+ * test (test/community/sw.spec.ts) pins all of it on the returned string.
15
+ *
16
+ * The worker registers only from the settings page or the centre, on the member's click („Zapnout upozornění"),
17
+ * never on page load — that is the host's code, with `pushManager.subscribe({ userVisibleOnly: true,
18
+ * applicationServerKey })` and the key from `me.push.vapid()`.
19
+ *
20
+ * Pure: no DOM, no globals, no import (it runs on the host's server and returns text).
21
+ */
22
+
23
+ /** The Content-Type a host serves the worker with. */
24
+ export const COMMUNITY_SW_CONTENT_TYPE = 'text/javascript; charset=utf-8'
25
+
26
+ /** The notification's tag: one community notification at a time, the newest replacing the last. */
27
+ export const COMMUNITY_SW_TAG = 'community'
28
+
29
+ /** The generic notification text (the only text a push shows), per locale; `tenant` is the site's name. */
30
+ export const COMMUNITY_PUSH_TEXTS: Readonly<Record<'cs' | 'en', (tenant: string | null) => string>> = Object.freeze({
31
+ cs: (tenant: string | null) => (tenant ? `Novinky v komunitě ${tenant}` : 'Novinky v komunitě'),
32
+ en: (tenant: string | null) => (tenant ? `News in the ${tenant} community` : 'News in the community'),
33
+ })
34
+
35
+ /** The generic text for a locale (`cs-CZ` → cs; anything else → en). */
36
+ export function communityPushText(locale: string | null | undefined, tenant?: string | null): string {
37
+ const lang = typeof locale === 'string' && /^cs(?:-|$)/i.test(locale) ? 'cs' : 'en'
38
+ const name = typeof tenant === 'string' && tenant.trim() !== '' ? tenant.trim() : null
39
+ return COMMUNITY_PUSH_TEXTS[lang](name)
40
+ }
41
+
42
+ export interface CommunityServiceWorkerOptions {
43
+ /** Where a click goes: the notification centre's path on this site (`/community/notifications`, `/komunita/upozorneni`). */
44
+ readonly centrePath: string
45
+ /** The notification's one line; default communityPushText(locale, tenant). */
46
+ readonly badgeText?: string
47
+ /** The site's name in the default text. */
48
+ readonly tenant?: string | null
49
+ /** The default text's language (`cs`, `en`). */
50
+ readonly locale?: string | null
51
+ /** The notification's icon, a path on this site. */
52
+ readonly icon?: string | null
53
+ /** The monochrome badge (Android's status bar), a path on this site. */
54
+ readonly badge?: string | null
55
+ }
56
+
57
+ const TEXT_MAX = 120
58
+
59
+ /** A same-origin path: `/…`, never `//host` or a scheme. */
60
+ function sitePath(value: unknown, what: string): string {
61
+ if (typeof value !== 'string' || !/^\/(?!\/)[^\s\\]*$/.test(value) || value.length > 512) {
62
+ throw new TypeError(`communityServiceWorkerSource: ${what} must be a path on this site (starting with a single /)`)
63
+ }
64
+ return value
65
+ }
66
+
67
+ /** JSON for a JavaScript source: `<`, U+2028 and U+2029 escaped (the string is served as a script). */
68
+ function jsonForScript(value: unknown): string {
69
+ return JSON.stringify(value).replace(/</g, '\\u003c').replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029')
70
+ }
71
+
72
+ /** The worker's source (see the module comment). Throws a TypeError for a path that is not this site's. */
73
+ export function communityServiceWorkerSource(options: CommunityServiceWorkerOptions): string {
74
+ const centre = sitePath(options?.centrePath, 'centrePath')
75
+ const icon = options.icon == null ? null : sitePath(options.icon, 'icon')
76
+ const badge = options.badge == null ? null : sitePath(options.badge, 'badge')
77
+ const raw = typeof options.badgeText === 'string' && options.badgeText.trim() !== '' ? options.badgeText : communityPushText(options.locale, options.tenant)
78
+ const title = raw.replace(/[\u0000-\u001f\u007f]/g, ' ').trim().slice(0, TEXT_MAX)
79
+ const config = { title, centre, tag: COMMUNITY_SW_TAG, icon, badge }
80
+ return `/* @blogmcp/sdk/community/sw — generated; a push shows one generic notification, a click opens the centre. */
81
+ 'use strict';
82
+ const CONFIG = ${jsonForScript(config)};
83
+
84
+ self.addEventListener('push', (event) => {
85
+ // The payload is {"t":"community"}: only t is read, and nothing shown depends on it.
86
+ let t = null;
87
+ try {
88
+ const data = event.data ? event.data.json() : null;
89
+ t = data && typeof data.t === 'string' ? data.t : null;
90
+ } catch (err) {
91
+ t = null;
92
+ }
93
+ const options = { tag: CONFIG.tag, renotify: false, data: { kind: t === 'community' ? 'community' : 'other' } };
94
+ if (CONFIG.icon) options.icon = CONFIG.icon;
95
+ if (CONFIG.badge) options.badge = CONFIG.badge;
96
+ event.waitUntil(self.registration.showNotification(CONFIG.title, options));
97
+ });
98
+
99
+ self.addEventListener('notificationclick', (event) => {
100
+ event.notification.close();
101
+ const target = new URL(CONFIG.centre, self.registration.scope).href;
102
+ event.waitUntil(
103
+ self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((windows) => {
104
+ for (const w of windows) {
105
+ if (w.url === target && 'focus' in w) return w.focus();
106
+ }
107
+ return self.clients.openWindow(target);
108
+ }),
109
+ );
110
+ });
111
+ `
112
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The one user-agent sniff of the SDK (community M2, slice T4; docs/internal/m2-contracts.md §3.4): Facebook's and
3
+ * Instagram's in-app browsers cannot take web push, so `<blogmcp-member-settings>` offers „Otevřít v prohlížeči"
4
+ * with the page's URL instead of „Zapnout upozornění". Nothing else in the SDK reads the user agent
5
+ * (test/community/ua.spec.ts pins it).
6
+ *
7
+ * Browser-safe: reads `navigator.userAgent` only when no string is given (and `navigator` exists).
8
+ */
9
+
10
+ /** The in-app browsers' markers: FBAN / FBAV (Facebook, Messenger) and Instagram. */
11
+ export const IN_APP_BROWSER_RE = /FBAN|FBAV|Instagram/
12
+
13
+ /** Whether the page runs in Facebook's or Instagram's in-app browser. */
14
+ export function isInAppBrowser(userAgent?: string | null): boolean {
15
+ const ua = typeof userAgent === 'string' ? userAgent : typeof navigator !== 'undefined' && typeof navigator.userAgent === 'string' ? navigator.userAgent : ''
16
+ return IN_APP_BROWSER_RE.test(ua)
17
+ }