@redeemid/emdash 0.0.11 → 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 +14 -4
- package/bin/redeemid-emdash.mjs +1 -1
- package/package.json +1 -1
- package/src/admin-api.ts +4 -1
- package/src/agents.ts +2 -1
- package/src/components/Content.astro +23 -2
- package/src/config.ts +19 -0
- package/src/guard.ts +32 -1
- package/src/plugin.ts +3 -2
- package/src/staff.ts +6 -1
- package/src/version.ts +1 -1
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.
|
|
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
|
|
|
@@ -89,6 +89,8 @@ Set these secrets in the environment (`.env` locally, `wrangler secret put` on W
|
|
|
89
89
|
| `REDEEMID_SHOPIFY_STOREFRONT_PRIVATE_TOKEN` | Storefront API | Private access token, for the plan cards and checkout (server-side). |
|
|
90
90
|
| `REDEEMID_SHOPIFY_STOREFRONT_PUBLIC_TOKEN` | Storefront API | Public access token. Only used when the private token isn't set. |
|
|
91
91
|
|
|
92
|
+
Set them as runtime variables or secrets of the site itself, not as build-time variables. If a value shows as Missing on the Status page, check its name: the Status page also lists any `REDEEMID_` variable that is set but that RedeemID doesn't read, by name only.
|
|
93
|
+
|
|
92
94
|
RedeemID finds the authorization, token and logout endpoints itself from the store's discovery documents, so they need no setting.
|
|
93
95
|
|
|
94
96
|
In Shopify, give the Customer Account API client these settings:
|
|
@@ -239,7 +241,7 @@ With `agents: { actions: true }` in the config, three actions are offered too:
|
|
|
239
241
|
| MCP tool | Command | What it does |
|
|
240
242
|
| --- | --- | --- |
|
|
241
243
|
| `resync_member` | `resync <customer id>` | Reads one member from Shopify and updates the site. Changes nothing in Shopify. |
|
|
242
|
-
| `resync_batch` | `resync-batch` | Reads the
|
|
244
|
+
| `resync_batch` | `resync-batch` | Reads the 25 members synced longest ago. |
|
|
243
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. |
|
|
244
246
|
|
|
245
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`.
|
|
@@ -293,7 +295,7 @@ No event carries an email address or a name: match people by the Shopify `custom
|
|
|
293
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:
|
|
294
296
|
|
|
295
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.
|
|
296
|
-
- **Bulk resync.** Reads the
|
|
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.
|
|
297
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).
|
|
298
300
|
- **Recent deliveries.** The last webhooks the site took, with their topics and times.
|
|
299
301
|
- **Access test.** Enter a collection, terms and a reader's tags to see what the entry needs and whether that reader gets in.
|
|
@@ -308,13 +310,15 @@ EmDash has no content filter hook, so templates decide what to render. Render th
|
|
|
308
310
|
---
|
|
309
311
|
import Content from "@redeemid/emdash/components/Content.astro";
|
|
310
312
|
---
|
|
311
|
-
<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}>
|
|
312
314
|
<a slot="locked" href="/join/">Become a member</a>
|
|
313
315
|
</Content>
|
|
314
316
|
```
|
|
315
317
|
|
|
316
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.
|
|
317
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
|
+
|
|
318
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.
|
|
319
323
|
|
|
320
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.
|
|
@@ -358,6 +362,12 @@ import Plan from "@redeemid/emdash/components/Plan.astro";
|
|
|
358
362
|
</Plan>
|
|
359
363
|
```
|
|
360
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
|
+
|
|
361
371
|
### Shop
|
|
362
372
|
|
|
363
373
|
Products, collections and the cart come from the Storefront API; Shopify works out prices, discounts, taxes and stock.
|
package/bin/redeemid-emdash.mjs
CHANGED
|
@@ -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
|
|
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
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 =
|
|
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;
|
package/src/agents.ts
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
import { adminConfigured } from "./admin-api.js";
|
|
13
13
|
import { describe as describeRequirement, dripDays, levelsFor, membership, passes, requirement, termTree, type Requirement, type Terms } from "./access.js";
|
|
14
|
-
import { ENV, env, storeDomain, type RedeemIDConfig } from "./config.js";
|
|
14
|
+
import { ENV, env, storeDomain, unknownEnvNames, type RedeemIDConfig } from "./config.js";
|
|
15
15
|
import { featuresFor } from "./features.js";
|
|
16
16
|
import { bodyClauses } from "./files.js";
|
|
17
17
|
import { holdsItem, soldItem, type SoldItem } from "./blocks-names.js";
|
|
@@ -155,6 +155,7 @@ export async function siteHealth(config: RedeemIDConfig, s: Stores): Promise<Rec
|
|
|
155
155
|
lastWebhook: items[0]?.data ?? null,
|
|
156
156
|
openPrivacyRequests: open.items.length,
|
|
157
157
|
ignoredTextReplacements: textProblems(config).length,
|
|
158
|
+
unusedSettings: unknownEnvNames(),
|
|
158
159
|
};
|
|
159
160
|
}
|
|
160
161
|
|
|
@@ -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
|
|
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).
|
|
@@ -192,6 +198,19 @@ export function env(name: string): string {
|
|
|
192
198
|
return typeof value === "string" ? value.trim() : "";
|
|
193
199
|
}
|
|
194
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Names of REDEEMID_ variables that are set but that RedeemID doesn't read,
|
|
203
|
+
* such as a misspelled secret or a WordPress plugin constant. Names only,
|
|
204
|
+
* never values.
|
|
205
|
+
*/
|
|
206
|
+
export function unknownEnvNames(): string[] {
|
|
207
|
+
if (typeof process === "undefined" || !process.env) return [];
|
|
208
|
+
const known = new Set<string>(Object.values(ENV));
|
|
209
|
+
return Object.keys(process.env)
|
|
210
|
+
.filter((k) => k.startsWith("REDEEMID_") && !known.has(k))
|
|
211
|
+
.sort();
|
|
212
|
+
}
|
|
213
|
+
|
|
195
214
|
/** The store origin, e.g. https://shop.example.com. */
|
|
196
215
|
export function shopOrigin(config: RedeemIDConfig): string {
|
|
197
216
|
const raw = storeDomain(config).replace(/\/+$/, "");
|
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
|
@@ -28,7 +28,7 @@ import type { ResolvedPlugin, RouteContext } from "emdash";
|
|
|
28
28
|
import { definePlugin } from "emdash";
|
|
29
29
|
|
|
30
30
|
import { describe, levelsFor, SIGNED_IN } from "./access.js";
|
|
31
|
-
import { env, ENV, storeDomain, type RedeemIDConfig } from "./config.js";
|
|
31
|
+
import { env, ENV, storeDomain, unknownEnvNames, type RedeemIDConfig } from "./config.js";
|
|
32
32
|
import { discover } from "./shopify.js";
|
|
33
33
|
import { stores, type MemberRecord, type Stores } from "./storage.js";
|
|
34
34
|
import { PAGES, WIDGETS } from "./descriptor.js";
|
|
@@ -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:
|
|
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 };
|
|
@@ -544,6 +544,7 @@ async function statusPage(config: RedeemIDConfig, request: Request) {
|
|
|
544
544
|
{ label: "Storefront API access", value: c.storefront },
|
|
545
545
|
{ label: "Last webhook", value: delivery ?? "None received yet" },
|
|
546
546
|
{ label: "Replacement text", value: textSummary(config) },
|
|
547
|
+
...(unknownEnvNames().length ? [{ label: "Set but not used (check the name)", value: unknownEnvNames().join(", ") }] : []),
|
|
547
548
|
],
|
|
548
549
|
},
|
|
549
550
|
...(textProblems(config).length
|
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
|
-
|
|
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.
|
|
1
|
+
export const VERSION = "0.0.14";
|