@redeemid/emdash 0.0.12 → 0.0.14

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Shopify customer accounts, member levels and paywalls for [EmDash](https://github.com/emdash-cms/emdash) sites. Shopify runs the store and the customer account; EmDash runs the content.
4
4
 
5
- **Status: 0.0.12, early.** Runs on Node and Cloudflare Workers. Sign-in, the member mirror, webhooks, levels, rules, the meter and drip, teasers, badges, the page gate, the Shopify cart and checkout, the staff tools, preferences, protected files, privacy requests, AI agent tools, the command line, replacement text and outbound events are covered by an end-to-end test against a fake store.
5
+ **Status: 0.0.14, early.** Runs on Node and Cloudflare Workers. Sign-in, the member mirror, webhooks, levels, rules, the meter and drip, teasers, paywall markup, badges, the page gate, the Shopify cart and checkout, the staff tools, preferences, protected files, privacy requests, AI agent tools, the command line, replacement text and outbound events are covered by an end-to-end test against a fake store.
6
6
 
7
7
  ## What it does
8
8
 
@@ -241,7 +241,7 @@ With `agents: { actions: true }` in the config, three actions are offered too:
241
241
  | MCP tool | Command | What it does |
242
242
  | --- | --- | --- |
243
243
  | `resync_member` | `resync <customer id>` | Reads one member from Shopify and updates the site. Changes nothing in Shopify. |
244
- | `resync_batch` | `resync-batch` | Reads the 50 members synced longest ago. |
244
+ | `resync_batch` | `resync-batch` | Reads the 25 members synced longest ago. |
245
245
  | `edit_member_tags` | `tags <customer id> --add a,b --remove c` | Adds or removes tags on the Shopify customer, then reads back what Shopify holds. It's marked destructive for agents. |
246
246
 
247
247
  **Agents.** EmDash's MCP server is at `/_emdash/api/mcp`, on EmDash versions with plugin MCP tools. RedeemID's tools stay off until an administrator turns them on for RedeemID under **Plugins**. Turning them on approves the list as it stands, and a release that changes the list needs approving again. The agent uses an EmDash API token with the `mcp:tools` or `mcp:tools:redeemid` scope, made by an administrator. Tool names carry the plugin's prefix, for example `redeemid__explain_access`.
@@ -295,7 +295,7 @@ No event carries an email address or a name: match people by the Shopify `custom
295
295
  With `REDEEMID_SHOPIFY_ADMIN_APP_CLIENT_ID` set (it also runs member preferences), RedeemID gets an Admin API token for the webhook app with the client credentials grant (the app must belong to the same Shopify organization as the store, as Dev Dashboard apps do) and needs the `read_customers` and `write_customers` scopes. Then, in the admin:
296
296
 
297
297
  - **Member detail.** Open a member from the Members page to see their levels, tags and features, resync them from Shopify, or add and remove tags. Tag edits are made in Shopify, then read back; nothing is granted on the EmDash side.
298
- - **Bulk resync.** Reads the 50 members synced longest ago from Shopify in one request. Run it again for the next 50. It never runs on its own.
298
+ - **Bulk resync.** Reads the 25 members synced longest ago from Shopify in one request, few enough for the Workers Free limit of 50 database queries a request. Run it again for the next 25. It never runs on its own.
299
299
  - **Webhooks.** The Status page lists the store's subscriptions to this site and registers any that are missing (`customers/redact` stays in the app's own settings).
300
300
  - **Recent deliveries.** The last webhooks the site took, with their topics and times.
301
301
  - **Access test.** Enter a collection, terms and a reader's tags to see what the entry needs and whether that reader gets in.
@@ -310,13 +310,15 @@ EmDash has no content filter hook, so templates decide what to render. Render th
310
310
  ---
311
311
  import Content from "@redeemid/emdash/components/Content.astro";
312
312
  ---
313
- <Content value={post.data.content} collection="posts" terms={post.data.terms} id={post.id}>
313
+ <Content value={post.data.content} collection="posts" terms={post.data.terms} id={post.id} title={post.data.title}>
314
314
  <a slot="locked" href="/join/">Become a member</a>
315
315
  </Content>
316
316
  ```
317
317
 
318
318
  Everything before the first Paywall break block is shown to everyone. What follows reaches the page only for readers the break allows: the levels picked on it ("Signed in" for any member), or when none are picked the entry's rule. With none picked and no rule covering the entry, the break changes nothing. A body with no break follows the entry's rule as a whole; readers it keeps out see the collection's `teaser` (the paragraphs within that many words, never half a paragraph), if one is set. Plain `<PortableText>` ignores the break and shows everything, so use `<Content>` for any body that may hold one.
319
319
 
320
+ On the entry's own page (with `id`), a locked entry also gets Google's paywalled-content markup: JSON-LD saying `isAccessibleForFree: false`, with the wall (`.redeemid-locked`) as the part that isn't free. Search engines can then tell a paywall from cloaking. Pass `title` for its headline. Set `schema: false` in the config if an SEO plugin already adds this markup.
321
+
320
322
  A "Members only" block starts a part for the levels picked on it (none picked: the entry's rule, or any signed-in member) that runs to the next "Members only: end" block. Readers who can't read it see the wall there, and the body goes on after the end block.
321
323
 
322
324
  The wall is `<LockedPanel>`: it says why the reader is kept out and offers sign-in or the join link, with the wall text of the first level they are missing. `<Content>` and `<Gate>` show it; its default slot (and `<Content>`'s "locked" slot) goes on top.
@@ -360,6 +362,12 @@ import Plan from "@redeemid/emdash/components/Plan.astro";
360
362
  </Plan>
361
363
  ```
362
364
 
365
+ ### Caching
366
+
367
+ `redeemidGuard()` keeps every site page private for a signed-in visitor or a visitor with a cart: `Cache-Control: private, no-store`, and out of Astro's route cache (it calls `cache.set(false)` after the page has set its own cache hint). Visitors with neither get the same page, so their pages stay cacheable.
368
+
369
+ A route cache keys pages by URL alone and serves a stored page without running any code. So if the site turns on Astro's route cache, don't cache the pages that render `<Content>` or `<Gate>` for locked entries: a member would get the visitor's stored copy, wall included. Call `Astro.cache.set(hint)` only on pages whose entries no rule covers.
370
+
363
371
  ### Shop
364
372
 
365
373
  Products, collections and the cart come from the Storefront API; Shopify works out prices, discounts, taxes and stock.
@@ -28,7 +28,7 @@ Commands:
28
28
  health What's set up (never the values)
29
29
  changes --since <date> Who joined or left a level [--level <slug>]
30
30
  resync <customer id> Read one member from Shopify again *
31
- resync-batch Read the 50 members synced longest ago *
31
+ resync-batch Read the 25 members synced longest ago *
32
32
  tags <customer id> Edit tags in Shopify: --add a,b --remove c *
33
33
 
34
34
  * only when the site sets agents: { actions: true }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redeemid/emdash",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "Shopify customer accounts, member levels and paywalls for EmDash sites. Shopify runs commerce; EmDash runs content.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/admin-api.ts CHANGED
@@ -124,9 +124,12 @@ export async function customers(config: RedeemIDConfig, ids: string[], fetcher:
124
124
  */
125
125
  export async function resync(config: RedeemIDConfig, s: Stores, ids: string[], source = "resync", fetcher: typeof fetch = fetch): Promise<{ updated: MemberRecord[]; missing: string[] }> {
126
126
  const found = await customers(config, ids, fetcher);
127
+ // One read for the whole batch: on Workers Free one request may make only
128
+ // 50 database queries, and each write below is one.
129
+ const known = await s.members.getMany(found.map((c) => c.customerId));
127
130
  const updated: MemberRecord[] = [];
128
131
  for (const c of found) {
129
- const m = (await s.members.get(c.customerId)) ?? emptyMember(c.customerId);
132
+ const m = known.get(c.customerId) ?? emptyMember(c.customerId);
130
133
  m.email = c.email ?? m.email;
131
134
  m.firstName = c.firstName ?? m.firstName;
132
135
  m.lastName = c.lastName ?? m.lastName;
@@ -24,6 +24,11 @@
24
24
  * `drip` keeps it shut until the member has held their level that many days,
25
25
  * and config `meter` lets signed-in readers open a few locked entries each
26
26
  * period (each entry counts once). Leave `id` off in lists.
27
+ *
28
+ * With `id`, a locked entry's page also gets Google's paywalled-content
29
+ * markup (isAccessibleForFree false, pointing at the wall), the same for
30
+ * every reader. Pass `title` for its headline. Config `schema: false` turns
31
+ * it off.
27
32
  */
28
33
  import { PortableText } from "emdash/ui";
29
34
 
@@ -37,11 +42,13 @@ interface Props {
37
42
  value?: unknown[] | null;
38
43
  collection?: string;
39
44
  terms?: Terms;
40
- /** The entry's id, on its own page: turns on drip and the meter. */
45
+ /** The entry's id, on its own page: turns on drip, the meter and the paywall markup. */
41
46
  id?: string;
47
+ /** The entry's title, for the paywall markup's headline. */
48
+ title?: string;
42
49
  }
43
50
 
44
- const { value, collection = "", terms, id } = Astro.props;
51
+ const { value, collection = "", terms, id, title } = Astro.props;
45
52
  const split = splitAtPaywall(value);
46
53
  const config = siteConfig((Astro.locals as { emdash?: { config?: unknown } }).emdash?.config);
47
54
  const entryRule = await entryRequirement(config, collection, terms);
@@ -77,6 +84,19 @@ async function parts(blocks: unknown[]) {
77
84
  }
78
85
  const before = await parts(shown);
79
86
  const after = await parts(rest);
87
+ // Google's paywalled-content markup, on the entry's own page when any of it is locked.
88
+ const lockedEntry = Boolean(split.paywall ? required : entryRule) || sections(value ?? []).some((s) => s.members);
89
+ const schema =
90
+ id && lockedEntry && config?.schema !== false
91
+ ? JSON.stringify({
92
+ "@context": "https://schema.org",
93
+ "@type": "Article",
94
+ ...(title ? { headline: title } : {}),
95
+ url: Astro.url.origin + Astro.url.pathname,
96
+ isAccessibleForFree: false,
97
+ hasPart: { "@type": "WebPageElement", isAccessibleForFree: false, cssSelector: ".redeemid-locked" },
98
+ }).replace(/</g, "\\u003c")
99
+ : null;
80
100
  const metered = verdict.reason === "metered" ? (verdict as Verdict) : null;
81
101
  const week = metered?.meter?.period === "week";
82
102
  const left = metered?.meterLeft ?? 0;
@@ -88,6 +108,7 @@ const meterText =
88
108
  : text(config, week ? "You have {count} free reads left this week." : "You have {count} free reads left this month.", { count: left });
89
109
  ---
90
110
 
111
+ {schema && <script type="application/ld+json" set:html={schema} />}
91
112
  {metered && (
92
113
  <p class="redeemid-meter" role="status" data-redeemid-left={metered.meterLeft}>
93
114
  {meterText}
package/src/config.ts CHANGED
@@ -150,6 +150,12 @@ export interface RedeemIDConfig {
150
150
  * one they see none of it.
151
151
  */
152
152
  teaser?: Record<string, number>;
153
+ /**
154
+ * Google's paywalled-content markup (isAccessibleForFree false, pointing at
155
+ * the wall) on the page of each locked entry, printed by <Content> when it
156
+ * gets `id`. Set false when an SEO plugin already adds it.
157
+ */
158
+ schema?: false;
153
159
  /**
154
160
  * Extra /_emdash path prefixes signed-in members may use. Everything else
155
161
  * under /_emdash is refused to Subscribers (see guard.ts).
package/src/guard.ts CHANGED
@@ -1,16 +1,28 @@
1
1
  /**
2
- * Keeps members out of EmDash's own APIs and admin.
2
+ * Keeps members out of EmDash's own APIs and admin, and keeps their pages out
3
+ * of shared caches.
3
4
  *
4
5
  * EmDash's Subscriber role may read every published entry through
5
6
  * /_emdash/api/content. With RedeemID, access comes from Shopify tags only, so
6
7
  * a member with one level could otherwise read another level's posts there.
7
8
  * This middleware runs after EmDash's auth and refuses Subscribers on every
8
9
  * /_emdash path except the few public pages need.
10
+ *
11
+ * Site pages for a signed-in visitor, or a visitor with a cart, are private:
12
+ * `Cache-Control: private, no-store` and out of Astro's route cache. The route
13
+ * cache ignores Cache-Control and takes its options from `cache.set()`, which
14
+ * EmDash templates call on every page with the content's cache hint, and it
15
+ * serves a stored page to the next visitor without running any code. EmDash
16
+ * opts editors out, not Subscribers, so without this a member's page (locked
17
+ * text included) could be stored and served to anyone. A later
18
+ * `Astro.cache.set(hint)` turns caching back on, and the route cache reads its
19
+ * options only after the page has rendered, so the opt-out runs after `next()`.
9
20
  */
10
21
 
11
22
  import { defineMiddleware } from "astro:middleware";
12
23
 
13
24
  import { signInUrl } from "./access.js";
25
+ import { readCart } from "./cart.js";
14
26
  import { keysFromPath, pathAccess, refusal } from "./files.js";
15
27
  import { siteConfig } from "./site-config.js";
16
28
 
@@ -45,6 +57,20 @@ async function withUser(context: { locals: unknown; session?: { get(k: string):
45
57
  return { ...context, locals: { ...locals, user: { id: user.id, role: user.role, disabled: user.disabled } } };
46
58
  }
47
59
 
60
+ /** Keep one visitor's response out of every shared cache, the route cache included. */
61
+ export function keepPrivate(context: { cache?: { set(options: false): void } }, res: Response): Response {
62
+ context.cache?.set(false);
63
+ try {
64
+ res.headers.set("cache-control", "private, no-store");
65
+ return res;
66
+ } catch {
67
+ // Immutable headers (a redirect made with Response.redirect).
68
+ const out = new Response(res.body, res);
69
+ out.headers.set("cache-control", "private, no-store");
70
+ return out;
71
+ }
72
+ }
73
+
48
74
  export const onRequest = defineMiddleware(async (context, next) => {
49
75
  const { locals, url } = context;
50
76
  const user = (locals as { user?: { role?: number } | null }).user;
@@ -72,6 +98,11 @@ export const onRequest = defineMiddleware(async (context, next) => {
72
98
  }
73
99
  }
74
100
 
101
+ if (!url.pathname.startsWith("/_emdash")) {
102
+ const own = Boolean(user) || Boolean(await readCart(context.session as never).catch(() => null));
103
+ return own ? keepPrivate(context, await next()) : next();
104
+ }
105
+
75
106
  if (!user || user.role !== SUBSCRIBER) return next();
76
107
 
77
108
  if (memberMayUse(url.pathname, config?.memberPaths)) return next();
package/src/plugin.ts CHANGED
@@ -279,7 +279,7 @@ function agentTools(config: RedeemIDConfig) {
279
279
  };
280
280
  if (config.agents?.actions) {
281
281
  tools.resync_member = { route: "resync-member", input: CUSTOMER, description: "Read one member from Shopify now and update the site. Changes nothing in Shopify." };
282
- tools.resync_batch = { route: "resync-batch", input: EMPTY, description: "Read the 50 members synced longest ago from Shopify. Run again for the next 50." };
282
+ tools.resync_batch = { route: "resync-batch", input: EMPTY, description: `Read the ${BULK_BATCH} members synced longest ago from Shopify. Run again for the next ${BULK_BATCH}.` };
283
283
  tools.edit_member_tags = { route: "edit-member-tags", input: TAGS, destructive: true, description: "Add or remove tags on a member's Shopify customer, then read back what Shopify holds. Access follows from Shopify." };
284
284
  }
285
285
  return { tools };
package/src/staff.ts CHANGED
@@ -13,7 +13,12 @@ import { recentRequests } from "./privacy.js";
13
13
  import { cleanTags, numericId, type MemberRecord, type Stores } from "./storage.js";
14
14
 
15
15
  /** Members resynced per click; well inside Shopify's query cost limits. */
16
- export const BULK_BATCH = 50;
16
+ /**
17
+ * Members per bulk resync click. Each one costs a database write, and on the
18
+ * Workers Free plan one request may make only 50 database queries (EmDash's
19
+ * own session and settings reads included), so a batch has to leave room.
20
+ */
21
+ export const BULK_BATCH = 25;
17
22
 
18
23
  type Blocks = { blocks: unknown[]; toast?: { message: string; type: "success" | "error" | "info" } };
19
24
 
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.0.12";
1
+ export const VERSION = "0.0.14";