@bright-logic-digital/site-editor 0.0.0-stage → 1.0.0-rc.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/CHANGELOG.md +30 -0
- package/LICENSE +4 -0
- package/README.md +318 -2
- package/bin/cli.js +92 -0
- package/bin/copy-editor.js +211 -0
- package/bin/node-version.js +20 -0
- package/bin/site-config.js +46 -0
- package/bin/site-editor.js +16 -0
- package/editor/admin.css +438 -0
- package/editor/api.js +138 -0
- package/editor/app.js +1178 -0
- package/editor/asset-map.js +38 -0
- package/editor/boot.js +72 -0
- package/editor/copy.js +239 -0
- package/editor/drafts.js +67 -0
- package/editor/format.js +80 -0
- package/editor/forms.js +918 -0
- package/editor/images.js +212 -0
- package/editor/index.html +27 -0
- package/editor/model.js +152 -0
- package/editor/pages.js +297 -0
- package/editor/photos.js +232 -0
- package/editor/publish.js +45 -0
- package/editor/shared/rules.js +118 -0
- package/editor/shared/site.js +128 -0
- package/editor/shared/validate.js +303 -0
- package/editor/signin.js +154 -0
- package/editor/status.js +90 -0
- package/editor/uploads.js +91 -0
- package/guide/check.js +233 -0
- package/hosts/next.js +19 -0
- package/hosts/pages.js +20 -0
- package/hosts/shared.js +42 -0
- package/package.json +91 -4
- package/server/blob-sha.js +10 -0
- package/server/changes.js +24 -0
- package/server/check-content.js +197 -0
- package/server/config.js +97 -0
- package/server/dispatch.js +207 -0
- package/server/github.js +149 -0
- package/server/handlers.js +364 -0
- package/server/index.js +153 -0
- package/server/local-repo.js +171 -0
- package/server/mapped-repo.js +48 -0
- package/server/memory-repo.js +104 -0
- package/server/path-map.js +26 -0
- package/server/signin/access.js +329 -0
- package/server/signin/code-store.sql +60 -0
- package/server/signin/code.js +192 -0
- package/server/signin/codes.js +171 -0
- package/server/signin/refusal.js +76 -0
- package/server/signin/session.js +90 -0
- package/tests/fixtures/image-files.json +14 -0
- package/tests/fixtures/slugs.json +202 -0
- package/tests/fixtures/validation.json +12734 -0
- package/types/bin/cli.d.ts +1 -0
- package/types/bin/copy-editor.d.ts +32 -0
- package/types/bin/node-version.d.ts +8 -0
- package/types/bin/site-config.d.ts +17 -0
- package/types/bin/site-editor.d.ts +2 -0
- package/types/editor/api.d.ts +37 -0
- package/types/editor/copy.d.ts +153 -0
- package/types/editor/shared/rules.d.ts +33 -0
- package/types/editor/shared/site.d.ts +35 -0
- package/types/editor/shared/validate.d.ts +19 -0
- package/types/guide/check.d.ts +31 -0
- package/types/hosts/next.d.ts +12 -0
- package/types/hosts/pages.d.ts +9 -0
- package/types/hosts/shared.d.ts +21 -0
- package/types/server/blob-sha.d.ts +1 -0
- package/types/server/changes.d.ts +2 -0
- package/types/server/check-content.d.ts +16 -0
- package/types/server/config.d.ts +46 -0
- package/types/server/dispatch.d.ts +51 -0
- package/types/server/github.d.ts +27 -0
- package/types/server/handlers.d.ts +116 -0
- package/types/server/index.d.ts +75 -0
- package/types/server/local-repo.d.ts +16 -0
- package/types/server/mapped-repo.d.ts +10 -0
- package/types/server/memory-repo.d.ts +28 -0
- package/types/server/path-map.d.ts +3 -0
- package/types/server/signin/access.d.ts +68 -0
- package/types/server/signin/code.d.ts +44 -0
- package/types/server/signin/codes.d.ts +98 -0
- package/types/server/signin/refusal.d.ts +122 -0
- package/types/server/signin/session.d.ts +29 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 1.0.0-rc.0
|
|
4
|
+
|
|
5
|
+
The first public version: a website editor for small-business owners. The
|
|
6
|
+
owner signs in at `/admin/`, changes the words and photos of the site's
|
|
7
|
+
pages, and publishes; each publish is one commit to the site's GitHub
|
|
8
|
+
repository. The package holds:
|
|
9
|
+
|
|
10
|
+
- the editor page, plain JavaScript served as it is;
|
|
11
|
+
- its API, as a Next.js route handler or a Cloudflare Pages Function, with
|
|
12
|
+
two kinds of sign-in: an emailed code, or a sign-in in front of the site
|
|
13
|
+
such as Cloudflare Access;
|
|
14
|
+
- the `site-editor` command, which copies the editor into a site
|
|
15
|
+
(`copy`) and checks the site's content before a build (`check-content`);
|
|
16
|
+
- a check of the site's guide for the owner against the editor's words.
|
|
17
|
+
|
|
18
|
+
One config file per site holds what differs between sites, including the
|
|
19
|
+
editor's title, its wording and who the owner is told to contact. See the
|
|
20
|
+
README for what a site adds and what it must provide.
|
|
21
|
+
|
|
22
|
+
Compared with the copies of the editor two sites carried before this
|
|
23
|
+
package:
|
|
24
|
+
|
|
25
|
+
- Checking an emailed code is rate limited, like asking for one:
|
|
26
|
+
`allowRequest` is asked before a guess is counted, and a refused check
|
|
27
|
+
answers the same 429.
|
|
28
|
+
- Local editing answers only requests that name this computer in their
|
|
29
|
+
`Host` header as well as their address.
|
|
30
|
+
- The editor's home screen ends with its version.
|
package/LICENSE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,319 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bright-logic-digital/site-editor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A website editor for small-business owners: the owner signs in at `/admin/`, changes the words and photos of the site's pages, and publishes. Each publish is one commit to the site's GitHub repository, which the site's host then builds and deploys as usual.
|
|
4
|
+
|
|
5
|
+
The package holds the editor page (plain JavaScript, served as it is), its API (a route handler for Next.js or a Cloudflare Pages Function), and the `site-editor` command that puts the editor in a site and checks the site's content. It needs Node 20 or later and has no runtime dependencies.
|
|
6
|
+
|
|
7
|
+
## Adding the editor to a site
|
|
8
|
+
|
|
9
|
+
A site adds four things: its config file, one route file, two commands in its build, and its settings. It must also provide what [What your site must provide](#what-your-site-must-provide) lists.
|
|
10
|
+
|
|
11
|
+
### 1. The config file
|
|
12
|
+
|
|
13
|
+
One file holds everything about the site the editor needs. It is an ES module whose default export is the config: name it `site-editor.config.mjs`, or `site-editor.config.js` in a package with `"type": "module"`.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
// site-editor.config.mjs
|
|
17
|
+
export default {
|
|
18
|
+
title: "Example website editor",
|
|
19
|
+
signIn: "code",
|
|
20
|
+
reserved: ["shop"],
|
|
21
|
+
developer: "Sam",
|
|
22
|
+
paths: {
|
|
23
|
+
repo: [["content/", "content/"], ["assets/", "public/assets/"]],
|
|
24
|
+
local: [["content/", "content/"], ["assets/", "public/assets/"]],
|
|
25
|
+
},
|
|
26
|
+
commit: { identity: null, userAgent: "example-site-editor" },
|
|
27
|
+
cookie: "__Host-example_admin",
|
|
28
|
+
liveBranchGuard: "vercel",
|
|
29
|
+
maxPublishFiles: 250,
|
|
30
|
+
guide: "editor-guide.html",
|
|
31
|
+
};
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Every key is described under [The config file's keys](#the-config-files-keys). A wrong key stops `site-editor copy` and the API, and they name it.
|
|
35
|
+
|
|
36
|
+
### 2. One route file
|
|
37
|
+
|
|
38
|
+
The API answers every request under `/api/admin/`.
|
|
39
|
+
|
|
40
|
+
**Next.js** (App Router), in `app/api/admin/[...path]/route.js`:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
import { createNextRoute } from "@bright-logic-digital/site-editor/hosts/next";
|
|
44
|
+
import { codeSignIn, createSupabaseCodeStore } from "@bright-logic-digital/site-editor/server/signin/code";
|
|
45
|
+
import config from "../../../../site-editor.config.mjs";
|
|
46
|
+
|
|
47
|
+
export const runtime = "nodejs";
|
|
48
|
+
export const dynamic = "force-dynamic";
|
|
49
|
+
|
|
50
|
+
export const { GET, HEAD, POST, PUT, PATCH, DELETE } = createNextRoute(() => ({
|
|
51
|
+
config,
|
|
52
|
+
signIn: codeSignIn({
|
|
53
|
+
codes: createSupabaseCodeStore(supabase), // the site's Supabase client, with the service-role key
|
|
54
|
+
sendCode: (email, code) => emailTheCode(email, code), // the site's own mailer
|
|
55
|
+
allowRequest: (request) => rateLimit(request), // the site's own rate limit
|
|
56
|
+
}),
|
|
57
|
+
}));
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Next.js does not serve `public/admin/index.html` at `/admin` by itself; add a rewrite to `next.config`: `{ source: "/admin", destination: "/admin/index.html" }`.
|
|
61
|
+
|
|
62
|
+
**Cloudflare Pages**, in `functions/api/admin/[[path]].js`:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import { createPagesFunction } from "@bright-logic-digital/site-editor/hosts/pages";
|
|
66
|
+
import { accessSignIn } from "@bright-logic-digital/site-editor/server/signin/access";
|
|
67
|
+
import config from "../../../site-editor.config.mjs";
|
|
68
|
+
|
|
69
|
+
export const onRequest = createPagesFunction({ config, signIn: accessSignIn() });
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The sign-in given must be the kind the config's `signIn` names. Either host takes the options, or a function that gives them, so a site can build its code store when the first request arrives.
|
|
73
|
+
|
|
74
|
+
### 3. The command, before every build and every dev start
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
site-editor copy --config site-editor.config.mjs --out public/admin
|
|
78
|
+
site-editor check-content --config site-editor.config.mjs
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
`copy` puts the editor in the folder the site serves at `/admin/` (the editor's page loads its files from `/admin/`). It also writes `site-config.json` there (the config's browser keys and the package's version) and copies the site's guide there as `guide.html`. The copied `index.html` carries the config's `title` in its `<title>` from the first paint (the script keeps setting it as the owner moves between screens). It empties that folder first, so add the folder to `.gitignore`. So that a mistyped `--out` cannot wipe a project, it refuses a folder that holds files unless its `site-config.json` is one an earlier copy wrote (a JSON object with a `version`). It keeps that file in the folder throughout, so a copy that stops part way can simply be run again.
|
|
82
|
+
|
|
83
|
+
`check-content` checks that every page's content fits the schema, that every photo it names is on disk, and that every extra page's address is free. It prints the problems and exits 1 when there are any. It finds content and photos through `paths.local`, under the config file's folder, or under the folder given with `--app <folder>`.
|
|
84
|
+
|
|
85
|
+
In `package.json`, run the command inside the `dev` and `build` scripts themselves:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
"scripts": {
|
|
89
|
+
"dev": "site-editor copy --config site-editor.config.mjs --out public/admin && next dev",
|
|
90
|
+
"build": "site-editor copy --config site-editor.config.mjs --out public/admin && site-editor check-content --config site-editor.config.mjs && next build"
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Not in a `predev` or `prebuild` script: some package managers do not run `pre*` scripts, and the editor would then be missing or out of date.
|
|
95
|
+
|
|
96
|
+
`site-editor version` prints the package's version. The editor's home screen ends with it too.
|
|
97
|
+
|
|
98
|
+
### 4. The settings
|
|
99
|
+
|
|
100
|
+
The API reads these settings (environment variables) on every request. It reports a missing or malformed one by its name, never its value. Two are secrets: `GITHUB_TOKEN` and `ADMIN_SESSION_SECRET`. Keep them in the host's encrypted settings, never in the repository.
|
|
101
|
+
|
|
102
|
+
| Setting | Needed by | Form |
|
|
103
|
+
| --- | --- | --- |
|
|
104
|
+
| `GITHUB_TOKEN` | both sign-ins | SECRET. A fine-grained GitHub token for the site's one repository, with the least it needs: Contents, read and write (GitHub adds read-only Metadata itself). Nothing else. |
|
|
105
|
+
| `GITHUB_REPO` | both sign-ins | The repository as `owner/name`, such as `example/website`. |
|
|
106
|
+
| `GITHUB_BRANCH` | both sign-ins | The branch the editor commits to. With the emailed code, letters, digits, `.`, `_` and `-` only; with `liveBranchGuard: "vercel"`, it may be `main` only when `VERCEL_ENV` is `production`. |
|
|
107
|
+
| `ADMIN_EMAILS` | both sign-ins | The addresses that may edit, separated by commas, such as `owner@example.com, helper@example.com`. With the emailed code, every entry must be an address. Compared without regard to case. |
|
|
108
|
+
| `ADMIN_SESSION_SECRET` | `signIn: "code"` | SECRET. At least 32 characters; signs the sign-in cookie and the codes' hashes. |
|
|
109
|
+
| `VERCEL_ENV` | `signIn: "code"` with `liveBranchGuard: "vercel"` | Set by Vercel itself. |
|
|
110
|
+
| `ACCESS_TEAM_DOMAIN` | `signIn: "access"` | The Cloudflare Access team domain, `<team>.cloudflareaccess.com`. |
|
|
111
|
+
| `ACCESS_AUD` | `signIn: "access"` | The Access application's audience (AUD) tag. |
|
|
112
|
+
| `ADMIN_LOCAL` | local editing | `1` turns local editing on, where allowed (below). Never set it on a deployed host. |
|
|
113
|
+
| `NODE_ENV`, `VERCEL` | local editing | Read only to keep local editing off: see below. |
|
|
114
|
+
|
|
115
|
+
### Local editing
|
|
116
|
+
|
|
117
|
+
With `ADMIN_LOCAL` set to `1`, the API edits the working tree on the developer's computer instead of GitHub, with no sign-in. Give the API `local: { user, repo }`, where `repo` is `createLocalRepo(appFolder, { rebuild, paths: config.paths.local })` from `@bright-logic-digital/site-editor/server/local-repo`. `paths` is required: `createLocalRepo` throws, naming `paths`, when it is missing or not of the form `paths.local` takes.
|
|
118
|
+
|
|
119
|
+
`isLocalMode(env)` decides, and is true exactly when `ADMIN_LOCAL` is `1`, `NODE_ENV` is not `production` (a missing `NODE_ENV` counts as not production) and `VERCEL` is not set. So a host that sets neither `NODE_ENV=production` nor `VERCEL` would honour `ADMIN_LOCAL`: `ADMIN_LOCAL` must never be set on a deployed host.
|
|
120
|
+
|
|
121
|
+
In local mode the API answers only a request whose address and `Host` header (when it has one) both name this computer: `localhost`, `127.0.0.1` or `[::1]`, with any port. Anything else gets "The local editor only answers on this computer." The `Host` check stops another machine's browser and DNS rebinding. It does not stop a script on the same network that forges the header, so use local mode only with the dev server listening on this computer alone. For Next.js: `ADMIN_LOCAL=1 next dev -H 127.0.0.1` (plain `next dev` listens on every network interface).
|
|
122
|
+
|
|
123
|
+
### The owner's guide
|
|
124
|
+
|
|
125
|
+
Each site writes its own guide for the owner (the file `guide` names). Check it against the editor's words in the site's tests:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
import { guideProblems } from "@bright-logic-digital/site-editor/guide/check";
|
|
129
|
+
|
|
130
|
+
assert.deepEqual(guideProblems(guideHtml, config, schema), []);
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`schema` is the site's `content/schema.json`. Each problem is one sentence saying what to change.
|
|
134
|
+
|
|
135
|
+
## What your site must provide
|
|
136
|
+
|
|
137
|
+
### Headers for the editor's pages
|
|
138
|
+
|
|
139
|
+
Send these three headers on every response that serves an editor page or file: `/admin/` and every address under it. Where `/admin` is only a redirect to `/admin/` (Cloudflare Pages does this), the redirect itself needs none; where the site serves the editor's page at `/admin` itself (the Next.js rewrite below), that response needs them too.
|
|
140
|
+
|
|
141
|
+
- `Content-Security-Policy: default-src 'self'; img-src 'self' blob: data:; frame-ancestors 'none'`: no other site may show the editor in a frame (a frame can trick the owner into clicking), and the page loads nothing from anywhere else.
|
|
142
|
+
- `Cache-Control: no-store`: after a package update, a browser never runs a mix of old and new editor files.
|
|
143
|
+
- `X-Robots-Tag: noindex`: search engines leave the editor out of their results.
|
|
144
|
+
|
|
145
|
+
In Next.js, with `headers()` in `next.config`:
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
const editorHeaders = [
|
|
149
|
+
{ key: "Content-Security-Policy", value: "default-src 'self'; img-src 'self' blob: data:; frame-ancestors 'none'" },
|
|
150
|
+
{ key: "Cache-Control", value: "no-store" },
|
|
151
|
+
{ key: "X-Robots-Tag", value: "noindex" },
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
export default {
|
|
155
|
+
async headers() {
|
|
156
|
+
return [
|
|
157
|
+
{ source: "/admin", headers: editorHeaders },
|
|
158
|
+
{ source: "/admin/:path*", headers: editorHeaders },
|
|
159
|
+
{ source: "/version.json", headers: [{ key: "Cache-Control", value: "no-store" }] },
|
|
160
|
+
];
|
|
161
|
+
},
|
|
162
|
+
async rewrites() {
|
|
163
|
+
return [{ source: "/admin", destination: "/admin/index.html" }];
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
On Cloudflare Pages, in the `_headers` file the site publishes:
|
|
169
|
+
|
|
170
|
+
```
|
|
171
|
+
/admin
|
|
172
|
+
Content-Security-Policy: default-src 'self'; img-src 'self' blob: data:; frame-ancestors 'none'
|
|
173
|
+
Cache-Control: no-store
|
|
174
|
+
X-Robots-Tag: noindex
|
|
175
|
+
/admin/*
|
|
176
|
+
Content-Security-Policy: default-src 'self'; img-src 'self' blob: data:; frame-ancestors 'none'
|
|
177
|
+
Cache-Control: no-store
|
|
178
|
+
X-Robots-Tag: noindex
|
|
179
|
+
/version.json
|
|
180
|
+
Cache-Control: no-store
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### `/version.json`: which commit is live
|
|
184
|
+
|
|
185
|
+
The site answers `/version.json` with the commit its live deployment was built from. The answer must never be stale after a deployment. `Cache-Control: no-store` guarantees that, and so does a host that replaces the file atomically with each deployment (a static route on Vercel does); a long-lived cache in front of the site does not. The editor asks for it with `cache: "no-store"` in any case.
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
{ "commit": "<the deployed commit's sha: 40 lower-case hex characters>" }
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
After a publish, the editor reads it every 5 seconds until the deployed commit is the one just published, or one after it (it asks the API whether the deployed commit contains the published one), and then tells the owner the change is live. It gives up after `liveWaitMs`. Without this file, or with any other shape, every publish ends in the "saved but not showing on the site yet" message (the `timeout` sentence), although the change was saved. Have the build write it, for example from `VERCEL_GIT_COMMIT_SHA` on Vercel or `CF_PAGES_COMMIT_SHA` on Cloudflare Pages.
|
|
192
|
+
|
|
193
|
+
### Photos: what `photos` assumes
|
|
194
|
+
|
|
195
|
+
The editor shows each photo at its 700-pixel width, the file `<base>-700.<ext>`, where `<base>` is the stored name, such as `/assets/img/hero` or `/assets/img/uploads/oak-k3x9qa`, and `<ext>` is `webp` or `jpg`. New photos are saved under the editor's `assets/` folder (`paths.repo`) as `assets/img/uploads/<name>-<width>.<ext>`, at up to three widths (700, 1100 and 1800 pixels).
|
|
196
|
+
|
|
197
|
+
- `photos: "key"` (the default) assumes the site serves the editor's `assets/` folder at `/assets/`, file names unchanged: `assets/img/uploads/oak-k3x9qa-700.webp` at `/assets/img/uploads/oak-k3x9qa-700.webp`. The editor adds a query (`?v=…`) that the site must ignore.
|
|
198
|
+
- `photos: "asset-map"` is for a site whose build renames each file with a hash of its content. The site serves a map of names at `/admin/asset-map.json`: a JSON object from each 700-wide file's stored address to its served address, which is the same with a 10-character lower-case hex hash before the extension:
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"/assets/img/hero-700.webp": "/assets/img/hero-700.0123456789.webp",
|
|
203
|
+
"/assets/img/uploads/oak-k3x9qa-700.webp": "/assets/img/uploads/oak-k3x9qa-700.abcdef0123.webp"
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
An entry of any other form is not used. `site-editor copy` empties `/admin/`'s folder, so write the map after it. When the map cannot be read within 10 seconds, the editor shows the photos without a preview.
|
|
208
|
+
|
|
209
|
+
### The code store, for emailed-code sign-in
|
|
210
|
+
|
|
211
|
+
The emailed code needs a store for codes that outlives one request. `createSupabaseCodeStore(client)` uses a Supabase (Postgres) database through three SQL functions, which the package ships in `server/signin/code-store.sql` (in the installed package: `node_modules/@bright-logic-digital/site-editor/server/signin/code-store.sql`). Run it once in the database, as a migration.
|
|
212
|
+
|
|
213
|
+
It makes one table, `admin_login_codes`, which holds only each code's keyed hash, never a code, with row-level security on and no policies, and three functions:
|
|
214
|
+
|
|
215
|
+
- `admin_login_code_issue(p_email, p_code_hash)`: stores a new code for the address, valid for 10 minutes, and retires the address's earlier codes. It refuses (returns false) once the address has had 5 codes in the last hour or 10 in the last day. It also deletes rows older than two days.
|
|
216
|
+
- `admin_login_code_attempt(p_email)`: counts one guess at the address's newest live code and returns its hash, or nothing once 5 guesses have been made or there is no live code.
|
|
217
|
+
- `admin_login_code_consume(p_email, p_code_hash)`: uses up the live code with that hash; true only for the call that used it.
|
|
218
|
+
|
|
219
|
+
These limits (codes 8 digits long, valid 10 minutes, 5 guesses each, 5 codes an hour and 10 a day per address) are the constants in `server/signin/codes.js`. Only the service role may execute the functions: the SQL revokes them from `public`, `anon` and `authenticated` and grants them to `service_role`, so the site's client must use the service-role key, on the server only.
|
|
220
|
+
|
|
221
|
+
A site may use another store: any object meeting the `CodeStore` contract (in `server/signin/codes.js`):
|
|
222
|
+
|
|
223
|
+
- `issue(email, codeHash)` returns a promise of `true` when the code is stored, `false` when an hourly or daily cap is reached;
|
|
224
|
+
- `attempt(email)` counts one guess and returns a promise of the hash to compare, or `null`;
|
|
225
|
+
- `consume(email, codeHash)` returns a promise of `true` only when this call used that live code.
|
|
226
|
+
|
|
227
|
+
`createMemoryCodeStore(now)` keeps codes in memory, for tests only: on a serverless host each instance would have its own store, so the caps and the guess counts would start again on every instance.
|
|
228
|
+
|
|
229
|
+
### A private repository
|
|
230
|
+
|
|
231
|
+
Each publish is a commit whose subject is `Edit site content: <names> (by <email>)`, so the site's repository holds the editors' addresses: keep it private.
|
|
232
|
+
|
|
233
|
+
## Sign-in upkeep
|
|
234
|
+
|
|
235
|
+
For the emailed code (`signIn: "code"`):
|
|
236
|
+
|
|
237
|
+
- `allowRequest(request)` is given each request for a code (`session/request`) and each check of a code (`session/verify`), as the standard `Request`, before any code is made or any guess is counted. It returns a promise of `true` to go on, or `false` to answer 429 "Too many tries. Wait a few minutes, then try again." The site reads the caller's address from the request itself.
|
|
238
|
+
- To remove one editor, take their address off `ADMIN_EMAILS`; their sign-in is refused from then on, even before it expires. To sign everyone out, change `ADMIN_SESSION_SECRET`. Either takes effect only on deployments made after the change.
|
|
239
|
+
- A sign-in lasts 30 days. Signing out clears the cookie in that browser; it does not cancel a copy of the cookie taken elsewhere, which works until it expires, the address is removed or the secret changes.
|
|
240
|
+
- Start the cookie's name (the config's `cookie`) with `__Host-`, so the browser keeps it to this site, sent only over HTTPS.
|
|
241
|
+
- When `sendCode` throws, its error's message is logged. It must never contain the code.
|
|
242
|
+
|
|
243
|
+
## Where it runs
|
|
244
|
+
|
|
245
|
+
- **Supported hosts**: Vercel (Next.js) and Cloudflare Pages. On a self-hosted Next.js server (`next start`), every POST fails the origin check ("This request did not come from the editor."), so nobody can sign in or publish: Next builds the request's address from the address the server listens on, not from the request, so it never matches the browser's `Origin` header.
|
|
246
|
+
- **One site per process**: `createEditorApi` configures process-wide state (the site config and the editor's sentences), so one process serves one site.
|
|
247
|
+
- **Request bodies**: the API checks a body's `Content-Length` first and the length of what it read second. A request with no `Content-Length` is read whole before the second check, so the host's own request-size limit is the outer bound.
|
|
248
|
+
|
|
249
|
+
## The config file's keys
|
|
250
|
+
|
|
251
|
+
Browser keys, which `copy` writes into `site-config.json` for the editor page:
|
|
252
|
+
|
|
253
|
+
| Key | Meaning | Allowed values (default) |
|
|
254
|
+
| --- | --- | --- |
|
|
255
|
+
| `title` | The browser tab's title and the name at the top of the editor. | Words (`"Website editor"`) |
|
|
256
|
+
| `signIn` | How the owner signs in. | `"code"`: an emailed code. `"access"`: a sign-in in front of the site, such as Cloudflare Access. (`"code"`) |
|
|
257
|
+
| `trailingSlash` | Whether the site serves its pages at `/<slug>/` rather than `/<slug>`. | `true` or `false` (`false`) |
|
|
258
|
+
| `photos` | How the editor finds a published photo (see [Photos](#photos-what-photos-assumes)). | `"key"`: by its stored name. `"asset-map"`: through the build's map of hashed names. (`"key"`) |
|
|
259
|
+
| `reserved` | Addresses the site uses itself, which no extra page may take, beyond the five every site keeps (`admin`, `api`, `assets`, `index`, `404`). | A list of lower-case words and digits joined by `-` (`[]`) |
|
|
260
|
+
| `bareHost` | A host serving the site without its sign-in in front; there the editor only points to `editorAddress`. | A host name, such as `example.pages.dev`, or `null` (`null`) |
|
|
261
|
+
| `editorAddress` | The editor's own address, shown on `bareHost`. | A full `https://` address in normal form, with a path, such as `https://example.com/admin/`, or `null` (`null`) |
|
|
262
|
+
| `liveWaitMs` | How long the editor waits for a publish to show on the site. | Milliseconds, a whole number above 0 (`360000`) |
|
|
263
|
+
| `developer` | Who the owner is told to tell when something needs a person: seven of the editor's sentences end "let … know", such as "Please let Sam know." | Words, at most 60 characters, on one line (`"your web developer"`) |
|
|
264
|
+
| `text` | The editor's own sentences, reworded for this site. | An object from sentence names (the keys of `TEXT` in `editor/copy.js`) to words (`{}`) |
|
|
265
|
+
|
|
266
|
+
`bareHost` and `editorAddress` go together: set both or neither, and only with `signIn: "access"`.
|
|
267
|
+
|
|
268
|
+
A sentence reworded in `text` replaces the whole sentence, `developer` included.
|
|
269
|
+
|
|
270
|
+
`version` is not a key a site sets: `copy` writes the package's version into `site-config.json`, and a config file that sets it is refused.
|
|
271
|
+
|
|
272
|
+
Server keys, which only the API and the command read:
|
|
273
|
+
|
|
274
|
+
| Key | Meaning | Allowed values |
|
|
275
|
+
| --- | --- | --- |
|
|
276
|
+
| `paths` | Where the editor's own folders, `content/` and `assets/`, are. `repo`: in the GitHub repository. `local`: under the app's folder, for local editing and `check-content`. | `{ repo, local }`, each a list of two pairs: `[["content/", "<folder>/"], ["assets/", "<folder>/"]]`. Folders are relative, end in `/`, and neither is inside the other. |
|
|
277
|
+
| `commit` | Who the site's commits are by, and the User-Agent sent to GitHub. | `{ identity, userAgent }`. `identity`: `null` (GitHub records the token's owner) or `{ name, email }`. `userAgent`: printable words. |
|
|
278
|
+
| `cookie` | The sign-in cookie's name. | A cookie name, such as `__Host-example_admin`. Required with `signIn: "code"`; not allowed with `"access"`. |
|
|
279
|
+
| `liveBranchGuard` | Stops a preview deployment publishing to `main`. | `"vercel"`: refuses a `GITHUB_BRANCH` of `main` unless `VERCEL_ENV` is `production`. It knows only a branch literally named `main`; a live branch with another name is not guarded. `null`: no guard. Must be `null` with `signIn: "access"`. |
|
|
280
|
+
| `maxPublishFiles` | The most photo files one publish may add (each photo is up to three files, one per width). The pages in one publish are capped separately: at most 20 changed or added and 20 deleted. | A whole number above 0 |
|
|
281
|
+
| `guide` | The site's guide for the owner, which `copy` puts at `/admin/guide.html`. | A file path, relative to the config file's folder |
|
|
282
|
+
|
|
283
|
+
Every server key is required, except `cookie` as above.
|
|
284
|
+
|
|
285
|
+
## What a site can import
|
|
286
|
+
|
|
287
|
+
Only the names in this table are supported. Anything else a file happens to export is internal and may change without notice.
|
|
288
|
+
|
|
289
|
+
| Import | What it gives |
|
|
290
|
+
| --- | --- |
|
|
291
|
+
| `@bright-logic-digital/site-editor/server` | `createEditorApi`, `serverConfigProblems`, `isLocalMode`; the request limits `MAX_UPLOAD_BYTES`, `MAX_UPLOAD_CHARS` (a photo, as base64 text) and `MAX_PUBLISH_CHARS` (a publish body) |
|
|
292
|
+
| `.../server/signin/code` | `codeSignIn`, `createSupabaseCodeStore`, `createMemoryCodeStore` (for tests only; see [the code store](#the-code-store-for-emailed-code-sign-in)) |
|
|
293
|
+
| `.../server/signin/access` | `accessSignIn` |
|
|
294
|
+
| `.../server/local-repo` | `createLocalRepo(appFolder, { rebuild, paths })`: the working tree as a repository, for local editing |
|
|
295
|
+
| `.../server/memory-repo` | `createMemoryRepo`: a repository in memory, for tests |
|
|
296
|
+
| `.../server/check-content` | `checkContent(appFolder, config)`, what `site-editor check-content` runs |
|
|
297
|
+
| `.../hosts/next` | `createNextRoute` |
|
|
298
|
+
| `.../hosts/pages` | `createPagesFunction` |
|
|
299
|
+
| `.../shared/site` | `configureSite`, `browserConfig`, `siteProblems`: the site config the editor and the API share |
|
|
300
|
+
| `.../shared/rules` | `reservedNames`: the addresses a simple page may not take; `isUsableSlug`: whether an address can be a new simple page's; `uniqueSlug`: a free address made from a title; `SLUG_RE`: the form every address has; `badLinks`: the link attempts in a text that would not become a link |
|
|
301
|
+
| `.../shared/validate` | `validateDoc`: the problems with a document; `docTypeForPath`: the kind of document a content file's path holds; `applies`: whether a field exists, given its `when` and its sibling values; `emptyDoc`, `emptyValue`: a document or a field's value with everything present and empty; `imageFiles`: the repository paths of every file of an image |
|
|
302
|
+
| `.../guide/check` | `guideProblems` |
|
|
303
|
+
| `.../fixtures/validation.json`, `.../fixtures/slugs.json`, `.../fixtures/image-files.json` | Test cases, for a site that repeats the rules in another language |
|
|
304
|
+
|
|
305
|
+
Nothing else is importable.
|
|
306
|
+
|
|
307
|
+
## Working on the package
|
|
308
|
+
|
|
309
|
+
`npm test` runs the vitest files (`server/**/*.test.ts`), then the Node tests (`tests/**/*.test.js`). `npx tsc -p .` checks the typed files and writes the type declarations to `types/`; `npm pack` runs it first. CI runs both on Node 22 and 24; on Node 20 it only starts the command and runs the content check, since the test runner's glob needs Node 21 or later.
|
|
310
|
+
|
|
311
|
+
## Releasing
|
|
312
|
+
|
|
313
|
+
1. Set the new version in `package.json` and add a `## X.Y.Z` section to `CHANGELOG.md`.
|
|
314
|
+
2. Commit and merge to `main`.
|
|
315
|
+
3. Tag that commit `vX.Y.Z` and push the tag.
|
|
316
|
+
|
|
317
|
+
The release workflow checks the tag equals `v` plus the version in `package.json`, runs the tests, and publishes to npm (trusted publishing, no stored token). Any prerelease (a version with a `-`, such as `X.Y.Z-rc.N` or `X.Y.Z-beta.N`) goes out under the `next` tag, never `latest`. Do not publish by hand.
|
|
318
|
+
|
|
319
|
+
Copyright (c) 2026 Bright Logic Digital LLC. All rights reserved. See `LICENSE`.
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
// The site-editor command (loaded by site-editor.js once Node is known to
|
|
3
|
+
// be new enough). It reads the command line, calls the function
|
|
4
|
+
// that does the work, and prints the outcome: one line on success; on
|
|
5
|
+
// failure a sentence saying what to fix, and exit code 1.
|
|
6
|
+
//
|
|
7
|
+
// site-editor copy --config <file> --out <folder>
|
|
8
|
+
// site-editor check-content --config <file> [--app <folder>]
|
|
9
|
+
// site-editor version
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { parseArgs } from "node:util";
|
|
12
|
+
import { checkContent } from "../server/check-content.js";
|
|
13
|
+
import { copyEditor } from "./copy-editor.js";
|
|
14
|
+
import { CommandError, loadSiteConfig, packageVersion } from "./site-config.js";
|
|
15
|
+
|
|
16
|
+
const USAGE = [
|
|
17
|
+
"Usage:",
|
|
18
|
+
" site-editor copy --config <file> --out <folder>",
|
|
19
|
+
" site-editor check-content --config <file> [--app <folder>]",
|
|
20
|
+
" site-editor version",
|
|
21
|
+
].join("\n");
|
|
22
|
+
|
|
23
|
+
/** A command line that is not one of the three; printed with the usage. */
|
|
24
|
+
class UsageError extends Error {}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The options after the command, by name; only those named are taken.
|
|
28
|
+
* @param {string[]} args
|
|
29
|
+
* @param {string[]} names
|
|
30
|
+
* @returns {Record<string, string | undefined>}
|
|
31
|
+
*/
|
|
32
|
+
function options(args, names) {
|
|
33
|
+
try {
|
|
34
|
+
const { values } = parseArgs({
|
|
35
|
+
args, strict: true, allowPositionals: false,
|
|
36
|
+
options: Object.fromEntries(names.map((name) => [name, { type: "string" }])),
|
|
37
|
+
});
|
|
38
|
+
return /** @type {Record<string, string | undefined>} */ (values);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
throw new UsageError(/** @type {Error} */ (err).message);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Run one command line; resolves to the exit code.
|
|
46
|
+
* @param {string[]} argv the arguments after the command's name
|
|
47
|
+
* @returns {Promise<number>}
|
|
48
|
+
*/
|
|
49
|
+
async function main(argv) {
|
|
50
|
+
const [command, ...rest] = argv;
|
|
51
|
+
switch (command) {
|
|
52
|
+
case "copy": {
|
|
53
|
+
const { config, out } = options(rest, ["config", "out"]);
|
|
54
|
+
if (!config || !out) throw new UsageError("site-editor copy needs --config <file> and --out <folder>.");
|
|
55
|
+
const done = await copyEditor({ configFile: config, out });
|
|
56
|
+
console.log(`Copied the editor (version ${done.version}) into ${done.out}.`);
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
case "check-content": {
|
|
60
|
+
const given = options(rest, ["config", "app"]);
|
|
61
|
+
if (!given.config) throw new UsageError("site-editor check-content needs --config <file>.");
|
|
62
|
+
const { config, file } = await loadSiteConfig(given.config);
|
|
63
|
+
const problems = checkContent(path.resolve(given.app ?? path.dirname(file)), config);
|
|
64
|
+
for (const p of problems) console.error(p);
|
|
65
|
+
if (problems.length > 0) {
|
|
66
|
+
console.error(`\n${problems.length} content problem${problems.length === 1 ? "" : "s"}.`);
|
|
67
|
+
return 1;
|
|
68
|
+
}
|
|
69
|
+
console.log("Content OK.");
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
case "version":
|
|
73
|
+
options(rest, []);
|
|
74
|
+
console.log(packageVersion());
|
|
75
|
+
return 0;
|
|
76
|
+
default:
|
|
77
|
+
throw new UsageError(command === undefined ? "Name a command." : `There is no command "${command}".`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
83
|
+
} catch (err) {
|
|
84
|
+
if (err instanceof UsageError) {
|
|
85
|
+
console.error(`${err.message}\n\n${USAGE}`);
|
|
86
|
+
} else if (err instanceof CommandError) {
|
|
87
|
+
console.error(err.message);
|
|
88
|
+
} else {
|
|
89
|
+
console.error(`site-editor stopped: ${/** @type {Error} */ (err)?.message ?? err}`);
|
|
90
|
+
}
|
|
91
|
+
process.exitCode = 1;
|
|
92
|
+
}
|