ferst-core 0.17.1 → 0.19.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/lib/members.ts CHANGED
@@ -1,134 +1,88 @@
1
- /**
2
- * Members: people who sign in on an organisation's sites (the Ferst Members module). The site
3
- * reaches the platform's members service only through its own address, `/ferst-api/…` (a Pages
4
- * Function the site template carries), so the session is a first-party cookie and nothing here
5
- * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
- *
7
- * Nothing a member reads is ever in the build: a members' page is a placeholder until the visitor's
8
- * browser asks for it, and the platform decides, per request, whether they may read it.
9
- *
10
- * The browser scripts (MembersContent, the header's account link) are thin; the decisions are
11
- * here, pure, so they're unit-tested.
12
- */
13
-
14
- /** Where a site's members service is, on the site's own address. */
15
- export const MEMBERS_API = '/ferst-api/members';
16
- /** The signed-in person, or `401 signed_out`. */
17
- export const MEMBERS_ME = `${MEMBERS_API}/me`;
18
- /** The members' account page: signing in, and the member's own details. */
19
- export const ACCOUNT_PATH = '/account';
20
-
21
- /** A members' page's id, as the portal makes them. */
22
- export const MEMBERS_PAGE_ID = /^mpg_[0-9a-f]{24}$/;
23
-
24
- /** Where the browser reads a members' page. */
25
- export function membersPageUrl(page: string): string {
26
- return `${MEMBERS_API}/pages/${encodeURIComponent(page)}`;
27
- }
28
-
29
- /** The account page, coming back here once signed in. */
30
- export function signInHref(here: string): string {
31
- return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
32
- }
33
-
34
- /** An answer from the members service: its status, and its JSON body (null when it isn't JSON). */
35
- export interface MembersAnswer {
36
- status: number;
37
- body: Record<string, unknown> | null;
38
- }
39
-
40
- /** Read a response as a MembersAnswer. Anything but JSON (a "page not found" page) has no body. */
41
- export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
42
- const json = /application\/json/i.test(res.headers.get('Content-Type') ?? '');
43
- let body: Record<string, unknown> | null = null;
44
- if (json) {
45
- try {
46
- const parsed: unknown = await res.json();
47
- body = parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : null;
48
- } catch {
49
- body = null;
50
- }
51
- }
52
- return { status: res.status, body };
53
- }
54
-
55
- /** What a membersContent block shows. */
56
- export type MembersPageView =
57
- | { kind: 'page'; title: string; html: string }
58
- /** 401: "This is for members. Sign in to read it". */
59
- | { kind: 'sign-in' }
60
- /** 403 not_for_you: a guest, someone waiting for approval, or a page for other members. */
61
- | { kind: 'not-included' }
62
- /** Anything else: the page is gone, Members is off for the site, the service is down. */
63
- | { kind: 'unavailable' }
64
- /** Running on a computer (`astro dev` / `astro preview`), where the site has no /ferst-api. */
65
- | { kind: 'local' };
66
-
67
- /**
68
- * What to show for the answer to `GET /ferst-api/members/pages/:id`. `local` is true when the site
69
- * runs on the author's own computer: there the answer is the dev server's "page not found", not the
70
- * service's, so the block says sign-in works on the published site.
71
- */
72
- export function membersPageView(answer: MembersAnswer | null, local = false): MembersPageView {
73
- if (!answer || !answer.body) return local ? { kind: 'local' } : { kind: 'unavailable' };
74
- const { status, body } = answer;
75
- if (status === 200 && typeof body.html === 'string') {
76
- return { kind: 'page', title: typeof body.title === 'string' ? body.title : '', html: body.html };
77
- }
78
- if (status === 401) return { kind: 'sign-in' };
79
- // 403 is also the answer when Members isn't on for the site (`module_off`): not about them.
80
- if (status === 403 && body.error === 'not_for_you') return { kind: 'not-included' };
81
- return { kind: 'unavailable' };
82
- }
83
-
84
- /** The words a members' page shows when it can't show the page itself. */
85
- export const MEMBERS_PAGE_TEXT = {
86
- signIn: { before: 'This is for members.', link: 'Sign in', after: 'to read it.' },
87
- notIncluded: 'This isn’t included in your membership.',
88
- unavailable: 'This can’t be shown right now.',
89
- local: 'Sign-in works on the published site.',
90
- } as const;
91
-
92
- /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
93
- export function firstName(displayName: unknown): string {
94
- if (typeof displayName !== 'string') return '';
95
- return displayName.trim().split(/\s+/)[0] ?? '';
96
- }
97
-
98
- /**
99
- * The header's account link, from the answer to `GET /ferst-api/members/me`: the member's first
100
- * name (or "Your account" when they have none yet), "Sign in" when signed out, and null (leave the
101
- * build's "Account") when the service can't say.
102
- */
103
- export function accountLinkText(answer: MembersAnswer | null): string | null {
104
- if (!answer || !answer.body) return null;
105
- if (answer.status === 401) return 'Sign in';
106
- if (answer.status !== 200) return null;
107
- const member = answer.body.member as { display_name?: unknown } | undefined;
108
- if (!member || typeof member !== 'object') return null;
109
- return firstName(member.display_name) || 'Your account';
110
- }
111
-
112
- /**
113
- * The header's account link without a flicker (0.17.1): the build says "Sign in" (most visitors
114
- * aren't signed in), and a tiny inline script puts back the label this browser last saw before the
115
- * page paints, under this key. The answer from `/me` still has the last word, and is remembered.
116
- */
117
- export const ACCOUNT_LABEL_KEY = 'ferst:account-label';
118
-
119
- /** A remembered label worth showing: one short line of plain text, as accountLinkText makes them. */
120
- export function rememberedLabel(v: unknown): string | null {
121
- if (typeof v !== 'string') return null;
122
- const t = v.trim();
123
- return t && t.length <= 40 && !/[\n\r<>]/.test(t) ? t : null;
124
- }
125
-
126
- /** Remember the link's label for the next page (nothing, where the browser keeps no storage). */
127
- export function rememberAccountLabel(text: string): void {
128
- try { localStorage.setItem(ACCOUNT_LABEL_KEY, text); } catch { /* private mode, storage off */ }
129
- }
130
-
131
- /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
132
- export function isLocalHost(hostname: string): boolean {
133
- return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
134
- }
1
+ /**
2
+ * Members: people who sign in on an organisation's sites (the Ferst Members module). The site
3
+ * reaches the platform's members service only through its own address, `/ferst-api/…` (a Pages
4
+ * Function the site template carries), so the session is a first-party cookie and nothing here
5
+ * needs CORS. Every call is same-origin, `credentials: 'same-origin'`, JSON.
6
+ *
7
+ * Nothing a member reads is ever in the build.
8
+ *
9
+ * The browser scripts (the header's account link, the account page) are thin; the decisions are
10
+ * here, pure, so they're unit-tested.
11
+ */
12
+
13
+ /** Where a site's members service is, on the site's own address. */
14
+ export const MEMBERS_API = '/ferst-api/members';
15
+ /** The signed-in person, or `401 signed_out`. */
16
+ export const MEMBERS_ME = `${MEMBERS_API}/me`;
17
+ /** The members' account page: signing in, and the member's own details. */
18
+ export const ACCOUNT_PATH = '/account';
19
+
20
+ /** The account page, coming back here once signed in. */
21
+ export function signInHref(here: string): string {
22
+ return `${ACCOUNT_PATH}?next=${encodeURIComponent(here)}`;
23
+ }
24
+
25
+ /** An answer from the members service: its status, and its JSON body (null when it isn't JSON). */
26
+ export interface MembersAnswer {
27
+ status: number;
28
+ body: Record<string, unknown> | null;
29
+ }
30
+
31
+ /** Read a response as a MembersAnswer. Anything but JSON (a "page not found" page) has no body. */
32
+ export async function readMembersAnswer(res: Response): Promise<MembersAnswer> {
33
+ const json = /application\/json/i.test(res.headers.get('Content-Type') ?? '');
34
+ let body: Record<string, unknown> | null = null;
35
+ if (json) {
36
+ try {
37
+ const parsed: unknown = await res.json();
38
+ body = parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : null;
39
+ } catch {
40
+ body = null;
41
+ }
42
+ }
43
+ return { status: res.status, body };
44
+ }
45
+
46
+ /** The first word of a person's name, for the header ("Sam" for "Sam Hill"). */
47
+ export function firstName(displayName: unknown): string {
48
+ if (typeof displayName !== 'string') return '';
49
+ return displayName.trim().split(/\s+/)[0] ?? '';
50
+ }
51
+
52
+ /**
53
+ * The header's account link, from the answer to `GET /ferst-api/members/me`: the member's first
54
+ * name (or "Your account" when they have none yet), "Sign in" when signed out, and null (leave the
55
+ * build's "Account") when the service can't say.
56
+ */
57
+ export function accountLinkText(answer: MembersAnswer | null): string | null {
58
+ if (!answer || !answer.body) return null;
59
+ if (answer.status === 401) return 'Sign in';
60
+ if (answer.status !== 200) return null;
61
+ const member = answer.body.member as { display_name?: unknown } | undefined;
62
+ if (!member || typeof member !== 'object') return null;
63
+ return firstName(member.display_name) || 'Your account';
64
+ }
65
+
66
+ /**
67
+ * The header's account link without a flicker (0.17.1): the build says "Sign in" (most visitors
68
+ * aren't signed in), and a tiny inline script puts back the label this browser last saw before the
69
+ * page paints, under this key. The answer from `/me` still has the last word, and is remembered.
70
+ */
71
+ export const ACCOUNT_LABEL_KEY = 'ferst:account-label';
72
+
73
+ /** A remembered label worth showing: one short line of plain text, as accountLinkText makes them. */
74
+ export function rememberedLabel(v: unknown): string | null {
75
+ if (typeof v !== 'string') return null;
76
+ const t = v.trim();
77
+ return t && t.length <= 40 && !/[\n\r<>]/.test(t) ? t : null;
78
+ }
79
+
80
+ /** Remember the link's label for the next page (nothing, where the browser keeps no storage). */
81
+ export function rememberAccountLabel(text: string): void {
82
+ try { localStorage.setItem(ACCOUNT_LABEL_KEY, text); } catch { /* private mode, storage off */ }
83
+ }
84
+
85
+ /** True on the author's own computer, where `/ferst-api` isn't there (no Pages Functions). */
86
+ export function isLocalHost(hostname: string): boolean {
87
+ return /^(localhost|127\.0\.0\.1|\[::1\]|::1)$/.test(hostname) || hostname.endsWith('.localhost');
88
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ferst-core",
3
- "version": "0.17.1",
3
+ "version": "0.19.0",
4
4
  "type": "module",
5
5
  "description": "Ferst Core — the shared, brand-agnostic client-site system: Astro components, layouts, content schemas, the layered config resolver, and a neutral styling architecture that every client overrides.",
6
6
  "license": "BUSL-1.1",
@@ -1,176 +0,0 @@
1
- ---
2
- /**
3
- * MembersContent — a members' page from the Ferst Members module, written in the portal. The
4
- * build renders only a placeholder (and the block's title, when it has one): what members read is
5
- * never in the build. In the visitor's browser the block asks the site for the page
6
- * (`/ferst-api/members/pages/<id>`, same-origin, the session cookie going with it), and the
7
- * platform checks who is signed in. Then it shows the page, a sign-in prompt (401), "not included"
8
- * (403) or "can't be shown" (anything else). On the author's own computer (`astro dev` /
9
- * `astro preview`, no Pages Functions) it says sign-in works on the published site. lib/members
10
- * holds the decisions.
11
- */
12
- import Heading from '../blocks/Heading.astro';
13
- import { MEMBERS_PAGE_TEXT, membersPageUrl } from '../../lib/members';
14
-
15
- interface Props {
16
- page: string;
17
- title?: string;
18
- }
19
- const { page, title } = Astro.props;
20
- const { align: blockAlign, headingAlign } = Astro.props as { align?: 'start' | 'center' | 'end'; headingAlign?: 'start' | 'center' | 'end' };
21
- const headAlign = headingAlign ?? blockAlign;
22
- const signIn = MEMBERS_PAGE_TEXT.signIn;
23
- ---
24
-
25
- <section class="b-members" data-members-page={membersPageUrl(page)} data-members-own-title={title ? undefined : ''} aria-busy="true">
26
- <div class="b-members__head" data-recipe-head data-content-align={headAlign} hidden={!title}>
27
- <Heading text={title ?? ''} level={2} />
28
- </div>
29
- <div class="b-members__skeleton" data-members-skeleton aria-hidden="true">
30
- <span></span><span></span><span></span>
31
- </div>
32
- <div class="b-members__body" data-members-body hidden></div>
33
- <p class="b-members__notice" role="status" data-members-notice="sign-in" hidden>
34
- {signIn.before} <a href="/account" data-members-sign-in>{signIn.link}</a> {signIn.after}
35
- </p>
36
- <p class="b-members__notice" role="status" data-members-notice="not-included" hidden>{MEMBERS_PAGE_TEXT.notIncluded}</p>
37
- <p class="b-members__notice" role="status" data-members-notice="unavailable" hidden>{MEMBERS_PAGE_TEXT.unavailable}</p>
38
- <p class="b-members__notice" role="status" data-members-notice="local" hidden>{MEMBERS_PAGE_TEXT.local}</p>
39
- </section>
40
-
41
- <script>
42
- import { isLocalHost, membersPageView, readMembersAnswer, signInHref, type MembersAnswer } from '../../lib/members';
43
-
44
- async function ask(url: string): Promise<MembersAnswer | null> {
45
- try {
46
- const res = await fetch(url, { credentials: 'same-origin', headers: { Accept: 'application/json' } });
47
- return await readMembersAnswer(res);
48
- } catch {
49
- return null;
50
- }
51
- }
52
-
53
- async function setUp(block: HTMLElement) {
54
- const url = block.dataset.membersPage;
55
- if (!url) return;
56
- const view = membersPageView(await ask(url), import.meta.env.DEV || isLocalHost(location.hostname));
57
- block.querySelector<HTMLElement>('[data-members-skeleton]')?.remove();
58
- block.removeAttribute('aria-busy');
59
- if (view.kind === 'page') {
60
- // The page's own title, unless the block gives its own.
61
- const head = block.querySelector<HTMLElement>('.b-members__head');
62
- if (head && block.dataset.membersOwnTitle !== undefined && view.title) {
63
- const heading = head.querySelector<HTMLElement>('.b-heading');
64
- if (heading) heading.textContent = view.title;
65
- head.hidden = false;
66
- }
67
- const body = block.querySelector<HTMLElement>('[data-members-body]');
68
- if (body) {
69
- // The platform renders the page's Markdown and allows only plain tags (no scripts, no styles).
70
- body.innerHTML = view.html;
71
- body.hidden = false;
72
- }
73
- return;
74
- }
75
- if (view.kind === 'sign-in') {
76
- const link = block.querySelector<HTMLAnchorElement>('[data-members-sign-in]');
77
- if (link) link.href = signInHref(location.pathname + location.search);
78
- }
79
- const notice = block.querySelector<HTMLElement>(`[data-members-notice="${view.kind}"]`);
80
- if (notice) notice.hidden = false;
81
- }
82
-
83
- document.querySelectorAll<HTMLElement>('[data-members-page]').forEach((b) => { void setUp(b); });
84
- </script>
85
-
86
- <style>
87
- .b-members {
88
- display: flex;
89
- flex-direction: column;
90
- gap: 1rem;
91
- max-width: var(--max);
92
- margin-inline: auto;
93
- padding-inline: 1rem;
94
- color: var(--fg);
95
- }
96
- .b-members__head[hidden],
97
- .b-members__body[hidden],
98
- .b-members__notice[hidden] {
99
- display: none;
100
- }
101
-
102
- /* The placeholder while the page loads: three quiet bars, still under reduced motion. */
103
- .b-members__skeleton {
104
- display: flex;
105
- flex-direction: column;
106
- gap: 0.75rem;
107
- }
108
- .b-members__skeleton span {
109
- display: block;
110
- height: 1rem;
111
- border-radius: var(--radius-sm);
112
- background: var(--bg-section);
113
- animation: b-members-pulse 1.6s ease-in-out infinite;
114
- }
115
- .b-members__skeleton span:nth-child(2) { width: 92%; }
116
- .b-members__skeleton span:nth-child(3) { width: 64%; }
117
- @keyframes b-members-pulse {
118
- 50% { opacity: 0.55; }
119
- }
120
- @media (prefers-reduced-motion: reduce) {
121
- .b-members__skeleton span { animation: none; }
122
- }
123
-
124
- /* The page itself: plain Markdown from the portal, read like a Prose block. */
125
- .b-members__body :global(:is(p, ul, ol, blockquote, pre, table)) {
126
- margin: 0 0 1rem;
127
- line-height: 1.7;
128
- }
129
- .b-members__body :global(:is(ul, ol)) {
130
- padding-inline-start: 1.4em;
131
- }
132
- .b-members__body :global(li + li) {
133
- margin-top: 0.25rem;
134
- }
135
- .b-members__body :global(:is(h1, h2, h3, h4, h5, h6)) {
136
- margin: 1.5rem 0 0.75rem;
137
- line-height: 1.25;
138
- color: var(--fg);
139
- }
140
- .b-members__body :global(a) {
141
- color: var(--accent);
142
- text-decoration: underline;
143
- text-underline-offset: 0.15em;
144
- }
145
- .b-members__body :global(img) {
146
- max-width: 100%;
147
- height: auto;
148
- border-radius: var(--radius-sm);
149
- }
150
- .b-members__body :global(blockquote) {
151
- padding-inline-start: 1rem;
152
- border-inline-start: 3px solid var(--border);
153
- color: var(--muted);
154
- }
155
- .b-members__body > :global(:first-child) {
156
- margin-top: 0;
157
- }
158
- .b-members__body > :global(:last-child) {
159
- margin-bottom: 0;
160
- }
161
-
162
- .b-members__notice {
163
- margin: 0;
164
- padding: 0.85rem 1rem;
165
- border: 1px solid var(--border);
166
- border-radius: var(--radius-sm);
167
- background: var(--bg-section);
168
- line-height: 1.5;
169
- }
170
- .b-members__notice a {
171
- color: var(--accent);
172
- font-weight: 600;
173
- text-decoration: underline;
174
- text-underline-offset: 0.15em;
175
- }
176
- </style>