@bright-logic-digital/site-editor 0.0.0-stage → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/LICENSE +4 -0
  3. package/README.md +318 -2
  4. package/bin/cli.js +92 -0
  5. package/bin/copy-editor.js +211 -0
  6. package/bin/node-version.js +20 -0
  7. package/bin/site-config.js +46 -0
  8. package/bin/site-editor.js +16 -0
  9. package/editor/admin.css +438 -0
  10. package/editor/api.js +138 -0
  11. package/editor/app.js +1178 -0
  12. package/editor/asset-map.js +38 -0
  13. package/editor/boot.js +72 -0
  14. package/editor/copy.js +239 -0
  15. package/editor/drafts.js +67 -0
  16. package/editor/format.js +80 -0
  17. package/editor/forms.js +918 -0
  18. package/editor/images.js +212 -0
  19. package/editor/index.html +27 -0
  20. package/editor/model.js +152 -0
  21. package/editor/pages.js +297 -0
  22. package/editor/photos.js +232 -0
  23. package/editor/publish.js +45 -0
  24. package/editor/shared/rules.js +118 -0
  25. package/editor/shared/site.js +128 -0
  26. package/editor/shared/validate.js +303 -0
  27. package/editor/signin.js +154 -0
  28. package/editor/status.js +90 -0
  29. package/editor/uploads.js +91 -0
  30. package/guide/check.js +233 -0
  31. package/hosts/next.js +19 -0
  32. package/hosts/pages.js +20 -0
  33. package/hosts/shared.js +42 -0
  34. package/package.json +91 -4
  35. package/server/blob-sha.js +10 -0
  36. package/server/changes.js +24 -0
  37. package/server/check-content.js +197 -0
  38. package/server/config.js +97 -0
  39. package/server/dispatch.js +207 -0
  40. package/server/github.js +149 -0
  41. package/server/handlers.js +364 -0
  42. package/server/index.js +153 -0
  43. package/server/local-repo.js +171 -0
  44. package/server/mapped-repo.js +48 -0
  45. package/server/memory-repo.js +104 -0
  46. package/server/path-map.js +26 -0
  47. package/server/signin/access.js +329 -0
  48. package/server/signin/code-store.sql +60 -0
  49. package/server/signin/code.js +192 -0
  50. package/server/signin/codes.js +171 -0
  51. package/server/signin/refusal.js +76 -0
  52. package/server/signin/session.js +90 -0
  53. package/tests/fixtures/image-files.json +14 -0
  54. package/tests/fixtures/slugs.json +202 -0
  55. package/tests/fixtures/validation.json +12734 -0
  56. package/types/bin/cli.d.ts +1 -0
  57. package/types/bin/copy-editor.d.ts +32 -0
  58. package/types/bin/node-version.d.ts +8 -0
  59. package/types/bin/site-config.d.ts +17 -0
  60. package/types/bin/site-editor.d.ts +2 -0
  61. package/types/editor/api.d.ts +37 -0
  62. package/types/editor/copy.d.ts +153 -0
  63. package/types/editor/shared/rules.d.ts +33 -0
  64. package/types/editor/shared/site.d.ts +35 -0
  65. package/types/editor/shared/validate.d.ts +19 -0
  66. package/types/guide/check.d.ts +31 -0
  67. package/types/hosts/next.d.ts +12 -0
  68. package/types/hosts/pages.d.ts +9 -0
  69. package/types/hosts/shared.d.ts +21 -0
  70. package/types/server/blob-sha.d.ts +1 -0
  71. package/types/server/changes.d.ts +2 -0
  72. package/types/server/check-content.d.ts +16 -0
  73. package/types/server/config.d.ts +46 -0
  74. package/types/server/dispatch.d.ts +51 -0
  75. package/types/server/github.d.ts +27 -0
  76. package/types/server/handlers.d.ts +116 -0
  77. package/types/server/index.d.ts +75 -0
  78. package/types/server/local-repo.d.ts +16 -0
  79. package/types/server/mapped-repo.d.ts +10 -0
  80. package/types/server/memory-repo.d.ts +28 -0
  81. package/types/server/path-map.d.ts +3 -0
  82. package/types/server/signin/access.d.ts +68 -0
  83. package/types/server/signin/code.d.ts +44 -0
  84. package/types/server/signin/codes.d.ts +98 -0
  85. package/types/server/signin/refusal.d.ts +122 -0
  86. package/types/server/signin/session.d.ts +29 -0
@@ -0,0 +1,438 @@
1
+ /* The editor. Phone first, one column, system fonts. Every control is at
2
+ least 44px tall and every input uses 16px text or larger (smaller text
3
+ makes phones zoom in on focus). */
4
+
5
+ :root {
6
+ --ink: #1d2b24;
7
+ --muted: #4d5c55;
8
+ --line: #c4cec9;
9
+ --paper: #ffffff;
10
+ --wash: #f2f5f3;
11
+ --brand: #1f5c3f;
12
+ --brand-ink: #ffffff;
13
+ --focus: #1a5fb4;
14
+ --danger: #a1261b;
15
+ --danger-wash: #fbeceb;
16
+ --notice-wash: #eaf3ee;
17
+ --radius: 8px;
18
+ --tap: 44px;
19
+ /* Room kept at the end of the page so the publish bar never covers the
20
+ last field. */
21
+ --bar-room: 11rem;
22
+ color-scheme: light;
23
+ }
24
+
25
+ *, *::before, *::after { box-sizing: border-box; }
26
+
27
+ html {
28
+ -webkit-text-size-adjust: 100%;
29
+ text-size-adjust: 100%;
30
+ /* Fields scrolled into view on focus stop clear of the publish bar. */
31
+ scroll-padding-bottom: var(--bar-room);
32
+ }
33
+
34
+ body {
35
+ margin: 0;
36
+ background: var(--wash);
37
+ color: var(--ink);
38
+ font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
39
+ font-size: 1rem;
40
+ line-height: 1.5;
41
+ }
42
+
43
+ h1, h2, h3, h4, h5, h6, p { margin: 0; }
44
+ h1 { font-size: 1.5rem; line-height: 1.25; margin: 0.5rem 0 1rem; }
45
+
46
+ a { color: var(--brand); }
47
+
48
+ :focus-visible {
49
+ outline: 3px solid var(--focus);
50
+ outline-offset: 2px;
51
+ }
52
+ h1:focus, main:focus, .alert:focus, .problem:focus, .confirm-text:focus,
53
+ .problems-other:focus { outline: none; }
54
+ h1:focus-visible, .alert:focus-visible, .problem:focus-visible,
55
+ .confirm-text:focus-visible, .problems-other:focus-visible {
56
+ outline: 3px solid var(--focus);
57
+ outline-offset: 2px;
58
+ }
59
+
60
+ .top {
61
+ display: flex;
62
+ align-items: center;
63
+ justify-content: space-between;
64
+ gap: 1rem;
65
+ background: var(--brand);
66
+ color: var(--brand-ink);
67
+ padding: 0.25rem 1rem;
68
+ min-height: 3.25rem;
69
+ }
70
+ .brand { font-weight: 700; min-width: 0; }
71
+ /* "Help", on every screen: opens the owner's guide in a new tab so the
72
+ page being edited stays as it is. */
73
+ .help-link {
74
+ flex: none;
75
+ display: inline-flex;
76
+ align-items: center;
77
+ min-height: var(--tap);
78
+ padding: 0 0.875rem;
79
+ border: 1px solid var(--brand-ink);
80
+ border-radius: 6px;
81
+ color: var(--brand-ink);
82
+ font-weight: 700;
83
+ text-decoration: none;
84
+ }
85
+ .top :focus-visible { outline-color: var(--brand-ink); }
86
+ .top-actions { display: flex; flex: none; gap: 0.5rem; }
87
+ /* "Sign out" looks like "Help" beside it. */
88
+ .sign-out { background: none; font: inherit; font-weight: 700; cursor: pointer; }
89
+ .sign-out[hidden] { display: none; }
90
+
91
+ .main {
92
+ max-width: 40rem;
93
+ margin: 0 auto;
94
+ padding: 1rem 1rem var(--bar-room);
95
+ }
96
+
97
+ .lead { margin-bottom: 1rem; }
98
+ .loading { color: var(--muted); padding: 2rem 0; }
99
+
100
+ /* Home */
101
+
102
+ .pages { list-style: none; margin: 0; padding: 0; }
103
+ .pages li + li { margin-top: 0.5rem; }
104
+ .page-link {
105
+ display: flex;
106
+ flex-wrap: wrap;
107
+ align-items: center;
108
+ justify-content: space-between;
109
+ gap: 0.25rem 0.75rem;
110
+ min-height: 3.5rem;
111
+ padding: 0.75rem 1rem;
112
+ background: var(--paper);
113
+ border: 1px solid var(--line);
114
+ border-radius: var(--radius);
115
+ color: var(--ink);
116
+ font-size: 1.125rem;
117
+ font-weight: 600;
118
+ text-decoration: none;
119
+ }
120
+ .page-link:hover { border-color: var(--brand); }
121
+ .badge {
122
+ font-size: 0.875rem;
123
+ font-weight: 600;
124
+ color: var(--brand);
125
+ background: var(--notice-wash);
126
+ border-radius: 999px;
127
+ padding: 0.125rem 0.625rem;
128
+ }
129
+
130
+ /* A page */
131
+
132
+ .back {
133
+ display: inline-flex;
134
+ align-items: center;
135
+ min-height: var(--tap);
136
+ font-weight: 600;
137
+ }
138
+
139
+ .notice, .alert {
140
+ border-radius: var(--radius);
141
+ padding: 0.75rem 1rem;
142
+ margin-bottom: 1rem;
143
+ }
144
+ .notice { background: var(--notice-wash); }
145
+ .notice.warn, .alert {
146
+ background: var(--danger-wash);
147
+ border: 1px solid var(--danger);
148
+ }
149
+ .alert p { font-weight: 600; }
150
+
151
+ .group, .loose {
152
+ background: var(--paper);
153
+ border: 1px solid var(--line);
154
+ border-radius: var(--radius);
155
+ margin-bottom: 0.75rem;
156
+ }
157
+ .loose { padding: 0 1rem; }
158
+ .summary {
159
+ display: flex;
160
+ align-items: center;
161
+ min-height: 3.5rem;
162
+ padding: 0.5rem 1rem;
163
+ cursor: pointer;
164
+ font-size: 1.125rem;
165
+ font-weight: 700;
166
+ }
167
+ .group-body { padding: 0 1rem 0.5rem; }
168
+ .group-body > .help { margin-bottom: 0.5rem; }
169
+
170
+ .field { padding: 0.75rem 0; }
171
+ .label, .legend {
172
+ display: block;
173
+ font-weight: 700;
174
+ margin-bottom: 0.25rem;
175
+ }
176
+ .help { color: var(--muted); font-size: 0.9375rem; margin-bottom: 0.375rem; }
177
+ .hint { margin: 0.375rem 0 0; }
178
+
179
+ .input {
180
+ display: block;
181
+ width: 100%;
182
+ min-height: var(--tap);
183
+ padding: 0.5rem 0.75rem;
184
+ border: 1px solid var(--muted);
185
+ border-radius: 6px;
186
+ background: var(--paper);
187
+ color: var(--ink);
188
+ font: inherit;
189
+ font-size: 1rem;
190
+ }
191
+ textarea.input {
192
+ min-height: 8rem;
193
+ resize: vertical;
194
+ field-sizing: content;
195
+ }
196
+ /* A long single-line field: one line tall, growing to show all its text
197
+ (the form script sets its height), never scrolled sideways. */
198
+ textarea.input.single {
199
+ min-height: var(--tap);
200
+ resize: none;
201
+ overflow: hidden;
202
+ overflow-wrap: anywhere;
203
+ }
204
+ select.input { appearance: auto; }
205
+ .input[aria-invalid="true"] { border: 2px solid var(--danger); }
206
+
207
+ .check {
208
+ display: flex;
209
+ align-items: center;
210
+ gap: 0.75rem;
211
+ min-height: var(--tap);
212
+ }
213
+ .checkbox { width: 1.5rem; height: 1.5rem; margin: 0; }
214
+ .check label { flex: 1; padding: 0.625rem 0; font-weight: 700; }
215
+
216
+ .count { color: var(--muted); font-size: 0.9375rem; margin-top: 0.25rem; }
217
+ .count:empty { display: none; }
218
+ .count.over { color: var(--danger); font-weight: 700; }
219
+
220
+ .problem {
221
+ color: var(--danger);
222
+ font-weight: 700;
223
+ margin-bottom: 0.375rem;
224
+ }
225
+ .problems-other {
226
+ background: var(--danger-wash);
227
+ border: 1px solid var(--danger);
228
+ border-radius: var(--radius);
229
+ padding: 0.75rem 1rem;
230
+ margin-bottom: 0.75rem;
231
+ }
232
+
233
+ /* Live regions stay rendered when empty, or a screen reader may miss what
234
+ is written into them. */
235
+ .note { color: var(--danger); font-size: 0.9375rem; margin-top: 0.25rem; }
236
+
237
+ /* Buttons */
238
+
239
+ .button {
240
+ display: inline-flex;
241
+ align-items: center;
242
+ justify-content: center;
243
+ min-height: var(--tap);
244
+ min-width: var(--tap);
245
+ padding: 0.5rem 1rem;
246
+ border: 1px solid var(--brand);
247
+ border-radius: 6px;
248
+ background: var(--paper);
249
+ color: var(--brand);
250
+ font: inherit;
251
+ font-weight: 600;
252
+ text-decoration: none;
253
+ cursor: pointer;
254
+ touch-action: manipulation;
255
+ }
256
+ .button:disabled {
257
+ border-color: var(--line);
258
+ color: var(--muted);
259
+ background: var(--wash);
260
+ cursor: not-allowed;
261
+ }
262
+ .button.primary {
263
+ background: var(--brand);
264
+ color: var(--brand-ink);
265
+ }
266
+ .button.primary:disabled {
267
+ background: var(--line);
268
+ border-color: var(--line);
269
+ color: var(--ink);
270
+ }
271
+ .button.danger { border-color: var(--danger); color: var(--danger); }
272
+
273
+ .actions, .format {
274
+ display: flex;
275
+ flex-wrap: wrap;
276
+ gap: 0.5rem;
277
+ margin-top: 0.5rem;
278
+ }
279
+ .format-button { padding: 0.5rem 0.875rem; }
280
+
281
+ /* Lists */
282
+
283
+ .list, .object {
284
+ border: 0;
285
+ margin: 0;
286
+ padding: 0.75rem 0;
287
+ min-width: 0;
288
+ }
289
+ .list > .help, .object > .help { margin-bottom: 0.5rem; }
290
+ .item {
291
+ border: 1px solid var(--line);
292
+ border-left: 4px solid var(--brand);
293
+ border-radius: var(--radius);
294
+ padding: 0.75rem;
295
+ margin-bottom: 0.75rem;
296
+ }
297
+ .item-name { font-size: 1rem; font-weight: 700; }
298
+ .item-actions { border-top: 1px solid var(--line); padding-top: 0.75rem; }
299
+ .add { width: 100%; }
300
+ .confirm {
301
+ border-top: 1px solid var(--line);
302
+ padding-top: 0.75rem;
303
+ margin-top: 0.5rem;
304
+ }
305
+ .confirm-text { font-weight: 700; }
306
+ .discard-area { margin-top: 1.5rem; }
307
+
308
+ /* Photos */
309
+
310
+ .photo-preview {
311
+ display: block;
312
+ width: 100%;
313
+ max-width: 28rem;
314
+ height: auto;
315
+ border-radius: 6px;
316
+ background: var(--wash);
317
+ }
318
+ .photo-none {
319
+ display: flex;
320
+ align-items: center;
321
+ justify-content: center;
322
+ aspect-ratio: 3 / 2;
323
+ border: 1px dashed var(--line);
324
+ color: var(--muted);
325
+ }
326
+ .photo .field { padding-bottom: 0; }
327
+ .photo .notice { margin: 0 0 0.5rem; }
328
+ .photo-actions { margin-top: 0.5rem; }
329
+ /* The file picker opens from its button; the input itself is never seen or
330
+ reached with the keyboard, but stays rendered so the button can open it. */
331
+ .photo-file {
332
+ position: absolute;
333
+ width: 1px;
334
+ height: 1px;
335
+ margin: 0;
336
+ padding: 0;
337
+ border: 0;
338
+ opacity: 0;
339
+ overflow: hidden;
340
+ clip-path: inset(50%);
341
+ pointer-events: none;
342
+ }
343
+
344
+ /* Simple pages ("Extra pages"): Home's list and "Add a page", and a
345
+ page's "Delete this page". A title may be one long word. */
346
+
347
+ h1, .page-name, .confirm-text, .address { overflow-wrap: anywhere; }
348
+ .page-name { min-width: 0; }
349
+ .extra { margin-top: 2rem; }
350
+ .version { color: var(--muted); font-size: 0.875rem; margin-top: 2rem; }
351
+ .extra h2 { font-size: 1.25rem; line-height: 1.3; margin-bottom: 0.75rem; }
352
+ .add-area { margin-top: 0.75rem; }
353
+ .add-page { width: 100%; }
354
+ .cap { margin: 0.5rem 0 0; }
355
+ .add-page-form {
356
+ background: var(--paper);
357
+ border: 1px solid var(--line);
358
+ border-radius: var(--radius);
359
+ padding: 1rem;
360
+ }
361
+ .address { font-weight: 600; margin-top: 0.5rem; }
362
+ .address-preview .help { margin: 0.25rem 0 0; }
363
+ .delete-area {
364
+ margin-top: 1.5rem;
365
+ padding-top: 1.5rem;
366
+ border-top: 1px solid var(--line);
367
+ }
368
+
369
+ /* The publish bar */
370
+
371
+ .bar {
372
+ position: fixed;
373
+ left: 0;
374
+ right: 0;
375
+ bottom: 0;
376
+ background: var(--paper);
377
+ border-top: 1px solid var(--line);
378
+ box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
379
+ padding-bottom: env(safe-area-inset-bottom);
380
+ }
381
+ .bar.is-empty { border-top: 0; box-shadow: none; }
382
+ .bar-inner {
383
+ max-width: 40rem;
384
+ margin: 0 auto;
385
+ padding: 0.75rem 1rem;
386
+ display: flex;
387
+ flex-direction: column;
388
+ gap: 0.5rem;
389
+ }
390
+ .bar.is-empty .bar-inner { padding: 0; }
391
+ .status { font-weight: 600; }
392
+ .status .see-page { margin-left: 0.5rem; vertical-align: middle; }
393
+ .bar-alert .alert { margin-bottom: 0; }
394
+ .publish { width: 100%; font-size: 1.125rem; }
395
+
396
+ /* The owner's guide (guide.html): a plain page of short sections. */
397
+
398
+ .guide { padding-bottom: 3rem; overflow-wrap: break-word; }
399
+ .guide h2 {
400
+ font-size: 1.25rem;
401
+ line-height: 1.3;
402
+ margin: 2rem 0 0.5rem;
403
+ padding-top: 1rem;
404
+ border-top: 1px solid var(--line);
405
+ }
406
+ .guide h3 { font-size: 1.0625rem; margin: 1.25rem 0 0.375rem; }
407
+ .guide p, .guide li { margin-bottom: 0.5rem; }
408
+ .guide ol, .guide ul { margin: 0 0 0.75rem; padding-left: 1.5rem; }
409
+ .guide .contents { padding-left: 1.25rem; }
410
+ .guide .contents li { margin: 0; }
411
+ /* A block link keeps the bullet beside its first line when it wraps, and
412
+ its padding makes each entry at least 44px tall. */
413
+ .guide .contents a {
414
+ display: block;
415
+ padding: 0.625rem 0;
416
+ font-weight: 600;
417
+ }
418
+ .guide .notice { margin: 0.75rem 0; }
419
+ .guide .back { margin-top: 1rem; }
420
+
421
+ @media (min-width: 48em) {
422
+ h1 { font-size: 1.75rem; }
423
+ .bar-inner {
424
+ flex-direction: row;
425
+ align-items: center;
426
+ justify-content: space-between;
427
+ }
428
+ .publish { width: auto; min-width: 12rem; margin-left: auto; }
429
+ .add, .add-page { width: auto; }
430
+ }
431
+
432
+ @media (prefers-reduced-motion: reduce) {
433
+ *, *::before, *::after {
434
+ animation: none !important;
435
+ transition: none !important;
436
+ scroll-behavior: auto !important;
437
+ }
438
+ }
package/editor/api.js ADDED
@@ -0,0 +1,138 @@
1
+ // Client for the editor's API. `fetch` is passed in.
2
+
3
+ export class ApiError extends Error {
4
+ constructor({ status = 0, code, message, problems, paths }) {
5
+ super(message);
6
+ this.name = "ApiError";
7
+ this.status = status;
8
+ this.code = code;
9
+ if (problems !== undefined) this.problems = problems;
10
+ if (paths !== undefined) this.paths = paths;
11
+ }
12
+ }
13
+
14
+ export class SessionExpired extends Error {
15
+ constructor() {
16
+ super("Your sign-in has expired. Sign in again to keep editing.");
17
+ this.name = "SessionExpired";
18
+ }
19
+ }
20
+
21
+ const OFFLINE = "The editor cannot reach the site. Check your internet connection and try again.";
22
+ const UPSTREAM = "The site sent back something the editor could not read. Try again in a minute.";
23
+ const UNCONFIRMED = "The site could not confirm the sign-in.";
24
+ const REQUEST_TIMEOUT_MS = 30 * 1000;
25
+ const DEPLOYED_TIMEOUT_MS = 10 * 1000;
26
+ /** A photo file of up to 2 MB, sent from a phone on a slow connection. */
27
+ export const UPLOAD_TIMEOUT_MS = 120 * 1000;
28
+
29
+ const offline = () => new ApiError({ status: 0, code: "offline", message: OFFLINE });
30
+
31
+ /** `timers` ({setTimeout, clearTimeout}) is passed in so tests need not wait.
32
+ * `signIn` is the site config's: "code" (the editor's own sign-in) or
33
+ * "access" (a sign-in in front of the site). */
34
+ export function createApi(fetch, timers = globalThis, { signIn = "code" } = {}) {
35
+ const access = signIn === "access";
36
+ async function send(url, init) {
37
+ let res;
38
+ try {
39
+ res = await fetch(url, {
40
+ redirect: "manual", credentials: "same-origin", cache: "no-store", ...init,
41
+ });
42
+ } catch {
43
+ throw offline();
44
+ }
45
+ // A redirect is never followed. With a sign-in in front of the site
46
+ // (signIn "access"), it is that sign-in sending to its login page: the
47
+ // sign-in has ended. With the editor's own sign-in nothing here
48
+ // redirects, so it is something in front of the site.
49
+ if (res.type === "opaqueredirect" || (res.status >= 300 && res.status < 400)) {
50
+ if (access) throw new SessionExpired();
51
+ throw new ApiError({ status: res.status, code: "upstream", message: UPSTREAM });
52
+ }
53
+ // Past a sign-in in front of the site, a 401 from the API means the
54
+ // sign-in could not be confirmed this time.
55
+ if (access && res.status === 401) {
56
+ throw new ApiError({ status: 401, code: "unauthorized", message: UNCONFIRMED });
57
+ }
58
+ let body;
59
+ try {
60
+ body = await res.json();
61
+ } catch {
62
+ body = undefined;
63
+ }
64
+ // Not signed in, or the sign-in has ended. Any other 401 (a wrong code)
65
+ // is an ordinary refusal.
66
+ if (res.status === 401 && body !== null && typeof body === "object" && body.error === "unauthorized") {
67
+ throw new SessionExpired();
68
+ }
69
+ if (!res.ok) {
70
+ if (body !== null && typeof body === "object" && typeof body.error === "string") {
71
+ throw new ApiError({
72
+ status: res.status, code: body.error,
73
+ message: typeof body.message === "string" ? body.message : UPSTREAM,
74
+ problems: body.problems, paths: body.paths,
75
+ });
76
+ }
77
+ throw new ApiError({ status: res.status, code: "upstream", message: UPSTREAM });
78
+ }
79
+ if (body === undefined) {
80
+ throw new ApiError({ status: res.status, code: "upstream", message: UPSTREAM });
81
+ }
82
+ return body;
83
+ }
84
+
85
+ /** send(), aborted and rejected as offline when the answer (body
86
+ * included) has not arrived within timeoutMs. */
87
+ async function request(url, init = {}, timeoutMs = REQUEST_TIMEOUT_MS) {
88
+ const controller = new AbortController();
89
+ let timer;
90
+ const expired = new Promise((resolve, reject) => {
91
+ timer = timers.setTimeout(() => {
92
+ controller.abort();
93
+ reject(offline());
94
+ }, timeoutMs);
95
+ });
96
+ try {
97
+ return await Promise.race([send(url, { ...init, signal: controller.signal }), expired]);
98
+ } finally {
99
+ timers.clearTimeout(timer);
100
+ }
101
+ }
102
+
103
+ const q = encodeURIComponent;
104
+ const postJson = (url, body) => request(url, {
105
+ method: "POST",
106
+ headers: { "Content-Type": "application/json" },
107
+ body: JSON.stringify(body),
108
+ });
109
+ return {
110
+ state: () => request("/api/admin/state"),
111
+ doc: (path) => request(`/api/admin/doc?path=${q(path)}`),
112
+ publish: (body) => request("/api/admin/publish", {
113
+ method: "POST",
114
+ headers: { "Content-Type": "application/json" },
115
+ body: JSON.stringify(body),
116
+ }),
117
+ status: (mine, deployed) =>
118
+ request(`/api/admin/status?mine=${q(mine)}&deployed=${q(deployed)}`),
119
+ upload: (name, base64) => request(`/api/admin/upload?name=${q(name)}`, {
120
+ method: "POST",
121
+ headers: { "Content-Type": "text/plain" },
122
+ body: base64,
123
+ }, UPLOAD_TIMEOUT_MS),
124
+ async deployed() {
125
+ const body = await request("/version.json", {}, DEPLOYED_TIMEOUT_MS);
126
+ if (body === null || typeof body.commit !== "string") {
127
+ throw new ApiError({ status: 200, code: "upstream", message: UPSTREAM });
128
+ }
129
+ return body.commit;
130
+ },
131
+ /** Sign-in by emailed code; verify sets the session cookie. */
132
+ session: {
133
+ request: (email) => postJson("/api/admin/session/request", { email }),
134
+ verify: (email, code) => postJson("/api/admin/session/verify", { email, code }),
135
+ signOut: () => request("/api/admin/session/signout", { method: "POST" }),
136
+ },
137
+ };
138
+ }